Add group individual label archive function #43
@ -1,10 +1,10 @@
|
||||
import { Individual } from "@Model/Individual";
|
||||
import { CtrlObject } from "@Model/CtrlObject";
|
||||
import { CtrlObject, IArchiveCtrlObject } from "@Model/CtrlObject";
|
||||
import type { Behavior, IAnyBehavior } from "@Model/Behavior";
|
||||
import { Label } from "@Model/Label";
|
||||
import { Range } from "@Model/Range";
|
||||
import { Model, ObjectID } from "@Model/Model";
|
||||
import { getDefaultValue, IObjectParamArchiveType } from "@Model/Parameter";
|
||||
import { getDefaultValue, IArchiveParseFn, IObjectParamArchiveType, object2ArchiveObject } from "@Model/Parameter";
|
||||
|
||||
enum GenMod {
|
||||
Point = "p",
|
||||
@ -12,7 +12,6 @@ enum GenMod {
|
||||
}
|
||||
|
||||
interface IArchiveGroup {
|
||||
individuals: IObjectParamArchiveType[];
|
||||
genMethod: Group["genMethod"];
|
||||
genPoint: Group["genPoint"];
|
||||
genRange: IObjectParamArchiveType | undefined;
|
||||
@ -418,6 +417,34 @@ class Group extends CtrlObject<IArchiveGroup> {
|
||||
});
|
||||
return dataBuffer;
|
||||
}
|
||||
|
||||
public override toArchive(): IArchiveCtrlObject & IArchiveGroup {
|
||||
return {
|
||||
...super.toArchive(),
|
||||
genMethod: this.genMethod,
|
||||
genPoint: this.genPoint.concat([]),
|
||||
genRange: object2ArchiveObject(this.genRange) as IObjectParamArchiveType,
|
||||
genCount: this.genCount,
|
||||
genErrorMessage: this.genErrorMessage,
|
||||
genErrorMessageShowCount: this.genErrorMessageShowCount,
|
||||
killCount: this.killCount,
|
||||
behaviors: object2ArchiveObject(this.behaviors) as IObjectParamArchiveType[]
|
||||
};
|
||||
}
|
||||
|
||||
public override fromArchive(archive: IArchiveCtrlObject & IArchiveGroup, paster: IArchiveParseFn): void {
|
||||
super.fromArchive(archive, paster);
|
||||
this.genMethod = archive.genMethod,
|
||||
this.genPoint = archive.genPoint.concat([]),
|
||||
this.genRange = archive.genRange ? paster(archive.genRange) as any : undefined,
|
||||
this.genCount = archive.genCount,
|
||||
this.genErrorMessage = archive.genErrorMessage,
|
||||
this.genErrorMessageShowCount = archive.genErrorMessageShowCount,
|
||||
this.killCount = archive.killCount,
|
||||
this.behaviors = archive.behaviors.map((item) => {
|
||||
return item ? paster(item) as any : undefined;
|
||||
});
|
||||
}
|
||||
|
||||
public constructor(model: Model) {
|
||||
|
||||
|
@ -1,5 +1,15 @@
|
||||
import type { Group } from "@Model/Group";
|
||||
import { ObjectID } from "@Model/Model";
|
||||
import { IAnyObject, ObjectID } from "@Model/Model";
|
||||
import { IArchiveParseFn, object2ArchiveObject, isArchiveObjectType } from "@Model/Parameter";
|
||||
|
||||
interface IArchiveIndividual {
|
||||
id: string;
|
||||
position: number[];
|
||||
velocity: number[];
|
||||
acceleration: number[];
|
||||
force: number[];
|
||||
metaData: IAnyObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 群中的个体类型
|
||||
@ -170,6 +180,67 @@ class Individual {
|
||||
this.metaData.set(key, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public toArchive(): IArchiveIndividual {
|
||||
|
||||
const metaDataArchive = {} as IAnyObject;
|
||||
this.metaData.forEach((value, key) => {
|
||||
|
||||
// 处理内置对象
|
||||
let ltObject = object2ArchiveObject(value);
|
||||
if (ltObject) {
|
||||
metaDataArchive[key] = ltObject;
|
||||
}
|
||||
|
||||
// 处理数组
|
||||
else if (Array.isArray(value)) {
|
||||
metaDataArchive[key] = value.concat([]);
|
||||
}
|
||||
|
||||
// 处理值
|
||||
else {
|
||||
metaDataArchive[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
id: this.id,
|
||||
position: this.position.concat([]),
|
||||
velocity: this.velocity.concat([]),
|
||||
acceleration: this.acceleration.concat([]),
|
||||
force: this.force.concat([]),
|
||||
metaData: metaDataArchive
|
||||
};
|
||||
}
|
||||
|
||||
public fromArchive(archive: IArchiveIndividual, paster: IArchiveParseFn): void {
|
||||
|
||||
const metaData = new Map() as Map<ObjectID, any>;
|
||||
for (const key in archive.metaData) {
|
||||
|
||||
const value = archive.metaData[key];
|
||||
|
||||
// 处理内置对象
|
||||
if (value instanceof Object && isArchiveObjectType(value)) {
|
||||
metaData.set(key, paster(value));
|
||||
}
|
||||
|
||||
else if (Array.isArray(value)) {
|
||||
metaData.set(key, value.concat([]));
|
||||
}
|
||||
|
||||
else {
|
||||
metaData.set(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
this.id = this.id,
|
||||
this.position = this.position.concat([]),
|
||||
this.velocity = this.velocity.concat([]),
|
||||
this.acceleration = this.acceleration.concat([]),
|
||||
this.force = this.force.concat([]),
|
||||
this.metaData = metaData;
|
||||
}
|
||||
}
|
||||
|
||||
export { Individual };
|
@ -1,7 +1,7 @@
|
||||
import { Group } from "@Model/Group";
|
||||
import { Range } from "@Model/Range";
|
||||
import { Label } from "@Model/Label";
|
||||
import { Behavior } from "@Model/Behavior";
|
||||
import { Behavior, IAnyBehavior } from "@Model/Behavior";
|
||||
import { Individual } from "@Model/Individual";
|
||||
|
||||
type IObjectParamArchiveType = {
|
||||
@ -225,7 +225,7 @@ function getDefaultValue<P extends IParameter> (option: IParameterOption<P>): IP
|
||||
return defaultObj;
|
||||
}
|
||||
|
||||
type IRealObjectType = Range | Group | Label | Behavior;
|
||||
type IRealObjectType = Range | Group | Label | IAnyBehavior;
|
||||
type IArchiveParseFn = (archive: IObjectParamArchiveType) => IRealObjectType | undefined;
|
||||
|
||||
function object2ArchiveObject(object: IRealObjectType | IRealObjectType[] | any, testArray: boolean = true):
|
||||
@ -358,5 +358,5 @@ export {
|
||||
IParamType, IParamValue, isObjectType, isVectorType, getDefaultValue,
|
||||
IParameterOptionItem, IParameter, IParameterOption, IParameterValue,
|
||||
object2ArchiveObject, parameter2ArchiveObject, archiveObject2Parameter,
|
||||
IArchiveParseFn, IObjectParamArchiveType
|
||||
IArchiveParseFn, IObjectParamArchiveType, isArchiveObjectType
|
||||
}
|
Loading…
Reference in New Issue
Block a user