Compare commits
2 Commits
b810e1b882
...
09c24e7123
| Author | SHA1 | Date | |
|---|---|---|---|
| 09c24e7123 | |||
| 87e0a69dcf |
@@ -20,7 +20,7 @@ public class TestGame : Game
|
||||
InitializeSystemsDefault();
|
||||
|
||||
_uiSystem = new UISystem(Input, ResourceRef<Style>.Empty());
|
||||
_uiSystem.RenderDebugRects = true;
|
||||
// _uiSystem.RenderDebugRects = true;
|
||||
|
||||
_particleSystem = new ParticleSystem();
|
||||
|
||||
@@ -63,10 +63,32 @@ public class TestGame : Game
|
||||
Input.AddInputMapping("reload", new IInputAction[] { new KeyInputAction(KeyboardKey.R) });
|
||||
_emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
|
||||
|
||||
// _fillContainer.AddChild(_container);
|
||||
// _marginContainer.AddChild(_container);
|
||||
var addButton = new Button("Add element", _defaultFontSet, () => { _container.AddChild(new Label("Hello, World!", _defaultFontSet)); });
|
||||
addButton.Padding = new Margin(8.0f);
|
||||
|
||||
_uiSystem.AddElement(_container);
|
||||
var removeButton = new Button("Remove element", _defaultFontSet, () =>
|
||||
{
|
||||
if (_container.Children.Count == 0) return;
|
||||
var lastChild = _container.Children.Last();
|
||||
_container.RemoveChild(lastChild);
|
||||
});
|
||||
|
||||
removeButton.Padding = new Margin(8.0f);
|
||||
|
||||
_buttonContainer.AddChild(addButton);
|
||||
_buttonContainer.AddChild(removeButton);
|
||||
|
||||
var c = new VerticalContainer();
|
||||
|
||||
var m = new MarginContainer();
|
||||
m.AddChild(_container);
|
||||
|
||||
c.AddChild(_buttonContainer);
|
||||
c.AddChild(m);
|
||||
|
||||
_rootFill.AddChild(c);
|
||||
|
||||
_uiSystem.AddElement(_rootFill);
|
||||
}
|
||||
|
||||
|
||||
@@ -77,17 +99,6 @@ public class TestGame : Game
|
||||
ResourceManager.Reload();
|
||||
_particleSystem!.RestartEmitter(_emitterId);
|
||||
}
|
||||
|
||||
if (Input.IsActionPressed("accept"))
|
||||
{
|
||||
_container.AddChild(new Label("こんにちは世界!", _defaultFontSet));
|
||||
}
|
||||
|
||||
if (Input.IsActionPressed("cancel") && _container.Children.Count != 0)
|
||||
{
|
||||
var lastChild = _container.Children.Last();
|
||||
_container.RemoveChild(lastChild);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Render(double deltaTime)
|
||||
@@ -146,12 +157,9 @@ public class TestGame : Game
|
||||
|
||||
[NotNull] private Label _label;
|
||||
|
||||
private FillContainer _fillContainer = new();
|
||||
private MarginContainer _marginContainer = new(new Margin(32.0f))
|
||||
private FillContainer _rootFill = new();
|
||||
private HorizontalContainer _buttonContainer = new(16)
|
||||
{
|
||||
ConfineToContents = true,
|
||||
};
|
||||
// private HorizontalContainer _container = new(new Rect(128.0f, 64.0f), new(), 16)
|
||||
// {
|
||||
// ConfineToContents = true,
|
||||
// };
|
||||
}
|
||||
@@ -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
|
||||
@@ -58,7 +65,10 @@ public class MarginContainer : Container
|
||||
base.OnUpdate();
|
||||
if (Parent == null) return;
|
||||
|
||||
Size = Parent.Size;
|
||||
if (Size != Parent.Size)
|
||||
{
|
||||
Size = Parent.Size;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Arrange()
|
||||
|
||||
@@ -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