From 4e9a7fdfd8a6f7188fce12ed1cc6ef42f793957e Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Mon, 17 Sep 2018 17:07:42 -0700 Subject: [PATCH] Get grid options menu to be an icon Save and load grid options from user settings --- .../View3D/GridOptionsPanel.cs | 93 +++++++++++-------- .../View3D/InteractionLayer.cs | 17 +++- .../View3D/InteractionVolume.cs | 2 +- .../PartPreviewWindow/View3D/View3DWidget.cs | 13 ++- .../SettingsManagement/UserSettings.cs | 31 +++++++ Submodules/agg-sharp | 2 +- 6 files changed, 115 insertions(+), 43 deletions(-) diff --git a/MatterControlLib/PartPreviewWindow/View3D/GridOptionsPanel.cs b/MatterControlLib/PartPreviewWindow/View3D/GridOptionsPanel.cs index 22cabff52..074a00b09 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/GridOptionsPanel.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/GridOptionsPanel.cs @@ -28,57 +28,72 @@ either expressed or implied, of the FreeBSD Project. */ using System.Collections.Generic; using MatterHackers.Agg; +using MatterHackers.Agg.Platform; using MatterHackers.Agg.UI; using MatterHackers.Localizations; +using MatterHackers.MatterControl.CustomWidgets; namespace MatterHackers.MatterControl.PartPreviewWindow { - public class GridOptionsPanel : FlowLayoutWidget, IIgnoredPopupChild + public class GridOptionsPanel : DropButton { + InteractionLayer interactionLayer; + public GridOptionsPanel(InteractionLayer interactionLayer, ThemeConfig theme) - : base(FlowDirection.TopToBottom) + : base(theme) { + this.interactionLayer = interactionLayer; + this.PopupContent = () => ShowGridOptions(theme); + + this.AddChild(new IconButton(AggContext.StaticData.LoadIcon("1694146.png", theme.InvertIcons), theme) + { + Selectable = false + }); this.HAnchor = HAnchor.Fit; - - ToolTipText = "Snap Grid".Localize(); - - var snapSettings = new Dictionary() - { - { 0, "Off" }, - { .1, "0.1" }, - { .25, "0.25" }, - { .5, "0.5" }, - { 1, "1" }, - { 2, "2" }, - { 5, "5" }, - }; - - var dropDownList = new DropDownList("0.25", theme.Colors.PrimaryTextColor, Direction.Down, pointSize: theme.DefaultFontSize) - { - TextColor = Color.Black, - HAnchor = HAnchor.Left, - BorderColor = theme.GetBorderColor(75) - }; - - foreach (var snapSetting in snapSettings) - { - MenuItem newItem = dropDownList.AddItem(snapSetting.Value); - if (interactionLayer.SnapGridDistance == snapSetting.Key) - { - dropDownList.SelectedLabel = snapSetting.Value; - } - - newItem.Selected += (sender, e) => - { - interactionLayer.SnapGridDistance = snapSetting.Key; - }; - } - this.AddChild(dropDownList); + this.VAnchor = VAnchor.Fit; } - public bool KeepMenuOpen() + private GuiWidget ShowGridOptions(ThemeConfig theme) { - return false; + var popupMenu = new PopupMenu(ApplicationController.Instance.MenuTheme); + + var siblingList = new List(); + + popupMenu.CreateBoolMenuItem( + "Off".Localize(), + () => interactionLayer.SnapGridDistance == 0, + (isChecked) => + { + interactionLayer.SnapGridDistance = 0; + }, + useRadioStyle: true, + siblingRadioButtonList: siblingList); + + var snapSettings = new List() + { + .1, .25, .5, 1, 2, 5 + }; + + foreach(var snap in snapSettings) + { + popupMenu.CreateBoolMenuItem( + snap.ToString(), + () => interactionLayer.SnapGridDistance == snap, + (isChecked) => + { + interactionLayer.SnapGridDistance = snap; + }, + useRadioStyle: true, + siblingRadioButtonList: siblingList); + } + + // Override menu left padding to improve radio circle -> icon spacing + foreach (var menuItem in popupMenu.Children) + { + //menuItem.Padding = menuItem.Padding.Clone(left: 25); + } + + return popupMenu; } } } \ No newline at end of file diff --git a/MatterControlLib/PartPreviewWindow/View3D/InteractionLayer.cs b/MatterControlLib/PartPreviewWindow/View3D/InteractionLayer.cs index a96e069da..1698a00df 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/InteractionLayer.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/InteractionLayer.cs @@ -320,7 +320,22 @@ namespace MatterHackers.MatterControl.PartPreviewWindow public InteractionVolume SelectedInteractionVolume { get; set; } = null; public InteractionVolume HoveredInteractionVolume { get; set; } = null; - public double SnapGridDistance { get; set; } = 1; + public double SnapGridDistance + { + get + { + if(string.IsNullOrEmpty(UserSettings.Instance.get(UserSettingsKey.SnapGridDistance))) + { + return 1; + } + return UserSettings.Instance.GetValue(UserSettingsKey.SnapGridDistance); + } + + set + { + UserSettings.Instance.set(UserSettingsKey.SnapGridDistance, value.ToString()); + } + } public GuiWidget GuiSurface => this; diff --git a/MatterControlLib/PartPreviewWindow/View3D/InteractionVolume.cs b/MatterControlLib/PartPreviewWindow/View3D/InteractionVolume.cs index 62ed74dae..b0dfb5ba8 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/InteractionVolume.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/InteractionVolume.cs @@ -186,13 +186,13 @@ namespace MatterHackers.MeshVisualizer InteractionVolume SelectedInteractionVolume { get; } InteractiveScene Scene { get; } WorldView World { get; } - double SnapGridDistance { get; } GuiWidget GuiSurface { get; } void AddTransformSnapshot(Matrix4X4 originalTransform); List InteractionVolumes { get; } + double SnapGridDistance { get; } } public class InteractionVolumePlugin diff --git a/MatterControlLib/PartPreviewWindow/View3D/View3DWidget.cs b/MatterControlLib/PartPreviewWindow/View3D/View3DWidget.cs index 25997e703..d0ef66236 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/View3DWidget.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/View3DWidget.cs @@ -287,7 +287,18 @@ namespace MatterHackers.MatterControl.PartPreviewWindow ApplicationController.Instance.GetViewOptionButtons(viewOptionsBar, sceneContext, printer, theme); // now add the grid snap button - viewOptionsBar.AddChild(new GridOptionsPanel(this.InteractionLayer, theme)); + var gridSnapButton = new GridOptionsPanel(InteractionLayer, theme) + { + ToolTipText = "Snap Grid".Localize(), + PopupMate = new MatePoint() + { + Mate = new MateOptions(MateEdge.Right, MateEdge.Top) + } + }; + gridSnapButton.AnchorMate.Mate.VerticalEdge = MateEdge.Bottom; + gridSnapButton.AnchorMate.Mate.HorizontalEdge = MateEdge.Right; + + viewOptionsBar.AddChild(gridSnapButton); var interactionVolumes = this.InteractionLayer.InteractionVolumes; interactionVolumes.Add(new MoveInZControl(this.InteractionLayer)); diff --git a/MatterControlLib/SettingsManagement/UserSettings.cs b/MatterControlLib/SettingsManagement/UserSettings.cs index 6f8e12c4c..ed93e1638 100644 --- a/MatterControlLib/SettingsManagement/UserSettings.cs +++ b/MatterControlLib/SettingsManagement/UserSettings.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Numerics; using System.Reflection; using MatterHackers.Agg; using MatterHackers.Agg.UI; @@ -24,6 +25,7 @@ namespace MatterHackers.MatterControl public const string CredentialsInvalid = nameof(CredentialsInvalid); public const string CredentialsInvalidReason = nameof(CredentialsInvalidReason); public const string defaultRenderSetting = nameof(defaultRenderSetting); + public const string SnapGridDistance = nameof(SnapGridDistance); public const string DisplayedTip_LoadFilament = nameof(DisplayedTip_LoadFilament); public const string EditorPanelExpanded = nameof(EditorPanelExpanded); public const string GCodeLineColorStyle = nameof(GCodeLineColorStyle); @@ -140,6 +142,35 @@ namespace MatterHackers.MatterControl public UserSettingsFields Fields { get; private set; } = new UserSettingsFields(); + /// + ///Returns the first matching value discovered while enumerating the settings layers + /// + public T GetValue(string settingsKey) where T : IConvertible + { + if (typeof(T) == typeof(string)) + { + // this way we can use the common pattern without error + return (T)(object)this.get(settingsKey); + } + else if (typeof(T) == typeof(bool)) + { + return (T)(object)(this.get(settingsKey) == "1"); + } + else if (typeof(T) == typeof(int)) + { + int result; + int.TryParse(this.get(settingsKey), out result); + return (T)(object)(result); + } + else if (typeof(T) == typeof(double)) + { + double.TryParse(this.get(settingsKey), out double result); + return (T)(object)(result); + } + + return (T)default(T); + } + public string get(string key) { UserSetting userSetting; diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index dfdc31ec9..e7cdad21e 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit dfdc31ec94b0a3d79e0061c5ea22ac215448b956 +Subproject commit e7cdad21ee05c90084993ca7f1375aab84d9080f