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>
{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({
iconName: this.props.status?.actuator.start() ? "Pause" : "Play",
i18NKey: "Command.Bar.Play.Info",

View File

@ -1,10 +1,18 @@
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 {
fileChange: Archive;
}
interface IArchiveObject {
nextIndividualId: number;
objectPool: IArchiveCtrlObject[];
labelPool: IArchiveLabel[];
}
class Archive<
M extends any = any,
E extends Record<EventType, any> = {}
@ -35,17 +43,32 @@ class Archive<
*
*/
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 { v4 as uuid } from "uuid";
import { parameter2ArchiveObject, archiveObject2Parameter, IArchiveParseFn } from "@Model/Parameter";
import type { IAnyObject, Model } from "@Model/Model";
import type { ObjectID } from "@Model/Model";
import {
parameter2ArchiveObject, archiveObject2Parameter,
IArchiveParseFn, IObjectParamArchiveType, object2ArchiveObject
} from "@Model/Parameter";
interface IArchiveCtrlObject {
displayName: CtrlObject["displayName"];
@ -12,6 +15,7 @@ interface IArchiveCtrlObject {
id: string;
renderParameter: any;
deleteFlag: CtrlObject["deleteFlag"];
labels: IObjectParamArchiveType[];
}
/**
@ -117,20 +121,26 @@ class CtrlObject<A extends IAnyObject = IAnyObject> extends LabelObject {
update: !!this.update,
id: this.id,
renderParameter: parameter2ArchiveObject(this.renderParameter),
deleteFlag: !!this.deleteFlag
deleteFlag: !!this.deleteFlag,
labels: this.labels.map((label) => {
return object2ArchiveObject(label);
})
} as any;
}
public fromArchive(archive: IArchiveCtrlObject & A, paster?: IArchiveParseFn): void {
public fromArchive(archive: IArchiveCtrlObject & A, paster: IArchiveParseFn): void {
this.displayName = archive.displayName;
this.color = archive.color.concat([]);
this.display = !!archive.display;
this.update = !!archive.update;
this.id = archive.id;
this.renderParameter = archiveObject2Parameter(
archive.renderParameter, paster ?? (() => undefined)
);
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.behaviors = archive.behaviors.map((item) => {
return item ? paster(item) as any : undefined;
});
}).filter(c => !!c);
}
public constructor(model: Model) {

View File

@ -1,6 +1,14 @@
import type { Model, ObjectID } from "@Model/Model";
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;
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
@ -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);
this.position = archive.position.concat([]),
this.radius = archive.radius.concat([])