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: TextInput cursor position on arrow keys and editor placeholder rendering

esengine269987da92ea
diff --git a/editor/src/scene/EditorSceneManager.ts b/editor/src/scene/EditorSceneManager.ts
index 157a3a143..6d00aab95 100644
--- a/editor/src/scene/EditorSceneManager.ts
+++ b/editor/src/scene/EditorSceneManager.ts
@@ -202,10 +202,12 @@ export class EditorSceneManager {
case 'UIMask':
this.world_.insert(entity, UIMask, comp.data as unknown as UIMaskData);
break;
+ case 'TextInput':
+ this.syncTextInput(entity, comp.data, entityId);
+ break;
case 'Interactable':
case 'Button':
case 'ScreenSpace':
- case 'TextInput':
break;
default:
@@ -702,6 +704,49 @@ export class EditorSceneManager {
});
}
+ private syncTextInput(entity: Entity, data: any, entityId: number): void {
+ const entityData = this.entityDataMap_.get(entityId);
+ const uiRectData = entityData ? getUIRectFromEntity(entityData) : null;
+
+ const value = data.value ?? '';
+ const placeholder = data.placeholder ?? '';
+ const isEmpty = value.length === 0;
+ const displayText = isEmpty ? placeholder : value;
+ if (!displayText) return;
+
+ const textData: TextData = {
+ content: displayText,
+ fontFamily: data.fontFamily ?? 'Arial',
+ fontSize: data.fontSize ?? 24,
+ color: isEmpty
+ ? (data.placeholderColor ?? { r: 0.6, g: 0.6, b: 0.6, a: 1 })
+ : (data.color ?? { r: 1, g: 1, b: 1, a: 1 }),
+ align: TextAlign.Left,
+ verticalAlign: TextVerticalAlign.Middle,
+ wordWrap: false,
+ overflow: TextOverflow.Clip,
+ lineHeight: 1.2,
+ dirty: true,
+ };
+
+ const result = this.textRenderer_.renderForEntity(entity, textData, uiRectData);
+
+ if (this.world_.has(entity, Sprite)) {
+ this.world_.remove(entity, Sprite);
+ }
+
+ this.world_.insert(entity, Sprite, {
+ texture: result.textureHandle,
+ color: { r: 1, g: 1, b: 1, a: 1 },
+ size: { x: result.width, y: result.height },
+ uvOffset: { x: 0, y: 0 },
+ uvScale: { x: 1, y: 1 },
+ layer: 25,
+ flipX: false,
+ flipY: false,
+ });
+ }
+
private async syncBitmapText(entity: Entity, data: any, _entityId: number): Promise<void> {
let fontHandle = INVALID_FONT as number;
diff --git a/sdk/src/ui/TextInputPlugin.ts b/sdk/src/ui/TextInputPlugin.ts
index a6a165639..f1fe4b4c6 100644
--- a/sdk/src/ui/TextInputPlugin.ts
+++ b/sdk/src/ui/TextInputPlugin.ts
@@ -65,6 +65,17 @@ export class TextInputPlugin implements Plugin {
}
});
+ textarea.addEventListener('keyup', () => {
+ if (focusedEntity === null || !world.valid(focusedEntity)) return;
+ const ti = world.get(focusedEntity, TextInput) as TextInputData;
+ const pos = textarea.selectionStart ?? ti.value.length;
+ if (pos !== ti.cursorPos) {
+ ti.cursorPos = pos;
+ ti.dirty = true;
+ resetCursorBlink();
+ }
+ });
+
textarea.addEventListener('blur', () => {
if (focusedEntity !== null && world.valid(focusedEntity) && world.has(focusedEntity, TextInput)) {
const ti = world.get(focusedEntity, TextInput) as TextInputData;