mattercontrol/CustomWidgets/ClickWidget.cs

118 lines
2.3 KiB
C#
Raw Normal View History

2015-04-08 15:20:10 -07:00
using MatterHackers.Agg;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
2015-04-08 15:20:10 -07:00
using System;
using System.Collections.Generic;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl
{
2015-04-08 15:20:10 -07:00
// A clickable GuiWidget
public class ClickWidget : GuiWidget
{
private event EventHandler PrivateClick;
private int borderWidth = 0;
private RGBA_Bytes borderColor = RGBA_Bytes.Black;
public ClickWidget()
: base()
{
}
public ClickWidget(double width, double height)
: base(width, height)
{
}
public int BorderWidth
{
get { return borderWidth; }
set
{
this.borderWidth = value;
this.Invalidate();
}
}
public RGBA_Bytes BorderColor
{
get { return borderColor; }
set
{
this.borderColor = value;
this.Invalidate();
}
}
private List<EventHandler> ClickEventDelegates = new List<EventHandler>();
public event EventHandler Click
{
//Wraps the PrivateClick event delegate so that we can track which events have been added and clear them if necessary
add
{
PrivateClick += value;
ClickEventDelegates.Add(value);
}
remove
{
PrivateClick -= value;
ClickEventDelegates.Remove(value);
}
}
public void UnbindClickEvents()
{
//Clears all event handlers from the Click event
foreach (EventHandler eh in ClickEventDelegates)
{
PrivateClick -= eh;
}
ClickEventDelegates.Clear();
}
public void ClickButton(MouseEventArgs mouseEvent)
{
if (PrivateClick != null)
{
UiThread.RunOnIdle(() => PrivateClick(this, mouseEvent));
2015-04-08 15:20:10 -07:00
}
}
public bool GetChildClicks = false;
2015-04-08 15:20:10 -07:00
override public void OnMouseUp(MouseEventArgs mouseEvent)
{
if (PositionWithinLocalBounds(mouseEvent.X, mouseEvent.Y))
2015-04-08 15:20:10 -07:00
{
if (GetChildClicks || this.MouseCaptured == true)
{
ClickButton(mouseEvent);
}
2015-04-08 15:20:10 -07:00
}
base.OnMouseUp(mouseEvent);
}
public override void OnDraw(Graphics2D graphics2D)
{
RectangleDouble borderRectangle = LocalBounds;
if (BorderWidth > 0)
{
if (BorderWidth == 1)
{
graphics2D.Rectangle(borderRectangle, BorderColor);
}
else
{
RoundedRect rectBorder = new RoundedRect(borderRectangle, 0);
graphics2D.Render(new Stroke(rectBorder, BorderWidth), BorderColor);
}
}
base.OnDraw(graphics2D);
}
}
}