From 55c150d1c2bebbbe9ef4dba723d7189bd06ffc18 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Tue, 2 Mar 2021 07:33:23 -0800 Subject: [PATCH] Making an editor for vector4 --- .../Attributes/VectorFieldLabelsAttribute.cs | 43 +++++++++++++++++++ .../DesignTools/PublicPropertyEditor.cs | 28 ++++++++++-- .../UIFields/BoundsField.cs | 2 +- .../UIFields/Vector4Field.cs | 10 ++--- 4 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 MatterControlLib/DesignTools/Attributes/VectorFieldLabelsAttribute.cs diff --git a/MatterControlLib/DesignTools/Attributes/VectorFieldLabelsAttribute.cs b/MatterControlLib/DesignTools/Attributes/VectorFieldLabelsAttribute.cs new file mode 100644 index 000000000..2161df5ca --- /dev/null +++ b/MatterControlLib/DesignTools/Attributes/VectorFieldLabelsAttribute.cs @@ -0,0 +1,43 @@ +/* +Copyright (c) 2018, Lars Brubaker, John Lewin +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation are those +of the authors and should not be interpreted as representing official policies, +either expressed or implied, of the FreeBSD Project. +*/ + +using System; + +namespace MatterHackers.MatterControl.DesignTools +{ + public class VectorFieldLabelsAttribute : Attribute + { + public char[] Labels { get; private set; } = new[] { 'X', 'Y', 'Z', 'W' }; + + public VectorFieldLabelsAttribute(char[] labels) + { + Labels = labels; + } + } +} \ No newline at end of file diff --git a/MatterControlLib/DesignTools/PublicPropertyEditor.cs b/MatterControlLib/DesignTools/PublicPropertyEditor.cs index 1ca28b176..e937087d7 100644 --- a/MatterControlLib/DesignTools/PublicPropertyEditor.cs +++ b/MatterControlLib/DesignTools/PublicPropertyEditor.cs @@ -59,7 +59,7 @@ namespace MatterHackers.MatterControl.DesignTools { typeof(double), typeof(int), typeof(char), typeof(string), typeof(bool), typeof(Color), - typeof(Vector2), typeof(Vector3), + typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(DirectionVector), typeof(DirectionAxis), typeof(SelectedChildren), typeof(ImageBuffer), @@ -421,6 +421,29 @@ namespace MatterHackers.MatterControl.DesignTools rowContainer = CreateSettingsColumn(property, field); } + else if (propertyValue is Vector4 vector4) + { + var field = new Vector4Field(theme); + if (property.PropertyInfo.GetCustomAttributes(true).OfType().FirstOrDefault() is VectorFieldLabelsAttribute vectorFieldLabels) + { + field.Labels = vectorFieldLabels.Labels; + } + + field.Initialize(0); + field.Vector4 = vector4; + field.ClearUndoHistory(); + + RegisterValueChanged( + field, + (valueString) => Vector4.Parse(valueString), + (value) => + { + var s = ((Vector4)value).ToString(); + return s.Substring(1, s.Length - 2); + }); + + rowContainer = CreateSettingsColumn(property, field); + } else if (propertyValue is DirectionVector directionVector) { var field = new DirectionVectorField(theme); @@ -500,8 +523,7 @@ namespace MatterHackers.MatterControl.DesignTools } else if (propertyValue is SelectedChildren childSelector) { - var showAsList = property.PropertyInfo.GetCustomAttributes(true).OfType().FirstOrDefault() != null; - if (showAsList) + if (property.PropertyInfo.GetCustomAttributes(true).OfType().FirstOrDefault() is ShowAsListAttribute showAsList) { UIField field = new ChildrenSelectorListField(property, theme); diff --git a/MatterControlLib/SlicerConfiguration/UIFields/BoundsField.cs b/MatterControlLib/SlicerConfiguration/UIFields/BoundsField.cs index 119843235..eefd95067 100644 --- a/MatterControlLib/SlicerConfiguration/UIFields/BoundsField.cs +++ b/MatterControlLib/SlicerConfiguration/UIFields/BoundsField.cs @@ -34,7 +34,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public BoundsField(ThemeConfig theme) : base (theme) { - labels = new[] { 'L', 'B', 'R', 'T' }; + Labels = new[] { 'L', 'B', 'R', 'T' }; } } } diff --git a/MatterControlLib/SlicerConfiguration/UIFields/Vector4Field.cs b/MatterControlLib/SlicerConfiguration/UIFields/Vector4Field.cs index 001abf4e6..46367a39a 100644 --- a/MatterControlLib/SlicerConfiguration/UIFields/Vector4Field.cs +++ b/MatterControlLib/SlicerConfiguration/UIFields/Vector4Field.cs @@ -44,7 +44,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration private ThemeConfig theme; - protected char[] labels = new[] { 'X', 'Y', 'Z', 'W' }; + public char[] Labels { get; set; } = new[] { 'X', 'Y', 'Z', 'W' }; public Vector4Field(ThemeConfig theme) { @@ -76,7 +76,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration double.TryParse(xyzValueStrings[0], out double currentXValue); - xEditWidget = new MHNumberEdit(currentXValue, theme, labels[0] /* X */, allowNegatives: true, allowDecimals: true, pixelWidth: VectorXYZWEditWidth, tabIndex: tabIndex) + xEditWidget = new MHNumberEdit(currentXValue, theme, Labels[0] /* X */, allowNegatives: true, allowDecimals: true, pixelWidth: VectorXYZWEditWidth, tabIndex: tabIndex) { ToolTipText = this.HelpText, TabIndex = tabIndex, @@ -98,7 +98,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration double.TryParse(xyzValueStrings[1], out double currentYValue); - yEditWidget = new MHNumberEdit(currentYValue, theme, labels[1] /* Y */, allowNegatives: true, allowDecimals: true, pixelWidth: VectorXYZWEditWidth, tabIndex: tabIndex) + yEditWidget = new MHNumberEdit(currentYValue, theme, Labels[1] /* Y */, allowNegatives: true, allowDecimals: true, pixelWidth: VectorXYZWEditWidth, tabIndex: tabIndex) { ToolTipText = this.HelpText, TabIndex = tabIndex + 1, @@ -120,7 +120,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration double.TryParse(xyzValueStrings[2], out double currentZValue); - zEditWidget = new MHNumberEdit(currentZValue, theme, labels[2] /* Z */, allowNegatives: true, allowDecimals: true, pixelWidth: VectorXYZWEditWidth, tabIndex: tabIndex) + zEditWidget = new MHNumberEdit(currentZValue, theme, Labels[2] /* Z */, allowNegatives: true, allowDecimals: true, pixelWidth: VectorXYZWEditWidth, tabIndex: tabIndex) { ToolTipText = this.HelpText, TabIndex = tabIndex + 1, @@ -142,7 +142,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration double.TryParse(xyzValueStrings[3], out double currentWValue); - wEditWidget = new MHNumberEdit(currentZValue, theme, labels[3] /* W */, allowNegatives: true, allowDecimals: true, pixelWidth: VectorXYZWEditWidth, tabIndex: tabIndex) + wEditWidget = new MHNumberEdit(currentZValue, theme, Labels[3] /* W */, allowNegatives: true, allowDecimals: true, pixelWidth: VectorXYZWEditWidth, tabIndex: tabIndex) { ToolTipText = this.HelpText, TabIndex = tabIndex + 1,