Add RemoveChild to IParentableElement, only resize Container if children overflow it.

This commit is contained in:
2025-06-20 19:14:29 +02:00
parent 3154b3fa10
commit a9a8113dd9
4 changed files with 28 additions and 9 deletions

View File

@@ -73,9 +73,16 @@ public class TestGame : Game
_container.AddChild(new RectangleWidget(new Rect(32.0f, 32.0f), MathUtils.RandomColor())); _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);
}
if (Input.IsMouseButtonDown(MouseButton.Left)) if (Input.IsMouseButtonDown(MouseButton.Left))
{ {
_particleSystem.SetEmitterPosition(_emitterId, Input.GetMousePosition()); var mousePos = Input.GetMousePosition();
_container.Size = new Rect(mousePos.X, mousePos.Y);
} }
} }
@@ -118,5 +125,5 @@ public class TestGame : Game
private ResourceRef<Sound> _sound; private ResourceRef<Sound> _sound;
private ResourceRef<Texture2d> _icon; private ResourceRef<Texture2d> _icon;
private GridContainer _container = new(); private GridContainer _container = new(minimumSize: new Rect(64.0f, 64.0f), new());
} }

View File

@@ -137,6 +137,9 @@ namespace Voile.Input
AddInputMapping("accept", [ AddInputMapping("accept", [
new KeyInputAction(KeyboardKey.Enter), new KeyInputAction(KeyboardKey.Enter),
]); ]);
AddInputMapping("cancel", [
new KeyInputAction(KeyboardKey.Backspace),
]);
} }
protected bool TryGetInputMappings(string forAction, [NotNullWhen(true)] out List<IInputAction>? inputActions) protected bool TryGetInputMappings(string forAction, [NotNullWhen(true)] out List<IInputAction>? inputActions)

View File

@@ -12,7 +12,7 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
public Vector2 Position { get; set; } public Vector2 Position { get; set; }
public Rect MinimumRect { get; set; } = Rect.Zero; public Rect MinimumRect { get; set; } = Rect.Zero;
public Rect Size { get; set; } = Rect.Zero; public Rect Size { get; set; } = Rect.Zero;
public bool Visible { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public bool Visible { get; set; } = true;
public Container() public Container()
{ {
@@ -67,13 +67,15 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
maxY = MathF.Max(maxY, pos.Y + size.Height); maxY = MathF.Max(maxY, pos.Y + size.Height);
} }
// Optionally apply padding float padding = 0f;
float padding = 0f; // or make this configurable
Size = new Rect( float occupiedWidth = (maxX - minX) + padding * 2;
maxX - minX + padding * 2, float occupiedHeight = (maxY - minY) + padding * 2;
maxY - minY + padding * 2
); float finalWidth = MathF.Max(occupiedWidth, MinimumRect.Width);
float finalHeight = MathF.Max(occupiedHeight, MinimumRect.Height);
Size = new Rect(finalWidth, finalHeight);
} }
public void AddChild(IElement child) public void AddChild(IElement child)
@@ -82,6 +84,12 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
Update(); Update();
} }
public void RemoveChild(IElement child)
{
_children.Remove(child);
Update();
}
public void Render(RenderSystem renderer, Style style) public void Render(RenderSystem renderer, Style style)
{ {
foreach (var child in Children) foreach (var child in Children)

View File

@@ -14,6 +14,7 @@ public interface IParentableElement
{ {
public IReadOnlyList<IElement> Children { get; } public IReadOnlyList<IElement> Children { get; }
public void AddChild(IElement child); public void AddChild(IElement child);
public void RemoveChild(IElement child);
} }
public interface IResizeableElement public interface IResizeableElement