Compare commits
50 Commits
7e86898e1a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 11423d86e5 | |||
| 828ec4973f | |||
| bef27762ee | |||
| 8a3ed42bb7 | |||
| 52bbf5a9e1 | |||
| 49aa3f071f | |||
| 6c0e6707ee | |||
| c6e1bf7f41 | |||
| 5d3a2c2222 | |||
| e1e965796b | |||
| 207c8a20a4 | |||
| 681496d812 | |||
| 6f3a945f34 | |||
| c18adaeede | |||
| e75dcb7753 | |||
| 09c24e7123 | |||
| 87e0a69dcf | |||
| b810e1b882 | |||
| 26cb66dbe0 | |||
| b3c1db3145 | |||
| 9bc9810c8f | |||
| 0ec4e45c38 | |||
| 6b108ba56c | |||
| e0e8d6e9ff | |||
| 552e05d498 | |||
| 17196c9437 | |||
| 4b2aa31b63 | |||
| 90fe38b017 | |||
| 8a1e359c22 | |||
| 64d3dba42d | |||
| 5bf052db96 | |||
| 389a73cf24 | |||
| d44341974f | |||
| b2f3e1c351 | |||
| 255dea138b | |||
| 9fa6b45cea | |||
| 5871e8966b | |||
| ed9f17e6c4 | |||
| 4362e88eab | |||
| 58efd449a8 | |||
| 78b46cb38e | |||
| 6c3576891e | |||
| 03668849bc | |||
| b228f04670 | |||
| a5d2668c18 | |||
| 9a3512702a | |||
| 61ac079f2b | |||
| 95ae2de7ac | |||
| 683656dee8 | |||
| ae1b612524 |
37
TODO.md
37
TODO.md
@@ -3,6 +3,9 @@
|
|||||||
## Bugfixes
|
## Bugfixes
|
||||||
|
|
||||||
- ActionJustPressed and KeyboardKeyJustPressed don't detect inputs consistently.
|
- ActionJustPressed and KeyboardKeyJustPressed don't detect inputs consistently.
|
||||||
|
- **Solution**: This is a problem related to custom frame pacing for fixed timestep. Raylib polls input in BeginFrame, which means certain functions related to JustPressed* may work incorrectly when polled in a separate timestep. This can be fixed if the entire input system will be moved to SDL or with a custom render + input backend altogether.
|
||||||
|
- ~~Fix any remaining bugs with anchor positioning system.~~
|
||||||
|
- ~~Containers don't position their chilren correctly when using anchors and adding/removing them.~~
|
||||||
|
|
||||||
## Core
|
## Core
|
||||||
|
|
||||||
@@ -27,6 +30,7 @@
|
|||||||
|
|
||||||
- Serialize attribute.
|
- Serialize attribute.
|
||||||
- Add automatic serialization of resources through source generation and System.Text.Json.
|
- Add automatic serialization of resources through source generation and System.Text.Json.
|
||||||
|
- Make sure this serialization system works well with CLR and NativeAOT.
|
||||||
- ~~Provide means for fetching key/value configuration (INI? TOML?)~~
|
- ~~Provide means for fetching key/value configuration (INI? TOML?)~~
|
||||||
- Expose some sort of ConfigFile class for safe key/value configuration fetching.
|
- Expose some sort of ConfigFile class for safe key/value configuration fetching.
|
||||||
|
|
||||||
@@ -65,16 +69,37 @@
|
|||||||
- Make action system use an InputMap resource instead.
|
- Make action system use an InputMap resource instead.
|
||||||
- Gamepad support
|
- Gamepad support
|
||||||
|
|
||||||
|
## Diagnostics
|
||||||
|
|
||||||
|
- Implement Profiler class.
|
||||||
|
- Generate a Speedscope report.
|
||||||
|
- Collect reports for systems
|
||||||
|
- Game
|
||||||
|
- Render
|
||||||
|
- Audio
|
||||||
|
- ResourceManager
|
||||||
|
- UI
|
||||||
|
- Particles
|
||||||
|
|
||||||
## UI
|
## UI
|
||||||
|
|
||||||
- Basic widgets (button, label, text input)
|
- ~~Layout~~
|
||||||
- Layout
|
- ~~Containers~~
|
||||||
- Containers
|
|
||||||
- ~~VerticalContainer~~
|
- ~~VerticalContainer~~
|
||||||
- ~~HorizontalContainer~~
|
- ~~HorizontalContainer~~
|
||||||
- ~~GridContainer~~
|
- ~~GridContainer~~
|
||||||
- ~~FlexContainer~~
|
- ~~FlexContainer~~
|
||||||
- Positioning (anchors)
|
- ~~Positioning (anchors)~~
|
||||||
|
- ~~Move layouting to Render instead of Update, use Update for input.~~
|
||||||
- Input propagation
|
- Input propagation
|
||||||
- Basic input elements (button, text field, toggle).
|
- ~~Pass input to widgets.~~
|
||||||
- Styling.
|
- Add element focus logic, make them focusable with action inputs.
|
||||||
|
- Basic input elements (~~button~~, text field, toggle).
|
||||||
|
- Styling
|
||||||
|
- ~~Style sheet~~
|
||||||
|
- ~~Add style settings for UI panels (for buttons, labels, etc.).~~
|
||||||
|
- ~~Parse StyleSheet from TOML file.~~
|
||||||
|
- Animated styles
|
||||||
|
- (stretch goal) Style variables
|
||||||
|
- Find a way to reference external assets in the style (fonts, textures).
|
||||||
|
- Create a default style for widgets.
|
||||||
|
|||||||
63
TestGame/Resources/default.style.toml
Normal file
63
TestGame/Resources/default.style.toml
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
[Button]
|
||||||
|
BackgroundColor = "#0f62fe"
|
||||||
|
TextColor = "#ffffff"
|
||||||
|
Padding = 16.0
|
||||||
|
|
||||||
|
# Creates an empty rule for Button.Normal.
|
||||||
|
# This will inherit style from Button, this is a temporary workaround.
|
||||||
|
[Button.Normal]
|
||||||
|
|
||||||
|
[Button.Hovered]
|
||||||
|
BackgroundColor = "#0353e9"
|
||||||
|
|
||||||
|
[Button.Pressed]
|
||||||
|
BackgroundColor = "#002d9c"
|
||||||
|
|
||||||
|
[Button.Danger]
|
||||||
|
TextColor = "#ffffff"
|
||||||
|
|
||||||
|
[Button.Danger.Normal]
|
||||||
|
BackgroundColor = "#da1e28"
|
||||||
|
|
||||||
|
[Button.Danger.Hovered]
|
||||||
|
BackgroundColor = "#ba1b23"
|
||||||
|
|
||||||
|
[Button.Danger.Pressed]
|
||||||
|
BackgroundColor = "#750e13"
|
||||||
|
|
||||||
|
[Button.Outline]
|
||||||
|
BackgroundColor = [0, 0, 0, 0]
|
||||||
|
BorderSize = 1.0
|
||||||
|
BorderColor = "#0f62fe"
|
||||||
|
|
||||||
|
[Button.Outline.Normal]
|
||||||
|
TextColor = "#0353e9"
|
||||||
|
|
||||||
|
[Button.Outline.Hovered]
|
||||||
|
BackgroundColor = "#0353e9"
|
||||||
|
|
||||||
|
[Button.Outline.Pressed]
|
||||||
|
BackgroundColor = "#002d9c"
|
||||||
|
BorderColor = [0, 0, 0, 0]
|
||||||
|
|
||||||
|
[Button.Link]
|
||||||
|
BackgroundColor = [0, 0, 0, 0]
|
||||||
|
TextColor = "#0f62fe"
|
||||||
|
Padding = 0.0
|
||||||
|
|
||||||
|
[Button.Link.Normal]
|
||||||
|
|
||||||
|
[Button.Link.Hovered]
|
||||||
|
BorderColor = "#0043ce"
|
||||||
|
TextColor = "#0043ce"
|
||||||
|
BorderSize = [0, 0, 0, 1]
|
||||||
|
|
||||||
|
[Button.Link.Pressed]
|
||||||
|
TextColor = "#161616"
|
||||||
|
BorderSize = [0, 0, 0, 1]
|
||||||
|
BorderColor = "#161616"
|
||||||
|
|
||||||
|
|
||||||
|
# Default background color for all Container derived classes.
|
||||||
|
[Container]
|
||||||
|
BackgroundColor = "#e0e0e0"
|
||||||
BIN
TestGame/Resources/fonts/NotoSansJP-Regular.ttf
Normal file
BIN
TestGame/Resources/fonts/NotoSansJP-Regular.ttf
Normal file
Binary file not shown.
3
TestGame/Resources/fonts/default.fontset.toml
Normal file
3
TestGame/Resources/fonts/default.fontset.toml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[FontSet]
|
||||||
|
|
||||||
|
fonts = ["Inter-Regular.ttf", "NotoSansJP-Regular.ttf"]
|
||||||
@@ -19,8 +19,10 @@ public class TestGame : Game
|
|||||||
{
|
{
|
||||||
InitializeSystemsDefault();
|
InitializeSystemsDefault();
|
||||||
|
|
||||||
_uiSystem = new UISystem(Input, ResourceRef<Style>.Empty());
|
_uiSystem = new UISystem(Input);
|
||||||
_uiSystem.RenderDebugRects = true;
|
// _uiSystem.RenderDebugRects = true;
|
||||||
|
|
||||||
|
ResourceManager.EnableFileWatching();
|
||||||
|
|
||||||
_particleSystem = new ParticleSystem();
|
_particleSystem = new ParticleSystem();
|
||||||
|
|
||||||
@@ -36,12 +38,18 @@ public class TestGame : Game
|
|||||||
protected override void LoadResources()
|
protected override void LoadResources()
|
||||||
{
|
{
|
||||||
ResourceManager.AddResourceLoaderAssociation(new ParticleEmitterSettingsResourceLoader());
|
ResourceManager.AddResourceLoaderAssociation(new ParticleEmitterSettingsResourceLoader());
|
||||||
|
ResourceManager.AddResourceLoaderAssociation(new StyleSheetLoader());
|
||||||
|
|
||||||
if (!ResourceManager.TryLoad("fonts/Inter-Regular.ttf", out _font))
|
if (!ResourceManager.TryLoad("fonts/Inter-Regular.ttf", out _font))
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!ResourceManager.TryLoad("fonts/NotoSansJP-Regular.ttf", out _jpFont))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
ResourceManager.TryLoad("icon.png", out _icon);
|
ResourceManager.TryLoad("icon.png", out _icon);
|
||||||
ResourceManager.TryLoad("sounds/test_sound_mono.ogg", out _sound);
|
ResourceManager.TryLoad("sounds/test_sound_mono.ogg", out _sound);
|
||||||
|
|
||||||
@@ -49,14 +57,57 @@ public class TestGame : Game
|
|||||||
{
|
{
|
||||||
throw new Exception("Failed to load emitter settings!");
|
throw new Exception("Failed to load emitter settings!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ResourceManager.TryLoad("default.style.toml", out _styleSheet))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
_defaultFontSet = new([_font, _jpFont]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Ready()
|
protected override void Ready()
|
||||||
{
|
{
|
||||||
Input.AddInputMapping("reload", new IInputAction[] { new KeyInputAction(KeyboardKey.R) });
|
Input.AddInputMapping("reload", new IInputAction[] { new KeyInputAction(KeyboardKey.R) });
|
||||||
_emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
|
// _emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
|
||||||
|
|
||||||
_uiSystem.AddElement(_container);
|
_uiSystem.SetStyleSheet(_styleSheet);
|
||||||
|
|
||||||
|
var addButton = new Button("Default button", _defaultFontSet);
|
||||||
|
|
||||||
|
var removeButton = new Button("Danger button", _defaultFontSet);
|
||||||
|
|
||||||
|
removeButton.StyleVariant = "Danger";
|
||||||
|
|
||||||
|
var outlineButton = new Button("Outline button", _defaultFontSet);
|
||||||
|
outlineButton.StyleVariant = "Outline";
|
||||||
|
|
||||||
|
var linkButton = new Button("Link button", _defaultFontSet);
|
||||||
|
linkButton.StyleVariant = "Link";
|
||||||
|
|
||||||
|
|
||||||
|
var c = new HorizontalContainer()
|
||||||
|
{
|
||||||
|
StyleVariant = "Layer01",
|
||||||
|
ConfineToContents = true,
|
||||||
|
Anchor = Anchor.TopCenter
|
||||||
|
};
|
||||||
|
|
||||||
|
c.AddChild(addButton);
|
||||||
|
c.AddChild(removeButton);
|
||||||
|
c.AddChild(outlineButton);
|
||||||
|
c.AddChild(linkButton);
|
||||||
|
|
||||||
|
var vc = new VerticalContainer(0.0f);
|
||||||
|
vc.AddChild(c);
|
||||||
|
|
||||||
|
var f = new MarginContainer(new Size(0.0f));
|
||||||
|
f.AddChild(_container);
|
||||||
|
|
||||||
|
vc.AddChild(f);
|
||||||
|
|
||||||
|
_rootFill.AddChild(vc);
|
||||||
|
_uiSystem.AddElement(_rootFill);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -64,29 +115,19 @@ public class TestGame : Game
|
|||||||
{
|
{
|
||||||
if (Input.IsActionPressed("reload"))
|
if (Input.IsActionPressed("reload"))
|
||||||
{
|
{
|
||||||
ResourceManager.Reload();
|
// ResourceManager.Reload();
|
||||||
_particleSystem!.RestartEmitter(_emitterId);
|
// _particleSystem!.RestartEmitter(_emitterId);
|
||||||
}
|
|
||||||
|
|
||||||
if (Input.IsActionPressed("accept"))
|
|
||||||
{
|
|
||||||
_container.AddChild(new RectangleWidget(new Rect(32.0f, 32.0f), MathUtils.RandomColor()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Input.IsActionPressed("cancel") && _container.Children.Count != 0)
|
|
||||||
{
|
|
||||||
var lastChild = _container.Children.Last();
|
|
||||||
_container.RemoveChild(lastChild);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Render(double deltaTime)
|
protected override void Render(double deltaTime)
|
||||||
{
|
{
|
||||||
Renderer.ClearBackground(Color.Black);
|
Renderer.ClearBackground(Color.Black);
|
||||||
foreach (var emitter in _particleSystem!.Emitters)
|
|
||||||
{
|
// foreach (var emitter in _particleSystem!.Emitters)
|
||||||
DrawEmitter(emitter);
|
// {
|
||||||
}
|
// DrawEmitter(emitter);
|
||||||
|
// }
|
||||||
|
|
||||||
Renderer.ResetTransform();
|
Renderer.ResetTransform();
|
||||||
_uiSystem.Render(Renderer);
|
_uiSystem.Render(Renderer);
|
||||||
@@ -116,17 +157,30 @@ public class TestGame : Game
|
|||||||
private int _emitterId;
|
private int _emitterId;
|
||||||
private ResourceRef<ParticleEmitterSettingsResource> _fireEffect;
|
private ResourceRef<ParticleEmitterSettingsResource> _fireEffect;
|
||||||
private ResourceRef<Font> _font;
|
private ResourceRef<Font> _font;
|
||||||
|
private ResourceRef<Font> _jpFont;
|
||||||
|
private ResourceRef<StyleSheet> _styleSheet;
|
||||||
|
|
||||||
|
private FontSet _defaultFontSet;
|
||||||
|
|
||||||
private ResourceRef<Sound> _sound;
|
private ResourceRef<Sound> _sound;
|
||||||
private ResourceRef<Texture2d> _icon;
|
private ResourceRef<Texture2d> _icon;
|
||||||
|
|
||||||
private FlexContainer _container = new(minimumSize: new Rect(64.0f, 64.0f), new())
|
private FlexContainer _container = new(minimumSize: new Rect(256.0f, 256.0f), new())
|
||||||
{
|
{
|
||||||
ConfineToContents = false,
|
Anchor = Anchor.Center,
|
||||||
Size = new Rect(500, 300),
|
Direction = FlexDirection.Column,
|
||||||
Direction = FlexDirection.Row,
|
|
||||||
Justify = JustifyContent.Start,
|
Justify = JustifyContent.Start,
|
||||||
Align = AlignItems.Center,
|
Align = AlignItems.Center,
|
||||||
Wrap = true,
|
Wrap = true,
|
||||||
Gap = 10f
|
Gap = 8.0f,
|
||||||
|
StyleVariant = "Layer02",
|
||||||
|
};
|
||||||
|
|
||||||
|
[NotNull] private Label _label;
|
||||||
|
|
||||||
|
private FillContainer _rootFill = new();
|
||||||
|
private HorizontalContainer _buttonContainer = new(16)
|
||||||
|
{
|
||||||
|
ConfineToContents = true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>WinExe</OutputType>
|
<OutputType>WinExe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>disable</Nullable>
|
<Nullable>disable</Nullable>
|
||||||
<PublishAot>true</PublishAot>
|
<PublishAot>true</PublishAot>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
|
|||||||
46
Voile/GridSet.cs
Normal file
46
Voile/GridSet.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Numerics;
|
||||||
|
using Voile.Extensions;
|
||||||
|
|
||||||
|
namespace Voile;
|
||||||
|
|
||||||
|
public class GridSet<T>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The size of a cell of this <see cref="GridSet"/>.
|
||||||
|
/// </summary>
|
||||||
|
public float CellSize { get; }
|
||||||
|
public GridSet(float cellSize = 32.0f)
|
||||||
|
{
|
||||||
|
CellSize = cellSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Add an element to this <see cref="GridSet"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position">Position of the element in this <see cref="GridSet"/>.</param>
|
||||||
|
/// <param name="child">Element to add.</param>
|
||||||
|
public void Add(Vector2 position, T child)
|
||||||
|
{
|
||||||
|
var snap = Vector2.One * CellSize;
|
||||||
|
position = position.Snapped(snap);
|
||||||
|
|
||||||
|
if (_values.TryGetValue(position, out var list))
|
||||||
|
{
|
||||||
|
list.Add(child);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_values.Add(position, new List<T>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes an element from this <see cref="GridSet"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="child">Element to remove.</param>
|
||||||
|
public void Remove(T child) => throw new NotImplementedException();
|
||||||
|
|
||||||
|
private Dictionary<Vector2, List<T>> _values = new();
|
||||||
|
}
|
||||||
302
Voile/Source/Color.cs
Normal file
302
Voile/Source/Color.cs
Normal file
@@ -0,0 +1,302 @@
|
|||||||
|
namespace Voile
|
||||||
|
{
|
||||||
|
// Based on https://github.com/ppr-game/PPR/blob/engine/PER.Util/src/Color.cs
|
||||||
|
/// <summary>
|
||||||
|
/// A record struct representing a color.
|
||||||
|
/// </summary>
|
||||||
|
public record struct Color
|
||||||
|
{
|
||||||
|
public static readonly Color Transparent = new(1.0f, 1.0f, 1.0f, 0.0f);
|
||||||
|
public static readonly Color AliceBlue = new(0xF0F8FF);
|
||||||
|
public static readonly Color AntiqueWhite = new(0xFAEBD7);
|
||||||
|
public static readonly Color Aqua = new(0x00FFFF);
|
||||||
|
public static readonly Color Aquamarine = new(0x7FFFD4);
|
||||||
|
public static readonly Color Azure = new(0xF0FFFF);
|
||||||
|
public static readonly Color Beige = new(0xF5F5DC);
|
||||||
|
public static readonly Color Bisque = new(0xFFE4C4);
|
||||||
|
public static readonly Color Black = new(0x000000);
|
||||||
|
public static readonly Color BlanchedAlmond = new(0xFFEBCD);
|
||||||
|
public static readonly Color Blue = new(0x0000FF);
|
||||||
|
public static readonly Color BlueViolet = new(0x8A2BE2);
|
||||||
|
public static readonly Color Brown = new(0xA52A2A);
|
||||||
|
public static readonly Color BurlyWood = new(0xDEB887);
|
||||||
|
public static readonly Color CadetBlue = new(0x5F9EA0);
|
||||||
|
public static readonly Color Chartreuse = new(0x7FFF00);
|
||||||
|
public static readonly Color Chocolate = new(0xD2691E);
|
||||||
|
public static readonly Color Coral = new(0xFF7F50);
|
||||||
|
public static readonly Color CornflowerBlue = new(0x6495ED);
|
||||||
|
public static readonly Color Cornsilk = new(0xFFF8DC);
|
||||||
|
public static readonly Color Crimson = new(0xDC143C);
|
||||||
|
public static readonly Color Cyan = new(0x00FFFF);
|
||||||
|
public static readonly Color DarkBlue = new(0x00008B);
|
||||||
|
public static readonly Color DarkCyan = new(0x008B8B);
|
||||||
|
public static readonly Color White = new(0xFFFFFF);
|
||||||
|
public static readonly Color Green = new(0x00FF00);
|
||||||
|
public static readonly Color Red = new(0xFF0000);
|
||||||
|
public static readonly Color DarkGoldenRod = new(0xB8860B);
|
||||||
|
public static readonly Color DarkGray = new(0xA9A9A9);
|
||||||
|
public static readonly Color DarkGreen = new(0x006400);
|
||||||
|
public static readonly Color DarkKhaki = new(0xBDB76B);
|
||||||
|
public static readonly Color DarkMagenta = new(0x8B008B);
|
||||||
|
public static readonly Color DarkOliveGreen = new(0x556B2F);
|
||||||
|
public static readonly Color DarkOrange = new(0xFF8C00);
|
||||||
|
public static readonly Color DarkOrchid = new(0x9932CC);
|
||||||
|
public static readonly Color DarkRed = new(0x8B0000);
|
||||||
|
public static readonly Color DarkSalmon = new(0xE9967A);
|
||||||
|
public static readonly Color DarkSeaGreen = new(0x8FBC8F);
|
||||||
|
public static readonly Color DarkSlateBlue = new(0x483D8B);
|
||||||
|
public static readonly Color DarkSlateGray = new(0x2F4F4F);
|
||||||
|
public static readonly Color DarkTurquoise = new(0x00CED1);
|
||||||
|
public static readonly Color DarkViolet = new(0x9400D3);
|
||||||
|
public static readonly Color DeepPink = new(0xFF1493);
|
||||||
|
public static readonly Color DeepSkyBlue = new(0x00BFFF);
|
||||||
|
public static readonly Color DimGray = new(0x696969);
|
||||||
|
public static readonly Color DodgerBlue = new(0x1E90FF);
|
||||||
|
public static readonly Color FireBrick = new(0xB22222);
|
||||||
|
public static readonly Color FloralWhite = new(0xFFFAF0);
|
||||||
|
public static readonly Color ForestGreen = new(0x228B22);
|
||||||
|
public static readonly Color Gainsboro = new(0xDCDCDC);
|
||||||
|
public static readonly Color GhostWhite = new(0xF8F8FF);
|
||||||
|
public static readonly Color Gold = new(0xFFD700);
|
||||||
|
public static readonly Color GoldenRod = new(0xDAA520);
|
||||||
|
public static readonly Color Gray = new(0x808080);
|
||||||
|
public static readonly Color GreenYellow = new(0xADFF2F);
|
||||||
|
public static readonly Color HoneyDew = new(0xF0FFF0);
|
||||||
|
public static readonly Color HotPink = new(0xFF69B4);
|
||||||
|
public static readonly Color IndianRed = new(0xCD5C5C);
|
||||||
|
public static readonly Color Indigo = new(0x4B0082);
|
||||||
|
public static readonly Color Ivory = new(0xFFFFF0);
|
||||||
|
public static readonly Color Khaki = new(0xF0E68C);
|
||||||
|
public static readonly Color Lavender = new(0xE6E6FA);
|
||||||
|
public static readonly Color LavenderBlush = new(0xFFF0F5);
|
||||||
|
public static readonly Color LawnGreen = new(0x7CFC00);
|
||||||
|
public static readonly Color LemonChiffon = new(0xFFFACD);
|
||||||
|
public static readonly Color LightBlue = new(0xADD8E6);
|
||||||
|
public static readonly Color LightCoral = new(0xF08080);
|
||||||
|
public static readonly Color LightCyan = new(0xE0FFFF);
|
||||||
|
public static readonly Color LightGoldenRodYellow = new(0xFAFAD2);
|
||||||
|
public static readonly Color LightGray = new(0xD3D3D3);
|
||||||
|
public static readonly Color LightGreen = new(0x90EE90);
|
||||||
|
public static readonly Color LightPink = new(0xFFB6C1);
|
||||||
|
public static readonly Color LightSalmon = new(0xFFA07A);
|
||||||
|
public static readonly Color LightSeaGreen = new(0x20B2AA);
|
||||||
|
public static readonly Color LightSkyBlue = new(0x87CEFA);
|
||||||
|
public static readonly Color LightSlateGray = new(0x778899);
|
||||||
|
public static readonly Color LightSteelBlue = new(0xB0C4DE);
|
||||||
|
public static readonly Color LightYellow = new(0xFFFFE0);
|
||||||
|
public static readonly Color Lime = new(0x00FF00);
|
||||||
|
public static readonly Color LimeGreen = new(0x32CD32);
|
||||||
|
public static readonly Color Linen = new(0xFAF0E6);
|
||||||
|
public static readonly Color Magenta = new(0xFF00FF);
|
||||||
|
public static readonly Color Maroon = new(0x800000);
|
||||||
|
public static readonly Color MediumAquaMarine = new(0x66CDAA);
|
||||||
|
public static readonly Color MediumBlue = new(0x0000CD);
|
||||||
|
public static readonly Color MediumOrchid = new(0xBA55D3);
|
||||||
|
public static readonly Color MediumPurple = new(0x9370DB);
|
||||||
|
public static readonly Color MediumSeaGreen = new(0x3CB371);
|
||||||
|
public static readonly Color MediumSlateBlue = new(0x7B68EE);
|
||||||
|
public static readonly Color MediumSpringGreen = new(0x00FA9A);
|
||||||
|
public static readonly Color MediumTurquoise = new(0x48D1CC);
|
||||||
|
public static readonly Color MediumVioletRed = new(0xC71585);
|
||||||
|
public static readonly Color MidnightBlue = new(0x191970);
|
||||||
|
public static readonly Color MintCream = new(0xF5FFFA);
|
||||||
|
public static readonly Color MistyRose = new(0xFFE4E1);
|
||||||
|
public static readonly Color Moccasin = new(0xFFE4B5);
|
||||||
|
public static readonly Color NavajoWhite = new(0xFFDEAD);
|
||||||
|
public static readonly Color Navy = new(0x000080);
|
||||||
|
public static readonly Color OldLace = new(0xFDF5E6);
|
||||||
|
public static readonly Color Olive = new(0x808000);
|
||||||
|
public static readonly Color OliveDrab = new(0x6B8E23);
|
||||||
|
public static readonly Color Orange = new(0xFFA500);
|
||||||
|
public static readonly Color OrangeRed = new(0xFF4500);
|
||||||
|
public static readonly Color Orchid = new(0xDA70D6);
|
||||||
|
public static readonly Color PaleGoldenRod = new(0xEEE8AA);
|
||||||
|
public static readonly Color PaleGreen = new(0x98FB98);
|
||||||
|
public static readonly Color PaleTurquoise = new(0xAFEEEE);
|
||||||
|
public static readonly Color PaleVioletRed = new(0xDB7093);
|
||||||
|
public static readonly Color PapayaWhip = new(0xFFEFD5);
|
||||||
|
public static readonly Color PeachPuff = new(0xFFDAB9);
|
||||||
|
public static readonly Color Peru = new(0xCD853F);
|
||||||
|
public static readonly Color Pink = new(0xFFC0CB);
|
||||||
|
public static readonly Color Plum = new(0xDDA0DD);
|
||||||
|
public static readonly Color PowderBlue = new(0xB0E0E6);
|
||||||
|
public static readonly Color Purple = new(0x800080);
|
||||||
|
public static readonly Color RebeccaPurple = new(0x663399);
|
||||||
|
public static readonly Color RosyBrown = new(0xBC8F8F);
|
||||||
|
public static readonly Color RoyalBlue = new(0x4169E1);
|
||||||
|
public static readonly Color SaddleBrown = new(0x8B4513);
|
||||||
|
public static readonly Color Salmon = new(0xFA8072);
|
||||||
|
public static readonly Color SandyBrown = new(0xF4A460);
|
||||||
|
public static readonly Color SeaGreen = new(0x2E8B57);
|
||||||
|
public static readonly Color Seashell = new(0xFFF5EE);
|
||||||
|
public static readonly Color Sienna = new(0xA0522D);
|
||||||
|
public static readonly Color Silver = new(0xC0C0C0);
|
||||||
|
public static readonly Color SkyBlue = new(0x87CEEB);
|
||||||
|
public static readonly Color SlateBlue = new(0x6A5ACD);
|
||||||
|
public static readonly Color SlateGray = new(0x708090);
|
||||||
|
public static readonly Color Snow = new(0xFFFAFA);
|
||||||
|
public static readonly Color SpringGreen = new(0x00FF7F);
|
||||||
|
public static readonly Color SteelBlue = new(0x4682B4);
|
||||||
|
public static readonly Color Tan = new(0xD2B48C);
|
||||||
|
public static readonly Color Teal = new(0x008080);
|
||||||
|
public static readonly Color Thistle = new(0xD8BFD8);
|
||||||
|
public static readonly Color Tomato = new(0xFF6347);
|
||||||
|
public static readonly Color Turquoise = new(0x40E0D0);
|
||||||
|
public static readonly Color Violet = new(0xEE82EE);
|
||||||
|
public static readonly Color Wheat = new(0xF5DEB3);
|
||||||
|
public static readonly Color WhiteSmoke = new(0xF5F5F5);
|
||||||
|
public static readonly Color Yellow = new(0xFFFF00);
|
||||||
|
public static readonly Color YellowGreen = new(0x9ACD32);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Red component of this <see cref="Color"/>.
|
||||||
|
/// </summary>
|
||||||
|
public byte R { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Green component of this <see cref="Color"/>.
|
||||||
|
/// </summary>
|
||||||
|
public byte G { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Blue component of this <see cref="Color"/>.
|
||||||
|
/// </summary>
|
||||||
|
public byte B { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Alpha component of this <see cref="Color"/>.
|
||||||
|
/// </summary>/// <summary>
|
||||||
|
/// Gets the color as a 32-bit ARGB integer in the format 0xAARRGGBB.
|
||||||
|
/// </summary>
|
||||||
|
public byte A { get; set; } = 255;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the color as a 32-bit ARGB integer in the format 0xAARRGGBB.
|
||||||
|
/// </summary>
|
||||||
|
public int Argb
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
int a = A << 24;
|
||||||
|
int r = R << 16;
|
||||||
|
int g = G << 8;
|
||||||
|
int b = B;
|
||||||
|
|
||||||
|
return a | r | g | b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="Color"/> struct using float RGB(A) values between 0 and 1.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="r">The red component (0.0 to 1.0).</param>
|
||||||
|
/// <param name="g">The green component (0.0 to 1.0).</param>
|
||||||
|
/// <param name="b">The blue component (0.0 to 1.0).</param>
|
||||||
|
/// <param name="a">The alpha component (0.0 to 1.0), default is 1.0 (fully opaque).</param>
|
||||||
|
public Color(float r, float g, float b, float a = 1.0f)
|
||||||
|
{
|
||||||
|
R = (byte)Math.Clamp(r * 255, 0, 255);
|
||||||
|
G = (byte)Math.Clamp(g * 255, 0, 255);
|
||||||
|
B = (byte)Math.Clamp(b * 255, 0, 255);
|
||||||
|
A = (byte)Math.Clamp(a * 255, 0, 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="Color"/> struct using byte RGB(A) values.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="r">The red component (0 to 255).</param>
|
||||||
|
/// <param name="g">The green component (0 to 255).</param>
|
||||||
|
/// <param name="b">The blue component (0 to 255).</param>
|
||||||
|
/// <param name="a">The alpha component (0 to 255), default is 255 (fully opaque).</param>
|
||||||
|
public Color(byte r, byte g, byte b, byte a = 255)
|
||||||
|
{
|
||||||
|
R = r;
|
||||||
|
G = g;
|
||||||
|
B = b;
|
||||||
|
A = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="Color"/> struct using a hexadecimal value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hex">
|
||||||
|
/// A 24-bit (RRGGBB) or 32-bit (AARRGGBB) integer representing the color.
|
||||||
|
/// Alpha is assumed to be 255 if not included.
|
||||||
|
/// </param>
|
||||||
|
public Color(int hex)
|
||||||
|
{
|
||||||
|
A = 255; // Default alpha to 255 if not provided
|
||||||
|
B = (byte)(hex & 0xFF);
|
||||||
|
G = (byte)((hex >> 8) & 0xFF);
|
||||||
|
R = (byte)((hex >> 16) & 0xFF);
|
||||||
|
if (hex > 0xFFFFFF) // If the hex value includes alpha
|
||||||
|
{
|
||||||
|
A = (byte)((hex >> 24) & 0xFF);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parses a color from a hexadecimal string in the format "#RRGGBB" or "#AARRGGBB".
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hex">The hex string representing the color.</param>
|
||||||
|
/// <returns>A <see cref="Color"/> instance parsed from the string.</returns>
|
||||||
|
/// <exception cref="ArgumentException">Thrown if the format is invalid.</exception>
|
||||||
|
public static Color FromHexString(string hex)
|
||||||
|
{
|
||||||
|
if (hex.StartsWith("#"))
|
||||||
|
{
|
||||||
|
hex = hex[1..];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hex.Length == 6)
|
||||||
|
{
|
||||||
|
int rgb = int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
|
||||||
|
return new Color(rgb);
|
||||||
|
}
|
||||||
|
else if (hex.Length == 8)
|
||||||
|
{
|
||||||
|
int rgba = int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
|
||||||
|
return new Color(rgba);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid hex color format. Use #RRGGBB or #RRGGBBAA.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a lightened version of the color by interpolating toward white.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="amount">A value from 0.0 (no change) to 1.0 (fully white).</param>
|
||||||
|
/// <returns>A lighter <see cref="Color"/>.</returns>
|
||||||
|
public Color Lightened(float amount)
|
||||||
|
{
|
||||||
|
var result = this;
|
||||||
|
result.R = (byte)Math.Min(255, R + (255 - R) * amount);
|
||||||
|
result.G = (byte)Math.Min(255, G + (255 - G) * amount);
|
||||||
|
result.B = (byte)Math.Min(255, B + (255 - B) * amount);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a darkened version of the color by interpolating toward black.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="amount">A value from 0.0 (no change) to 1.0 (fully black).</param>
|
||||||
|
/// <returns>A darker <see cref="Color"/>.</returns>
|
||||||
|
public Color Darkened(float amount)
|
||||||
|
{
|
||||||
|
var result = this;
|
||||||
|
result.R = (byte)(R * (1.0f - amount));
|
||||||
|
result.G = (byte)(G * (1.0f - amount));
|
||||||
|
result.B = (byte)(B * (1.0f - amount));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts this color to a <see cref="System.Drawing.Color"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A <see cref="System.Drawing.Color"/> with equivalent ARGB values.</returns>
|
||||||
|
public System.Drawing.Color ToSystemColor()
|
||||||
|
{
|
||||||
|
var result = System.Drawing.Color.FromArgb(Argb);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,5 +8,31 @@ namespace Voile.Extensions
|
|||||||
{
|
{
|
||||||
return new Vector2((float)MathUtils.Lerp(a.X, b.X, t), (float)MathUtils.Lerp(a.Y, b.Y, t));
|
return new Vector2((float)MathUtils.Lerp(a.X, b.X, t), (float)MathUtils.Lerp(a.Y, b.Y, t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Vector2 Snapped(this Vector2 a, Vector2 snap)
|
||||||
|
{
|
||||||
|
var x = a.X % snap.X;
|
||||||
|
var y = a.Y % snap.Y;
|
||||||
|
|
||||||
|
if (x == 0)
|
||||||
|
{
|
||||||
|
x = a.X;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
x = a.X - x;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y == 0)
|
||||||
|
{
|
||||||
|
y = a.Y;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
y = a.Y - y;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Vector2(x, y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,6 +164,11 @@ namespace Voile
|
|||||||
throw new NullReferenceException("No renderer provided.");
|
throw new NullReferenceException("No renderer provided.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Input is null)
|
||||||
|
{
|
||||||
|
throw new NullReferenceException("No input system provided.");
|
||||||
|
}
|
||||||
|
|
||||||
Stopwatch stopwatch = Stopwatch.StartNew();
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
||||||
double previousTime = stopwatch.Elapsed.TotalSeconds;
|
double previousTime = stopwatch.Elapsed.TotalSeconds;
|
||||||
|
|
||||||
@@ -175,6 +180,8 @@ namespace Voile
|
|||||||
|
|
||||||
_accumulator += elapsedTime;
|
_accumulator += elapsedTime;
|
||||||
|
|
||||||
|
Input.Poll();
|
||||||
|
|
||||||
while (_accumulator >= UpdateTimeStep)
|
while (_accumulator >= UpdateTimeStep)
|
||||||
{
|
{
|
||||||
foreach (var system in _updatableSystems)
|
foreach (var system in _updatableSystems)
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ namespace Voile.Input
|
|||||||
|
|
||||||
public bool IsPressed(InputSystem inputSystem)
|
public bool IsPressed(InputSystem inputSystem)
|
||||||
{
|
{
|
||||||
return inputSystem.IsMousePressed(MouseButton);
|
return inputSystem.IsMouseButtonPressed(MouseButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsDown(InputSystem inputSystem)
|
public bool IsDown(InputSystem inputSystem)
|
||||||
|
|||||||
@@ -20,6 +20,12 @@ namespace Voile.Input
|
|||||||
CreateDefaultMappings();
|
CreateDefaultMappings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Forces this input system to poll all of its inputs during current frame.<br />
|
||||||
|
/// Some backends require inputs to be polled once per specific interval. Override this method to implement this behavior.
|
||||||
|
/// </summary>
|
||||||
|
public virtual void Poll() { }
|
||||||
|
|
||||||
public void Shutdown() => Dispose();
|
public void Shutdown() => Dispose();
|
||||||
|
|
||||||
public void Dispose() => GC.SuppressFinalize(this);
|
public void Dispose() => GC.SuppressFinalize(this);
|
||||||
@@ -102,7 +108,7 @@ namespace Voile.Input
|
|||||||
|
|
||||||
public abstract int GetCharPressed();
|
public abstract int GetCharPressed();
|
||||||
|
|
||||||
public abstract bool IsMousePressed(MouseButton button);
|
public abstract bool IsMouseButtonPressed(MouseButton button);
|
||||||
public abstract bool IsMouseButtonDown(MouseButton button);
|
public abstract bool IsMouseButtonDown(MouseButton button);
|
||||||
public abstract bool IsMouseButtonReleased(MouseButton button);
|
public abstract bool IsMouseButtonReleased(MouseButton button);
|
||||||
public abstract float GetMouseWheelMovement();
|
public abstract float GetMouseWheelMovement();
|
||||||
|
|||||||
@@ -8,63 +8,63 @@ namespace Voile.Input
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class RaylibInputSystem : InputSystem
|
public class RaylibInputSystem : InputSystem
|
||||||
{
|
{
|
||||||
public override int GetCharPressed()
|
public override void Poll()
|
||||||
{
|
{
|
||||||
return Raylib.GetCharPressed();
|
_justPressedKeys.Clear();
|
||||||
|
_justReleasedKeys.Clear();
|
||||||
|
_downKeys.Clear();
|
||||||
|
|
||||||
|
_pressedMouseButtons.Clear();
|
||||||
|
_releasedMouseButtons.Clear();
|
||||||
|
_downMouseButtons.Clear();
|
||||||
|
|
||||||
|
for (int key = 32; key <= 349; key++)
|
||||||
|
{
|
||||||
|
var k = (KeyboardKey)key;
|
||||||
|
if (Raylib.IsKeyPressed((Raylib_cs.KeyboardKey)k)) _justPressedKeys.Add(k);
|
||||||
|
if (Raylib.IsKeyReleased((Raylib_cs.KeyboardKey)k)) _justReleasedKeys.Add(k);
|
||||||
|
if (Raylib.IsKeyDown((Raylib_cs.KeyboardKey)k)) _downKeys.Add(k);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (MouseButton button in System.Enum.GetValues(typeof(MouseButton)))
|
||||||
|
{
|
||||||
|
var rayButton = (Raylib_cs.MouseButton)button;
|
||||||
|
if (Raylib.IsMouseButtonPressed(rayButton)) _pressedMouseButtons.Add(button);
|
||||||
|
if (Raylib.IsMouseButtonReleased(rayButton)) _releasedMouseButtons.Add(button);
|
||||||
|
if (Raylib.IsMouseButtonDown(rayButton)) _downMouseButtons.Add(button);
|
||||||
|
}
|
||||||
|
|
||||||
|
_mousePosition = Raylib.GetMousePosition();
|
||||||
|
_mouseWheelMove = Raylib.GetMouseWheelMove();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Vector2 GetMousePosition()
|
public override int GetCharPressed() => Raylib.GetCharPressed();
|
||||||
{
|
public override Vector2 GetMousePosition() => _mousePosition;
|
||||||
return Raylib.GetMousePosition();
|
public override float GetMouseWheelMovement() => _mouseWheelMove;
|
||||||
}
|
|
||||||
|
|
||||||
public override float GetMouseWheelMovement()
|
public override void HideCursor() => Raylib.HideCursor();
|
||||||
{
|
|
||||||
return Raylib.GetMouseWheelMove();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void HideCursor()
|
|
||||||
{
|
|
||||||
Raylib.HideCursor();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool IsKeyboardKeyDown(KeyboardKey key)
|
|
||||||
{
|
|
||||||
Raylib_cs.KeyboardKey rayKey = (Raylib_cs.KeyboardKey)key;
|
|
||||||
return Raylib.IsKeyDown(rayKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool IsMousePressed(MouseButton button)
|
|
||||||
{
|
|
||||||
return Raylib.IsMouseButtonPressed((Raylib_cs.MouseButton)button);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool IsMouseButtonReleased(MouseButton button)
|
|
||||||
{
|
|
||||||
return Raylib.IsMouseButtonReleased((Raylib_cs.MouseButton)button);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool IsMouseButtonDown(MouseButton button)
|
|
||||||
{
|
|
||||||
return Raylib.IsMouseButtonDown((Raylib_cs.MouseButton)button);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool KeyboardKeyJustPressed(KeyboardKey key)
|
|
||||||
{
|
|
||||||
Raylib_cs.KeyboardKey rayKey = (Raylib_cs.KeyboardKey)key;
|
|
||||||
return Raylib.IsKeyPressed(rayKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool KeyboardKeyJustReleased(KeyboardKey key)
|
|
||||||
{
|
|
||||||
return Raylib.IsKeyReleased((Raylib_cs.KeyboardKey)key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void SetMousePosition(Vector2 position)
|
|
||||||
{
|
|
||||||
Raylib.SetMousePosition((int)position.X, (int)position.Y);
|
|
||||||
}
|
|
||||||
public override void ShowCursor() => Raylib.ShowCursor();
|
public override void ShowCursor() => Raylib.ShowCursor();
|
||||||
public override bool IsCursorHidden() => Raylib.IsCursorHidden();
|
public override bool IsCursorHidden() => Raylib.IsCursorHidden();
|
||||||
|
|
||||||
|
public override bool IsKeyboardKeyDown(KeyboardKey key) => _downKeys.Contains(key);
|
||||||
|
public override bool KeyboardKeyJustPressed(KeyboardKey key) => _justPressedKeys.Contains(key);
|
||||||
|
public override bool KeyboardKeyJustReleased(KeyboardKey key) => _justReleasedKeys.Contains(key);
|
||||||
|
|
||||||
|
public override bool IsMouseButtonPressed(MouseButton button) => _pressedMouseButtons.Contains(button);
|
||||||
|
public override bool IsMouseButtonReleased(MouseButton button) => _releasedMouseButtons.Contains(button);
|
||||||
|
public override bool IsMouseButtonDown(MouseButton button) => _downMouseButtons.Contains(button);
|
||||||
|
|
||||||
|
public override void SetMousePosition(Vector2 position) => Raylib.SetMousePosition((int)position.X, (int)position.Y);
|
||||||
|
|
||||||
|
private readonly HashSet<KeyboardKey> _justPressedKeys = new();
|
||||||
|
private readonly HashSet<KeyboardKey> _justReleasedKeys = new();
|
||||||
|
private readonly HashSet<KeyboardKey> _downKeys = new();
|
||||||
|
|
||||||
|
private readonly HashSet<MouseButton> _pressedMouseButtons = new();
|
||||||
|
private readonly HashSet<MouseButton> _releasedMouseButtons = new();
|
||||||
|
private readonly HashSet<MouseButton> _downMouseButtons = new();
|
||||||
|
|
||||||
|
private Vector2 _mousePosition;
|
||||||
|
private float _mouseWheelMove;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
81
Voile/Source/Rect.cs
Normal file
81
Voile/Source/Rect.cs
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
namespace Voile;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a rectangle. Used to determine widget confines for UI layout.
|
||||||
|
/// </summary>
|
||||||
|
public record Rect(float Width = 0.0f, float Height = 0.0f)
|
||||||
|
{
|
||||||
|
public float Width { get; set; } = Width;
|
||||||
|
public float Height { get; set; } = Height;
|
||||||
|
public static Rect Zero => new Rect(0.0f, 0.0f);
|
||||||
|
public float Area => Width * Height;
|
||||||
|
|
||||||
|
public int CompareTo(Rect? other)
|
||||||
|
{
|
||||||
|
if (other is null) return 1;
|
||||||
|
return Area.CompareTo(other.Area);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator >(Rect left, Rect right) => left.CompareTo(right) > 0;
|
||||||
|
public static bool operator <(Rect left, Rect right) => left.CompareTo(right) < 0;
|
||||||
|
public static bool operator >=(Rect left, Rect right) => left.CompareTo(right) >= 0;
|
||||||
|
public static bool operator <=(Rect left, Rect right) => left.CompareTo(right) <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents the size offsets applied around a rectangle.
|
||||||
|
/// </summary>
|
||||||
|
public struct Size : IEquatable<Size>
|
||||||
|
{
|
||||||
|
public float Left;
|
||||||
|
public float Right;
|
||||||
|
public float Top;
|
||||||
|
public float Bottom;
|
||||||
|
|
||||||
|
public Size(float uniform)
|
||||||
|
{
|
||||||
|
Left = Right = Top = Bottom = uniform;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Size(float horizontal, float vertical)
|
||||||
|
{
|
||||||
|
Left = Right = horizontal;
|
||||||
|
Top = Bottom = vertical;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Size(float left, float right, float top, float bottom)
|
||||||
|
{
|
||||||
|
Left = left;
|
||||||
|
Right = right;
|
||||||
|
Top = top;
|
||||||
|
Bottom = bottom;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Size Zero => new Size(0);
|
||||||
|
|
||||||
|
public static Rect operator +(Size margin, Rect rect) =>
|
||||||
|
new Rect(rect.Width + margin.Left + margin.Right,
|
||||||
|
rect.Height + margin.Top + margin.Bottom);
|
||||||
|
|
||||||
|
public static Rect operator +(Rect rect, Size margin) =>
|
||||||
|
margin + rect;
|
||||||
|
|
||||||
|
|
||||||
|
public static bool operator ==(Size a, Size b) =>
|
||||||
|
a.Equals(b);
|
||||||
|
|
||||||
|
public static bool operator !=(Size a, Size b) =>
|
||||||
|
!a.Equals(b);
|
||||||
|
|
||||||
|
public bool Equals(Size other) =>
|
||||||
|
Left == other.Left &&
|
||||||
|
Right == other.Right &&
|
||||||
|
Top == other.Top &&
|
||||||
|
Bottom == other.Bottom;
|
||||||
|
|
||||||
|
public override bool Equals(object? obj) =>
|
||||||
|
obj is Size other && Equals(other);
|
||||||
|
|
||||||
|
public override int GetHashCode() =>
|
||||||
|
HashCode.Combine(Left, Right, Top, Bottom);
|
||||||
|
}
|
||||||
@@ -49,6 +49,7 @@ namespace Voile.Rendering
|
|||||||
_defaultFlags = flags;
|
_defaultFlags = flags;
|
||||||
|
|
||||||
Raylib.SetConfigFlags(flags);
|
Raylib.SetConfigFlags(flags);
|
||||||
|
Raylib.SetTargetFPS(settings.TargetFps);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void CreateAndInitializeWithWindow(RendererSettings settings)
|
public override void CreateAndInitializeWithWindow(RendererSettings settings)
|
||||||
@@ -80,7 +81,7 @@ namespace Voile.Rendering
|
|||||||
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, windowSettings.Title);
|
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, windowSettings.Title);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Raylib.SetWindowState(windowFlags);
|
Raylib.SetWindowState(windowFlags);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
@@ -243,6 +244,12 @@ namespace Voile.Rendering
|
|||||||
LoadFont(font);
|
LoadFont(font);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (font.Dirty && font.Handle != -1)
|
||||||
|
{
|
||||||
|
UnloadFont(font);
|
||||||
|
LoadFont(font);
|
||||||
|
}
|
||||||
|
|
||||||
var rayFont = _fontPool[font.Handle];
|
var rayFont = _fontPool[font.Handle];
|
||||||
|
|
||||||
Raylib.DrawTextPro(rayFont, text, transformPosition, transformPivot, transformRotation, font.Size, 0.0f, VoileColorToRaylibColor(color));
|
Raylib.DrawTextPro(rayFont, text, transformPosition, transformPivot, transformRotation, font.Size, 0.0f, VoileColorToRaylibColor(color));
|
||||||
@@ -267,10 +274,10 @@ namespace Voile.Rendering
|
|||||||
{
|
{
|
||||||
Raylib_cs.Font fontRay;
|
Raylib_cs.Font fontRay;
|
||||||
|
|
||||||
string ext = ".ttf"; // TODO: don't use a hardcoded extension.
|
string ext = ".ttf";
|
||||||
int fontChars = 250; // TODO: control this dynamically to not load the entire font.
|
int fontChars = font.Codepoints.Count;
|
||||||
|
|
||||||
fontRay = Raylib.LoadFontFromMemory(ext, font.Buffer, font.Size, null, fontChars);
|
fontRay = Raylib.LoadFontFromMemory(ext, font.Buffer, font.Size, font.Codepoints.ToArray(), fontChars);
|
||||||
|
|
||||||
Raylib.GenTextureMipmaps(ref fontRay.Texture);
|
Raylib.GenTextureMipmaps(ref fontRay.Texture);
|
||||||
Raylib.SetTextureFilter(fontRay.Texture, TextureFilter.Bilinear);
|
Raylib.SetTextureFilter(fontRay.Texture, TextureFilter.Bilinear);
|
||||||
@@ -280,6 +287,14 @@ namespace Voile.Rendering
|
|||||||
font.Handle = _fontPool.Count - 1;
|
font.Handle = _fontPool.Count - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void UnloadFont(Font font)
|
||||||
|
{
|
||||||
|
var fontRay = _fontPool[font.Handle];
|
||||||
|
Raylib.UnloadFont(fontRay);
|
||||||
|
|
||||||
|
_fontPool.RemoveAt(font.Handle);
|
||||||
|
}
|
||||||
|
|
||||||
private void LoadTexture(Texture2d texture)
|
private void LoadTexture(Texture2d texture)
|
||||||
{
|
{
|
||||||
Image image = new();
|
Image image = new();
|
||||||
|
|||||||
@@ -256,7 +256,7 @@ namespace Voile.Rendering
|
|||||||
{
|
{
|
||||||
public string Title;
|
public string Title;
|
||||||
public Vector2 Size = new Vector2(1280, 720);
|
public Vector2 Size = new Vector2(1280, 720);
|
||||||
public bool Resizable { get; set; }
|
public bool Resizable { get; set; } = true;
|
||||||
|
|
||||||
public WindowSettings(string title, Vector2 size)
|
public WindowSettings(string title, Vector2 size)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -65,6 +65,9 @@ namespace Voile.Resources.DataReaders
|
|||||||
/// <param name="defaultValue">Default value in case this getter fails to get data.</param>
|
/// <param name="defaultValue">Default value in case this getter fails to get data.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
double GetDouble(string key, double defaultValue = 0.0);
|
double GetDouble(string key, double defaultValue = 0.0);
|
||||||
|
|
||||||
|
string GetString(string key, string defaultValue);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a Voile.Color from this data getter.
|
/// Get a Voile.Color from this data getter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -79,5 +82,15 @@ namespace Voile.Resources.DataReaders
|
|||||||
/// <param name="defaultValue">Default value in case this getter fails to get data.</param>
|
/// <param name="defaultValue">Default value in case this getter fails to get data.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Vector2 GetVector2(string key, Vector2 defaultValue);
|
Vector2 GetVector2(string key, Vector2 defaultValue);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get a <see cref="Size"/> from this data getter.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">Key of the value.</param>
|
||||||
|
/// <param name="defaultValue">Default value in case this getter fails to get data.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Size GetSize(string key, Size defaultValue);
|
||||||
|
|
||||||
|
T[] GetArray<T>(string key, T[] defaultValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,163 +9,129 @@ namespace Voile.Resources.DataReaders;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class TomlDataReader : IStreamDataReader, IDataValidator, IStreamKeyValueGetter
|
public class TomlDataReader : IStreamDataReader, IDataValidator, IStreamKeyValueGetter
|
||||||
{
|
{
|
||||||
public string ExpectedHeader { get; private set; } = string.Empty;
|
public TomlDataReader() { }
|
||||||
public TomlDataReader(string expectedHeader)
|
|
||||||
{
|
|
||||||
ExpectedHeader = expectedHeader;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Read(Stream data)
|
public void Read(Stream data)
|
||||||
{
|
{
|
||||||
using (var reader = new StreamReader(data))
|
using var reader = new StreamReader(data);
|
||||||
|
_table = TOML.Parse(reader);
|
||||||
|
_valid = _table != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Valid() => _valid;
|
||||||
|
|
||||||
|
public bool HasKey(string key)
|
||||||
|
{
|
||||||
|
return _table != null &&
|
||||||
|
_table.HasKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<string> GetSubKeys()
|
||||||
|
{
|
||||||
|
if (_table == null)
|
||||||
|
return Enumerable.Empty<string>();
|
||||||
|
|
||||||
|
return _table.Keys
|
||||||
|
.Where(k => _table[k].IsTable)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<string> GetSubKeysRecursive(string prefix = "")
|
||||||
|
{
|
||||||
|
if (_table == null)
|
||||||
|
yield break;
|
||||||
|
|
||||||
|
foreach (var key in _table.Keys)
|
||||||
{
|
{
|
||||||
_table = TOML.Parse(reader);
|
var fullKey = string.IsNullOrEmpty(prefix) ? key : $"{prefix}.{key}";
|
||||||
_valid = _table.HasKey(ExpectedHeader);
|
if (_table[key].IsTable)
|
||||||
|
{
|
||||||
|
var subReader = GetSubReader(key);
|
||||||
|
if (subReader != null)
|
||||||
|
{
|
||||||
|
foreach (var subKey in subReader.GetSubKeysRecursive(fullKey))
|
||||||
|
yield return subKey;
|
||||||
|
|
||||||
|
yield return fullKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerable<string> GetSubKeys(string subPath)
|
||||||
|
{
|
||||||
|
var subReader = GetSubReader(subPath);
|
||||||
|
if (subReader?._table == null)
|
||||||
|
return Enumerable.Empty<string>();
|
||||||
|
|
||||||
|
return subReader._table.Keys
|
||||||
|
.Where(k => subReader._table[k].IsTable)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TomlDataReader? GetSubReader(string path)
|
||||||
|
{
|
||||||
|
var current = _table;
|
||||||
|
foreach (var part in path.Split('.'))
|
||||||
|
{
|
||||||
|
if (current == null || !current.HasKey(part) || !current[part].IsTable)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
current = current[part].AsTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TomlDataReader { _table = current, _valid = true };
|
||||||
|
}
|
||||||
|
|
||||||
public bool GetBool(string key, bool defaultValue = false)
|
public bool GetBool(string key, bool defaultValue = false)
|
||||||
{
|
=> TryGetNode(key, out var node) && node.IsBoolean ? node.AsBoolean : defaultValue;
|
||||||
if (_table is null)
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var dataTable = _table[ExpectedHeader];
|
|
||||||
|
|
||||||
if (!dataTable.HasKey(key))
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var tableValue = dataTable[key];
|
|
||||||
|
|
||||||
if (!tableValue.IsBoolean)
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return tableValue.AsBoolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int GetInt(string key, int defaultValue = 0)
|
public int GetInt(string key, int defaultValue = 0)
|
||||||
{
|
=> TryGetNode(key, out var node) && node.IsInteger ? node.AsInteger : defaultValue;
|
||||||
if (_table is null)
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var dataTable = _table[ExpectedHeader];
|
|
||||||
|
|
||||||
if (!dataTable.HasKey(key))
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var tableValue = dataTable[key];
|
|
||||||
|
|
||||||
if (!tableValue.IsInteger)
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return tableValue.AsInteger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long GetLong(string key, long defaultValue = 0)
|
public long GetLong(string key, long defaultValue = 0)
|
||||||
{
|
=> TryGetNode(key, out var node) && node.IsInteger ? node.AsInteger.Value : defaultValue;
|
||||||
if (_table is null)
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var dataTable = _table[ExpectedHeader];
|
|
||||||
|
|
||||||
if (!dataTable.HasKey(key))
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var tableValue = dataTable[key];
|
|
||||||
|
|
||||||
if (!tableValue.IsInteger)
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return tableValue.AsInteger.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public float GetFloat(string key, float defaultValue = 0)
|
public float GetFloat(string key, float defaultValue = 0)
|
||||||
{
|
{
|
||||||
if (_table is null)
|
if (!TryGetNode(key, out var node))
|
||||||
{
|
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
|
||||||
|
|
||||||
var dataTable = _table[ExpectedHeader];
|
if (node.IsFloat) return node.AsFloat;
|
||||||
|
if (node.IsInteger) return node.AsInteger;
|
||||||
if (!dataTable.HasKey(key))
|
return defaultValue;
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var tableValue = dataTable[key];
|
|
||||||
|
|
||||||
if (!tableValue.IsFloat)
|
|
||||||
{
|
|
||||||
if (tableValue.IsInteger) return (float)tableValue.AsInteger.Value;
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return tableValue.AsFloat;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public double GetDouble(string key, double defaultValue = 0)
|
public double GetDouble(string key, double defaultValue = 0)
|
||||||
|
=> TryGetNode(key, out var node) && node.IsFloat ? node.AsFloat : defaultValue;
|
||||||
|
|
||||||
|
public string GetString(string key, string defaultValue)
|
||||||
{
|
{
|
||||||
if (_table is null)
|
if (!TryGetNode(key, out var node))
|
||||||
{
|
{
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataTable = _table[ExpectedHeader];
|
if (node.IsString)
|
||||||
|
|
||||||
if (!dataTable.HasKey(key))
|
|
||||||
{
|
{
|
||||||
return defaultValue;
|
return node.AsString;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dataTable.IsFloat)
|
return defaultValue;
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return dataTable.AsFloat.Value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Color GetColor(string key, Color defaultValue)
|
public Color GetColor(string key, Color defaultValue)
|
||||||
{
|
{
|
||||||
if (_table is null)
|
if (!TryGetNode(key, out var node))
|
||||||
{
|
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
|
||||||
|
|
||||||
var dataTable = _table[ExpectedHeader];
|
if (node.IsInteger)
|
||||||
|
|
||||||
if (!dataTable.HasKey(key))
|
|
||||||
{
|
{
|
||||||
return defaultValue;
|
return new Color(node.AsInteger);
|
||||||
}
|
}
|
||||||
|
else if (node.IsArray)
|
||||||
var colorNode = dataTable[key];
|
|
||||||
|
|
||||||
if (colorNode.IsInteger)
|
|
||||||
{
|
{
|
||||||
return new Color(colorNode.AsInteger);
|
var colorArray = node.AsArray;
|
||||||
}
|
|
||||||
else if (colorNode.IsArray)
|
|
||||||
{
|
|
||||||
var colorArray = colorNode.AsArray;
|
|
||||||
|
|
||||||
var rNode = colorArray[0];
|
var rNode = colorArray[0];
|
||||||
var gNode = colorArray[1];
|
var gNode = colorArray[1];
|
||||||
@@ -185,45 +151,102 @@ public class TomlDataReader : IStreamDataReader, IDataValidator, IStreamKeyValue
|
|||||||
|
|
||||||
return new Color((byte)r, (byte)g, (byte)b, (byte)a);
|
return new Color((byte)r, (byte)g, (byte)b, (byte)a);
|
||||||
}
|
}
|
||||||
else if (colorNode.IsString)
|
else if (node.IsString)
|
||||||
{
|
{
|
||||||
var colorHexString = colorNode.AsString.Value;
|
var colorHexString = node.AsString.Value;
|
||||||
return Color.FromHexString(colorHexString);
|
return Color.FromHexString(colorHexString);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Color can only be represented as an array of integers in the range of 0-255, array of floats (0-1), hexadecimal, or hex string.");
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 GetVector2(string key, Vector2 defaultValue)
|
public Vector2 GetVector2(string key, Vector2 defaultValue)
|
||||||
{
|
{
|
||||||
if (_table is null)
|
if (!TryGetNode(key, out var node) || !node.IsArray || node.AsArray.RawArray.Count != 2)
|
||||||
{
|
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
|
||||||
|
|
||||||
var dataTable = _table[ExpectedHeader];
|
var arr = node.AsArray;
|
||||||
|
return new Vector2(arr[0], arr[1]);
|
||||||
if (!dataTable.HasKey(key))
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var vector2Node = dataTable[key];
|
|
||||||
|
|
||||||
if (!vector2Node.IsArray)
|
|
||||||
{
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var vector2Array = vector2Node.AsArray;
|
|
||||||
|
|
||||||
return new Vector2(vector2Array[0], vector2Array[1]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Valid() => _valid;
|
public Size GetSize(string key, Size defaultValue)
|
||||||
|
{
|
||||||
|
if (!TryGetNode(key, out var node))
|
||||||
|
return defaultValue;
|
||||||
|
|
||||||
|
if (node.IsInteger)
|
||||||
|
{
|
||||||
|
return new Size(node.AsInteger);
|
||||||
|
}
|
||||||
|
else if (node.IsFloat)
|
||||||
|
{
|
||||||
|
return new Size(node.AsFloat);
|
||||||
|
}
|
||||||
|
else if (node.IsArray)
|
||||||
|
{
|
||||||
|
var sizeArray = node.AsArray;
|
||||||
|
|
||||||
|
var lNode = sizeArray[0];
|
||||||
|
var rNode = sizeArray[1];
|
||||||
|
var tNode = sizeArray[2];
|
||||||
|
var bNode = sizeArray[3];
|
||||||
|
|
||||||
|
var l = lNode.IsInteger ? lNode.AsInteger : 0;
|
||||||
|
var t = tNode.IsInteger ? tNode.AsInteger : 0;
|
||||||
|
var r = rNode.IsInteger ? rNode.AsInteger : 0;
|
||||||
|
var b = bNode.IsInteger ? bNode.AsInteger : 0;
|
||||||
|
|
||||||
|
return new Size(l, r, t, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T[] GetArray<T>(string key, T[] defaultValue)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException("Generic array reading not implemented yet.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool TryGetNode(string key, out TomlNode node)
|
||||||
|
{
|
||||||
|
node = null!;
|
||||||
|
if (_table == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var current = _table;
|
||||||
|
var parts = key.Split('.');
|
||||||
|
|
||||||
|
for (int i = 0; i < parts.Length; i++)
|
||||||
|
{
|
||||||
|
if (!current.HasKey(parts[i]))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var child = current[parts[i]];
|
||||||
|
|
||||||
|
if (i == parts.Length - 1)
|
||||||
|
{
|
||||||
|
node = child;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!child.IsTable)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
current = child.AsTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private TomlTable? _table;
|
private TomlTable? _table;
|
||||||
private bool _valid;
|
private bool _valid;
|
||||||
|
|
||||||
|
// Internal use for subreaders
|
||||||
|
private TomlDataReader(TomlTable table)
|
||||||
|
{
|
||||||
|
_table = table;
|
||||||
|
_valid = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,26 @@
|
|||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
using FreeTypeSharp;
|
||||||
|
using static FreeTypeSharp.FT;
|
||||||
|
using static FreeTypeSharp.FT_LOAD;
|
||||||
|
|
||||||
namespace Voile;
|
namespace Voile;
|
||||||
|
|
||||||
|
public struct Glyph
|
||||||
|
{
|
||||||
|
public int TextureId { get; set; } = -1;
|
||||||
|
public float Width { get; set; }
|
||||||
|
public float Height { get; set; }
|
||||||
|
public Vector2 Bearing { get; set; }
|
||||||
|
public int Advance { get; set; }
|
||||||
|
|
||||||
|
public Glyph() { }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents font data.
|
/// Represents font data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Font : Resource
|
public class Font : Resource, IUpdatableResource, IDisposable
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Internal handle for the font. If it got successfully loaded into the GPU, the value will be other than -1.
|
/// Internal handle for the font. If it got successfully loaded into the GPU, the value will be other than -1.
|
||||||
@@ -14,8 +31,162 @@ public class Font : Resource
|
|||||||
public byte[]? Buffer { get; private set; }
|
public byte[]? Buffer { get; private set; }
|
||||||
public long BufferSize { get; set; }
|
public long BufferSize { get; set; }
|
||||||
|
|
||||||
|
public bool Dirty => _dirty;
|
||||||
|
|
||||||
|
internal float UnitsPerEm;
|
||||||
|
|
||||||
|
internal nint FacePtr;
|
||||||
|
internal nint LibraryPtr;
|
||||||
|
|
||||||
|
internal List<int> Codepoints => _glyphs.Keys.ToList();
|
||||||
|
|
||||||
public Font(string path, byte[] buffer) : base(path)
|
public Font(string path, byte[] buffer) : base(path)
|
||||||
{
|
{
|
||||||
Buffer = buffer;
|
Buffer = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
if (FacePtr != IntPtr.Zero)
|
||||||
|
FT_Done_Face((FT_FaceRec_*)FacePtr);
|
||||||
|
|
||||||
|
if (LibraryPtr != IntPtr.Zero)
|
||||||
|
FT_Done_FreeType((FT_LibraryRec_*)LibraryPtr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads a basic ASCII charset for this font.
|
||||||
|
/// </summary>
|
||||||
|
public void LoadAsciiData()
|
||||||
|
{
|
||||||
|
for (char c = ' '; c < 127; c++)
|
||||||
|
{
|
||||||
|
GetGlyph(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Measures a given string using the font metrics.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">Text to measure.</param>
|
||||||
|
/// <returns>A <see cref="Rect"/> with the sizes of a given text using this font.</returns>
|
||||||
|
public Rect Measure(string text)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(text))
|
||||||
|
return Rect.Zero;
|
||||||
|
|
||||||
|
float totalWidth = 0;
|
||||||
|
float maxAscent = 0;
|
||||||
|
float maxDescent = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < text.Length; i++)
|
||||||
|
{
|
||||||
|
char c = text[i];
|
||||||
|
Glyph glyph = GetGlyph(c);
|
||||||
|
|
||||||
|
totalWidth += glyph.Advance;
|
||||||
|
|
||||||
|
float ascent = glyph.Bearing.Y;
|
||||||
|
float descent = glyph.Height - glyph.Bearing.Y;
|
||||||
|
|
||||||
|
if (ascent > maxAscent)
|
||||||
|
maxAscent = ascent;
|
||||||
|
if (descent > maxDescent)
|
||||||
|
maxDescent = descent;
|
||||||
|
|
||||||
|
if (i > 0)
|
||||||
|
{
|
||||||
|
char prevChar = text[i - 1];
|
||||||
|
totalWidth += GetKerning(prevChar, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float totalHeight = Size;
|
||||||
|
return new Rect(totalWidth, totalHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetKerning(char left, char right)
|
||||||
|
{
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
if (FacePtr == IntPtr.Zero)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
FT_FaceRec_* face = (FT_FaceRec_*)FacePtr;
|
||||||
|
|
||||||
|
uint leftIndex = FT_Get_Char_Index(face, left);
|
||||||
|
uint rightIndex = FT_Get_Char_Index(face, right);
|
||||||
|
|
||||||
|
if (leftIndex == 0 || rightIndex == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
FT_Vector_ kerning;
|
||||||
|
if (FT_Get_Kerning(face, leftIndex, rightIndex, FT_Kerning_Mode_.FT_KERNING_DEFAULT, &kerning) != 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return (int)kerning.x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Glyph GetGlyph(char character)
|
||||||
|
{
|
||||||
|
if (!HasGlyph(character))
|
||||||
|
{
|
||||||
|
_glyphs.TryGetValue('?', out var defGlyph);
|
||||||
|
return defGlyph;
|
||||||
|
}
|
||||||
|
if (_glyphs.TryGetValue(character, out var glyph))
|
||||||
|
return glyph;
|
||||||
|
|
||||||
|
var loaded = LoadGlyph(character);
|
||||||
|
_glyphs[character] = loaded;
|
||||||
|
|
||||||
|
_dirty = true;
|
||||||
|
|
||||||
|
return loaded;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasGlyph(char character)
|
||||||
|
{
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
var face = (FT_FaceRec_*)FacePtr;
|
||||||
|
return FT_Get_Char_Index(face, character) != 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private unsafe Glyph LoadGlyph(char character)
|
||||||
|
{
|
||||||
|
var face = (FT_FaceRec_*)FacePtr;
|
||||||
|
|
||||||
|
// TODO: for now, we're loading glyphs for metrics, but when implementing WebGPU rendering, we want to somehow render them to SDF or bitmap.
|
||||||
|
FT_Error error = FT_Set_Pixel_Sizes(face, 0, (uint)Size);
|
||||||
|
error = FT_Load_Char(face, character, FT_LOAD_NO_BITMAP);
|
||||||
|
|
||||||
|
if (error != 0)
|
||||||
|
throw new Exception($"Failed to load glyph for '{character}'");
|
||||||
|
|
||||||
|
var glyph = face->glyph;
|
||||||
|
var bitmap = glyph->bitmap;
|
||||||
|
var metrics = glyph->metrics;
|
||||||
|
|
||||||
|
return new Glyph
|
||||||
|
{
|
||||||
|
Width = metrics.width >> 6,
|
||||||
|
Height = metrics.height >> 6,
|
||||||
|
Bearing = new Vector2(metrics.horiBearingX >> 6, metrics.horiBearingY >> 6),
|
||||||
|
Advance = (int)metrics.horiAdvance >> 6,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MarkUpdated()
|
||||||
|
{
|
||||||
|
_dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool _dirty;
|
||||||
|
private Dictionary<int, Glyph> _glyphs = new();
|
||||||
}
|
}
|
||||||
45
Voile/Source/Resources/FontSet.cs
Normal file
45
Voile/Source/Resources/FontSet.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Voile.Resources;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Contains a set of multiple fonts. Used to fetch fonts based on availability of glyphs.
|
||||||
|
/// </summary>
|
||||||
|
public class FontSet
|
||||||
|
{
|
||||||
|
public FontSet() { }
|
||||||
|
|
||||||
|
public FontSet(IEnumerable<ResourceRef<Font>> fonts)
|
||||||
|
{
|
||||||
|
_fonts = fonts.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddFont(ResourceRef<Font> font) => _fonts.Add(font);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to get a suitable font that has a given character.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="c">Character to get a suitable font for.</param>
|
||||||
|
/// <param name="result">Font that contains this character.</param>
|
||||||
|
/// <returns><c>true</c> if a font that contains this character exists, <c>false</c> otherwise.</returns>
|
||||||
|
public bool TryGetFontFor(char c, [NotNullWhen(true)] out ResourceRef<Font>? result)
|
||||||
|
{
|
||||||
|
result = ResourceRef<Font>.Empty();
|
||||||
|
foreach (var fontRef in _fonts)
|
||||||
|
{
|
||||||
|
if (!fontRef.TryGetValue(out var font))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (font.HasGlyph(c))
|
||||||
|
{
|
||||||
|
result = fontRef;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ResourceRef<Font>> _fonts = new();
|
||||||
|
}
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
|
|
||||||
|
using System.Numerics;
|
||||||
|
using FreeTypeSharp;
|
||||||
using Voile.VFS;
|
using Voile.VFS;
|
||||||
|
|
||||||
|
using static FreeTypeSharp.FT;
|
||||||
|
using static FreeTypeSharp.FT_LOAD;
|
||||||
|
|
||||||
namespace Voile.Resources;
|
namespace Voile.Resources;
|
||||||
|
|
||||||
public class FontLoader : ResourceLoader<Font>
|
public class FontLoader : ResourceLoader<Font>
|
||||||
@@ -10,7 +15,6 @@ public class FontLoader : ResourceLoader<Font>
|
|||||||
".ttf"
|
".ttf"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
protected override Font LoadResource(string path)
|
protected override Font LoadResource(string path)
|
||||||
{
|
{
|
||||||
using Stream stream = VirtualFileSystem.Read(path);
|
using Stream stream = VirtualFileSystem.Read(path);
|
||||||
@@ -21,6 +25,30 @@ public class FontLoader : ResourceLoader<Font>
|
|||||||
|
|
||||||
result.BufferSize = bytesRead;
|
result.BufferSize = bytesRead;
|
||||||
|
|
||||||
|
LoadFaceData(result);
|
||||||
|
result.LoadAsciiData();
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private unsafe void LoadFaceData(Font font)
|
||||||
|
{
|
||||||
|
FT_LibraryRec_* lib;
|
||||||
|
FT_Error error = FT_Init_FreeType(&lib);
|
||||||
|
if (error != 0)
|
||||||
|
throw new Exception("Failed to init FreeType");
|
||||||
|
|
||||||
|
font.LibraryPtr = (nint)lib;
|
||||||
|
|
||||||
|
fixed (byte* data = font.Buffer)
|
||||||
|
{
|
||||||
|
FT_FaceRec_* face;
|
||||||
|
error = FT_New_Memory_Face(lib, data, (nint)font.BufferSize, 0, &face);
|
||||||
|
if (error != 0)
|
||||||
|
throw new Exception("Failed to load font face");
|
||||||
|
|
||||||
|
font.UnitsPerEm = face->units_per_EM;
|
||||||
|
font.FacePtr = (nint)face;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -65,7 +65,12 @@ namespace Voile.Resources
|
|||||||
var resource = ResourceManager.LoadedResources[resourceGuid];
|
var resource = ResourceManager.LoadedResources[resourceGuid];
|
||||||
|
|
||||||
ResourceManager.RemoveResource(resourceGuid);
|
ResourceManager.RemoveResource(resourceGuid);
|
||||||
resource.Dispose();
|
|
||||||
|
if (resource is IDisposable disposable)
|
||||||
|
{
|
||||||
|
disposable.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
using Voile.UI;
|
|
||||||
|
|
||||||
namespace Voile.Resources;
|
|
||||||
|
|
||||||
public class StyleLoader : ResourceLoader<Style>
|
|
||||||
{
|
|
||||||
public override IEnumerable<string> SupportedExtensions =>
|
|
||||||
[
|
|
||||||
".toml"
|
|
||||||
];
|
|
||||||
|
|
||||||
protected override Style LoadResource(string path)
|
|
||||||
{
|
|
||||||
// TODO: implement loading styles.
|
|
||||||
return new Style(string.Empty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using Voile.Resources;
|
using Voile.Resources;
|
||||||
|
|
||||||
namespace Voile
|
namespace Voile
|
||||||
@@ -13,11 +14,35 @@ namespace Voile
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public readonly Guid Guid = Guid.Empty;
|
public readonly Guid Guid = Guid.Empty;
|
||||||
public bool HasValue => Guid != Guid.Empty;
|
public bool HasValue => Guid != Guid.Empty;
|
||||||
/// <summary>
|
|
||||||
/// Retrieve a reference.
|
|
||||||
/// </summary>
|
|
||||||
public T Value => ResourceManager.GetResource<T>(Guid);
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves a <see cref="Resource"/>.<br />
|
||||||
|
/// This will throw an <see cref="InvalidOperationException"/> if the resource wasn't loaded or is invalid. <br />
|
||||||
|
/// You can check if resource was loaded with <see cref="HasValue"/>, or consider using <see cref="TryGetValue"/>.
|
||||||
|
/// </summary>
|
||||||
|
public T Value => ResourceManager.GetResource<T>(Guid)
|
||||||
|
?? throw new InvalidOperationException($"Resource with GUID {Guid} is not loaded or invalid.");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves a resource or <c>null</c> if the resource wasn't loaded or is invalid.
|
||||||
|
/// </summary>
|
||||||
|
public T? ValueOrNull => ResourceManager.GetResource<T>(Guid);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to retrieve a <see cref="Resource"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">An instance of a retrieved <see cref="Resource"/>.</param>
|
||||||
|
/// <returns><c>true</c> if the resource was successfully retrieved, otherwise <c>false</c>.</returns>
|
||||||
|
public bool TryGetValue([NotNullWhen(true)] out T? value)
|
||||||
|
{
|
||||||
|
value = ResourceManager.GetResource<T>(Guid);
|
||||||
|
return value != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create an empty <see cref="ResourceRef"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
public static ResourceRef<T> Empty()
|
public static ResourceRef<T> Empty()
|
||||||
{
|
{
|
||||||
return new ResourceRef<T>(Guid.Empty);
|
return new ResourceRef<T>(Guid.Empty);
|
||||||
@@ -27,12 +52,34 @@ namespace Voile
|
|||||||
{
|
{
|
||||||
Guid = guid;
|
Guid = guid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override bool Equals(object? obj)
|
||||||
|
{
|
||||||
|
return obj is ResourceRef<T> other && Guid.Equals(other.Guid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return Guid.GetHashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator ==(ResourceRef<T>? left, ResourceRef<T>? right)
|
||||||
|
{
|
||||||
|
if (ReferenceEquals(left, right)) return true;
|
||||||
|
if (left is null || right is null) return false;
|
||||||
|
return left.Guid == right.Guid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool operator !=(ResourceRef<T>? left, ResourceRef<T>? right)
|
||||||
|
{
|
||||||
|
return !(left == right);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents data usable by Voile.
|
/// Represents data usable by Voile.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class Resource : IDisposable
|
public abstract class Resource
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Path to this resource.
|
/// Path to this resource.
|
||||||
@@ -43,9 +90,21 @@ namespace Voile
|
|||||||
{
|
{
|
||||||
Path = path;
|
Path = path;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose()
|
/// <summary>
|
||||||
{
|
/// Represents a Resource that requires systems to react to its changes.
|
||||||
}
|
/// </summary>
|
||||||
|
public interface IUpdatableResource
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether this resource's state has changed and needs to be reloaded.
|
||||||
|
/// </summary>
|
||||||
|
bool Dirty { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Marks this resource as updated.
|
||||||
|
/// </summary>
|
||||||
|
void MarkUpdated();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -231,7 +231,7 @@ namespace Voile.Resources
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="path">Path to the resource.</param>
|
/// <param name="path">Path to the resource.</param>
|
||||||
/// <returns>True if a resource at the specified path is loaded, otherwise false.</returns>
|
/// <returns>True if a resource at the specified path is loaded, otherwise false.</returns>
|
||||||
public bool IsResourceLoaded(string path) => _resourcePathMap.ContainsKey(path);
|
public static bool IsResourceLoaded(string path) => _resourcePathMap.ContainsKey(path);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a resource loader associated with a resource type.
|
/// Adds a resource loader associated with a resource type.
|
||||||
@@ -269,7 +269,6 @@ namespace Voile.Resources
|
|||||||
| NotifyFilters.CreationTime
|
| NotifyFilters.CreationTime
|
||||||
| NotifyFilters.DirectoryName
|
| NotifyFilters.DirectoryName
|
||||||
| NotifyFilters.FileName
|
| NotifyFilters.FileName
|
||||||
| NotifyFilters.LastAccess
|
|
||||||
| NotifyFilters.LastWrite
|
| NotifyFilters.LastWrite
|
||||||
| NotifyFilters.Security
|
| NotifyFilters.Security
|
||||||
| NotifyFilters.Size;
|
| NotifyFilters.Size;
|
||||||
@@ -351,7 +350,6 @@ namespace Voile.Resources
|
|||||||
{ typeof(Sound), new SoundLoader()},
|
{ typeof(Sound), new SoundLoader()},
|
||||||
{typeof(Texture2d), new Texture2dLoader()},
|
{typeof(Texture2d), new Texture2dLoader()},
|
||||||
{typeof(Font), new FontLoader()},
|
{typeof(Font), new FontLoader()},
|
||||||
{ typeof(Style), new StyleLoader()}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private static readonly Dictionary<Type, object> _resourceSaverAssociations = new()
|
private static readonly Dictionary<Type, object> _resourceSaverAssociations = new()
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public class ParticleEmitterSettingsResourceLoader : ResourceLoader<ParticleEmit
|
|||||||
{
|
{
|
||||||
var settings = new ParticleEmitterSettings();
|
var settings = new ParticleEmitterSettings();
|
||||||
|
|
||||||
var reader = new TomlDataReader("ParticleEmitterSettings");
|
var reader = new TomlDataReader();
|
||||||
reader.Read(VirtualFileSystem.Read(path));
|
reader.Read(VirtualFileSystem.Read(path));
|
||||||
|
|
||||||
settings.Local = reader.GetBool("Local", true);
|
settings.Local = reader.GetBool("Local", true);
|
||||||
|
|||||||
94
Voile/Source/UI/Anchor.cs
Normal file
94
Voile/Source/UI/Anchor.cs
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace Voile.UI;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies predefined anchor points used to position UI elements relative to their parent container.
|
||||||
|
/// </summary>
|
||||||
|
public enum Anchor
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Anchors the element to the top-left corner of the parent.
|
||||||
|
/// </summary>
|
||||||
|
TopLeft,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Anchors the element to the top-center of the parent.
|
||||||
|
/// </summary>
|
||||||
|
TopCenter,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Anchors the element to the top-right corner of the parent.
|
||||||
|
/// </summary>
|
||||||
|
TopRight,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Anchors the element to the center-left edge of the parent.
|
||||||
|
/// </summary>
|
||||||
|
CenterLeft,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Anchors the element to the exact center of the parent.
|
||||||
|
/// </summary>
|
||||||
|
Center,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Anchors the element to the center-right edge of the parent.
|
||||||
|
/// </summary>
|
||||||
|
CenterRight,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Anchors the element to the bottom-left corner of the parent.
|
||||||
|
/// </summary>
|
||||||
|
BottomLeft,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Anchors the element to the bottom-center of the parent.
|
||||||
|
/// </summary>
|
||||||
|
BottomCenter,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Anchors the element to the bottom-right corner of the parent.
|
||||||
|
/// </summary>
|
||||||
|
BottomRight,
|
||||||
|
Fill
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Provides extension methods for calculating anchored positions of UI elements.
|
||||||
|
/// </summary>
|
||||||
|
public static class AnchorExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Calculates the offset position for an element based on the specified <see cref="Anchor"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="anchor">The anchor mode to use.</param>
|
||||||
|
/// <param name="parentPosition">The absolute position of the parent container (top-left corner).</param>
|
||||||
|
/// <param name="parentRect">The bounding rectangle of the parent container.</param>
|
||||||
|
/// <param name="elementRect">The size of the element being anchored.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// A <see cref="Vector2"/> representing the local offset position where the element should be placed inside the parent.
|
||||||
|
/// </returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// The result is the relative offset from the parent's origin, not a global position.
|
||||||
|
/// </remarks>
|
||||||
|
public static Vector2 Calculate(this Anchor anchor, Vector2 parentPosition, Rect parentRect, Rect elementRect)
|
||||||
|
{
|
||||||
|
var size = new Vector2(elementRect.Width, elementRect.Height);
|
||||||
|
var parentSize = new Vector2(parentRect.Width, parentRect.Height);
|
||||||
|
|
||||||
|
return anchor switch
|
||||||
|
{
|
||||||
|
Anchor.TopLeft => Vector2.Zero,
|
||||||
|
Anchor.TopCenter => new Vector2((parentSize.X - size.X) / 2, 0),
|
||||||
|
Anchor.TopRight => new Vector2(parentSize.X - size.X, 0),
|
||||||
|
Anchor.CenterLeft => new Vector2(0, (parentSize.Y - size.Y) / 2),
|
||||||
|
Anchor.Center => (parentSize - size) / 2,
|
||||||
|
Anchor.CenterRight => new Vector2(parentSize.X - size.X, (parentSize.Y - size.Y) / 2),
|
||||||
|
Anchor.BottomLeft => new Vector2(0, parentSize.Y - size.Y),
|
||||||
|
Anchor.BottomCenter => new Vector2((parentSize.X - size.X) / 2, parentSize.Y - size.Y),
|
||||||
|
Anchor.BottomRight => parentSize - size,
|
||||||
|
_ => Vector2.Zero
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,45 +6,20 @@ namespace Voile.UI.Containers;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// A base class for all UI containers, used to position and rendering child <see cref="IElement">s.
|
/// A base class for all UI containers, used to position and rendering child <see cref="IElement">s.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class Container : IElement, IParentableElement, IUpdatableElement, IResizeableElement, IRenderableElement
|
public abstract class Container : UIElement, IParentableElement
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IReadOnlyList<IElement> Children => _children;
|
public IReadOnlyList<UIElement> Children => _children;
|
||||||
/// <inheritdoc />
|
|
||||||
public Vector2 Position { get; set; }
|
|
||||||
/// <inheritdoc />
|
|
||||||
public Rect MinimumRect { get; set; } = Rect.Zero;
|
|
||||||
/// <inheritdoc />
|
|
||||||
public Rect Size
|
|
||||||
{
|
|
||||||
get => _size;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_size = value;
|
|
||||||
|
|
||||||
if (value.Width < MinimumRect.Width)
|
|
||||||
{
|
|
||||||
_size.Width = MinimumRect.Width;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value.Height < MinimumRect.Height)
|
|
||||||
{
|
|
||||||
_size.Height = MinimumRect.Height;
|
|
||||||
}
|
|
||||||
|
|
||||||
MarkDirty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool Visible { get; set; } = true;
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool Dirty => _isDirty;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Specifies if this <see cref="Container"/>'s minimum size will be confined to contents.
|
/// Specifies if this <see cref="Container"/>'s minimum size will be confined to contents.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool ConfineToContents { get; set; } = false;
|
public bool ConfineToContents { get; set; } = false;
|
||||||
|
|
||||||
|
public override string? StyleElementName => nameof(Container);
|
||||||
|
|
||||||
|
public override Rect MinimumSize => _minimumSize;
|
||||||
|
|
||||||
public Container()
|
public Container()
|
||||||
{
|
{
|
||||||
MarkDirty();
|
MarkDirty();
|
||||||
@@ -52,29 +27,31 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
|||||||
|
|
||||||
public Container(Rect minimumSize)
|
public Container(Rect minimumSize)
|
||||||
{
|
{
|
||||||
MinimumRect = minimumSize;
|
_minimumSize = minimumSize;
|
||||||
|
|
||||||
MarkDirty();
|
MarkDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Container(Rect minimumSize, List<IElement> children)
|
public Container(Rect minimumSize, List<UIElement> children)
|
||||||
{
|
{
|
||||||
MinimumRect = minimumSize;
|
_minimumSize = minimumSize;
|
||||||
_children = children;
|
_children = children;
|
||||||
|
|
||||||
MarkDirty();
|
MarkDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
protected override void OnUpdate()
|
||||||
{
|
{
|
||||||
if (!_isDirty) return;
|
|
||||||
_isDirty = false;
|
|
||||||
|
|
||||||
|
|
||||||
foreach (var child in _children)
|
foreach (var child in _children)
|
||||||
{
|
{
|
||||||
if (child is not IUpdatableElement updatable) continue;
|
if (child is not IUpdatableElement updatable) continue;
|
||||||
|
|
||||||
updatable.Update();
|
updatable.Update();
|
||||||
|
|
||||||
|
if (child is IAnchorableElement anchorable)
|
||||||
|
{
|
||||||
|
anchorable.ApplyAnchor(GlobalPosition, Size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Arrange();
|
Arrange();
|
||||||
@@ -85,7 +62,17 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MarkDirty() => _isDirty = true;
|
public override void MarkDirty()
|
||||||
|
{
|
||||||
|
base.MarkDirty();
|
||||||
|
|
||||||
|
foreach (var child in _children)
|
||||||
|
{
|
||||||
|
if (child is not IUpdatableElement updatable) continue;
|
||||||
|
|
||||||
|
updatable.MarkDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called when this <see cref="Container"/> has to rearrange its children.
|
/// Called when this <see cref="Container"/> has to rearrange its children.
|
||||||
@@ -105,7 +92,7 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
|||||||
|
|
||||||
foreach (var child in Children)
|
foreach (var child in Children)
|
||||||
{
|
{
|
||||||
var pos = child.Position;
|
var pos = child.GlobalPosition;
|
||||||
var size = child.Size;
|
var size = child.Size;
|
||||||
|
|
||||||
minX = MathF.Min(minX, pos.X);
|
minX = MathF.Min(minX, pos.X);
|
||||||
@@ -119,26 +106,33 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
|||||||
float occupiedWidth = (maxX - minX) + padding * 2;
|
float occupiedWidth = (maxX - minX) + padding * 2;
|
||||||
float occupiedHeight = (maxY - minY) + padding * 2;
|
float occupiedHeight = (maxY - minY) + padding * 2;
|
||||||
|
|
||||||
float finalWidth = MathF.Max(occupiedWidth, MinimumRect.Width);
|
float finalWidth = MathF.Max(occupiedWidth, _minimumSize.Width);
|
||||||
float finalHeight = MathF.Max(occupiedHeight, MinimumRect.Height);
|
float finalHeight = MathF.Max(occupiedHeight, _minimumSize.Height);
|
||||||
|
|
||||||
MinimumRect = new Rect(finalWidth, finalHeight);
|
var finalSize = new Rect(finalWidth, finalHeight);
|
||||||
|
|
||||||
if (MinimumRect > _size)
|
if (finalSize != Size)
|
||||||
{
|
{
|
||||||
_size = MinimumRect;
|
Size = finalSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_minimumSize > Size)
|
||||||
|
{
|
||||||
|
Size = _minimumSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddChild(IElement child)
|
public void AddChild(UIElement child)
|
||||||
{
|
{
|
||||||
|
// child.StyleSheetOverride = StyleSheet;
|
||||||
_children.Add(child);
|
_children.Add(child);
|
||||||
|
child.SetParent(this);
|
||||||
|
|
||||||
MarkDirty();
|
MarkDirty();
|
||||||
Update();
|
Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveChild(IElement child)
|
public void RemoveChild(UIElement child)
|
||||||
{
|
{
|
||||||
_children.Remove(child);
|
_children.Remove(child);
|
||||||
|
|
||||||
@@ -146,22 +140,21 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
|||||||
Update();
|
Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Render(RenderSystem renderer, Style style)
|
protected override void OnRender(RenderSystem renderer, Style style)
|
||||||
{
|
{
|
||||||
foreach (var child in Children)
|
foreach (var child in Children)
|
||||||
{
|
{
|
||||||
if (child is not IRenderableElement renderable) continue;
|
if (child is not IRenderableElement renderable) continue;
|
||||||
renderable.Render(renderer, style);
|
|
||||||
|
if (!child.TryGetStyle(StyleSheet.Value, out var childStyle))
|
||||||
|
{
|
||||||
|
childStyle = new Style();
|
||||||
|
}
|
||||||
|
|
||||||
|
renderable.Render(renderer, childStyle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawSize(RenderSystem renderer)
|
private List<UIElement> _children = new();
|
||||||
{
|
private Rect _minimumSize = Rect.Zero;
|
||||||
renderer.SetTransform(Position, Vector2.Zero);
|
|
||||||
renderer.DrawRectangleOutline(new Vector2(Size.Width, Size.Height), Color.Red, 2.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<IElement> _children = new();
|
|
||||||
private bool _isDirty;
|
|
||||||
private Rect _size = Rect.Zero;
|
|
||||||
}
|
}
|
||||||
63
Voile/Source/UI/Containers/FillContainer.cs
Normal file
63
Voile/Source/UI/Containers/FillContainer.cs
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
using Voile.Rendering;
|
||||||
|
|
||||||
|
namespace Voile.UI.Containers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A special container that occupies the entire available size of the parent. <br />
|
||||||
|
/// Usually used as a root element for the UI system.
|
||||||
|
/// </summary>
|
||||||
|
public class FillContainer : Container
|
||||||
|
{
|
||||||
|
public FillContainer()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public FillContainer(Rect minimumSize) : base(minimumSize)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Arrange()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnRender(RenderSystem renderer, Style style)
|
||||||
|
{
|
||||||
|
base.OnRender(renderer, style);
|
||||||
|
|
||||||
|
Rect parentSize;
|
||||||
|
|
||||||
|
if (Parent != null)
|
||||||
|
{
|
||||||
|
parentSize = Parent.Size;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var windowSize = renderer.WindowSize;
|
||||||
|
var windowRect = new Rect(windowSize.X, windowSize.Y);
|
||||||
|
|
||||||
|
parentSize = windowRect;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_lastParentSize != parentSize)
|
||||||
|
{
|
||||||
|
Size = parentSize;
|
||||||
|
_lastParentSize = parentSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnUpdate()
|
||||||
|
{
|
||||||
|
base.OnUpdate();
|
||||||
|
Size = _lastParentSize;
|
||||||
|
|
||||||
|
if (Children.Count != 0)
|
||||||
|
{
|
||||||
|
Children[0].Size = Size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Rect _lastParentSize = Rect.Zero;
|
||||||
|
}
|
||||||
@@ -78,16 +78,16 @@ public class FlexContainer : Container
|
|||||||
|
|
||||||
public FlexContainer() : base() { }
|
public FlexContainer() : base() { }
|
||||||
|
|
||||||
public FlexContainer(Rect minimumSize, List<IElement> children) : base(minimumSize, children) { }
|
public FlexContainer(Rect minimumSize, List<UIElement> children) : base(minimumSize, children) { }
|
||||||
|
|
||||||
public override void Arrange()
|
public override void Arrange()
|
||||||
{
|
{
|
||||||
float containerMainSize = (Direction == FlexDirection.Row) ? Size.Width : Size.Height;
|
float containerMainSize = (Direction == FlexDirection.Row) ? Size.Width : Size.Height;
|
||||||
float mainPos = (Direction == FlexDirection.Row) ? Position.X : Position.Y;
|
float mainPos = 0.0f;
|
||||||
float crossPos = (Direction == FlexDirection.Row) ? Position.Y : Position.X;
|
float crossPos = 0.0f;
|
||||||
|
|
||||||
List<List<IElement>> lines = new();
|
List<List<UIElement>> lines = new();
|
||||||
List<IElement> currentLine = new();
|
List<UIElement> currentLine = new();
|
||||||
float lineMainSum = 0f;
|
float lineMainSum = 0f;
|
||||||
float maxCross = 0f;
|
float maxCross = 0f;
|
||||||
|
|
||||||
@@ -136,7 +136,7 @@ public class FlexContainer : Container
|
|||||||
? new Vector2(currentMain, alignedCross)
|
? new Vector2(currentMain, alignedCross)
|
||||||
: new Vector2(alignedCross, currentMain);
|
: new Vector2(alignedCross, currentMain);
|
||||||
|
|
||||||
child.Position = childPos;
|
child.LocalPosition = childPos;
|
||||||
|
|
||||||
currentMain += GetMainSize(childSize) + Gap;
|
currentMain += GetMainSize(childSize) + Gap;
|
||||||
}
|
}
|
||||||
@@ -148,7 +148,7 @@ public class FlexContainer : Container
|
|||||||
private Vector2 GetChildSize(IElement child)
|
private Vector2 GetChildSize(IElement child)
|
||||||
{
|
{
|
||||||
if (child is IResizeableElement resizeable)
|
if (child is IResizeableElement resizeable)
|
||||||
return new Vector2(resizeable.MinimumRect.Width, resizeable.MinimumRect.Height);
|
return new Vector2(resizeable.MinimumSize.Width, resizeable.MinimumSize.Height);
|
||||||
return new Vector2(child.Size.Width, child.Size.Height);
|
return new Vector2(child.Size.Width, child.Size.Height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public class GridContainer : Container
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public float RowSpacing { get; set; } = 16.0f;
|
public float RowSpacing { get; set; } = 16.0f;
|
||||||
|
|
||||||
public GridContainer(Rect minimumSize, List<IElement> children, int columns = 2, float colSpacing = 16.0f, float rowSpacing = 16.0f)
|
public GridContainer(Rect minimumSize, List<UIElement> children, int columns = 2, float colSpacing = 16.0f, float rowSpacing = 16.0f)
|
||||||
: base(minimumSize, children)
|
: base(minimumSize, children)
|
||||||
{
|
{
|
||||||
Columns = columns;
|
Columns = columns;
|
||||||
@@ -38,16 +38,13 @@ public class GridContainer : Container
|
|||||||
|
|
||||||
public override void Arrange()
|
public override void Arrange()
|
||||||
{
|
{
|
||||||
float startX = Position.X;
|
float currentX = 0.0f;
|
||||||
float startY = Position.Y;
|
float currentY = 0.0f;
|
||||||
|
|
||||||
float currentX = startX;
|
|
||||||
float currentY = startY;
|
|
||||||
int colIndex = 0;
|
int colIndex = 0;
|
||||||
|
|
||||||
foreach (var child in Children)
|
foreach (var child in Children)
|
||||||
{
|
{
|
||||||
child.Position = new Vector2(currentX, currentY);
|
child.LocalPosition = new Vector2(currentX, currentY);
|
||||||
|
|
||||||
float childWidth = 0.0f;
|
float childWidth = 0.0f;
|
||||||
float childHeight = 0.0f;
|
float childHeight = 0.0f;
|
||||||
@@ -60,7 +57,7 @@ public class GridContainer : Container
|
|||||||
if (colIndex >= Columns)
|
if (colIndex >= Columns)
|
||||||
{
|
{
|
||||||
colIndex = 0;
|
colIndex = 0;
|
||||||
currentX = startX;
|
currentX = 0.0f;
|
||||||
currentY += childHeight + RowSpacing;
|
currentY += childHeight + RowSpacing;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public class HorizontalContainer : Container
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public float Spacing { get; set; } = 16.0f;
|
public float Spacing { get; set; } = 16.0f;
|
||||||
|
|
||||||
public HorizontalContainer(Rect minimumSize, List<IElement> children, float spacing = 16.0f) : base(minimumSize, children)
|
public HorizontalContainer(Rect minimumSize, List<UIElement> children, float spacing = 16.0f) : base(minimumSize, children)
|
||||||
{
|
{
|
||||||
Spacing = spacing;
|
Spacing = spacing;
|
||||||
}
|
}
|
||||||
@@ -25,13 +25,13 @@ public class HorizontalContainer : Container
|
|||||||
|
|
||||||
public override void Arrange()
|
public override void Arrange()
|
||||||
{
|
{
|
||||||
float currentX = Position.X;
|
float currentX = 0.0f;
|
||||||
|
|
||||||
for (int i = 0; i < Children.Count; i++)
|
for (int i = 0; i < Children.Count; i++)
|
||||||
{
|
{
|
||||||
var child = Children[i];
|
var child = Children[i];
|
||||||
var pos = new Vector2(currentX, Position.Y);
|
var pos = new Vector2(currentX, 0.0f);
|
||||||
child.Position = pos;
|
child.LocalPosition = pos;
|
||||||
|
|
||||||
currentX += child.Size.Width;
|
currentX += child.Size.Width;
|
||||||
|
|
||||||
|
|||||||
49
Voile/Source/UI/Containers/MarginContainer.cs
Normal file
49
Voile/Source/UI/Containers/MarginContainer.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace Voile.UI.Containers;
|
||||||
|
|
||||||
|
public class MarginContainer : Container
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The margin to apply around the contents of this container.
|
||||||
|
/// </summary>
|
||||||
|
public Size Margin { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies if this <see cref="MarginContainer"/> will fill to parent size.
|
||||||
|
/// </summary>
|
||||||
|
public bool Fill { get; set; } = true;
|
||||||
|
|
||||||
|
public MarginContainer() : this(new Size()) { }
|
||||||
|
|
||||||
|
public MarginContainer(Size margin)
|
||||||
|
{
|
||||||
|
Margin = margin;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnUpdate()
|
||||||
|
{
|
||||||
|
base.OnUpdate();
|
||||||
|
if (Parent == null) return;
|
||||||
|
|
||||||
|
if (Size != Parent.Size)
|
||||||
|
{
|
||||||
|
Size = Parent.Size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Arrange()
|
||||||
|
{
|
||||||
|
foreach (var child in Children)
|
||||||
|
{
|
||||||
|
var newPosition = new Vector2(Margin.Left, Margin.Top);
|
||||||
|
var newSize = new Rect(
|
||||||
|
Size.Width - Margin.Left - Margin.Right,
|
||||||
|
Size.Height - Margin.Top - Margin.Bottom
|
||||||
|
);
|
||||||
|
|
||||||
|
child.Size = newSize;
|
||||||
|
child.LocalPosition = newPosition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@ public class VerticalContainer : Container
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public float Spacing { get; set; } = 16.0f;
|
public float Spacing { get; set; } = 16.0f;
|
||||||
|
|
||||||
public VerticalContainer(Rect minimumSize, List<IElement> children, float spacing = 16.0f) : base(minimumSize, children)
|
public VerticalContainer(Rect minimumSize, List<UIElement> children, float spacing = 16.0f) : base(minimumSize, children)
|
||||||
{
|
{
|
||||||
Spacing = spacing;
|
Spacing = spacing;
|
||||||
}
|
}
|
||||||
@@ -25,13 +25,13 @@ public class VerticalContainer : Container
|
|||||||
|
|
||||||
public override void Arrange()
|
public override void Arrange()
|
||||||
{
|
{
|
||||||
float currentY = Position.Y;
|
float currentY = 0.0f;
|
||||||
|
|
||||||
for (int i = 0; i < Children.Count; i++)
|
for (int i = 0; i < Children.Count; i++)
|
||||||
{
|
{
|
||||||
var child = Children[i];
|
var child = Children[i];
|
||||||
var pos = new Vector2(Position.X, currentY);
|
var pos = new Vector2(0.0f, currentY);
|
||||||
child.Position = pos;
|
child.LocalPosition = pos;
|
||||||
|
|
||||||
currentY += child.Size.Height;
|
currentY += child.Size.Height;
|
||||||
|
|
||||||
|
|||||||
@@ -1,63 +1,78 @@
|
|||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Voile.Input;
|
|
||||||
using Voile.Rendering;
|
using Voile.Rendering;
|
||||||
|
|
||||||
namespace Voile.UI;
|
namespace Voile.UI;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a basic UI element with position and size information.
|
||||||
|
/// </summary>
|
||||||
public interface IElement
|
public interface IElement
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This element's position in pixels relative to the viewport top-left edge.
|
/// This element's position in pixels relative to the viewport top-left edge.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Vector2 Position { get; set; }
|
public Vector2 GlobalPosition { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The size of this element.
|
/// The size of this element.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Rect Size { get; set; }
|
public Rect Size { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a UI element that can contain child elements.
|
||||||
|
/// </summary>
|
||||||
public interface IParentableElement
|
public interface IParentableElement
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This parentable element's children.
|
/// This parentable element's children.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IReadOnlyList<IElement> Children { get; }
|
public IReadOnlyList<UIElement> Children { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add a child element to this element.
|
/// Add a child element to this element.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="child">Child <see cref="IElement"/>.</param>
|
/// <param name="child">Child <see cref="UIElement"/>.</param>
|
||||||
public void AddChild(IElement child);
|
public void AddChild(UIElement child);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Remove a child element from this element.
|
/// Remove a child element from this element.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="child">Child <see cref="IElement"/> to remove.</param>
|
/// <param name="child">Child <see cref="UIElement"/> to remove.</param>
|
||||||
public void RemoveChild(IElement child);
|
public void RemoveChild(UIElement child);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a UI element that can provide a minimum size constraint.<br />
|
||||||
|
/// Implement this interface if your UI element is expected to be resizeable.
|
||||||
|
/// </summary>
|
||||||
public interface IResizeableElement
|
public interface IResizeableElement
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a minimum rectangle size for this element.
|
/// Get a minimum rectangle size for this element.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract Rect MinimumRect { get; }
|
public abstract Rect MinimumSize { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a UI element that supports updates when its state changes.
|
||||||
|
/// </summary>
|
||||||
public interface IUpdatableElement
|
public interface IUpdatableElement
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Specifies if this element's properties have changed, making it necessary to update it.
|
/// Gets a value indicating whether the element's state has changed and needs to be updated.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Dirty { get; }
|
public bool Dirty { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Update this element.
|
/// Update this element.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void Update();
|
void Update(float dt = 0.0f);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marks this element as changed, requiring an update.
|
/// Marks this element as changed, requiring an update.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void MarkDirty();
|
void MarkDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a UI element that can be rendered to the screen.
|
||||||
|
/// </summary>
|
||||||
public interface IRenderableElement
|
public interface IRenderableElement
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -77,6 +92,9 @@ public interface IRenderableElement
|
|||||||
public void DrawSize(RenderSystem renderer);
|
public void DrawSize(RenderSystem renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a UI element that can receive and process user input.
|
||||||
|
/// </summary>
|
||||||
public interface IInputElement
|
public interface IInputElement
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -89,3 +107,24 @@ public interface IInputElement
|
|||||||
/// <param name="action">Input action to send.</param>
|
/// <param name="action">Input action to send.</param>
|
||||||
void Input(UIInputContext action);
|
void Input(UIInputContext action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a UI element that supports positional anchoring within a parent.
|
||||||
|
/// </summary>
|
||||||
|
public interface IAnchorableElement
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the anchor point relative to the parent container.
|
||||||
|
/// </summary>
|
||||||
|
public Anchor Anchor { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets an additional offset to apply after anchoring, in pixels.
|
||||||
|
/// </summary>
|
||||||
|
public Vector2 AnchorOffset { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Applies the current anchor settings based on the parent's position and size.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parentPosition">The parent's top-left global position.</param>
|
||||||
|
/// <param name="parentRect">The bounding rectangle of the parent container.</param>
|
||||||
|
public void ApplyAnchor(Vector2 parentPosition, Rect parentRect);
|
||||||
|
}
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
namespace Voile.UI;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a rectangle. Used to determine widget confines for UI layout.
|
|
||||||
/// </summary>
|
|
||||||
public record Rect(float Width = 0.0f, float Height = 0.0f)
|
|
||||||
{
|
|
||||||
public float Width { get; set; } = Width;
|
|
||||||
public float Height { get; set; } = Height;
|
|
||||||
public static Rect Zero => new Rect(0.0f, 0.0f);
|
|
||||||
public float Area => Width * Height;
|
|
||||||
|
|
||||||
public int CompareTo(Rect? other)
|
|
||||||
{
|
|
||||||
if (other is null) return 1;
|
|
||||||
return Area.CompareTo(other.Area);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator >(Rect left, Rect right) => left.CompareTo(right) > 0;
|
|
||||||
public static bool operator <(Rect left, Rect right) => left.CompareTo(right) < 0;
|
|
||||||
public static bool operator >=(Rect left, Rect right) => left.CompareTo(right) >= 0;
|
|
||||||
public static bool operator <=(Rect left, Rect right) => left.CompareTo(right) <= 0;
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,230 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using Voile.Resources;
|
using Voile.Resources;
|
||||||
|
using Voile.Resources.DataReaders;
|
||||||
|
using Voile.UI.Containers;
|
||||||
|
using Voile.VFS;
|
||||||
|
|
||||||
namespace Voile.UI;
|
namespace Voile.UI;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A resource containing UI style settings.
|
/// UI style settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Style : TextDataResource
|
public class Style
|
||||||
{
|
{
|
||||||
public Style(string path) : base(path)
|
public enum AnimationType
|
||||||
{
|
{
|
||||||
|
Linear,
|
||||||
|
EaseIn,
|
||||||
|
EaseOut,
|
||||||
|
EaseInOut
|
||||||
|
}
|
||||||
|
|
||||||
|
public float TransitionDuration = 0f;
|
||||||
|
public AnimationType TransitionType = AnimationType.Linear;
|
||||||
|
|
||||||
|
public Style() { }
|
||||||
|
|
||||||
|
public Size? Padding { get; set; }
|
||||||
|
public Color? BackgroundColor { get; set; }
|
||||||
|
public Size? BorderSize { get; set; }
|
||||||
|
public Color? BorderColor { get; set; }
|
||||||
|
public float CornerRadius { get; set; }
|
||||||
|
public Color? TextColor { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Merges this <see cref="Style"/> with a different one.<br />
|
||||||
|
/// Properties that are not set for this <see cref="Style"/> will be inherited from <paramref name="overrideStyle"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="overrideStyle"></param>
|
||||||
|
/// <returns>A merged <see cref="Style"/>.</returns>
|
||||||
|
public Style Merge(Style overrideStyle)
|
||||||
|
{
|
||||||
|
return new Style
|
||||||
|
{
|
||||||
|
BackgroundColor = overrideStyle.BackgroundColor != default ? overrideStyle.BackgroundColor : BackgroundColor,
|
||||||
|
TextColor = overrideStyle.TextColor != default ? overrideStyle.TextColor : TextColor,
|
||||||
|
Padding = overrideStyle.Padding != default ? overrideStyle.Padding : Padding,
|
||||||
|
BorderSize = overrideStyle.BorderSize != default ? overrideStyle.BorderSize : BorderSize,
|
||||||
|
BorderColor = overrideStyle.BorderColor != default ? overrideStyle.BorderColor : BorderColor,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class StyleSheetLoader : ResourceLoader<StyleSheet>
|
||||||
|
{
|
||||||
|
public override IEnumerable<string> SupportedExtensions => [".toml"];
|
||||||
|
|
||||||
|
protected override StyleSheet LoadResource(string path)
|
||||||
|
{
|
||||||
|
var result = new StyleSheet(path);
|
||||||
|
var allStyles = new Dictionary<string, Style>();
|
||||||
|
|
||||||
|
using var stream = VirtualFileSystem.Read(path);
|
||||||
|
_reader.Read(stream);
|
||||||
|
|
||||||
|
foreach (var styleKey in _reader.GetSubKeysRecursive())
|
||||||
|
{
|
||||||
|
var subReader = _reader.GetSubReader(styleKey);
|
||||||
|
if (subReader == null || !subReader.Valid())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
var style = ParseStyle(subReader, styleKey);
|
||||||
|
if (style != null)
|
||||||
|
{
|
||||||
|
allStyles[styleKey] = style;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var kvp in allStyles)
|
||||||
|
{
|
||||||
|
var finalStyle = GetMergedStyle(kvp.Key, allStyles);
|
||||||
|
result.Add(kvp.Key, finalStyle);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Style ParseStyle(TomlDataReader reader, string input)
|
||||||
|
{
|
||||||
|
var style = new Style();
|
||||||
|
|
||||||
|
string easingName = reader.GetString("TransitionType", "Linear");
|
||||||
|
|
||||||
|
if (!Enum.TryParse<Style.AnimationType>(easingName, true, out var easing))
|
||||||
|
easing = Style.AnimationType.Linear;
|
||||||
|
|
||||||
|
style.TransitionType = easing;
|
||||||
|
|
||||||
|
|
||||||
|
if (reader.HasKey("BackgroundColor"))
|
||||||
|
style.BackgroundColor = reader.GetColor("BackgroundColor", Color.Transparent);
|
||||||
|
|
||||||
|
if (reader.HasKey("TextColor"))
|
||||||
|
style.TextColor = reader.GetColor("TextColor", Color.Black);
|
||||||
|
|
||||||
|
if (reader.HasKey("Padding"))
|
||||||
|
style.Padding = reader.GetSize("Padding", Size.Zero);
|
||||||
|
|
||||||
|
if (reader.HasKey("BorderSize"))
|
||||||
|
style.BorderSize = reader.GetSize("BorderSize", Size.Zero);
|
||||||
|
|
||||||
|
if (reader.HasKey("BorderColor"))
|
||||||
|
style.BorderColor = reader.GetColor("BorderColor", Color.Transparent);
|
||||||
|
|
||||||
|
return style;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Style GetMergedStyle(string fullKey, Dictionary<string, Style> allStyles)
|
||||||
|
{
|
||||||
|
var parts = fullKey.Split('.');
|
||||||
|
var merged = new Style();
|
||||||
|
|
||||||
|
for (int i = 1; i <= parts.Length; i++)
|
||||||
|
{
|
||||||
|
var subKey = string.Join('.', parts.Take(i));
|
||||||
|
if (allStyles.TryGetValue(subKey, out var parentStyle))
|
||||||
|
{
|
||||||
|
merged = merged.Merge(parentStyle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly TomlDataReader _reader = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StyleSheet : Resource
|
||||||
|
{
|
||||||
|
public StyleSheet(string path) : base(path)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public StyleSheet(Dictionary<string, Style> styles) : base(string.Empty)
|
||||||
|
{
|
||||||
|
_styles = styles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(string key, Style style) => _styles.Add(key, style);
|
||||||
|
|
||||||
|
public bool TryGet(string styleName, [NotNullWhen(true)] out Style? style)
|
||||||
|
{
|
||||||
|
return _styles.TryGetValue(styleName, out style);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static StyleSheet Default => new(new Dictionary<string, Style>()
|
||||||
|
{
|
||||||
|
{"Label", new Style()
|
||||||
|
{
|
||||||
|
TextColor = Color.FromHexString("#161616"),
|
||||||
|
BackgroundColor = Color.DarkRed,
|
||||||
|
BorderSize = new Size(2.0f),
|
||||||
|
BorderColor = Color.Red
|
||||||
|
}},
|
||||||
|
{ "Button", new Style()
|
||||||
|
{
|
||||||
|
Padding = new Size(8.0f),
|
||||||
|
BackgroundColor = Color.FromHexString("#0f62fe"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
{"Button.Normal", new Style()
|
||||||
|
{
|
||||||
|
Padding = new Size(8.0f),
|
||||||
|
BackgroundColor = Color.FromHexString("#0f62fe"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
{"Button.Hovered", new Style()
|
||||||
|
{
|
||||||
|
Padding = new Size(8.0f),
|
||||||
|
BackgroundColor = Color.FromHexString("#0353e9"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
{"Button.Pressed", new Style()
|
||||||
|
{
|
||||||
|
Padding = new Size(8.0f),
|
||||||
|
BackgroundColor = Color.FromHexString("#002d9c"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
{"Button.Danger", new Style()
|
||||||
|
{
|
||||||
|
Padding = new Size(8.0f),
|
||||||
|
BackgroundColor = Color.FromHexString("#da1e28"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
{"Button.Danger.Normal", new Style()
|
||||||
|
{
|
||||||
|
Padding = new Size(8.0f),
|
||||||
|
BackgroundColor = Color.FromHexString("#da1e28"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
{"Button.Danger.Hovered", new Style()
|
||||||
|
{
|
||||||
|
Padding = new Size(8.0f),
|
||||||
|
BackgroundColor = Color.FromHexString("#ba1b23"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
{"Button.Danger.Pressed", new Style()
|
||||||
|
{
|
||||||
|
Padding = new Size(8.0f),
|
||||||
|
BackgroundColor = Color.FromHexString("#750e13"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
{"Container", new Style()
|
||||||
|
{
|
||||||
|
BackgroundColor = Color.FromHexString("#ffffff"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
{"Container.Layer01", new Style()
|
||||||
|
{
|
||||||
|
BackgroundColor = Color.FromHexString("#f4f4f4"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
{"Container.Layer02", new Style()
|
||||||
|
{
|
||||||
|
BackgroundColor = Color.FromHexString("#e8e8e8"),
|
||||||
|
TextColor = Color.FromHexString("#ffffff"),
|
||||||
|
}},
|
||||||
|
});
|
||||||
|
|
||||||
|
private Dictionary<string, Style> _styles = new();
|
||||||
|
}
|
||||||
56
Voile/Source/UI/StyleAnimator.cs
Normal file
56
Voile/Source/UI/StyleAnimator.cs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
namespace Voile.UI;
|
||||||
|
|
||||||
|
public class StyleAnimator
|
||||||
|
{
|
||||||
|
public bool IsComplete => _elapsed >= _duration;
|
||||||
|
|
||||||
|
public StyleAnimator(Style from, Style to, float duration)
|
||||||
|
{
|
||||||
|
_from = from;
|
||||||
|
_to = to;
|
||||||
|
|
||||||
|
_duration = duration;
|
||||||
|
_elapsed = 0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float Ease(float t, Style.AnimationType type)
|
||||||
|
{
|
||||||
|
return type switch
|
||||||
|
{
|
||||||
|
Style.AnimationType.Linear => t,
|
||||||
|
Style.AnimationType.EaseIn => t * t,
|
||||||
|
Style.AnimationType.EaseOut => t * (2 - t),
|
||||||
|
Style.AnimationType.EaseInOut => t < 0.5f
|
||||||
|
? 2 * t * t
|
||||||
|
: -1 + (4 - 2 * t) * t,
|
||||||
|
_ => t
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public Style Update(float deltaTime)
|
||||||
|
{
|
||||||
|
_elapsed = MathF.Min(_elapsed + deltaTime, _duration);
|
||||||
|
float t = _duration == 0 ? 1 : _elapsed / _duration;
|
||||||
|
float easedT = Ease(t, _to.TransitionType);
|
||||||
|
|
||||||
|
return LerpStyle(_from, _to, easedT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Style LerpStyle(Style from, Style to, float t)
|
||||||
|
{
|
||||||
|
var result = new Style()
|
||||||
|
{
|
||||||
|
BackgroundColor = MathUtils.LerpColor(from.BackgroundColor ?? Color.Transparent, to.BackgroundColor ?? Color.Transparent, t),
|
||||||
|
TextColor = MathUtils.LerpColor(from.TextColor ?? Color.Black, to.TextColor ?? Color.Black, t),
|
||||||
|
Padding = MathUtils.LerpSize(from.Padding ?? Size.Zero, to.Padding ?? Size.Zero, t),
|
||||||
|
BorderColor = MathUtils.LerpColor(from.BorderColor ?? Color.Transparent, to.BorderColor ?? Color.Transparent, t),
|
||||||
|
BorderSize = MathUtils.LerpSize(from.BorderSize ?? Size.Zero, to.BorderSize ?? Size.Zero, t),
|
||||||
|
TransitionType = to.TransitionType
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Style _from, _to;
|
||||||
|
private float _duration, _elapsed;
|
||||||
|
}
|
||||||
232
Voile/Source/UI/UIElement.cs
Normal file
232
Voile/Source/UI/UIElement.cs
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Numerics;
|
||||||
|
using System.Text;
|
||||||
|
using Voile.Rendering;
|
||||||
|
|
||||||
|
namespace Voile.UI;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Base class for all UI elements.
|
||||||
|
/// </summary>
|
||||||
|
public abstract class UIElement : IElement, IRenderableElement, IResizeableElement, IUpdatableElement, IAnchorableElement
|
||||||
|
{
|
||||||
|
public bool Visible { get; set; } = true;
|
||||||
|
public bool IgnoreInput { get; set; } = false;
|
||||||
|
public Vector2 LocalPosition { get; set; } = Vector2.Zero;
|
||||||
|
public Vector2 GlobalPosition => _parent?.GlobalPosition + LocalPosition ?? LocalPosition;
|
||||||
|
|
||||||
|
public string StyleName => $"{StyleElementName ?? "UIElement"}{GetStyleVariantString()}{ConstructStyleModifiers(StyleModifiers)}";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An element name for style.
|
||||||
|
/// </summary>
|
||||||
|
public virtual string? StyleElementName { get; }
|
||||||
|
|
||||||
|
public string StyleVariant { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List of style modifiers for this <see cref="UIElement"/>.
|
||||||
|
/// </summary>
|
||||||
|
public virtual string[]? StyleModifiers { get; }
|
||||||
|
|
||||||
|
public ResourceRef<StyleSheet> StyleSheet => Parent?.StyleSheet ?? StyleSheetOverride;
|
||||||
|
public ResourceRef<StyleSheet> StyleSheetOverride { get; set; } = ResourceRef<StyleSheet>.Empty();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parent <see cref="UIElement"/> of this element.
|
||||||
|
/// </summary>
|
||||||
|
public UIElement? Parent => _parent;
|
||||||
|
|
||||||
|
public Rect Size
|
||||||
|
{
|
||||||
|
get => _size;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value.Width < MinimumSize.Width)
|
||||||
|
{
|
||||||
|
_size.Width = MinimumSize.Width;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.Height < MinimumSize.Height)
|
||||||
|
{
|
||||||
|
_size.Height = MinimumSize.Height;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_size != value)
|
||||||
|
{
|
||||||
|
MarkDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
_size = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public Vector2 AnchorOffset { get; set; } = Vector2.Zero;
|
||||||
|
public Anchor Anchor { get; set; } = Anchor.TopLeft;
|
||||||
|
|
||||||
|
public abstract Rect MinimumSize { get; }
|
||||||
|
public bool Dirty => _dirty;
|
||||||
|
|
||||||
|
public bool TryGetStyle(StyleSheet styleSheet, [NotNullWhen(true)] out Style? style)
|
||||||
|
{
|
||||||
|
return styleSheet.TryGet(StyleName, out style);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void MarkDirty()
|
||||||
|
{
|
||||||
|
if (Parent != null && !Parent.Dirty)
|
||||||
|
{
|
||||||
|
Parent.MarkDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
_dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets a parent element for this <see cref="UIElement"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="parent">Element to parent this <see cref="UIElement"/> to.</param>
|
||||||
|
public void SetParent(UIElement parent)
|
||||||
|
{
|
||||||
|
_parent = parent;
|
||||||
|
MarkDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(float dt = 0.0f)
|
||||||
|
{
|
||||||
|
if (!_dirty) return;
|
||||||
|
_dirty = false;
|
||||||
|
|
||||||
|
if (Size == Rect.Zero)
|
||||||
|
Size = MinimumSize;
|
||||||
|
|
||||||
|
OnUpdate();
|
||||||
|
|
||||||
|
if (_parent is not null && _parent.Size != Rect.Zero)
|
||||||
|
{
|
||||||
|
ApplyAnchor(_parent.GlobalPosition, _parent.Size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Render(RenderSystem renderer, Style style)
|
||||||
|
{
|
||||||
|
RenderStyleBox(renderer, style);
|
||||||
|
OnRender(renderer, style);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void OnRender(RenderSystem renderer, Style style);
|
||||||
|
protected abstract void OnUpdate();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Renders a stylebox from a given style.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="renderer"></param>
|
||||||
|
/// <param name="style"></param>
|
||||||
|
protected void RenderStyleBox(RenderSystem renderer, Style style)
|
||||||
|
{
|
||||||
|
var backgroundColor = style.BackgroundColor ?? Color.Transparent;
|
||||||
|
var borderColor = style.BorderColor ?? Color.Transparent;
|
||||||
|
var borderSize = style.BorderSize;
|
||||||
|
|
||||||
|
var borderLeft = borderSize?.Left ?? 0;
|
||||||
|
var borderRight = borderSize?.Right ?? 0;
|
||||||
|
var borderTop = borderSize?.Top ?? 0;
|
||||||
|
var borderBottom = borderSize?.Bottom ?? 0;
|
||||||
|
|
||||||
|
renderer.SetTransform(GlobalPosition, Vector2.Zero);
|
||||||
|
|
||||||
|
renderer.DrawRectangle(new Vector2(Size.Width, Size.Height), backgroundColor);
|
||||||
|
|
||||||
|
if (borderLeft > 0)
|
||||||
|
{
|
||||||
|
renderer.SetTransform(GlobalPosition, Vector2.Zero);
|
||||||
|
renderer.DrawRectangle(
|
||||||
|
new Vector2(borderLeft, Size.Height),
|
||||||
|
borderColor
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (borderTop > 0)
|
||||||
|
{
|
||||||
|
renderer.SetTransform(GlobalPosition, Vector2.Zero);
|
||||||
|
renderer.DrawRectangle(
|
||||||
|
new Vector2(Size.Width, borderTop),
|
||||||
|
borderColor
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (borderRight > 0)
|
||||||
|
{
|
||||||
|
var rightX = GlobalPosition.X + Size.Width - borderRight;
|
||||||
|
renderer.SetTransform(new Vector2(rightX, GlobalPosition.Y), Vector2.Zero);
|
||||||
|
renderer.DrawRectangle(
|
||||||
|
new Vector2(borderRight, Size.Height),
|
||||||
|
borderColor
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (borderBottom > 0)
|
||||||
|
{
|
||||||
|
var bottomY = GlobalPosition.Y + Size.Height - borderBottom;
|
||||||
|
renderer.SetTransform(new Vector2(GlobalPosition.X, bottomY), Vector2.Zero);
|
||||||
|
renderer.DrawRectangle(
|
||||||
|
new Vector2(Size.Width, borderBottom),
|
||||||
|
borderColor
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawSize(RenderSystem renderer)
|
||||||
|
{
|
||||||
|
renderer.SetTransform(GlobalPosition, Vector2.Zero);
|
||||||
|
renderer.DrawRectangleOutline(new Vector2(Size.Width, Size.Height), Color.Red, 2.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if this <see cref="UIElement"/> contains a point within its confines.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="pointPosition">A global position of the point.</param>
|
||||||
|
/// <returns>True if the point is inside the widget; otherwise, false.</returns>
|
||||||
|
public bool ContainsPoint(Vector2 point)
|
||||||
|
{
|
||||||
|
return point.X >= GlobalPosition.X && point.Y >= GlobalPosition.Y &&
|
||||||
|
point.X <= GlobalPosition.X + Size.Width &&
|
||||||
|
point.Y <= GlobalPosition.Y + Size.Height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Applies this <see cref="UIElement"/> anchor.
|
||||||
|
/// </summary>
|
||||||
|
public virtual void ApplyAnchor(Vector2 parentPosition, Rect parentRect)
|
||||||
|
{
|
||||||
|
LocalPosition = Anchor.Calculate(parentPosition, parentRect, Size) + new Vector2(AnchorOffset.X, AnchorOffset.Y);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string ConstructStyleModifiers(string[]? modifiers)
|
||||||
|
{
|
||||||
|
if (modifiers == null)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
foreach (var modifier in modifiers)
|
||||||
|
{
|
||||||
|
sb.Append($".{modifier}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetStyleVariantString()
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(StyleVariant))
|
||||||
|
return string.Empty;
|
||||||
|
|
||||||
|
return $".{StyleVariant}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool _dirty = true;
|
||||||
|
private Rect _size = Rect.Zero;
|
||||||
|
|
||||||
|
private UIElement? _parent;
|
||||||
|
}
|
||||||
@@ -3,19 +3,48 @@ using Voile.Input;
|
|||||||
|
|
||||||
namespace Voile.UI;
|
namespace Voile.UI;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Input information for UI elements.
|
||||||
|
/// </summary>
|
||||||
public class UIInputContext
|
public class UIInputContext
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Current action handled by this <see cref="UIElement"/>.
|
||||||
|
/// </summary>
|
||||||
public IInputAction Action { get; }
|
public IInputAction Action { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// Current mouse position.
|
||||||
|
/// </summary>
|
||||||
public Vector2 MousePosition { get; }
|
public Vector2 MousePosition { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if a mouse button was pressed.
|
||||||
|
/// </summary>
|
||||||
public bool MousePressed { get; set; }
|
public bool MousePressed { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if a mouse button was released.
|
||||||
|
/// </summary>
|
||||||
public bool MouseReleased { get; set; }
|
public bool MouseReleased { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if a mouse button is currently held.
|
||||||
|
/// </summary>
|
||||||
public bool MouseDown { get; set; }
|
public bool MouseDown { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Name of the current <see cref="IInputAction"/>.
|
||||||
|
/// </summary>
|
||||||
public string ActionName { get; }
|
public string ActionName { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// Keycode of a currently pressed character.
|
||||||
|
/// </summary>
|
||||||
public int CharPressed { get; }
|
public int CharPressed { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if this <see cref="UIInputContext"/> registered any character input from keyboard.
|
||||||
|
/// </summary>
|
||||||
public bool HasCharInput => CharPressed != 0;
|
public bool HasCharInput => CharPressed != 0;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if this context's input was already handled and no longer needs to be processed.
|
||||||
|
/// </summary>
|
||||||
public bool Handled => _handled;
|
public bool Handled => _handled;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,39 +1,50 @@
|
|||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Voile.Input;
|
using Voile.Input;
|
||||||
using Voile.Rendering;
|
using Voile.Rendering;
|
||||||
|
using Voile.Resources;
|
||||||
|
|
||||||
namespace Voile.UI;
|
namespace Voile.UI;
|
||||||
|
|
||||||
public class UISystem : IUpdatableSystem, IRenderableSystem
|
public class UISystem : IUpdatableSystem, IRenderableSystem, IReloadableSystem
|
||||||
{
|
{
|
||||||
public IReadOnlyList<IElement> Elements => _elements;
|
public IReadOnlyList<IElement> Elements => _elements;
|
||||||
|
|
||||||
public bool RenderDebugRects { get; set; }
|
public bool RenderDebugRects { get; set; }
|
||||||
|
public Color DebugSizeRectColor { get; set; } = Color.Red;
|
||||||
|
public Color DebugDirtyRectColor { get; set; } = new Color(1.0f, 1.0f, 0.0f, 0.5f);
|
||||||
|
|
||||||
public UISystem(InputSystem inputSystem)
|
public UISystem(InputSystem inputSystem)
|
||||||
{
|
{
|
||||||
_style = ResourceRef<Style>.Empty();
|
|
||||||
_input = inputSystem;
|
_input = inputSystem;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UISystem(InputSystem inputSystem, ResourceRef<Style> style)
|
public UISystem(InputSystem inputSystem, List<UIElement> elements)
|
||||||
{
|
{
|
||||||
_input = inputSystem;
|
_input = inputSystem;
|
||||||
_style = style;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UISystem(InputSystem inputSystem, ResourceRef<Style> style, List<IElement> elements)
|
|
||||||
{
|
|
||||||
_input = inputSystem;
|
|
||||||
_style = style;
|
|
||||||
_elements = elements;
|
_elements = elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddElement(IElement element) => _elements.Add(element);
|
public void SetStyleSheet(ResourceRef<StyleSheet> styleSheet)
|
||||||
public void RemoveElement(IElement element) => _elements.Remove(element);
|
{
|
||||||
|
_styleSheet = styleSheet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddElement(UIElement element)
|
||||||
|
{
|
||||||
|
element.StyleSheetOverride = _styleSheet;
|
||||||
|
_elements.Add(element);
|
||||||
|
_inputElementIndices.Add(element.GlobalPosition, _elements.Count - 1);
|
||||||
|
}
|
||||||
|
public void RemoveElement(UIElement element) => _elements.Remove(element);
|
||||||
|
|
||||||
public void Update(double deltaTime)
|
public void Update(double deltaTime)
|
||||||
{
|
{
|
||||||
|
// HandleInput();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Render(RenderSystem renderer)
|
||||||
|
{
|
||||||
|
// Update elements each time UI system is rendered.
|
||||||
HandleInput();
|
HandleInput();
|
||||||
|
|
||||||
foreach (var element in _elements)
|
foreach (var element in _elements)
|
||||||
@@ -41,29 +52,54 @@ public class UISystem : IUpdatableSystem, IRenderableSystem
|
|||||||
if (element is not IUpdatableElement updatable) continue;
|
if (element is not IUpdatableElement updatable) continue;
|
||||||
updatable.Update();
|
updatable.Update();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void Render(RenderSystem renderer)
|
|
||||||
{
|
|
||||||
foreach (var element in _elements)
|
foreach (var element in _elements)
|
||||||
{
|
{
|
||||||
if (element is IRenderableElement renderable)
|
if (element is IRenderableElement renderable)
|
||||||
{
|
{
|
||||||
renderable.Render(renderer, _style.Value);
|
var styleSheet = _styleSheet.Value;
|
||||||
|
|
||||||
if (!RenderDebugRects) return;
|
if (!styleSheet.TryGet(element.StyleName, out var style))
|
||||||
renderable.DrawSize(renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (element is IParentableElement parentable)
|
|
||||||
{
|
|
||||||
foreach (var child in parentable.Children)
|
|
||||||
{
|
{
|
||||||
if (child is not IRenderableElement renderableChild) continue;
|
style = new Style();
|
||||||
|
|
||||||
if (!RenderDebugRects) return;
|
|
||||||
renderableChild.DrawSize(renderer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderable.Render(renderer, style);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!RenderDebugRects) return;
|
||||||
|
|
||||||
|
foreach (var element in _elements)
|
||||||
|
{
|
||||||
|
if (element is not UIElement uiElement) continue;
|
||||||
|
DrawDebugForElement(renderer, uiElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Reload()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawDebugForElement(RenderSystem renderer, UIElement element)
|
||||||
|
{
|
||||||
|
var size = new Vector2(element.Size.Width, element.Size.Height);
|
||||||
|
renderer.SetTransform(element.GlobalPosition, Vector2.Zero);
|
||||||
|
renderer.DrawRectangleOutline(size, DebugSizeRectColor);
|
||||||
|
|
||||||
|
if (element.Dirty)
|
||||||
|
{
|
||||||
|
renderer.DrawRectangle(size, DebugDirtyRectColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element is IParentableElement parentableElement)
|
||||||
|
{
|
||||||
|
foreach (var child in parentableElement.Children)
|
||||||
|
{
|
||||||
|
if (child is not UIElement childElement) continue;
|
||||||
|
DrawDebugForElement(renderer, childElement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -109,18 +145,20 @@ public class UISystem : IUpdatableSystem, IRenderableSystem
|
|||||||
{
|
{
|
||||||
MouseDown = _input.IsMouseButtonDown(MouseButton.Left),
|
MouseDown = _input.IsMouseButtonDown(MouseButton.Left),
|
||||||
MouseReleased = _input.IsMouseButtonReleased(MouseButton.Left),
|
MouseReleased = _input.IsMouseButtonReleased(MouseButton.Left),
|
||||||
MousePressed = _input.IsMouseButtonReleased(MouseButton.Left),
|
MousePressed = _input.IsMouseButtonPressed(MouseButton.Left),
|
||||||
};
|
};
|
||||||
PropagateInput(_elements, context);
|
PropagateInput(_elements, context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool PropagateInput(List<IElement> elements, UIInputContext context)
|
private bool PropagateInput(List<UIElement> elements, UIInputContext context)
|
||||||
{
|
{
|
||||||
for (int i = elements.Count - 1; i >= 0; i--)
|
for (int i = elements.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
var element = elements[i];
|
var element = elements[i];
|
||||||
|
|
||||||
|
// if (!element.ContainsPoint(context.MousePosition)) continue;
|
||||||
|
|
||||||
if (element is IInputElement inputElement && !inputElement.IgnoreInput)
|
if (element is IInputElement inputElement && !inputElement.IgnoreInput)
|
||||||
{
|
{
|
||||||
inputElement.Input(context);
|
inputElement.Input(context);
|
||||||
@@ -138,9 +176,11 @@ public class UISystem : IUpdatableSystem, IRenderableSystem
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResourceRef<Style> _style;
|
private ResourceRef<StyleSheet> _styleSheet;
|
||||||
private List<IElement> _elements = new();
|
private List<UIElement> _elements = new();
|
||||||
private InputSystem _input;
|
private InputSystem _input;
|
||||||
|
|
||||||
|
private GridSet<int> _inputElementIndices = new();
|
||||||
|
|
||||||
private Vector2 _lastMousePosition = Vector2.Zero;
|
private Vector2 _lastMousePosition = Vector2.Zero;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Voile.Input;
|
|
||||||
using Voile.Rendering;
|
using Voile.Rendering;
|
||||||
|
using Voile.Resources;
|
||||||
|
using Voile.UI.Containers;
|
||||||
|
|
||||||
namespace Voile.UI.Widgets;
|
namespace Voile.UI.Widgets;
|
||||||
|
|
||||||
@@ -17,26 +18,153 @@ public enum ButtonState
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Button : Widget
|
public class Button : Widget
|
||||||
{
|
{
|
||||||
public string Label { get; set; } = "Button";
|
public string Text
|
||||||
public override Rect MinimumRect => new Rect(Width: 128.0f, Height: 64.0f);
|
|
||||||
|
|
||||||
public Button(string label, Action pressedAction)
|
|
||||||
{
|
{
|
||||||
Label = label;
|
get => _text; set
|
||||||
|
{
|
||||||
|
_text = value;
|
||||||
|
MarkDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ButtonState CurrentState { get; private set; } = ButtonState.Normal;
|
||||||
|
|
||||||
|
public override Rect MinimumSize => Padding + _textSize;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// <see cref="FontSet"/> to use with this button.
|
||||||
|
/// </summary>
|
||||||
|
public FontSet FontSet { get; set; } = new();
|
||||||
|
public Size Padding
|
||||||
|
{
|
||||||
|
get => _padding; set
|
||||||
|
{
|
||||||
|
_padding = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string? StyleElementName => nameof(Button);
|
||||||
|
public override string[]? StyleModifiers =>
|
||||||
|
[
|
||||||
|
CurrentState.ToString()
|
||||||
|
];
|
||||||
|
|
||||||
|
public Button(string text, ResourceRef<Font> fontOverride, Action pressedAction)
|
||||||
|
{
|
||||||
|
_text = text;
|
||||||
_pressedAction = pressedAction;
|
_pressedAction = pressedAction;
|
||||||
|
|
||||||
|
FontSet.AddFont(fontOverride);
|
||||||
|
|
||||||
|
MarkDirty();
|
||||||
|
Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Render(RenderSystem renderer, Style style)
|
public Button(string text, FontSet fontSet)
|
||||||
{
|
{
|
||||||
// TODO: use a button color from style.
|
_text = text;
|
||||||
renderer.SetTransform(Position, Vector2.Zero);
|
|
||||||
renderer.DrawRectangle(new Vector2(MinimumRect.Width, MinimumRect.Height), new Color(0.25f, 0.25f, 0.25f));
|
FontSet = fontSet;
|
||||||
|
|
||||||
|
MarkDirty();
|
||||||
|
Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Button(string text, FontSet fontSet, Action pressedAction)
|
||||||
|
{
|
||||||
|
_text = text;
|
||||||
|
_pressedAction = pressedAction;
|
||||||
|
|
||||||
|
FontSet = fontSet;
|
||||||
|
|
||||||
|
MarkDirty();
|
||||||
|
Update();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnRender(RenderSystem renderer, Style style)
|
||||||
|
{
|
||||||
|
if (_padding != style.Padding)
|
||||||
|
{
|
||||||
|
MarkDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
_padding = style.Padding ?? Voile.Size.Zero;
|
||||||
|
var textColor = style.TextColor ?? Color.Black;
|
||||||
|
|
||||||
|
var textPosition = new Vector2(GlobalPosition.X + Padding.Left, GlobalPosition.Y + Padding.Top);
|
||||||
|
renderer.SetTransform(textPosition, Vector2.Zero);
|
||||||
|
renderer.DrawText(_suitableFont, _text, textColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Pressed() { }
|
||||||
|
|
||||||
protected override void OnInput(UIInputContext action)
|
protected override void OnInput(UIInputContext action)
|
||||||
{
|
{
|
||||||
|
bool isHovering = ContainsPoint(action.MousePosition);
|
||||||
|
|
||||||
|
if (action.MousePressed && isHovering)
|
||||||
|
{
|
||||||
|
_isHeldDown = true;
|
||||||
|
CurrentState = ButtonState.Pressed;
|
||||||
|
}
|
||||||
|
else if (action.MouseReleased)
|
||||||
|
{
|
||||||
|
if (_isHeldDown && isHovering)
|
||||||
|
{
|
||||||
|
_pressedAction?.Invoke();
|
||||||
|
Pressed();
|
||||||
|
}
|
||||||
|
|
||||||
|
_isHeldDown = false;
|
||||||
|
CurrentState = isHovering ? ButtonState.Hovered : ButtonState.Normal;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (_isHeldDown)
|
||||||
|
{
|
||||||
|
CurrentState = ButtonState.Pressed; // keep showing as pressed
|
||||||
|
}
|
||||||
|
else if (isHovering)
|
||||||
|
{
|
||||||
|
CurrentState = ButtonState.Hovered;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CurrentState = ButtonState.Normal;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Action _pressedAction;
|
protected override void OnUpdate()
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
|
private Size _padding;
|
||||||
|
|
||||||
|
private bool _isHeldDown;
|
||||||
}
|
}
|
||||||
@@ -1,38 +1,85 @@
|
|||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Voile.Input;
|
using Voile.Input;
|
||||||
using Voile.Rendering;
|
using Voile.Rendering;
|
||||||
|
using Voile.Resources;
|
||||||
|
|
||||||
namespace Voile.UI.Widgets;
|
namespace Voile.UI.Widgets;
|
||||||
|
|
||||||
public class Label : Widget
|
public class Label : Widget
|
||||||
{
|
{
|
||||||
public override Rect MinimumRect => throw new NotImplementedException();
|
public override Rect MinimumSize => _textSize;
|
||||||
|
|
||||||
public string Text { get; set; } = "Hello World!";
|
public string Text
|
||||||
|
|
||||||
public Label(string text)
|
|
||||||
{
|
{
|
||||||
Text = text;
|
get => _text; set
|
||||||
|
{
|
||||||
|
_text = value;
|
||||||
|
MarkDirty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string? StyleElementName => nameof(Label);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// <see cref="FontSet"/> to use with this label.
|
||||||
|
/// </summary>
|
||||||
|
public FontSet FontSet { get; set; } = new();
|
||||||
|
|
||||||
public Label(string text, ResourceRef<Font> fontOverride)
|
public Label(string text, ResourceRef<Font> fontOverride)
|
||||||
{
|
{
|
||||||
Text = text;
|
_text = text;
|
||||||
_fontOverride = fontOverride;
|
|
||||||
|
FontSet.AddFont(fontOverride);
|
||||||
|
|
||||||
|
MarkDirty();
|
||||||
|
Update();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Label(string text, FontSet fontSet)
|
||||||
|
{
|
||||||
|
_text = text;
|
||||||
|
|
||||||
|
FontSet = fontSet;
|
||||||
|
|
||||||
|
MarkDirty();
|
||||||
|
Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnInput(UIInputContext action)
|
protected override void OnInput(UIInputContext action)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Render(RenderSystem renderer, Style style)
|
protected override void OnRender(RenderSystem renderer, Style style)
|
||||||
{
|
{
|
||||||
// TODO: use style here.
|
renderer.SetTransform(GlobalPosition, Vector2.Zero);
|
||||||
if (!_fontOverride.HasValue) return;
|
renderer.DrawText(_suitableFont, _text, style.TextColor ?? Color.Black);
|
||||||
renderer.SetTransform(Position, Vector2.Zero);
|
|
||||||
renderer.DrawText(_fontOverride, Text, Color.White);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResourceRef<Font> _fontOverride = ResourceRef<Font>.Empty();
|
protected override void OnUpdate()
|
||||||
|
{
|
||||||
|
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 = _textSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResourceRef<Font> _suitableFont = ResourceRef<Font>.Empty();
|
||||||
|
|
||||||
|
private string _text = "Hello, World!";
|
||||||
|
private Rect _textSize = Rect.Zero;
|
||||||
}
|
}
|
||||||
@@ -6,11 +6,11 @@ namespace Voile.UI.Widgets;
|
|||||||
|
|
||||||
public class RectangleWidget : Widget
|
public class RectangleWidget : Widget
|
||||||
{
|
{
|
||||||
public override Rect MinimumRect { get; }
|
public override Rect MinimumSize { get; }
|
||||||
public Color Color { get; set; } = Color.White;
|
public Color Color { get; set; } = Color.White;
|
||||||
public RectangleWidget(Rect minimumRect, Color color) : base()
|
public RectangleWidget(Rect minimumRect, Color color) : base()
|
||||||
{
|
{
|
||||||
MinimumRect = minimumRect;
|
MinimumSize = minimumRect;
|
||||||
Color = color;
|
Color = color;
|
||||||
|
|
||||||
_defaultColor = color;
|
_defaultColor = color;
|
||||||
@@ -20,9 +20,9 @@ public class RectangleWidget : Widget
|
|||||||
_hoverColor = color.Lightened(0.25f);
|
_hoverColor = color.Lightened(0.25f);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Render(RenderSystem renderer, Style style)
|
protected override void OnRender(RenderSystem renderer, Style style)
|
||||||
{
|
{
|
||||||
renderer.SetTransform(Position, Vector2.Zero);
|
renderer.SetTransform(GlobalPosition, Vector2.Zero);
|
||||||
renderer.DrawRectangle(new Vector2(Size.Width, Size.Height), Color);
|
renderer.DrawRectangle(new Vector2(Size.Width, Size.Height), Color);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,15 +49,15 @@ public class RectangleWidget : Widget
|
|||||||
Color = _defaultColor;
|
Color = _defaultColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mouseInside && inputContext.MouseDown)
|
// if (mouseInside && inputContext.MouseDown)
|
||||||
{
|
// {
|
||||||
Size = _downSize;
|
// Size = _downSize;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (mouseInside && inputContext.MouseReleased)
|
// if (mouseInside && inputContext.MouseReleased)
|
||||||
{
|
// {
|
||||||
Size = _defaultSize;
|
// Size = _defaultSize;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (mouseInside && inputContext.MousePressed)
|
if (mouseInside && inputContext.MousePressed)
|
||||||
{
|
{
|
||||||
@@ -65,6 +65,11 @@ public class RectangleWidget : Widget
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void OnUpdate()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private Color _defaultColor;
|
private Color _defaultColor;
|
||||||
private Color _hoverColor;
|
private Color _hoverColor;
|
||||||
|
|
||||||
|
|||||||
@@ -7,13 +7,8 @@ namespace Voile.UI.Widgets;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// A base class for all UI widgets.
|
/// A base class for all UI widgets.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class Widget : IElement, IRenderableElement, IInputElement, IResizeableElement, IUpdatableElement
|
public abstract class Widget : UIElement, IInputElement
|
||||||
{
|
{
|
||||||
public bool Visible { get; set; } = true;
|
|
||||||
public bool IgnoreInput { get; set; }
|
|
||||||
public Vector2 Position { get; set; } = Vector2.Zero;
|
|
||||||
public Rect Size { get; set; } = new();
|
|
||||||
|
|
||||||
public Widget()
|
public Widget()
|
||||||
{
|
{
|
||||||
MarkDirty();
|
MarkDirty();
|
||||||
@@ -27,24 +22,13 @@ public abstract class Widget : IElement, IRenderableElement, IInputElement, IRes
|
|||||||
|
|
||||||
public Widget(Vector2 position)
|
public Widget(Vector2 position)
|
||||||
{
|
{
|
||||||
Position = position;
|
LocalPosition = position;
|
||||||
MarkDirty();
|
MarkDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// </inheritdoc>
|
|
||||||
public abstract Rect MinimumRect { get; }
|
|
||||||
|
|
||||||
public bool Dirty => _isDirty;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called when its time to draw this widget.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="renderer"></param>
|
|
||||||
public abstract void Render(RenderSystem renderer, Style style);
|
|
||||||
|
|
||||||
public void Input(UIInputContext context)
|
public void Input(UIInputContext context)
|
||||||
{
|
{
|
||||||
if (context.Handled) return;
|
if (context.Handled || IgnoreInput) return;
|
||||||
OnInput(context);
|
OnInput(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,39 +36,5 @@ public abstract class Widget : IElement, IRenderableElement, IInputElement, IRes
|
|||||||
/// Called when this widget receives input.
|
/// Called when this widget receives input.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="action">An input action this widget received.</param>
|
/// <param name="action">An input action this widget received.</param>
|
||||||
protected abstract void OnInput(UIInputContext action);
|
protected abstract void OnInput(UIInputContext context);
|
||||||
|
|
||||||
public void Update()
|
|
||||||
{
|
|
||||||
if (!_isDirty) return;
|
|
||||||
_isDirty = false;
|
|
||||||
|
|
||||||
if (Size == Rect.Zero)
|
|
||||||
{
|
|
||||||
Size = MinimumRect;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void MarkDirty() => _isDirty = true;
|
|
||||||
|
|
||||||
public void DrawSize(RenderSystem renderer)
|
|
||||||
{
|
|
||||||
renderer.SetTransform(Position, Vector2.Zero);
|
|
||||||
renderer.DrawRectangleOutline(new Vector2(Size.Width, Size.Height), Color.Red, 2.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if this <see cref="Widget"/> contains a point within its confines.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="pointPosition">A global position of the point.</param>
|
|
||||||
/// <returns>True if the point is inside the widget; otherwise, false.</returns>
|
|
||||||
public bool ContainsPoint(Vector2 pointPosition)
|
|
||||||
{
|
|
||||||
return pointPosition.X >= Position.X &&
|
|
||||||
pointPosition.Y >= Position.Y &&
|
|
||||||
pointPosition.X <= Position.X + Size.Width &&
|
|
||||||
pointPosition.Y <= Position.Y + Size.Height;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool _isDirty = true;
|
|
||||||
}
|
}
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
namespace Voile
|
|
||||||
{
|
|
||||||
// Based on https://github.com/ppr-game/PPR/blob/engine/PER.Util/src/Color.cs
|
|
||||||
/// <summary>
|
|
||||||
/// A record struct representing a color.
|
|
||||||
/// </summary>
|
|
||||||
public record struct Color
|
|
||||||
{
|
|
||||||
// TODO: add more HTML colors.
|
|
||||||
public static Color AliceBlue = new(0xF0F8FF);
|
|
||||||
public static Color AntiqueWhite = new(0xFAEBD7);
|
|
||||||
public static Color Aqua = new(0x00FFFF);
|
|
||||||
public static Color Aquamarine = new(0x7FFFD4);
|
|
||||||
public static Color Azure = new(0xF0FFFF);
|
|
||||||
public static Color Beige = new(0xF5F5DC);
|
|
||||||
public static Color Bisque = new(0xFFE4C4);
|
|
||||||
public static Color Black = new(0x000000);
|
|
||||||
public static Color BlanchedAlmond = new(0xFFEBCD);
|
|
||||||
public static Color Blue = new(0x0000FF);
|
|
||||||
public static Color BlueViolet = new(0x8A2BE2);
|
|
||||||
public static Color Brown = new(0xA52A2A);
|
|
||||||
public static Color BurlyWood = new(0xDEB887);
|
|
||||||
public static Color CadetBlue = new(0x5F9EA0);
|
|
||||||
public static Color Chartreuse = new(0x7FFF00);
|
|
||||||
public static Color Chocolate = new(0xD2691E);
|
|
||||||
public static Color Coral = new(0xFF7F50);
|
|
||||||
public static Color CornflowerBlue = new(0x6495ED);
|
|
||||||
public static Color Cornsilk = new(0xFFF8DC);
|
|
||||||
public static Color Crimson = new(0xDC143C);
|
|
||||||
public static Color Cyan = new(0x00FFFF);
|
|
||||||
public static Color DarkBlue = new(0x00008B);
|
|
||||||
public static Color DarkCyan = new(0x008B8B);
|
|
||||||
public static Color White = new(0xFFFFFF);
|
|
||||||
public static Color Green = new(0x00FF00);
|
|
||||||
public static Color Red = new(0xFF0000);
|
|
||||||
|
|
||||||
public byte R { get; set; }
|
|
||||||
public byte G { get; set; }
|
|
||||||
public byte B { get; set; }
|
|
||||||
public byte A { get; set; } = 255;
|
|
||||||
|
|
||||||
public int Argb
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
int a = A << 24;
|
|
||||||
int r = R << 16;
|
|
||||||
int g = G << 8;
|
|
||||||
int b = B;
|
|
||||||
|
|
||||||
return a | r | g | b;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color(float r, float g, float b, float a = 1.0f)
|
|
||||||
{
|
|
||||||
R = (byte)Math.Clamp(r * 255, 0, 255);
|
|
||||||
G = (byte)Math.Clamp(g * 255, 0, 255);
|
|
||||||
B = (byte)Math.Clamp(b * 255, 0, 255);
|
|
||||||
A = (byte)Math.Clamp(a * 255, 0, 255);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color(byte r, byte g, byte b, byte a = 255)
|
|
||||||
{
|
|
||||||
R = r;
|
|
||||||
G = g;
|
|
||||||
B = b;
|
|
||||||
A = a;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color(int hex)
|
|
||||||
{
|
|
||||||
A = 255; // Default alpha to 255 if not provided
|
|
||||||
B = (byte)(hex & 0xFF);
|
|
||||||
G = (byte)((hex >> 8) & 0xFF);
|
|
||||||
R = (byte)((hex >> 16) & 0xFF);
|
|
||||||
if (hex > 0xFFFFFF) // If the hex value includes alpha
|
|
||||||
{
|
|
||||||
A = (byte)((hex >> 24) & 0xFF);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Color FromHexString(string hex)
|
|
||||||
{
|
|
||||||
if (hex.StartsWith("#"))
|
|
||||||
{
|
|
||||||
hex = hex[1..];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hex.Length == 6)
|
|
||||||
{
|
|
||||||
int rgb = int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
|
|
||||||
return new Color(rgb);
|
|
||||||
}
|
|
||||||
else if (hex.Length == 8)
|
|
||||||
{
|
|
||||||
int rgba = int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
|
|
||||||
return new Color(rgba);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new ArgumentException("Invalid hex color format. Use #RRGGBB or #RRGGBBAA.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color Lightened(float amount)
|
|
||||||
{
|
|
||||||
var result = this;
|
|
||||||
result.R = (byte)Math.Min(255, R + (255 - R) * amount);
|
|
||||||
result.G = (byte)Math.Min(255, G + (255 - G) * amount);
|
|
||||||
result.B = (byte)Math.Min(255, B + (255 - B) * amount);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color Darkened(float amount)
|
|
||||||
{
|
|
||||||
var result = this;
|
|
||||||
result.R = (byte)(R * (1.0f - amount));
|
|
||||||
result.G = (byte)(G * (1.0f - amount));
|
|
||||||
result.B = (byte)(B * (1.0f - amount));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public System.Drawing.Color ToSystemColor()
|
|
||||||
{
|
|
||||||
var result = System.Drawing.Color.FromArgb(Argb);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -15,6 +15,18 @@ namespace Voile
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||||
public static float Lerp(float a, float b, float t) => t <= 0f ? a : t >= 1f ? b : LerpUnclamped(a, b, t);
|
public static float Lerp(float a, float b, float t) => t <= 0f ? a : t >= 1f ? b : LerpUnclamped(a, b, t);
|
||||||
|
|
||||||
|
public static Size LerpSize(Size a, Size b, float t)
|
||||||
|
{
|
||||||
|
t = Math.Clamp(t, 0f, 1f);
|
||||||
|
|
||||||
|
float left = Lerp(a.Left, b.Left, t);
|
||||||
|
float right = Lerp(a.Right, b.Right, t);
|
||||||
|
float top = Lerp(a.Top, b.Top, t);
|
||||||
|
float bottom = Lerp(a.Bottom, b.Bottom, t);
|
||||||
|
|
||||||
|
return new Size(left, right, top, bottom);
|
||||||
|
}
|
||||||
|
|
||||||
public static Color LerpColor(Color colorA, Color colorB, float t)
|
public static Color LerpColor(Color colorA, Color colorB, float t)
|
||||||
{
|
{
|
||||||
t = Math.Clamp(t, 0f, 1f);
|
t = Math.Clamp(t, 0f, 1f);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ public class FileSystemFile : VirtualFile
|
|||||||
|
|
||||||
public override Stream GetStream()
|
public override Stream GetStream()
|
||||||
{
|
{
|
||||||
return new FileStream(_fsPath, FileMode.Open, FileAccess.Read);
|
return new FileStream(_fsPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string _fsPath;
|
private string _fsPath;
|
||||||
|
|||||||
@@ -2,13 +2,14 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="FreeTypeSharp" Version="3.0.0" />
|
||||||
<PackageReference Include="Silk.NET.WebGPU" Version="2.20.0" />
|
<PackageReference Include="Silk.NET.WebGPU" Version="2.20.0" />
|
||||||
<PackageReference Include="Silk.NET.WebGPU.Native.WGPU" Version="2.20.0" />
|
<PackageReference Include="Silk.NET.WebGPU.Native.WGPU" Version="2.20.0" />
|
||||||
<PackageReference Include="Silk.NET.Windowing" Version="2.20.0" />
|
<PackageReference Include="Silk.NET.Windowing" Version="2.20.0" />
|
||||||
@@ -16,8 +17,6 @@
|
|||||||
<PackageReference Include="Tommy" Version="3.1.2" />
|
<PackageReference Include="Tommy" Version="3.1.2" />
|
||||||
<PackageReference Include="ImGui.NET" Version="1.89.4" />
|
<PackageReference Include="ImGui.NET" Version="1.89.4" />
|
||||||
<PackageReference Include="Raylib-cs" Version="7.0.1" />
|
<PackageReference Include="Raylib-cs" Version="7.0.1" />
|
||||||
<PackageReference Include="SharpFont" Version="4.0.1" />
|
|
||||||
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta19" />
|
|
||||||
<PackageReference Include="StbImageSharp" Version="2.27.13" />
|
<PackageReference Include="StbImageSharp" Version="2.27.13" />
|
||||||
<PackageReference Include="StbVorbisSharp" Version="1.22.4" />
|
<PackageReference Include="StbVorbisSharp" Version="1.22.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user