Skip to content

Commit

Permalink
🦄 refactor: refactor Model
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Apr 8, 2024
1 parent cfb03f8 commit 72c74f3
Show file tree
Hide file tree
Showing 62 changed files with 478 additions and 429 deletions.
2 changes: 1 addition & 1 deletion packages/chili-core/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import { ICommand } from "./command";
import { IDocument } from "./document";
import { IStorage, ObservableCollection } from "./foundation";
import { IShapeFactory } from "./geometry";
import { Serialized } from "./serialize";
import { IService } from "./service";
import { IShapeFactory } from "./shape";
import { IWindow } from "./ui/window";
import { IView, IVisualFactory } from "./visual";

Expand Down
7 changes: 4 additions & 3 deletions packages/chili-core/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default {
display: "English",
code: "en",
translation: {
"common.general": "General",
"common.color": "Color",
"common.opacity": "Opacity",
"common.name": "Name",
Expand Down Expand Up @@ -42,9 +43,9 @@ export default {
"material.texture": "Texture",
"material.repeatU": "U repeat",
"material.repeatV": "V repeat",
"model.translation": "Translation",
"model.rotation": "Rotation",
"model.scale": "Scale",
"transform.translation": "Translation",
"transform.rotation": "Rotation",
"transform.scale": "Scale",
"model.visible": "Visible",
"vertex.point": "Point",
"line.type.line": "Line",
Expand Down
7 changes: 4 additions & 3 deletions packages/chili-core/src/i18n/keys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

export type I18nKeys =
| "common.general"
| "common.color"
| "common.name"
| "common.matrix"
Expand Down Expand Up @@ -51,9 +52,6 @@ export type I18nKeys =
| "material.texture"
| "material.repeatU"
| "material.repeatV"
| "model.translation"
| "model.rotation"
| "model.scale"
| "model.visible"
| "vertex.point"
| "line.type.line"
Expand Down Expand Up @@ -118,6 +116,9 @@ export type I18nKeys =
| "axis.x"
| "axis.y"
| "axis.z"
| "transform.translation"
| "transform.rotation"
| "transform.scale"
| "toast.command.{0}excuting"
| "toast.document.saved"
| "toast.document.noActived"
Expand Down
7 changes: 4 additions & 3 deletions packages/chili-core/src/i18n/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default {
display: "简体中文",
code: "zh-CN",
translation: {
"common.general": "常规",
"common.color": "颜色",
"common.opacity": "不透明度",
"common.cancel": "取消",
Expand Down Expand Up @@ -42,9 +43,9 @@ export default {
"material.texture": "贴图",
"material.repeatU": "U 重复",
"material.repeatV": "V 重复",
"model.translation": "位移",
"model.rotation": "旋转",
"model.scale": "缩放",
"transform.translation": "位移",
"transform.rotation": "旋转",
"transform.scale": "缩放",
"model.visible": "可见",
"vertex.point": "点",
"line.type.line": "直线",
Expand Down
2 changes: 1 addition & 1 deletion packages/chili-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export * from "./constants";
export * from "./document";
export * from "./editor";
export * from "./foundation";
export * from "./geometry";
export * from "./i18n";
export * from "./material";
export * from "./math";
Expand All @@ -17,6 +16,7 @@ export * from "./selection";
export * from "./selectionFilter";
export * from "./serialize";
export * from "./service";
export * from "./shape";
export * from "./snapType";
export * from "./ui";
export * from "./visual";
19 changes: 0 additions & 19 deletions packages/chili-core/src/model/body.ts

This file was deleted.

21 changes: 4 additions & 17 deletions packages/chili-core/src/model/entity.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { HistoryObservable, IEqualityComparer, Result } from "../foundation";
import { IShape } from "../geometry";
import { I18nKeys } from "../i18n";
import { IShape } from "../shape";

export abstract class Entity extends HistoryObservable {
protected readonly shapeChangeHandler: Set<(source: Entity) => void> = new Set();

private _retryCount: number = 0;
protected shouldRegenerate: boolean = true;
abstract display: I18nKeys;

private _shape: Result<IShape> = Result.err("Not initialised");

protected _shape: Result<IShape> = Result.err("Not initialised");
get shape(): Result<IShape> {
if (this.shouldRegenerate || (!this._shape.isOk && this._retryCount < 3)) {
if (this.shouldRegenerate) {
this._shape = this.generateShape();
this._retryCount = this._shape.isOk ? 0 : this._retryCount + 1;
this.shouldRegenerate = false;
}
return this._shape;
Expand All @@ -35,15 +30,7 @@ export abstract class Entity extends HistoryObservable {
}

protected emitShapeChanged() {
this.shapeChangeHandler.forEach((handler) => handler(this));
}

onShapeChanged(handler: (source: Entity) => void) {
this.shapeChangeHandler.add(handler);
}

removeShapeChanged(handler: (source: Entity) => void) {
this.shapeChangeHandler.delete(handler);
this.emitPropertyChanged("shape", this._shape);
}

protected abstract generateShape(): Result<IShape>;
Expand Down
2 changes: 1 addition & 1 deletion packages/chili-core/src/model/feature.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IShape } from "../geometry";
import { IShape } from "../shape";
import { Entity } from "./entity";

export abstract class Feature extends Entity {
Expand Down
124 changes: 124 additions & 0 deletions packages/chili-core/src/model/geometry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IDocument } from "../document";
import { Matrix4 } from "../math";
import { Property } from "../property";
import { Serializer } from "../serialize";
import { Entity } from "./entity";

export abstract class GeometryObject extends Entity {
private _materialId: string;
@Serializer.serialze()
@Property.define("common.material", { type: "materialId" })
get materialId(): string {
return this._materialId;
}
set materialId(value: string) {
this.setProperty("materialId", value);
}

protected _matrix: Matrix4 = Matrix4.identity();
@Serializer.serialze()
get matrix(): Matrix4 {
return this._matrix;
}
set matrix(value: Matrix4) {
this.setProperty(
"matrix",
value,
() => {
if (this.shape.isOk) {
this.shape.value.matrix = value;
}
},
{
equals: (left, right) => left.equals(right),
},
);
}

constructor(document: IDocument, materialId?: string) {
super(document);
this._materialId = materialId ?? document.materials.at(0)!.id;
}
}

/*
export abstract class HistoryBody extends Body {
private readonly _features: Feature[] = [];
private onShapeChanged = (entity: Entity) => {
if (entity === this) {
this.drawShape();
} else {
let editor = entity as Feature;
let i = this._features.indexOf(editor);
this.applyFeatures(i);
}
this.redrawModel();
};
drawShape() {
this.applyFeatures(0);
}
private applyFeatures(startIndex: number) {
if (startIndex < 0) return;
for (let i = startIndex; i < this._features.length; i++) {
this._shape = this._features[i].shape;
if (!this._shape.isOk) {
return;
}
}
if (this._shape) {
this._shape.matrix = this._matrix;
}
}
removeFeature(feature: Feature) {
const index = this._features.indexOf(feature, 0);
if (index > -1) {
this._features.splice(index, 1);
feature.removeShapeChanged(this.onShapeChanged);
this._features[index].origin =
index === 0 ? this.body.shape.unwrap() : this._features[index - 1].shape.unwrap(); // todo
this.applyFeatures(index);
this.redrawModel();
}
}
addFeature(feature: Feature) {
if (this._features.indexOf(feature) > -1) return;
this._features.push(feature);
feature.onShapeChanged(this.onShapeChanged);
if (this._shape !== undefined) {
feature.origin = this._shape;
this.applyFeatures(this._features.length - 1);
this.redrawModel();
}
}
getFeature(index: number) {
if (index < this._features.length) {
return this._features[index];
}
return undefined;
}
features() {
return [...this._features];
}
}
*/

export abstract class FaceableGeometry extends GeometryObject {
protected _isFace: boolean = false;
@Serializer.serialze()
@Property.define("command.faceable.isFace")
get isFace() {
return this._isFace;
}
set isFace(value: boolean) {
this.setPropertyAndUpdate("isFace", value);
}
}
2 changes: 1 addition & 1 deletion packages/chili-core/src/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

export * from "./body";
export * from "./entity";
export * from "./feature";
export * from "./geometry";
export * from "./model";
export * from "./node";
export * from "./nodeLinkedList";
Loading

0 comments on commit 72c74f3

Please sign in to comment.