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.

refactor(component): single-source ScaleMode from C++ CanvasScaleMode

yhhb6b2f0b74602
diff --git a/sdk/src/component.ts b/sdk/src/component.ts
index f47ff1fad..9d60a0c1e 100644
--- a/sdk/src/component.ts
+++ b/sdk/src/component.ts
@@ -21,9 +21,10 @@ import type {
// Builtin enums whose values come from C++ ES_ENUMs — imported from the generated
// module (single source) and re-exported below, so a TS const cannot drift from the
// C++ enum and the editor dropdowns (enumOptions) derive from the same values. The
-// remaining hand-written enums (ScaleMode aliases, ShapeType, ParticleEasing) have no
-// matching C++ enum yet, so they stay local.
-import { ProjectionType, ClearFlags, EmitterShape, SimulationSpace, Light2DType } from './wasm.generated';
+// remaining hand-written enums (ShapeType, ParticleEasing) have no matching C++ enum
+// yet, so they stay local; ScaleMode's canonical values come from CanvasScaleMode
+// (only its Cocos-compat aliases ShowAll/NoBorder are TS-side).
+import { ProjectionType, ClearFlags, EmitterShape, SimulationSpace, Light2DType, CanvasScaleMode } from './wasm.generated';
import { BlendMode } from './blend';
import { getDefaultContext } from './context';
import type {
@@ -507,14 +508,16 @@ export function ensureBuiltinComponentsRegistered(): void {
// module; their values and the editor dropdowns built from them now have one source.
export { ProjectionType, ClearFlags, EmitterShape, SimulationSpace, Light2DType };
+// Canonical values single-sourced from the C++ CanvasScaleMode enum (generated);
+// ShowAll/NoBorder are Cocos-compat aliases with no C++ member.
export const ScaleMode = {
- FixedWidth: 0,
- FixedHeight: 1,
- Expand: 2,
- Shrink: 3,
- Match: 4,
- ShowAll: 2,
- NoBorder: 3,
+ FixedWidth: CanvasScaleMode.FixedWidth,
+ FixedHeight: CanvasScaleMode.FixedHeight,
+ Expand: CanvasScaleMode.Expand,
+ Shrink: CanvasScaleMode.Shrink,
+ Match: CanvasScaleMode.Match,
+ ShowAll: CanvasScaleMode.Expand,
+ NoBorder: CanvasScaleMode.Shrink,
} as const;
export type ScaleMode = (typeof ScaleMode)[keyof typeof ScaleMode];
diff --git a/sdk/src/defaults.ts b/sdk/src/defaults.ts
index 3356640b7..8f4299cd5 100644
--- a/sdk/src/defaults.ts
+++ b/sdk/src/defaults.ts
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { CanvasScaleMode } from './wasm.generated';
+
export const DEFAULT_DESIGN_WIDTH = 1920;
export const DEFAULT_DESIGN_HEIGHT = 1080;
export const DEFAULT_PIXELS_PER_UNIT = 100;
@@ -57,9 +59,15 @@ export interface RuntimeBuildConfig {
assetFailureCooldown?: number;
}
-const CANVAS_SCALE_MODE_MAP: Record<string, number> = {
- FixedWidth: 0, FixedHeight: 1, Expand: 2, Shrink: 3, Match: 4, ShowAll: 2, NoBorder: 3,
-};
+/** Canvas scale-mode name → value. Canonical names are single-sourced from the
+ * C++ CanvasScaleMode enum; ShowAll/NoBorder are Cocos-compat aliases with no C++
+ * member. Unknown names fall back to FixedHeight (the C++ default). */
+function canvasScaleModeValue(name: string): number {
+ if (name === 'ShowAll') return CanvasScaleMode.Expand;
+ if (name === 'NoBorder') return CanvasScaleMode.Shrink;
+ const v = (CanvasScaleMode as unknown as Record<string, number>)[name];
+ return typeof v === 'number' ? v : CanvasScaleMode.FixedHeight;
+}
export function applyBuildRuntimeConfig(app: { setMaxDeltaTime(v: number): void; setMaxFixedSteps(v: number): void }, config: RuntimeBuildConfig): void {
if (config.maxDeltaTime !== undefined) {
@@ -89,7 +97,7 @@ export function applyBuildRuntimeConfig(app: { setMaxDeltaTime(v: number): void;
};
}
if (config.canvasScaleMode !== undefined) {
- RuntimeConfig.canvasScaleMode = CANVAS_SCALE_MODE_MAP[config.canvasScaleMode] ?? 1;
+ RuntimeConfig.canvasScaleMode = canvasScaleModeValue(config.canvasScaleMode);
}
if (config.canvasMatchWidthOrHeight !== undefined) {
RuntimeConfig.canvasMatchWidthOrHeight = config.canvasMatchWidthOrHeight;