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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(ui): retire object drag capture before cancelled movement
yhh292319d99380
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 904975504..455df8078 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Object drags stop when their input or target becomes unavailable.** Editor-consumed releases, disabled or removed drag components, hidden targets and lost pointer projections clear capture before another position write. Drag callbacks may disable or remove their target safely, and an active gesture emits its end once without resuming later.+- **Cancelled scroll gestures stop without flinging.** Editor-consumed pointer releases cancel capture even after UI pointer sampling consumes its marker. Inactive, noninteractive or hidden viewports stop dragging and coasting; hidden layout boxes no longer clamp the saved scroll position to zero; losing all pointer projections discards motion instead of resuming an old drag when the view returns.- **Scene scroll settings update while the game is running.** Changes to axes, wheel speed, drag enablement and movement settings rebuild the adopted container while preserving legal offsets. Disabling both axes suspends scrolling, reactivation restores the content position, and replaced containers cannot inherit an old pointer drag or fling.diff --git a/sdk/src/ui/input/drag.ts b/sdk/src/ui/input/drag.tsindex 5db83f12b..dbb9fbf4a 100644--- a/sdk/src/ui/input/drag.ts+++ b/sdk/src/ui/input/drag.ts@@ -9,12 +9,14 @@ import { px } from '../core/dimension';import { defineSystem, Schedule } from '../../ecs/system';import { Res } from '../../ecs/resource';import { Input } from '../../input/input';+import { primaryPointerCancellationVersion } from '../../input/pointerCancellation';+import { engineApi } from '../../ecs/bridge/engineApi';import type { InputState } from '../../input/input';import type { Entity } from '../../types';import type { World } from '../../ecs/world';import { Draggable, DragState } from './draggable';import type { DraggableData, DragStateData } from './draggable';-import { UIInteraction } from './interactable';+import { Interactable, UIInteraction } from './interactable';import type { UIInteractionData } from './interactable';import { UIEvents, UIEventQueue } from '../core/events';import { UICameraInfo } from '../core/ui-camera-info';@@ -88,16 +90,42 @@ export class DragPlugin implements Plugin {const world = app.world;const events = app.getResource(UIEvents) as UIEventQueue;+ const engine = engineApi(app);+ const registry = world.getCppRegistry();+ const eligible = (entity: Entity): boolean => world.isEntityActive(entity)+ && !world.isDestructionPending(entity)+ && world.has(entity, Draggable) && world.get(entity, Draggable).enabled+ && world.has(entity, Transform)+ && (!world.has(entity, Interactable) || world.get(entity, Interactable).enabled)+ && (!world.has(entity, UINode) || !engine?.getUINodeHiddenInTree?.(registry!, entity));let pendingEntity: Entity | null = null;let pendingStartWorld = { x: 0, y: 0 };let activeEntity: Entity | null = null;+ let lastCancellationVersion = primaryPointerCancellationVersion(app.getResource(Input));+ const stopCapture = (): void => {+ const entity = activeEntity ?? pendingEntity;+ const started = activeEntity !== null;+ activeEntity = pendingEntity = null;+ if (entity === null || !world.valid(entity) || world.isDestructionPending(entity)) return;+ if (world.has(entity, DragState)) {+ world.update(entity, DragState, d => { d.isDragging = false; });+ }+ if (started) events.emit(entity, 'drag_end');+ };app.addSystemToSchedule(Schedule.PreUpdate, defineSystem([Res(Input), Res(UICameraInfo), Res(ScreenLayout), Res(ScreenOverlay)],(input: InputState, camera: UICameraData, layout: ScreenLayoutData,overlay: ScreenOverlayData) => {- if (!camera.valid && !overlay.active) return;+ const cancellationVersion = primaryPointerCancellationVersion(input);+ const cancelled = cancellationVersion !== lastCancellationVersion;+ lastCancellationVersion = cancellationVersion;+ if (!camera.valid && !overlay.active) { stopCapture(); return; }+ const captured = activeEntity ?? pendingEntity;+ if (captured !== null && (cancelled || !eligible(captured)+ || !world.has(captured, DragState)+ || (!input.isMouseButtonDown(0) && !input.isMouseButtonReleased(0)))) stopCapture();// Per ENTITY, not once per frame: a drag subtracts a pointer from// a transform, and the two share coordinates only if the pointer@@ -105,15 +133,14 @@ export class DragPlugin implements Plugin {const pointerFor = (entity: Entity): { x: number; y: number } =>uiPointerFor(world, entity, camera, overlay);- if (input.isMouseButtonPressed(0)) {+ if (input.isMouseButtonPressed(0) && input.isMouseButtonDown(0)) {const draggableEntities = world.getEntitiesWithComponents([Draggable, UIInteraction]);let bestEntity: Entity | null = null;let bestLayer = -Infinity;let bestDepth = -1;for (const entity of draggableEntities) {- const draggable = world.get(entity, Draggable) as DraggableData;- if (!draggable.enabled) continue;+ if (!eligible(entity)) continue;const interaction = world.get(entity, UIInteraction) as UIInteractionData;if (interaction.hovered) {let layer = 0;@@ -178,8 +205,8 @@ export class DragPlugin implements Plugin {}if (activeEntity !== null) {- if (!world.valid(activeEntity) || !world.has(activeEntity, DragState)) {- activeEntity = null;+ if (!eligible(activeEntity) || !world.has(activeEntity, DragState)) {+ stopCapture();return;}@@ -234,10 +261,13 @@ export class DragPlugin implements Plugin {events.emit(activeEntity, 'drag_move');+ if (!eligible(activeEntity) || !world.has(activeEntity, DragState)) {+ stopCapture();+ return;+ }+if (input.isMouseButtonReleased(0)) {- world.update(activeEntity, DragState, (d) => { d.isDragging = false; });- events.emit(activeEntity, 'drag_end');- activeEntity = null;+ stopCapture();}}},diff --git a/sdk/tests/ui-drag-cancel-wasm.test.ts b/sdk/tests/ui-drag-cancel-wasm.test.tsnew file mode 100644index 000000000..eeac1ad96--- /dev/null+++ b/sdk/tests/ui-drag-cancel-wasm.test.ts@@ -0,0 +1,63 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';+import { App, flushPendingRegistrations } from '../src/app/app';+import { AppContext, setDefaultContext } from '../src/ecs/context';+import { Canvas, Disabled, Draggable, Transform } from '../src/ecs/component';+import { Input, InputState, inputEventCallbacks } from '../src/input/input';+import { inputRouter } from '../src/input/inputRouter';+import { takePrimaryPointerCancellation } from '../src/input/pointerCancellation';+import { UICameraInfo } from '../src/ui/core/ui-camera-info';+import { UINode } from '../src/ui/core/ui-node';+import { UIEvents, UIEventQueue } from '../src/ui/core/events';+import { UIInteraction } from '../src/ui/input/interactable';+import { DragPlugin } from '../src/ui/input/drag';+import { DragState } from '../src/ui/input/draggable';+import type { CppRegistry, ESEngineModule } from '../src/wasm';+import type { Entity } from '../src/types';+import { HAS_WASM, loadWasmModule } from './helpers/loadWasm';++describe.skipIf(!HAS_WASM)('drag cancellation on real WASM', () => {+ let engine: ESEngineModule, registry: CppRegistry, app: App, input: InputState, entity: Entity;+ beforeAll(async () => { engine = await loadWasmModule(); });+ beforeEach(() => {+ setDefaultContext(new AppContext()); registry = new engine.Registry();+ app = App.new(); app.connectCpp(registry, engine);+ input = new InputState(); app.insertResource(Input, input);+ app.insertResource(UIEvents, new UIEventQueue());+ app.insertResource(UICameraInfo, { ...UICameraInfo._default, valid: true, vpW: 100, vpH: 100 });+ new DragPlugin().build(app); flushPendingRegistrations(app);+ entity = app.world.spawn(); app.world.insert(entity, Transform); app.world.insert(entity, Draggable);+ app.world.insert(entity, UIInteraction, { hovered: true });+ });+ afterEach(() => { app.quit({ keepRenderer: true }); registry.delete(); });+ async function step(x: number) {+ app.getResource(UICameraInfo).worldMouseX = x;+ await app.tick(1 / 60); input.clearFrameState();+ }+ async function drag() { input.noteMouseDown(0); await step(0); await step(20); }+ function stopped() {+ expect(app.world.get(entity, DragState).isDragging).toBe(false);+ expect(app.world.get(entity, Transform).position.x).toBeCloseTo(20);+ }+ it('claimed release ends capture after the UI sample consumes cancellation', async () => {+ await drag(); const off = inputRouter.setEditorHandler({ onPointerUp: () => true });+ try { inputEventCallbacks(input).onPointerUp(0); } finally { off(); }+ takePrimaryPointerCancellation(input); await step(100); stopped();+ });+ it.each(['inactive', 'disabled'])('%s targets cannot keep dragging', async kind => {+ await drag();+ if (kind === 'inactive') app.world.insert(entity, Disabled);+ else app.world.update(entity, Draggable, d => { d.enabled = false; });+ await step(100); stopped();+ });+ it('hidden targets stop at the position reached before layout hides them', async () => {+ await drag(); app.world.insert(entity, Canvas); app.world.insert(entity, UINode, { display: 1 });+ engine.uiLayout_update(registry, -50, -50, 50, 50, true);+ expect(engine.getUINodeHiddenInTree!(registry, entity)).toBe(true);+ const resolved = app.world.get(entity, Transform).position.x;+ await step(100);+ expect(app.world.get(entity, DragState).isDragging).toBe(false);+ expect(app.world.get(entity, Transform).position.x).toBe(resolved);+ });+});diff --git a/sdk/tests/ui-drag-plugin.test.ts b/sdk/tests/ui-drag-plugin.test.tsindex 49e9cb37b..988dd48f5 100644--- a/sdk/tests/ui-drag-plugin.test.ts+++ b/sdk/tests/ui-drag-plugin.test.ts@@ -9,17 +9,19 @@* observable state (DragState.isDragging, Transform.position) rather than* the emitted events.*/-import { describe, it, expect, beforeEach } from 'vitest';+import { describe, it, expect, beforeEach, vi } from 'vitest';import { flushPendingRegistrations } from '../src/app/app';import type { App } from '../src/app/app';-import { Transform } from '../src/ecs/component';-import { Input, InputState } from '../src/input/input';+import { Disabled, Transform } from '../src/ecs/component';+import { Input, InputState, inputEventCallbacks } from '../src/input/input';+import { inputRouter } from '../src/input/inputRouter';+import { takePrimaryPointerCancellation } from '../src/input/pointerCancellation';import { UIEvents, UIEventQueue } from '../src/ui/core/events';import { UICameraInfo } from '../src/ui/core/ui-camera-info';import type { UICameraData } from '../src/ui/core/ui-camera-info';import { Draggable, DragState } from '../src/ui/input/draggable';import type { DragStateData } from '../src/ui/input/draggable';-import { UIInteraction } from '../src/ui/input/interactable';+import { Interactable, UIInteraction } from '../src/ui/input/interactable';import { dragPlugin } from '../src/ui/input/drag';import { bootMockApp } from './helpers/mockApp';import type { Entity } from '../src/types';@@ -48,8 +50,6 @@ function draggable(opts: Partial<{ enabled: boolean; dragThreshold: number; lockdragThreshold: opts.dragThreshold ?? 5,lockX: opts.lockX ?? false,lockY: opts.lockY ?? false,- constraintMin: null,- constraintMax: null,});// hovered is set by hand here, which makes every test below about the drag// LOGIC and none of them about whether such an entity can be hovered at all.@@ -148,3 +148,50 @@ describe('DragPlugin hit-test gating', () => {expect(posX(e)).toBe(0);});});++describe('DragPlugin cancellation', () => {+ it('an editor-consumed release stops without applying another pointer movement', async () => {+ const e = draggable(); await press(); await hold(20, 0);+ const off = inputRouter.setEditorHandler({ onPointerUp: () => true });+ try { inputEventCallbacks(input()).onPointerUp(0); } finally { off(); }+ takePrimaryPointerCancellation(input());+ cam().worldMouseX = 100; await step();+ expect(posX(e)).toBeCloseTo(20);+ expect(dragOf(e).isDragging).toBe(false);+ });+ it.each(['inactive', 'draggable-disabled', 'noninteractive', 'removed'])('%s cancels an active drag', async kind => {+ const e = draggable(); await press(); await hold(20, 0);+ if (kind === 'inactive') app.world.insert(e, Disabled);+ if (kind === 'draggable-disabled') app.world.update(e, Draggable, d => { d.enabled = false; });+ if (kind === 'noninteractive') app.world.insert(e, Interactable, { enabled: false });+ if (kind === 'removed') app.world.remove(e, Draggable);+ await hold(100, 0);+ expect(posX(e)).toBeCloseTo(20);+ expect(dragOf(e).isDragging).toBe(false);+ });+ it('losing the pointer projection cannot resume an old drag', async () => {+ const e = draggable(); await press(); await hold(20, 0);+ cam().valid = false; await hold(40, 0);+ cam().valid = true; await hold(100, 0);+ expect(posX(e)).toBeCloseTo(20);+ expect(dragOf(e).isDragging).toBe(false);+ });+ it('a drag_start callback can disable the target before its first movement', async () => {+ const e = draggable(), events = app.getResource(UIEvents), ended = vi.fn();+ events.on(e, 'drag_start', () => app.world.update(e, Draggable, d => { d.enabled = false; }));+ events.on(e, 'drag_end', ended);+ await press(); await hold(20, 0); await hold(100, 0);+ expect(posX(e)).toBe(0);+ expect(dragOf(e).isDragging).toBe(false);+ expect(ended).toHaveBeenCalledTimes(1);+ });+ it('a drag_move callback can remove Draggable without another movement or duplicate end', async () => {+ const e = draggable(), events = app.getResource(UIEvents), ended = vi.fn();+ events.on(e, 'drag_move', () => app.world.remove(e, Draggable));+ events.on(e, 'drag_end', ended);+ await press(); await hold(20, 0); await release(100, 0);+ expect(posX(e)).toBeCloseTo(20);+ expect(dragOf(e).isDragging).toBe(false);+ expect(ended).toHaveBeenCalledTimes(1);+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex aa6e24541..7a80b4c1e 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(ui): retire object drag capture before cancelled movement':+ { note: 'Object drags stop when their input or target becomes unavailable.' },'fix(ui): cancel scroll capture and motion when input ownership changes':{ note: 'Cancelled scroll gestures stop without flinging.' },'fix(ui): apply live scroll settings and isolate replaced motion':