From 35bc8f510dfee349d220b48002dda582b5cd7d8c Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Wed, 2 Jun 2021 17:58:43 -0700 Subject: [PATCH] Fixing problem with scale controls issue: MatterHackers/MatterControl#5073 Crashes when entering width, depth or height --- .../ScaleControls/ScaleController.cs | 26 +++++++++++-------- .../ScaleControls/ScaleDiameterControl.cs | 12 +++++++-- .../ScaleControls/ScaleHeightControl.cs | 22 +++++++++++++--- .../ScaleWidthDepthCornerControl.cs | 13 +++++++--- .../ScaleWidthDepthEdgeControl.cs | 16 ++++++++++-- .../DesignTools/Operations/ScaleObject3D_2.cs | 10 +------ .../DesignTools/Operations/ScaleObject3D_3.cs | 10 +------ .../DesignTools/Primitives/CubeObject3D.cs | 10 +------ .../Primitives/HalfWedgeObject3D.cs | 10 +------ .../DesignTools/Primitives/PyramidObject3D.cs | 10 +------ .../DesignTools/Primitives/WedgeObject3D.cs | 10 +------ .../PartPreviewWindow/MainViewWidget.cs | 12 ++++++--- .../View3D/Object3DControlsLayer.cs | 18 +++++++++++++ 13 files changed, 100 insertions(+), 79 deletions(-) diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleController.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleController.cs index 979789f75..fe4616bd0 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleController.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleController.cs @@ -56,7 +56,8 @@ namespace MatterHackers.Plugins.EditorTools private Func getHeight; private Action setHeight; - public ScaleController(Func getWidth, + public ScaleController(IObject3DControlContext context, + Func getWidth, Action setWidth, Func getDepth, Action setDepth, @@ -65,6 +66,8 @@ namespace MatterHackers.Plugins.EditorTools List> getDiameters = null, List> setDiameters = null) { + this.context = context; + this.getWidth = getWidth; this.setWidth = setWidth; @@ -84,6 +87,8 @@ namespace MatterHackers.Plugins.EditorTools InitialState.Diameters.Add(0); } } + + SetInitialState(context); } public bool HasChange @@ -137,12 +142,7 @@ namespace MatterHackers.Plugins.EditorTools /// public void EditComplete() { - var doState = FinalState; - doState.Matrix = selectedItem.Matrix; - - var undoState = InitialState; - - EditComplete(undoState, doState); + EditComplete(InitialState, FinalState); } public void ScaleDepth(double newDepth) @@ -189,10 +189,8 @@ namespace MatterHackers.Plugins.EditorTools SetItem(selectedItem, FinalState); } - public void SetInitialState(IObject3DControlContext context) + private void SetInitialState(IObject3DControlContext context) { - this.context = context; - if (getWidth != null) { InitialState.Width = getWidth(); @@ -235,9 +233,15 @@ namespace MatterHackers.Plugins.EditorTools private void EditComplete(ScaleStates undoState, ScaleStates doState) { - var undoBuffer = context.Scene.UndoBuffer; var selectedItem = this.selectedItem; + // make copies of the scale states as they will be save into the undo redo stack + doState = new ScaleStates(doState); + doState.Matrix = selectedItem.Matrix; + undoState = new ScaleStates(undoState); + + var undoBuffer = context.Scene.UndoBuffer; + undoBuffer.Add(new UndoRedoActions(async () => { SetItem(selectedItem, undoState); diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleDiameterControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleDiameterControl.cs index f6e7a776a..c86891e8f 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleDiameterControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleDiameterControl.cs @@ -65,7 +65,10 @@ namespace MatterHackers.Plugins.EditorTools private Vector3 initialHitPosition; private ScaleController scaleController; + private Func getHeight; + private Action setHeight; private List> getDiameters; + private readonly List> setDiameters; private readonly Func controlVisible; private readonly ObjectSpace.Placement placement; private readonly int diameterIndex; @@ -82,14 +85,17 @@ namespace MatterHackers.Plugins.EditorTools double angleOffset = 0) : base(context) { + this.getHeight = getHeight; + this.setHeight = setHeight; this.getDiameters = getDiameters; + this.setDiameters = setDiameters; this.controlVisible = controlVisible; this.placement = placement; this.diameterIndex = diameterIndex; this.angleOffset = angleOffset; theme = MatterControl.AppContext.Theme; - scaleController = new ScaleController(null, null, null, null, getHeight, setHeight, getDiameters, setDiameters); + scaleController = new ScaleController(Object3DControlContext, null, null, null, null, getHeight, setHeight, getDiameters, setDiameters); diameterValueDisplayInfo = new InlineEditControl() { @@ -114,6 +120,7 @@ namespace MatterHackers.Plugins.EditorTools ActiveSelectedItem.Translate(lockedEdge - newLockedEdge); scaleController.EditComplete(); + scaleController = new ScaleController(Object3DControlContext, null, null, null, null, getHeight, setHeight, getDiameters, setDiameters); }; diameterValueDisplayInfo.VisibleChanged += (s, e) => @@ -235,7 +242,7 @@ namespace MatterHackers.Plugins.EditorTools initialHitPosition = mouseEvent3D.info.HitPosition; - scaleController.SetInitialState(Object3DControlContext); + scaleController = new ScaleController(Object3DControlContext, null, null, null, null, getHeight, setHeight, getDiameters, setDiameters); Object3DControlContext.Scene.ShowSelectionShadow = false; } @@ -310,6 +317,7 @@ namespace MatterHackers.Plugins.EditorTools if (getDiameters[diameterIndex]() != scaleController.InitialState.Diameters[diameterIndex]) { scaleController.EditComplete(); + scaleController = new ScaleController(Object3DControlContext, null, null, null, null, getHeight, setHeight, getDiameters, setDiameters); } Object3DControlContext.Scene.ShowSelectionShadow = true; } diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs index d08cd742c..580fbb676 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs @@ -52,7 +52,10 @@ namespace MatterHackers.Plugins.EditorTools private readonly InlineEditControl heightValueDisplayInfo; private readonly ThemeConfig theme; - + private readonly Func getWidth; + private readonly Action setWidth; + private readonly Func getDepth; + private readonly Action setDepth; private readonly Mesh topScaleMesh; private IObject3D activeSelectedItem; @@ -67,6 +70,9 @@ namespace MatterHackers.Plugins.EditorTools private ScaleController scaleController; private readonly Func getHeight; + private readonly Action setHeight; + private readonly List> getDiameters; + private readonly List> setDiameters; public ScaleHeightControl(IObject3DControlContext context, Func getWidth, @@ -81,8 +87,16 @@ namespace MatterHackers.Plugins.EditorTools { theme = MatterControl.AppContext.Theme; - scaleController = new ScaleController(getWidth, setWidth, getDepth, setDepth, getHeight, setHeight, getDiameters, setDiameters); + this.getWidth = getWidth; + this.setWidth = setWidth; + this.getDepth = getDepth; + this.setDepth = setDepth; this.getHeight = getHeight; + this.setHeight = setHeight; + this.getDiameters = getDiameters; + this.setDiameters = setDiameters; + + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight, getDiameters, setDiameters); heightValueDisplayInfo = new InlineEditControl() { @@ -136,6 +150,7 @@ namespace MatterHackers.Plugins.EditorTools selectedItem.Translate(bottom - postScaleBottom); scaleController.EditComplete(); + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight, getDiameters, setDiameters); }; Object3DControlContext.GuiSurface.AddChild(heightValueDisplayInfo); @@ -273,7 +288,7 @@ namespace MatterHackers.Plugins.EditorTools hitPlane = new PlaneShape(new Plane(planeNormal, mouseEvent3D.info.HitPosition), null); initialHitPosition = mouseEvent3D.info.HitPosition; - scaleController.SetInitialState(Object3DControlContext); + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight, getDiameters, setDiameters); Object3DControlContext.Scene.ShowSelectionShadow = false; } @@ -346,6 +361,7 @@ namespace MatterHackers.Plugins.EditorTools if (MouseDownOnControl) { scaleController.EditComplete(); + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight, getDiameters, setDiameters); Object3DControlContext.Scene.ShowSelectionShadow = true; } diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs index 8cee63763..dd4e04905 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs @@ -60,7 +60,8 @@ namespace MatterHackers.Plugins.EditorTools private readonly Action setWidth; private readonly Func getDepth; private readonly Action setDepth; - + private readonly Func getHeight; + private readonly Action setHeight; private readonly InlineEditControl xValueDisplayInfo; private readonly InlineEditControl yValueDisplayInfo; @@ -88,7 +89,9 @@ namespace MatterHackers.Plugins.EditorTools this.setWidth = setWidth; this.getDepth = getDepth; this.setDepth = setDepth; - scaleController = new ScaleController(getWidth, setWidth, getDepth, setDepth, getHeight, setHeight); + this.getHeight = getHeight; + this.setHeight = setHeight; + scaleController = new ScaleController(object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight); xValueDisplayInfo = new InlineEditControl() { @@ -239,7 +242,7 @@ namespace MatterHackers.Plugins.EditorTools hitPlane = new PlaneShape(new Plane(planeNormal, edge0), null); initialHitPosition = mouseEvent3D.info.HitPosition; - scaleController.SetInitialState(Object3DControlContext); + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight); Object3DControlContext.Scene.ShowSelectionShadow = false; } @@ -336,6 +339,8 @@ namespace MatterHackers.Plugins.EditorTools || getDepth() != scaleController.InitialState.Depth) { scaleController.EditComplete(); + // make a new controller so we will have new undo data + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight); } Object3DControlContext.Scene.ShowSelectionShadow = true; } @@ -421,6 +426,8 @@ namespace MatterHackers.Plugins.EditorTools ActiveSelectedItem.Translate(lockedEdge - newLockedEdge); scaleController.EditComplete(); + // make a new controller so we will have new undo data + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight); } private bool ForceHideScale() diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs index 32af77d2d..1dee3e13b 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs @@ -57,7 +57,11 @@ namespace MatterHackers.Plugins.EditorTools private readonly ThemeConfig theme; private readonly Func getWidth; + private readonly Action setWidth; private readonly Func getDepth; + private readonly Action setDepth; + private readonly Func getHeight; + private readonly Action setHeight; private readonly InlineEditControl xValueDisplayInfo; private readonly InlineEditControl yValueDisplayInfo; @@ -83,8 +87,12 @@ namespace MatterHackers.Plugins.EditorTools theme = MatterControl.AppContext.Theme; this.getWidth = getWidth; + this.setWidth = setWidth; this.getDepth = getDepth; - scaleController = new ScaleController(getWidth, setWidth, getDepth, setDepth, getHeight, setHeight); + this.setDepth = setDepth; + this.getHeight = getHeight; + this.setHeight = setHeight; + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight); xValueDisplayInfo = new InlineEditControl() { @@ -233,7 +241,7 @@ namespace MatterHackers.Plugins.EditorTools initialHitPosition = mouseEvent3D.info.HitPosition; - scaleController.SetInitialState(Object3DControlContext); + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight); Object3DControlContext.Scene.ShowSelectionShadow = false; } @@ -331,6 +339,8 @@ namespace MatterHackers.Plugins.EditorTools || (getDepth != null && getDepth() != scaleController.InitialState.Depth)) { scaleController.EditComplete(); + // make a new controller so we will have new undo data + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight); } Object3DControlContext.Scene.ShowSelectionShadow = true; } @@ -425,6 +435,8 @@ namespace MatterHackers.Plugins.EditorTools ActiveSelectedItem.Translate(lockedEdge - newLockedEdge); scaleController.EditComplete(); + // make a new controller so we will have new undo data + scaleController = new ScaleController(Object3DControlContext, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight); } private bool ForceHideScale() diff --git a/MatterControlLib/DesignTools/Operations/ScaleObject3D_2.cs b/MatterControlLib/DesignTools/Operations/ScaleObject3D_2.cs index bb0889b4b..47ccc9133 100644 --- a/MatterControlLib/DesignTools/Operations/ScaleObject3D_2.cs +++ b/MatterControlLib/DesignTools/Operations/ScaleObject3D_2.cs @@ -361,15 +361,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer) { - var controls = object3DControlsLayer.Object3DControls; - - controls.Add(new ScaleHeightControl(object3DControlsLayer, - () => Width, - (width) => Width = width, - () => Depth, - (depth) => Depth = depth, - () => Height, - (height) => Height = height)); + object3DControlsLayer.AddHeightControl(this, Width, Depth, Height); object3DControlsLayer.AddWidthDepthControls(this, Width, Depth, Height); object3DControlsLayer.AddControls(ControlTypes.MoveInZ); diff --git a/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs b/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs index 1161ed934..d782400b3 100644 --- a/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs +++ b/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs @@ -461,15 +461,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer) { - var controls = object3DControlsLayer.Object3DControls; - - controls.Add(new ScaleHeightControl(object3DControlsLayer, - () => Width.Value(this), - (width) => Width = width, - () => Depth.Value(this), - (depth) => Depth = depth, - () => Height.Value(this), - (height) => Height = height)); + object3DControlsLayer.AddHeightControl(this, Width, Depth, Height); object3DControlsLayer.AddWidthDepthControls(this, Width, Depth, Height); object3DControlsLayer.AddControls(ControlTypes.MoveInZ); diff --git a/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs b/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs index 8d4fad6a3..ca7be3cb1 100644 --- a/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs @@ -80,15 +80,7 @@ namespace MatterHackers.MatterControl.DesignTools public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer) { - var controls = object3DControlsLayer.Object3DControls; - - controls.Add(new ScaleHeightControl(object3DControlsLayer, - () => Width.Value(this), - (width) => Width = width, - () => Depth.Value(this), - (depth) => Depth = depth, - () => Height.Value(this), - (height) => Height = height)); + object3DControlsLayer.AddHeightControl(this, Width, Depth, Height); object3DControlsLayer.AddWidthDepthControls(this, Width, Depth, Height); object3DControlsLayer.AddControls(ControlTypes.MoveInZ); diff --git a/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs b/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs index f0d82a0dd..a0cb56002 100644 --- a/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs @@ -101,15 +101,7 @@ namespace MatterHackers.MatterControl.DesignTools public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer) { - var controls = object3DControlsLayer.Object3DControls; - - controls.Add(new ScaleHeightControl(object3DControlsLayer, - () => Width.Value(this), - (width) => Width = width, - () => Depth.Value(this), - (depth) => Depth = depth, - () => Height.Value(this), - (height) => Height = height)); + object3DControlsLayer.AddHeightControl(this, Width, Depth, Height); object3DControlsLayer.AddWidthDepthControls(this, Width, Depth, Height); object3DControlsLayer.AddControls(ControlTypes.MoveInZ); diff --git a/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs b/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs index 1ca1dae41..6e41165be 100644 --- a/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs @@ -102,15 +102,7 @@ namespace MatterHackers.MatterControl.DesignTools public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer) { - var controls = object3DControlsLayer.Object3DControls; - - controls.Add(new ScaleHeightControl(object3DControlsLayer, - () => Width.Value(this), - (width) => Width = width, - () => Depth.Value(this), - (depth) => Depth = depth, - () => Height.Value(this), - (height) => Height = height)); + object3DControlsLayer.AddHeightControl(this, Width, Depth, Height); object3DControlsLayer.AddWidthDepthControls(this, Width, Depth, Height); object3DControlsLayer.AddControls(ControlTypes.MoveInZ); diff --git a/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs b/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs index 22600e30d..7d793e1df 100644 --- a/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs @@ -137,15 +137,7 @@ namespace MatterHackers.MatterControl.DesignTools public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer) { - var controls = object3DControlsLayer.Object3DControls; - - controls.Add(new ScaleHeightControl(object3DControlsLayer, - () => Width.Value(this), - (width) => Width = width, - () => Depth.Value(this), - (depth) => Depth = depth, - () => Height.Value(this), - (height) => Height = height)); + object3DControlsLayer.AddHeightControl(this, Width, Depth, Height); object3DControlsLayer.AddWidthDepthControls(this, Width, Depth, Height); object3DControlsLayer.AddControls(ControlTypes.MoveInZ); diff --git a/MatterControlLib/PartPreviewWindow/MainViewWidget.cs b/MatterControlLib/PartPreviewWindow/MainViewWidget.cs index c31798030..39ddf6afd 100644 --- a/MatterControlLib/PartPreviewWindow/MainViewWidget.cs +++ b/MatterControlLib/PartPreviewWindow/MainViewWidget.cs @@ -58,7 +58,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow private int partCount = 0; private ThemeConfig theme; private Toolbar statusBar; - private FlowLayoutWidget tasksContainer; + private GuiWidget tasksContainer; private GuiWidget stretchStatusPanel; private LinkLabel updateAvailableButton; @@ -386,14 +386,18 @@ namespace MatterHackers.MatterControl.PartPreviewWindow statusBar.ActionArea.VAnchor = VAnchor.Stretch; - tasksContainer = new FlowLayoutWidget(FlowDirection.LeftToRight) + tasksContainer = statusBar.AddChild(new FlowLayoutWidget(FlowDirection.LeftToRight) { HAnchor = HAnchor.Fit, VAnchor = VAnchor.Stretch, BackgroundColor = theme.MinimalShade, Name = "runningTasksPanel" - }; - statusBar.AddChild(tasksContainer); + }); + + tasksContainer.AddChild(new TextWidget("This is a status message") + { + TextColor = theme.TextColor + }); stretchStatusPanel = new GuiWidget() { diff --git a/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs b/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs index 21fdf76eb..fd6dab923 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs @@ -38,6 +38,7 @@ using MatterHackers.Agg.Image; using MatterHackers.Agg.UI; using MatterHackers.DataConverters3D; using MatterHackers.MatterControl.DesignTools; +using MatterHackers.MatterControl.DesignTools.Operations; using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.MeshVisualizer; using MatterHackers.Plugins.EditorTools; @@ -207,6 +208,23 @@ namespace MatterHackers.MatterControl.PartPreviewWindow base.OnDraw(graphics2D); } + public void AddHeightControl(IObject3D item, DoubleOrExpression width, DoubleOrExpression depth, DoubleOrExpression height) + { + Func getWidth = () => width.Value(item); + Action setWidth = (newWidth) => width.Expression = newWidth.ToString(); + Func getDepth = () => depth.Value(item); + Action setDepth = (newDepth) => depth.Expression = newDepth.ToString(); + Func getHeight = null; + Action setHeight = null; + if (height != null) + { + getHeight = () => height.Value(item); + setHeight = (newHeight) => height.Expression = newHeight.ToString(); + } + + Object3DControls.Add(new ScaleHeightControl(this, getWidth, setWidth, getDepth, setDepth, getHeight, setHeight)); + } + public void AddWidthDepthControls(IObject3D item, DoubleOrExpression width, DoubleOrExpression depth, DoubleOrExpression height) { Func getWidth = () => width.Value(item);