Add group ctrl method in model class

This commit is contained in:
2022-02-18 17:09:52 +08:00
parent e15cc0e410
commit c5014dc838
3 changed files with 81 additions and 9 deletions
+8 -1
View File
@@ -1,10 +1,16 @@
import { LabelObject } from "./Label"
import type { Model } from "./Model";
import type { ObjectID } from "./Renderer";
/**
* 可控对象
*/
class CtrlObject extends LabelObject {
/**
* 唯一标识符
*/
public id: ObjectID;
/**
* 控制模型
@@ -14,9 +20,10 @@ class CtrlObject extends LabelObject {
/**
* 构造器
*/
public constructor(model: Model) {
public constructor(model: Model, id: ObjectID) {
super();
this.model = model;
this.id = id;
}
}
+65 -2
View File
@@ -2,12 +2,73 @@
import { Individual } from "./Individual";
import { Group } from "./Group";
import { Emitter, EventType, EventMixin } from "./Emitter";
import { CtrlObject } from "./CtrlObject";
import { ObjectID } from "./Renderer";
type ModelEvent = {
addGroup: Group;
deleteGroup: Group[];
};
/**
* 模型 全局控制器
*/
class Model extends Emitter<{}> {
class Model extends Emitter<ModelEvent> {
/**
* 下一个需要分配的 ID
*/
private idIndex: number = 1;
public get nextId(): number {
return this.idIndex ++;
}
/**
* 对象列表
*/
public objectPool: CtrlObject[] = [];
/**
* 添加组
*/
public addGroup(): void {
console.log(`Model: Creat group with id ${this.idIndex}`);
let group = new Group(this, this.nextId);
this.objectPool.push(group);
this.emit("addGroup", group);
}
/**
* 删除组
*/
public deleteGroup(groups: Group[] | ObjectID[]): void {
let deletedGroups: Group[] = [];
this.objectPool = this.objectPool.filter((object) => {
if (!(object instanceof Group)) return true;
let deletedGroup: Group | undefined;
for (let i = 0; i < groups.length; i++) {
if (groups[i] instanceof Group) {
if (groups[i] === object) {
deletedGroup = object;
}
} else {
if (groups[i] === object.id) {
deletedGroup = object;
}
}
}
if (deletedGroup) {
deletedGroups.push(deletedGroup);
return false;
} else {
return true;
}
});
this.emit("deleteGroup", deletedGroups);
}
}
export {
@@ -16,5 +77,7 @@ export {
Emitter,
EventType,
EventMixin,
Model
Model,
CtrlObject,
ObjectID
}