Add label archive function

This commit is contained in:
MrKBear 2022-04-22 23:48:10 +08:00
parent 276c8c63a1
commit 068acb19a8
6 changed files with 85 additions and 20 deletions

View File

@ -33,7 +33,13 @@ class CommandBar extends Component<ICommandBarProps & IMixinSettingProps & IMixi
}} }}
> >
<div> <div>
{this.getRenderButton({ iconName: "Save", i18NKey: "Command.Bar.Save.Info" })} {this.getRenderButton({
iconName: "Save",
i18NKey: "Command.Bar.Save.Info",
click: () => {
this.props.status?.archive.save(this.props.status.model);
}
})}
{this.getRenderButton({ {this.getRenderButton({
iconName: this.props.status?.actuator.start() ? "Pause" : "Play", iconName: this.props.status?.actuator.start() ? "Pause" : "Play",
i18NKey: "Command.Bar.Play.Info", i18NKey: "Command.Bar.Play.Info",

View File

@ -1,10 +1,18 @@
import { Emitter, EventType } from "@Model/Emitter"; import { Emitter, EventType } from "@Model/Emitter";
import { Model } from "./Model"; import { IArchiveCtrlObject } from "@Model/CtrlObject";
import { Model } from "@Model/Model";
import { IArchiveLabel } from "@Model/Label";
interface IArchiveEvent { interface IArchiveEvent {
fileChange: Archive; fileChange: Archive;
} }
interface IArchiveObject {
nextIndividualId: number;
objectPool: IArchiveCtrlObject[];
labelPool: IArchiveLabel[];
}
class Archive< class Archive<
M extends any = any, M extends any = any,
E extends Record<EventType, any> = {} E extends Record<EventType, any> = {}
@ -35,17 +43,32 @@ class Archive<
* *
*/ */
public save(model: Model): string { public save(model: Model): string {
let fileData: Record<string, any> = {};
// 保存对象
fileData.objects = [];
// 记录
model.objectPool.map((object) => {
// 存贮 CtrlObject
const objectPool: IArchiveCtrlObject[] = [];
model.objectPool.forEach(obj => {
objectPool.push(obj.toArchive());
}) })
return JSON.stringify(model); // 存储 Label
const labelPool: IArchiveLabel[] = [];
model.labelPool.forEach(obj => {
labelPool.push(obj.toArchive());
})
const fileData: IArchiveObject = {
nextIndividualId: model.nextIndividualId,
objectPool: objectPool,
labelPool: labelPool
};
console.log(fileData);
console.log({value: JSON.stringify(fileData)});
this.isSaved = true;
this.emit( ...["fileChange", this] as any );
return "";
} }
/** /**

View File

@ -1,8 +1,11 @@
import { LabelObject } from "@Model/Label" import { LabelObject } from "@Model/Label"
import { v4 as uuid } from "uuid"; import { v4 as uuid } from "uuid";
import { parameter2ArchiveObject, archiveObject2Parameter, IArchiveParseFn } from "@Model/Parameter";
import type { IAnyObject, Model } from "@Model/Model"; import type { IAnyObject, Model } from "@Model/Model";
import type { ObjectID } from "@Model/Model"; import type { ObjectID } from "@Model/Model";
import {
parameter2ArchiveObject, archiveObject2Parameter,
IArchiveParseFn, IObjectParamArchiveType, object2ArchiveObject
} from "@Model/Parameter";
interface IArchiveCtrlObject { interface IArchiveCtrlObject {
displayName: CtrlObject["displayName"]; displayName: CtrlObject["displayName"];
@ -12,6 +15,7 @@ interface IArchiveCtrlObject {
id: string; id: string;
renderParameter: any; renderParameter: any;
deleteFlag: CtrlObject["deleteFlag"]; deleteFlag: CtrlObject["deleteFlag"];
labels: IObjectParamArchiveType[];
} }
/** /**
@ -117,20 +121,26 @@ class CtrlObject<A extends IAnyObject = IAnyObject> extends LabelObject {
update: !!this.update, update: !!this.update,
id: this.id, id: this.id,
renderParameter: parameter2ArchiveObject(this.renderParameter), renderParameter: parameter2ArchiveObject(this.renderParameter),
deleteFlag: !!this.deleteFlag deleteFlag: !!this.deleteFlag,
labels: this.labels.map((label) => {
return object2ArchiveObject(label);
})
} as any; } as any;
} }
public fromArchive(archive: IArchiveCtrlObject & A, paster?: IArchiveParseFn): void { public fromArchive(archive: IArchiveCtrlObject & A, paster: IArchiveParseFn): void {
this.displayName = archive.displayName; this.displayName = archive.displayName;
this.color = archive.color.concat([]); this.color = archive.color.concat([]);
this.display = !!archive.display; this.display = !!archive.display;
this.update = !!archive.update; this.update = !!archive.update;
this.id = archive.id; this.id = archive.id;
this.renderParameter = archiveObject2Parameter(
archive.renderParameter, paster ?? (() => undefined)
);
this.deleteFlag = !!archive.deleteFlag; this.deleteFlag = !!archive.deleteFlag;
this.renderParameter = archiveObject2Parameter(
archive.renderParameter, paster
);
this.labels = archive.labels.map((obj) => {
return paster(obj) as any;
}).filter((c) => !!c);
} }
} }

View File

@ -443,7 +443,7 @@ class Group extends CtrlObject<IArchiveGroup> {
this.killCount = archive.killCount, this.killCount = archive.killCount,
this.behaviors = archive.behaviors.map((item) => { this.behaviors = archive.behaviors.map((item) => {
return item ? paster(item) as any : undefined; return item ? paster(item) as any : undefined;
}); }).filter(c => !!c);
} }
public constructor(model: Model) { public constructor(model: Model) {

View File

@ -1,6 +1,14 @@
import type { Model, ObjectID } from "@Model/Model"; import type { Model, ObjectID } from "@Model/Model";
import { v4 as uuid } from "uuid"; import { v4 as uuid } from "uuid";
interface IArchiveLabel {
isBuildIn: boolean;
id: string;
name: string;
color: number[];
deleteFlag: boolean;
}
/** /**
* *
*/ */
@ -83,6 +91,24 @@ class Label {
this.isBuildIn = true; this.isBuildIn = true;
return this; return this;
} }
public toArchive(): IArchiveLabel {
return {
isBuildIn: this.isBuildIn,
id: this.id,
name: this.name,
color: this.color.concat([]),
deleteFlag: this.deleteFlag
} as any;
}
public fromArchive(archive: IArchiveLabel): void {
this.isBuildIn = archive.isBuildIn,
this.id = archive.id,
this.name = archive.name,
this.color = archive.color.concat([]),
this.deleteFlag = archive.deleteFlag
}
} }
/** /**
@ -93,7 +119,7 @@ class LabelObject {
/** /**
* *
*/ */
private labels: Label[] = []; public labels: Label[] = [];
/** /**
* Label * Label
@ -133,4 +159,4 @@ class LabelObject {
} }
} }
export { Label, LabelObject }; export { Label, LabelObject, IArchiveLabel };

View File

@ -39,7 +39,7 @@ class Range extends CtrlObject<IArchiveRange> {
}; };
} }
public override fromArchive(archive: IArchiveCtrlObject & IArchiveRange, paster?: IArchiveParseFn): void { public override fromArchive(archive: IArchiveCtrlObject & IArchiveRange, paster: IArchiveParseFn): void {
super.fromArchive(archive, paster); super.fromArchive(archive, paster);
this.position = archive.position.concat([]), this.position = archive.position.concat([]),
this.radius = archive.radius.concat([]) this.radius = archive.radius.concat([])