70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Numerics;
|
|
using Dashboard.Drawing;
|
|
using Dashboard.Layout;
|
|
using Dashboard.Pal;
|
|
|
|
namespace Dashboard.Controls
|
|
{
|
|
public class Button : Control
|
|
{
|
|
private Vector2 _intrinsicSize = Vector2.Zero;
|
|
|
|
public bool AutoSize { get; set; } = true;
|
|
public string Text { get; set; } = "Click!";
|
|
|
|
public Font Font { get; set; } = Font.Create(new FontInfo("Rec Mono Linear"));
|
|
public float TextSize { get; set; } = 12f;
|
|
public Brush TextBrush { get; set; } = new SolidColorBrush(Color.Black);
|
|
public Brush ButtonBrush { get; set; } = new SolidColorBrush(Color.DarkSlateGray);
|
|
|
|
public Vector2 Padding { get; set; } = new Vector2(4, 4);
|
|
|
|
public event EventHandler? Clicked;
|
|
|
|
public override Vector2 CalculateIntrinsicSize()
|
|
{
|
|
return _intrinsicSize + 2 * Padding;
|
|
}
|
|
|
|
protected void CalculateSize(DeviceContext dc)
|
|
{
|
|
Box2d box = dc.ExtensionRequire<ITextRenderer>().MeasureText(Font.Base, TextSize, Text);
|
|
_intrinsicSize = box.Size;
|
|
// Layout.Size = box.Size;
|
|
// ClientArea = new Box2d(ClientArea.Min, ClientArea.Min + Layout.Size);
|
|
}
|
|
|
|
public override void OnPaint(DeviceContext dc)
|
|
{
|
|
base.OnPaint(dc);
|
|
|
|
if (AutoSize)
|
|
CalculateSize(dc);
|
|
|
|
bool hidden = Layout.OverflowMode == OverflowMode.Hidden;
|
|
var dcb = dc.ExtensionRequire<IDeviceContextBase>();
|
|
if (hidden)
|
|
dcb.PushScissor(ClientArea);
|
|
|
|
dcb.PushTransforms(Matrix4x4.CreateTranslation(ClientArea.Left, ClientArea.Top, 0));
|
|
|
|
var imm = dc.ExtensionRequire<IImmediateMode>();
|
|
Color color = (ButtonBrush as SolidColorBrush)?.Color ?? Color.Black;
|
|
Vector4 colorVector = new Vector4(color.R / 255f, color.G / 255f, color.B / 255f, color.A / 255f);
|
|
imm.Rectangle(ClientArea, 0, colorVector);
|
|
|
|
var text = dc.ExtensionRequire<ITextRenderer>();
|
|
color = (TextBrush as SolidColorBrush)?.Color ?? Color.Black;
|
|
colorVector = new Vector4(color.R / 255f, color.G / 255f, color.B / 255f, color.A / 255f);
|
|
text.DrawText(Vector2.Zero, colorVector, TextSize, Font.Base, Text);
|
|
|
|
if (hidden)
|
|
dcb.PopScissor();
|
|
|
|
dcb.PopTransforms();
|
|
}
|
|
}
|
|
}
|