WIP: GridSet, add Vector2.Snapped extension method.

This commit is contained in:
2025-06-25 19:50:03 +02:00
parent 64d3dba42d
commit 8a1e359c22
4 changed files with 75 additions and 5 deletions

View File

@@ -92,10 +92,11 @@ public class TestGame : Game
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

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