Skip to content

Commit

Permalink
✨ feat: add MiltiShapeNode
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Dec 20, 2024
1 parent b673f4a commit a12500d
Show file tree
Hide file tree
Showing 16 changed files with 292 additions and 146 deletions.
1 change: 1 addition & 0 deletions packages/chili-core/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default {
"box.dz": "Height",
"body.editableShape": "Editable Shape",
"body.meshNode": "Mesh Node",
"body.multiShape": "Multi Shape",
"circle.center": "Center",
"circle.radius": "Radius",
"command.arc": "Arc",
Expand Down
1 change: 1 addition & 0 deletions packages/chili-core/src/i18n/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const I18N_KEYS = [
"body.fuse",
"body.imported",
"body.line",
"body.multiShape",
"body.polygon",
"body.prism",
"body.rect",
Expand Down
1 change: 1 addition & 0 deletions packages/chili-core/src/i18n/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default {
"body.wire": "线框",
"body.editableShape": "可编辑的形状",
"body.meshNode": "网格节点",
"body.multiShape": "多形状",
"box.dx": "长",
"box.dy": "宽",
"box.dz": "高",
Expand Down
2 changes: 2 additions & 0 deletions packages/chili-core/src/math/xy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Precision } from "../foundation";
import { Serializer } from "../serialize";
import { MathUtils } from "./mathUtils";

export type XYLike = { x: number; y: number };

@Serializer.register(["x", "y"])
export class XY {
static readonly zero = new XY(0, 0);
Expand Down
2 changes: 2 additions & 0 deletions packages/chili-core/src/math/xyz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Precision } from "../foundation";
import { Serializer } from "../serialize";
import { MathUtils } from "./mathUtils";

export type XYZLike = { x: number; y: number; z: number };

@Serializer.register(["x", "y", "z"])
export class XYZ {
static readonly zero = new XYZ(0, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion packages/chili-core/src/model/facebaseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Property } from "../property";
import { Serializer } from "../serialize";
import { ParameterShapeNode } from "./node";
import { ParameterShapeNode } from "./shapeNode";

export abstract class FacebaseNode extends ParameterShapeNode {
@Serializer.serialze()
Expand Down
1 change: 1 addition & 0 deletions packages/chili-core/src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from "./facebaseNode";
export * from "./folderNode";
export * from "./groupNode";
export * from "./node";
export * from "./shapeNode";

115 changes: 4 additions & 111 deletions packages/chili-core/src/model/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import { IDocument } from "../document";
import {
HistoryObservable,
IDisposable,
IEqualityComparer,
IPropertyChanged,
Id,
PubSub,
Result,
Id
} from "../foundation";
import { I18n, I18nKeys } from "../i18n";
import { I18nKeys } from "../i18n";
import { BoundingBox, Matrix4 } from "../math";
import { Property } from "../property";
import { Serialized, Serializer } from "../serialize";
import { IShape, IShapeMeshData, Mesh } from "../shape";
import { IShapeMeshData, Mesh } from "../shape";

export interface INode extends IPropertyChanged, IDisposable {
readonly id: string;
Expand Down Expand Up @@ -331,110 +328,6 @@ export abstract class GeometryNode extends VisualNode {
protected abstract createMesh(): IShapeMeshData;
}

export abstract class ShapeNode extends GeometryNode {
protected _shape: Result<IShape> = Result.err(SHAPE_UNDEFINED);
get shape(): Result<IShape> {
return this._shape;
}

protected setShape(shape: Result<IShape>) {
if (this._shape.isOk && this._shape.value.isEqual(shape.value)) {
return;
}

if (!shape.isOk) {
PubSub.default.pub("displayError", shape.error);
return;
}

let oldShape = this._shape;
this._shape = shape;
this._mesh = undefined;
this._boundingBox = undefined;
this._shape.value.matrix = this.transform;
this.emitPropertyChanged("shape", oldShape);
}

protected override onTransformChanged(newMatrix: Matrix4): void {
if (this.shape.isOk) this.shape.value.matrix = newMatrix;
}

protected override createMesh(): IShapeMeshData {
if (!this.shape.isOk) {
throw new Error(this.shape.error);
}
return this.shape.value.mesh;
}

override dispose(): void {
super.dispose();
this.shape.unchecked()?.dispose();
}
}

const SHAPE_UNDEFINED = "Shape not initialized";
export abstract class ParameterShapeNode extends ShapeNode {
override get shape(): Result<IShape> {
if (!this._shape.isOk && this._shape.error === SHAPE_UNDEFINED) {
this._shape = this.generateShape();
}
return this._shape;
}

protected setPropertyEmitShapeChanged<K extends keyof this>(
property: K,
newValue: this[K],
onPropertyChanged?: (property: K, oldValue: this[K]) => void,
equals?: IEqualityComparer<this[K]> | undefined,
): boolean {
if (this.setProperty(property, newValue, onPropertyChanged, equals)) {
this.setShape(this.generateShape());
return true;
}

return false;
}

constructor(document: IDocument, materialId?: string, id?: string) {
super(document, undefined as any, materialId, id);
this.setPrivateValue("name", I18n.translate(this.display()));
}

protected abstract generateShape(): Result<IShape>;
}

@Serializer.register(["document", "name", "shape", "materialId", "id"])
export class EditableShapeNode extends ShapeNode {
override display(): I18nKeys {
return "body.editableShape";
}

@Serializer.serialze()
override get shape(): Result<IShape> {
return this._shape;
}
override set shape(shape: Result<IShape>) {
this.setShape(shape);
}

constructor(
document: IDocument,
name: string,
shape: IShape | Result<IShape>,
materialId?: string,
id?: string,
) {
super(document, name, materialId, id);

if (shape instanceof Result) {
this._shape = shape;
} else {
this._shape = Result.ok(shape);
}
this.setPrivateValue("transform", this._shape.value.matrix);
}
}

export namespace NodeSerializer {
export function serialize(node: INode) {
let nodes: Serialized[] = [];
Expand Down Expand Up @@ -473,4 +366,4 @@ export namespace NodeSerializer {
});
return nodeMap.get(nodes[0].properties["id"]);
}
}
}
Loading

0 comments on commit a12500d

Please sign in to comment.