From 28d03c57d8ec338636c8ec8eb8f0686c8bffb3b9 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Tue, 13 Apr 2021 17:53:36 -0700 Subject: [PATCH] The width depth controls are working on all sides Still have rendering issues --- .../ScaleControls/ScaleWidthDepthControl.cs | 116 +++++++----------- .../Primitives/HalfWedgeObject3D.cs | 2 +- .../DesignTools/Primitives/PyramidObject3D.cs | 2 +- .../DesignTools/Primitives/WedgeObject3D.cs | 2 +- .../View3D/Object3DControlsLayer.cs | 7 +- 5 files changed, 52 insertions(+), 77 deletions(-) diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthControl.cs index 12fc5a0eb..7238796be 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthControl.cs @@ -70,8 +70,6 @@ namespace MatterHackers.Plugins.EditorTools private Vector3 initialHitPosition; - private AxisAlignedBoundingBox mouseDownSelectedBounds; - private Vector3 originalPointToMove; private Vector2 sizeOnMouseDown; @@ -252,8 +250,8 @@ namespace MatterHackers.Plugins.EditorTools if (hitPlane != null) { - Object3DControlContext.World.RenderPlane(hitPlane.Plane, Color.Red, true, 50, 3); - Object3DControlContext.World.RenderPlane(initialHitPosition, hitPlane.Plane.Normal, Color.Red, true, 50, 3); + //Object3DControlContext.World.RenderPlane(hitPlane.Plane, Color.Red, true, 50, 3); + //Object3DControlContext.World.RenderPlane(initialHitPosition, hitPlane.Plane.Normal, Color.Red, true, 50, 3); } } @@ -308,7 +306,6 @@ namespace MatterHackers.Plugins.EditorTools xValueDisplayInfo.Visible = true; yValueDisplayInfo.Visible = true; - hitPlane = new PlaneShape(Vector3.UnitZ, mouseEvent3D.info.HitPosition.Z, null); var edge = GetEdgePosition(selectedItem, edgeIndex); var otherSide = GetEdgePosition(selectedItem, (edgeIndex + 2) % 4); originalPointToMove = edge; @@ -323,7 +320,6 @@ namespace MatterHackers.Plugins.EditorTools { sizeOnMouseDown = new Vector2(widthDepthItem.Width, widthDepthItem.Depth); } - mouseDownSelectedBounds = selectedItem.GetAxisAlignedBoundingBox(); } base.OnMouseDown(mouseEvent3D); @@ -334,7 +330,7 @@ namespace MatterHackers.Plugins.EditorTools var selectedItem = RootSelection; ActiveSelectedItem = selectedItem; - if (MouseIsOver) + if (MouseIsOver || MouseDownOnControl) { xValueDisplayInfo.Visible = true; yValueDisplayInfo.Visible = true; @@ -349,54 +345,52 @@ namespace MatterHackers.Plugins.EditorTools if (MouseDownOnControl && hitPlane != null) { - IntersectInfo info = hitPlane.GetClosestIntersection(mouseEvent3D.MouseRay); + var info = hitPlane.GetClosestIntersection(mouseEvent3D.MouseRay); if (info != null && selectedItem != null) { - AxisAlignedBoundingBox originalSelectedBounds = selectedItem.GetAxisAlignedBoundingBox(); + var delta = info.HitPosition - initialHitPosition; - Vector3 delta = info.HitPosition - initialHitPosition; + var lockedEdge = GetEdgePosition(selectedItem, (edgeIndex + 2) % 4); - Vector3 newPosition = originalPointToMove + delta; - - if (Object3DControlContext.SnapGridDistance > 0) - { - // snap this position to the grid - double snapGridDistance = Object3DControlContext.SnapGridDistance; - - // snap this position to the grid - newPosition.X = ((int)((newPosition.X / snapGridDistance) + .5)) * snapGridDistance; - newPosition.Y = ((int)((newPosition.Y / snapGridDistance) + .5)) * snapGridDistance; - } - - Vector3 lockedEdge = GetEdgePosition(selectedItem, (edgeIndex + 2) % 4); - - Vector3 newSize = Vector3.Zero; - if (edgeIndex % 2 == 1) - { - newSize.X = lockedEdge.X - newPosition.X; - if (edgeIndex == 0 || edgeIndex == 3) - { - newSize.X *= -1; - } - } - else - { - newSize.Y = lockedEdge.Y - newPosition.Y; - if (edgeIndex == 0 || edgeIndex == 1) - { - newSize.Y *= -1; - } - } - - Vector3 scaleAmount = GetScalingConsideringShiftKey(originalSelectedBounds, mouseDownSelectedBounds, newSize, Object3DControlContext.GuiSurface.ModifierKeys); + var stretchDirection = (GetEdgePosition(selectedItem, edgeIndex) - lockedEdge).GetNormal(); + var deltaAlongStretch = stretchDirection.Dot(delta); // scale it if (selectedItem is IObjectWithWidthAndDepth widthDepthItem) { - widthDepthItem.Width *= scaleAmount.X; - widthDepthItem.Depth *= scaleAmount.Y; + var newSize = new Vector3(widthDepthItem.Width, widthDepthItem.Depth, 0); + if (edgeIndex % 2 == 1) + { + newSize.X = sizeOnMouseDown.X + deltaAlongStretch; + newSize.X = Math.Max(Math.Max(newSize.X, .001), Object3DControlContext.SnapGridDistance); + } + else + { + newSize.Y = sizeOnMouseDown.Y + deltaAlongStretch; + newSize.Y = Math.Max(Math.Max(newSize.Y, .001), Object3DControlContext.SnapGridDistance); + } + + if (Object3DControlContext.SnapGridDistance > 0) + { + // snap this position to the grid + double snapGridDistance = Object3DControlContext.SnapGridDistance; + + // snap this position to the grid + if (edgeIndex % 2 == 1) + { + newSize.X = ((int)((newSize.X / snapGridDistance) + .5)) * snapGridDistance; + } + else + { + newSize.Y = ((int)((newSize.Y / snapGridDistance) + .5)) * snapGridDistance; + } + } + + widthDepthItem.Width = newSize.X; + widthDepthItem.Depth = newSize.Y; + selectedItem.Invalidate(new InvalidateArgs(selectedItem, InvalidateType.DisplayValues)); } await selectedItem.Rebuild(); @@ -404,10 +398,6 @@ namespace MatterHackers.Plugins.EditorTools // and keep the locked edge in place Vector3 newLockedEdge = GetEdgePosition(selectedItem, (edgeIndex + 2) % 4); - AxisAlignedBoundingBox postScaleBounds = selectedItem.GetAxisAlignedBoundingBox(); - newLockedEdge.Z = 0; - lockedEdge.Z = mouseDownSelectedBounds.MinXYZ.Z - postScaleBounds.MinXYZ.Z; - selectedItem.Matrix *= Matrix4X4.CreateTranslation(lockedEdge - newLockedEdge); Invalidate(); @@ -548,9 +538,6 @@ namespace MatterHackers.Plugins.EditorTools private void EditComplete(object s, EventArgs e) { var selectedItem = ActiveSelectedItem; - Matrix4X4 startingTransform = selectedItem.Matrix; - - AxisAlignedBoundingBox originalSelectedBounds = selectedItem.GetAxisAlignedBoundingBox(); Vector3 lockedEdge = GetEdgePosition(selectedItem, (edgeIndex + 2) % 4); @@ -558,28 +545,17 @@ namespace MatterHackers.Plugins.EditorTools newSize.X = xValueDisplayInfo.Value; newSize.Y = yValueDisplayInfo.Value; - Vector3 scaleAmount = GetScalingConsideringShiftKey(originalSelectedBounds, mouseDownSelectedBounds, newSize, Object3DControlContext.GuiSurface.ModifierKeys); - - // scale it - var scale = Matrix4X4.CreateScale(scaleAmount); - - selectedItem.Matrix = selectedItem.ApplyAtBoundsCenter(scale); - - // and keep the locked edge in place - Vector3 newLockedEdge = GetEdgePosition(selectedItem, (edgeIndex + 2) % 4); - - AxisAlignedBoundingBox postScaleBounds = selectedItem.GetAxisAlignedBoundingBox(); - newLockedEdge.Z = 0; - lockedEdge.Z = originalSelectedBounds.MinXYZ.Z - postScaleBounds.MinXYZ.Z; - - selectedItem.Matrix *= Matrix4X4.CreateTranslation(lockedEdge - newLockedEdge); - - Invalidate(); - if (selectedItem is IObjectWithWidthAndDepth widthDepthItem) { SetWidthDepthUndo(new Vector2(widthDepthItem.Width, widthDepthItem.Depth), sizeOnMouseDown); } + + // and keep the locked edge in place + Vector3 newLockedEdge = GetEdgePosition(selectedItem, (edgeIndex + 2) % 4); + + selectedItem.Matrix *= Matrix4X4.CreateTranslation(lockedEdge - newLockedEdge); + + Invalidate(); } private bool ForceHideScale() diff --git a/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs b/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs index 0aa24e182..72e4e1428 100644 --- a/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs @@ -35,7 +35,7 @@ using MatterHackers.VectorMath; namespace MatterHackers.MatterControl.DesignTools { - public class HalfWedgeObject3D : PrimitiveObject3D, IObjectWithHeight + public class HalfWedgeObject3D : PrimitiveObject3D, IObjectWithHeight, IObjectWithWidthAndDepth { public HalfWedgeObject3D() { diff --git a/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs b/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs index bf0c01680..3dbce9dac 100644 --- a/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs @@ -36,7 +36,7 @@ using MatterHackers.VectorMath; namespace MatterHackers.MatterControl.DesignTools { - public class PyramidObject3D : PrimitiveObject3D, IObjectWithHeight + public class PyramidObject3D : PrimitiveObject3D, IObjectWithHeight, IObjectWithWidthAndDepth { public PyramidObject3D() { diff --git a/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs b/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs index d3f80b4cc..d0a28b96a 100644 --- a/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs @@ -38,7 +38,7 @@ using MatterHackers.VectorMath; namespace MatterHackers.MatterControl.DesignTools { - public class WedgeObject3D : PrimitiveObject3D, IPropertyGridModifier, IObjectWithHeight + public class WedgeObject3D : PrimitiveObject3D, IPropertyGridModifier, IObjectWithHeight, IObjectWithWidthAndDepth { public WedgeObject3D() { diff --git a/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs b/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs index 5dd6c460d..b9c4cf697 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs @@ -190,11 +190,10 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (selectedItem is IObjectWithWidthAndDepth widthAndDepth) { -#if DEBUG Object3DControls.Add(new ScaleWidthDepthControl(this, 0)); -#else - AddControls(ControlTypes.ScaleMatrixXY); -#endif + Object3DControls.Add(new ScaleWidthDepthControl(this, 1)); + Object3DControls.Add(new ScaleWidthDepthControl(this, 2)); + Object3DControls.Add(new ScaleWidthDepthControl(this, 3)); } else {