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(cocos): a Label takes a game's replaced string setter (the engine's _string and friends land on the Text), and an EditBox shows its text label while it holds text

yhh2a07ad931bcb
diff --git a/sdk/src/cocos/ui/editBox.ts b/sdk/src/cocos/ui/editBox.ts
index 115b2a4a7..bf45e7916 100644
--- a/sdk/src/cocos/ui/editBox.ts
+++ b/sdk/src/cocos/ui/editBox.ts
@@ -168,7 +168,11 @@ export class EditBox extends Component {
/** What the labels show: the text (as bullets for a password), or the hint while there is none. */
private refresh_(): void {
const shown = this.inputFlag === InputFlag.PASSWORD ? BULLET.repeat(this.string_.length) : this.string_;
- if (this.textLabel) this.textLabel.string = shown;
+ if (this.textLabel) {
+ this.textLabel.string = shown;
+ // The text label is authored switched off (the field starts empty); it is on while there is text, which is when Cocos shows it.
+ this.textLabel.node.active = shown !== '';
+ }
if (this.placeholderLabel) this.placeholderLabel.node.active = this.string_ === '';
}
}
diff --git a/sdk/src/cocos/ui/label.ts b/sdk/src/cocos/ui/label.ts
index 8150db723..1d46da274 100644
--- a/sdk/src/cocos/ui/label.ts
+++ b/sdk/src/cocos/ui/label.ts
@@ -93,6 +93,17 @@ export class Label extends TextView {
get useSystemFont(): boolean { return !this.text_().font; }
set useSystemFont(_v: boolean) {}
+ // What a game that patches the engine's `Label` (a replaced `string` setter, a replaced `onEnable`) reads and writes of its internals: the
+ // same data as the properties above, so the patch lands on the `Text` instead of on a field nothing draws from.
+ get _string(): string { return this.text_().content; }
+ set _string(v: string) { this.patch_({ content: v }); }
+ get _fontFamily(): string { return this.text_().fontFamily; }
+ set _fontFamily(v: string) { this.patch_({ fontFamily: v }); }
+ get _isSystemFontUsed(): boolean { return !this.text_().font; }
+ set _isSystemFontUsed(_v: boolean) {}
+ _font: unknown = null;
+ _applyFontTexture(): void {}
+
/** The measured size of the text is the engine's layout to know; a no-op here, as a script calls it to force a refresh. */
updateRenderData(_force = false): void {}
diff --git a/sdk/src/cocos/ui/renderer.ts b/sdk/src/cocos/ui/renderer.ts
index c25708197..d1091e26e 100644
--- a/sdk/src/cocos/ui/renderer.ts
+++ b/sdk/src/cocos/ui/renderer.ts
@@ -16,6 +16,8 @@ export class UIRenderer extends Component {
markForUpdateRenderData(_enable = true): void { /* drawn from the component's data every frame */ }
setVertsDirty(): void { /* see markForUpdateRenderData */ }
updateRenderer(): void { /* see markForUpdateRenderData */ }
+ /** A script that replaces `onEnable` on the engine's class calls the base one first; there is nothing to set up here. */
+ onEnable(): void { /* the engine draws from the component's data */ }
/** Whether the element is waiting for a rebuild: never, here. */
get renderEntity(): undefined { return undefined; }
}
diff --git a/sdk/tests/cocos/editbox-wasm.test.ts b/sdk/tests/cocos/editbox-wasm.test.ts
index c5325a35a..7ff16fed7 100644
--- a/sdk/tests/cocos/editbox-wasm.test.ts
+++ b/sdk/tests/cocos/editbox-wasm.test.ts
@@ -127,6 +127,15 @@ describe.skipIf(!HAS_WASM)('EditBox', () => {
expect(hint.node.active).toBe(false);
});
+ it('switches its text label on while it holds text, even when the label was authored off', async () => {
+ const { edit, text } = await field();
+ text.node.active = false;
+ edit.string = 'preset';
+ expect(text.node.active).toBe(true);
+ edit.string = '';
+ expect(text.node.active).toBe(false);
+ });
+
it('does nothing on a host with no text editor', async () => {
editor = null;
const { edit } = await field();
diff --git a/sdk/tests/cocos/ui-adapters-wasm.test.ts b/sdk/tests/cocos/ui-adapters-wasm.test.ts
index 7fda1e1ef..acb2a5c3e 100644
--- a/sdk/tests/cocos/ui-adapters-wasm.test.ts
+++ b/sdk/tests/cocos/ui-adapters-wasm.test.ts
@@ -73,6 +73,28 @@ describe.skipIf(!HAS_WASM)('built-in UI components on the engine', () => {
expect(label.string).toBe('胜利');
});
+ it('takes a game that replaces the engine\'s string setter and writes its internal _string', () => {
+ connect();
+ const n = node('l');
+ const label = n.addComponent(Label)!;
+ const original = Object.getOwnPropertyDescriptor(Label.prototype, 'string')!;
+ // what a project patching the engine's Label does: it sets the engine's own field and marks the element for a rebuild
+ Object.defineProperty(Label.prototype, 'string', {
+ set(this: { _string: string; markForUpdateRenderData(): void }, value: string) {
+ if (this._string === value) return;
+ this._string = value;
+ this.markForUpdateRenderData();
+ },
+ });
+ try {
+ label.string = 'patched';
+ expect(W().get(n._entity, Text).content).toBe('patched');
+ expect(label.string).toBe('patched');
+ } finally {
+ Object.defineProperty(Label.prototype, 'string', original);
+ }
+ });
+
it('turns a null or undefined string into empty text, as Cocos does', () => {
connect();
const label = node('l').addComponent(Label)!;