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(ui): a pointer lands on what is drawn last by its place in the hierarchy, not on what was made last; a Toggle belongs to the container its parent holds and assigning isChecked switches its group's others off

yhh241998c4ffbf
diff --git a/sdk/src/cocos/ui/controls.ts b/sdk/src/cocos/ui/controls.ts
index d14734788..89fe0834a 100644
--- a/sdk/src/cocos/ui/controls.ts
+++ b/sdk/src/cocos/ui/controls.ts
@@ -72,10 +72,17 @@ export class ToggleContainer extends Component {
export class Toggle extends Button {
static readonly EventType = Object.freeze({ TOGGLE: 'toggle', CLICK: 'click' });
- isChecked = false;
+ private checked_ = false;
+ /** Whether it is on. Assigning to it switches the others of its group off, as checking it does (without the events). */
+ get isChecked(): boolean { return this.checked_; }
+ set isChecked(value: boolean) { this.setChecked_(!!value, false); }
/** The graphic shown while checked. */
checkMark: Sprite | null = null;
toggleGroup: ToggleContainer | null = null;
+ /** The group it belongs to: the one named, else the container its parent node holds, as in Cocos. */
+ private group_(): ToggleContainer | null {
+ return this.toggleGroup ?? this.node.parent?.getComponent(ToggleContainer) ?? null;
+ }
checkTargets: Array<Node | null> = [];
checkHandlers: Handlers = [];
checkEvents: EventHandler[] = [];
@@ -98,7 +105,7 @@ export class Toggle extends Button {
protected onClicked_(_event: EventTouch): void {
if (!this.interactable) return;
- const group = this.toggleGroup;
+ const group = this.group_();
// in a group that needs one on, clicking the one that is on does nothing
if (group && group.enabled && this.isChecked && !group.allowSwitchOff) return;
this.setChecked_(!this.isChecked, true);
@@ -106,10 +113,11 @@ export class Toggle extends Button {
/** @internal */
setChecked_(value: boolean, emit: boolean): void {
- if (this.isChecked === value) return;
- this.isChecked = value;
+ if (this.checked_ === value) return;
+ this.checked_ = value;
this.updateMark_();
- if (value && this.toggleGroup && this.toggleGroup.enabled) this.toggleGroup.notifyToggleCheck(this, emit);
+ const group = this.group_();
+ if (value && group && group.enabled) group.notifyToggleCheck(this, emit);
if (emit) {
EventHandler.emitEvents(this.checkEvents, this);
this.node.emit(Toggle.EventType.TOGGLE, this);
diff --git a/sdk/tests/cocos/controls-wasm.test.ts b/sdk/tests/cocos/controls-wasm.test.ts
index 473c9f80a..2ba2f5b36 100644
--- a/sdk/tests/cocos/controls-wasm.test.ts
+++ b/sdk/tests/cocos/controls-wasm.test.ts
@@ -102,6 +102,19 @@ describe.skipIf(!HAS_WASM)('toggles, bars, sliders and scroll views', () => {
expect(group.anyTogglesChecked()).toBe(false);
});
+ it('belongs to the container its parent holds, and switching one on by assignment switches the others off', async () => {
+ boot();
+ const holder = box('group', 400, 400, 400, 100);
+ holder.addComponent<ToggleContainer>('ToggleContainer')!;
+ const a = await toggle('a', 300, 400, holder);
+ const b = await toggle('b', 500, 400, holder); // no toggleGroup named: the parent's container is the group
+ await frame(1);
+ a.t.isChecked = true;
+ b.t.isChecked = true;
+ expect(b.t.isChecked).toBe(true);
+ expect(a.t.isChecked).toBe(false);
+ });
+
// ---- ProgressBar -----------------------------------------------------------------------------------------------
it('sizes its bar to total length times progress, clamped', async () => {
diff --git a/sdk/tests/cocos/touch-wasm.test.ts b/sdk/tests/cocos/touch-wasm.test.ts
index d1020ff97..0e6c8d11d 100644
--- a/sdk/tests/cocos/touch-wasm.test.ts
+++ b/sdk/tests/cocos/touch-wasm.test.ts
@@ -83,6 +83,22 @@ describe.skipIf(!HAS_WASM)('touches and buttons on the engine', () => {
expect(log).toHaveLength(2);
});
+ it('hands the touch to what is drawn last, not to what was made last: a node made later and placed first is underneath', async () => {
+ connect();
+ const root = box('root', 0, 0, 1000, 1000);
+ const content = box('content', 200, 200, 100, 100, root);
+ // a backdrop taken from a pool and put under the panel after the panel was built: made last, drawn first
+ const backdrop = box('backdrop', 200, 200, 400, 400, root);
+ backdrop.setSiblingIndex(0);
+ const log: string[] = [];
+ record(content, log);
+ record(backdrop, log);
+ await press(1, 210, 200);
+ await release(1, 210, 200);
+ expect(log).toHaveLength(2);
+ expect(log.every((l) => l.startsWith('content:'))).toBe(true);
+ });
+
it('keeps sending the gesture to the node that claimed it, wherever the finger goes', async () => {
connect();
const a = box('a', 200, 200);
diff --git a/src/esengine/ui/UISystem.cpp b/src/esengine/ui/UISystem.cpp
index aa776d30a..8ab37bbe8 100644
--- a/src/esengine/ui/UISystem.cpp
+++ b/src/esengine/ui/UISystem.cpp
@@ -16,11 +16,13 @@
#include "../ecs/components/UIVisual.hpp"
#include "../ecs/components/Sprite.hpp"
#include "../ecs/components/ContentRect.hpp"
+#include "../ecs/components/Hierarchy.hpp"
#include "../ecs/components/MeshRenderer.hpp"
#include "../resource/ResourceManager.hpp"
#include <algorithm>
#include <cstdint>
+#include <vector>
namespace esengine::ecs {
@@ -83,6 +85,36 @@ void UISystem::hitTestUpdate(Registry& registry, const PickRay& worldRay, const
hitWorldContent(registry, worldRay, resources);
}
+namespace {
+
+/// Where an entity stands in the hierarchy walk: its root, then the index of each ancestor and of itself among its siblings, so that two
+/// paths compare in the order the walk visits them (a parent before its children, an earlier sibling's whole subtree before a later one).
+std::vector<u32> hierarchyPath(Registry& registry, Entity entity) {
+ std::vector<u32> reversed;
+ for (Entity current = entity; ; ) {
+ const auto* parent = registry.tryGet<Parent>(current);
+ if (!parent || parent->entity == INVALID_ENTITY || !registry.valid(parent->entity)) {
+ reversed.push_back(current.id()); // roots are told apart by their number
+ break;
+ }
+ u32 index = 0;
+ if (const auto* children = registry.tryGet<Children>(parent->entity)) {
+ const auto& list = children->entities;
+ index = static_cast<u32>(std::find(list.begin(), list.end(), current) - list.begin());
+ }
+ reversed.push_back(index);
+ current = parent->entity;
+ }
+ return std::vector<u32>(reversed.rbegin(), reversed.rend());
+}
+
+/// Whether @p a is drawn before @p b: the hierarchy's order, which is the order a scene of nodes is stacked in.
+bool drawsBefore(Registry& registry, Entity a, Entity b) {
+ return hierarchyPath(registry, a) < hierarchyPath(registry, b);
+}
+
+} // namespace
+
/**
* World-space pick, for entities the layout tree does not contain — it takes its
* geometry from UINode, leaving a bare Sprite and every MeshRenderer unreachable.
@@ -106,9 +138,9 @@ void UISystem::hitWorldContent(Registry& registry, const PickRay& ray,
const bool sameDepth = std::abs(t - bestT) <= 1e-4f;
if (!sameDepth && t > bestT) return;
if (sameDepth && layer < bestLayer) return;
- // A full tie goes to the later entity, by its number rather than by where a pool happened to list it: the order content is
- // created in is the order it is drawn in, and which component pool is walked first says nothing about either.
- if (sameDepth && layer == bestLayer && entity.id() < best.id()) return;
+ // A full tie goes to the one drawn later, by its place in the hierarchy rather than by where a pool happened to list it or when
+ // it was created: a node taken from a pool and added under a panel is made last and drawn first, and a number cannot tell.
+ if (sameDepth && layer == bestLayer && drawsBefore(registry, entity, best)) return;
}
haveBest = true;
bestT = t;