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(ui): delete legacy Button / Toggle / Slider / Dropdown / ProgressBar

esengine06735c7dc113
diff --git a/sdk/src/core.ts b/sdk/src/core.ts
index 3e628953e..84dc1e589 100644
--- a/sdk/src/core.ts
+++ b/sdk/src/core.ts
@@ -350,8 +350,6 @@ export {
Interactable,
UIInteraction,
AnimOverride,
- Button,
- ButtonState,
UIEvents,
UIEventQueue,
makeInteractable,
@@ -367,20 +365,14 @@ export {
ImageType,
FillMethod,
FillOrigin,
- Toggle,
- ProgressBar,
- ProgressBarDirection,
Draggable,
DragState,
ScrollView,
- Slider,
- SliderDirection,
FillDirection,
Focusable,
FocusManager,
FocusManagerState,
SafeArea,
- Dropdown,
type TextData,
type UIRectData,
type UIMaskData,
@@ -389,8 +381,6 @@ export {
type ScreenRect,
type InteractableData,
type UIInteractionData,
- type ButtonTransition,
- type ButtonData,
type UIEvent,
type UIEventType,
type UIEventHandler,
@@ -402,16 +392,11 @@ export {
type UIRendererData,
type UILayoutGenerationData,
type ImageData,
- type ToggleTransition,
- type ToggleData,
- type ProgressBarData,
type DraggableData,
type DragStateData,
type ScrollViewData,
- type SliderData,
type FocusableData,
type SafeAreaData,
- type DropdownData,
UIThemeRes,
DARK_THEME,
withChildEntity,
diff --git a/sdk/src/systemLabels.ts b/sdk/src/systemLabels.ts
index f879f7d15..af8ef74a4 100644
--- a/sdk/src/systemLabels.ts
+++ b/sdk/src/systemLabels.ts
@@ -12,14 +12,9 @@ export const PluginName = {
Image: 'image',
Focus: 'focus',
ScrollView: 'scrollView',
- Dropdown: 'dropdown',
- ProgressBar: 'progressBar',
- Slider: 'slider',
- Toggle: 'toggle',
Drag: 'drag',
SafeArea: 'safeArea',
TextInput: 'textInput',
- StateMachine: 'stateMachine',
} as const;
export const SystemLabel = {
@@ -27,7 +22,6 @@ export const SystemLabel = {
UILayoutLate: 'UILayoutLateSystem',
UIRenderOrder: 'UIRenderOrderSystem',
UIInteraction: 'UIInteractionSystem',
- Button: 'ButtonSystem',
ScrollView: 'ScrollViewSystem',
ListView: 'ListViewSystem',
Text: 'TextSystem',
diff --git a/sdk/src/ui/Button.ts b/sdk/src/ui/Button.ts
deleted file mode 100644
index 12640fc73..000000000
--- a/sdk/src/ui/Button.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import { defineComponent } from '../component';
-import type { ColorTransition } from './uiTypes';
-
-export type { ColorTransition } from './uiTypes';
-export type ButtonTransition = ColorTransition;
-
-export const ButtonState = {
- Normal: 0,
- Hovered: 1,
- Pressed: 2,
- Disabled: 3,
-} as const;
-
-export type ButtonState = (typeof ButtonState)[keyof typeof ButtonState];
-
-export interface ButtonData {
- state: ButtonState;
- transition: ColorTransition | null;
-}
-
-export const Button = defineComponent<ButtonData>('Button', {
- state: ButtonState.Normal,
- transition: null,
-});
diff --git a/sdk/src/ui/Dropdown.ts b/sdk/src/ui/Dropdown.ts
deleted file mode 100644
index 526913691..000000000
--- a/sdk/src/ui/Dropdown.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import { defineComponent } from '../component';
-import type { Entity } from '../types';
-
-export interface DropdownData {
- options: string[];
- selectedIndex: number;
- isOpen: boolean;
- listEntity: Entity;
- labelEntity: Entity;
-}
-
-export const Dropdown = defineComponent<DropdownData>('Dropdown', {
- options: [],
- selectedIndex: -1,
- isOpen: false,
- listEntity: 0 as Entity,
- labelEntity: 0 as Entity,
-}, { entityFields: ['listEntity', 'labelEntity'] });
diff --git a/sdk/src/ui/DropdownPlugin.ts b/sdk/src/ui/DropdownPlugin.ts
deleted file mode 100644
index 044e23fc3..000000000
--- a/sdk/src/ui/DropdownPlugin.ts
+++ /dev/null
@@ -1,400 +0,0 @@
-import type { App, Plugin } from '../app';
-import { registerComponent, Transform, Name, Sprite } from '../component';
-import type { TransformData, SpriteData } from '../component';
-import { UIRenderer, UIVisualType } from './UIRenderer';
-import type { UIRendererData } from './UIRenderer';
-import { defineSystem, Schedule } from '../system';
-import { Res } from '../resource';
-import type { Entity } from '../types';
-import { Dropdown } from './Dropdown';
-import type { DropdownData } from './Dropdown';
-import { Interactable } from './Interactable';
-import type { InteractableData } from './Interactable';
-import { UIInteraction } from './UIInteraction';
-import type { UIInteractionData } from './UIInteraction';
-import { UIEvents, UIEventQueue } from './UIEvents';
-import { Text, TextAlign, TextVerticalAlign, TextOverflow } from './text';
-import type { TextData } from './text';
-import { UIRect } from './UIRect';
-import type { UIRectData } from './UIRect';
-import { Input } from '../input';
-import type { InputState } from '../input';
-import { isEditor, isPlayMode } from '../env';
-import { ensureComponent, getEffectiveWidth, getEffectiveHeight, setEntityColor, setEntityEnabled, withChildEntity } from './uiHelpers';
-import { DROPDOWN_ITEM_HEIGHT, DROPDOWN_FONT_SIZE, DROPDOWN_HIGHLIGHT_COLOR, DROPDOWN_DEFAULT_COLOR, DROPDOWN_OPTION_COLOR } from './uiConstants';
-import { SystemLabel, PluginName } from '../systemLabels';
-
-interface DropdownState {
- optionEntities: Entity[];
- textEntities: Entity[];
- highlightIndex: number;
- ddWidth: number;
-}
-
-export class DropdownPlugin implements Plugin {
- name = PluginName.Dropdown;
- dependencies = [PluginName.UIInteraction];
-
- private cleanup_: (() => void) | null = null;
-
- cleanup(): void {
- if (this.cleanup_) {
- this.cleanup_();
- this.cleanup_ = null;
- }
- }
-
- build(app: App): void {
- registerComponent('Dropdown', Dropdown);
-
- const world = app.world;
- const dropdownStates = new Map<Entity, DropdownState>();
- const appearanceInitialized = new Set<Entity>();
-
- app.addSystemToSchedule(Schedule.Update, defineSystem(
- [Res(Input), Res(UIEvents)],
- (input: InputState, events: UIEventQueue) => {
- for (const [e, st] of dropdownStates) {
- if (!world.valid(e)) {
- destroyOptions(st);
- dropdownStates.delete(e);
- appearanceInitialized.delete(e);
- }
- }
-
- const editorSceneView = isEditor() && !isPlayMode();
- const entities = world.getEntitiesWithComponents([Dropdown]);
- for (const entity of entities) {
- const dropdown = world.get(entity, Dropdown) as DropdownData;
-
- let state = dropdownStates.get(entity);
- if (!state) {
- state = { optionEntities: [], textEntities: [], highlightIndex: -1, ddWidth: 0 };
- dropdownStates.set(entity, state);
- }
-
- normalizeListTransform(dropdown.listEntity);
- if (!appearanceInitialized.has(entity)) {
- initDropdownAppearance(entity);
- appearanceInitialized.add(entity);
- }
- if (!dropdown.isOpen) {
- setListVisible(dropdown.listEntity, false);
- }
-
- syncLabel(dropdown);
-
- if (editorSceneView) continue;
-
- ensureComponent(world, entity, Interactable, { enabled: true, blockRaycast: true });
-
- const interaction = world.has(entity, UIInteraction)
- ? world.get(entity, UIInteraction) as UIInteractionData
- : null;
-
- if (interaction?.justPressed) {
- if (dropdown.isOpen) {
- closeDropdown(entity, dropdown, state);
- } else {
- openDropdown(entity, dropdown, state);
- }
- }
-
- if (dropdown.isOpen) {
- for (let i = 0; i < state.optionEntities.length; i++) {
- const optEntity = state.optionEntities[i];
- if (!world.valid(optEntity) || !world.has(optEntity, UIInteraction)) continue;
- const optInteraction = world.get(optEntity, UIInteraction) as UIInteractionData;
- if (optInteraction.justPressed) {
- selectOption(entity, dropdown, state, i, events);
- break;
- }
- }
-
- let hoveredIndex = -1;
- for (let i = 0; i < state.optionEntities.length; i++) {
- const optEntity = state.optionEntities[i];
- if (!world.valid(optEntity) || !world.has(optEntity, UIInteraction)) continue;
- const optInteraction = world.get(optEntity, UIInteraction) as UIInteractionData;
- if (optInteraction.hovered) {
- hoveredIndex = i;
- break;
- }
- }
- if (hoveredIndex >= 0 && hoveredIndex !== state.highlightIndex) {
- state.highlightIndex = hoveredIndex;
- updateHighlight(state);
- }
-
- if (input.isKeyPressed('ArrowDown')) {
- if (state.highlightIndex >= dropdown.options.length - 1) {
- state.highlightIndex = 0;
- } else {
- state.highlightIndex++;
- }
- updateHighlight(state);
- } else if (input.isKeyPressed('ArrowUp')) {
- if (state.highlightIndex <= 0) {
- state.highlightIndex = dropdown.options.length - 1;
- } else {
- state.highlightIndex--;
- }
- updateHighlight(state);
- } else if (input.isKeyPressed('Enter') && state.highlightIndex >= 0) {
- selectOption(entity, dropdown, state, state.highlightIndex, events);
- } else if (input.isKeyPressed('Escape')) {
- closeDropdown(entity, dropdown, state);
- }
-
- if (input.isMouseButtonPressed(0)) {
- if (!interaction?.hovered && !isAnyOptionHovered(state)) {
- closeDropdown(entity, dropdown, state);
- }
- }
- }
- }
- },
- { name: 'DropdownSystem' }
- ), { runAfter: [SystemLabel.UIInteraction] });
-
- function normalizeListTransform(listEntity: Entity): void {
- withChildEntity(world, listEntity, (e) => {
- if (!world.has(e, Transform)) return;
- const t = world.get(e, Transform) as TransformData;
- if (t.scale.x === 0 && t.scale.y === 0 && t.scale.z === 0) {
- world.insert(e, Transform, {
- position: { x: t.position.x, y: t.position.y, z: t.position.z },
- rotation: { x: t.rotation.x, y: t.rotation.y, z: t.rotation.z, w: t.rotation.w },
- scale: { x: 1, y: 1, z: 1 },
- });
- }
- });
- }
-
- function initDropdownAppearance(entity: Entity): void {
- if (world.has(entity, UIRenderer)) {
- const r = world.get(entity, UIRenderer) as UIRendererData;
- if (r.color.r === 1 && r.color.g === 1 && r.color.b === 1 && r.color.a === 1) {
- setEntityColor(world, entity, { ...DROPDOWN_DEFAULT_COLOR });
- }
- } else if (world.has(entity, Sprite)) {
- const s = world.get(entity, Sprite) as SpriteData;
- if (s.color.r === 1 && s.color.g === 1 && s.color.b === 1 && s.color.a === 1) {
- setEntityColor(world, entity, { ...DROPDOWN_DEFAULT_COLOR });
- }
- }
- }
-
- function openDropdown(entity: Entity, dropdown: DropdownData, state: DropdownState): void {
- dropdown.isOpen = true;
- state.highlightIndex = dropdown.selectedIndex;
-
- const listEntity = dropdown.listEntity;
- if (listEntity === 0 || !world.valid(listEntity)) return;
-
- setListVisible(listEntity, true);
- destroyOptions(state);
-
- let ddWidth = 0;
- if (world.has(entity, UIRect)) {
- const ddRect = world.get(entity, UIRect) as UIRectData;
- ddWidth = getEffectiveWidth(ddRect, entity) || ddRect.size.x;
- } else if (world.has(entity, Sprite)) {
- const ddSprite = world.get(entity, Sprite) as SpriteData;
- ddWidth = ddSprite.size.x;
- }
- state.ddWidth = ddWidth;
- const totalHeight = dropdown.options.length * DROPDOWN_ITEM_HEIGHT;
-
- world.insert(listEntity, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 1, y: 0 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: ddWidth, y: totalHeight },
- pivot: { x: 0.5, y: 1 },
- });
- world.insert(listEntity, UIRenderer, {
- visualType: UIVisualType.SolidColor,
- texture: 0,
- color: { r: 1, g: 1, b: 1, a: 1 },
- uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 },
- sliceBorder: { x: 0, y: 0, z: 0, w: 0 },
- material: 0,
- enabled: true,
- });
-
- for (let i = 0; i < dropdown.options.length; i++) {
- const optEntity = world.spawn();
- world.insert(optEntity, Name, { value: `Option_${i}` });
- world.insert(optEntity, UIRect, {
- anchorMin: { x: 0, y: 1 },
- anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: -i * DROPDOWN_ITEM_HEIGHT },
- offsetMax: { x: 0, y: 0 },
- size: { x: ddWidth, y: DROPDOWN_ITEM_HEIGHT },
- pivot: { x: 0.5, y: 1 },
- });
- world.insert(optEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { w: 1, x: 0, y: 0, z: 0 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(optEntity, UIRenderer, {
- visualType: UIVisualType.SolidColor,
- texture: 0,
- color: { ...DROPDOWN_OPTION_COLOR },
- uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 },
- sliceBorder: { x: 0, y: 0, z: 0, w: 0 },
- material: 0,
- enabled: true,
- });
- world.insert(optEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
- world.setParent(optEntity, listEntity);
- state.optionEntities.push(optEntity);
-
- const textEntity = world.spawn();
- world.insert(textEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { w: 1, x: 0, y: 0, z: 0 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(textEntity, Text, {
- content: dropdown.options[i],
- fontSize: DROPDOWN_FONT_SIZE,
- fontFamily: 'Arial',
- color: { r: 0, g: 0, b: 0, a: 1 },
- align: TextAlign.Left,
- verticalAlign: TextVerticalAlign.Middle,
- wordWrap: false,
- overflow: TextOverflow.Clip,
- lineHeight: 1.2,
- });
- world.insert(textEntity, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: ddWidth, y: DROPDOWN_ITEM_HEIGHT },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.setParent(textEntity, optEntity);
- state.textEntities.push(textEntity);
- }
-
- updateHighlight(state);
- }
-
- function closeDropdown(_entity: Entity, dropdown: DropdownData, state: DropdownState): void {
- dropdown.isOpen = false;
- destroyOptions(state);
- setListVisible(dropdown.listEntity, false);
- }
-
- function selectOption(
- entity: Entity,
- dropdown: DropdownData,
- state: DropdownState,
- index: number,
- events: UIEventQueue,
- ): void {
- dropdown.selectedIndex = index;
- closeDropdown(entity, dropdown, state);
- events.emit(entity, 'change');
- }
-
- function syncLabel(dropdown: DropdownData): void {
- const labelEntity = dropdown.labelEntity;
- if (labelEntity === 0 || !world.valid(labelEntity)) return;
- if (!world.has(labelEntity, Text)) return;
-
- const t = world.get(labelEntity, Text) as TextData;
- let newColor = { r: t.color.r, g: t.color.g, b: t.color.b, a: t.color.a };
- let newContent = t.content;
- let changed = false;
-
- if (newColor.r === 1 && newColor.g === 1 && newColor.b === 1 && newColor.a === 1) {
- newColor = { r: 0, g: 0, b: 0, a: 1 };
- changed = true;
- }
-
- if (dropdown.selectedIndex >= 0 && dropdown.selectedIndex < dropdown.options.length) {
- const selectedText = dropdown.options[dropdown.selectedIndex];
- if (newContent !== selectedText) {
- newContent = selectedText;
- changed = true;
- }
- }
-
- if (changed) {
- world.insert(labelEntity, Text, {
- content: newContent,
- fontSize: t.fontSize,
- fontFamily: t.fontFamily,
- color: newColor,
- align: t.align,
- verticalAlign: t.verticalAlign,
- wordWrap: t.wordWrap,
- overflow: t.overflow,
- lineHeight: t.lineHeight,
- });
- }
- }
-
- function destroyOptions(state: DropdownState): void {
- for (const textEntity of state.textEntities) {
- if (world.valid(textEntity)) {
- world.despawn(textEntity);
- }
- }
- for (const optEntity of state.optionEntities) {
- if (world.valid(optEntity)) {
- world.despawn(optEntity);
- }
- }
- state.optionEntities = [];
- state.textEntities = [];
- }
-
- function setListVisible(entity: Entity, visible: boolean): void {
- withChildEntity(world, entity, (e) => {
- if (world.has(e, Interactable)) {
- world.insert(e, Interactable, { enabled: visible, blockRaycast: true, raycastTarget: true });
- }
- setEntityEnabled(world, e, visible);
- });
- }
-
- function updateHighlight(state: DropdownState): void {
- for (let i = 0; i < state.optionEntities.length; i++) {
- const optEntity = state.optionEntities[i];
- if (!world.valid(optEntity)) continue;
- const color = i === state.highlightIndex
- ? { ...DROPDOWN_HIGHLIGHT_COLOR }
- : { ...DROPDOWN_OPTION_COLOR };
- setEntityColor(world, optEntity, color);
- }
- }
-
- function isAnyOptionHovered(state: DropdownState): boolean {
- for (const optEntity of state.optionEntities) {
- if (!world.valid(optEntity) || !world.has(optEntity, UIInteraction)) continue;
- const optInteraction = world.get(optEntity, UIInteraction) as UIInteractionData;
- if (optInteraction.hovered) return true;
- }
- return false;
- }
-
- this.cleanup_ = () => {
- for (const [, st] of dropdownStates) {
- destroyOptions(st);
- }
- dropdownStates.clear();
- appearanceInitialized.clear();
- };
- }
-}
-
-export const dropdownPlugin = new DropdownPlugin();
diff --git a/sdk/src/ui/ProgressBar.ts b/sdk/src/ui/ProgressBar.ts
deleted file mode 100644
index d98e54524..000000000
--- a/sdk/src/ui/ProgressBar.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import { defineComponent } from '../component';
-import type { Entity } from '../types';
-import { FillDirection } from './uiTypes';
-
-export { FillDirection } from './uiTypes';
-export const ProgressBarDirection = FillDirection;
-export type ProgressBarDirection = FillDirection;
-
-export interface ProgressBarData {
- value: number;
- fillEntity: Entity;
- direction: FillDirection;
-}
-
-export const ProgressBar = defineComponent<ProgressBarData>('ProgressBar', {
- value: 0,
- fillEntity: 0 as Entity,
- direction: FillDirection.LeftToRight,
-}, { entityFields: ['fillEntity'] });
diff --git a/sdk/src/ui/ProgressBarPlugin.ts b/sdk/src/ui/ProgressBarPlugin.ts
deleted file mode 100644
index b3b36a1cf..000000000
--- a/sdk/src/ui/ProgressBarPlugin.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import type { App, Plugin } from '../app';
-import { registerComponent } from '../component';
-import { defineSystem, Schedule } from '../system';
-import { UIRect } from './UIRect';
-import type { UIRectData } from './UIRect';
-import { ProgressBar } from './ProgressBar';
-import type { ProgressBarData } from './ProgressBar';
-import { applyDirectionalFill, withChildEntity } from './uiHelpers';
-import { SystemLabel, PluginName } from '../systemLabels';
-
-export class ProgressBarPlugin implements Plugin {
- name = PluginName.ProgressBar;
- dependencies = [PluginName.UILayout];
-
- build(app: App): void {
- registerComponent('ProgressBar', ProgressBar);
-
- const world = app.world;
-
- app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
- [],
- () => {
- const entities = world.getEntitiesWithComponents([ProgressBar, UIRect]);
- for (const entity of entities) {
- const bar = world.get(entity, ProgressBar) as ProgressBarData;
- const value = Math.max(0, Math.min(1, bar.value));
-
- withChildEntity(world, bar.fillEntity, (fill) => {
- applyDirectionalFill(world, fill, bar.direction, value);
- });
- }
- },
- { name: 'ProgressBarSystem' }
- ), { runAfter: [SystemLabel.UILayout] });
- }
-}
-
-export const progressBarPlugin = new ProgressBarPlugin();
diff --git a/sdk/src/ui/Slider.ts b/sdk/src/ui/Slider.ts
deleted file mode 100644
index d9383c896..000000000
--- a/sdk/src/ui/Slider.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-import { defineComponent } from '../component';
-import type { Entity } from '../types';
-import { FillDirection } from './uiTypes';
-
-export { FillDirection } from './uiTypes';
-export const SliderDirection = FillDirection;
-export type SliderDirection = FillDirection;
-
-export interface SliderData {
- value: number;
- minValue: number;
- maxValue: number;
- direction: FillDirection;
- fillEntity: Entity;
- handleEntity: Entity;
- wholeNumbers: boolean;
-}
-
-export const Slider = defineComponent<SliderData>('Slider', {
- value: 0,
- minValue: 0,
- maxValue: 1,
- direction: FillDirection.LeftToRight,
- fillEntity: 0 as Entity,
- handleEntity: 0 as Entity,
- wholeNumbers: false,
-}, { entityFields: ['fillEntity', 'handleEntity'] });
diff --git a/sdk/src/ui/SliderPlugin.ts b/sdk/src/ui/SliderPlugin.ts
deleted file mode 100644
index f0de1b6cc..000000000
--- a/sdk/src/ui/SliderPlugin.ts
+++ /dev/null
@@ -1,164 +0,0 @@
-import type { App, Plugin } from '../app';
-import { registerComponent, Transform } from '../component';
-import type { TransformData } from '../component';
-import { defineSystem, Schedule } from '../system';
-import { Res } from '../resource';
-import { Input } from '../input';
-import type { InputState } from '../input';
-import type { Entity } from '../types';
-import { Slider, FillDirection } from './Slider';
-import type { SliderData } from './Slider';
-import { UIRect } from './UIRect';
-import type { UIRectData } from './UIRect';
-import { Interactable } from './Interactable';
-import { UIInteraction } from './UIInteraction';
-import type { UIInteractionData } from './UIInteraction';
-import { UICameraInfo } from './UICameraInfo';
-import type { UICameraData } from './UICameraInfo';
-import { UIEvents, UIEventQueue } from './UIEvents';
-import { isEditor, isPlayMode } from '../env';
-import { applyDirectionalFill, getEffectiveWidth, getEffectiveHeight, ensureComponent, withChildEntity } from './uiHelpers';
-import { quaternionToAngle2D } from './uiMath';
-import { SystemLabel, PluginName } from '../systemLabels';
-
-function computeSliderValue(
- worldMouseX: number, worldMouseY: number,
- wt: TransformData, rect: UIRectData, entity: Entity,
- direction: FillDirection,
-): number {
- const worldW = getEffectiveWidth(rect, entity) * wt.worldScale.x;
- const worldH = getEffectiveHeight(rect, entity) * wt.worldScale.y;
-
- const angle = quaternionToAngle2D(wt.worldRotation.z, wt.worldRotation.w);
- const sin = Math.sin(-angle);
- const cos = Math.cos(-angle);
- const dx = worldMouseX - wt.worldPosition.x;
- const dy = worldMouseY - wt.worldPosition.y;
- const localMouseX = dx * cos - dy * sin;
- const localMouseY = dx * sin + dy * cos;
-
- const originX = -rect.pivot.x * worldW;
- const originY = -rect.pivot.y * worldH;
-
- switch (direction) {
- case FillDirection.LeftToRight:
- return worldW > 0 ? (localMouseX - originX) / worldW : 0;
- case FillDirection.RightToLeft:
- return worldW > 0 ? 1 - (localMouseX - originX) / worldW : 0;
- case FillDirection.BottomToTop:
- return worldH > 0 ? (localMouseY - originY) / worldH : 0;
- case FillDirection.TopToBottom:
- return worldH > 0 ? 1 - (localMouseY - originY) / worldH : 0;
- }
-}
-
-function syncHandleRect(
- handleRect: UIRectData,
- direction: FillDirection,
- normalizedValue: number,
-): void {
- handleRect.offsetMin.x = 0; handleRect.offsetMin.y = 0;
- handleRect.offsetMax.x = 0; handleRect.offsetMax.y = 0;
- switch (direction) {
- case FillDirection.LeftToRight:
- handleRect.anchorMin.x = normalizedValue; handleRect.anchorMin.y = 0.5;
- handleRect.anchorMax.x = normalizedValue; handleRect.anchorMax.y = 0.5;
- break;
- case FillDirection.RightToLeft:
- handleRect.anchorMin.x = 1 - normalizedValue; handleRect.anchorMin.y = 0.5;
- handleRect.anchorMax.x = 1 - normalizedValue; handleRect.anchorMax.y = 0.5;
- break;
- case FillDirection.BottomToTop:
- handleRect.anchorMin.x = 0.5; handleRect.anchorMin.y = normalizedValue;
- handleRect.anchorMax.x = 0.5; handleRect.anchorMax.y = normalizedValue;
- break;
- case FillDirection.TopToBottom:
- handleRect.anchorMin.x = 0.5; handleRect.anchorMin.y = 1 - normalizedValue;
- handleRect.anchorMax.x = 0.5; handleRect.anchorMax.y = 1 - normalizedValue;
- break;
- }
-}
-
-export class SliderPlugin implements Plugin {
- name = PluginName.Slider;
- dependencies = [PluginName.UIInteraction];
-
- build(app: App): void {
- registerComponent('Slider', Slider);
-
- const world = app.world;
-
- app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
- [Res(Input), Res(UICameraInfo), Res(UIEvents)],
- (input: InputState, camera: UICameraData, events: UIEventQueue) => {
- if (!camera.valid) return;
-
- const entities = world.getEntitiesWithComponents([Slider, UIRect]);
- for (const entity of entities) {
- const slider = world.get(entity, Slider) as SliderData;
- const rect = world.get(entity, UIRect) as UIRectData;
-
- if (!isEditor() || isPlayMode()) {
- const hasWT = world.has(entity, Transform);
- if (hasWT) {
- ensureComponent(world, entity, Interactable, { enabled: true, blockRaycast: true });
- withChildEntity(world, slider.handleEntity, (handle) => {
- ensureComponent(world, handle, Interactable, { enabled: true, blockRaycast: false });
- });
-
- const wt = world.get(entity, Transform) as TransformData;
-
- let isDragging = false;
- if (world.has(entity, UIInteraction)) {
- isDragging = (world.get(entity, UIInteraction) as UIInteractionData).pressed;
- }
- withChildEntity(world, slider.handleEntity, (handle) => {
- if (!isDragging && world.has(handle, UIInteraction)) {
- isDragging = (world.get(handle, UIInteraction) as UIInteractionData).pressed;
- }
- });
-
- if (isDragging && input.isMouseButtonDown(0)) {
- let t = computeSliderValue(
- camera.worldMouseX, camera.worldMouseY,
- wt, rect, entity, slider.direction,
- );
- t = Math.max(0, Math.min(1, t));
-
- let newValue = slider.minValue + t * (slider.maxValue - slider.minValue);
- if (slider.wholeNumbers) {
- newValue = Math.round(newValue);
- }
- const oldValue = slider.value;
- slider.value = Math.max(slider.minValue, Math.min(slider.maxValue, newValue));
- world.insert(entity, Slider, slider);
-
- if (slider.value !== oldValue) {
- events.emit(entity, 'change');
- }
- }
-
- }
- }
-
- const range = slider.maxValue - slider.minValue;
- const normalizedValue = range > 0 ? (slider.value - slider.minValue) / range : 0;
-
- withChildEntity(world, slider.fillEntity, (fill) => {
- applyDirectionalFill(world, fill, slider.direction, normalizedValue);
- });
-
- withChildEntity(world, slider.handleEntity, (handle) => {
- if (!world.has(handle, UIRect)) return;
- const handleRect = world.get(handle, UIRect) as UIRectData;
- syncHandleRect(handleRect, slider.direction, normalizedValue);
- world.insert(handle, UIRect, handleRect);
- });
- }
- },
- { name: 'SliderSystem' }
- ), { runAfter: [SystemLabel.UIInteraction] });
- }
-}
-
-export const sliderPlugin = new SliderPlugin();
diff --git a/sdk/src/ui/Toggle.ts b/sdk/src/ui/Toggle.ts
deleted file mode 100644
index 288b93a2f..000000000
--- a/sdk/src/ui/Toggle.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import { defineComponent } from '../component';
-import type { Color, Entity } from '../types';
-import type { ColorTransition } from './uiTypes';
-
-export type { ColorTransition } from './uiTypes';
-export type ToggleTransition = ColorTransition;
-
-export interface ToggleData {
- isOn: boolean;
- graphicEntity: Entity;
- group: Entity;
- transition: ColorTransition | null;
- onColor: Color;
- offColor: Color;
-}
-
-export const Toggle = defineComponent<ToggleData>('Toggle', {
- isOn: true,
- graphicEntity: 0 as Entity,
- group: 0 as Entity,
- transition: null,
- onColor: { r: 0.2, g: 0.6, b: 1, a: 1 },
- offColor: { r: 0.4, g: 0.4, b: 0.4, a: 1 },
-}, { entityFields: ['graphicEntity', 'group'] });
diff --git a/sdk/src/ui/ToggleGroup.ts b/sdk/src/ui/ToggleGroup.ts
deleted file mode 100644
index e309b8716..000000000
--- a/sdk/src/ui/ToggleGroup.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-import { defineComponent } from '../component';
-
-export interface ToggleGroupData {
- allowSwitchOff: boolean;
-}
-
-export const ToggleGroup = defineComponent<ToggleGroupData>('ToggleGroup', {
- allowSwitchOff: false,
-});
diff --git a/sdk/src/ui/TogglePlugin.ts b/sdk/src/ui/TogglePlugin.ts
deleted file mode 100644
index 29b113c7c..000000000
--- a/sdk/src/ui/TogglePlugin.ts
+++ /dev/null
@@ -1,147 +0,0 @@
-import type { App, Plugin } from '../app';
-import { registerComponent } from '../component';
-import { defineSystem, Schedule } from '../system';
-import { Res } from '../resource';
-import type { Entity, Color } from '../types';
-import { Interactable } from './Interactable';
-import type { InteractableData } from './Interactable';
-import { UIInteraction } from './UIInteraction';
-import type { UIInteractionData } from './UIInteraction';
-import { Toggle } from './Toggle';
-import type { ToggleData } from './Toggle';
-import { ToggleGroup } from './ToggleGroup';
-import type { ToggleGroupData } from './ToggleGroup';
-import { UIEvents, UIEventQueue } from './UIEvents';
-import { UIRenderer } from './UIRenderer';
-import type { UIRendererData } from './UIRenderer';
-import { isEditor, isPlayMode } from '../env';
-import { applyColorTransition, applyDefaultTint, ensureComponent, withChildEntity } from './uiHelpers';
-import { SystemLabel, PluginName } from '../systemLabels';
-
-function setRendererColor(world: import('../world').World, entity: Entity, color: Color): void {
- if (!world.has(entity, UIRenderer)) return;
- const r = world.get(entity, UIRenderer) as UIRendererData;
- if (r.color.r === color.r && r.color.g === color.g && r.color.b === color.b && r.color.a === color.a) return;
- r.color = color;
- world.insert(entity, UIRenderer, r);
-}
-
-function setRendererEnabled(world: import('../world').World, entity: Entity, enabled: boolean): void {
- if (!world.has(entity, UIRenderer)) return;
- const r = world.get(entity, UIRenderer) as UIRendererData;
- if (r.enabled === enabled) return;
- r.enabled = enabled;
- world.insert(entity, UIRenderer, r);
-}
-
-export class TogglePlugin implements Plugin {
- name = PluginName.Toggle;
- dependencies = [PluginName.UIInteraction];
-
- build(app: App): void {
- registerComponent('Toggle', Toggle);
- registerComponent('ToggleGroup', ToggleGroup);
-
- const world = app.world;
- const initializedEntities = new Set<Entity>();
-
- app.addSystemToSchedule(Schedule.Update, defineSystem(
- [Res(UIEvents)],
- (events: UIEventQueue) => {
- const toggleEntities = world.getEntitiesWithComponents([Toggle]);
-
- if (!isEditor() || isPlayMode()) {
- const groupFirstOn = new Map<Entity, Entity>();
- for (const entity of toggleEntities) {
- if (initializedEntities.has(entity)) continue;
- initializedEntities.add(entity);
- const toggle = world.get(entity, Toggle) as ToggleData;
- if (!toggle.isOn || toggle.group === 0 || !world.valid(toggle.group)) continue;
- if (!groupFirstOn.has(toggle.group)) {
- groupFirstOn.set(toggle.group, entity);
- } else {
- toggle.isOn = false;
- world.insert(entity, Toggle, toggle);
- }
- }
- const togglesByGroup = new Map<Entity, Entity[]>();
- for (const entity of toggleEntities) {
- const toggle = world.get(entity, Toggle) as ToggleData;
- if (toggle.group !== 0 && world.valid(toggle.group)) {
- let group = togglesByGroup.get(toggle.group);
- if (!group) {
- group = [];
- togglesByGroup.set(toggle.group, group);
- }
- group.push(entity);
- }
- }
-
- for (const entity of toggleEntities) {
- ensureComponent(world, entity, Interactable, { enabled: true });
- if (!world.has(entity, UIInteraction)) continue;
-
- const interaction = world.get(entity, UIInteraction) as UIInteractionData;
- const toggle = world.get(entity, Toggle) as ToggleData;
- const interactable = world.get(entity, Interactable) as InteractableData;
-
- if (interaction.justPressed && interactable.enabled) {
- const groupEntity = toggle.group;
- const hasGroup = groupEntity !== 0 && world.valid(groupEntity)
- && world.has(groupEntity, ToggleGroup);
-
- if (toggle.isOn && hasGroup) {
- const group = world.get(groupEntity, ToggleGroup) as ToggleGroupData;
- if (!group.allowSwitchOff) continue;
- }
-
- toggle.isOn = !toggle.isOn;
- world.insert(entity, Toggle, toggle);
-
- if (toggle.isOn && hasGroup) {
- const siblings = togglesByGroup.get(groupEntity);
- if (siblings) {
- for (const other of siblings) {
- if (other === entity) continue;
- const otherToggle = world.get(other, Toggle) as ToggleData;
- if (otherToggle.isOn) {
- otherToggle.isOn = false;
- world.insert(other, Toggle, otherToggle);
- events.emit(other, 'change');
- }
- }
- }
- }
-
- events.emit(entity, 'change');
- }
-
- let color: Color;
- if (toggle.transition) {
- color = applyColorTransition(
- toggle.transition,
- interactable.enabled,
- interaction.pressed,
- interaction.hovered,
- );
- } else {
- const baseColor = toggle.isOn ? toggle.onColor : toggle.offColor;
- color = applyDefaultTint(baseColor, interactable.enabled, interaction.pressed, interaction.hovered);
- }
- setRendererColor(world, entity, color);
- }
- }
-
- for (const entity of toggleEntities) {
- const toggle = world.get(entity, Toggle) as ToggleData;
- withChildEntity(world, toggle.graphicEntity, (graphic) => {
- setRendererEnabled(world, graphic, toggle.isOn);
- });
- }
- },
- { name: 'ToggleSystem' }
- ), { runAfter: [SystemLabel.UIInteraction] });
- }
-}
-
-export const togglePlugin = new TogglePlugin();
diff --git a/sdk/src/ui/UIInteractionPlugin.ts b/sdk/src/ui/UIInteractionPlugin.ts
index d9d40b11a..be26779e0 100644
--- a/sdk/src/ui/UIInteractionPlugin.ts
+++ b/sdk/src/ui/UIInteractionPlugin.ts
@@ -10,8 +10,6 @@ import { playModeOnly } from '../env';
import { Interactable } from './Interactable';
import { UIInteraction } from './UIInteraction';
import type { UIInteractionData } from './UIInteraction';
-import { Button, ButtonState } from './Button';
-import type { ButtonData } from './Button';
import { UIEvents, UIEventQueue } from './UIEvents';
import type { UIEventType } from './UIEvents';
import { UICameraInfo } from './UICameraInfo';
@@ -19,9 +17,7 @@ import type { UICameraData } from './UICameraInfo';
import type { InteractableData } from './Interactable';
import { screenToWorld, createInvVPCache } from './uiMath';
import { platformDevicePixelRatio } from '../platform';
-import { applyColorTransition, applyDefaultTint, ensureComponent, walkParentChain, setEntityColor } from './uiHelpers';
-import { Image } from './Image';
-import type { ImageData } from './Image';
+import { ensureComponent, walkParentChain } from './uiHelpers';
import type { ESEngineModule, CppRegistry } from '../wasm';
import { UILayoutGeneration } from './UILayoutGeneration';
import { SystemLabel, PluginName } from '../systemLabels';
@@ -55,7 +51,6 @@ export class UIInteractionPlugin implements Plugin {
build(app: App): void {
registerComponent('Interactable', Interactable);
- registerComponent('Button', Button);
const world = app.world;
const module = app.wasmModule as ESEngineModule;
@@ -172,59 +167,6 @@ export class UIInteractionPlugin implements Plugin {
},
{ name: 'UIInteractionSystem' }
), { runAfter: [SystemLabel.UILayout], runIf: playModeOnly });
-
- const buttonInitialized = new Set<Entity>();
-
- app.addSystemToSchedule(Schedule.Update, defineSystem(
- [],
- () => {
- for (const e of buttonInitialized) {
- if (!world.valid(e)) buttonInitialized.delete(e);
- }
- const buttonEntities = world.getEntitiesWithComponents([Button]);
- for (const entity of buttonEntities) {
- ensureComponent(world, entity, Interactable, { enabled: true });
- if (!world.has(entity, UIInteraction)) continue;
-
- const interaction = world.get(entity, UIInteraction) as UIInteractionData;
- const button = world.get(entity, Button) as ButtonData;
- const interactable = world.get(entity, Interactable) as InteractableData;
-
- const isFirstFrame = !buttonInitialized.has(entity);
- const prevState = button.state;
-
- if (!interactable.enabled) {
- button.state = ButtonState.Disabled;
- } else if (interaction.pressed) {
- button.state = ButtonState.Pressed;
- } else if (interaction.hovered) {
- button.state = ButtonState.Hovered;
- } else {
- button.state = ButtonState.Normal;
- }
-
- if (isFirstFrame) {
- buttonInitialized.add(entity);
- }
-
- if ((isFirstFrame || prevState !== button.state) && button.transition) {
- const color = applyColorTransition(
- button.transition,
- interactable.enabled,
- interaction.pressed,
- interaction.hovered,
- );
- setEntityColor(world, entity, color);
- } else if ((isFirstFrame || prevState !== button.state) && !button.transition
- && world.has(entity, Image)) {
- const image = world.get(entity, Image) as ImageData;
- const tinted = applyDefaultTint(image.color, interactable.enabled, interaction.pressed, interaction.hovered);
- setEntityColor(world, entity, tinted);
- }
- }
- },
- { name: 'ButtonSystem' }
- ));
}
}
diff --git a/sdk/src/ui/index.ts b/sdk/src/ui/index.ts
index b6c94f1c5..5e56139b3 100644
--- a/sdk/src/ui/index.ts
+++ b/sdk/src/ui/index.ts
@@ -123,14 +123,6 @@ export {
type UIInteractionData,
} from './UIInteraction';
-// Button Component
-export {
- Button,
- ButtonState,
- type ButtonTransition,
- type ButtonData,
-} from './Button';
-
// UI Events
export {
UIEvents,
@@ -199,38 +191,6 @@ export {
imagePlugin,
} from './ImagePlugin';
-// Toggle Component
-export {
- Toggle,
- type ToggleTransition,
- type ToggleData,
-} from './Toggle';
-
-// ToggleGroup Component
-export {
- ToggleGroup,
- type ToggleGroupData,
-} from './ToggleGroup';
-
-// Toggle Plugin
-export {
- TogglePlugin,
- togglePlugin,
-} from './TogglePlugin';
-
-// ProgressBar Component
-export {
- ProgressBar,
- ProgressBarDirection,
- type ProgressBarData,
-} from './ProgressBar';
-
-// ProgressBar Plugin
-export {
- ProgressBarPlugin,
- progressBarPlugin,
-} from './ProgressBarPlugin';
-
// Draggable Component
export {
Draggable,
@@ -257,19 +217,6 @@ export {
scrollViewPlugin,
} from './ScrollViewPlugin';
-// Slider Component
-export {
- Slider,
- SliderDirection,
- type SliderData,
-} from './Slider';
-
-// Slider Plugin
-export {
- SliderPlugin,
- sliderPlugin,
-} from './SliderPlugin';
-
// Focusable Component
export {
Focusable,
@@ -296,18 +243,6 @@ export {
safeAreaPlugin,
} from './SafeAreaPlugin';
-// Dropdown Component
-export {
- Dropdown,
- type DropdownData,
-} from './Dropdown';
-
-// Dropdown Plugin
-export {
- DropdownPlugin,
- dropdownPlugin,
-} from './DropdownPlugin';
-
// FlexContainer Component
export {
FlexContainer,
diff --git a/sdk/src/uiPlugins.ts b/sdk/src/uiPlugins.ts
index b33a29502..4616d128c 100644
--- a/sdk/src/uiPlugins.ts
+++ b/sdk/src/uiPlugins.ts
@@ -4,22 +4,18 @@ import { uiInteractionPlugin } from './ui/UIInteractionPlugin';
import { uiLayoutPlugin } from './ui/UILayoutPlugin';
import { textInputPlugin } from './ui/TextInputPlugin';
import { imagePlugin } from './ui/ImagePlugin';
-import { togglePlugin } from './ui/TogglePlugin';
-import { progressBarPlugin } from './ui/ProgressBarPlugin';
import { dragPlugin } from './ui/DragPlugin';
import { scrollViewPlugin } from './ui/ScrollViewPlugin';
-import { sliderPlugin } from './ui/SliderPlugin';
import { focusPlugin } from './ui/FocusPlugin';
import { safeAreaPlugin } from './ui/SafeAreaPlugin';
-import { dropdownPlugin } from './ui/DropdownPlugin';
import { uiRenderOrderPlugin } from './ui/UIRenderOrderPlugin';
import type { Plugin } from './app';
export const uiPlugins: Plugin[] = [
textPlugin, uiMaskPlugin, uiLayoutPlugin,
- imagePlugin, progressBarPlugin,
- uiInteractionPlugin, dragPlugin, scrollViewPlugin, sliderPlugin,
- togglePlugin, textInputPlugin,
- focusPlugin, safeAreaPlugin, dropdownPlugin,
+ imagePlugin,
+ uiInteractionPlugin, dragPlugin, scrollViewPlugin,
+ textInputPlugin,
+ focusPlugin, safeAreaPlugin,
uiRenderOrderPlugin,
];
diff --git a/sdk/src/webAppFactory.ts b/sdk/src/webAppFactory.ts
index 20d6164f0..dd2141b48 100644
--- a/sdk/src/webAppFactory.ts
+++ b/sdk/src/webAppFactory.ts
@@ -22,14 +22,10 @@ export { uiRenderOrderPlugin, UIRenderOrderPlugin } from './ui/UIRenderOrderPlug
export { textInputPlugin, TextInputPlugin } from './ui/TextInputPlugin';
export { imagePlugin, ImagePlugin } from './ui/ImagePlugin';
-export { progressBarPlugin, ProgressBarPlugin } from './ui/ProgressBarPlugin';
-export { sliderPlugin, SliderPlugin } from './ui/SliderPlugin';
-export { togglePlugin, TogglePlugin } from './ui/TogglePlugin';
export { dragPlugin, DragPlugin } from './ui/DragPlugin';
export { scrollViewPlugin, ScrollViewPlugin } from './ui/ScrollViewPlugin';
export { focusPlugin, FocusPlugin } from './ui/FocusPlugin';
export { safeAreaPlugin, SafeAreaPlugin } from './ui/SafeAreaPlugin';
-export { dropdownPlugin, DropdownPlugin } from './ui/DropdownPlugin';
export { PhysicsPlugin, PhysicsEvents, Physics, loadPhysicsModule } from './physics';
export { AnimationPlugin, animationPlugin } from './animation';
diff --git a/sdk/tests/dropdown-interaction.test.ts b/sdk/tests/dropdown-interaction.test.ts
deleted file mode 100644
index 51f6fe9d5..000000000
--- a/sdk/tests/dropdown-interaction.test.ts
+++ /dev/null
@@ -1,345 +0,0 @@
-/**
- * Regression test: UIInteractionPlugin must propagate justPressed through
- * Emscripten-backed UIInteraction components.
- *
- * Bug: UIInteractionPlugin passes Emscripten wrapper objects directly to
- * world.insert(). Object.entries() on these wrappers may not enumerate
- * properties, causing insertBuiltin to reset all fields to defaults.
- * This means justPressed is never set to true, so the dropdown never opens.
- */
-import { describe, it, expect, beforeAll } from 'vitest';
-import { existsSync } from 'fs';
-import { resolve } from 'path';
-import { App } from '../src/app';
-import { Transform, Sprite, Name, Canvas } from '../src/component';
-import { UIRect } from '../src/ui/UIRect';
-import { UICameraInfo } from '../src/ui/UICameraInfo';
-import { Input, InputState } from '../src/input';
-import { UIEvents, UIEventQueue } from '../src/ui/UIEvents';
-import { uiLayoutPlugin } from '../src/ui/UILayoutPlugin';
-import { uiRenderOrderPlugin } from '../src/ui/UIRenderOrderPlugin';
-import { uiInteractionPlugin } from '../src/ui/UIInteractionPlugin';
-import { dropdownPlugin } from '../src/ui/DropdownPlugin';
-import { Dropdown } from '../src/ui/Dropdown';
-import type { DropdownData } from '../src/ui/Dropdown';
-import { Interactable } from '../src/ui/Interactable';
-import { UIInteraction } from '../src/ui/UIInteraction';
-import type { UIInteractionData } from '../src/ui/UIInteraction';
-import { Text } from '../src/ui/text';
-import type { TextData } from '../src/ui/text';
-import type { ESEngineModule, CppRegistry } from '../src/wasm';
-import { loadWasmModule } from './helpers/loadWasm';
-
-const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
-const HAS_WASM = existsSync(WASM_PATH);
-
-describe.skipIf(!HAS_WASM)('Dropdown Interaction (WASM integration)', () => {
- let module: ESEngineModule;
-
- beforeAll(async () => {
- module = await loadWasmModule();
- });
-
- function createApp() {
- const app = App.new();
- const registry = new module.Registry() as unknown as CppRegistry;
- app.connectCpp(registry, module);
-
- const vp = new Float32Array(16);
- vp[0] = 2 / 800; vp[5] = 2 / 600; vp[10] = -1; vp[15] = 1;
- app.insertResource(UICameraInfo, {
- viewProjection: vp,
- vpX: 0, vpY: 0, vpW: 800, vpH: 600,
- screenW: 800, screenH: 600,
- worldLeft: -400, worldBottom: -300, worldRight: 400, worldTop: 300,
- worldMouseX: 0, worldMouseY: 0,
- valid: true,
- });
- app.insertResource(Input, new InputState());
- app.insertResource(UIEvents, new UIEventQueue());
-
- app.addPlugin(uiLayoutPlugin);
- app.addPlugin(uiRenderOrderPlugin);
- app.addPlugin(uiInteractionPlugin);
- app.addPlugin(dropdownPlugin);
-
- return { app, registry };
- }
-
- function disposeApp(app: App, registry: CppRegistry): void {
- const world = app.world;
- for (const e of world.getAllEntities()) {
- try { world.despawn(e); } catch (_) {}
- }
- world.disconnectCpp();
- (registry as any).delete();
- }
-
- function createDropdownHierarchy(app: App) {
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(root, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
-
- const ddEntity = world.spawn();
- world.setParent(ddEntity, root);
- world.insert(ddEntity, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 160, y: 30 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(ddEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(ddEntity, Sprite, {
- texture: 0xFFFFFFFF, color: { r: 1, g: 1, b: 1, a: 1 },
- size: { x: 160, y: 30 }, uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 }, layer: 0, flipX: false, flipY: false,
- });
- world.insert(ddEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
-
- const listEntity = world.spawn();
- world.setParent(listEntity, ddEntity);
- world.insert(listEntity, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 1, y: 0 },
- offsetMin: { x: 0, y: -90 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 160, y: 90 },
- pivot: { x: 0.5, y: 1 },
- });
- world.insert(listEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(listEntity, Sprite, {
- texture: 0xFFFFFFFF, color: { r: 1, g: 1, b: 1, a: 1 },
- size: { x: 160, y: 90 }, uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 }, layer: 0, flipX: false, flipY: false,
- enabled: false,
- });
- world.insert(listEntity, Interactable, { enabled: false, blockRaycast: true, raycastTarget: true });
-
- const labelEntity = world.spawn();
- world.setParent(labelEntity, ddEntity);
- world.insert(labelEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(labelEntity, Text, {
- content: 'Select...',
- fontFamily: 'Arial',
- fontSize: 16,
- color: { r: 1, g: 1, b: 1, a: 1 },
- align: 0, verticalAlign: 1,
- wordWrap: false, overflow: 1, lineHeight: 1.2,
- });
-
- world.insert(ddEntity, Dropdown, {
- options: ['Option A', 'Option B', 'Option C'],
- selectedIndex: 0,
- isOpen: false,
- listEntity,
- labelEntity,
- });
-
- return { root, ddEntity, listEntity, labelEntity };
- }
-
- it('should propagate justPressed through C++ UIInteraction component', async () => {
- const { app, registry } = createApp();
- const { ddEntity } = createDropdownHierarchy(app);
- const world = app.world;
-
- // First tick to let layout compute positions
- await app.tick(1 / 60);
-
- // The ddEntity should now have UIInteraction from the hit test system (getOrEmplace)
- // Verify UIInteraction round-trip: read from C++, modify, write back
- world.insert(ddEntity, UIInteraction, {
- hovered: false, pressed: false,
- justPressed: true, justReleased: false,
- });
-
- const readBack = world.get(ddEntity, UIInteraction) as UIInteractionData;
- expect(readBack.justPressed).toBe(true);
-
- // Now test the Emscripten wrapper round-trip
- const wrapper = world.get(ddEntity, UIInteraction) as UIInteractionData;
- wrapper.justPressed = false;
- wrapper.hovered = true;
- world.insert(ddEntity, UIInteraction, wrapper);
-
- const readBack2 = world.get(ddEntity, UIInteraction) as UIInteractionData;
- expect(readBack2.hovered).toBe(true);
- expect(readBack2.justPressed).toBe(false);
-
- disposeApp(app, registry);
- });
-
- it('should detect click on dropdown via C++ hit test and open it', async () => {
- const { app, registry } = createApp();
- const { ddEntity, listEntity } = createDropdownHierarchy(app);
- const world = app.world;
-
- // Initial tick to compute layout
- await app.tick(1 / 60);
-
- // Verify dropdown is closed
- let dd = world.get(ddEntity, Dropdown) as DropdownData;
- expect(dd.isOpen).toBe(false);
-
- // The ddEntity should be at world origin (0,0) with size 160x30
- // Run hit test with mouse at (0, 0) - center of dropdown
- module.uiHitTest_update(registry, 0, 0, false, true, false);
- const hitRaw = module.uiHitTest_getHitEntity();
- const hitEntity = hitRaw === 0xFFFFFFFF ? null : hitRaw;
-
- // The hit test should detect the dropdown entity
- expect(hitEntity).toBe(ddEntity);
-
- // Simulate a mouse click through the Input resource so UIInteractionPlugin
- // naturally sets justPressed on the hit entity
- const input = app.getResource(Input) as InputState;
- input.mouseX = 400;
- input.mouseY = 300;
- input.mouseButtons.add(0);
- input.mouseButtonsPressed.add(0);
-
- await app.tick(1 / 60);
-
- // Clear mouse state after the press frame
- input.mouseButtonsPressed.clear();
-
- dd = world.get(ddEntity, Dropdown) as DropdownData;
- expect(dd.isOpen).toBe(true);
-
- disposeApp(app, registry);
- });
-
- it('should correctly round-trip UIInteraction through Emscripten wrapper', async () => {
- const { app, registry } = createApp();
- const { ddEntity } = createDropdownHierarchy(app);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- // Set known values via plain object
- world.insert(ddEntity, UIInteraction, {
- hovered: true, pressed: false,
- justPressed: false, justReleased: true,
- });
-
- // Read back as Emscripten wrapper
- const wrapper = world.get(ddEntity, UIInteraction);
-
- // Check Object.entries on the wrapper (this is what insertBuiltin uses)
- const entries = Object.entries(wrapper as Record<string, unknown>);
- const entryKeys = entries.map(([k]) => k).sort();
-
- // All 4 properties must be enumerable
- expect(entryKeys).toContain('hovered');
- expect(entryKeys).toContain('pressed');
- expect(entryKeys).toContain('justPressed');
- expect(entryKeys).toContain('justReleased');
-
- // Modify and write back through the wrapper
- (wrapper as any).hovered = false;
- (wrapper as any).justPressed = true;
- (wrapper as any).justReleased = false;
- world.insert(ddEntity, UIInteraction, wrapper as UIInteractionData);
-
- // Read back and verify modifications persisted
- const result = world.get(ddEntity, UIInteraction) as UIInteractionData;
- expect(result.hovered).toBe(false);
- expect(result.pressed).toBe(false);
- expect(result.justPressed).toBe(true);
- expect(result.justReleased).toBe(false);
-
- disposeApp(app, registry);
- });
-
- it('should read sprite color fields as {r,g,b,a} not {x,y,z,w}', () => {
- const { app, registry } = createApp();
- const { ddEntity } = createDropdownHierarchy(app);
- const world = app.world;
-
- // Write color using {r,g,b,a} format
- world.insert(ddEntity, Sprite, {
- texture: 0xFFFFFFFF,
- color: { r: 0.5, g: 0.6, b: 0.7, a: 0.8 },
- size: { x: 100, y: 50 },
- uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 },
- layer: 0, flipX: false, flipY: false,
- });
-
- // Read back - should use {r,g,b,a} format, not {x,y,z,w}
- const s = world.get(ddEntity, Sprite) as any;
-
- // This is the critical test: color must have r,g,b,a fields
- expect(s.color.r).toBeCloseTo(0.5, 1);
- expect(s.color.g).toBeCloseTo(0.6, 1);
- expect(s.color.b).toBeCloseTo(0.7, 1);
- expect(s.color.a).toBeCloseTo(0.8, 1);
-
- // Verify the color check used in initDropdownAppearance
- world.insert(ddEntity, Sprite, {
- texture: 0xFFFFFFFF,
- color: { r: 1, g: 1, b: 1, a: 1 },
- size: { x: 100, y: 50 },
- uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 },
- layer: 0, flipX: false, flipY: false,
- });
-
- const white = world.get(ddEntity, Sprite) as any;
- expect(white.color.r).toBe(1);
- expect(white.color.g).toBe(1);
- expect(white.color.b).toBe(1);
- expect(white.color.a).toBe(1);
-
- disposeApp(app, registry);
- });
-
- it('should fix white label text to black after initialization', async () => {
- const { app, registry } = createApp();
- const { labelEntity } = createDropdownHierarchy(app);
- const world = app.world;
-
- // Before tick, text is white
- let text = world.get(labelEntity, Text) as TextData;
- expect(text.color.r).toBe(1);
- expect(text.color.g).toBe(1);
- expect(text.color.b).toBe(1);
-
- // After tick, syncLabel should fix to black
- await app.tick(1 / 60);
-
- text = world.get(labelEntity, Text) as TextData;
- const isWhite = text.color.r === 1 && text.color.g === 1 && text.color.b === 1;
- expect(isWhite).toBe(false);
-
- disposeApp(app, registry);
- });
-});
diff --git a/sdk/tests/dropdown-layout.test.ts b/sdk/tests/dropdown-layout.test.ts
deleted file mode 100644
index 6fc7a6cf4..000000000
--- a/sdk/tests/dropdown-layout.test.ts
+++ /dev/null
@@ -1,399 +0,0 @@
-/**
- * Regression test: Dropdown option entities must have distinct positions.
- *
- * Bug: C++ UILayoutSystem overwrites manually-set Transform positions based on
- * UIRect anchors. When all options share the same UIRect anchors, they overlap.
- * Fix: use UIRect offsetMin.y to differentiate positions so the layout system
- * computes unique positions for each option.
- */
-import { describe, it, expect, beforeAll } from 'vitest';
-import { existsSync } from 'fs';
-import { resolve } from 'path';
-import { App } from '../src/app';
-import { Transform, Sprite, Name, Canvas } from '../src/component';
-import type { SpriteData } from '../src/component';
-import { UIRenderer } from '../src/ui/UIRenderer';
-import { UIRect } from '../src/ui/UIRect';
-import { UICameraInfo } from '../src/ui/UICameraInfo';
-import { Input, InputState } from '../src/input';
-import { UIEvents, UIEventQueue } from '../src/ui/UIEvents';
-import { uiLayoutPlugin } from '../src/ui/UILayoutPlugin';
-import { uiRenderOrderPlugin } from '../src/ui/UIRenderOrderPlugin';
-import { uiInteractionPlugin } from '../src/ui/UIInteractionPlugin';
-import { dropdownPlugin } from '../src/ui/DropdownPlugin';
-import { Dropdown } from '../src/ui/Dropdown';
-import type { DropdownData } from '../src/ui/Dropdown';
-import { Interactable } from '../src/ui/Interactable';
-import type { InteractableData } from '../src/ui/Interactable';
-import { UIInteraction } from '../src/ui/UIInteraction';
-import { Text } from '../src/ui/text';
-import type { TextData } from '../src/ui/text';
-import { DROPDOWN_ITEM_HEIGHT } from '../src/ui/uiConstants';
-import type { ESEngineModule, CppRegistry } from '../src/wasm';
-import { loadWasmModule } from './helpers/loadWasm';
-
-const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
-const HAS_WASM = existsSync(WASM_PATH);
-
-describe.skipIf(!HAS_WASM)('Dropdown Layout (WASM integration)', () => {
- let module: ESEngineModule;
-
- beforeAll(async () => {
- module = await loadWasmModule();
- });
-
- function createDropdownApp() {
- const app = App.new();
- const registry = new module.Registry() as unknown as CppRegistry;
- app.connectCpp(registry, module);
-
- const vp = new Float32Array(16);
- vp[0] = 2 / 800; vp[5] = 2 / 600; vp[10] = -1; vp[15] = 1;
- app.insertResource(UICameraInfo, {
- viewProjection: vp,
- vpX: 0, vpY: 0, vpW: 800, vpH: 600,
- screenW: 800, screenH: 600,
- worldLeft: -400, worldBottom: -300, worldRight: 400, worldTop: 300,
- worldMouseX: 0, worldMouseY: 0,
- valid: true,
- });
- app.insertResource(Input, new InputState());
- app.insertResource(UIEvents, new UIEventQueue());
-
- app.addPlugin(uiLayoutPlugin);
- app.addPlugin(uiRenderOrderPlugin);
- app.addPlugin(uiInteractionPlugin);
- app.addPlugin(dropdownPlugin);
-
- return { app, registry };
- }
-
- function disposeApp(app: App, registry: CppRegistry): void {
- const world = app.world;
- for (const e of world.getAllEntities()) {
- try { world.despawn(e); } catch (_) {}
- }
- world.disconnectCpp();
- (registry as any).delete();
- }
-
- function makeTransform() {
- return {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- };
- }
-
- function createDropdownHierarchy(app: App) {
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(root, Transform, makeTransform());
-
- const ddEntity = world.spawn();
- world.setParent(ddEntity, root);
- world.insert(ddEntity, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 160, y: 30 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(ddEntity, Transform, makeTransform());
- world.insert(ddEntity, Sprite, {
- texture: 0, color: { r: 0.9, g: 0.9, b: 0.9, a: 1 },
- size: { x: 160, y: 30 }, uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 }, layer: 0, flipX: false, flipY: false,
- });
- world.insert(ddEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
-
- // List entity (positioned below dropdown)
- const listEntity = world.spawn();
- world.setParent(listEntity, ddEntity);
- world.insert(listEntity, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 1, y: 0 },
- offsetMin: { x: 0, y: -90 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 160, y: 90 },
- pivot: { x: 0.5, y: 1 },
- });
- world.insert(listEntity, Transform, makeTransform());
- world.insert(listEntity, Sprite, {
- texture: 0, color: { r: 1, g: 1, b: 1, a: 1 },
- size: { x: 160, y: 90 }, uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 }, layer: 0, flipX: false, flipY: false,
- enabled: false,
- });
- world.insert(listEntity, Interactable, { enabled: false, blockRaycast: true, raycastTarget: true });
-
- // Label entity with Text (default white color — simulates real scene)
- const labelEntity = world.spawn();
- world.setParent(labelEntity, ddEntity);
- world.insert(labelEntity, Transform, makeTransform());
- world.insert(labelEntity, Text, {
- content: '',
- fontFamily: 'Arial',
- fontSize: 16,
- color: { r: 1, g: 1, b: 1, a: 1 },
- align: 0,
- verticalAlign: 1,
- wordWrap: false,
- overflow: 1,
- lineHeight: 1.2,
- });
-
- world.insert(ddEntity, Dropdown, {
- options: ['Option A', 'Option B', 'Option C'],
- selectedIndex: 0,
- isOpen: false,
- listEntity,
- labelEntity,
- });
-
- return { root, ddEntity, listEntity, labelEntity };
- }
-
- it('should create option entities with distinct Y positions after opening', async () => {
- const { app, registry } = createDropdownApp();
- const { ddEntity, listEntity } = createDropdownHierarchy(app);
-
- // Initial tick to set up layout
- await app.tick(1 / 60);
-
- // Open dropdown by setting isOpen directly and simulating click via Input
- const world = app.world;
- const input = app.getResource(Input) as InputState;
- input.mouseX = 400;
- input.mouseY = 300;
- input.mouseButtons.add(0);
- input.mouseButtonsPressed.add(0);
-
- await app.tick(1 / 60);
-
- input.mouseButtonsPressed.clear();
-
- // After tick, the dropdown should be open and options should exist
- const ddData = world.get(ddEntity, Dropdown) as any;
-
- // Find option entities (children of listEntity that have Sprite + Interactable)
- const allEntities = world.getAllEntities();
- const optionEntities: number[] = [];
- for (const e of allEntities) {
- if (e === listEntity || e === ddEntity) continue;
- if ((!world.has(e, Sprite) && !world.has(e, UIRenderer)) || !world.has(e, Interactable)) continue;
- if (!world.has(e, UIRect)) continue;
- // Check if this is a child of listEntity
- if (world.has(e, Transform)) {
- const name = world.has(e, Name) ? (world.get(e, Name) as any).value : '';
- if (name.startsWith('Option_')) {
- optionEntities.push(e);
- }
- }
- }
-
- // Should have 3 option entities
- expect(optionEntities.length).toBe(3);
-
- // Run another tick so layout computes positions
- await app.tick(1 / 60);
-
- // Each option should have a DISTINCT Y position
- const yPositions = optionEntities.map(e => {
- const t = registry.getTransform(e);
- return t.position.y;
- });
-
- console.log('Option Y positions:', yPositions);
-
- // All Y positions must be different
- const uniqueY = new Set(yPositions.map(y => Math.round(y)));
- expect(uniqueY.size).toBe(3);
-
- // Options should be spaced by DROPDOWN_ITEM_HEIGHT
- yPositions.sort((a, b) => b - a); // highest first
- for (let i = 1; i < yPositions.length; i++) {
- const gap = yPositions[i - 1] - yPositions[i];
- expect(gap).toBeCloseTo(DROPDOWN_ITEM_HEIGHT, 0);
- }
-
- disposeApp(app, registry);
- });
-
- it('should have option sprites with non-zero size after layout', async () => {
- const { app, registry } = createDropdownApp();
- const { ddEntity, listEntity } = createDropdownHierarchy(app);
-
- await app.tick(1 / 60);
-
- const world = app.world;
- const input = app.getResource(Input) as InputState;
- input.mouseX = 400;
- input.mouseY = 300;
- input.mouseButtons.add(0);
- input.mouseButtonsPressed.add(0);
- await app.tick(1 / 60);
- input.mouseButtonsPressed.clear();
-
- await app.tick(1 / 60);
-
- const allEntities = world.getAllEntities();
- const optionEntities: number[] = [];
- for (const e of allEntities) {
- if (!world.has(e, Name)) continue;
- const name = (world.get(e, Name) as any).value;
- if (name.startsWith('Option_')) {
- optionEntities.push(e);
- }
- }
-
- expect(optionEntities.length).toBe(3);
-
- for (const e of optionEntities) {
- expect(world.has(e, UIRenderer)).toBe(true);
- const renderer = world.get(e, UIRenderer) as any;
- expect(renderer.enabled).toBe(true);
- expect(world.has(e, UIRect)).toBe(true);
- }
-
- disposeApp(app, registry);
- });
-
- it('should hide list entity on initialization when dropdown is closed', async () => {
- const { app, registry } = createDropdownApp();
- const { listEntity } = createDropdownHierarchy(app);
- const world = app.world;
-
- // Simulate list entity starting with enabled=true (scene not pre-configured)
- world.insert(listEntity, Sprite, {
- texture: 0, color: { r: 1, g: 1, b: 1, a: 1 },
- size: { x: 160, y: 90 }, uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 }, layer: 0, flipX: false, flipY: false,
- enabled: true,
- });
- world.insert(listEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
-
- await app.tick(1 / 60);
-
- const sprite = registry.getSprite(listEntity);
- expect(sprite.enabled).toBe(false);
-
- const interactable = world.get(listEntity, Interactable) as InteractableData;
- expect(interactable.enabled).toBe(false);
-
- disposeApp(app, registry);
- });
-
- it('should set label text color to non-white after sync', async () => {
- const { app, registry } = createDropdownApp();
- const { labelEntity } = createDropdownHierarchy(app);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- const text = world.get(labelEntity, Text) as TextData;
- const isWhite = text.color.r === 1 && text.color.g === 1 && text.color.b === 1;
- expect(isWhite).toBe(false);
- expect(text.content).toBe('Option A');
-
- disposeApp(app, registry);
- });
-
- it('should preserve label placeholder text when selectedIndex is -1', async () => {
- const { app, registry } = createDropdownApp();
- const { ddEntity, labelEntity } = createDropdownHierarchy(app);
- const world = app.world;
-
- // Set label placeholder text and reset selectedIndex to -1
- world.insert(labelEntity, Text, {
- content: 'Select...',
- fontFamily: 'Arial',
- fontSize: 16,
- color: { r: 0.5, g: 0.5, b: 0.5, a: 1 },
- align: 0, verticalAlign: 1,
- wordWrap: false, overflow: 1, lineHeight: 1.2,
- });
-
- const dd = world.get(ddEntity, Dropdown) as DropdownData;
- dd.selectedIndex = -1;
- world.insert(ddEntity, Dropdown, dd);
-
- await app.tick(1 / 60);
-
- const text = world.get(labelEntity, Text) as TextData;
- expect(text.content).toBe('Select...');
-
- disposeApp(app, registry);
- });
-
- it('should fix white label text even when selectedIndex is -1', async () => {
- const { app, registry } = createDropdownApp();
- const { ddEntity, labelEntity } = createDropdownHierarchy(app);
- const world = app.world;
-
- // White text + selectedIndex -1 (placeholder scenario from scene)
- world.insert(labelEntity, Text, {
- content: 'Select...',
- fontFamily: 'Arial',
- fontSize: 16,
- color: { r: 1, g: 1, b: 1, a: 1 },
- align: 0, verticalAlign: 1,
- wordWrap: false, overflow: 1, lineHeight: 1.2,
- });
-
- const dd = world.get(ddEntity, Dropdown) as DropdownData;
- dd.selectedIndex = -1;
- world.insert(ddEntity, Dropdown, dd);
-
- await app.tick(1 / 60);
-
- const text = world.get(labelEntity, Text) as TextData;
- // White text should be fixed to black even without valid selection
- const isWhite = text.color.r === 1 && text.color.g === 1 && text.color.b === 1;
- expect(isWhite).toBe(false);
- // Placeholder content should be preserved
- expect(text.content).toBe('Select...');
-
- disposeApp(app, registry);
- });
-
- it('should open dropdown and show list entity on click', async () => {
- const { app, registry } = createDropdownApp();
- const { ddEntity, listEntity } = createDropdownHierarchy(app);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- // Verify list is hidden initially
- let sprite = registry.getSprite(listEntity);
- expect(sprite.enabled).toBe(false);
-
- // Simulate click via Input resource
- const input = app.getResource(Input) as InputState;
- input.mouseX = 400;
- input.mouseY = 300;
- input.mouseButtons.add(0);
- input.mouseButtonsPressed.add(0);
- await app.tick(1 / 60);
- input.mouseButtonsPressed.clear();
-
- const dd = world.get(ddEntity, Dropdown) as any;
- expect(dd.isOpen).toBe(true);
-
- sprite = registry.getSprite(listEntity);
- expect(sprite.enabled).toBe(true);
-
- disposeApp(app, registry);
- });
-});
diff --git a/sdk/tests/dropdown-list-text.test.ts b/sdk/tests/dropdown-list-text.test.ts
deleted file mode 100644
index dba0dab8a..000000000
--- a/sdk/tests/dropdown-list-text.test.ts
+++ /dev/null
@@ -1,299 +0,0 @@
-/**
- * Regression test: a Text entity manually placed as a child of the dropdown's
- * list entity should be visible even when the dropdown is closed.
- *
- * Bug: user places a Text entity under the list entity. The text is only
- * visible when the dropdown is open, not when closed.
- */
-import { describe, it, expect, beforeAll } from 'vitest';
-import { existsSync } from 'fs';
-import { resolve } from 'path';
-import { App } from '../src/app';
-import { Transform, Sprite, Name, Canvas } from '../src/component';
-import type { SpriteData, TransformData } from '../src/component';
-import { UIRect } from '../src/ui/UIRect';
-import { UICameraInfo } from '../src/ui/UICameraInfo';
-import { Input, InputState } from '../src/input';
-import { UIEvents, UIEventQueue } from '../src/ui/UIEvents';
-import { uiLayoutPlugin } from '../src/ui/UILayoutPlugin';
-import { uiRenderOrderPlugin } from '../src/ui/UIRenderOrderPlugin';
-import { uiInteractionPlugin } from '../src/ui/UIInteractionPlugin';
-import { dropdownPlugin } from '../src/ui/DropdownPlugin';
-import { Dropdown } from '../src/ui/Dropdown';
-import { Interactable } from '../src/ui/Interactable';
-import { UIInteraction } from '../src/ui/UIInteraction';
-import type { UIInteractionData } from '../src/ui/UIInteraction';
-import { Text } from '../src/ui/text';
-import { INVALID_TEXTURE } from '../src/types';
-import type { ESEngineModule, CppRegistry } from '../src/wasm';
-import { loadWasmModule } from './helpers/loadWasm';
-
-const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
-const HAS_WASM = existsSync(WASM_PATH);
-
-describe.skipIf(!HAS_WASM)('Dropdown list child text visibility (WASM)', () => {
- let module: ESEngineModule;
-
- beforeAll(async () => {
- module = await loadWasmModule();
- });
-
- function createApp() {
- const app = App.new();
- const registry = new module.Registry() as unknown as CppRegistry;
- app.connectCpp(registry, module);
-
- const vp = new Float32Array(16);
- vp[0] = 2 / 800; vp[5] = 2 / 600; vp[10] = -1; vp[15] = 1;
- app.insertResource(UICameraInfo, {
- viewProjection: vp,
- vpX: 0, vpY: 0, vpW: 800, vpH: 600,
- screenW: 800, screenH: 600,
- worldLeft: -400, worldBottom: -300, worldRight: 400, worldTop: 300,
- worldMouseX: 0, worldMouseY: 0,
- valid: true,
- });
- app.insertResource(Input, new InputState());
- app.insertResource(UIEvents, new UIEventQueue());
-
- app.addPlugin(uiLayoutPlugin);
- app.addPlugin(uiRenderOrderPlugin);
- app.addPlugin(uiInteractionPlugin);
- app.addPlugin(dropdownPlugin);
-
- return { app, registry };
- }
-
- function disposeApp(app: App, registry: CppRegistry): void {
- const world = app.world;
- for (const e of world.getAllEntities()) {
- try { world.despawn(e); } catch (_) {}
- }
- world.disconnectCpp();
- (registry as any).delete();
- }
-
- /**
- * Build a dropdown hierarchy that mirrors the editor's default setup,
- * plus a user-placed Text entity as a child of the list.
- */
- function buildDropdownWithUserText(app: App) {
- const world = app.world;
-
- // Canvas root (full-screen canvas)
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(root, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
-
- // Dropdown button entity
- const ddEntity = world.spawn();
- world.setParent(ddEntity, root);
- world.insert(ddEntity, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 }, anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 160, y: 30 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(ddEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(ddEntity, Sprite, {
- texture: INVALID_TEXTURE, color: { r: 0.9, g: 0.9, b: 0.9, a: 1 },
- size: { x: 160, y: 30 }, uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 }, layer: 0, flipX: false, flipY: false,
- });
- world.insert(ddEntity, Interactable, { enabled: true, blockRaycast: true, raycastTarget: true });
-
- // List entity (initially hidden)
- const listEntity = world.spawn();
- world.setParent(listEntity, ddEntity);
- world.insert(listEntity, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 0 },
- offsetMin: { x: 0, y: -90 }, offsetMax: { x: 0, y: 0 },
- size: { x: 160, y: 90 }, pivot: { x: 0.5, y: 1 },
- });
- world.insert(listEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 0, y: 0, z: 0 },
- });
- world.insert(listEntity, Sprite, {
- texture: INVALID_TEXTURE, color: { r: 1, g: 1, b: 1, a: 1 },
- size: { x: 160, y: 90 }, uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 }, layer: 0, flipX: false, flipY: false,
- });
- world.insert(listEntity, Interactable, { enabled: false, blockRaycast: true, raycastTarget: true });
-
- // Label entity
- const labelEntity = world.spawn();
- world.setParent(labelEntity, ddEntity);
- world.insert(labelEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(labelEntity, Text, {
- content: 'Select...', fontFamily: 'Arial', fontSize: 16,
- color: { r: 1, g: 1, b: 1, a: 1 },
- align: 0, verticalAlign: 1,
- wordWrap: false, overflow: 1, lineHeight: 1.2,
- });
-
- // Dropdown component
- world.insert(ddEntity, Dropdown, {
- options: ['Option A', 'Option B', 'Option C'],
- selectedIndex: 0, isOpen: false,
- listEntity, labelEntity,
- });
-
- // === USER'S TEXT ENTITY (child of list) ===
- const userTextEntity = world.spawn();
- world.setParent(userTextEntity, listEntity);
- world.insert(userTextEntity, Transform, {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(userTextEntity, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 160, y: 90 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(userTextEntity, Text, {
- content: 'Custom Label', fontFamily: 'Arial', fontSize: 14,
- color: { r: 0, g: 0, b: 0, a: 1 },
- align: 1, verticalAlign: 1,
- wordWrap: false, overflow: 0, lineHeight: 1.2,
- });
- // Simulate what TextPlugin would do: add a Sprite
- world.insert(userTextEntity, Sprite, {
- texture: 42, // simulated valid texture handle
- color: { r: 1, g: 1, b: 1, a: 1 },
- size: { x: 80, y: 14 },
- uvOffset: { x: 0, y: 0 },
- uvScale: { x: 1, y: 1 },
- layer: 0,
- flipX: false, flipY: false,
- material: 0,
- enabled: true,
- });
-
- return { root, ddEntity, listEntity, labelEntity, userTextEntity };
- }
-
- it('list entity scale should be normalized from (0,0,0) to (1,1,1) on init', async () => {
- const { app, registry } = createApp();
- const { ddEntity, listEntity, userTextEntity } = buildDropdownWithUserText(app);
- const world = app.world;
-
- // Verify initial scale is (0,0,0) as the editor sets it
- const beforeTick = world.get(listEntity, Transform) as TransformData;
- expect(beforeTick.scale.x).toBe(0);
- expect(beforeTick.scale.y).toBe(0);
-
- // First tick initializes the dropdown (closed state)
- await app.tick(1 / 60);
-
- // DropdownPlugin should normalize list scale to (1,1,1) so children are visible
- const listTransform = world.get(listEntity, Transform) as TransformData;
- expect(listTransform.scale.x).toBe(1);
- expect(listTransform.scale.y).toBe(1);
- expect(listTransform.scale.z).toBe(1);
-
- // The list sprite should be disabled (dropdown closed)
- const listSprite = world.get(listEntity, Sprite) as SpriteData;
- expect(listSprite.enabled).toBe(false);
-
- // The user's text sprite should still be ENABLED
- const userSprite = world.get(userTextEntity, Sprite) as SpriteData;
- expect(userSprite.enabled).toBe(true);
-
- disposeApp(app, registry);
- });
-
- it('user text entity should have non-zero sprite size after layout', async () => {
- const { app, registry } = createApp();
- const { userTextEntity } = buildDropdownWithUserText(app);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- const sprite = world.get(userTextEntity, Sprite) as SpriteData;
- expect(sprite.size.x).toBeGreaterThan(0);
- expect(sprite.size.y).toBeGreaterThan(0);
-
- disposeApp(app, registry);
- });
-
- it('user text entity should get layer from UIRenderOrderSystem', async () => {
- const { app, registry } = createApp();
- const { ddEntity, listEntity, userTextEntity } = buildDropdownWithUserText(app);
- const world = app.world;
-
- await app.tick(1 / 60);
-
- const ddSprite = world.get(ddEntity, Sprite) as SpriteData;
- const listSprite = world.get(listEntity, Sprite) as SpriteData;
- const userSprite = world.get(userTextEntity, Sprite) as SpriteData;
-
- // UIRenderOrderSystem should assign layers in hierarchy order:
- // root → ddEntity → listEntity → userTextEntity → labelEntity
- // User text should have a HIGHER layer than the list
- expect(userSprite.layer).toBeGreaterThan(listSprite.layer);
-
- disposeApp(app, registry);
- });
-
- it('user text entity survives dropdown open/close cycle', async () => {
- const { app, registry } = createApp();
- const { ddEntity, listEntity, userTextEntity } = buildDropdownWithUserText(app);
- const world = app.world;
-
- // Initialize
- await app.tick(1 / 60);
-
- // Simulate click to open dropdown via Input resource
- const input = app.getResource(Input) as InputState;
- input.mouseX = 400;
- input.mouseY = 300;
- input.mouseButtons.add(0);
- input.mouseButtonsPressed.add(0);
- await app.tick(1 / 60);
- input.mouseButtonsPressed.clear();
-
- // Verify dropdown is open and text entity still exists
- const dd = world.get(ddEntity, Dropdown) as any;
- expect(dd.isOpen).toBe(true);
- expect(world.valid(userTextEntity)).toBe(true);
- expect(world.has(userTextEntity, Sprite)).toBe(true);
-
- const openSprite = world.get(userTextEntity, Sprite) as SpriteData;
- expect(openSprite.enabled).toBe(true);
-
- // Close dropdown via another click
- input.mouseButtonsPressed.add(0);
- await app.tick(1 / 60);
- input.mouseButtonsPressed.clear();
-
- // After close, user text entity must still exist and be enabled
- expect(world.valid(userTextEntity)).toBe(true);
- expect(world.has(userTextEntity, Sprite)).toBe(true);
-
- const closedSprite = world.get(userTextEntity, Sprite) as SpriteData;
- expect(closedSprite.enabled).toBe(true);
- expect(closedSprite.size.x).toBeGreaterThan(0);
- expect(closedSprite.size.y).toBeGreaterThan(0);
-
- disposeApp(app, registry);
- });
-});
diff --git a/sdk/tests/slider-layout.test.ts b/sdk/tests/slider-layout.test.ts
deleted file mode 100644
index 60e66089e..000000000
--- a/sdk/tests/slider-layout.test.ts
+++ /dev/null
@@ -1,997 +0,0 @@
-/**
- * Integration tests: Slider fill/handle layout via real WASM module.
- *
- * Simulates how the editor renders a Slider:
- * 1. Create root Canvas → Slider background → Fill + Handle as children
- * 2. Call applyDirectionalFill / computeHandleAnchors to set anchors
- * 3. Run await app.tick() to compute layout
- * 4. Verify computed width/height and world positions
- *
- * Requires pre-built WASM at desktop/public/wasm/esengine.wasm.
- */
-import { describe, it, expect, beforeAll } from 'vitest';
-import { existsSync } from 'fs';
-import { resolve } from 'path';
-import { App } from '../src/app';
-import { Transform, Sprite, Canvas } from '../src/component';
-import { UIRect } from '../src/ui/UIRect';
-import { UICameraInfo } from '../src/ui/UICameraInfo';
-import { Input, InputState } from '../src/input';
-import { UIEvents, UIEventQueue } from '../src/ui/UIEvents';
-import { uiLayoutPlugin } from '../src/ui/UILayoutPlugin';
-import { uiRenderOrderPlugin } from '../src/ui/UIRenderOrderPlugin';
-import { uiInteractionPlugin } from '../src/ui/UIInteractionPlugin';
-import { sliderPlugin } from '../src/ui/SliderPlugin';
-import { Slider } from '../src/ui/Slider';
-import { applyDirectionalFill, computeHandleAnchors, computeFillAnchors } from '../src/ui/uiHelpers';
-import { FillDirection } from '../src/ui/uiTypes';
-import type { UIRectData } from '../src/ui/UIRect';
-import type { ESEngineModule, CppRegistry } from '../src/wasm';
-import { loadWasmModule } from './helpers/loadWasm';
-
-const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
-const HAS_WASM = existsSync(WASM_PATH);
-
-describe.skipIf(!HAS_WASM)('Slider Layout (WASM integration)', () => {
- let module: ESEngineModule;
-
- beforeAll(async () => {
- module = await loadWasmModule();
- });
-
- function createEditorApp(): { app: App; registry: CppRegistry } {
- const app = App.new();
- const registry = new module.Registry() as unknown as CppRegistry;
- app.connectCpp(registry, module);
-
- app.insertResource(UICameraInfo, {
- viewProjection: new Float32Array(16),
- vpX: 0, vpY: 0, vpW: 0, vpH: 0,
- screenW: 0, screenH: 0,
- worldLeft: 0, worldBottom: 0, worldRight: 0, worldTop: 0,
- worldMouseX: 0, worldMouseY: 0,
- valid: false,
- });
-
- app.addPlugin(uiLayoutPlugin);
- app.addPlugin(uiRenderOrderPlugin);
- return { app, registry };
- }
-
- function disposeApp(app: App, registry: CppRegistry): void {
- const world = app.world;
- for (const e of world.getAllEntities()) {
- try { world.despawn(e); } catch (_) {}
- }
- world.disconnectCpp();
- (registry as any).delete();
- }
-
- function setCanvasRect(app: App, left: number, bottom: number, right: number, top: number): void {
- const cam = app.getResource(UICameraInfo);
- cam.worldLeft = left;
- cam.worldBottom = bottom;
- cam.worldRight = right;
- cam.worldTop = top;
- cam.valid = true;
- }
-
- function makeTransform() {
- return {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- };
- }
-
- function makeSprite(w = 100, h = 100) {
- return {
- texture: 0,
- 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,
- };
- }
-
- function makeFullRect() {
- return {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 },
- pivot: { x: 0.5, y: 0.5 },
- };
- }
-
- function createSliderHierarchy(app: App) {
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, makeFullRect());
- world.insert(root, Transform, makeTransform());
-
- const sliderBg = world.spawn();
- world.setParent(sliderBg, root);
- world.insert(sliderBg, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 200, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(sliderBg, Transform, makeTransform());
- world.insert(sliderBg, Sprite, makeSprite(200, 20));
-
- const fill = world.spawn();
- world.setParent(fill, sliderBg);
- world.insert(fill, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 0.5, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(fill, Transform, makeTransform());
- world.insert(fill, Sprite, makeSprite(100, 20));
-
- const handle = world.spawn();
- world.setParent(handle, sliderBg);
- world.insert(handle, UIRect, {
- anchorMin: { x: 0.5, y: 0 },
- anchorMax: { x: 0.5, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 20, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(handle, Transform, makeTransform());
- world.insert(handle, Sprite, makeSprite(20, 20));
-
- return { root, sliderBg, fill, handle };
- }
-
- describe('computeFillAnchors (pure logic)', () => {
- it('should compute LeftToRight at 50%', () => {
- const anchors = computeFillAnchors(FillDirection.LeftToRight, 0.5);
- expect(anchors.anchorMin).toEqual({ x: 0, y: 0 });
- expect(anchors.anchorMax).toEqual({ x: 0.5, y: 1 });
- });
-
- it('should compute LeftToRight at 0%', () => {
- const anchors = computeFillAnchors(FillDirection.LeftToRight, 0);
- expect(anchors.anchorMin).toEqual({ x: 0, y: 0 });
- expect(anchors.anchorMax).toEqual({ x: 0, y: 1 });
- });
-
- it('should compute LeftToRight at 100%', () => {
- const anchors = computeFillAnchors(FillDirection.LeftToRight, 1);
- expect(anchors.anchorMin).toEqual({ x: 0, y: 0 });
- expect(anchors.anchorMax).toEqual({ x: 1, y: 1 });
- });
-
- it('should compute RightToLeft at 50%', () => {
- const anchors = computeFillAnchors(FillDirection.RightToLeft, 0.5);
- expect(anchors.anchorMin).toEqual({ x: 0.5, y: 0 });
- expect(anchors.anchorMax).toEqual({ x: 1, y: 1 });
- });
-
- it('should compute BottomToTop at 75%', () => {
- const anchors = computeFillAnchors(FillDirection.BottomToTop, 0.75);
- expect(anchors.anchorMin).toEqual({ x: 0, y: 0 });
- expect(anchors.anchorMax).toEqual({ x: 1, y: 0.75 });
- });
-
- it('should compute TopToBottom at 30%', () => {
- const anchors = computeFillAnchors(FillDirection.TopToBottom, 0.3);
- expect(anchors.anchorMin).toEqual({ x: 0, y: 0.7 });
- expect(anchors.anchorMax).toEqual({ x: 1, y: 1 });
- });
- });
-
- describe('computeHandleAnchors (pure logic)', () => {
- it('should position handle at value for LeftToRight', () => {
- const anchors = computeHandleAnchors(FillDirection.LeftToRight, 0.5);
- expect(anchors.anchorMin).toEqual({ x: 0.5, y: 0.5 });
- expect(anchors.anchorMax).toEqual({ x: 0.5, y: 0.5 });
- });
-
- it('should position handle at start for LeftToRight 0%', () => {
- const anchors = computeHandleAnchors(FillDirection.LeftToRight, 0);
- expect(anchors.anchorMin).toEqual({ x: 0, y: 0.5 });
- expect(anchors.anchorMax).toEqual({ x: 0, y: 0.5 });
- });
-
- it('should position handle at end for LeftToRight 100%', () => {
- const anchors = computeHandleAnchors(FillDirection.LeftToRight, 1);
- expect(anchors.anchorMin).toEqual({ x: 1, y: 0.5 });
- expect(anchors.anchorMax).toEqual({ x: 1, y: 0.5 });
- });
- });
-
- describe('fill layout after applyDirectionalFill + tick', () => {
- it('should compute fill width = 50% of slider background', async () => {
- const { app, registry } = createEditorApp();
- const { root, sliderBg, fill, handle } = createSliderHierarchy(app);
-
- applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.5);
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
-
- const bgW = module.getUIRectComputedWidth(registry, sliderBg);
- const bgH = module.getUIRectComputedHeight(registry, sliderBg);
- expect(bgW).toBeCloseTo(200, 0);
- expect(bgH).toBeCloseTo(20, 0);
-
- const fillW = module.getUIRectComputedWidth(registry, fill);
- const fillH = module.getUIRectComputedHeight(registry, fill);
- expect(fillW).toBeCloseTo(100, 0);
- expect(fillH).toBeCloseTo(20, 0);
-
- disposeApp(app, registry);
- });
-
- it('should compute fill width = 0 at value 0', async () => {
- const { app, registry } = createEditorApp();
- const { fill } = createSliderHierarchy(app);
-
- applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0);
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
-
- const fillW = module.getUIRectComputedWidth(registry, fill);
- expect(fillW).toBeCloseTo(0, 0);
-
- disposeApp(app, registry);
- });
-
- it('should compute fill width = 100% at value 1', async () => {
- const { app, registry } = createEditorApp();
- const { sliderBg, fill } = createSliderHierarchy(app);
-
- applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 1);
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
-
- const bgW = module.getUIRectComputedWidth(registry, sliderBg);
- const fillW = module.getUIRectComputedWidth(registry, fill);
- expect(fillW).toBeCloseTo(bgW, 0);
-
- disposeApp(app, registry);
- });
-
- it('should compute fill width = 25% at value 0.25', async () => {
- const { app, registry } = createEditorApp();
- const { fill } = createSliderHierarchy(app);
-
- applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.25);
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
-
- const fillW = module.getUIRectComputedWidth(registry, fill);
- expect(fillW).toBeCloseTo(50, 0);
-
- disposeApp(app, registry);
- });
-
- it('should position fill at left side, not centered (LeftToRight 50%)', async () => {
- const { app, registry } = createEditorApp();
- const { sliderBg, fill } = createSliderHierarchy(app);
-
- applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.5);
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
- await app.tick(1 / 60);
-
- const fillTransform = registry.getTransform(fill);
- const bgTransform = registry.getTransform(sliderBg);
-
- console.log('bgTransform:', JSON.stringify(bgTransform));
- console.log('fillTransform:', JSON.stringify(fillTransform));
- console.log('fill UIRect:', JSON.stringify(registry.getUIRect(fill)));
- console.log('fill Sprite:', JSON.stringify(registry.getSprite(fill)));
-
- // Slider bg: 200 wide, centered (position.x = 0)
- expect(bgTransform.position.x).toBeCloseTo(0, 0);
-
- // Fill: 100 wide, should be at x = -50 (left side of 200-wide parent)
- // NOT at x = 0 (center)
- expect(fillTransform.position.x).toBeCloseTo(-50, 0);
-
- disposeApp(app, registry);
- });
- });
-
- describe('DEBUG: full pipeline sprite bounds', () => {
- it('should verify sprite left/right edges for LeftToRight fill', async () => {
- const { app, registry } = createEditorApp();
- const { root, sliderBg, fill } = createSliderHierarchy(app);
-
- setCanvasRect(app, -400, -300, 400, 300);
-
- const values = [0, 0.25, 0.5, 0.75, 1.0];
- for (const value of values) {
- applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, value);
- await app.tick(1 / 60);
-
- const fillT = registry.getTransform(fill);
- const bgT = registry.getTransform(sliderBg);
- const rootT = registry.getTransform(root);
- const fillS = registry.getSprite(fill);
- const fillR = registry.getUIRect(fill);
- const bgR = registry.getUIRect(sliderBg);
-
- // hierarchy: root→sliderBg→fill
- // root has no parent → worldPos = position
- // sliderBg's parent is root → worldPos = root.worldPos + sliderBg.position
- // fill's parent is sliderBg → worldPos = sliderBg.worldPos + fill.position
- const rootWorldX = rootT.position.x;
- const bgWorldX = rootWorldX + bgT.position.x;
- const fillWorldX = bgWorldX + fillT.position.x;
-
- // Sprite renders centered at worldPos, width = sprite.size.x
- const spriteLeft = fillWorldX - fillS.size.x / 2;
- const spriteRight = fillWorldX + fillS.size.x / 2;
- const parentLeft = bgWorldX - 100; // slider is 200 wide, centered
- const parentRight = bgWorldX + 100;
- const expectedFillWidth = 200 * value;
-
- console.log(`\n--- value=${value} ---`);
- console.log(` root pos.x=${rootT.position.x}`);
- console.log(` bg pos.x=${bgT.position.x}, worldX=${bgWorldX}`);
- console.log(` fill pos.x=${fillT.position.x}, worldX=${fillWorldX}`);
- console.log(` fill sprite.size=${fillS.size.x}x${fillS.size.y}`);
- console.log(` fill UIRect anchors: min=${fillR.anchorMin.x},${fillR.anchorMin.y} max=${fillR.anchorMax.x},${fillR.anchorMax.y}`);
- console.log(` sprite bounds: [${spriteLeft}, ${spriteRight}]`);
- console.log(` parent bounds: [${parentLeft}, ${parentRight}]`);
- console.log(` expected fill width: ${expectedFillWidth}`);
-
- // Sprite size should match fill value
- expect(fillS.size.x).toBeCloseTo(expectedFillWidth, 0);
-
- // Fill position should be LEFT-aligned, not centered
- // For LeftToRight, fill's left edge should match parent's left edge
- if (value > 0) {
- expect(spriteLeft).toBeCloseTo(parentLeft, 0);
- }
- }
-
- disposeApp(app, registry);
- });
-
- it('should verify Children.entities is populated after setParent', () => {
- const { app, registry } = createEditorApp();
- const { root, sliderBg, fill, handle } = createSliderHierarchy(app);
-
- const rootChildren = registry.getChildren(root);
- const bgChildren = registry.getChildren(sliderBg);
-
- const rootEntities: number[] = [];
- const bgEntities: number[] = [];
- for (let i = 0; i < rootChildren.entities.size(); i++) {
- rootEntities.push(rootChildren.entities.get(i));
- }
- for (let i = 0; i < bgChildren.entities.size(); i++) {
- bgEntities.push(bgChildren.entities.get(i));
- }
-
- console.log('\n--- Hierarchy check ---');
- console.log(`root children: [${rootEntities}]`);
- console.log(`sliderBg children: [${bgEntities}]`);
- console.log(`root entity=${root}, sliderBg=${sliderBg}, fill=${fill}, handle=${handle}`);
-
- expect(rootEntities).toContain(sliderBg);
- expect(bgEntities).toContain(fill);
- expect(bgEntities).toContain(handle);
-
- const fillParent = registry.getParent(fill);
- const handleParent = registry.getParent(handle);
- console.log(`fill parent=${fillParent.entity}, handle parent=${handleParent.entity}`);
- expect(fillParent.entity).toBe(sliderBg);
- expect(handleParent.entity).toBe(sliderBg);
-
- disposeApp(app, registry);
- });
-
- it('should test with SliderPlugin running (full runtime simulation)', async () => {
- const app = App.new();
- const registry = new module.Registry() as unknown as CppRegistry;
- app.connectCpp(registry, module);
-
- app.insertResource(UICameraInfo, {
- viewProjection: new Float32Array(16),
- vpX: 0, vpY: 0, vpW: 0, vpH: 0,
- screenW: 0, screenH: 0,
- worldLeft: 0, worldBottom: 0, worldRight: 0, worldTop: 0,
- worldMouseX: 0, worldMouseY: 0,
- valid: false,
- });
- app.insertResource(Input, new InputState());
- app.insertResource(UIEvents, new UIEventQueue());
-
- // Register layout + interaction + slider plugins (skip text/image which need canvas)
- app.addPlugin(uiLayoutPlugin);
- app.addPlugin(uiRenderOrderPlugin);
- app.addPlugin(uiInteractionPlugin);
- app.addPlugin(sliderPlugin);
-
- const world = app.world;
-
- // Create scene hierarchy: root → slider → fill + handle
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, makeFullRect());
- world.insert(root, Transform, makeTransform());
-
- const sliderBg = world.spawn();
- world.setParent(sliderBg, root);
- world.insert(sliderBg, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 200, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(sliderBg, Transform, makeTransform());
- world.insert(sliderBg, Sprite, makeSprite(200, 20));
-
- const fill = world.spawn();
- world.setParent(fill, sliderBg);
- world.insert(fill, UIRect, makeFullRect());
- world.insert(fill, Transform, makeTransform());
- world.insert(fill, Sprite, makeSprite(200, 20));
-
- const handle = world.spawn();
- world.setParent(handle, sliderBg);
- world.insert(handle, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 20, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(handle, Transform, makeTransform());
- world.insert(handle, Sprite, makeSprite(20, 20));
-
- // Register Slider component with fillEntity pointing to fill
- world.insert(sliderBg, Slider, {
- value: 0.5,
- minValue: 0,
- maxValue: 1,
- direction: FillDirection.LeftToRight,
- fillEntity: fill,
- handleEntity: handle,
- wholeNumbers: false,
- });
-
- // Set camera and tick
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
- await app.tick(1 / 60);
-
- const fillT = registry.getTransform(fill);
- const bgT = registry.getTransform(sliderBg);
- const fillS = registry.getSprite(fill);
- const fillR = registry.getUIRect(fill);
-
- const bgWorldX = bgT.position.x;
- const fillWorldX = bgWorldX + fillT.position.x;
- const spriteLeft = fillWorldX - fillS.size.x / 2;
-
- console.log('\n--- SliderPlugin full test (value=0.5) ---');
- console.log(` bg pos.x=${bgT.position.x}`);
- console.log(` fill pos.x=${fillT.position.x}`);
- console.log(` fill sprite.size=${fillS.size.x}x${fillS.size.y}`);
- console.log(` fill UIRect: min=${fillR.anchorMin.x},${fillR.anchorMin.y} max=${fillR.anchorMax.x},${fillR.anchorMax.y}`);
- console.log(` fill worldX=${fillWorldX}, sprite left=${spriteLeft}`);
-
- // Fill should be 100 wide (50% of 200)
- expect(fillS.size.x).toBeCloseTo(100, 0);
- // Fill position should be -50 (left-aligned)
- expect(fillT.position.x).toBeCloseTo(-50, 0);
- // Sprite left edge should be at parent left (-100)
- expect(spriteLeft).toBeCloseTo(-100, 0);
-
- disposeApp(app, registry);
- });
-
- it('should verify worldPosition via TransformSystem', async () => {
- const app = App.new();
- const registry = new module.Registry() as unknown as CppRegistry;
- app.connectCpp(registry, module);
-
- app.insertResource(UICameraInfo, {
- viewProjection: new Float32Array(16),
- vpX: 0, vpY: 0, vpW: 0, vpH: 0,
- screenW: 0, screenH: 0,
- worldLeft: 0, worldBottom: 0, worldRight: 0, worldTop: 0,
- worldMouseX: 0, worldMouseY: 0,
- valid: false,
- });
- app.insertResource(Input, new InputState());
- app.insertResource(UIEvents, new UIEventQueue());
-
- app.addPlugin(uiLayoutPlugin);
- app.addPlugin(uiRenderOrderPlugin);
- app.addPlugin(uiInteractionPlugin);
- app.addPlugin(sliderPlugin);
-
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, makeFullRect());
- world.insert(root, Transform, makeTransform());
-
- const sliderBg = world.spawn();
- world.setParent(sliderBg, root);
- world.insert(sliderBg, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 200, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(sliderBg, Transform, makeTransform());
- world.insert(sliderBg, Sprite, makeSprite(200, 20));
-
- const fill = world.spawn();
- world.setParent(fill, sliderBg);
- world.insert(fill, UIRect, makeFullRect());
- world.insert(fill, Transform, makeTransform());
- world.insert(fill, Sprite, makeSprite(200, 20));
-
- const handle = world.spawn();
- world.setParent(handle, sliderBg);
- world.insert(handle, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 20, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(handle, Transform, makeTransform());
- world.insert(handle, Sprite, makeSprite(20, 20));
-
- world.insert(sliderBg, Slider, {
- value: 0.5,
- minValue: 0,
- maxValue: 1,
- direction: FillDirection.LeftToRight,
- fillEntity: fill,
- handleEntity: handle,
- wholeNumbers: false,
- });
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
- await app.tick(1 / 60);
-
- // Now run TransformSystem to compute worldPosition
- module.transform_update(registry);
-
- const fillT = registry.getTransform(fill);
- const bgT = registry.getTransform(sliderBg);
- const rootT = registry.getTransform(root);
- const fillS = registry.getSprite(fill);
-
- console.log('\n--- TransformSystem worldPosition test ---');
- console.log(` root: pos=(${rootT.position.x}, ${rootT.position.y}), worldPos=(${rootT.worldPosition.x}, ${rootT.worldPosition.y})`);
- console.log(` bg: pos=(${bgT.position.x}, ${bgT.position.y}), worldPos=(${bgT.worldPosition.x}, ${bgT.worldPosition.y})`);
- console.log(` fill: pos=(${fillT.position.x}, ${fillT.position.y}), worldPos=(${fillT.worldPosition.x}, ${fillT.worldPosition.y})`);
- console.log(` fill sprite.size=(${fillS.size.x}, ${fillS.size.y})`);
-
- const spriteLeft = fillT.worldPosition.x - fillS.size.x / 2;
- const spriteRight = fillT.worldPosition.x + fillS.size.x / 2;
- const sliderLeft = bgT.worldPosition.x - 100;
- const sliderRight = bgT.worldPosition.x + 100;
-
- console.log(` sprite bounds: [${spriteLeft}, ${spriteRight}]`);
- console.log(` slider bounds: [${sliderLeft}, ${sliderRight}]`);
-
- // worldPosition should be computed from hierarchy
- expect(bgT.worldPosition.x).toBeCloseTo(0, 0);
- expect(fillT.worldPosition.x).toBeCloseTo(-50, 0);
-
- // Sprite left edge should match slider left edge
- expect(spriteLeft).toBeCloseTo(sliderLeft, 0);
-
- disposeApp(app, registry);
- });
- });
-
- describe('handle layout after anchor update + tick', () => {
- it('should position handle sprite at 50%', async () => {
- const { app, registry } = createEditorApp();
- const { sliderBg, handle } = createSliderHierarchy(app);
-
- const anchors = computeHandleAnchors(FillDirection.LeftToRight, 0.5);
- app.world.insert(handle, UIRect, {
- anchorMin: anchors.anchorMin,
- anchorMax: anchors.anchorMax,
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 20, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
-
- const handleW = module.getUIRectComputedWidth(registry, handle);
- const handleH = module.getUIRectComputedHeight(registry, handle);
- expect(handleW).toBeCloseTo(20, 0);
- expect(handleH).toBeCloseTo(20, 0);
-
- disposeApp(app, registry);
- });
- });
-
- describe('render order in slider hierarchy', () => {
- it('should order: background < fill < handle', async () => {
- const { app, registry } = createEditorApp();
- const { sliderBg, fill, handle } = createSliderHierarchy(app);
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
-
- const bgLayer = registry.getSprite(sliderBg).layer;
- const fillLayer = registry.getSprite(fill).layer;
- const handleLayer = registry.getSprite(handle).layer;
-
- expect(fillLayer).toBeGreaterThan(bgLayer);
- expect(handleLayer).toBeGreaterThan(bgLayer);
-
- disposeApp(app, registry);
- });
- });
-
- describe('dynamic fill changes across ticks', () => {
- it('should update fill width when value changes', async () => {
- const { app, registry } = createEditorApp();
- const { fill } = createSliderHierarchy(app);
-
- setCanvasRect(app, -400, -300, 400, 300);
-
- applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.3);
- await app.tick(1 / 60);
- expect(module.getUIRectComputedWidth(registry, fill)).toBeCloseTo(60, 0);
-
- applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.7);
- await app.tick(1 / 60);
- expect(module.getUIRectComputedWidth(registry, fill)).toBeCloseTo(140, 0);
-
- applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0);
- await app.tick(1 / 60);
- expect(module.getUIRectComputedWidth(registry, fill)).toBeCloseTo(0, 0);
-
- disposeApp(app, registry);
- });
- });
-
- describe('two sliders side by side', () => {
- it('should compute independent layouts for two sliders', async () => {
- const { app, registry } = createEditorApp();
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, makeFullRect());
- world.insert(root, Transform, makeTransform());
-
- // Slider 1 (top half)
- const slider1Bg = world.spawn();
- world.setParent(slider1Bg, root);
- world.insert(slider1Bg, UIRect, {
- anchorMin: { x: 0.5, y: 0.75 },
- anchorMax: { x: 0.5, y: 0.75 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 200, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(slider1Bg, Transform, makeTransform());
- world.insert(slider1Bg, Sprite, makeSprite(200, 20));
-
- const fill1 = world.spawn();
- world.setParent(fill1, slider1Bg);
- world.insert(fill1, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 0.3, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(fill1, Transform, makeTransform());
- world.insert(fill1, Sprite, makeSprite(60, 20));
-
- // Slider 2 (bottom half)
- const slider2Bg = world.spawn();
- world.setParent(slider2Bg, root);
- world.insert(slider2Bg, UIRect, {
- anchorMin: { x: 0.5, y: 0.25 },
- anchorMax: { x: 0.5, y: 0.25 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 300, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(slider2Bg, Transform, makeTransform());
- world.insert(slider2Bg, Sprite, makeSprite(300, 20));
-
- const fill2 = world.spawn();
- world.setParent(fill2, slider2Bg);
- world.insert(fill2, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 0.8, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(fill2, Transform, makeTransform());
- world.insert(fill2, Sprite, makeSprite(240, 20));
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
-
- // Slider 1: bg = 200w, fill = 30% = 60w
- expect(module.getUIRectComputedWidth(registry, slider1Bg)).toBeCloseTo(200, 0);
- expect(module.getUIRectComputedWidth(registry, fill1)).toBeCloseTo(60, 0);
- expect(module.getUIRectComputedHeight(registry, fill1)).toBeCloseTo(20, 0);
-
- // Slider 2: bg = 300w, fill = 80% = 240w
- expect(module.getUIRectComputedWidth(registry, slider2Bg)).toBeCloseTo(300, 0);
- expect(module.getUIRectComputedWidth(registry, fill2)).toBeCloseTo(240, 0);
- expect(module.getUIRectComputedHeight(registry, fill2)).toBeCloseTo(20, 0);
-
- // Both backgrounds should have sprites
- expect(registry.hasSprite(slider1Bg)).toBe(true);
- expect(registry.hasSprite(slider2Bg)).toBe(true);
- expect(registry.hasSprite(fill1)).toBe(true);
- expect(registry.hasSprite(fill2)).toBe(true);
-
- // Render order: fill should be above background
- expect(registry.getSprite(fill1).layer).toBeGreaterThan(registry.getSprite(slider1Bg).layer);
- expect(registry.getSprite(fill2).layer).toBeGreaterThan(registry.getSprite(slider2Bg).layer);
-
- disposeApp(app, registry);
- });
- });
-
- describe('fill sprite size sync via layout system', () => {
- it('should update fill Sprite.size to match computed UIRect size', async () => {
- const { app, registry } = createEditorApp();
- const { fill } = createSliderHierarchy(app);
-
- applyDirectionalFill(app.world, fill, FillDirection.LeftToRight, 0.5);
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
-
- const sprite = registry.getSprite(fill);
- const computedW = module.getUIRectComputedWidth(registry, fill);
- const computedH = module.getUIRectComputedHeight(registry, fill);
- expect(sprite.size.x).toBeCloseTo(computedW, 0);
- expect(sprite.size.y).toBeCloseTo(computedH, 0);
-
- disposeApp(app, registry);
- });
- });
-
- describe('editor-like slider creation flow', () => {
- it('should reproduce exact editor slider hierarchy and verify fill visibility', async () => {
- const { app, registry } = createEditorApp();
- const world = app.world;
-
- // Simulate editor scene: Canvas root → Slider → Fill + Handle
- // This mirrors what HierarchyContextMenu.createSliderEntity does
-
- const canvasRoot = world.spawn();
- world.insert(canvasRoot, Canvas, {});
- world.insert(canvasRoot, UIRect, makeFullRect());
- world.insert(canvasRoot, Transform, makeTransform());
-
- // Slider background (same as editor: size 200x20, centered anchors)
- const slider = world.spawn();
- world.setParent(slider, canvasRoot);
- world.insert(slider, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 200, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(slider, Transform, makeTransform());
- // Editor gives slider a smaller Sprite (200x8) than UIRect (200x20)
- world.insert(slider, Sprite, makeSprite(200, 8));
-
- // Fill child (same as editor)
- const fill = world.spawn();
- world.setParent(fill, slider);
- world.insert(fill, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: 0.5, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 100, y: 100 }, // default SDK size, ignored in stretch
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(fill, Transform, makeTransform());
- world.insert(fill, Sprite, makeSprite(200, 8));
-
- // Handle child (same as editor)
- const handle = world.spawn();
- world.setParent(handle, slider);
- world.insert(handle, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 20, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(handle, Transform, makeTransform());
- world.insert(handle, Sprite, makeSprite(20, 20));
-
- // Step 1: applyFillStates (editor calls this after loading scene)
- applyDirectionalFill(world, fill, FillDirection.LeftToRight, 0.5);
-
- // Verify fill UIRect anchors were set correctly
- const fillRect = registry.getUIRect(fill);
- expect(fillRect.anchorMin.x).toBeCloseTo(0);
- expect(fillRect.anchorMin.y).toBeCloseTo(0);
- expect(fillRect.anchorMax.x).toBeCloseTo(0.5);
- expect(fillRect.anchorMax.y).toBeCloseTo(1);
-
- // Step 2: render() calls tick() which runs C++ layout
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
-
- // Verify slider bg got correct size (fixed-size centered)
- const sliderW = module.getUIRectComputedWidth(registry, slider);
- const sliderH = module.getUIRectComputedHeight(registry, slider);
- expect(sliderW).toBeCloseTo(200, 0);
- expect(sliderH).toBeCloseTo(20, 0);
-
- // Verify fill got correct size (50% of 200 = 100)
- const fillW = module.getUIRectComputedWidth(registry, fill);
- const fillH = module.getUIRectComputedHeight(registry, fill);
- expect(fillW).toBeCloseTo(100, 0);
- expect(fillH).toBeCloseTo(20, 0);
-
- // Verify fill sprite was updated by C++ layout
- const fillSprite = registry.getSprite(fill);
- expect(fillSprite.size.x).toBeCloseTo(100, 0);
- expect(fillSprite.size.y).toBeCloseTo(20, 0);
-
- // Verify handle got correct size
- const handleW = module.getUIRectComputedWidth(registry, handle);
- const handleH = module.getUIRectComputedHeight(registry, handle);
- expect(handleW).toBeCloseTo(20, 0);
- expect(handleH).toBeCloseTo(20, 0);
-
- // Verify render order
- const sliderLayer = registry.getSprite(slider).layer;
- const fillLayer = registry.getSprite(fill).layer;
- const handleLayer = registry.getSprite(handle).layer;
- expect(fillLayer).toBeGreaterThan(sliderLayer);
- expect(handleLayer).toBeGreaterThan(sliderLayer);
-
- disposeApp(app, registry);
- });
-
- it('should handle two sliders at different positions', async () => {
- const { app, registry } = createEditorApp();
- const world = app.world;
-
- const canvasRoot = world.spawn();
- world.insert(canvasRoot, Canvas, {});
- world.insert(canvasRoot, UIRect, makeFullRect());
- world.insert(canvasRoot, Transform, makeTransform());
-
- function createEditorSlider(yOffset: number, value: number) {
- const bg = world.spawn();
- world.setParent(bg, canvasRoot);
- world.insert(bg, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 },
- anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 200, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(bg, Transform, {
- position: { x: 0, y: yOffset, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- });
- world.insert(bg, Sprite, makeSprite(200, 8));
-
- const fill = world.spawn();
- world.setParent(fill, bg);
- world.insert(fill, UIRect, {
- anchorMin: { x: 0, y: 0 },
- anchorMax: { x: value, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 100, y: 100 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(fill, Transform, makeTransform());
- world.insert(fill, Sprite, makeSprite(200, 8));
-
- const handle = world.spawn();
- world.setParent(handle, bg);
- world.insert(handle, UIRect, {
- anchorMin: { x: value, y: 0 },
- anchorMax: { x: value, y: 1 },
- offsetMin: { x: 0, y: 0 },
- offsetMax: { x: 0, y: 0 },
- size: { x: 20, y: 20 },
- pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(handle, Transform, makeTransform());
- world.insert(handle, Sprite, makeSprite(20, 20));
-
- return { bg, fill, handle };
- }
-
- const slider1 = createEditorSlider(50, 0.3);
- const slider2 = createEditorSlider(-50, 0.7);
-
- // Apply fill states (like editor does after load)
- applyDirectionalFill(world, slider1.fill, FillDirection.LeftToRight, 0.3);
- applyDirectionalFill(world, slider2.fill, FillDirection.LeftToRight, 0.7);
-
- setCanvasRect(app, -400, -300, 400, 300);
- await app.tick(1 / 60);
-
- // Slider 1: fill = 30% of 200 = 60
- const fill1W = module.getUIRectComputedWidth(registry, slider1.fill);
- expect(fill1W).toBeCloseTo(60, 0);
- expect(module.getUIRectComputedHeight(registry, slider1.fill)).toBeCloseTo(20, 0);
-
- // Slider 2: fill = 70% of 200 = 140
- const fill2W = module.getUIRectComputedWidth(registry, slider2.fill);
- expect(fill2W).toBeCloseTo(140, 0);
- expect(module.getUIRectComputedHeight(registry, slider2.fill)).toBeCloseTo(20, 0);
-
- // Both fills should have positive sprite sizes
- const fill1Sprite = registry.getSprite(slider1.fill);
- const fill2Sprite = registry.getSprite(slider2.fill);
- expect(fill1Sprite.size.x).toBeGreaterThan(0);
- expect(fill2Sprite.size.x).toBeGreaterThan(0);
-
- // Both handles should be visible
- const handle1Sprite = registry.getSprite(slider1.handle);
- const handle2Sprite = registry.getSprite(slider2.handle);
- expect(handle1Sprite.size.x).toBeCloseTo(20, 0);
- expect(handle2Sprite.size.x).toBeCloseTo(20, 0);
-
- // Both bgs should be rendered
- expect(registry.getSprite(slider1.bg).size.x).toBeCloseTo(200, 0);
- expect(registry.getSprite(slider2.bg).size.x).toBeCloseTo(200, 0);
-
- disposeApp(app, registry);
- });
- });
-});
diff --git a/sdk/tests/slider-system.test.ts b/sdk/tests/slider-system.test.ts
deleted file mode 100644
index e33a3e2a2..000000000
--- a/sdk/tests/slider-system.test.ts
+++ /dev/null
@@ -1,270 +0,0 @@
-/**
- * Integration test: verify SliderPlugin system produces correct fill layout.
- * This uses the actual SliderPlugin (not just applyDirectionalFill) to
- * catch system ordering issues.
- */
-import { describe, it, expect, beforeAll } from 'vitest';
-import { existsSync } from 'fs';
-import { resolve } from 'path';
-import { App } from '../src/app';
-import { Transform, Sprite, Canvas } from '../src/component';
-import { UIRect } from '../src/ui/UIRect';
-import { UICameraInfo } from '../src/ui/UICameraInfo';
-import { Input, InputState } from '../src/input';
-import { UIEvents, UIEventQueue } from '../src/ui/UIEvents';
-import { uiLayoutPlugin } from '../src/ui/UILayoutPlugin';
-import { uiRenderOrderPlugin } from '../src/ui/UIRenderOrderPlugin';
-import { uiInteractionPlugin } from '../src/ui/UIInteractionPlugin';
-import { sliderPlugin } from '../src/ui/SliderPlugin';
-import { Slider, FillDirection } from '../src/ui/Slider';
-import type { SliderData } from '../src/ui/Slider';
-import type { UIRectData } from '../src/ui/UIRect';
-import type { SpriteData, TransformData } from '../src/component';
-import type { ESEngineModule, CppRegistry } from '../src/wasm';
-import { loadWasmModule } from './helpers/loadWasm';
-import type { Entity } from '../src/types';
-
-const WASM_PATH = resolve(__dirname, '../../desktop/public/wasm/esengine.wasm');
-const HAS_WASM = existsSync(WASM_PATH);
-
-describe.skipIf(!HAS_WASM)('SliderPlugin system integration', () => {
- let module: ESEngineModule;
-
- beforeAll(async () => {
- module = await loadWasmModule();
- });
-
- function createFullApp(): { app: App; registry: CppRegistry } {
- const app = App.new();
- const registry = new module.Registry() as unknown as CppRegistry;
- app.connectCpp(registry, module);
-
- app.insertResource(UICameraInfo, {
- viewProjection: new Float32Array(16),
- vpX: 0, vpY: 0, vpW: 0, vpH: 0,
- screenW: 0, screenH: 0,
- worldLeft: -100, worldBottom: -100, worldRight: 100, worldTop: 100,
- worldMouseX: 0, worldMouseY: 0,
- valid: true,
- });
- app.insertResource(Input, new InputState());
- app.insertResource(UIEvents, new UIEventQueue());
-
- app.addPlugin(uiLayoutPlugin);
- app.addPlugin(uiRenderOrderPlugin);
- app.addPlugin(uiInteractionPlugin);
- app.addPlugin(sliderPlugin);
-
- return { app, registry };
- }
-
- function makeTransform() {
- return {
- position: { x: 0, y: 0, z: 0 },
- rotation: { x: 0, y: 0, z: 0, w: 1 },
- scale: { x: 1, y: 1, z: 1 },
- };
- }
-
- function makeSprite(w = 100, h = 100) {
- return {
- texture: 0,
- 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,
- };
- }
-
- it('SliderPlugin should produce left-aligned fill (value=0.5)', async () => {
- const { app, registry } = createFullApp();
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(root, Transform, makeTransform());
-
- const sliderBg = world.spawn();
- world.setParent(sliderBg, root);
- world.insert(sliderBg, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 }, anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 200, y: 20 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(sliderBg, Transform, makeTransform());
- world.insert(sliderBg, Sprite, makeSprite(200, 20));
-
- const fill = world.spawn();
- world.setParent(fill, sliderBg);
- world.insert(fill, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(fill, Transform, makeTransform());
- world.insert(fill, Sprite, makeSprite(200, 20));
-
- const handle = world.spawn();
- world.setParent(handle, sliderBg);
- world.insert(handle, UIRect, {
- anchorMin: { x: 0.5, y: 0 }, anchorMax: { x: 0.5, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 20, y: 20 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(handle, Transform, makeTransform());
- world.insert(handle, Sprite, makeSprite(20, 20));
-
- world.insert(sliderBg, Slider, {
- value: 0.5,
- minValue: 0,
- maxValue: 1,
- direction: FillDirection.LeftToRight,
- fillEntity: fill,
- handleEntity: handle,
- wholeNumbers: false,
- });
-
- for (let i = 0; i < 3; i++) {
- await app.tick(1 / 60);
- }
-
- // Run TransformSystem to compute worldPositions (normally done by renderer)
- module.transform_update(registry);
-
- const fillT = world.get(fill, Transform) as TransformData;
- const fillS = world.get(fill, Sprite) as SpriteData;
- const fillR = world.get(fill, UIRect) as UIRectData;
- const bgT = world.get(sliderBg, Transform) as TransformData;
-
- console.log('\n=== After SliderPlugin + TransformSystem ===');
- console.log('slider bg position:', bgT.position.x, bgT.position.y);
- console.log('slider bg worldPosition:', bgT.worldPosition.x, bgT.worldPosition.y);
- console.log('fill position:', fillT.position.x, fillT.position.y);
- console.log('fill worldPosition:', fillT.worldPosition.x, fillT.worldPosition.y);
- console.log('fill sprite.size:', fillS.size.x, fillS.size.y);
- console.log('fill UIRect anchors:', fillR.anchorMin.x, fillR.anchorMin.y, '->', fillR.anchorMax.x, fillR.anchorMax.y);
-
- expect(fillS.size.x).toBeCloseTo(100, 0);
- expect(fillT.position.x).toBeCloseTo(-50, 0);
-
- // Verify worldPosition is correct (this is what the renderer uses)
- expect(bgT.worldPosition.x).toBeCloseTo(0, 0);
- expect(fillT.worldPosition.x).toBeCloseTo(-50, 0);
-
- // Simulate renderer quad computation
- const left = fillT.worldPosition.x - fillS.size.x * 0.5;
- const right = fillT.worldPosition.x + fillS.size.x * 0.5;
- const parentLeft = bgT.worldPosition.x - 100;
- const parentRight = bgT.worldPosition.x + 100;
-
- console.log('fill quad bounds: [', left, ',', right, ']');
- console.log('parent quad bounds: [', parentLeft, ',', parentRight, ']');
-
- // Fill left edge must align with parent left edge
- expect(left).toBeCloseTo(parentLeft, 0);
- // Fill right edge must be at parent center (50% fill)
- expect(right).toBeCloseTo(0, 0);
-
- for (const e of world.getAllEntities()) {
- try { world.despawn(e); } catch (_) {}
- }
- world.disconnectCpp();
- (registry as any).delete();
- });
-
- it('full pipeline: UILayout → SliderPlugin → UILayoutLate → TransformSystem', async () => {
- const { app, registry } = createFullApp();
- const world = app.world;
-
- const root = world.spawn();
- world.insert(root, Canvas, {});
- world.insert(root, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(root, Transform, makeTransform());
-
- const sliderBg = world.spawn();
- world.setParent(sliderBg, root);
- world.insert(sliderBg, UIRect, {
- anchorMin: { x: 0.5, y: 0.5 }, anchorMax: { x: 0.5, y: 0.5 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 200, y: 20 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(sliderBg, Transform, makeTransform());
- world.insert(sliderBg, Sprite, makeSprite(200, 20));
-
- const fill = world.spawn();
- world.setParent(fill, sliderBg);
- world.insert(fill, UIRect, {
- anchorMin: { x: 0, y: 0 }, anchorMax: { x: 1, y: 1 },
- offsetMin: { x: 0, y: 0 }, offsetMax: { x: 0, y: 0 },
- size: { x: 0, y: 0 }, pivot: { x: 0.5, y: 0.5 },
- });
- world.insert(fill, Transform, makeTransform());
- world.insert(fill, Sprite, makeSprite(200, 20));
-
- world.insert(sliderBg, Slider, {
- value: 0,
- minValue: 0,
- maxValue: 1,
- direction: FillDirection.LeftToRight,
- fillEntity: fill,
- handleEntity: 0 as Entity,
- wholeNumbers: false,
- });
-
- const testValues = [0, 0.25, 0.5, 0.75, 1.0];
- for (const value of testValues) {
- world.insert(sliderBg, Slider, {
- value,
- minValue: 0,
- maxValue: 1,
- direction: FillDirection.LeftToRight,
- fillEntity: fill,
- handleEntity: 0 as Entity,
- wholeNumbers: false,
- });
-
- await app.tick(1 / 60);
- module.transform_update(registry);
-
- const fillT = world.get(fill, Transform) as TransformData;
- const fillS = world.get(fill, Sprite) as SpriteData;
- const bgT = world.get(sliderBg, Transform) as TransformData;
-
- const expectedWidth = 200 * value;
- const expectedPosX = value > 0 ? -(200 - expectedWidth) / 2 : 0;
-
- // Simulate renderer quad computation
- const quadLeft = fillT.worldPosition.x - fillS.size.x * 0.5;
- const quadRight = fillT.worldPosition.x + fillS.size.x * 0.5;
- const parentLeft = bgT.worldPosition.x - 100;
-
- console.log(`\nvalue=${value}: pos.x=${fillT.position.x}, worldPos.x=${fillT.worldPosition.x}, sprite.size.x=${fillS.size.x}, quad=[${quadLeft}, ${quadRight}], parentLeft=${parentLeft}`);
-
- expect(fillS.size.x).toBeCloseTo(expectedWidth, 0);
- if (value > 0) {
- expect(fillT.position.x).toBeCloseTo(expectedPosX, 0);
- expect(fillT.worldPosition.x).toBeCloseTo(expectedPosX, 0);
- // Fill quad left edge must align with parent left edge
- expect(quadLeft).toBeCloseTo(parentLeft, 0);
- }
- }
-
- for (const e of world.getAllEntities()) {
- try { world.despawn(e); } catch (_) {}
- }
- world.disconnectCpp();
- (registry as any).delete();
- });
-});