Dashboard/Quik/Controls/Button.cs
H. Utku Maden 9339295378 Push all uncommitted changes.
I have had a long break from this project due to other higher priority
things going on in my life. Big changes inbound.
2023-05-13 16:17:57 +03:00

105 lines
3.0 KiB
C#

using Quik.Typography;
namespace Quik.Controls
{
public class Button : Control
{
public string Text { get; set; } = "Button";
public float Padding { get; set; } = 4.0f;
public QuikFont Font { get; set; }
public QuikStrokeStyle NormalStroke { get; set; }
public QuikFillStyle NormalFill { get; set; }
public QuikStrokeStyle HoverStroke { get; set; }
public QuikFillStyle HoverFill { get; set; }
public QuikStrokeStyle ActiveStroke { get; set; }
public QuikFillStyle ActiveFill { get; set; }
private ButtonClass _class = ButtonClass.Normal;
private enum ButtonClass
{
Normal,
Hover,
Active
}
protected override void OnMouseEnter(MouseMoveEventArgs args)
{
base.OnMouseEnter(args);
if (_class == ButtonClass.Normal)
_class = ButtonClass.Hover;
}
protected override void OnMouseLeave(MouseMoveEventArgs args)
{
base.OnMouseLeave(args);
if (_class == ButtonClass.Hover)
_class = ButtonClass.Normal;
}
protected override void OnMouseDown(MouseButtonEventArgs args)
{
base.OnMouseDown(args);
if (_class == ButtonClass.Hover)
_class = ButtonClass.Active;
}
protected override void OnMouseUp(MouseButtonEventArgs args)
{
base.OnMouseUp(args);
if (_class == ButtonClass.Active)
{
_class = ButtonClass.Hover;
}
}
protected override void OnPaint(QuikDraw draw)
{
QuikRectangle bounds = AbsoluteBounds;
switch (_class)
{
default:
case ButtonClass.Normal:
draw.Commands.Enqueue(new QuikCommandRectangle(bounds) {
StrokeStyle = NormalStroke,
FillStyle = NormalFill
});
break;
case ButtonClass.Hover:
draw.Commands.Enqueue(new QuikCommandRectangle(bounds) {
StrokeStyle = HoverStroke,
FillStyle = HoverFill
});
break;
case ButtonClass.Active:
draw.Commands.Enqueue(new QuikCommandRectangle(bounds) {
StrokeStyle = ActiveStroke,
FillStyle = ActiveFill
});
break;
}
// Position the text so that it is centered.
float ascender = Root.Context.DefaultFont.Ascender;
float descender = -Root.Context.DefaultFont.Descender;
QuikVec2 position =
bounds.Min +
new QuikVec2(
Padding,
(
(bounds.Size.Y - ascender - descender) /
2)
+ descender);
draw.PutText(Text, position);
}
}
}