mirror of
https://github.com/motajs/template.git
synced 2026-11-27 16:34:31 +08:00
refactor: 渲染系统
This commit is contained in:
1 parent
9ab0db9465
commit
191ba8d1db
74 files changed
+3776
-7748
No files matched your search
@@ -67,7 +67,7 @@ export class Animater implements IAnimater {
|
||||
private planning: boolean = false;
|
||||
|
||||
/** 当前绑定的激励源 */
|
||||
private excitation: IExcitation<number> | null = null;
|
||||
excitation: IExcitation<number> | null = null;
|
||||
/** 当前定义在绑定激励源上的可激励对象 */
|
||||
private controller: IExcitableController<number> | null = null;
|
||||
|
||||
@@ -317,6 +317,22 @@ export class Animater implements IAnimater {
|
||||
return index;
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.unbindExcitation();
|
||||
this.animatableStatus = null;
|
||||
this.currentAnimatable = null;
|
||||
this.planningStore.clear();
|
||||
this.groupStore.clear();
|
||||
this.whens.clear();
|
||||
this.afters.clear();
|
||||
this.executingGroupObj = null;
|
||||
this.executing.clear();
|
||||
this.executingMap.clear();
|
||||
this.planningStart.clear();
|
||||
this.whenBind = null;
|
||||
this.afterBind = null;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 动画执行
|
||||
|
||||
@@ -5,7 +5,8 @@ import {
|
||||
IExcitableController,
|
||||
IExcitationVariator,
|
||||
ExcitationCurve,
|
||||
VariatorCurveMode
|
||||
VariatorCurveMode,
|
||||
IExcitationDivider
|
||||
} from './types';
|
||||
import { excited } from './utils';
|
||||
|
||||
@@ -323,3 +324,63 @@ export class ExcitationVariator
|
||||
super.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
export class ExcitationDivider<T>
|
||||
extends ExcitationBase<T>
|
||||
implements IExcitationDivider<T>
|
||||
{
|
||||
divider: number = 1;
|
||||
source: IExcitation<T> | null = null;
|
||||
|
||||
/** 当前的激励对象控制器 */
|
||||
private controller: IExcitableController<T> | null = null;
|
||||
/** 当前的激励负载 */
|
||||
private nowPayload: T | null = null;
|
||||
/** 分频计数器 */
|
||||
private counter: number = 0;
|
||||
|
||||
payload(): T {
|
||||
if (!this.source) {
|
||||
logger.error(52);
|
||||
throw new Error('Expected an excitation binding');
|
||||
}
|
||||
return this.nowPayload ?? this.source.payload();
|
||||
}
|
||||
|
||||
/**
|
||||
* 激励当前所有的激励源
|
||||
* @param payload 激励负载
|
||||
*/
|
||||
excite(payload: T) {
|
||||
this.counter++;
|
||||
if (this.counter >= this.divider) {
|
||||
this.counter = 0;
|
||||
this.nowPayload = payload;
|
||||
super.excite(payload);
|
||||
}
|
||||
}
|
||||
|
||||
bindExcitation(excitation: IExcitation<T>): void {
|
||||
this.unbindExcitation();
|
||||
this.source = excitation;
|
||||
this.divider = 1;
|
||||
this.counter = this.divider - 1;
|
||||
this.nowPayload = excitation.payload();
|
||||
const controller = excitation.add(
|
||||
excited(payload => this.excite(payload))
|
||||
);
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
unbindExcitation(): void {
|
||||
this.controller?.revoke();
|
||||
this.counter = 0;
|
||||
this.divider = 1;
|
||||
}
|
||||
|
||||
setDivider(divider: number): void {
|
||||
if (!this.source) return;
|
||||
this.divider = divider;
|
||||
this.counter = divider - 1;
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ interface ITransitionData {
|
||||
|
||||
export class Transition implements ITransition {
|
||||
/** 当前绑定的激励源 */
|
||||
private excitation: IExcitation<number> | null = null;
|
||||
excitation: IExcitation<number> | null = null;
|
||||
/** 当前定义在绑定激励源上的可激励对象 */
|
||||
private controller: IExcitableController<number> | null = null;
|
||||
|
||||
@@ -64,7 +64,7 @@ export class Transition implements ITransition {
|
||||
return this;
|
||||
}
|
||||
|
||||
transite(animatable: IAnimatable): this {
|
||||
transition(animatable: IAnimatable): this {
|
||||
this.animatableStatus = animatable;
|
||||
return this;
|
||||
}
|
||||
@@ -115,6 +115,12 @@ export class Transition implements ITransition {
|
||||
this.animatableStatus = null;
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.controller?.revoke();
|
||||
this.unbindExcitation();
|
||||
this.animatableStatus = null;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 渐变执行
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//#region 激励对象
|
||||
|
||||
/**
|
||||
* 动画速率曲线,输入时间完成度,输出一个值,时间完成度范围在 `0-1` 之间,而输出值没有范围限制。
|
||||
* 在不同场景下,速率曲线返回值的含义可能不同,有的可能表示动画完成度,有的可能表示绝对的坐标值。
|
||||
@@ -43,6 +45,10 @@ export interface IExcitableController<T> {
|
||||
excite(payload: T): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 激励源
|
||||
|
||||
export interface IExcitation<T> {
|
||||
/**
|
||||
* 获取当前的激励负载
|
||||
@@ -94,7 +100,7 @@ export interface IExcitationVariator extends IExcitation<number> {
|
||||
readonly source: IExcitation<number> | null;
|
||||
|
||||
/**
|
||||
* 绑定激励源对象,变速器将以此激励源为基础实施变速
|
||||
* 绑定激励源对象,变速器将以此激励源为基础实施变速。绑定后当前速度值会被设为 1。
|
||||
* @param excitation 绑定的激励源
|
||||
*/
|
||||
bindExcitation(excitation: IExcitation<number>): void;
|
||||
@@ -128,6 +134,34 @@ export interface IExcitationVariator extends IExcitation<number> {
|
||||
endAllCurves(): void;
|
||||
}
|
||||
|
||||
export interface IExcitationDivider<T> extends IExcitation<T> {
|
||||
/** 分频器当前的分频比例 */
|
||||
readonly divider: number;
|
||||
/** 当前绑定的激励源 */
|
||||
readonly source: IExcitation<T> | null;
|
||||
|
||||
/**
|
||||
* 绑定激励源对象,分配器将以此激励源为基础实施分配。绑定后分频比例会设为 1。
|
||||
* @param excitation 绑定的激励源
|
||||
*/
|
||||
bindExcitation(excitation: IExcitation<T>): void;
|
||||
|
||||
/**
|
||||
* 取消绑定当前的激励源
|
||||
*/
|
||||
unbindExcitation(): void;
|
||||
|
||||
/**
|
||||
* 设置当前分频器的分配比例。设置后会在下一次激励源被激励时便触发一次激励,然后按照分配比例触发。
|
||||
* @param divider 分配比例
|
||||
*/
|
||||
setDivider(divider: number): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 动画类
|
||||
|
||||
export interface IAnimatable {
|
||||
/** 动画数值 */
|
||||
value: number;
|
||||
@@ -173,6 +207,9 @@ export const enum EndRelation {
|
||||
}
|
||||
|
||||
export interface IAnimater extends IExcitable<number> {
|
||||
/** 渐变对象绑定的激励源 */
|
||||
readonly excitation: IExcitation<number> | null;
|
||||
|
||||
/**
|
||||
* 在动画执行器上绑定激励源
|
||||
* @param excitation 绑定的激励源
|
||||
@@ -310,9 +347,17 @@ export interface IAnimater extends IExcitable<number> {
|
||||
* @param postTime 计划执行后的等待时长,等待这么长时间之后计划才真正结束
|
||||
*/
|
||||
planEnd(preTime?: number, postTime?: number): number;
|
||||
|
||||
/**
|
||||
* 销毁此动画对象
|
||||
*/
|
||||
destroy(): void;
|
||||
}
|
||||
|
||||
export interface ITransition extends IExcitable<number> {
|
||||
/** 渐变对象绑定的激励源 */
|
||||
readonly excitation: IExcitation<number> | null;
|
||||
|
||||
/**
|
||||
* 在动画执行器上绑定激励源
|
||||
* @param excitation 绑定的激励源
|
||||
@@ -334,7 +379,7 @@ export interface ITransition extends IExcitable<number> {
|
||||
* 绑定渐变对象,之后的渐变操作都会作用在此对象上
|
||||
* @param animatable 渐变对象
|
||||
*/
|
||||
transite(animatable: IAnimatable): this;
|
||||
transition(animatable: IAnimatable): this;
|
||||
|
||||
/**
|
||||
* 将当前绑定的值立刻缓慢渐变至目标值
|
||||
@@ -351,7 +396,14 @@ export interface ITransition extends IExcitable<number> {
|
||||
wait(animatable: IAnimatable): Promise<void>;
|
||||
|
||||
/**
|
||||
* 释放当前绑定的渐变对象,防止绑定的渐变对象一直不会被垃圾回收
|
||||
* 释放当前使用 {@link transition} 绑定的渐变对象,防止当前被绑定的渐变对象一直不会被垃圾回收。
|
||||
*/
|
||||
revoke(): void;
|
||||
|
||||
/**
|
||||
* 销毁当前渐变执行器
|
||||
*/
|
||||
destroy(): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -250,15 +250,15 @@ export function stackCurve(curves: ExcitationCurve[]): GeneralExcitationCurve {
|
||||
/**
|
||||
* 对速率曲线归一化,此函数假设传入的曲线单调,会使用 `curve(0)` 和 `curve(1)` 作为最大值或最小值。
|
||||
*
|
||||
* - `f(0) > f(1)`: `g(x) = (f(x) - f(0)) / (f(0) - f(1))`
|
||||
* - `f(0) < f(1)`: `g(x) = (f(x) - f(1)) / (f(1) - f(0))`
|
||||
* - `f(0) > f(1)`: `g(x) = (f(x) - f(1)) / (f(0) - f(1))`
|
||||
* - `f(0) < f(1)`: `g(x) = (f(x) - f(0)) / (f(1) - f(0))`
|
||||
* - `f(0) = f(1)`: `g(x) = f(x)`
|
||||
* @param curve 需要归一化的曲线
|
||||
* @returns
|
||||
*/
|
||||
export function normalize(curve: ExcitationCurve): ExcitationCurve {
|
||||
const head = curve(1);
|
||||
const tail = curve(0);
|
||||
const head = curve(0);
|
||||
const tail = curve(1);
|
||||
if (head > tail) {
|
||||
const diff = head - tail;
|
||||
return p => (curve(p) - tail) / diff;
|
||||
|
||||
Reference in new issue
Block a user