Make it possible to change the name of an enum button
This commit is contained in:
parent
bd01dfa69a
commit
c43ed1e73f
5 changed files with 56 additions and 35 deletions
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -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)]
|
||||
|
|
|
|||
|
|
@ -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<Face>();
|
||||
var facesToRemove = new HashSet<int>();
|
||||
|
||||
var cutRemove = CutHeight - cutMargin;
|
||||
var cutRemove = CutHeight.Value(this) - cutMargin;
|
||||
for (int i = 0; i < mesh.Faces.Count; i++)
|
||||
{
|
||||
var face = mesh.Faces[i];
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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<EnumNameAttribute>(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<string>();
|
||||
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<string> 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<string> 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<string> 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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue