Skip to content

Commit

Permalink
Fixing undo for pending objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nilscognite committed Jun 26, 2024
1 parent a077933 commit 3b35e23
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ export abstract class DomainObject {
return true; // to be overridden
}

/**
* Gets a value indicating whether the domain object is legal.
* Normally it is legal, but if the object is pending when creating it, it is not legal.
* @returns {boolean} A boolean value indicating whether the domain object is legal.
*/

public get isLegal(): boolean {
return true;
}

// ==================================================
// VIRTUAL METHODS: Notification
// ==================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export abstract class PrimitiveEditTool extends BaseEditTool {
parent.addChildInteractive(domainObject);
domainObject.setSelectedInteractive(true);
domainObject.setVisibleInteractive(true, renderTarget);
this.addTransaction(domainObject.createTransaction(Changes.added));
} else {
this._creator = undefined;
return;
Expand All @@ -180,15 +181,18 @@ export abstract class PrimitiveEditTool extends BaseEditTool {
}

public override onUndo(): void {
// If a undo is coming, the creator should be ended.
if (this._creator === undefined) {
return;
}
// End the creator if the domainObject is removed by undo.
// To check, it doesn't have any parent if it is removed.
const domainObject = this._creator.domainObject;
if (domainObject === undefined || !domainObject.hasParent) {
if (domainObject !== undefined && !domainObject.isLegal) {
if (domainObject.hasParent) {
domainObject.removeInteractive();
}
this._creator = undefined;
this.setDefaultPrimitiveType();
}
}

Expand Down Expand Up @@ -326,21 +330,6 @@ export abstract class PrimitiveEditTool extends BaseEditTool {
if (force || creator.isFinished) {
this.setDefaultPrimitiveType();
this._creator = undefined;
} else if (creator.notPendingPointCount < creator.minimumPointCount) {
return;
}
// Add the transaction only when it is legal at the first time.
const domainObject = creator.domainObject;
if (domainObject === undefined) {
return;
}
if (this.undoManager === undefined) {
return;
}
const exists = this.undoManager.hasUniqueId(domainObject.uniqueId);
if (exists) {
return;
}
this.addTransaction(domainObject.createTransaction(Changes.added));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export abstract class BoxDomainObject extends VisualDomainObject {
}
}

public override get isLegal(): boolean {
return this.focusType !== FocusType.Pending;
}

public override createRenderStyle(): RenderStyle | undefined {
return new BoxRenderStyle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ export abstract class LineDomainObject extends VisualDomainObject {
}
}

public override get isLegal(): boolean {
if (this.focusType !== FocusType.Pending) {
return true;
}
switch (this.primitiveType) {
case PrimitiveType.Line:
return this.points.length === 2;
case PrimitiveType.Polyline:
return this.points.length >= 2;
case PrimitiveType.Polygon:
return this.points.length >= 3;
default:
throw new Error('Unknown PrimitiveType');
}
}

public override createRenderStyle(): RenderStyle | undefined {
return new LineRenderStyle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export abstract class PlaneDomainObject extends VisualDomainObject {
}
}

public override get isLegal(): boolean {
return this.focusType !== FocusType.Pending;
}

public override createRenderStyle(): RenderStyle | undefined {
return new PlaneRenderStyle();
}
Expand Down

0 comments on commit 3b35e23

Please sign in to comment.