From fa80380c26fc1da328ef97fc6ba053291bde357c Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Fri, 16 Apr 2021 17:03:39 -0700 Subject: [PATCH] All the corner and edge controls are working --- .../ScaleControls/ScaleHeightControl.cs | 10 +- .../ScaleControls/ScaleMatrixCornerControl.cs | 125 +++++++++++------- .../ScaleWidthDepthCornerControl.cs | 24 ++-- .../ScaleWidthDepthEdgeControl.cs | 20 +-- .../DesignTools/Primitives/ConeObject3D.cs | 3 + .../DesignTools/Primitives/CubeObject3D.cs | 3 + .../Primitives/HalfCylinderObject3D.cs | 13 +- .../Primitives/HalfWedgeObject3D.cs | 5 + .../DesignTools/Primitives/PyramidObject3D.cs | 5 + .../DesignTools/Primitives/TextObject3D.cs | 1 + .../DesignTools/Primitives/WedgeObject3D.cs | 3 + .../View3D/Object3DControlsLayer.cs | 36 ++--- 12 files changed, 162 insertions(+), 86 deletions(-) diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs index 1f9f08a62..51f5a3dca 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleHeightControl.cs @@ -340,10 +340,14 @@ namespace MatterHackers.Plugins.EditorTools public override void OnMouseUp(Mouse3DEventArgs mouseEvent3D) { - if (activeSelectedItem is IObjectWithHeight heightObject - && heightObject.Height != heightOnMouseDown) + if (MouseDownOnControl) { - SetHeightUndo(heightObject.Height, heightOnMouseDown); + if (activeSelectedItem is IObjectWithHeight heightObject + && heightObject.Height != heightOnMouseDown) + { + SetHeightUndo(heightObject.Height, heightOnMouseDown); + } + Object3DControlContext.Scene.ShowSelectionShadow = true; } diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleMatrixCornerControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleMatrixCornerControl.cs index 78d84d38b..eb2229695 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleMatrixCornerControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleMatrixCornerControl.cs @@ -60,7 +60,6 @@ namespace MatterHackers.Plugins.EditorTools private double LineLength => 35 * GuiWidget.DeviceScale; - private readonly List lines = new List(); private Vector3 originalPointToMove; private readonly int quadrantIndex; private readonly double selectCubeSize = 7 * GuiWidget.DeviceScale; @@ -194,11 +193,69 @@ namespace MatterHackers.Plugins.EditorTools } } + if (MouseIsOver || MouseDownOnControl) + { + DrawMeasureLines(e, quadrantIndex); + DrawMeasureLines(e, quadrantIndex + 1); + } + base.Draw(e); } + private (Vector3 start0, Vector3 end0, Vector3 start1, Vector3 end1) GetMeasureLine(int quadrant) + { + var selectedItem = RootSelection; + var corner = new Vector3[4]; + var screen = new Vector3[4]; + for (int i = 0; i < 4; i++) + { + corner[i] = GetCornerPosition(selectedItem, quadrant + i); + screen[i] = Object3DControlContext.World.GetScreenSpace(corner[i]); + } + + var start = corner[0]; + var direction = (start - corner[1]).GetNormal(); + var end = corner[3]; + // find out which side we should render on (the one closer to the screen) + if (screen[0].Z > screen[1].Z) + { + start = corner[1]; + end = corner[2]; + direction = (start - corner[0]).GetNormal(); + } + + var startScale = Object3DControlContext.World.GetWorldUnitsPerScreenPixelAtPosition(start); + var endScale = Object3DControlContext.World.GetWorldUnitsPerScreenPixelAtPosition(end); + var offset = .3; + var start0 = start + direction * LineLength * offset * startScale; + var end0 = start + direction * LineLength * (1 + offset) * endScale; + var start1 = end + direction * LineLength * offset * endScale; + var end1 = end + direction * LineLength * (1 + offset) * endScale; + return (start0, end0, start1, end1); + } + + private void DrawMeasureLines(DrawGlContentEventArgs e, int quadrant) + { + var (start0, end0, start1, end1) = GetMeasureLine(quadrant); + + var color = theme.TextColor.WithAlpha(e.Alpha0to255); + if (!e.ZBuffered) + { + theme.TextColor.WithAlpha(Constants.LineAlpha); + } + + Frustum clippingFrustum = Object3DControlContext.World.GetClippingFrustum(); + + Object3DControlContext.World.Render3DLine(clippingFrustum, start0, end0, color, e.ZBuffered, GuiWidget.DeviceScale); + Object3DControlContext.World.Render3DLine(clippingFrustum, start1, end1, color, e.ZBuffered, GuiWidget.DeviceScale); + var start = (start0 + end0) / 2; + var end = (start1 + end1) / 2; + Object3DControlContext.World.Render3DLine(clippingFrustum, start, end, color, e.ZBuffered, GuiWidget.DeviceScale * 1.2, true, true); + } + public Vector3 GetCornerPosition(IObject3D item, int quadrantIndex) { + quadrantIndex %= 4; AxisAlignedBoundingBox originalSelectedBounds = item.GetAxisAlignedBoundingBox(); Vector3 cornerPosition = originalSelectedBounds.GetBottomCorner(quadrantIndex); @@ -359,24 +416,6 @@ namespace MatterHackers.Plugins.EditorTools var centerMatrix = Matrix4X4.CreateTranslation(boxCenter); centerMatrix = Matrix4X4.CreateScale(distBetweenPixelsWorldSpace) * centerMatrix; TotalTransform = centerMatrix; - - var xOtherSide = new Vector3(cornerPosition.X + otherSideDelta.X, cornerPosition.Y, cornerPosition.Z); - var yOtherSide = new Vector3(cornerPosition.X, cornerPosition.Y + otherSideDelta.Y, cornerPosition.Z); - - lines.Clear(); - // left lines - lines.Add(Object3DControlContext.World.GetScreenPosition(cornerPosition - new Vector3(xSign * DistToStart * distBetweenPixelsWorldSpace, 0, 0))); - lines.Add(Object3DControlContext.World.GetScreenPosition(cornerPosition - new Vector3(xSign * (DistToStart + LineLength) * distBetweenPixelsWorldSpace, 0, 0))); - - lines.Add(Object3DControlContext.World.GetScreenPosition(yOtherSide - new Vector3(xSign * DistToStart * distBetweenPixelsWorldSpace, 0, 0))); - lines.Add(Object3DControlContext.World.GetScreenPosition(yOtherSide - new Vector3(xSign * (DistToStart + LineLength) * distBetweenPixelsWorldSpace, 0, 0))); - - // bottom lines - lines.Add(Object3DControlContext.World.GetScreenPosition(cornerPosition - new Vector3(0, ySign * DistToStart * distBetweenPixelsWorldSpace, 0))); - lines.Add(Object3DControlContext.World.GetScreenPosition(cornerPosition - new Vector3(0, ySign * (DistToStart + LineLength) * distBetweenPixelsWorldSpace, 0))); - - lines.Add(Object3DControlContext.World.GetScreenPosition(xOtherSide - new Vector3(0, ySign * DistToStart * distBetweenPixelsWorldSpace, 0))); - lines.Add(Object3DControlContext.World.GetScreenPosition(xOtherSide - new Vector3(0, ySign * (DistToStart + LineLength) * distBetweenPixelsWorldSpace, 0))); } public static Vector3 GetScalingConsideringShiftKey(AxisAlignedBoundingBox originalSelectedBounds, @@ -487,36 +526,32 @@ namespace MatterHackers.Plugins.EditorTools { if (MouseIsOver || MouseDownOnControl) { - for (int i = 0; i < lines.Count; i += 2) - { - // draw the line that is on the ground - drawEvent.Graphics2D.Line(lines[i], lines[i + 1], theme.TextColor); - } - - for (int i = 0; i < lines.Count; i += 4) - { - drawEvent.Graphics2D.DrawMeasureLine((lines[i] + lines[i + 1]) / 2, - (lines[i + 2] + lines[i + 3]) / 2, - LineArrows.Both, - theme); - } - - int j = 4; - - AxisAlignedBoundingBox selectedBounds = selectedItem.GetAxisAlignedBoundingBox(); - - Vector2 widthDisplayCenter = (((lines[j] + lines[j + 1]) / 2) + ((lines[j + 2] + lines[j + 3]) / 2)) / 2; - xValueDisplayInfo.Value = selectedBounds.XSize; - xValueDisplayInfo.OriginRelativeParent = widthDisplayCenter - xValueDisplayInfo.LocalBounds.Center; - - j = 0; - Vector2 heightDisplayCenter = (((lines[j] + lines[j + 1]) / 2) + ((lines[j + 2] + lines[j + 3]) / 2)) / 2; - yValueDisplayInfo.Value = selectedBounds.YSize; - yValueDisplayInfo.OriginRelativeParent = heightDisplayCenter - yValueDisplayInfo.LocalBounds.Center; + UpdateNumberControl(quadrantIndex); + UpdateNumberControl(quadrantIndex + 1); } } } + private void UpdateNumberControl(int quadrant) + { + var (start0, end0, start1, end1) = GetMeasureLine(quadrant); + var start = (start0 + end0) / 2; + var end = (start1 + end1) / 2; + var screenStart = Object3DControlContext.World.GetScreenPosition(start); + var screenEnd = Object3DControlContext.World.GetScreenPosition(end); + + if (quadrant % 2 == 1) + { + xValueDisplayInfo.Value = (start - end).Length; + xValueDisplayInfo.OriginRelativeParent = (screenStart + screenEnd) / 2 - xValueDisplayInfo.LocalBounds.Center; + } + else + { + yValueDisplayInfo.Value = (start - end).Length; + yValueDisplayInfo.OriginRelativeParent = (screenStart + screenEnd) / 2 - yValueDisplayInfo.LocalBounds.Center; + } + } + public override void Dispose() { xValueDisplayInfo.Close(); diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthCornerControl.cs index 4eb44a21f..f1c49a562 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 ScaleMatrixEdgeControl == null) + && Object3DControlContext.SelectedObject3DControl as ScaleMatrixCornerControl == null) { shouldDrawScaleControls = false; } @@ -180,7 +180,7 @@ namespace MatterHackers.Plugins.EditorTools } else { - GLHelper.Render(minXminYMesh, theme.TextColor.Blend(theme.BackgroundColor, .35).WithAlpha(e.Alpha0to255), TotalTransform, RenderTypes.Shaded); + GLHelper.Render(minXminYMesh, theme.TextColor.WithAlpha(e.Alpha0to255), TotalTransform, RenderTypes.Shaded); } } @@ -334,16 +334,18 @@ namespace MatterHackers.Plugins.EditorTools public override void OnMouseUp(Mouse3DEventArgs mouseEvent3D) { - if (hadClickOnControl - && RootSelection is IObjectWithWidthAndDepth widthDepthItem - && (widthDepthItem.Width != sizeOnMouseDown.X || widthDepthItem.Depth != sizeOnMouseDown.Y)) + if (hadClickOnControl) { - SetWidthDepthUndo(RootSelection, - Object3DControlContext.Scene.UndoBuffer, - new Vector2(widthDepthItem.Width,widthDepthItem.Depth), - RootSelection.Matrix, - sizeOnMouseDown, - matrixOnMouseDown); + if (RootSelection is IObjectWithWidthAndDepth widthDepthItem + && (widthDepthItem.Width != sizeOnMouseDown.X || widthDepthItem.Depth != sizeOnMouseDown.Y)) + { + SetWidthDepthUndo(RootSelection, + Object3DControlContext.Scene.UndoBuffer, + new Vector2(widthDepthItem.Width, widthDepthItem.Depth), + RootSelection.Matrix, + sizeOnMouseDown, + matrixOnMouseDown); + } Object3DControlContext.Scene.ShowSelectionShadow = true; } diff --git a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs index 8cb340eaf..12a2a0ec0 100644 --- a/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs +++ b/MatterControlLib/DesignTools/EditorTools/ScaleControls/ScaleWidthDepthEdgeControl.cs @@ -325,16 +325,18 @@ namespace MatterHackers.Plugins.EditorTools public override void OnMouseUp(Mouse3DEventArgs mouseEvent3D) { - if (hadClickOnControl - && RootSelection is IObjectWithWidthAndDepth widthDepthItem - && (widthDepthItem.Width != sizeOnMouseDown.X || widthDepthItem.Depth != sizeOnMouseDown.Y)) + if (hadClickOnControl) { - ScaleWidthDepthCornerControl.SetWidthDepthUndo(RootSelection, - Object3DControlContext.Scene.UndoBuffer, - new Vector2(widthDepthItem.Width, widthDepthItem.Depth), - RootSelection.Matrix, - sizeOnMouseDown, - matrixOnMouseDown); + if (RootSelection is IObjectWithWidthAndDepth widthDepthItem + && (widthDepthItem.Width != sizeOnMouseDown.X || widthDepthItem.Depth != sizeOnMouseDown.Y)) + { + ScaleWidthDepthCornerControl.SetWidthDepthUndo(RootSelection, + Object3DControlContext.Scene.UndoBuffer, + new Vector2(widthDepthItem.Width, widthDepthItem.Depth), + RootSelection.Matrix, + sizeOnMouseDown, + matrixOnMouseDown); + } Object3DControlContext.Scene.ShowSelectionShadow = true; } diff --git a/MatterControlLib/DesignTools/Primitives/ConeObject3D.cs b/MatterControlLib/DesignTools/Primitives/ConeObject3D.cs index 9c0a01249..76e7367d8 100644 --- a/MatterControlLib/DesignTools/Primitives/ConeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/ConeObject3D.cs @@ -56,7 +56,10 @@ namespace MatterHackers.MatterControl.DesignTools public double Diameter { get; set; } = 20; //[DisplayName("Top")] //public double TopDiameter { get; set; } = 0; + + [MaxDecimalPlaces(2)] public double Height { get; set; } = 20; + public int Sides { get; set; } = 40; public override async void OnInvalidate(InvalidateArgs invalidateType) diff --git a/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs b/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs index ba242b8a5..f499543f7 100644 --- a/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs @@ -44,10 +44,13 @@ namespace MatterHackers.MatterControl.DesignTools public override string ThumbnailName => "Cube"; + [MaxDecimalPlaces(2)] public double Width { get; set; } = 20; + [MaxDecimalPlaces(2)] public double Depth { get; set; } = 20; + [MaxDecimalPlaces(2)] public double Height { get; set; } = 20; public static async Task Create() diff --git a/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs b/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs index 1a575af70..51cc317fc 100644 --- a/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs @@ -33,11 +33,12 @@ using MatterHackers.Agg; using MatterHackers.Agg.VertexSource; using MatterHackers.DataConverters3D; using MatterHackers.Localizations; +using MatterHackers.MatterControl.PartPreviewWindow; using MatterHackers.VectorMath; namespace MatterHackers.MatterControl.DesignTools { - public class HalfCylinderObject3D : PrimitiveObject3D + public class HalfCylinderObject3D : PrimitiveObject3D, IObject3DControlsProvider, IObjectWithWidthAndDepth { public HalfCylinderObject3D() { @@ -55,8 +56,13 @@ namespace MatterHackers.MatterControl.DesignTools return item; } + [MaxDecimalPlaces(2)] public double Width { get; set; } = 20; + + [MaxDecimalPlaces(2)] public double Depth { get; set; } = 20; + + [MaxDecimalPlaces(2)] public int Sides { get; set; } = 20; public override async void OnInvalidate(InvalidateArgs invalidateType) @@ -105,5 +111,10 @@ namespace MatterHackers.MatterControl.DesignTools Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh)); return Task.CompletedTask; } + + public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer) + { + object3DControlsLayer.AddControls(ControlTypes.ScaleWidthDepth); + } } } \ No newline at end of file diff --git a/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs b/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs index 72e4e1428..55aa2a797 100644 --- a/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs @@ -53,8 +53,13 @@ namespace MatterHackers.MatterControl.DesignTools return item; } + [MaxDecimalPlaces(2)] public double Width { get; set; } = 20; + + [MaxDecimalPlaces(2)] public double Depth { get; set; } = 20; + + [MaxDecimalPlaces(2)] public double Height { get; set; } = 10; public override async void OnInvalidate(InvalidateArgs invalidateType) diff --git a/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs b/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs index 3dbce9dac..42ade25a1 100644 --- a/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs @@ -54,8 +54,13 @@ namespace MatterHackers.MatterControl.DesignTools return item; } + [MaxDecimalPlaces(2)] public double Width { get; set; } = 20; + + [MaxDecimalPlaces(2)] public double Depth { get; set; } = 20; + + [MaxDecimalPlaces(2)] public double Height { get; set; } = 20; public override async void OnInvalidate(InvalidateArgs invalidateType) diff --git a/MatterControlLib/DesignTools/Primitives/TextObject3D.cs b/MatterControlLib/DesignTools/Primitives/TextObject3D.cs index 541655e99..7e057c4fc 100644 --- a/MatterControlLib/DesignTools/Primitives/TextObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/TextObject3D.cs @@ -70,6 +70,7 @@ namespace MatterHackers.MatterControl.DesignTools public double PointSize { get; set; } = 24; + [MaxDecimalPlaces(2)] public double Height { get; set; } = 5; [Sortable] diff --git a/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs b/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs index d0a28b96a..ebf208990 100644 --- a/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs @@ -56,10 +56,13 @@ namespace MatterHackers.MatterControl.DesignTools return item; } + [MaxDecimalPlaces(2)] public double Width { get; set; } = 20; + [MaxDecimalPlaces(2)] public double Depth { get; set; } = 20; + [MaxDecimalPlaces(2)] public double Height { get; set; } = 20; public bool Round { get; set; } = false; diff --git a/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs b/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs index 8c62cd17e..4ac70b138 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Object3DControlsLayer.cs @@ -55,7 +55,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow MoveInZ = 1, RotateXYZ = 2, RotateZ = 4, - ScaleCornersXY = 8, + ScaleWidthDepth = 8, ScaleMatrixXY = 16, Shadow = 32, SnappingIndicators = 64, @@ -190,11 +190,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (selectedItem is IObjectWithWidthAndDepth widthAndDepth) { - for (int i = 0; i < 4; i++) - { - Object3DControls.Add(new ScaleWidthDepthCornerControl(this, i)); - Object3DControls.Add(new ScaleWidthDepthEdgeControl(this, i)); - } + AddControls(ControlTypes.ScaleWidthDepth); } else { @@ -213,9 +209,10 @@ namespace MatterHackers.MatterControl.PartPreviewWindow { if (controls.HasFlag(ControlTypes.RotateXYZ)) { - Object3DControls.Add(new RotateCornerControl(this, 0)); - Object3DControls.Add(new RotateCornerControl(this, 1)); - Object3DControls.Add(new RotateCornerControl(this, 2)); + for (int i = 0; i < 3; i++) + { + Object3DControls.Add(new RotateCornerControl(this, i)); + } } if (controls.HasFlag(ControlTypes.RotateZ)) @@ -235,15 +232,20 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (controls.HasFlag(ControlTypes.ScaleMatrixXY)) { - Object3DControls.Add(new ScaleMatrixCornerControl(this, 0)); - Object3DControls.Add(new ScaleMatrixCornerControl(this, 1)); - Object3DControls.Add(new ScaleMatrixCornerControl(this, 2)); - Object3DControls.Add(new ScaleMatrixCornerControl(this, 3)); + for (int i = 0; i < 4; i++) + { + Object3DControls.Add(new ScaleMatrixCornerControl(this, i)); + Object3DControls.Add(new ScaleMatrixEdgeControl(this, i)); + } + } - Object3DControls.Add(new ScaleMatrixEdgeControl(this, 0)); - Object3DControls.Add(new ScaleMatrixEdgeControl(this, 1)); - Object3DControls.Add(new ScaleMatrixEdgeControl(this, 2)); - Object3DControls.Add(new ScaleMatrixEdgeControl(this, 3)); + if (controls.HasFlag(ControlTypes.ScaleWidthDepth)) + { + for (int i = 0; i < 4; i++) + { + Object3DControls.Add(new ScaleWidthDepthCornerControl(this, i)); + Object3DControls.Add(new ScaleWidthDepthEdgeControl(this, i)); + } } if (controls.HasFlag(ControlTypes.Shadow))