Get grid options menu to be an icon

Save and load grid options from user settings
This commit is contained in:
Lars Brubaker 2018-09-17 17:07:42 -07:00
parent 4d44a2490d
commit 4e9a7fdfd8
6 changed files with 115 additions and 43 deletions

View file

@ -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<double, string>()
{
{ 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<GuiWidget>();
popupMenu.CreateBoolMenuItem(
"Off".Localize(),
() => interactionLayer.SnapGridDistance == 0,
(isChecked) =>
{
interactionLayer.SnapGridDistance = 0;
},
useRadioStyle: true,
siblingRadioButtonList: siblingList);
var snapSettings = new List<double>()
{
.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;
}
}
}

View file

@ -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<double>(UserSettingsKey.SnapGridDistance);
}
set
{
UserSettings.Instance.set(UserSettingsKey.SnapGridDistance, value.ToString());
}
}
public GuiWidget GuiSurface => this;

View file

@ -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<InteractionVolume> InteractionVolumes { get; }
double SnapGridDistance { get; }
}
public class InteractionVolumePlugin

View file

@ -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));

View file

@ -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();
///<summary>
///Returns the first matching value discovered while enumerating the settings layers
///</summary>
public T GetValue<T>(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;

@ -1 +1 @@
Subproject commit dfdc31ec94b0a3d79e0061c5ea22ac215448b956
Subproject commit e7cdad21ee05c90084993ca7f1375aab84d9080f