WIP: button widget
This commit is contained in:
@@ -32,6 +32,13 @@ public struct Margin
|
||||
}
|
||||
|
||||
public static Margin Zero => new Margin(0);
|
||||
|
||||
public static Rect operator +(Margin margin, Rect rect) =>
|
||||
new Rect(rect.Width + margin.Left + margin.Right,
|
||||
rect.Height + margin.Top + margin.Bottom);
|
||||
|
||||
public static Rect operator +(Rect rect, Margin margin) =>
|
||||
margin + rect;
|
||||
}
|
||||
|
||||
public class MarginContainer : Container
|
||||
|
||||
@@ -150,6 +150,8 @@ public class UISystem : IUpdatableSystem, IRenderableSystem
|
||||
{
|
||||
var element = elements[i];
|
||||
|
||||
// if (!element.ContainsPoint(context.MousePosition)) continue;
|
||||
|
||||
if (element is IInputElement inputElement && !inputElement.IgnoreInput)
|
||||
{
|
||||
inputElement.Input(context);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Numerics;
|
||||
using Voile.Input;
|
||||
using Voile.Rendering;
|
||||
using Voile.Resources;
|
||||
using Voile.UI.Containers;
|
||||
|
||||
namespace Voile.UI.Widgets;
|
||||
|
||||
@@ -17,13 +19,42 @@ public enum ButtonState
|
||||
/// </summary>
|
||||
public class Button : Widget
|
||||
{
|
||||
public string Label { get; set; } = "Button";
|
||||
public override Rect MinimumSize => new Rect(Width: 128.0f, Height: 64.0f);
|
||||
|
||||
public Button(string label, Action pressedAction)
|
||||
public string Text
|
||||
{
|
||||
Label = label;
|
||||
get => _text; set
|
||||
{
|
||||
_text = value;
|
||||
MarkDirty();
|
||||
}
|
||||
}
|
||||
public override Rect MinimumSize => Padding + _textSize;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="FontSet"/> to use with this button.
|
||||
/// </summary>
|
||||
public FontSet FontSet { get; set; } = new();
|
||||
public Margin Padding { get; set; } = Margin.Zero;
|
||||
|
||||
public Button(string text, ResourceRef<Font> fontOverride, Action pressedAction)
|
||||
{
|
||||
_text = text;
|
||||
_pressedAction = pressedAction;
|
||||
|
||||
FontSet.AddFont(fontOverride);
|
||||
|
||||
MarkDirty();
|
||||
Update();
|
||||
}
|
||||
|
||||
public Button(string text, FontSet fontSet, Action pressedAction)
|
||||
{
|
||||
_text = text;
|
||||
_pressedAction = pressedAction;
|
||||
|
||||
FontSet = fontSet;
|
||||
|
||||
MarkDirty();
|
||||
Update();
|
||||
}
|
||||
|
||||
public override void Render(RenderSystem renderer, Style style)
|
||||
@@ -31,17 +62,46 @@ public class Button : Widget
|
||||
// TODO: use a button color from style.
|
||||
renderer.SetTransform(GlobalPosition, Vector2.Zero);
|
||||
renderer.DrawRectangle(new Vector2(MinimumSize.Width, MinimumSize.Height), new Color(0.25f, 0.25f, 0.25f));
|
||||
|
||||
var textPosition = new Vector2(GlobalPosition.X + Padding.Left, GlobalPosition.Y + Padding.Top);
|
||||
renderer.SetTransform(textPosition, Vector2.Zero);
|
||||
renderer.DrawText(_suitableFont, _text, Color.White);
|
||||
}
|
||||
|
||||
protected override void OnInput(UIInputContext action)
|
||||
{
|
||||
|
||||
if (action.MouseReleased && ContainsPoint(action.MousePosition))
|
||||
{
|
||||
_pressedAction?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
ResourceRef<Font> fontRef = ResourceRef<Font>.Empty();
|
||||
foreach (var c in _text)
|
||||
{
|
||||
if (FontSet.TryGetFontFor(c, out var fallbackFont))
|
||||
{
|
||||
if (fallbackFont != fontRef)
|
||||
{
|
||||
fontRef = fallbackFont;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_suitableFont = fontRef;
|
||||
|
||||
var font = _suitableFont.Value;
|
||||
_textSize = font.Measure(_text);
|
||||
|
||||
Size = Padding + _textSize;
|
||||
}
|
||||
|
||||
private Action _pressedAction;
|
||||
|
||||
private ResourceRef<Font> _suitableFont = ResourceRef<Font>.Empty();
|
||||
|
||||
private string _text = "Hello, World!";
|
||||
private Rect _textSize = Rect.Zero;
|
||||
}
|
||||
Reference in New Issue
Block a user