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(animation): honor activity changes during frame callbacks
yhh0949f3071f23
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex aa8deb08e..7dd75b50e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Animation callbacks respect entity activity immediately.** Disabling an animator, entity or ancestor during a frame event stops remaining callbacks and sprite writes; later entries disabled by an earlier callback keep their playback position. Re-enabling applies deferred geometry without replaying handled events or restarting a completed one-shot.+- **Text submission preserves callback-selected focus.** A submit handler can focus the next field without the previous field's default blur clearing it. Submission still closes the editing session when the handler leaves focus unchanged.- **Text input events stay with the active editing session.** Switching fields no longer lets the previous surface's blur clear the new focus or its queued input overwrite the next value. Web and native editors reset interrupted composition state when focus restarts, so normal typing resumes without requiring a composition-end event.diff --git a/sdk/src/animation/SpriteAnimator.ts b/sdk/src/animation/SpriteAnimator.tsindex 7f1573bd2..7449adb23 100644--- a/sdk/src/animation/SpriteAnimator.ts+++ b/sdk/src/animation/SpriteAnimator.ts@@ -160,6 +160,10 @@ export class SpriteAnimationAPI {}private isCurrentAnimator(world: World, entity: Entity, animator: SpriteAnimatorData, clipName: string, clip: SpriteAnimClip, frames: SpriteAnimFrame[], frameCount: number): boolean {+ if (!world.isEntityActive(entity) || !animator.enabled) {+ this.pendingApplies.add(animator);+ return false;+ }if (world.tryGet(entity, SpriteAnimator) !== animator|| animator.clip !== clipName || this.getClip(clipName) !== clip|| this.pendingApplies.has(animator)) return false;@@ -235,6 +239,7 @@ export class SpriteAnimationAPI {for (const entity of entities) {// An earlier entity's event can remove a later entity/component// from the query snapshot we are currently walking.+ if (!world.isEntityActive(entity)) continue;const animator = world.tryGet(entity, SpriteAnimator);if (!animator) continue;if (!animator.enabled || (!animator.playing && !this.pendingApplies.has(animator)) || !animator.clip) continue;@@ -245,7 +250,7 @@ export class SpriteAnimationAPI {// playing raised on a finished one-shot = replay from the top// (TimelinePlayer's flag contract).- if (animator.finished) {+ if (animator.finished && animator.playing) {animator.finished = false;animator.currentFrame = 0;animator.frameTimer = 0;@@ -269,7 +274,7 @@ export class SpriteAnimationAPI {animator.frameTimer = Math.min(Number.MAX_VALUE,(Number.isFinite(animator.frameTimer) ? Math.max(0, animator.frameTimer) : 0) + elapsed);- let frameChanged = needsInitialApply;+ let frameChanged = needsInitialApply || this.pendingApplies.has(animator);// Accumulate one cycle while walking it, then skip whole extra cycles// in constant time. At most two cycles are inspected, with no backlog.let steps = 0;diff --git a/sdk/tests/fsm-sprite-anim.test.ts b/sdk/tests/fsm-sprite-anim.test.tsindex dd2fa3336..bff6622b4 100644--- a/sdk/tests/fsm-sprite-anim.test.ts+++ b/sdk/tests/fsm-sprite-anim.test.ts@@ -122,6 +122,7 @@ describe('finished-latch flag contract (system side)', () => {resolveGetter: () => null,resolveSetter: () => null,getEntitiesWithComponents: () => [1],+ isEntityActive: () => true,get: (_e: number, c: { _name: string }) => comps.get(c._name),tryGet: (_e: number, c: { _name: string }) => comps.get(c._name) ?? null,has: (_e: number, c: { _name: string }) => comps.has(c._name),diff --git a/sdk/tests/sprite-animation-activity-callback.test.ts b/sdk/tests/sprite-animation-activity-callback.test.tsnew file mode 100644index 000000000..2ec309908--- /dev/null+++ b/sdk/tests/sprite-animation-activity-callback.test.ts@@ -0,0 +1,78 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { expect, it, vi } from 'vitest';+import { bootMockApp } from './helpers/mockApp';+import { Disabled, Sprite } from '../src/ecs/component';+import { SpriteAnimator, SpriteAnimationAPI } from '../src/animation/SpriteAnimator';++function setup() {+ const { app } = bootMockApp(), world = app.world;+ const animation = new SpriteAnimationAPI();+ animation.registerClip({ name: 'walk', fps: 10, loop: true,+ frames: [{ texture: 11 }, { texture: 22 }, { texture: 33 }],+ events: [{ frame: 0, name: 'start' }, { frame: 1, name: 'step' }],+ });+ const spawn = () => {+ const entity = world.spawn();+ world.insert(entity, Sprite, { texture: 99 });+ world.insert(entity, SpriteAnimator, { clip: 'walk' });+ return entity;+ };+ return { app, world, animation, spawn };+}++it.each(['self', 'ancestor', 'component'] as const)('stops event dispatch and sprite writes when a callback disables %s', target => {+ const { app, world, animation, spawn } = setup(), entity = spawn();+ const parent = world.spawn();+ world.setParent(entity, parent);+ const events: string[] = [], global = vi.fn();+ const off = animation.onEvent(entity, event => {+ events.push(event.name);+ if (target === 'component') world.update(entity, SpriteAnimator, data => { data.enabled = false; });+ else world.insert(target === 'self' ? entity : parent, Disabled);+ });+ animation.onEventGlobal(global);+ animation.update(world, 0.15);+ expect(events).toEqual(['start']);+ expect(global).not.toHaveBeenCalled();+ expect(world.get(entity, Sprite).texture).toBe(99);+ off();+ if (target === 'component') world.update(entity, SpriteAnimator, data => { data.enabled = true; });+ else world.remove(target === 'self' ? entity : parent, Disabled);+ animation.update(world, 0);+ expect(world.get(entity, Sprite).texture).toBe(22);+ expect(global).not.toHaveBeenCalled();+ app.quit();+});++it('applies a deferred final frame without replaying a completed one-shot', () => {+ const { app, world, animation, spawn } = setup(), entity = spawn();+ world.update(entity, SpriteAnimator, data => { data.loop = false; });+ const off = animation.onEvent(entity, () => world.insert(entity, Disabled));+ animation.update(world, 1);+ expect(world.get(entity, SpriteAnimator).finished).toBe(true);+ expect(world.get(entity, Sprite).texture).toBe(99);+ off();+ world.remove(entity, Disabled);+ animation.update(world, 0);+ expect(world.get(entity, SpriteAnimator).currentFrame).toBe(2);+ expect(world.get(entity, SpriteAnimator).finished).toBe(true);+ expect(world.get(entity, SpriteAnimator).playing).toBe(false);+ expect(world.get(entity, Sprite).texture).toBe(33);+ app.quit();+});++it.each(['entity', 'ancestor'] as const)('skips a later query entry disabled through its %s by an earlier callback', target => {+ const { app, world, animation, spawn } = setup(), first = spawn(), next = spawn();+ const parent = world.spawn();+ world.setParent(next, parent);+ const nextEvent = vi.fn();+ animation.onEvent(first, () => world.insert(target === 'entity' ? next : parent, Disabled));+ animation.onEvent(next, nextEvent);+ animation.update(world, 0.15);+ expect(world.get(next, SpriteAnimator).currentFrame).toBe(0);+ expect(world.get(next, SpriteAnimator).frameTimer).toBe(0);+ expect(world.get(next, Sprite).texture).toBe(99);+ expect(nextEvent).not.toHaveBeenCalled();+ app.quit();+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex b1d7eef1f..f8296b4d9 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(animation): honor activity changes during frame callbacks':+ { note: 'Animation callbacks respect entity activity immediately.' },'fix(ui): preserve focus selected by text submit handlers':{ note: 'Text submission preserves callback-selected focus.' },'fix(ui): bind text input events to the active session':