diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleController.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleController.cs index 1e9cb6654..44adfe46f 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleController.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleController.cs @@ -79,16 +79,14 @@ namespace MatterHackers.Plugins.EditorTools selectedItem.Matrix = InitialState.Matrix; } - public void ScaleWidth(double newWidth) + public void EditComplete() { - FinalState = InitialState; - FinalState.Width = newWidth; - if (context.GuiSurface.ModifierKeys == Keys.Shift) - { - ScaleProportional(newWidth / InitialState.Width); - } + var doState = FinalState; + doState.Matrix = selectedItem.Matrix; - SetItem(selectedItem, FinalState); + var undoState = InitialState; + + EditComplete(undoState, doState); } public void ScaleDepth(double newDepth) @@ -103,27 +101,28 @@ namespace MatterHackers.Plugins.EditorTools SetItem(selectedItem, FinalState); } - private void ScaleProportional(double scale) + public void ScaleHeight(double newHeight) { - FinalState.Width = InitialState.Width * scale; - FinalState.Depth = InitialState.Depth * scale; - FinalState.Height = InitialState.Height * scale; + FinalState = InitialState; + FinalState.Height = newHeight; + if (context.GuiSurface.ModifierKeys == Keys.Shift) + { + ScaleProportional(newHeight / InitialState.Height); + } + + SetItem(selectedItem, FinalState); } - private void SetItem(IObject3D item, ScaleStates states) + public void ScaleWidth(double newWidth) { - if (item is IObjectWithWidthAndDepth widthDepthItem) + FinalState = InitialState; + FinalState.Width = newWidth; + if (context.GuiSurface.ModifierKeys == Keys.Shift) { - widthDepthItem.Width = states.Width; - widthDepthItem.Depth = states.Depth; + ScaleProportional(newWidth / InitialState.Width); } - if (item is IObjectWithHeight heightItem) - { - heightItem.Height = states.Height; - } - - item.Matrix = states.Matrix; + SetItem(selectedItem, FinalState); } public void SetInitialState(IObject3DControlContext context) @@ -144,16 +143,6 @@ namespace MatterHackers.Plugins.EditorTools InitialState.Matrix = selectedItem.Matrix; } - public void WidthDepthEditComplete() - { - var doState = FinalState; - doState.Matrix = selectedItem.Matrix; - - var undoState = InitialState; - - EditComplete(undoState, doState); - } - private void EditComplete(ScaleStates undoState, ScaleStates doState) { var undoBuffer = context.Scene.UndoBuffer; @@ -177,6 +166,29 @@ namespace MatterHackers.Plugins.EditorTools })); } + private void ScaleProportional(double scale) + { + FinalState.Width = InitialState.Width * scale; + FinalState.Depth = InitialState.Depth * scale; + FinalState.Height = InitialState.Height * scale; + } + + private void SetItem(IObject3D item, ScaleStates states) + { + if (item is IObjectWithWidthAndDepth widthDepthItem) + { + widthDepthItem.Width = states.Width; + widthDepthItem.Depth = states.Depth; + } + + if (item is IObjectWithHeight heightItem) + { + heightItem.Height = states.Height; + } + + item.Matrix = states.Matrix; + } + public struct ScaleStates { public double Depth; diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs index 51f5a3dca..5faa3b11c 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs @@ -58,14 +58,14 @@ namespace MatterHackers.Plugins.EditorTools private bool hadClickOnControl; - private double heightOnMouseDown = 0; - private PlaneShape hitPlane; private Vector3 initialHitPosition; private Vector3 originalPointToMove; + private ScaleController scaleController = new ScaleController(); + public ScaleHeightControl(IObject3DControlContext context) : base(context) { @@ -118,7 +118,7 @@ namespace MatterHackers.Plugins.EditorTools Invalidate(); - SetHeightUndo(heightValueDisplayInfo.Value, heightObject.Height); + scaleController.EditComplete(); } }; @@ -143,10 +143,7 @@ namespace MatterHackers.Plugins.EditorTools if (selectedItem != null && MouseDownOnControl) { - if (activeSelectedItem is IObjectWithHeight heightObject) - { - heightObject.Height = heightOnMouseDown; - } + scaleController.Cancel(); MouseDownOnControl = false; MouseIsOver = false; @@ -262,10 +259,7 @@ namespace MatterHackers.Plugins.EditorTools hitPlane = new PlaneShape(new Plane(planeNormal, mouseEvent3D.info.HitPosition), null); initialHitPosition = mouseEvent3D.info.HitPosition; - if (selectedItem is IObjectWithHeight heightObject) - { - heightOnMouseDown = heightObject.Height; - } + scaleController.SetInitialState(Object3DControlContext); Object3DControlContext.Scene.ShowSelectionShadow = false; } @@ -318,12 +312,7 @@ namespace MatterHackers.Plugins.EditorTools newSize = ((int)((newSize / snapGridDistance) + .5)) * snapGridDistance; } - if (selectedItem is IObjectWithHeight heightObject) - { - heightObject.Height = newSize; - selectedItem.Invalidate(new InvalidateArgs(selectedItem, InvalidateType.DisplayValues)); - } - + scaleController.ScaleHeight(newSize); await selectedItem.Rebuild(); @@ -343,9 +332,9 @@ namespace MatterHackers.Plugins.EditorTools if (MouseDownOnControl) { if (activeSelectedItem is IObjectWithHeight heightObject - && heightObject.Height != heightOnMouseDown) + && heightObject.Height != scaleController.InitialState.Height) { - SetHeightUndo(heightObject.Height, heightOnMouseDown); + scaleController.EditComplete(); } Object3DControlContext.Scene.ShowSelectionShadow = true; @@ -421,23 +410,5 @@ namespace MatterHackers.Plugins.EditorTools } } } - - private void SetHeightUndo(double doHeight, double undoHeight) - { - if (activeSelectedItem is IObjectWithHeight heightObject) - { - var undoBuffer = Object3DControlContext.Scene.UndoBuffer; - undoBuffer.AddAndDo(new UndoRedoActions(() => - { - heightObject.Height = undoHeight; - activeSelectedItem?.Invalidate(new InvalidateArgs(activeSelectedItem, InvalidateType.Properties)); - }, - () => - { - heightObject.Height = doHeight; - activeSelectedItem?.Invalidate(new InvalidateArgs(activeSelectedItem, InvalidateType.Properties)); - })); - } - } } } \ No newline at end of file diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs index 9809bb309..b7658802b 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs @@ -159,7 +159,7 @@ namespace MatterHackers.Plugins.EditorTools { bool shouldDrawScaleControls = true; if (Object3DControlContext.SelectedObject3DControl != null - && Object3DControlContext.SelectedObject3DControl as ScaleMatrixCornerControl == null) + && Object3DControlContext.SelectedObject3DControl as ScaleWidthDepthCornerControl == null) { shouldDrawScaleControls = false; } diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs index bd9ee3ab8..fb40c6c75 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs @@ -158,7 +158,7 @@ namespace MatterHackers.Plugins.EditorTools { bool shouldDrawScaleControls = true; if (Object3DControlContext.SelectedObject3DControl != null - && Object3DControlContext.SelectedObject3DControl as ScaleMatrixEdgeControl == null) + && Object3DControlContext.SelectedObject3DControl as ScaleWidthDepthEdgeControl == null) { shouldDrawScaleControls = false; } @@ -282,15 +282,22 @@ namespace MatterHackers.Plugins.EditorTools if (edgeIndex % 2 == 1) { newSize.X = ((int)((newSize.X / snapGridDistance) + .5)) * snapGridDistance; - scaleController.ScaleWidth(newSize.X); } else { newSize.Y = ((int)((newSize.Y / snapGridDistance) + .5)) * snapGridDistance; - scaleController.ScaleDepth(newSize.Y); } } + if (edgeIndex % 2 == 1) + { + scaleController.ScaleWidth(newSize.X); + } + else + { + scaleController.ScaleDepth(newSize.Y); + } + selectedItem.Invalidate(new InvalidateArgs(selectedItem, InvalidateType.DisplayValues)); await selectedItem.Rebuild(); @@ -315,7 +322,7 @@ namespace MatterHackers.Plugins.EditorTools && (widthDepthItem.Width != scaleController.InitialState.Width || widthDepthItem.Depth != scaleController.InitialState.Depth)) { - scaleController.WidthDepthEditComplete(); + scaleController.EditComplete(); } Object3DControlContext.Scene.ShowSelectionShadow = true; } @@ -388,7 +395,7 @@ namespace MatterHackers.Plugins.EditorTools newSize.X = xValueDisplayInfo.Value != 0 ? xValueDisplayInfo.Value : widthDepthItem.Width; newSize.Y = yValueDisplayInfo.Value != 0 ? yValueDisplayInfo.Value : widthDepthItem.Depth; - scaleController.WidthDepthEditComplete(); + scaleController.EditComplete(); // and keep the locked edge in place Vector3 newLockedEdge = ObjectSpace.GetEdgePosition(ActiveSelectedItem, edgeIndex + 2);