Compare commits

..

5 Commits

8 changed files with 180 additions and 14 deletions

View File

@@ -80,6 +80,8 @@
- ~~Positioning (anchors)~~ - ~~Positioning (anchors)~~
- ~~Move layouting to Render instead of Update, use Update for input.~~ - ~~Move layouting to Render instead of Update, use Update for input.~~
- Input propagation - Input propagation
- Use GridSet to efficiently query inputtable UI elements.
- Add element focus logic, make them focusable with action inputs.
- Basic input elements (button, text field, toggle). - Basic input elements (button, text field, toggle).
- Styling - Styling
- Add style settings for UI panels (for buttons, labels, etc.). - Add style settings for UI panels (for buttons, labels, etc.).

View File

@@ -81,21 +81,16 @@ public class TestGame : Game
var lastChild = _container.Children.Last(); var lastChild = _container.Children.Last();
_container.RemoveChild(lastChild); _container.RemoveChild(lastChild);
} }
if (Input.IsMouseButtonDown(MouseButton.Left))
{
var mousePos = Input.GetMousePosition();
_fillContainer.Size = new Rect(mousePos.X, mousePos.Y);
}
} }
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);

37
Voile/GridSet.cs Normal file
View File

@@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using Voile.Extensions;
namespace Voile;
public class GridSet<T>
{
public float GridSize { get; }
public GridSet(float gridSize = 32.0f)
{
GridSize = gridSize;
}
public void Add(Vector2 position, T child)
{
var snap = Vector2.One * GridSize;
position = position.Snapped(snap);
if (_values.TryGetValue(position, out var list))
{
list.Add(child);
}
else
{
_values.Add(position, new List<T>());
}
}
public void Remove(T child)
{
}
private Dictionary<Vector2, List<T>> _values = new();
}

View File

@@ -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);
}
} }
} }

View File

@@ -1,5 +1,18 @@
using System.Numerics;
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>
@@ -18,4 +31,24 @@ public class Font : Resource
{ {
Buffer = buffer; Buffer = buffer;
} }
public void Measure(string text)
{
foreach (char c in text)
{
}
}
internal void AddGlyph(char c, Glyph glyph)
{
_glyphs.Add(c, glyph);
}
internal void GetGlyphBoundingBox(Glyph glyph)
{
}
private Dictionary<char, Glyph> _glyphs = new();
} }

View File

@@ -1,6 +1,13 @@
using System.Numerics;
using System.Runtime.InteropServices;
using FreeTypeSharp;
using Voile.VFS; using Voile.VFS;
using static FreeTypeSharp.FT;
using static FreeTypeSharp.FT_LOAD;
using static FreeTypeSharp.FT_Render_Mode_;
namespace Voile.Resources; namespace Voile.Resources;
public class FontLoader : ResourceLoader<Font> public class FontLoader : ResourceLoader<Font>
@@ -10,7 +17,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 +27,68 @@ public class FontLoader : ResourceLoader<Font>
result.BufferSize = bytesRead; result.BufferSize = bytesRead;
LoadFaceData(result);
return result; return result;
} }
private unsafe void LoadFaceData(Font font)
{
LoadFreeType();
fixed (FT_LibraryRec_** lib = &_lib)
{
fixed (FT_FaceRec_* face = &_face)
{
FT_Error error;
var buffer = new Memory<byte>(font.Buffer);
var handle = buffer.Pin();
error = FT_New_Memory_Face(*lib, (byte*)handle.Pointer, (nint)font.BufferSize, 0, &face);
error = FT_Set_Pixel_Sizes(face, 0, (uint)font.Size);
error = FT_Select_Charmap(face, FT_Encoding_.FT_ENCODING_UNICODE);
uint gid;
var charcode = FT_Get_First_Char(face, &gid);
while (gid != 0)
{
Console.WriteLine($"Codepoint: {(char)charcode}, gid {gid}");
charcode = FT_Get_Next_Char(face, charcode, &gid);
}
error = FT_Load_Char(face, 'F', FT_LOAD_NO_BITMAP);
var metrics = face->glyph->metrics;
font.AddGlyph('F', new Glyph()
{
Width = metrics.width >> 6,
Height = metrics.height >> 6,
Bearing = new Vector2(metrics.horiBearingX >> 6, metrics.horiBearingY >> 6),
Advance = (int)metrics.horiAdvance >> 6,
});
FT_Done_Face(face);
FT_Done_FreeType(*lib);
}
}
}
private unsafe void LoadFreeType()
{
fixed (FT_LibraryRec_** lib = &_lib)
{
FT_Error error;
if (_lib == null)
{
error = FT_Init_FreeType(lib);
}
}
}
private unsafe FT_LibraryRec_* _lib;
private unsafe FT_FaceRec_ _face;
} }

View File

@@ -31,7 +31,11 @@ public class UISystem : IUpdatableSystem, IRenderableSystem
_elements = elements; _elements = elements;
} }
public void AddElement(UIElement element) => _elements.Add(element); public void AddElement(UIElement element)
{
_elements.Add(element);
_inputElementIndices.Add(element.GlobalPosition, _elements.Count - 1);
}
public void RemoveElement(UIElement element) => _elements.Remove(element); public void RemoveElement(UIElement element) => _elements.Remove(element);
public void Update(double deltaTime) public void Update(double deltaTime)
@@ -167,5 +171,7 @@ public class UISystem : IUpdatableSystem, IRenderableSystem
private List<UIElement> _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;
} }

View File

@@ -9,6 +9,7 @@
</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>