Change object id into uuid

This commit is contained in:
2022-04-18 17:23:30 +08:00
parent a788968525
commit a2b1819dd8
12 changed files with 158 additions and 116 deletions
-3
View File
@@ -36,9 +36,6 @@ class Archive<
*/
public save(model: Model): string {
let fileData: Record<string, any> = {};
// 保存 Next ID
fileData.idIndex = model.idIndex;
// 保存对象
fileData.objects = [];
+6 -17
View File
@@ -1,4 +1,5 @@
import { Emitter, EventType } from "@Model/Emitter";
import { v4 as uuid } from "uuid";
import type { Individual } from "@Model/Individual";
import type { Group } from "@Model/Group";
import type { Model } from "@Model/Model";
@@ -10,7 +11,7 @@ import { getDefaultValue, IParameter, IParameterOption, IParameterValue } from "
type IBehaviorConstructor<
P extends IParameter = {},
E extends Record<EventType, any> = {}
> = new (id: string, parameter: IParameterValue<P>) => Behavior<P, E>;
> = new (parameter: IParameterValue<P>) => Behavior<P, E>;
type IAnyBehavior = Behavior<any, any>;
type IAnyBehaviorRecorder = BehaviorRecorder<any, any>;
@@ -76,18 +77,6 @@ class BehaviorRecorder<
E extends Record<EventType, any> = {}
> extends BehaviorInfo<{}> {
/**
* 命名序号
*/
public nameIndex: number = 0;
/**
* 获取下一个 ID
*/
public getNextId() {
return `B-${this.behaviorId}-${this.nameIndex ++}`;
}
/**
* 行为类型
*/
@@ -107,13 +96,13 @@ class BehaviorRecorder<
* 创建一个新的行为实例
*/
public new(): Behavior<P, E> {
return new this.behavior(this.getNextId(), getDefaultValue(this.parameterOption));
return new this.behavior(getDefaultValue(this.parameterOption));
}
public constructor(behavior: IBehaviorConstructor<P, E>) {
super();
this.behavior = behavior;
this.behaviorInstance = new this.behavior(this.getNextId(), {} as any);
this.behaviorInstance = new this.behavior({} as any);
this.parameterOption = this.behaviorInstance.parameterOption;
this.iconName = this.behaviorInstance.iconName;
this.behaviorId = this.behaviorInstance.behaviorId;
@@ -168,9 +157,9 @@ class Behavior<
*/
public parameterOption: IParameterOption<P> = {} as any;
public constructor(id: string, parameter: IParameterValue<P>) {
public constructor(parameter: IParameterValue<P>) {
super();
this.id = id;
this.id = uuid();
this.parameter = parameter;
}
+3 -2
View File
@@ -1,4 +1,5 @@
import { LabelObject } from "@Model/Label"
import { v4 as uuid } from "uuid";
import type { IAnyObject, Model } from "@Model/Model";
import type { ObjectID } from "@Model/Model";
@@ -45,10 +46,10 @@ class CtrlObject extends LabelObject {
/**
* 构造器
*/
public constructor(model: Model, id: ObjectID) {
public constructor(model: Model) {
super();
this.model = model;
this.id = id;
this.id = uuid();
}
/**
+2 -2
View File
@@ -407,9 +407,9 @@ class Group extends CtrlObject {
return dataBuffer;
}
public constructor(model: Model, id: ObjectID) {
public constructor(model: Model) {
super(model, id);
super(model);
if (model.renderer) {
this.renderParameter = getDefaultValue(model.renderer.pointsParameterOption);
+3 -2
View File
@@ -1,4 +1,5 @@
import type { Model, ObjectID } from "@Model/Model";
import { v4 as uuid } from "uuid";
/**
* 数据标签
@@ -35,9 +36,9 @@ class Label {
* @param id 标签 ID
* @param name 用户定义的名称
*/
public constructor(model: Model, id: ObjectID, name?: string) {
public constructor(model: Model, name?: string) {
this.model = model;
this.id = id;
this.id = uuid();
this.name = name ?? this.name;
}
+8 -27
View File
@@ -2,9 +2,8 @@ import { Label } from "@Model/Label";
import { Group } from "@Model/Group";
import { Range } from "@Model/Range";
import { IParamValue } from "@Model/Parameter";
import { Individual } from "@Model/Individual";
import { CtrlObject } from "@Model/CtrlObject";
import { Emitter, EventType, EventMixin } from "@Model/Emitter";
import { Emitter } from "@Model/Emitter";
import { AbstractRenderer } from "@Model/Renderer";
import { Behavior, IAnyBehavior, IAnyBehaviorRecorder } from "@Model/Behavior";
@@ -30,14 +29,6 @@ type ModelEvent = {
*/
class Model extends Emitter<ModelEvent> {
/**
* 下一个需要分配的 ID
*/
public idIndex: number = 1;
public nextId(label: string = "U"): string {
return `${label}-${this.idIndex ++}`;
}
/**
* 对象列表
*/
@@ -75,10 +66,10 @@ class Model extends Emitter<ModelEvent> {
* 添加标签
*/
public addLabel(name: string): Label {
console.log(`Model: Creat label with id ${this.idIndex}`);
let label = new Label(this, this.nextId("L"), name);
let label = new Label(this, name);
this.labelPool.push(label);
this.emit("labelChange", this.labelPool);
console.log(`Model: Creat label with id ${label.id}`);
return label;
}
@@ -140,10 +131,10 @@ class Model extends Emitter<ModelEvent> {
* 添加组
*/
public addGroup(): Group {
console.log(`Model: Creat group with id ${this.idIndex}`);
let group = new Group(this, this.nextId("G"));
let group = new Group(this);
this.objectPool.push(group);
this.emit("objectChange", this.objectPool);
console.log(`Model: Creat group with id ${group.id}`);
return group;
}
@@ -151,10 +142,10 @@ class Model extends Emitter<ModelEvent> {
* 添加范围
*/
public addRange(): Range {
console.log(`Model: Creat range with id ${this.idIndex}`);
let range = new Range(this, this.nextId("R"));
let range = new Range(this);
this.objectPool.push(range);
this.emit("objectChange", this.objectPool);
console.log(`Model: Creat range with id ${range.id}`);
return range;
}
@@ -393,14 +384,4 @@ class Model extends Emitter<ModelEvent> {
}
}
export {
Individual,
Group,
Emitter,
EventType,
EventMixin,
Model,
CtrlObject,
ObjectID,
IAnyObject
}
export { Model, ObjectID, IAnyObject }
+2 -2
View File
@@ -17,9 +17,9 @@ class Range extends CtrlObject {
*/
public radius: number[] = [1, 1, 1];
public constructor(model: Model, id: ObjectID) {
public constructor(model: Model) {
super(model, id);
super(model);
if (model.renderer) {
this.renderParameter = getDefaultValue(model.renderer.cubeParameterOption);