Remove and replace DynamicDropDownMenu with DropDownMenu

This commit is contained in:
John Lewin 2017-05-31 12:42:44 -07:00
parent 2d737b679c
commit 565fa3606f
11 changed files with 228 additions and 284 deletions

View file

@ -29,6 +29,7 @@ either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using MatterHackers.Agg.VertexSource;
using MatterHackers.VectorMath;
@ -41,14 +42,18 @@ namespace MatterHackers.Agg.UI
private GuiWidget mainControlWidget;
public BorderDouble MenuItemsPadding { get; set; }
private List<NamedAction> menuActions = null;
TextWidget textWidget = null;
public DropDownMenu(string topMenuText, Direction direction = Direction.Down, double pointSize = 12)
: base(direction)
{
TextWidget textWidget = new TextWidget(topMenuText, pointSize: pointSize);
textWidget.TextColor = this.TextColor;
TextColorChanged += (s, e) => textWidget.TextColor = this.TextColor;
textWidget = new TextWidget(topMenuText, pointSize: pointSize)
{
TextColor = this.TextColor
};
textWidget.AutoExpandBoundsToText = true;
this.Name = topMenuText + " Menu";
@ -61,12 +66,30 @@ namespace MatterHackers.Agg.UI
SetStates(topMenuContent);
}
public List<NamedAction> MenuActions
{
get => menuActions;
set
{
menuActions = value;
if (menuActions != null)
{
foreach (var action in menuActions)
{
this.AddItem(action.Title);
}
}
}
}
public bool MenuAsWideAsItems { get; set; } = true;
public bool DrawDirectionalArrow { get; set; } = true;
public int BorderWidth { get; set; } = 1;
public BorderDouble MenuItemsPadding { get; set; }
public RGBA_Bytes BorderColor { get; set; }
public RGBA_Bytes NormalArrowColor { get; set; }
@ -77,15 +100,24 @@ namespace MatterHackers.Agg.UI
public RGBA_Bytes HoverColor { get; set; }
RGBA_Bytes textColor = RGBA_Bytes.Black;
private RGBA_Bytes textColor = RGBA_Bytes.Black;
public RGBA_Bytes TextColor
{
get { return textColor; }
set { if (value != textColor) { textColor = value; TextColorChanged?.Invoke(this, null); } }
set
{
if (value != textColor)
{
textColor = value;
if (textWidget != null)
{
textWidget.TextColor = textColor;
}
}
}
}
public event EventHandler TextColorChanged;
private int selectedIndex = -1;
public int SelectedIndex
@ -99,10 +131,7 @@ namespace MatterHackers.Agg.UI
}
}
public String SelectedValue
{
get { return GetValue(SelectedIndex); }
}
public string SelectedValue => GetValue(SelectedIndex);
public string GetValue(int itemIndex)
{
@ -121,9 +150,6 @@ namespace MatterHackers.Agg.UI
HAnchor = HAnchor.FitToChildren;
VAnchor = VAnchor.FitToChildren;
MouseEnter += new EventHandler(DropDownList_MouseEnter);
MouseLeave += new EventHandler(DropDownList_MouseLeave);
//IE Don't show arrow unless color is set explicitly
NormalArrowColor = new RGBA_Bytes(255, 255, 255, 0);
HoverArrowColor = TextColor;
@ -135,22 +161,33 @@ namespace MatterHackers.Agg.UI
base.DropListItems_Closed(sender, e);
}
private void DropDownList_MouseLeave(object sender, EventArgs e)
public override void OnMouseLeave(MouseEventArgs mouseEvent)
{
if (!this.IsOpen)
{
BackgroundColor = NormalColor;
}
base.OnMouseLeave(mouseEvent);
}
private void DropDownList_MouseEnter(object sender, EventArgs e)
public override void OnMouseEnter(MouseEventArgs mouseEvent)
{
BackgroundColor = HoverColor;
base.OnMouseEnter(mouseEvent);
}
private void OnSelectionChanged(EventArgs e)
{
SelectionChanged?.Invoke(this, e);
if (MenuActions != null
&& this.selectedIndex <= MenuActions.Count - 1)
{
MenuActions[this.selectedIndex].Action?.Invoke();
}
else
{
SelectionChanged?.Invoke(this, e);
}
}
private void MenuItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
@ -179,9 +216,9 @@ namespace MatterHackers.Agg.UI
foreach (MenuItem item in e.NewItems)
{
item.MinimumSize = new Vector2(minSize.x, item.MinimumSize.y);
// remove it if it is there so we don't have two. It is ok to remove a delagate that is not present.
item.Selected -= new EventHandler(item_Selected);
item.Selected += new EventHandler(item_Selected);
// remove it if it is there so we don't have two. It is ok to remove a delegate that is not present.
item.Selected -= item_Selected;
item.Selected += item_Selected;
}
}
@ -275,24 +312,27 @@ namespace MatterHackers.Agg.UI
{
value = name;
}
if (mainControlWidget.Text != "")
{
mainControlWidget.Margin = MenuItemsPadding;
}
//MenuItem menuItem = new MenuItem(new MenuItemStatesView(normalTextWithMargin, hoverTextWithMargin), value);
MenuItem menuItem = new MenuItem(new MenuItemColorStatesView(name)
{
NormalBackgroundColor = MenuItemsBackgroundColor,
OverBackgroundColor = MenuItemsBackgroundHoverColor,
var menuItem = new MenuItem(
new MenuItemColorStatesView(name)
{
NormalBackgroundColor = MenuItemsBackgroundColor,
OverBackgroundColor = MenuItemsBackgroundHoverColor,
NormalTextColor = MenuItemsTextColor,
OverTextColor = MenuItemsTextHoverColor,
DisabledTextColor = RGBA_Bytes.Gray,
NormalTextColor = MenuItemsTextColor,
OverTextColor = MenuItemsTextHoverColor,
DisabledTextColor = RGBA_Bytes.Gray,
PointSize = pointSize,
Padding = MenuItemsPadding,
},
value);
PointSize = pointSize,
Padding = MenuItemsPadding,
}, value);
menuItem.Text = name;
menuItem.Name = name + " Menu Item";
MenuItems.Add(menuItem);

