diff --git a/source/Model/Archive.ts b/source/Model/Archive.ts index 8f9e2d6..e446953 100644 --- a/source/Model/Archive.ts +++ b/source/Model/Archive.ts @@ -2,6 +2,9 @@ import { Emitter, EventType } from "@Model/Emitter"; import { IArchiveCtrlObject } from "@Model/CtrlObject"; import { Model } from "@Model/Model"; import { IArchiveLabel } from "@Model/Label"; +import { Group, IArchiveGroup } from "@Model/Group"; +import { IArchiveIndividual } from "@Model/Individual"; +import { IArchiveBehavior } from "@Model/Behavior"; interface IArchiveEvent { fileChange: Archive; @@ -11,6 +14,7 @@ interface IArchiveObject { nextIndividualId: number; objectPool: IArchiveCtrlObject[]; labelPool: IArchiveLabel[]; + behaviorPool: IArchiveBehavior[]; } class Archive< @@ -47,19 +51,42 @@ class Archive< // 存贮 CtrlObject const objectPool: IArchiveCtrlObject[] = []; model.objectPool.forEach(obj => { - objectPool.push(obj.toArchive()); + let archiveObject = obj.toArchive(); + + // 处理每个群的个体 + if (archiveObject.objectType === "G") { + const archiveGroup: IArchiveGroup = archiveObject as any; + const group: Group = obj as Group; + + const individuals: IArchiveIndividual[] = []; + group.individuals.forEach((item) => { + individuals.push(item.toArchive()); + }); + + archiveGroup.individuals = individuals; + } + + objectPool.push(archiveObject); }) // 存储 Label const labelPool: IArchiveLabel[] = []; model.labelPool.forEach(obj => { labelPool.push(obj.toArchive()); - }) + }); + // 存储全部行为 + const behaviorPool: IArchiveBehavior[] = []; + model.behaviorPool.forEach(obj => { + behaviorPool.push(obj.toArchive()); + }); + + // 生成存档对象 const fileData: IArchiveObject = { nextIndividualId: model.nextIndividualId, objectPool: objectPool, - labelPool: labelPool + labelPool: labelPool, + behaviorPool: behaviorPool }; console.log(fileData); diff --git a/source/Model/Behavior.ts b/source/Model/Behavior.ts index 7db5f66..ffdd9b4 100644 --- a/source/Model/Behavior.ts +++ b/source/Model/Behavior.ts @@ -3,7 +3,11 @@ import { v4 as uuid } from "uuid"; import type { Individual } from "@Model/Individual"; import type { Group } from "@Model/Group"; import type { Model } from "@Model/Model"; -import { getDefaultValue, IParameter, IParameterOption, IParameterValue } from "@Model/Parameter"; +import { + archiveObject2Parameter, + getDefaultValue, IArchiveParameterValue, IArchiveParseFn, + IParameter, IParameterOption, IParameterValue, parameter2ArchiveObject +} from "@Model/Parameter"; /** * 行为构造函数类型 @@ -113,6 +117,17 @@ class BehaviorRecorder< } } +interface IArchiveBehavior { + behaviorId: string; + name: string; + id: string; + color: number[]; + priority: number; + currentGroupKey: string[]; + deleteFlag: boolean; + parameter: IArchiveParameterValue; +} + /** * 群体的某种行为 */ @@ -189,6 +204,33 @@ class Behavior< return this.deleteFlag; } + public toArchive(): IArchiveBehavior { + return { + behaviorId: this.behaviorId, + name: this.name, + id: this.id, + color: this.color.concat([]), + priority: this.priority, + currentGroupKey: this.currentGroupKey.concat([]) as any, + deleteFlag: this.deleteFlag, + parameter: parameter2ArchiveObject( + this.parameter, this.parameterOption + ) + }; + } + + public fromArchive(archive: IArchiveBehavior, paster: IArchiveParseFn): void { + this.name = this.name, + this.id = this.id, + this.color = this.color.concat([]), + this.priority = this.priority, + this.currentGroupKey = this.currentGroupKey.concat([]) as any, + this.deleteFlag = this.deleteFlag, + this.parameter = archiveObject2Parameter( + archive.parameter, paster + ) as any; + } + /** * 加载时调用 */ @@ -241,6 +283,6 @@ class Behavior< type IRenderBehavior = BehaviorInfo | Behavior; export { - Behavior, BehaviorRecorder, IAnyBehavior, IAnyBehaviorRecorder, BehaviorInfo, IRenderBehavior -}; -export default { Behavior }; \ No newline at end of file + Behavior, BehaviorRecorder, IAnyBehavior, IAnyBehaviorRecorder, + BehaviorInfo, IRenderBehavior, IArchiveBehavior +}; \ No newline at end of file diff --git a/source/Model/CtrlObject.ts b/source/Model/CtrlObject.ts index 21a6f31..a0495dc 100644 --- a/source/Model/CtrlObject.ts +++ b/source/Model/CtrlObject.ts @@ -4,16 +4,18 @@ import type { IAnyObject, Model } from "@Model/Model"; import type { ObjectID } from "@Model/Model"; import { parameter2ArchiveObject, archiveObject2Parameter, - IArchiveParseFn, IObjectParamArchiveType, object2ArchiveObject + IArchiveParseFn, IObjectParamArchiveType, object2ArchiveObject, + IArchiveParameterValue, IParameter } from "@Model/Parameter"; interface IArchiveCtrlObject { + objectType: "R" | "G"; displayName: CtrlObject["displayName"]; color: CtrlObject["color"]; display: CtrlObject["display"]; update: CtrlObject["update"]; id: string; - renderParameter: any; + renderParameter: IArchiveParameterValue; deleteFlag: CtrlObject["deleteFlag"]; labels: IObjectParamArchiveType[]; } diff --git a/source/Model/Group.ts b/source/Model/Group.ts index bb3a970..fb3184e 100644 --- a/source/Model/Group.ts +++ b/source/Model/Group.ts @@ -4,7 +4,11 @@ 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, IArchiveParseFn, IObjectParamArchiveType, object2ArchiveObject } from "@Model/Parameter"; +import { IArchiveIndividual } from "@Model/Individual"; +import { + getDefaultValue, IArchiveParseFn, + IObjectParamArchiveType, object2ArchiveObject +} from "@Model/Parameter"; enum GenMod { Point = "p", @@ -12,6 +16,7 @@ enum GenMod { } interface IArchiveGroup { + individuals: IArchiveIndividual[]; genMethod: Group["genMethod"]; genPoint: Group["genPoint"]; genRange: IObjectParamArchiveType | undefined; @@ -421,6 +426,7 @@ class Group extends CtrlObject { public override toArchive(): IArchiveCtrlObject & IArchiveGroup { return { ...super.toArchive(), + objectType: "G", genMethod: this.genMethod, genPoint: this.genPoint.concat([]), genRange: object2ArchiveObject(this.genRange) as IObjectParamArchiveType, @@ -456,4 +462,4 @@ class Group extends CtrlObject { } } -export { Group, GenMod }; \ No newline at end of file +export { Group, GenMod, IArchiveGroup }; \ No newline at end of file diff --git a/source/Model/Individual.ts b/source/Model/Individual.ts index 96637fd..27fcb9c 100644 --- a/source/Model/Individual.ts +++ b/source/Model/Individual.ts @@ -243,4 +243,4 @@ class Individual { } } -export { Individual }; \ No newline at end of file +export { Individual, IArchiveIndividual }; \ No newline at end of file diff --git a/source/Model/Parameter.ts b/source/Model/Parameter.ts index 82aba18..6e4dc33 100644 --- a/source/Model/Parameter.ts +++ b/source/Model/Parameter.ts @@ -358,5 +358,6 @@ export { IParamType, IParamValue, isObjectType, isVectorType, getDefaultValue, IParameterOptionItem, IParameter, IParameterOption, IParameterValue, object2ArchiveObject, parameter2ArchiveObject, archiveObject2Parameter, - IArchiveParseFn, IObjectParamArchiveType, isArchiveObjectType + IArchiveParseFn, IObjectParamArchiveType, isArchiveObjectType, + IArchiveParameterValue } \ No newline at end of file diff --git a/source/Model/Range.ts b/source/Model/Range.ts index 7c0b39b..fece5d7 100644 --- a/source/Model/Range.ts +++ b/source/Model/Range.ts @@ -34,6 +34,7 @@ class Range extends CtrlObject { public override toArchive(): IArchiveCtrlObject & IArchiveRange { return { ...super.toArchive(), + objectType: "R", position: this.position.concat([]), radius: this.radius.concat([]) };