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 { 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 IParamValue<K extends IParamType> = AllMapType[K];
/**
*
*/
@ -111,25 +103,34 @@ interface IBehaviorParameterOptionItem<T extends IParamType = IParamType> {
iconName?: string;
}
/**
*
*/
type IBehaviorParameterValueItem<P extends IBehaviorParameterOptionItem> = IParamValue<P["type"]>;
interface IBehaviorParameter {
[x: string]: IParamType;
}
/**
*
*/
interface IBehaviorParameterOption {
[x: string]: IBehaviorParameterOptionItem;
type IBehaviorParameterOption<P extends IBehaviorParameter> = {
[X in keyof P]: IBehaviorParameterOptionItem<P[X]>;
}
/**
*
*/
type IBehaviorParameterValue<P extends IBehaviorParameterOption> = {
[x in keyof P]: IBehaviorParameterValueItem<P[x]>
type IBehaviorParameterValue<P extends IBehaviorParameter> = {
[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<
B extends Behavior<any, any>
> extends BehaviorInfo {
P extends IBehaviorParameter = {},
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"]> {
let defaultObj = {} as IBehaviorParameterValue<B["parameterOption"]>;
public getDefaultValue(): IBehaviorParameterValue<P> {
let defaultObj = {} as IBehaviorParameterValue<P>;
for (let key in this.parameterOption) {
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());
}
public constructor(behavior: IBehaviorConstructor<B>) {
public constructor(behavior: IBehaviorConstructor<P, E>) {
super();
this.behavior = behavior;
this.behaviorInstance = new this.behavior(this.getNextId(), {} as any);
@ -244,7 +246,7 @@ class BehaviorRecorder<
*
*/
class Behavior<
P extends IBehaviorParameterOption = {},
P extends IBehaviorParameter = {},
E extends Record<EventType, any> = {}
> 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>) {
super();
@ -355,5 +357,8 @@ class Behavior<
}
export { Behavior, BehaviorRecorder };
export {
Behavior, BehaviorRecorder, IBehaviorParameterOption, IBehaviorParameterOptionItem,
IAnyBehavior, IAnyBehaviorRecorder
};
export default { Behavior };

View File

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