11// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22
33import { IDocument } from "../document" ;
4- import { IEqualityComparer , Result } from "../foundation" ;
4+ import { IEqualityComparer , PubSub , Result } from "../foundation" ;
5+ import { I18nKeys } from "../i18n" ;
56import { Matrix4 } from "../math" ;
67import { Property } from "../property" ;
78import { Serializer } from "../serialize" ;
@@ -19,119 +20,83 @@ export abstract class GeometryEntity extends Entity {
1920 this . setProperty ( "materialId" , value ) ;
2021 }
2122
22- protected shouldRegenerate : boolean = true ;
23-
24- protected _shape : Result < IShape > = Result . err ( "Not initialised" ) ;
23+ protected _shape : Result < IShape > = Result . err ( "undefined" ) ;
2524 get shape ( ) : Result < IShape > {
26- if ( this . shouldRegenerate ) {
27- this . _shape = this . generateShape ( ) ;
28- if ( this . _shape . isOk ) {
29- this . _shape . value . matrix = this . _matrix ;
30- }
31- this . shouldRegenerate = false ;
32- }
3325 return this . _shape ;
3426 }
3527
36- protected setPropertyAndUpdate < K extends keyof this> (
37- property : K ,
38- newValue : this[ K ] ,
39- onPropertyChanged ?: ( property : K , oldValue : this[ K ] ) => void ,
40- equals ?: IEqualityComparer < this[ K ] > ,
41- ) {
42- if ( this . setProperty ( property , newValue , onPropertyChanged , equals ) ) {
43- this . shouldRegenerate = true ;
44- this . emitShapeChanged ( ) ;
45- }
46- }
47-
4828 constructor ( document : IDocument , materialId ?: string ) {
4929 super ( document ) ;
5030 this . _materialId = materialId ?? document . materials . at ( 0 ) ! . id ;
5131 }
5232
53- protected emitShapeChanged ( ) {
54- this . emitPropertyChanged ( "shape" , this . _shape ) ;
33+ override onMatrixChanged ( newMatrix : Matrix4 , oldMatrix : Matrix4 ) : void {
34+ if ( this . _shape . isOk ) this . _shape . value . matrix = newMatrix ;
5535 }
5636
57- override onMatrixChanged ( newMatrix : Matrix4 , oldMatrix : Matrix4 ) : void {
58- if ( this . shape . isOk ) {
59- this . shape . value . matrix = newMatrix ;
37+ protected setShape ( shape : Result < IShape > , notify : boolean ) : boolean {
38+ if ( shape . isOk && this . _shape . isOk && this . _shape . value . isEqual ( shape . value ) ) {
39+ return false ;
6040 }
61- }
6241
63- protected abstract generateShape ( ) : Result < IShape > ;
42+ let oldShape = this . _shape ;
43+ this . _shape = shape ;
44+ if ( this . _shape . isOk ) {
45+ this . _shape . value . matrix = this . _matrix ;
46+ }
47+ if ( notify ) this . emitPropertyChanged ( "shape" , oldShape ) ;
48+ return true ;
49+ }
6450}
6551
66- /*
67- export abstract class HistoryBody extends Body {
68- private readonly _features: Feature[] = [];
69-
70- private onShapeChanged = (entity: Entity) => {
71- if (entity === this) {
72- this.drawShape();
73- } else {
74- let editor = entity as Feature;
75- let i = this._features.indexOf(editor);
76- this.applyFeatures(i);
77- }
78- this.redrawModel();
79- };
52+ @Serializer . register ( "FreeGeometryEntity" , [ "document" , "shape" , "materialId" ] )
53+ export class FreeGeometry extends GeometryEntity {
54+ override display : I18nKeys = "common.angle" ;
8055
81- drawShape() {
82- this.applyFeatures(0);
56+ @Serializer . serialze ( )
57+ override get shape ( ) : Result < IShape > {
58+ return this . _shape ;
8359 }
84-
85- private applyFeatures(startIndex: number) {
86- if (startIndex < 0) return;
87- for (let i = startIndex; i < this._features.length; i++) {
88- this._shape = this._features[i].shape;
89- if (!this._shape.isOk) {
90- return;
91- }
92- }
93- if (this._shape) {
94- this._shape.matrix = this._matrix;
95- }
60+ override set shape ( value : Result < IShape > ) {
61+ this . setShape ( value , true ) ;
9662 }
9763
98- removeFeature(feature: Feature) {
99- const index = this._features.indexOf(feature, 0);
100- if (index > -1) {
101- this._features.splice(index, 1);
102- feature.removeShapeChanged(this.onShapeChanged);
103- this._features[index].origin =
104- index === 0 ? this.body.shape.unwrap() : this._features[index - 1].shape.unwrap(); // todo
105- this.applyFeatures(index);
106- this.redrawModel();
107- }
64+ constructor ( document : IDocument , shape : IShape , materialId ?: string ) {
65+ super ( document , materialId ) ;
66+ this . _shape = Result . ok ( shape ) ;
10867 }
68+ }
10969
110- addFeature(feature: Feature) {
111- if (this._features.indexOf(feature) > -1) return;
112- this._features.push(feature);
113- feature.onShapeChanged(this.onShapeChanged);
114- if (this._shape !== undefined) {
115- feature.origin = this._shape;
116- this.applyFeatures(this._features.length - 1);
117- this.redrawModel();
70+ export abstract class ParameterGeometry extends GeometryEntity {
71+ protected shouldRegenerateShape : boolean = true ;
72+ override get shape ( ) : Result < IShape > {
73+ if ( this . shouldRegenerateShape ) {
74+ this . setShape ( this . generateShape ( ) , false ) ;
75+ this . shouldRegenerateShape = false ;
11876 }
77+ return this . _shape ;
11978 }
12079
121- getFeature(index: number) {
122- if (index < this._features.length) {
123- return this._features[index];
80+ protected setPropertyAndUpdate < K extends keyof this> (
81+ property : K ,
82+ newValue : this[ K ] ,
83+ onPropertyChanged ?: ( property : K , oldValue : this[ K ] ) => void ,
84+ equals ?: IEqualityComparer < this[ K ] > ,
85+ ) {
86+ if ( this . setProperty ( property , newValue , onPropertyChanged , equals ) ) {
87+ let shape = this . generateShape ( ) ;
88+ if ( ! shape . isOk ) {
89+ PubSub . default . pub ( "showToast" , "error.default" ) ;
90+ return ;
91+ }
92+ this . setShape ( shape , true ) ;
12493 }
125- return undefined;
12694 }
12795
128- features() {
129- return [...this._features];
130- }
96+ protected abstract generateShape ( ) : Result < IShape > ;
13197}
132- */
13398
134- export abstract class FaceableGeometry extends GeometryEntity {
99+ export abstract class FaceableGeometry extends ParameterGeometry {
135100 protected _isFace : boolean = false ;
136101 @Serializer . serialze ( )
137102 @Property . define ( "command.faceable.isFace" )
0 commit comments