From b13b3ed0566165c7e8e1ece03433ed1241f93e68 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Fri, 16 Apr 2021 16:21:27 -0700 Subject: [PATCH] fixing undo --- .../ScaleWidthDepthCornerControl.cs | 25 ++++++++--- .../ScaleWidthDepthEdgeControl.cs | 45 +++++++------------ .../View3D/Object3DControlsLayer.cs | 10 ++--- 3 files changed, 41 insertions(+), 39 deletions(-) diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs index 688c62ea7..4eb44a21f 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs @@ -67,6 +67,7 @@ namespace MatterHackers.Plugins.EditorTools private Vector3 initialHitPosition; private Vector2 sizeOnMouseDown; + private Matrix4X4 matrixOnMouseDown; public ScaleWidthDepthCornerControl(IObject3DControlContext context, int quadrant) : base(context) @@ -135,6 +136,8 @@ namespace MatterHackers.Plugins.EditorTools widthDepthItem.Depth = sizeOnMouseDown.Y; } + selectedItem.Matrix = matrixOnMouseDown; + MouseDownOnControl = false; MouseIsOver = false; @@ -184,7 +187,7 @@ namespace MatterHackers.Plugins.EditorTools if (hitPlane != null) { //Object3DControlContext.World.RenderPlane(hitPlane.Plane, Color.Red, true, 30, 3); - Object3DControlContext.World.RenderPlane(initialHitPosition, hitPlane.Plane.Normal, Color.Red, true, 30, 3); + //Object3DControlContext.World.RenderPlane(initialHitPosition, hitPlane.Plane.Normal, Color.Red, true, 30, 3); } } @@ -233,6 +236,8 @@ namespace MatterHackers.Plugins.EditorTools sizeOnMouseDown = new Vector2(widthDepthItem.Width, widthDepthItem.Depth); } + matrixOnMouseDown = selectedItem.Matrix; + Object3DControlContext.Scene.ShowSelectionShadow = false; } @@ -333,7 +338,12 @@ namespace MatterHackers.Plugins.EditorTools && RootSelection is IObjectWithWidthAndDepth widthDepthItem && (widthDepthItem.Width != sizeOnMouseDown.X || widthDepthItem.Depth != sizeOnMouseDown.Y)) { - SetWidthDepthUndo(new Vector2(widthDepthItem.Width, widthDepthItem.Depth), sizeOnMouseDown); + SetWidthDepthUndo(RootSelection, + Object3DControlContext.Scene.UndoBuffer, + new Vector2(widthDepthItem.Width,widthDepthItem.Depth), + RootSelection.Matrix, + sizeOnMouseDown, + matrixOnMouseDown); Object3DControlContext.Scene.ShowSelectionShadow = true; } @@ -409,7 +419,12 @@ namespace MatterHackers.Plugins.EditorTools newSize.X = xValueDisplayInfo.Value != 0 ? xValueDisplayInfo.Value : widthDepthItem.Width; newSize.Y = yValueDisplayInfo.Value != 0 ? yValueDisplayInfo.Value : widthDepthItem.Depth; - SetWidthDepthUndo(new Vector2(newSize), sizeOnMouseDown); + SetWidthDepthUndo(RootSelection, + Object3DControlContext.Scene.UndoBuffer, + new Vector2(newSize), + RootSelection.Matrix, + sizeOnMouseDown, + matrixOnMouseDown); // and keep the locked edge in place Vector3 newLockedEdge = ObjectSpace.GetCornerPosition(ActiveSelectedItem, quadrantIndex + 2); @@ -511,12 +526,10 @@ namespace MatterHackers.Plugins.EditorTools } } - private void SetWidthDepthUndo(Vector2 doWidthDepth, Matrix4X4 doMatrix, Vector2 undoWidthDepth, Matrix4X4 undoMatrix) + public static void SetWidthDepthUndo(IObject3D selectedItem, UndoBuffer undoBuffer, Vector2 doWidthDepth, Matrix4X4 doMatrix, Vector2 undoWidthDepth, Matrix4X4 undoMatrix) { - var selectedItem = RootSelection; if (selectedItem is IObjectWithWidthAndDepth widthDepthItem) { - var undoBuffer = Object3DControlContext.Scene.UndoBuffer; undoBuffer.AddAndDo(new UndoRedoActions(async () => { widthDepthItem.Width = undoWidthDepth.X; diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs index a65151416..8cb340eaf 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs @@ -67,7 +67,7 @@ namespace MatterHackers.Plugins.EditorTools private Vector3 initialHitPosition; private Vector2 sizeOnMouseDown; - private Vector2 matrixOnMouseDown; + private Matrix4X4 matrixOnMouseDown; public ScaleWidthDepthEdgeControl(IObject3DControlContext context, int edgeIndex) : base(context) @@ -142,6 +142,8 @@ namespace MatterHackers.Plugins.EditorTools widthDepthItem.Depth = sizeOnMouseDown.Y; } + selectedItem.Matrix = matrixOnMouseDown; + MouseDownOnControl = false; MouseIsOver = false; @@ -231,6 +233,8 @@ namespace MatterHackers.Plugins.EditorTools sizeOnMouseDown = new Vector2(widthDepthItem.Width, widthDepthItem.Depth); } + matrixOnMouseDown = selectedItem.Matrix; + Object3DControlContext.Scene.ShowSelectionShadow = false; } @@ -325,7 +329,12 @@ namespace MatterHackers.Plugins.EditorTools && RootSelection is IObjectWithWidthAndDepth widthDepthItem && (widthDepthItem.Width != sizeOnMouseDown.X || widthDepthItem.Depth != sizeOnMouseDown.Y)) { - SetWidthDepthUndo(new Vector2(widthDepthItem.Width, widthDepthItem.Depth), sizeOnMouseDown); + ScaleWidthDepthCornerControl.SetWidthDepthUndo(RootSelection, + Object3DControlContext.Scene.UndoBuffer, + new Vector2(widthDepthItem.Width, widthDepthItem.Depth), + RootSelection.Matrix, + sizeOnMouseDown, + matrixOnMouseDown); Object3DControlContext.Scene.ShowSelectionShadow = true; } @@ -397,7 +406,12 @@ namespace MatterHackers.Plugins.EditorTools newSize.X = xValueDisplayInfo.Value != 0 ? xValueDisplayInfo.Value : widthDepthItem.Width; newSize.Y = yValueDisplayInfo.Value != 0 ? yValueDisplayInfo.Value : widthDepthItem.Depth; - SetWidthDepthUndo(new Vector2(newSize), sizeOnMouseDown); + ScaleWidthDepthCornerControl.SetWidthDepthUndo(RootSelection, + Object3DControlContext.Scene.UndoBuffer, + new Vector2(newSize), + ActiveSelectedItem.Matrix, + sizeOnMouseDown, + matrixOnMouseDown); // and keep the locked edge in place Vector3 newLockedEdge = ObjectSpace.GetEdgePosition(ActiveSelectedItem, edgeIndex + 2); @@ -513,31 +527,6 @@ namespace MatterHackers.Plugins.EditorTools } } } - - private void SetWidthDepthUndo(Vector2 doWidthDepth, Matrix4X4 doMatrix, Vector2 undoWidthDepth, Matrix4X4 undoMatrix) - { - var selectedItem = RootSelection; - if (selectedItem is IObjectWithWidthAndDepth widthDepthItem) - { - var undoBuffer = Object3DControlContext.Scene.UndoBuffer; - undoBuffer.AddAndDo(new UndoRedoActions(async () => - { - widthDepthItem.Width = undoWidthDepth.X; - widthDepthItem.Depth = undoWidthDepth.Y; - await selectedItem.Rebuild(); - selectedItem.Matrix = undoMatrix; - selectedItem?.Invalidate(new InvalidateArgs(selectedItem, InvalidateType.DisplayValues)); - }, - async () => - { - widthDepthItem.Width = doWidthDepth.X; - widthDepthItem.Depth = doWidthDepth.Y; - await selectedItem.Rebuild(); - selectedItem.Matrix = doMatrix; - selectedItem?.Invalidate(new InvalidateArgs(selectedItem, InvalidateType.DisplayValues)); - })); - } - } } public static class ObjectSpace diff --git a/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs b/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs index 7e30b00cf..8c62cd17e 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs @@ -190,11 +190,11 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (selectedItem is IObjectWithWidthAndDepth widthAndDepth) { - Object3DControls.Add(new ScaleWidthDepthCornerControl(this, 0)); - //Object3DControls.Add(new ScaleWidthDepthEdgeControl(this, 0)); - //Object3DControls.Add(new ScaleWidthDepthEdgeControl(this, 1)); - //Object3DControls.Add(new ScaleWidthDepthEdgeControl(this, 2)); - //Object3DControls.Add(new ScaleWidthDepthEdgeControl(this, 3)); + for (int i = 0; i < 4; i++) + { + Object3DControls.Add(new ScaleWidthDepthCornerControl(this, i)); + Object3DControls.Add(new ScaleWidthDepthEdgeControl(this, i)); + } } else {