mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Add Tween.update API. (#17099)
* And make ActionInterval.reverse abstract.
This commit is contained in:
@@ -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<T extends object, Args extends any[]> extends ActionInterval {
|
||||
private _cb: TTweenUpdateCallback<T, Args>;
|
||||
private _args: Args;
|
||||
|
||||
constructor (duration: number, cb: TTweenUpdateCallback<T, Args>, args: Args) {
|
||||
super(duration);
|
||||
this._cb = cb;
|
||||
this._args = args;
|
||||
}
|
||||
|
||||
clone (): ActionCustomUpdate<T, Args> {
|
||||
return new ActionCustomUpdate(this._duration, this._cb, this._args);
|
||||
}
|
||||
|
||||
update (ratio: number): void {
|
||||
this._cb(this.target as T, ratio, ...this._args);
|
||||
}
|
||||
|
||||
reverse (): ActionCustomUpdate<T, Args> {
|
||||
return this.clone();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
+20
-1
@@ -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> = T extends Node ? Tween<T> : unknown;
|
||||
|
||||
const notIntervalPrompt = 'the last action is not ActionInterval';
|
||||
|
||||
export type TTweenUpdateCallback<T extends object, Args extends any[]> = (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<T extends object = any> {
|
||||
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<Args extends any[]> (duration: number, cb: TTweenUpdateCallback<T, Args>, ...args: Args): Tween<T> {
|
||||
const action = new ActionCustomUpdate<T, Args>(duration, cb, args);
|
||||
this._actions.push(action);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @en
|
||||
* Directly set target properties.
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
Reference in New Issue
Block a user