View file

@ -1,89 +0,0 @@
/*
Copyright (c) 2015, Kevin Pope
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
namespace MatterHackers.Agg.UI
{
public class DynamicDropDownMenu : DropDownMenu
{
private List<Action> menuItems;
public DynamicDropDownMenu(GuiWidget buttonView, Direction direction = Direction.Down, double pointSize = 12)
: base(buttonView, direction, pointSize)
{
menuItems = new List<Action>();
TextColor = RGBA_Bytes.Black;
NormalArrowColor = RGBA_Bytes.Black;
HoverArrowColor = RGBA_Bytes.Black;
BorderWidth = 1;
BorderColor = new RGBA_Bytes(ActiveTheme.Instance.PrimaryTextColor, 200);
this.SelectionChanged += AltChoices_SelectionChanged;
}
public DynamicDropDownMenu(string topMenuText, Direction direction = Direction.Down, double pointSize = 12)
: base(topMenuText, direction, pointSize)
{
menuItems = new List<Action>();
NormalColor = RGBA_Bytes.White;
TextColor = RGBA_Bytes.Black;
NormalArrowColor = RGBA_Bytes.Black;
HoverArrowColor = RGBA_Bytes.Black;
HoverColor = new RGBA_Bytes(255, 255, 255, 200);
BorderWidth = 1;
BorderColor = new RGBA_Bytes(ActiveTheme.Instance.PrimaryTextColor, 200);
this.SelectionChanged += AltChoices_SelectionChanged;
}
public override void OnDraw(Graphics2D graphics2D)
{
base.OnDraw(graphics2D);
}
public void addItem(string name, Action clickFunction)
{
this.AddItem(name);
menuItems.Add(clickFunction);
}
private void AltChoices_SelectionChanged(object sender, EventArgs e)
{
var dropDownMenu = sender as DropDownMenu;
var action = menuItems[dropDownMenu.SelectedIndex];
action();
}
}
}

View file

@ -29,6 +29,7 @@ either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
namespace MatterHackers.Agg.UI
{
@ -37,4 +38,16 @@ namespace MatterHackers.Agg.UI
public string Title { get; set; }
public Action Action { get; set; }
}
public static class NamedActionExtensions
{
public static void Add(this List<NamedAction> list, string title, Action action)
{
list.Add(new NamedAction()
{
Title = title,
Action = action
});
}
}
}

View file

@ -1,71 +1,97 @@
using MatterHackers.Agg;
/*
Copyright (c) 2017, Matt Moening, Lars Brubaker, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using System;
namespace MatterHackers.MatterControl
{
public class SplitButton : FlowLayoutWidget
{
private Button defaultButton;
private DynamicDropDownMenu altChoices;
private DropDownMenu altChoices;
private Button DefaultButton { get { return defaultButton; } }
private Button DefaultButton { get; }
public SplitButton(string buttonText, Direction direction = Direction.Down)
: base(FlowDirection.LeftToRight)
{
HAnchor = HAnchor.FitToChildren;
VAnchor = VAnchor.FitToChildren;
defaultButton = CreateDefaultButton(buttonText);
this.DefaultButton = CreateDefaultButton(buttonText);
this.DefaultButton.VAnchor = VAnchor.ParentCenter;
altChoices = CreateDropDown(direction);
defaultButton.VAnchor = VAnchor.ParentCenter;
AddChild(defaultButton);
AddChild(this.DefaultButton);
AddChild(altChoices);
}
public SplitButton(Button button, DynamicDropDownMenu menu)
public SplitButton(Button button, DropDownMenu menu)
: base(FlowDirection.LeftToRight)
{
HAnchor = HAnchor.FitToChildren;
VAnchor = VAnchor.FitToChildren;
defaultButton = button;
this.DefaultButton = button;
this.DefaultButton.VAnchor = VAnchor.ParentCenter;
altChoices = menu;
defaultButton.VAnchor = VAnchor.ParentCenter;
AddChild(defaultButton);
AddChild(this.DefaultButton);
AddChild(altChoices);
}
public void AddItem(string name, Action clickFunction)
private DropDownMenu CreateDropDown(Direction direction)
{
altChoices.addItem(name, clickFunction);
}
private DynamicDropDownMenu CreateDropDown(Direction direction)
{
DynamicDropDownMenu menu = new DynamicDropDownMenu("", direction);
menu.VAnchor = VAnchor.ParentCenter;
menu.MenuAsWideAsItems = false;
menu.AlignToRightEdge = true;
menu.Height = defaultButton.Height;
return menu;
return new DropDownMenu("", direction)
{
VAnchor = VAnchor.ParentCenter,
MenuAsWideAsItems = false,
AlignToRightEdge = true,
Height = this.DefaultButton.Height
};
}
private Button CreateDefaultButton(string buttonText)
{
TextImageButtonFactory buttonFactory = new TextImageButtonFactory();
buttonFactory.FixedHeight = 30 * GuiWidget.DeviceScale;
buttonFactory.normalFillColor = RGBA_Bytes.White;
buttonFactory.normalTextColor = RGBA_Bytes.Black;
buttonFactory.hoverTextColor = RGBA_Bytes.Black;
buttonFactory.hoverFillColor = new RGBA_Bytes(255, 255, 255, 200);
buttonFactory.borderWidth = 1;
buttonFactory.normalBorderColor = new RGBA_Bytes(ActiveTheme.Instance.PrimaryTextColor, 200);
buttonFactory.hoverBorderColor = new RGBA_Bytes(ActiveTheme.Instance.PrimaryTextColor, 200);
var buttonFactory = new TextImageButtonFactory()
{
FixedHeight = 30 * GuiWidget.DeviceScale,
normalFillColor = RGBA_Bytes.White,
normalTextColor = RGBA_Bytes.Black,
hoverTextColor = RGBA_Bytes.Black,
hoverFillColor = new RGBA_Bytes(255, 255, 255, 200),
borderWidth = 1,
normalBorderColor = new RGBA_Bytes(ActiveTheme.Instance.PrimaryTextColor, 200),
hoverBorderColor = new RGBA_Bytes(ActiveTheme.Instance.PrimaryTextColor, 200)
};
return buttonFactory.Generate(buttonText, centerText: true);
}