Optimizing behavioral models

This commit is contained in:
MrKBear 2022-03-25 17:06:39 +08:00
parent feb277c3a2
commit 850eea254c
2 changed files with 40 additions and 35 deletions

View File

@ -6,12 +6,6 @@ import type { Model } from "./Model";
import type { Range } from "./Range"; import type { Range } from "./Range";
import type { Label } from "./Label"; import type { Label } from "./Label";
/**
*
*/
type IBehaviorConstructor<B extends Behavior<any, any>> =
new (id: string, parameter: IBehaviorParameterValue<B["parameterOption"]>) => B;
/** /**
* *
*/ */
@ -43,8 +37,6 @@ type IObjectType = keyof IMapObjectParamTypeKeyToType;
type IVectorType = keyof IMapVectorParamTypeKeyToType; type IVectorType = keyof IMapVectorParamTypeKeyToType;
type IParamValue<K extends IParamType> = AllMapType[K]; type IParamValue<K extends IParamType> = AllMapType[K];
/** /**
* *
*/ */
@ -111,25 +103,34 @@ interface IBehaviorParameterOptionItem<T extends IParamType = IParamType> {
iconName?: string; iconName?: string;
} }
/** interface IBehaviorParameter {
* [x: string]: IParamType;
*/ }
type IBehaviorParameterValueItem<P extends IBehaviorParameterOptionItem> = IParamValue<P["type"]>;
/** /**
* *
*/ */
interface IBehaviorParameterOption { type IBehaviorParameterOption<P extends IBehaviorParameter> = {
[x: string]: IBehaviorParameterOptionItem; [X in keyof P]: IBehaviorParameterOptionItem<P[X]>;
} }
/** /**
* *
*/ */
type IBehaviorParameterValue<P extends IBehaviorParameterOption> = { type IBehaviorParameterValue<P extends IBehaviorParameter> = {
[x in keyof P]: IBehaviorParameterValueItem<P[x]> [X in keyof P]: IParamValue<P[X]>
} }
/**
*
*/
type IBehaviorConstructor<
P extends IBehaviorParameter = {},
E extends Record<EventType, any> = {}
> = new (id: string, parameter: IBehaviorParameterValue<P>) => Behavior<P, E>;
type IAnyBehavior = Behavior<any, any>;
type IAnyBehaviorRecorder = BehaviorRecorder<any, any>;
/** /**
* *
@ -158,8 +159,9 @@ class BehaviorInfo<E extends Record<EventType, any> = {}> extends Emitter<E> {
} }
class BehaviorRecorder< class BehaviorRecorder<
B extends Behavior<any, any> P extends IBehaviorParameter = {},
> extends BehaviorInfo { E extends Record<EventType, any> = {}
> extends BehaviorInfo<{}> {
/** /**
* *
@ -176,23 +178,23 @@ class BehaviorRecorder<
/** /**
* *
*/ */
public behavior: IBehaviorConstructor<B>; public behavior: IBehaviorConstructor<P, E>;
/** /**
* *
*/ */
public behaviorInstance: B; public behaviorInstance: Behavior<P, E>;
/** /**
* *
*/ */
public parameterOption: B["parameterOption"]; public parameterOption: IBehaviorParameterOption<P>;
/** /**
* *
*/ */
public getDefaultValue(): IBehaviorParameterValue<B["parameterOption"]> { public getDefaultValue(): IBehaviorParameterValue<P> {
let defaultObj = {} as IBehaviorParameterValue<B["parameterOption"]>; let defaultObj = {} as IBehaviorParameterValue<P>;
for (let key in this.parameterOption) { for (let key in this.parameterOption) {
let defaultVal = this.parameterOption[key].defaultValue; let defaultVal = this.parameterOption[key].defaultValue;
@ -224,11 +226,11 @@ class BehaviorRecorder<
/** /**
* *
*/ */
public new(): B { public new(): Behavior<P, E> {
return new this.behavior(this.getNextId(), this.getDefaultValue()); return new this.behavior(this.getNextId(), this.getDefaultValue());
} }
public constructor(behavior: IBehaviorConstructor<B>) { public constructor(behavior: IBehaviorConstructor<P, E>) {
super(); super();
this.behavior = behavior; this.behavior = behavior;
this.behaviorInstance = new this.behavior(this.getNextId(), {} as any); this.behaviorInstance = new this.behavior(this.getNextId(), {} as any);
@ -244,7 +246,7 @@ class BehaviorRecorder<
* *
*/ */
class Behavior< class Behavior<
P extends IBehaviorParameterOption = {}, P extends IBehaviorParameter = {},
E extends Record<EventType, any> = {} E extends Record<EventType, any> = {}
> extends BehaviorInfo<E> { > extends BehaviorInfo<E> {
@ -272,7 +274,7 @@ class Behavior<
/** /**
* *
*/ */
public parameterOption: P = {} as any; public parameterOption: IBehaviorParameterOption<P> = {} as any;
public constructor(id: string, parameter: IBehaviorParameterValue<P>) { public constructor(id: string, parameter: IBehaviorParameterValue<P>) {
super(); super();
@ -355,5 +357,8 @@ class Behavior<
} }
export { Behavior, BehaviorRecorder }; export {
Behavior, BehaviorRecorder, IBehaviorParameterOption, IBehaviorParameterOptionItem,
IAnyBehavior, IAnyBehaviorRecorder
};
export default { Behavior }; export default { Behavior };

View File

@ -5,14 +5,14 @@ import { Emitter, EventType, EventMixin } from "./Emitter";
import { CtrlObject } from "./CtrlObject"; import { CtrlObject } from "./CtrlObject";
import { ObjectID, AbstractRenderer } from "./Renderer"; import { ObjectID, AbstractRenderer } from "./Renderer";
import { Label } from "./Label"; import { Label } from "./Label";
import { Behavior, BehaviorRecorder } from "./Behavior"; import { Behavior, IAnyBehavior, IAnyBehaviorRecorder } from "./Behavior";
type ModelEvent = { type ModelEvent = {
loop: number; loop: number;
labelChange: Label[]; labelChange: Label[];
objectChange: CtrlObject[]; objectChange: CtrlObject[];
individualChange: Group; individualChange: Group;
behaviorChange: Behavior; behaviorChange: IAnyBehavior;
}; };
/** /**
@ -182,12 +182,12 @@ class Model extends Emitter<ModelEvent> {
/** /**
* *
*/ */
public behaviorPool: Behavior<any, any>[] = []; public behaviorPool: IAnyBehavior[] = [];
/** /**
* *
*/ */
public addBehavior<B extends Behavior<any, any>>(recorder: BehaviorRecorder<B>): B { public addBehavior<B extends IAnyBehaviorRecorder>(recorder: B): B["behaviorInstance"] {
let behavior = recorder.new(); let behavior = recorder.new();
behavior.load(this); behavior.load(this);
this.behaviorPool.push(behavior); this.behaviorPool.push(behavior);
@ -199,7 +199,7 @@ class Model extends Emitter<ModelEvent> {
/** /**
* ID * ID
*/ */
public getBehaviorById(id: ObjectID): Behavior<any, any> | undefined { public getBehaviorById(id: ObjectID): IAnyBehavior | undefined {
for (let i = 0; i < this.behaviorPool.length; i++) { for (let i = 0; i < this.behaviorPool.length; i++) {
if (this.behaviorPool[i].id.toString() === id.toString()) { if (this.behaviorPool[i].id.toString() === id.toString()) {
return this.behaviorPool[i]; return this.behaviorPool[i];
@ -211,8 +211,8 @@ class Model extends Emitter<ModelEvent> {
* Behavior * Behavior
* @param name * @param name
*/ */
public deleteBehavior(name: Behavior<any, any> | ObjectID) { public deleteBehavior(name: IAnyBehavior | ObjectID) {
let deletedBehavior: Behavior<any, any> | undefined; let deletedBehavior: IAnyBehavior | undefined;
let index = 0; let index = 0;
for (let i = 0; i < this.behaviorPool.length; i++) { for (let i = 0; i < this.behaviorPool.length; i++) {