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 with no overflow rule does not wrap and is laid out from its anchor, as the node grows to the text in Cocos

yhh1cb24ad72262
diff --git a/pipeline/src/import/cocos/components.ts b/pipeline/src/import/cocos/components.ts
index 71919f858..a228a297b 100644
--- a/pipeline/src/import/cocos/components.ts
+++ b/pipeline/src/import/cocos/components.ts
@@ -239,7 +239,9 @@ function label(c: CocosComponent, siblings: CocosComponent[], box: UiBox | null,
// Left/Center/Right and Top/Middle/Bottom number the same in both engines (0, 1, 2).
align: clamp012(d._horizontalAlign),
verticalAlign: clamp012(d._verticalAlign),
- wordWrap: d._enableWrapText !== false,
+ // Cocos wraps only where the box limits the text: with no overflow rule (NONE) the node grows to the text and nothing wraps, and
+ // ResizeHeight wraps by width whatever the flag says.
+ wordWrap: overflow === 0 ? false : overflow === 3 ? true : d._enableWrapText !== false,
overflow: overflow === 1 ? 1 : overflow === 2 ? 3 : overflow === 3 ? 4 : 0, // Clip, Shrink, ResizeHeight, otherwise Visible
lineHeight: fontSize > 0 ? Math.round((lineHeight / fontSize) * 1000) / 1000 : 1.2,
bold: d._isBold === true,
@@ -251,6 +253,12 @@ function label(c: CocosComponent, siblings: CocosComponent[], box: UiBox | null,
data.boxWidth = box.width;
data.boxHeight = box.height;
data.boxPivot = { x: box.ax, y: box.ay };
+ // A node that grows to its text keeps its anchor where it was, so the text is laid out from the anchor: a run on a node anchored at
+ // its middle spreads both ways from there, whatever alignment the label names (the alignment only matters inside a box that limits it).
+ if (overflow === 0) {
+ data.align = box.ax <= 0.25 ? 0 : box.ax >= 0.75 ? 2 : 1;
+ data.verticalAlign = box.ay >= 0.75 ? 0 : box.ay <= 0.25 ? 2 : 1;
+ }
}
const fontUuid = uuidOf(d._font);
if (d._isSystemFontUsed === false && fontUuid) {
diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.ts
index 1d5b8bee2..1ffd407ca 100644
--- a/pipeline/src/import/cocos/importProject.ts
+++ b/pipeline/src/import/cocos/importProject.ts
@@ -31,7 +31,7 @@ import { convertPass, isOutlinePass, type PassConversion } from './effectConvert
* importer that made less (or differently), and is brought up to date when it is opened. Raise it when the converted output changes in a
* way an existing project needs to pick up: a new generated file, a field a document or a manifest now carries.
*/
-export const COCOS_IMPORT_REVISION = 8;
+export const COCOS_IMPORT_REVISION = 9;
/** Whether the project at @p projectRoot was written by an importer of this revision. A project that was not converted has nothing to bring up. */
export function cocosImportIsCurrent(projectRoot: string): boolean {
diff --git a/pipeline/tests/cocos-import.test.ts b/pipeline/tests/cocos-import.test.ts
index 5eaf8eaf2..4275b7484 100644
--- a/pipeline/tests/cocos-import.test.ts
+++ b/pipeline/tests/cocos-import.test.ts
@@ -193,6 +193,8 @@ describe('importing a Cocos project', () => {
content: 'hello', fontSize: 20, align: 1, verticalAlign: 1, overflow: 0, font: 'assets/cocos/ui/f.fnt',
// the Label's UITransform becomes its layout box, with the node origin at the anchor
boxWidth: 80, boxHeight: 20, boxPivot: { x: 0.5, y: 0.5 },
+ // no overflow rule: the node grows to its text, so nothing wraps in the box and the run is laid out from the anchor
+ wordWrap: false,
});
const written = [...files(out)].map((f) => path.relative(out, f).split(path.sep).join('/'));
expect(written, written.join(',')).toContain('assets/cocos/ui/f_0.png');