From c43ed1e73fa419582a57b0c67933ca1063875566 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Mon, 20 Sep 2021 09:20:22 -0700 Subject: [PATCH] Make it possible to change the name of an enum button --- .../Attributes/EnumDisplayAttribute.cs | 10 ++++ .../Operations/DecimateObject3D.cs | 3 + .../Operations/PlaneCutObject3D.cs | 14 +++-- .../Operations/TranslateObject3D.cs | 6 -- .../UIFields/EnumDisplayField.cs | 58 +++++++++++-------- 5 files changed, 56 insertions(+), 35 deletions(-) diff --git a/MatterControlLib/DesignTools/Attributes/EnumDisplayAttribute.cs b/MatterControlLib/DesignTools/Attributes/EnumDisplayAttribute.cs index 0c62512bd..4dda302c8 100644 --- a/MatterControlLib/DesignTools/Attributes/EnumDisplayAttribute.cs +++ b/MatterControlLib/DesignTools/Attributes/EnumDisplayAttribute.cs @@ -57,4 +57,14 @@ namespace MatterHackers.MatterControl.DesignTools public bool InvertIcons { get; set; } = false; } + + public class EnumNameAttribute : Attribute + { + public EnumNameAttribute(string name) + { + this.Name = name; + } + + public string Name { get; set; } + } } \ No newline at end of file diff --git a/MatterControlLib/DesignTools/Operations/DecimateObject3D.cs b/MatterControlLib/DesignTools/Operations/DecimateObject3D.cs index f4ffe1f38..34f2e56f9 100644 --- a/MatterControlLib/DesignTools/Operations/DecimateObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/DecimateObject3D.cs @@ -49,12 +49,15 @@ namespace MatterHackers.MatterControl.DesignTools public enum ReductionMode { + [EnumName("Count")] Polygon_Count, + [EnumName("Percent")] Polygon_Percent } public override bool Persistable => ApplicationController.Instance.UserHasPermission(this); + [EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)] public ReductionMode Mode { get; set; } = ReductionMode.Polygon_Percent; [ReadOnly(true)] diff --git a/MatterControlLib/DesignTools/Operations/PlaneCutObject3D.cs b/MatterControlLib/DesignTools/Operations/PlaneCutObject3D.cs index 72cac905f..dff4623e4 100644 --- a/MatterControlLib/DesignTools/Operations/PlaneCutObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/PlaneCutObject3D.cs @@ -48,7 +48,9 @@ namespace MatterHackers.MatterControl.DesignTools Name = "Plane Cut".Localize(); } - public double CutHeight { get; set; } = 10; + + [Slider(1, 200, Easing.EaseType.Quadratic, useSnappingGrid: true)] + public DoubleOrExpression CutHeight { get; set; } = 10; private double cutMargin = .01; @@ -59,18 +61,20 @@ namespace MatterHackers.MatterControl.DesignTools var itemMatrix = item.WorldMatrix(this); mesh.Transform(itemMatrix); + var cutHeight = CutHeight.Value(this); + // calculate and add the PWN face from the loops - var cutPlane = new Plane(Vector3.UnitZ, new Vector3(0, 0, CutHeight)); + var cutPlane = new Plane(Vector3.UnitZ, new Vector3(0, 0, cutHeight)); var slice = SliceLayer.CreateSlice(mesh, cutPlane); // copy every face that is on or below the cut plane // cut the faces at the cut plane - mesh.Split(new Plane(Vector3.UnitZ, CutHeight), cutMargin, cleanAndMerge: false); + mesh.Split(new Plane(Vector3.UnitZ, cutHeight), cutMargin, cleanAndMerge: false); // remove every face above the cut plane RemoveFacesAboveCut(mesh); - slice.Vertices().TriangulateFaces(null, mesh, CutHeight); + slice.Vertices().TriangulateFaces(null, mesh, cutHeight); mesh.Transform(itemMatrix.Inverted); @@ -83,7 +87,7 @@ namespace MatterHackers.MatterControl.DesignTools var newFaces = new List(); var facesToRemove = new HashSet(); - var cutRemove = CutHeight - cutMargin; + var cutRemove = CutHeight.Value(this) - cutMargin; for (int i = 0; i < mesh.Faces.Count; i++) { var face = mesh.Faces[i]; diff --git a/MatterControlLib/DesignTools/Operations/TranslateObject3D.cs b/MatterControlLib/DesignTools/Operations/TranslateObject3D.cs index 6e675c3ae..42642f2f3 100644 --- a/MatterControlLib/DesignTools/Operations/TranslateObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/TranslateObject3D.cs @@ -27,11 +27,9 @@ 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.DataConverters3D; using MatterHackers.Localizations; using MatterHackers.VectorMath; -using System.Linq; using System.Threading.Tasks; namespace MatterHackers.MatterControl.DesignTools.Operations @@ -72,12 +70,8 @@ namespace MatterHackers.MatterControl.DesignTools.Operations return translate; } - #region // editable properties - public Vector3 Translation { get; set; } = Vector3.Zero; - #endregion // editable properties - public override async void OnInvalidate(InvalidateArgs invalidateArgs) { if ((invalidateArgs.InvalidateType.HasFlag(InvalidateType.Children) diff --git a/MatterControlLib/SlicerConfiguration/UIFields/EnumDisplayField.cs b/MatterControlLib/SlicerConfiguration/UIFields/EnumDisplayField.cs index cf314b425..112a2e518 100644 --- a/MatterControlLib/SlicerConfiguration/UIFields/EnumDisplayField.cs +++ b/MatterControlLib/SlicerConfiguration/UIFields/EnumDisplayField.cs @@ -71,8 +71,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public override void Initialize(int tabIndex) { - // Enum keyed on name to friendly name - var enumItems = Enum.GetNames(property.PropertyType).Select(enumName => (enumName, enumName.Replace('_', ' ').Trim())); + (string key, string name) GetKeyName(Enum value) + { + var name = value.ToString(); + + if (GetAttribute(value) is EnumNameAttribute attr) + { + return (name, attr.Name); + } + + return (name, name.Replace('_', ' ').Trim()); + } string GetDescription(Enum value) { @@ -84,24 +93,25 @@ namespace MatterHackers.MatterControl.SlicerConfiguration return null; } - var enumDescriptions = new List(); + var enumDescriptions = new List<(string key, string name, string description)>(); foreach (var value in Enum.GetValues(property.PropertyType)) { - enumDescriptions.Add(GetDescription((Enum)value)); + var keyName = GetKeyName((Enum)value); + enumDescriptions.Add((keyName.key, keyName.name, GetDescription((Enum)value))); } switch (enumDisplayAttibute.Mode) { case EnumDisplayAttribute.PresentationMode.IconRow: - AddIconRow(enumItems, enumDescriptions); + AddIconRow(enumDescriptions); break; case EnumDisplayAttribute.PresentationMode.Tabs: - AddTabs(enumItems, enumDescriptions); + AddTabs(enumDescriptions); break; case EnumDisplayAttribute.PresentationMode.Buttons: - AddButtons(enumItems, enumDescriptions); + AddButtons(enumDescriptions); break; default: @@ -172,7 +182,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration enumTab.HAnchor = HAnchor.Stretch; } - private void AddTabs(IEnumerable<(string Key, string Value)> enumItems, List descriptions) + private void AddTabs(List<(string key, string name, string description)> items) { var menuRow = new FlowLayoutWidget() { @@ -180,13 +190,13 @@ namespace MatterHackers.MatterControl.SlicerConfiguration }; int index = 0; - foreach (var enumItem in enumItems) + foreach (var item in items) { var localIndex = index; - var radioButton = new RadioTextButton(enumItem.Value, theme) + var radioButton = new RadioTextButton(item.name, theme) { - ToolTipText = descriptions[index], + ToolTipText = item.description, }; menuRow.AfterDraw += (s, e) => @@ -203,7 +213,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration radioButton.SelectedBackgroundColor = theme.PrimaryAccentColor; // set it if checked - if (enumItem.Key == this.InitialValue) + if (item.key == this.InitialValue) { radioButton.Checked = true; } @@ -212,12 +222,12 @@ namespace MatterHackers.MatterControl.SlicerConfiguration menuRow.AddChild(radioButton); - var localItem = enumItem; + var localItem = item; radioButton.CheckedStateChanged += (s, e) => { if (radioButton.Checked) { - this.SetValue(localItem.Key, true); + this.SetValue(localItem.key, true); } }; @@ -227,20 +237,20 @@ namespace MatterHackers.MatterControl.SlicerConfiguration this.Content = menuRow; } - private void AddButtons(IEnumerable<(string Key, string Value)> enumItems, List descriptions) + private void AddButtons(List<(string key, string name, string description)> items) { var menuRow = new FlowLayoutWidget(); int index = 0; - foreach (var enumItem in enumItems) + foreach (var item in items) { var localIndex = index; - var radioButton = CreateThemedRadioButton(enumItem.Value, enumItem.Key, descriptions[index], this.InitialValue == enumItem.Key, () => this.SetValue(enumItem.Key, true), theme); + var radioButton = CreateThemedRadioButton(item.name, item.key, item.description, this.InitialValue == item.key, () => this.SetValue(item.key, true), theme); menuRow.AddChild(radioButton); - var localItem = enumItem; + var localItem = item; index++; } @@ -284,13 +294,13 @@ namespace MatterHackers.MatterControl.SlicerConfiguration return radioButton; } - private void AddIconRow(IEnumerable<(string Key, string Value)> enumItems, List descriptions) + private void AddIconRow(List<(string key, string name, string description)> items) { var iconsRow = new FlowLayoutWidget(); int index = 0; var radioButtonSize = new Vector2(enumDisplayAttibute.IconWidth, enumDisplayAttibute.IconHeight); - foreach (var enumItem in enumItems) + foreach (var item in items) { var localIndex = index; ImageBuffer iconImage = null; @@ -309,25 +319,25 @@ namespace MatterHackers.MatterControl.SlicerConfiguration var radioButton = new RadioIconButton(iconImage, theme) { - ToolTipText = descriptions[index] == null ? enumItem.Key : descriptions[index], + ToolTipText = item.description == null ? item.name : item.description, }; radioButtonSize = new Vector2(radioButton.Width, radioButton.Height); // set it if checked - if (enumItem.Key == this.InitialValue) + if (item.key == this.InitialValue) { radioButton.Checked = true; } iconsRow.AddChild(radioButton); - var localItem = enumItem; + var localItem = item; radioButton.CheckedStateChanged += (s, e) => { if (radioButton.Checked) { - this.SetValue(localItem.Key, true); + this.SetValue(localItem.key, true); } }; }