Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: improve TextInput editor visual and enable UIMask clipping in editor

esengine8827aab037e0
diff --git a/editor/src/panels/HierarchyPanel.ts b/editor/src/panels/HierarchyPanel.ts
index de4755766..43f96f73f 100644
--- a/editor/src/panels/HierarchyPanel.ts
+++ b/editor/src/panels/HierarchyPanel.ts
@@ -709,7 +709,12 @@ export class HierarchyPanel {
private createTextInputEntity(parent: Entity | null): void {
const newEntity = this.store_.createEntity('TextInput', parent);
this.store_.addComponent(newEntity, 'LocalTransform', getDefaultComponentData('LocalTransform'));
- this.store_.addComponent(newEntity, 'Sprite', getDefaultComponentData('Sprite'));
+ const tiDefaults = getDefaultComponentData('TextInput');
+ this.store_.addComponent(newEntity, 'Sprite', {
+ ...getDefaultComponentData('Sprite'),
+ color: tiDefaults.backgroundColor,
+ size: { x: 200, y: 36 },
+ });
this.store_.addComponent(newEntity, 'UIRect', {
...getDefaultComponentData('UIRect'),
size: { x: 200, y: 36 },
diff --git a/editor/src/scene/EditorSceneManager.ts b/editor/src/scene/EditorSceneManager.ts
index fa2dbcb95..0ab11f8aa 100644
--- a/editor/src/scene/EditorSceneManager.ts
+++ b/editor/src/scene/EditorSceneManager.ts
@@ -16,11 +16,15 @@ import {
TextAlign,
TextVerticalAlign,
TextOverflow,
+ UIRect,
+ UIMask,
type SceneComponentData,
type TextData,
type BitmapTextData,
type LocalTransformData,
type WorldTransformData,
+ type UIRectData,
+ type UIMaskData,
} from 'esengine';
import type { SpineModuleController } from 'esengine/spine';
import { submitSpineMeshesToCore } from 'esengine/spine';
@@ -191,10 +195,14 @@ export class EditorSceneManager {
break;
case 'UIRect':
+ this.world_.insert(entity, UIRect, comp.data as unknown as UIRectData);
+ break;
+ case 'UIMask':
+ this.world_.insert(entity, UIMask, comp.data as unknown as UIMaskData);
+ break;
case 'Interactable':
case 'Button':
case 'ScreenSpace':
- case 'UIMask':
case 'TextInput':
break;
diff --git a/editor/src/store/EditorStore.ts b/editor/src/store/EditorStore.ts
index 8cce2ad78..c6f9ea263 100644
--- a/editor/src/store/EditorStore.ts
+++ b/editor/src/store/EditorStore.ts
@@ -882,6 +882,14 @@ export class EditorStore {
}
private notifyPropertyChange(event: PropertyChangeEvent): void {
+ if (event.componentType === 'TextInput' && event.propertyName === 'backgroundColor') {
+ const entityData = this.entityMap_.get(event.entity);
+ const spriteComp = entityData?.components.find(c => c.type === 'Sprite');
+ if (spriteComp) {
+ spriteComp.data.color = event.newValue;
+ }
+ }
+
for (const listener of this.propertyListeners_) {
listener(event);
}
diff --git a/sdk/src/ui/TextInputPlugin.ts b/sdk/src/ui/TextInputPlugin.ts
new file mode 100644
index 000000000..a6a165639
--- /dev/null
+++ b/sdk/src/ui/TextInputPlugin.ts
@@ -0,0 +1,375 @@
+import type { App, Plugin } from '../app';
+import type { Entity } from '../types';
+import { INVALID_TEXTURE } from '../types';
+import { defineSystem, Schedule } from '../system';
+import { Sprite, type SpriteData } from '../component';
+import { TextInput, type TextInputData } from './TextInput';
+import { UIRect, type UIRectData } from './UIRect';
+import { Interactable } from './Interactable';
+import { UIInteraction, type UIInteractionData } from './UIInteraction';
+import { UIEvents, UIEventQueue } from './UIEvents';
+import { Res } from '../resource';
+import { platformCreateCanvas } from '../platform';
+
+export class TextInputPlugin implements Plugin {
+ build(app: App): void {
+ const module = app.wasmModule;
+ if (!module) {
+ console.warn('TextInputPlugin: No WASM module available');
+ return;
+ }
+
+ const world = app.world;
+ const textureCache = new Map<Entity, number>();
+ const canvas = platformCreateCanvas(512, 64);
+ const ctx = canvas.getContext('2d', { willReadFrequently: true })! as
+ CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
+
+ let focusedEntity: Entity | null = null;
+ let composing = false;
+ let cursorVisible = true;
+ let cursorTimer = 0;
+ let lastTime = 0;
+
+ const textarea = createHiddenTextarea();
+
+ textarea.addEventListener('input', () => {
+ if (composing || focusedEntity === null) return;
+ syncFromTextarea();
+ });
+
+ textarea.addEventListener('compositionstart', () => {
+ composing = true;
+ });
+
+ textarea.addEventListener('compositionend', () => {
+ composing = false;
+ syncFromTextarea();
+ });
+
+ textarea.addEventListener('keydown', (e: KeyboardEvent) => {
+ if (focusedEntity === null) return;
+
+ if (e.key === 'Escape') {
+ blurCurrent();
+ return;
+ }
+
+ const ti = world.get(focusedEntity, TextInput) as TextInputData;
+ if (e.key === 'Enter' && !ti.multiline) {
+ e.preventDefault();
+ const events = app.getResource(UIEvents) as UIEventQueue;
+ events.emit(focusedEntity, 'submit');
+ blurCurrent();
+ return;
+ }
+ });
+
+ textarea.addEventListener('blur', () => {
+ if (focusedEntity !== null && world.valid(focusedEntity) && world.has(focusedEntity, TextInput)) {
+ const ti = world.get(focusedEntity, TextInput) as TextInputData;
+ ti.focused = false;
+ ti.dirty = true;
+ }
+ focusedEntity = null;
+ });
+
+ function syncFromTextarea(): void {
+ if (focusedEntity === null || !world.valid(focusedEntity)) return;
+ const ti = world.get(focusedEntity, TextInput) as TextInputData;
+ if (ti.readOnly) return;
+
+ let val = textarea.value;
+ if (ti.maxLength > 0 && val.length > ti.maxLength) {
+ val = val.substring(0, ti.maxLength);
+ textarea.value = val;
+ }
+
+ if (val !== ti.value) {
+ ti.value = val;
+ const events = app.getResource(UIEvents) as UIEventQueue;
+ events.emit(focusedEntity, 'change');
+ }
+ ti.cursorPos = textarea.selectionStart ?? val.length;
+ ti.dirty = true;
+ resetCursorBlink();
+ }
+
+ function focusEntity(entity: Entity): void {
+ if (focusedEntity === entity) return;
+ blurCurrent();
+
+ const ti = world.get(entity, TextInput) as TextInputData;
+ if (ti.readOnly) return;
+
+ focusedEntity = entity;
+ ti.focused = true;
+ ti.dirty = true;
+
+ textarea.value = ti.value;
+ textarea.selectionStart = ti.cursorPos;
+ textarea.selectionEnd = ti.cursorPos;
+ textarea.focus();
+ resetCursorBlink();
+ }
+
+ function blurCurrent(): void {
+ if (focusedEntity !== null && world.valid(focusedEntity) && world.has(focusedEntity, TextInput)) {
+ const ti = world.get(focusedEntity, TextInput) as TextInputData;
+ ti.focused = false;
+ ti.dirty = true;
+ }
+ focusedEntity = null;
+ textarea.blur();
+ }
+
+ function resetCursorBlink(): void {
+ cursorVisible = true;
+ cursorTimer = 0;
+ }
+
+ // Focus system
+ app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
+ [Res(UIEvents)],
+ (events: UIEventQueue) => {
+ const textInputEntities = world.getEntitiesWithComponents([TextInput, Interactable]);
+
+ for (const entity of textInputEntities) {
+ if (!world.has(entity, UIInteraction)) continue;
+ const interaction = world.get(entity, UIInteraction) as UIInteractionData;
+ if (interaction.justPressed) {
+ focusEntity(entity);
+ return;
+ }
+ }
+
+ if (focusedEntity !== null) {
+ const allInteractables = world.getEntitiesWithComponents([Interactable]);
+ for (const entity of allInteractables) {
+ if (world.has(entity, TextInput)) continue;
+ if (!world.has(entity, UIInteraction)) continue;
+ const interaction = world.get(entity, UIInteraction) as UIInteractionData;
+ if (interaction.justPressed) {
+ blurCurrent();
+ return;
+ }
+ }
+ }
+ },
+ { name: 'TextInputFocusSystem' }
+ ));
+
+ // Render system
+ app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
+ [],
+ () => {
+ const now = typeof performance !== 'undefined' ? performance.now() : Date.now();
+ const dt = lastTime === 0 ? 0 : (now - lastTime) / 1000;
+ lastTime = now;
+
+ if (focusedEntity !== null) {
+ cursorTimer += dt;
+ if (cursorTimer >= 0.5) {
+ cursorTimer -= 0.5;
+ cursorVisible = !cursorVisible;
+ if (focusedEntity !== null && world.valid(focusedEntity) && world.has(focusedEntity, TextInput)) {
+ const ti = world.get(focusedEntity, TextInput) as TextInputData;
+ ti.dirty = true;
+ }
+ }
+ }
+
+ const rm = module!.getResourceManager();
+ for (const [e, tex] of textureCache) {
+ if (!world.valid(e) || !world.has(e, TextInput)) {
+ rm.releaseTexture(tex);
+ textureCache.delete(e);
+ }
+ }
+ const entities = world.getEntitiesWithComponents([TextInput, UIRect]);
+
+ for (const entity of entities) {
+ const ti = world.get(entity, TextInput) as TextInputData;
+ if (!ti.dirty) continue;
+
+ const uiRect = world.get(entity, UIRect) as UIRectData;
+ const w = Math.ceil(uiRect.size.x);
+ const h = Math.ceil(uiRect.size.y);
+ if (w <= 0 || h <= 0) continue;
+
+ if (!world.has(entity, Sprite)) {
+ world.insert(entity, Sprite, {
+ texture: INVALID_TEXTURE,
+ color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: w, y: h },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer: 0,
+ flipX: false,
+ flipY: false,
+ material: 0,
+ });
+ }
+
+ renderTextInput(entity, ti, w, h);
+ ti.dirty = false;
+ }
+ },
+ { name: 'TextInputRenderSystem' }
+ ));
+
+ function renderTextInput(entity: Entity, ti: TextInputData, w: number, h: number): void {
+ if (canvas.width < w || canvas.height < h) {
+ canvas.width = nextPowerOf2(Math.max(canvas.width, w));
+ canvas.height = nextPowerOf2(Math.max(canvas.height, h));
+ }
+
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
+
+ // Background
+ const bg = ti.backgroundColor;
+ ctx.fillStyle = `rgba(${Math.round(bg.r * 255)}, ${Math.round(bg.g * 255)}, ${Math.round(bg.b * 255)}, ${bg.a})`;
+ ctx.fillRect(0, 0, w, h);
+
+ ctx.save();
+ ctx.beginPath();
+ ctx.rect(ti.padding, 0, w - ti.padding * 2, h);
+ ctx.clip();
+
+ ctx.font = `${ti.fontSize}px ${ti.fontFamily}`;
+ ctx.textBaseline = 'top';
+ ctx.textAlign = 'left';
+
+ const isEmpty = ti.value.length === 0;
+ const displayText = isEmpty
+ ? ti.placeholder
+ : ti.password
+ ? '\u25CF'.repeat(ti.value.length)
+ : ti.value;
+
+ const textColor = isEmpty ? ti.placeholderColor : ti.color;
+ ctx.fillStyle = `rgba(${Math.round(textColor.r * 255)}, ${Math.round(textColor.g * 255)}, ${Math.round(textColor.b * 255)}, ${textColor.a})`;
+
+ const lineHeight = Math.ceil(ti.fontSize * 1.2);
+ const padding = ti.padding;
+
+ if (ti.multiline) {
+ const lines = wrapText(ctx, displayText, w - padding * 2);
+ let y = padding;
+ for (const line of lines) {
+ ctx.fillText(line, padding, y);
+ y += lineHeight;
+ }
+ } else {
+ const textY = (h - ti.fontSize) / 2;
+ ctx.fillText(displayText, padding, textY);
+ }
+
+ // Cursor
+ if (ti.focused && cursorVisible) {
+ const cursorText = ti.password
+ ? '\u25CF'.repeat(ti.cursorPos)
+ : ti.value.substring(0, ti.cursorPos);
+
+ let cursorX: number;
+ let cursorY: number;
+ let cursorH: number;
+
+ if (ti.multiline) {
+ const lines = wrapText(ctx, ti.value.substring(0, ti.cursorPos), w - padding * 2);
+ const lastLine = lines.length > 0 ? lines[lines.length - 1] : '';
+ cursorX = padding + ctx.measureText(lastLine).width;
+ cursorY = padding + (lines.length - 1) * lineHeight;
+ cursorH = lineHeight;
+ } else {
+ cursorX = padding + ctx.measureText(cursorText).width;
+ cursorY = (h - ti.fontSize) / 2;
+ cursorH = ti.fontSize;
+ }
+
+ ctx.fillStyle = `rgba(${Math.round(ti.color.r * 255)}, ${Math.round(ti.color.g * 255)}, ${Math.round(ti.color.b * 255)}, ${ti.color.a})`;
+ ctx.fillRect(cursorX, cursorY, 2, cursorH);
+ }
+
+ ctx.restore();
+
+ // Upload texture
+ const imageData = ctx.getImageData(0, 0, w, h);
+ const pixels = new Uint8Array(imageData.data.buffer);
+ const rm = module!.getResourceManager();
+ const ptr = module!._malloc(pixels.length);
+ module!.HEAPU8.set(pixels, ptr);
+
+ const existingTex = textureCache.get(entity);
+ if (existingTex !== undefined) {
+ rm.releaseTexture(existingTex);
+ }
+
+ const textureHandle = rm.createTexture(w, h, ptr, pixels.length, 1, true);
+ textureCache.set(entity, textureHandle);
+ module!._free(ptr);
+
+ const sprite = world.get(entity, Sprite) as SpriteData;
+ sprite.texture = textureHandle;
+ sprite.size.x = w;
+ sprite.size.y = h;
+ sprite.uvOffset.x = 0;
+ sprite.uvOffset.y = 0;
+ sprite.uvScale.x = 1;
+ sprite.uvScale.y = 1;
+ sprite.color = { r: 1, g: 1, b: 1, a: 1 };
+ world.insert(entity, Sprite, sprite);
+ }
+ }
+}
+
+function createHiddenTextarea(): HTMLTextAreaElement {
+ const textarea = document.createElement('textarea');
+ textarea.style.position = 'fixed';
+ textarea.style.left = '-9999px';
+ textarea.style.top = '-9999px';
+ textarea.style.width = '1px';
+ textarea.style.height = '1px';
+ textarea.style.opacity = '0';
+ textarea.style.zIndex = '-1';
+ textarea.autocomplete = 'off';
+ textarea.setAttribute('autocorrect', 'off');
+ textarea.setAttribute('autocapitalize', 'off');
+ textarea.setAttribute('spellcheck', 'false');
+ document.body.appendChild(textarea);
+ return textarea;
+}
+
+function wrapText(
+ ctx: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
+ text: string,
+ maxWidth: number
+): string[] {
+ if (!text) return [''];
+ if (maxWidth <= 0) return text.split('\n');
+ const paragraphs = text.split('\n');
+ const lines: string[] = [];
+ for (const paragraph of paragraphs) {
+ if (!paragraph) { lines.push(''); continue; }
+ let currentLine = '';
+ for (const char of paragraph) {
+ const testLine = currentLine + char;
+ if (ctx.measureText(testLine).width > maxWidth && currentLine) {
+ lines.push(currentLine);
+ currentLine = char;
+ } else {
+ currentLine = testLine;
+ }
+ }
+ if (currentLine) lines.push(currentLine);
+ }
+ return lines.length > 0 ? lines : [''];
+}
+
+function nextPowerOf2(n: number): number {
+ let p = 1;
+ while (p < n) p *= 2;
+ return p;
+}
+
+export const textInputPlugin = new TextInputPlugin();