diff --git a/SlicerConfiguration/SliceSettingsWidget.cs b/SlicerConfiguration/SliceSettingsWidget.cs index 513ef6cf2..745e237c1 100644 --- a/SlicerConfiguration/SliceSettingsWidget.cs +++ b/SlicerConfiguration/SliceSettingsWidget.cs @@ -142,6 +142,16 @@ namespace MatterHackers.MatterControl.SlicerConfiguration field.UpdateStyle(); } } + + if (allUiFields.TryGetValue(settingsKey, out IUIField field2)) + { + string currentValue = settingsContext.GetValue(settingsKey); + if (field2.Value != currentValue + || settingsKey == "com_port") + { + field2.Value = currentValue; + } + } } }, ref unregisterEvents); @@ -730,6 +740,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } Dictionary allFields = new Dictionary(); + Dictionary allUiFields = new Dictionary(); private GuiWidget CreateSettingInfoUIControls( PrinterConnection printerConnection, @@ -776,6 +787,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration settingsRow.Name = settingData.SlicerConfigName + " Edit Field"; ISettingsField field = null; + IUIField uiField = null; if (!PrinterSettings.KnownSettings.Contains(settingData.SlicerConfigName)) { @@ -798,7 +810,8 @@ namespace MatterHackers.MatterControl.SlicerConfiguration switch (settingData.DataEditType) { case SliceSettingData.DataEditTypes.INT: - field = new IntField(); + + uiField = new NumberField(); break; case SliceSettingData.DataEditTypes.DOUBLE: @@ -968,7 +981,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration field.Value = sliceSettingValue; this.PrepareRow( - field, field.Create(settingsContext, settingData, tabIndexForItem++), dataArea, unitsArea, @@ -977,6 +989,36 @@ namespace MatterHackers.MatterControl.SlicerConfiguration field.UpdateStyle = settingsRow.UpdateStyle; } + if (uiField != null) + { + allUiFields.Add(settingData.SlicerConfigName, uiField); + + uiField.Initialize(tabIndexForItem++); + + uiField.Value = sliceSettingValue; + + // After initializing the field, wrap with dropmenu if applicable + if (settingData.QuickMenuSettings.Count > 0) + { + uiField = new DropMenuWrappedField(uiField, settingData); + uiField.Initialize(tabIndexForItem); + } + + this.PrepareRow( + uiField.Content, + dataArea, + unitsArea, + settingData); + + uiField.ValueChanged += (s, e) => + { + settingsContext.SetValue(settingData.SlicerConfigName, uiField.Value); + settingsRow.UpdateStyle(); + }; + } + + + // Invoke the UpdateStyle implementation settingsRow.UpdateStyle(); @@ -1006,7 +1048,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration return settingsRow; } - private void PrepareRow(ISettingsField field, GuiWidget content, GuiWidget dataArea, GuiWidget unitsArea, SliceSettingData settingData) + private void PrepareRow(GuiWidget content, GuiWidget dataArea, GuiWidget unitsArea, SliceSettingData settingData) { dataArea.AddChild(content); diff --git a/SlicerConfiguration/UIFields/ISettingsField.cs b/SlicerConfiguration/UIFields/ISettingsField.cs index cb041177e..2504cb054 100644 --- a/SlicerConfiguration/UIFields/ISettingsField.cs +++ b/SlicerConfiguration/UIFields/ISettingsField.cs @@ -32,6 +32,17 @@ using MatterHackers.Agg.UI; namespace MatterHackers.MatterControl.SlicerConfiguration { + public interface IUIField + { + event EventHandler ValueChanged; + + string Value { get; set; } + + void Initialize(int tabIndex); + + GuiWidget Content { get; } + } + public interface ISettingsField { void OnValueChanged(string text); diff --git a/SlicerConfiguration/UIFields/IntField.cs b/SlicerConfiguration/UIFields/IntField.cs index c90c5a73d..7f67cf2b3 100644 --- a/SlicerConfiguration/UIFields/IntField.cs +++ b/SlicerConfiguration/UIFields/IntField.cs @@ -28,36 +28,163 @@ either expressed or implied, of the FreeBSD Project. */ using System; +using MatterHackers.Agg; using MatterHackers.Agg.UI; namespace MatterHackers.MatterControl.SlicerConfiguration { - public class IntField : ISettingsField + public class BasicField { - private readonly int intEditWidth = (int)(60 * GuiWidget.DeviceScale + .5); + public event EventHandler ValueChanged; - public Action UpdateStyle { get; set; } + private string fieldValue; + public string Value + { + get => fieldValue; + set + { + if (fieldValue != value) + { + fieldValue = value; + this.OnValueChanged(); + } + } + } - public string Value { get; set; } + public GuiWidget Content { get; protected set; } - private MHNumberEdit intEditWidget; + public string HelpText { get; set; } + public string Name { get; set; } + + protected virtual void OnValueChanged() + { + ValueChanged?.Invoke(this, new EventArgs()); + } + } + + public class NumberField : BasicField, IUIField + { + protected MHNumberEdit numberEdit; + + private readonly int ControlWidth = (int)(60 * GuiWidget.DeviceScale + .5); + + public void Initialize(int tabIndex) + { + numberEdit = new MHNumberEdit(0, pixelWidth: ControlWidth, tabIndex: tabIndex) + { + ToolTipText = this.HelpText, + SelectAllOnFocus = true, + Name = this.Name, + }; + numberEdit.ActuallNumberEdit.EditComplete += (s, e) => + { + if (this.Value != numberEdit.Value.ToString()) + { + this.Value = numberEdit.Value.ToString(); + } + }; + + this.Content = numberEdit; + } + + protected override void OnValueChanged() + { + int.TryParse(this.Value, out int currentValue); + numberEdit.ActuallNumberEdit.Value = currentValue; + + base.OnValueChanged(); + } + } + + public class DropMenuWrappedField : IUIField + { + private IUIField uiField; + private SliceSettingData settingData; + + public DropMenuWrappedField(IUIField uiField, SliceSettingData settingData) + { + this.settingData = settingData; + this.uiField = uiField; + } + + public string Value { get => uiField.Value; set => uiField.Value = value; } + + public GuiWidget Content { get; private set; } + + public event EventHandler ValueChanged; + + public void Initialize(int tabIndex) + { + var totalContent = new FlowLayoutWidget(); + + var selectableOptions = new DropDownList("Custom", maxHeight: 200); + selectableOptions.Margin = new BorderDouble(0, 0, 10, 0); + + foreach (QuickMenuNameValue nameValue in settingData.QuickMenuSettings) + { + string valueLocal = nameValue.Value; + + MenuItem newItem = selectableOptions.AddItem(nameValue.MenuName); + if (uiField.Value == valueLocal) + { + selectableOptions.SelectedLabel = nameValue.MenuName; + } + + newItem.Selected += (s, e) => + { + uiField.Value = valueLocal; + }; + } + + totalContent.AddChild(selectableOptions); + + uiField.Content.VAnchor = VAnchor.Center; + totalContent.AddChild(uiField.Content); + + EventHandler localUnregisterEvents = null; + + ActiveSliceSettings.SettingChanged.RegisterEvent((sender, e) => + { + if (e is StringEventArgs stringArgs + && stringArgs.Data == settingData.SlicerConfigName) + { + bool foundSetting = false; + foreach (QuickMenuNameValue nameValue in settingData.QuickMenuSettings) + { + string localName = nameValue.MenuName; + string newSliceSettingValue = uiField.Value; + if (newSliceSettingValue == nameValue.Value) + { + selectableOptions.SelectedLabel = localName; + foundSetting = true; + break; + } + } + + if (!foundSetting) + { + selectableOptions.SelectedLabel = "Custom"; + } + } + }, ref localUnregisterEvents); + + totalContent.Closed += (s, e) => + { + localUnregisterEvents?.Invoke(s, null); + }; + + this.Content = totalContent; + } + } + + /* + public class IntField : NumberField, ISettingsField + { public GuiWidget Create(SettingsContext settingsContext, SliceSettingData settingData, int tabIndex) { int.TryParse(this.Value, out int currentValue); - intEditWidget = new MHNumberEdit(currentValue, pixelWidth: intEditWidth, tabIndex: tabIndex) - { - ToolTipText = settingData.HelpText, - SelectAllOnFocus = true, - Name = settingData.PresentationName + " Edit", - }; - intEditWidget.ActuallNumberEdit.EditComplete += (sender, e) => - { - settingsContext.SetValue(settingData.SlicerConfigName, ((NumberEdit)sender).Value.ToString()); - this.UpdateStyle(); - }; - if (settingData.QuickMenuSettings.Count > 0) { return SliceSettingsWidget.CreateQuickMenu(settingData, settingsContext, intEditWidget, intEditWidget.ActuallNumberEdit.InternalTextEditWidget); @@ -72,5 +199,5 @@ namespace MatterHackers.MatterControl.SlicerConfiguration { intEditWidget.Text = text; } - } + }*/ }