Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: add system ordering, fix toggle O(n²), and clean up UI plugins

esenginec2ec3399d76a
diff --git a/sdk/src/ui/DropdownPlugin.ts b/sdk/src/ui/DropdownPlugin.ts
index 30788a589..d8f6d3623 100644
--- a/sdk/src/ui/DropdownPlugin.ts
+++ b/sdk/src/ui/DropdownPlugin.ts
@@ -105,7 +105,7 @@ export class DropdownPlugin implements Plugin {
}
},
{ name: 'DropdownSystem' }
- ));
+ ), { runAfter: ['UIInteractionSystem'] });
function openDropdown(entity: Entity, dropdown: DropdownData, state: DropdownState): void {
dropdown.isOpen = true;
diff --git a/sdk/src/ui/ListViewPlugin.ts b/sdk/src/ui/ListViewPlugin.ts
index 608b3d4b8..ad2af9d2b 100644
--- a/sdk/src/ui/ListViewPlugin.ts
+++ b/sdk/src/ui/ListViewPlugin.ts
@@ -24,17 +24,18 @@ interface ListViewState {
renderer: ListViewItemRenderer | null;
}
+let activeListViewStates: Map<Entity, ListViewState> | null = null;
+
export function setListViewRenderer(entity: Entity, renderer: ListViewItemRenderer): void {
- const activeStates = ListViewPlugin.activeStates;
- if (!activeStates) {
+ if (!activeListViewStates) {
console.warn('setListViewRenderer: ListViewPlugin has not been initialized');
return;
}
- const state = activeStates.get(entity);
+ const state = activeListViewStates.get(entity);
if (state) {
state.renderer = renderer;
} else {
- activeStates.set(entity, {
+ activeListViewStates.set(entity, {
visibleStart: 0,
visibleEnd: 0,
itemEntities: new Map(),
@@ -44,14 +45,13 @@ export function setListViewRenderer(entity: Entity, renderer: ListViewItemRender
}
export class ListViewPlugin implements Plugin {
- static activeStates: Map<Entity, ListViewState> | null = null;
build(app: App): void {
registerComponent('ListView', ListView);
const world = app.world;
const listViewStates = new Map<Entity, ListViewState>();
- ListViewPlugin.activeStates = listViewStates;
+ activeListViewStates = listViewStates;
app.addSystemToSchedule(Schedule.PreUpdate, defineSystem(
[Res(Input)],
diff --git a/sdk/src/ui/TextInputPlugin.ts b/sdk/src/ui/TextInputPlugin.ts
index e2905fa6e..816e8f6cd 100644
--- a/sdk/src/ui/TextInputPlugin.ts
+++ b/sdk/src/ui/TextInputPlugin.ts
@@ -134,6 +134,11 @@ export class TextInputPlugin implements Plugin {
textarea.removeEventListener('keydown', onKeyDown);
textarea.removeEventListener('blur', onBlur);
textarea.remove();
+ const rm = module!.getResourceManager();
+ for (const tex of textureCache.values()) {
+ rm.releaseTexture(tex);
+ }
+ textureCache.clear();
};
function syncFromTextarea(): void {
diff --git a/sdk/src/ui/TogglePlugin.ts b/sdk/src/ui/TogglePlugin.ts
index 01778bc32..7c96528da 100644
--- a/sdk/src/ui/TogglePlugin.ts
+++ b/sdk/src/ui/TogglePlugin.ts
@@ -40,6 +40,19 @@ export class TogglePlugin implements Plugin {
toggle.isOn = false;
}
}
+ 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;
@@ -61,12 +74,15 @@ export class TogglePlugin implements Plugin {
toggle.isOn = !toggle.isOn;
if (toggle.isOn && hasGroup) {
- for (const other of toggleEntities) {
- if (other === entity) continue;
- const otherToggle = world.get(other, Toggle) as ToggleData;
- if (otherToggle.group === groupEntity && otherToggle.isOn) {
- otherToggle.isOn = false;
- events.emit(other, 'change');
+ 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;
+ events.emit(other, 'change');
+ }
}
}
}
@@ -95,7 +111,7 @@ export class TogglePlugin implements Plugin {
}
},
{ name: 'ToggleSystem' }
- ));
+ ), { runAfter: ['UIInteractionSystem'] });
}
}