mattercontrol/ControlElements/LinkButtonFactory.cs

153 lines
4.4 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;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl
{
2015-04-08 15:20:10 -07:00
public class ChangeTextColorEventArgs : EventArgs
{
2017-10-31 11:43:25 -07:00
public Color color;
2015-04-08 15:20:10 -07:00
2017-10-31 11:43:25 -07:00
public ChangeTextColorEventArgs(Color color)
2015-04-08 15:20:10 -07:00
{
this.color = color;
}
}
//Base widget for use in ButtonStatesViewWidget
public class LinkButtonViewBase : GuiWidget
{
2017-10-31 11:43:25 -07:00
protected Color fillColor = new Color(0, 0, 0, 0);
protected Color borderColor = new Color(0, 0, 0, 0);
2015-04-08 15:20:10 -07:00
protected double borderWidth = 0;
protected double borderRadius;
protected double padding;
protected bool isUnderlined = false;
2017-10-31 11:43:25 -07:00
public Color TextColor { get; set; }
2015-04-08 15:20:10 -07:00
private TextWidget buttonText;
public LinkButtonViewBase(string label,
double textHeight,
double padding,
2017-10-31 11:43:25 -07:00
Color textColor,
2015-04-08 15:20:10 -07:00
bool isUnderlined = false)
: base()
{
this.padding = padding;
this.TextColor = textColor;
this.isUnderlined = isUnderlined;
buttonText = new TextWidget(label, pointSize: textHeight);
buttonText.VAnchor = VAnchor.Center;
buttonText.HAnchor = HAnchor.Center;
2015-04-08 15:20:10 -07:00
buttonText.TextColor = this.TextColor;
//this.AnchorAll();
this.AddChild(buttonText);
HAnchor = HAnchor.Fit;
VAnchor = VAnchor.Fit;
2015-04-08 15:20:10 -07:00
}
public override void SendToChildren(object objectToRoute)
2015-04-08 15:20:10 -07:00
{
var changeColorEvent = objectToRoute as ChangeTextColorEventArgs;
2015-04-08 15:20:10 -07:00
if (changeColorEvent != null)
{
buttonText.TextColor = changeColorEvent.color;
}
base.SendToChildren(objectToRoute);
2015-04-08 15:20:10 -07:00
}
public override void OnDraw(Graphics2D graphics2D)
2015-04-08 15:20:10 -07:00
{
RectangleDouble Bounds = LocalBounds;
RoundedRect rectBorder = new RoundedRect(Bounds, this.borderRadius);
graphics2D.Render(rectBorder, borderColor);
RectangleDouble insideBounds = Bounds;
insideBounds.Inflate(-this.borderWidth);
RoundedRect rectInside = new RoundedRect(insideBounds, Math.Max(this.borderRadius - this.borderWidth, 0));
graphics2D.Render(rectInside, this.fillColor);
if (this.isUnderlined)
{
//Printer.TypeFaceStyle.DoUnderline = true;
RectangleDouble underline = new RectangleDouble(LocalBounds.Left, LocalBounds.Bottom, LocalBounds.Right, LocalBounds.Bottom);
graphics2D.Rectangle(underline, buttonText.TextColor);
}
base.OnDraw(graphics2D);
}
}
public class LinkButtonFactory
{
public double fontSize = 14;
public double padding = 3;
2017-10-31 11:43:25 -07:00
public Color fillColor = new Color(63, 63, 70, 0);
public Color borderColor = new Color(37, 37, 38, 0);
public Color textColor = ActiveTheme.Instance.PrimaryAccentColor;
2015-04-08 15:20:10 -07:00
public BorderDouble margin = new BorderDouble(0, 3);
public Button Generate(string buttonText)
{
//Widgets to show during the four button states
LinkButtonViewBase buttonWidgetPressed = getButtonWidgetPressed(buttonText);
LinkButtonViewBase buttonWidgetHover = getButtonWidgetHover(buttonText);
LinkButtonViewBase buttonWidgetNormal = getButtonWidgetNormal(buttonText);
LinkButtonViewBase buttonWidgetDisabled = getButtonWidgetDisabled(buttonText);
//Create container for the three state widgets for the button
ButtonViewStates buttonViewWidget = new ButtonViewStates(buttonWidgetNormal, buttonWidgetHover, buttonWidgetPressed, buttonWidgetDisabled);
//Create button based on view container widget
2017-06-19 18:52:10 -07:00
return new Button(0, 0, buttonViewWidget)
{
Margin = margin,
Cursor = Cursors.Hand,
};
2015-04-08 15:20:10 -07:00
}
private LinkButtonViewBase getButtonWidgetPressed(string buttonText)
{
LinkButtonViewBase widget = new LinkButtonViewBase(buttonText,
this.fontSize,
this.padding,
this.textColor);
return widget;
}
private LinkButtonViewBase getButtonWidgetHover(string buttonText)
{
LinkButtonViewBase widget = new LinkButtonViewBase(buttonText,
this.fontSize,
this.padding,
this.textColor);
return widget;
}
public LinkButtonViewBase getButtonWidgetNormal(string buttonText)
{
LinkButtonViewBase widget = new LinkButtonViewBase(buttonText,
this.fontSize,
this.padding,
this.textColor,
true);
return widget;
}
private LinkButtonViewBase getButtonWidgetDisabled(string buttonText)
{
LinkButtonViewBase widget = new LinkButtonViewBase(buttonText,
this.fontSize,
this.padding,
this.textColor);
return widget;
}
}
}