From ca68a64ab37af8606d7e6094ca87c4fcc76a8345 Mon Sep 17 00:00:00 2001 From: Danny McGee Date: Wed, 28 Feb 2024 04:05:32 -0500 Subject: [PATCH] Wire up reactive updates --- image-generator-demo/src/app/app.element.ts | 50 ++++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/image-generator-demo/src/app/app.element.ts b/image-generator-demo/src/app/app.element.ts index 9e0ab67..2825ea8 100644 --- a/image-generator-demo/src/app/app.element.ts +++ b/image-generator-demo/src/app/app.element.ts @@ -7,7 +7,7 @@ import { StudioMode, } from "@storyteller/studio"; import { match } from "@storyteller/utility"; -import { LitElement, html, nothing } from "lit"; +import { LitElement, type PropertyValues, html, nothing } from "lit"; import { customElement, state } from "lit/decorators.js"; import { styleMap } from "lit/directives/style-map.js"; import { classMap } from "lit/directives/class-map.js"; @@ -40,6 +40,19 @@ export class AppElement extends LitElement { super.disconnectedCallback(); } + protected override willUpdate(changes: PropertyValues): void { + if (changes.has("prompt") || changes.has("seed") || changes.has("imageCaptureUrl")) { + // TODO: Send parameters to model + console.log("changes:", { + prompt: this.prompt, + seed: this.seed, + imageCaptureUrl: this.imageCaptureUrl, + }); + } + + super.willUpdate(changes); + } + @on("scene-state") onSceneStateChange({ detail: state }: SceneStateEvent): void { console.log("scene state change:", SceneState[state]); @@ -74,6 +87,25 @@ export class AppElement extends LitElement { URL.revokeObjectURL(prevUrl); } + updatePrompt(event: Event): void { + this.prompt = (event.target as HTMLInputElement).value; + } + + refreshSeed(): void { + this.seed = randomSeed(); + } + + stopTextInputBubbling(event: KeyboardEvent): void { + // We want modifier keys like "Alt" to always be received by the Bevy + // canvas because they affect the camera controller behavior, but we do + // NOT want Bevy to receive typing inputs like W, A, S, and D while the + // user is trying to type into the Prompt field. + if (/^(Alt|Control|Shift)$/.test(event.key)) + return; + + event.stopPropagation(); + } + onResizeStart(): void { this.resizing = true; } @@ -87,10 +119,6 @@ export class AppElement extends LitElement { this.hierarchyWidth = Math.max(Math.min(updated, 512), 128); } - refreshSeed(): void { - this.seed = randomSeed(); - } - protected override render = () => html`
event.stopPropagation()} - @keyup=${(event: KeyboardEvent) => event.stopPropagation()} + @keydown=${this.stopTextInputBubbling} + @keyup=${this.stopTextInputBubbling} > { + if (event.key === "Enter") { + event.preventDefault(); + this.updatePrompt(event); + } + }} />