Skip to content

Commit

Permalink
add GroupNode
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Dec 18, 2024
1 parent 113f900 commit b673f4a
Show file tree
Hide file tree
Showing 19 changed files with 207 additions and 91 deletions.
4 changes: 2 additions & 2 deletions packages/chili-core/src/dataExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { IDocument } from "./document";
import { PubSub } from "./foundation";
import { EditableShapeNode, NodeLinkedList, ShapeNode, VisualNode } from "./model";
import { EditableShapeNode, FolderNode, ShapeNode, VisualNode } from "./model";

export interface IDataExchange {
importFormats(): string[];
Expand Down Expand Up @@ -35,7 +35,7 @@ export class DefaultDataExchange implements IDataExchange {
let shapes = shape.value.map((x, i) => {
return new EditableShapeNode(document, `Imported ${i}`, x);
});
let nodeList = new NodeLinkedList(document, file.name);
let nodeList = new FolderNode(document, file.name);
document.addNode(nodeList);
nodeList.add(...shapes);
document.visual.update();
Expand Down
20 changes: 10 additions & 10 deletions packages/chili-core/src/material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export class Material extends HistoryObservable {

@Serializer.serialze()
@Property.define("common.color", { type: "color" })
get color(): number {
get color(): number | string {
return this.getPrivateValue("color");
}
set color(value: number) {
set color(value: number | string) {
this.setProperty("color", value);
}

Expand All @@ -111,10 +111,10 @@ export class Material extends HistoryObservable {
this.setProperty("map", value);
}

constructor(document: IDocument, name: string, color: number, id: string = Id.generate()) {
constructor(document: IDocument, name: string, color: number | string, id: string = Id.generate()) {
super(document);
this.id = id;
this.setPrivateValue("name", name ? name : "unnamed");
this.setPrivateValue("name", name?.length > 0 ? name : "unnamed");
this.setPrivateValue("color", color);
}

Expand All @@ -130,10 +130,10 @@ export class Material extends HistoryObservable {
export class PhongMaterial extends Material {
@Serializer.serialze()
@Property.define("material.specular", {type: "color"})
get specular(): number {
get specular(): number | string {
return this.getPrivateValue("specular", 0x111111);
}
set specular(value: number) {
set specular(value: number | string) {
this.setProperty("specular", value);
}

Expand All @@ -148,10 +148,10 @@ export class PhongMaterial extends Material {

@Serializer.serialze()
@Property.define("material.emissive", {type: "color"})
get emissive(): number {
get emissive(): number | string {
return this.getPrivateValue("emissive", 0x000000);
}
set emissive(value: number) {
set emissive(value: number | string) {
this.setProperty("emissive", value);
}

Expand Down Expand Up @@ -232,10 +232,10 @@ export class PhysicalMaterial extends Material {

@Serializer.serialze()
@Property.define("material.emissive", {type: "color"})
get emissive(): number {
get emissive(): number | string {
return this.getPrivateValue("emissive", 0x000000);
}
set emissive(value: number) {
set emissive(value: number | string) {
this.setProperty("emissive", value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Serializer } from "../serialize";
import { INode, INodeLinkedList, Node } from "./node";

@Serializer.register(["document", "name", "id"])
export class NodeLinkedList extends Node implements INodeLinkedList {
export class FolderNode extends Node implements INodeLinkedList {
private _count: number = 0;

private _firstChild: INode | undefined;
Expand Down Expand Up @@ -159,11 +159,11 @@ export class NodeLinkedList extends Node implements INodeLinkedList {
newPrevious: target,
node,
};
NodeLinkedList.insertNodeAfter(this, target, node);
FolderNode.insertNodeAfter(this, target, node);
this.document.notifyNodeChanged([record]);
}

private static insertNodeAfter(parent: NodeLinkedList, target: INode | undefined, node: INode) {
private static insertNodeAfter(parent: FolderNode, target: INode | undefined, node: INode) {
if (parent.initParentAndAssertNotFirst(node)) {
if (target === undefined) {
parent._firstChild!.previousSibling = node;
Expand All @@ -181,7 +181,7 @@ export class NodeLinkedList extends Node implements INodeLinkedList {
parent._count++;
}

move(child: INode, newParent: NodeLinkedList, previousSibling?: INode): void {
move(child: INode, newParent: FolderNode, previousSibling?: INode): void {
if (previousSibling !== undefined && previousSibling.parent !== newParent) {
Logger.warn(`${previousSibling.name} is not a child node of the ${newParent.name} node`);
return;
Expand All @@ -195,7 +195,7 @@ export class NodeLinkedList extends Node implements INodeLinkedList {
node: child,
};
this.removeNode(child, false);
NodeLinkedList.insertNodeAfter(newParent, previousSibling, child);
FolderNode.insertNodeAfter(newParent, previousSibling, child);

this.document.notifyNodeChanged([record]);
}
Expand Down
23 changes: 23 additions & 0 deletions packages/chili-core/src/model/groupNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { Matrix4 } from "../math";
import { Serializer } from "../serialize";
import { FolderNode } from "./folderNode";

@Serializer.register(["document", "name", "id"])
export class GroupNode extends FolderNode {
@Serializer.serialze()
get transform(): Matrix4 {
return this.getPrivateValue("transform", Matrix4.identity());
}
set transform(value: Matrix4) {
this.setProperty(
"transform",
value,
undefined,
{
equals: (left, right) => left.equals(right),
},
);
}
}
4 changes: 3 additions & 1 deletion packages/chili-core/src/model/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

export * from "./facebaseNode";
export * from "./folderNode";
export * from "./groupNode";
export * from "./node";
export * from "./nodeLinkedList";

10 changes: 9 additions & 1 deletion packages/chili-core/src/shape/meshData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import { IShape } from "./shape";

@Serializer.register([])
export class Mesh {
static createSurface() {
let mesh = new Mesh();
mesh.meshType = "surface";
mesh.normal = [];
mesh.uv = []
return mesh;
}

@Serializer.serialze()
meshType: "line" | "surface" | "linesegments" = "line";

Expand All @@ -27,7 +35,7 @@ export class Mesh {
uv: number[] | undefined = undefined;

@Serializer.serialze()
groups: { start: number; count: number; materialId: number }[] = [];
groups: { start: number; count: number; materialId: string }[] = [];
}

export interface IShapeMeshData {
Expand Down
22 changes: 11 additions & 11 deletions packages/chili-core/test/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IDocument, INode, NodeLinkedList } from "../src";
import { FolderNode, IDocument, INode } from "../src";
import { TestDocument } from "./testDocument";

describe("test node", () => {
let doc: IDocument = new TestDocument() as any;

test("test get all nodes between two nodes", () => {
let n1 = new NodeLinkedList(doc, "n1");
let n2 = new NodeLinkedList(doc, "n2");
let n3 = new NodeLinkedList(doc, "n3");
let n4 = new NodeLinkedList(doc, "n4");
let n5 = new NodeLinkedList(doc, "n5");
let n6 = new NodeLinkedList(doc, "n6");
let n7 = new NodeLinkedList(doc, "n7");
let n8 = new NodeLinkedList(doc, "n8");
let n9 = new NodeLinkedList(doc, "n9");
let n10 = new NodeLinkedList(doc, "n10");
let n1 = new FolderNode(doc, "n1");
let n2 = new FolderNode(doc, "n2");
let n3 = new FolderNode(doc, "n3");
let n4 = new FolderNode(doc, "n4");
let n5 = new FolderNode(doc, "n5");
let n6 = new FolderNode(doc, "n6");
let n7 = new FolderNode(doc, "n7");
let n8 = new FolderNode(doc, "n8");
let n9 = new FolderNode(doc, "n9");
let n10 = new FolderNode(doc, "n10");
let n11: INode = {
id: "n11",
name: "n11",
Expand Down
54 changes: 27 additions & 27 deletions packages/chili-core/test/nodeList.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IDocument, NodeLinkedList } from "../src";
import { FolderNode, IDocument } from "../src";
import { TestDocument } from "./testDocument";

describe("test NodeLinkedList", () => {
let doc: IDocument = new TestDocument() as any;

test("test add and remove", () => {
let l1 = new NodeLinkedList(doc, "l1");
let l2 = new NodeLinkedList(doc, "l2");
let l3 = new NodeLinkedList(doc, "l3");
let l4 = new NodeLinkedList(doc, "l4");
let l1 = new FolderNode(doc, "l1");
let l2 = new FolderNode(doc, "l2");
let l3 = new FolderNode(doc, "l3");
let l4 = new FolderNode(doc, "l4");

l1.add(l2);
expect(l1.firstChild).toEqual(l2);
Expand Down Expand Up @@ -48,10 +48,10 @@ describe("test NodeLinkedList", () => {
});

test("test insert before", () => {
let l1 = new NodeLinkedList(doc, "l1");
let l2 = new NodeLinkedList(doc, "l2");
let l3 = new NodeLinkedList(doc, "l3");
let l4 = new NodeLinkedList(doc, "l4");
let l1 = new FolderNode(doc, "l1");
let l2 = new FolderNode(doc, "l2");
let l3 = new FolderNode(doc, "l3");
let l4 = new FolderNode(doc, "l4");

l1.insertBefore(undefined, l2);
expect(l1.firstChild).toBe(l2);
Expand All @@ -73,11 +73,11 @@ describe("test NodeLinkedList", () => {
});

test("test insert after", () => {
let l1 = new NodeLinkedList(doc, "l1");
let l2 = new NodeLinkedList(doc, "l2");
let l3 = new NodeLinkedList(doc, "l3");
let l4 = new NodeLinkedList(doc, "l4");
let l5 = new NodeLinkedList(doc, "l5");
let l1 = new FolderNode(doc, "l1");
let l2 = new FolderNode(doc, "l2");
let l3 = new FolderNode(doc, "l3");
let l4 = new FolderNode(doc, "l4");
let l5 = new FolderNode(doc, "l5");

l1.insertAfter(undefined, l2);
expect(l1.firstChild).toEqual(l2);
Expand Down Expand Up @@ -106,12 +106,12 @@ describe("test NodeLinkedList", () => {
});

test("test moveTo", () => {
let l1 = new NodeLinkedList(doc, "l1");
let l2 = new NodeLinkedList(doc, "l2");
let l3 = new NodeLinkedList(doc, "l3");
let l4 = new NodeLinkedList(doc, "l4");
let l5 = new NodeLinkedList(doc, "l5");
let l6 = new NodeLinkedList(doc, "l6");
let l1 = new FolderNode(doc, "l1");
let l2 = new FolderNode(doc, "l2");
let l3 = new FolderNode(doc, "l3");
let l4 = new FolderNode(doc, "l4");
let l5 = new FolderNode(doc, "l5");
let l6 = new FolderNode(doc, "l6");

l1.add(l2, l4);
l2.add(l3);
Expand All @@ -133,7 +133,7 @@ describe("test NodeLinkedList", () => {
});

test("test undo redo", () => {
let rootNode = new NodeLinkedList(doc, "root");
let rootNode = new FolderNode(doc, "root");
Object.defineProperties(doc, {
rootNode: {
get() {
Expand All @@ -144,7 +144,7 @@ describe("test NodeLinkedList", () => {
expect(doc.rootNode).not.toBeUndefined();
expect(doc.rootNode).toBe(rootNode);

let l1 = new NodeLinkedList(doc, "l1");
let l1 = new FolderNode(doc, "l1");

// add undo redo
doc.rootNode.add(l1);
Expand All @@ -166,8 +166,8 @@ describe("test NodeLinkedList", () => {
expect(doc.rootNode.firstChild).toBeUndefined();
doc.rootNode.remove(l1);

let l2 = new NodeLinkedList(doc, "l2");
let l3 = new NodeLinkedList(doc, "l3");
let l2 = new FolderNode(doc, "l2");
let l3 = new FolderNode(doc, "l3");
// insertAfter undo redo
doc.rootNode.add(l1, l3);
doc.rootNode.insertAfter(l1, l2);
Expand Down Expand Up @@ -217,9 +217,9 @@ describe("test NodeLinkedList", () => {
expect(l1.previousSibling).toBe(l2);
doc.rootNode.remove(l1, l2);

let l4 = new NodeLinkedList(doc, "l4");
let l5 = new NodeLinkedList(doc, "l5");
let l6 = new NodeLinkedList(doc, "l6");
let l4 = new FolderNode(doc, "l4");
let l5 = new FolderNode(doc, "l5");
let l6 = new FolderNode(doc, "l6");
doc.rootNode.add(l1, l2);
l1.add(l3, l4);
l2.add(l5);
Expand Down
10 changes: 5 additions & 5 deletions packages/chili-core/test/serializer.test.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 { IDocument, NodeLinkedList, NodeSerializer, Serialized, Serializer } from "../src";
import { FolderNode, IDocument, NodeSerializer, Serialized, Serializer } from "../src";
import { TestDocument } from "./testDocument";

@Serializer.register(["k1" as any])
Expand Down Expand Up @@ -41,10 +41,10 @@ test("test Serializer", () => {
test("test Node Serializer", () => {
let doc: IDocument = new TestDocument() as any;

let n1 = new NodeLinkedList(doc, "n1");
let n2 = new NodeLinkedList(doc, "n2");
let n3 = new NodeLinkedList(doc, "n3");
let n4 = new NodeLinkedList(doc, "n4");
let n1 = new FolderNode(doc, "n1");
let n2 = new FolderNode(doc, "n2");
let n3 = new FolderNode(doc, "n3");
let n4 = new FolderNode(doc, "n4");
n1.add(n2, n3);
n2.add(n4);
let s = NodeSerializer.serialize(n1);
Expand Down
9 changes: 8 additions & 1 deletion packages/chili-three/src/threeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Box3,
Camera,
DoubleSide,
Object3D,
OrthographicCamera,
PerspectiveCamera,
RepeatWrapping,
Expand Down Expand Up @@ -38,7 +39,7 @@ export class ThreeHelper {
return (camera as OrthographicCamera).isOrthographicCamera;
}

static fromColor(color: number): ThreeColor {
static fromColor(color: number | string): ThreeColor {
return new ThreeColor(color);
}

Expand All @@ -63,6 +64,12 @@ export class ThreeHelper {
return new Vector3(x, y, z);
}

static getBoundingBox(object: Object3D) {
const box = new Box3();
box.setFromObject(object);
return { min: ThreeHelper.toXYZ(box.min), max: ThreeHelper.toXYZ(box.max) };
}

static boxCorners(box: Box3) {
let min = box.min;
let max = box.max;
Expand Down
2 changes: 1 addition & 1 deletion packages/chili-three/src/threeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export class ThreeView extends Observable implements IView {
let visual = new Set<IVisualObject>();
for (const obj of selectionBox.select()) {
let threeObject = obj.parent as ThreeVisualObject;
if (!threeObject || !threeObject.visible) continue;
if (!threeObject?.visible) continue;

let node = this.getNodeFromObject(threeObject);
if (node === undefined) continue;
Expand Down
Loading

0 comments on commit b673f4a

Please sign in to comment.