From 5a842e6207fbca235f13e4dbaec7f445039fe955 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Fri, 8 Jun 2018 20:42:06 -0700 Subject: [PATCH] Create new DirectionVectorField for DirectionVector content --- DesignTools/PublicPropertyEditor.cs | 38 ++---- MatterControl.csproj | 1 + .../UIFields/DirectionVectorField.cs | 115 ++++++++++++++++++ SlicerConfiguration/UIFields/TextField.cs | 2 +- .../MatterControl/SliceSettingsFieldTests.cs | 7 ++ 5 files changed, 132 insertions(+), 31 deletions(-) create mode 100644 SlicerConfiguration/UIFields/DirectionVectorField.cs diff --git a/DesignTools/PublicPropertyEditor.cs b/DesignTools/PublicPropertyEditor.cs index 353252143..8424b9ca3 100644 --- a/DesignTools/PublicPropertyEditor.cs +++ b/DesignTools/PublicPropertyEditor.cs @@ -276,39 +276,17 @@ namespace MatterHackers.MatterControl.DesignTools } else if (property.Value is DirectionVector directionVector) { - var dropDownList = new DropDownList("Name".Localize(), theme.Colors.PrimaryTextColor, Direction.Down, pointSize: theme.DefaultFontSize) + var field = new DirectionVectorField(); + field.Initialize(0); + field.SetValue(directionVector); + field.ValueChanged += (s, e) => { - BorderColor = theme.GetBorderColor(75) + property.PropertyInfo.GetSetMethod().Invoke(property.Item, new Object[] { field.DirectionVector }); + rebuildable?.Rebuild(undoBuffer); + propertyGridModifier?.UpdateControls(context); }; - foreach (var orderItem in new string[] { "Right", "Back", "Up" }) - { - MenuItem newItem = dropDownList.AddItem(orderItem); - - var localOredrItem = orderItem; - newItem.Selected += (sender, e) => - { - switch (dropDownList.SelectedValue) - { - case "Right": - property.PropertyInfo.GetSetMethod().Invoke(property.Item, new Object[] { new DirectionVector() { Normal = Vector3.UnitX } }); - break; - case "Back": - property.PropertyInfo.GetSetMethod().Invoke(property.Item, new Object[] { new DirectionVector() { Normal = Vector3.UnitY } }); - break; - case "Up": - property.PropertyInfo.GetSetMethod().Invoke(property.Item, new Object[] { new DirectionVector() { Normal = Vector3.UnitZ } }); - break; - } - - rebuildable?.Rebuild(undoBuffer); - propertyGridModifier?.UpdateControls(context); - }; - } - - dropDownList.SelectedLabel = "Right"; - - rowContainer = CreateSettingsRow(property, dropDownList); + rowContainer = CreateSettingsRow(property, field.Content); } else if (property.Value is DirectionAxis directionAxis) { diff --git a/MatterControl.csproj b/MatterControl.csproj index ac77583c5..f19e0c91d 100644 --- a/MatterControl.csproj +++ b/MatterControl.csproj @@ -256,6 +256,7 @@ + diff --git a/SlicerConfiguration/UIFields/DirectionVectorField.cs b/SlicerConfiguration/UIFields/DirectionVectorField.cs new file mode 100644 index 000000000..3f89a6117 --- /dev/null +++ b/SlicerConfiguration/UIFields/DirectionVectorField.cs @@ -0,0 +1,115 @@ +/* +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 MatterHackers.Agg.UI; +using MatterHackers.Localizations; +using MatterHackers.MatterControl.DesignTools.EditableTypes; +using MatterHackers.VectorMath; +using Newtonsoft.Json; + +namespace MatterHackers.MatterControl.SlicerConfiguration +{ + public class DirectionVectorField : UIField + { + private DropDownList dropDownList; + + public override void Initialize(int tabIndex) + { + var theme = ApplicationController.Instance.Theme; + + dropDownList = new DropDownList("Name".Localize(), theme.Colors.PrimaryTextColor, Direction.Down, pointSize: theme.DefaultFontSize) + { + BorderColor = theme.GetBorderColor(75) + }; + + dropDownList.AddItem( + "Back".Localize(), + JsonConvert.SerializeObject( + new DirectionVector() + { + Normal = Vector3.UnitY + })); + + dropDownList.AddItem( + "Up".Localize(), + JsonConvert.SerializeObject( + new DirectionVector() + { + Normal = Vector3.UnitZ + })); + + dropDownList.AddItem( + "Right".Localize(), + JsonConvert.SerializeObject( + new DirectionVector() + { + Normal = Vector3.UnitX + })); + + dropDownList.SelectedLabel = "Right"; + + dropDownList.SelectionChanged += (s, e) => + { + if (this.Value != dropDownList.SelectedValue) + { + this.SetValue( + dropDownList.SelectedValue, + userInitiated: true); + }; + }; + + this.Content = dropDownList; + } + + public DirectionVector DirectionVector { get; private set; } + + public void SetValue(DirectionVector directionVector) + { + this.SetValue( + JsonConvert.SerializeObject(directionVector), + false); + } + + protected override string ConvertValue(string newValue) + { + this.DirectionVector = JsonConvert.DeserializeObject(newValue); + return base.ConvertValue(newValue); + } + + protected override void OnValueChanged(FieldChangedEventArgs fieldChangedEventArgs) + { + if (this.Value != dropDownList.SelectedValue) + { + dropDownList.SelectedValue = this.Value; + } + + base.OnValueChanged(fieldChangedEventArgs); + } + } +} diff --git a/SlicerConfiguration/UIFields/TextField.cs b/SlicerConfiguration/UIFields/TextField.cs index 93eca95dd..7e9338cef 100644 --- a/SlicerConfiguration/UIFields/TextField.cs +++ b/SlicerConfiguration/UIFields/TextField.cs @@ -46,7 +46,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration if (this.Value != textEditWidget.Text) { this.SetValue( - textEditWidget.Text, + textEditWidget.Text, userInitiated: true); } }; diff --git a/Tests/MatterControl.Tests/MatterControl/SliceSettingsFieldTests.cs b/Tests/MatterControl.Tests/MatterControl/SliceSettingsFieldTests.cs index 7db355cf8..f08992175 100644 --- a/Tests/MatterControl.Tests/MatterControl/SliceSettingsFieldTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/SliceSettingsFieldTests.cs @@ -344,6 +344,13 @@ namespace MatterControl.Tests.MatterControl Assert.Fail(); } + [Test, Ignore("Not Implemented")] + public void DirectionVectorFieldTest() + { + //var field = new BoundDoubleField(); + Assert.Fail(); + } + public class ValueMap { [DebuggerStepThrough]