From 99183fd2e4e7806546dcd7faa7781444aed0f7c2 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 4 Jun 2024 16:07:12 +0800 Subject: [PATCH] Add Tween.update API. (#17099) * And make ActionInterval.reverse abstract. --- cocos/tween/actions/action-interval.ts | 29 ++++- cocos/tween/index.ts | 2 +- cocos/tween/tween.ts | 21 +++- tests/tween/tween.test.ts | 158 +++++++++++++++++++++++++ 4 files changed, 204 insertions(+), 6 deletions(-) diff --git a/cocos/tween/actions/action-interval.ts b/cocos/tween/actions/action-interval.ts index e6d1c5a120..d0f547b4c2 100644 --- a/cocos/tween/actions/action-interval.ts +++ b/cocos/tween/actions/action-interval.ts @@ -28,6 +28,7 @@ import { FiniteTimeAction, Action } from './action'; import { macro, logID, errorID } from '../../core'; import { ActionInstant } from './action-instant'; +import type { TTweenUpdateCallback } from '../tween'; // Extra action for making a Sequence or Spawn when only adding one action to it. class DummyAction extends FiniteTimeAction { @@ -133,10 +134,7 @@ export abstract class ActionInterval extends FiniteTimeAction { this._firstTick = true; } - reverse (): ActionInterval { - logID(1010); - return this; - } + abstract reverse (): ActionInterval; /** * @en @@ -939,3 +937,26 @@ export class ReverseTime extends ActionInterval { export function reverseTime (action: ActionInterval): ReverseTime { return new ReverseTime(action); } + +export class ActionCustomUpdate extends ActionInterval { + private _cb: TTweenUpdateCallback; + private _args: Args; + + constructor (duration: number, cb: TTweenUpdateCallback, args: Args) { + super(duration); + this._cb = cb; + this._args = args; + } + + clone (): ActionCustomUpdate { + return new ActionCustomUpdate(this._duration, this._cb, this._args); + } + + update (ratio: number): void { + this._cb(this.target as T, ratio, ...this._args); + } + + reverse (): ActionCustomUpdate { + return this.clone(); + } +} diff --git a/cocos/tween/index.ts b/cocos/tween/index.ts index fbafe26032..68a95666e6 100644 --- a/cocos/tween/index.ts +++ b/cocos/tween/index.ts @@ -23,6 +23,6 @@ */ export { TweenSystem } from './tween-system'; -export { tween, tweenUtil, Tween } from './tween'; +export * from './tween'; export * from './export-api'; export { TweenAction } from './tween-action'; diff --git a/cocos/tween/tween.ts b/cocos/tween/tween.ts index 8c11b7ef51..d75e5afd3d 100644 --- a/cocos/tween/tween.ts +++ b/cocos/tween/tween.ts @@ -24,7 +24,10 @@ import { TweenSystem } from './tween-system'; import { warn } from '../core'; -import { ActionInterval, sequence, reverseTime, delayTime, spawn, Sequence, Spawn, repeat, repeatForever, RepeatForever } from './actions/action-interval'; +import { + ActionInterval, sequence, reverseTime, delayTime, spawn, Sequence, + Spawn, repeat, repeatForever, RepeatForever, ActionCustomUpdate, +} from './actions/action-interval'; import { removeSelf, show, hide, callFunc, TCallFuncCallback } from './actions/action-instant'; import { Action, FiniteTimeAction } from './actions/action'; import { ITweenOption } from './export-api'; @@ -44,6 +47,8 @@ type TweenWithNodeTargetOrUnknown = T extends Node ? Tween : unknown; const notIntervalPrompt = 'the last action is not ActionInterval'; +export type TTweenUpdateCallback = (target: T, ratio: number, ...args: Args) => void; + /** * @en * Tween provide a simple and flexible way to action, It's transplanted from cocos creator。 @@ -410,6 +415,20 @@ export class Tween { return this; } + /** + * @en Add an custom action. + * @zh 添加一个自定义动作。 + * @param duration @en The tween time in seconds. @zh 缓动时间,单位为秒。 + * @param cb @en The callback of the current action. @zh 动作回调函数。 + * @param args @en The arguments passed to the callback function. @zh 传递给动作回调函数的参数。 + * @return @en The current tween instance, for chain operations. @zh 当前缓动示例,用于级联操作。 + */ + update (duration: number, cb: TTweenUpdateCallback, ...args: Args): Tween { + const action = new ActionCustomUpdate(duration, cb, args); + this._actions.push(action); + return this; + } + /** * @en * Directly set target properties. diff --git a/tests/tween/tween.test.ts b/tests/tween/tween.test.ts index 2cdb944628..f7a0eaec48 100644 --- a/tests/tween/tween.test.ts +++ b/tests/tween/tween.test.ts @@ -2499,6 +2499,164 @@ test('pauseAllByTarget/resumeAllByTarget', function () { expect(node2.position.equals(new Vec3(90, 90, 90))).toBeTruthy(); expect(node2.scale.equals(new Vec3(9, 9, 9))).toBeTruthy(); + // + director.unregisterSystem(sys); +}); + +test('update 1', function () { + const sys = new TweenSystem(); + (TweenSystem.instance as any) = sys; + director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM); + // + const node = new Node(); + node.setScale(0, 0, 0); + + let done = false; + let currentRatio = 0; + + tween(node) + .delay(1) + .by(1, { position: v3(90, 90, 90) }) + .update(1, (target: Node, ratio: number, a: number, b: boolean, c: string, d: { 'world': () => number }): void =>{ + expect(target === node).toBeTruthy(); + expect(a).toBe(123); + expect(b).toBe(true); + expect(c).toBe('hello'); + expect(d['world']()).toBe(456); + currentRatio = ratio; + }, 123, true, 'hello', { 'world': (): number => 456 }) + .repeat(2) + .call(()=>{ done = true; }) + .start(); + + // Start + runFrames(1); + expect(node.position.equals(new Vec3(0, 0, 0))).toBeTruthy(); + + runFrames(60); + expect(node.position.equals(new Vec3(0, 0, 0))).toBeTruthy(); + + runFrames(60); + expect(node.position.equals(new Vec3(90, 90, 90))).toBeTruthy(); + + runFrames(20); + expect(currentRatio).toBeCloseTo(1/3); + runFrames(20); + expect(currentRatio).toBeCloseTo(2/3); + runFrames(20); + expect(currentRatio).toBeCloseTo(3/3); + + runFrames(20); + expect(currentRatio).toBeCloseTo(1/3); + runFrames(20); + expect(currentRatio).toBeCloseTo(2/3); + runFrames(20); + expect(currentRatio).toBeCloseTo(3/3); + + runFrames(1); // Do one more step to make the tween done! + expect(done).toBeTruthy(); + + // + director.unregisterSystem(sys); +}); + +test('update 2', function () { + const sys = new TweenSystem(); + (TweenSystem.instance as any) = sys; + director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM); + // + const node = new Node(); + node.setScale(0, 0, 0); + + let done = false; + let currentRatio = 0; + + const args = [123, true, 'hello', { 'world': (): number => 456 }]; + + tween(node) + .delay(1) + .by(1, { position: v3(90, 90, 90) }) + .update(1, (target: Node, ratio: number, ...args: any[]): void =>{ + expect(target === node).toBeTruthy(); + const [a, b, c, d] = args; + expect(a).toBe(123); + expect(b).toBe(true); + expect(c).toBe('hello'); + expect(d['world']()).toBe(456); + currentRatio = ratio; + }, ...args) + .call(()=>{ done = true; }) + .start(); + + // Start + runFrames(1); + expect(node.position.equals(new Vec3(0, 0, 0))).toBeTruthy(); + + runFrames(60); + expect(node.position.equals(new Vec3(0, 0, 0))).toBeTruthy(); + + runFrames(60); + expect(node.position.equals(new Vec3(90, 90, 90))).toBeTruthy(); + + runFrames(20); + expect(currentRatio).toBeCloseTo(1/3); + runFrames(20); + expect(currentRatio).toBeCloseTo(2/3); + runFrames(20); + expect(currentRatio).toBeCloseTo(3/3); + + runFrames(1); // Do one more step to make the tween done! + expect(done).toBeTruthy(); + + // + director.unregisterSystem(sys); +}); + +test('update 3', function () { + const sys = new TweenSystem(); + (TweenSystem.instance as any) = sys; + director.registerSystem(TweenSystem.ID, sys, System.Priority.MEDIUM); + // + const node = new Node(); + node.setScale(0, 0, 0); + + const head = new Node(); + head.setPosition(0, 90, 0); + const hand = new Node(); + hand.setPosition(0, 60, 0); + const foot = new Node(); + foot.setPosition(0, 30, 0); + + tween(node) + .update(1.0, (target: Node, ratio: number, head: Node, hand: Node, foot: Node) => { + const [v1, v2, v3] = [100, 10, 1]; + head.setPosition(v1 * ratio, head.position.y, head.position.z); + hand.setPosition(v2 * ratio, hand.position.y, hand.position.z); + foot.setPosition(v3 * ratio, foot.position.y, foot.position.z); + }, head, hand, foot) + .start(); + + // Start + runFrames(1); + expect(head.position.equals(new Vec3(0, 90, 0))).toBeTruthy(); + expect(hand.position.equals(new Vec3(0, 60, 0))).toBeTruthy(); + expect(foot.position.equals(new Vec3(0, 30, 0))).toBeTruthy(); + + runFrames(20); + expect(head.position.equals(new Vec3(100 * 1/3, 90, 0))).toBeTruthy(); + expect(hand.position.equals(new Vec3(10 * 1/3, 60, 0))).toBeTruthy(); + expect(foot.position.equals(new Vec3(1 * 1/3, 30, 0))).toBeTruthy(); + + runFrames(20); + expect(head.position.equals(new Vec3(100 * 2/3, 90, 0))).toBeTruthy(); + expect(hand.position.equals(new Vec3(10 * 2/3, 60, 0))).toBeTruthy(); + expect(foot.position.equals(new Vec3(1 * 2/3, 30, 0))).toBeTruthy(); + + runFrames(20); + expect(head.position.equals(new Vec3(100 * 3/3, 90, 0))).toBeTruthy(); + expect(hand.position.equals(new Vec3(10 * 3/3, 60, 0))).toBeTruthy(); + expect(foot.position.equals(new Vec3(1 * 3/3, 30, 0))).toBeTruthy(); + // director.unregisterSystem(sys); }); \ No newline at end of file