diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index 0c256d0..7803ece 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -127,22 +127,22 @@ public class TestGame : Game private ResourceRef _sound; private ResourceRef _icon; - private FlexContainer _container = new(minimumSize: new Rect(64.0f, 64.0f), new()) - { - Anchor = Anchor.Center, - Size = new Rect(500, 300), - Direction = FlexDirection.Column, - Justify = JustifyContent.Start, - Align = AlignItems.Center, - Wrap = true, - Gap = 10f - }; + // private FlexContainer _container = new(minimumSize: new Rect(64.0f, 64.0f), new()) + // { + // Anchor = Anchor.Center, + // Size = new Rect(500, 300), + // Direction = FlexDirection.Column, + // Justify = JustifyContent.Start, + // Align = AlignItems.Center, + // Wrap = true, + // Gap = 10f + // }; private Frame _frame = new(); - // private VerticalContainer _container = new(new Rect(128.0f, 64.0f), new(), 16) - // { - // ConfineToContents = true, - // Anchor = Anchor.CenterLeft, - // AnchorOffset = new Vector2(0.5f, 0.0f) - // }; + private VerticalContainer _container = new(new Rect(128.0f, 64.0f), new(), 16) + { + ConfineToContents = true, + Anchor = Anchor.CenterLeft, + AnchorOffset = new Vector2(0.5f, 0.0f) + }; } \ No newline at end of file diff --git a/Voile/Source/Resources/Resource.cs b/Voile/Source/Resources/Resource.cs index 6b41c7b..7ef4a76 100644 --- a/Voile/Source/Resources/Resource.cs +++ b/Voile/Source/Resources/Resource.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using Voile.Resources; namespace Voile @@ -32,7 +33,7 @@ namespace Voile /// /// An instance of a retrieved . /// true if the resource was successfully retrieved, otherwise false. - public bool TryGetValue(out T? value) + public bool TryGetValue([NotNullWhen(true)] out T? value) { value = ResourceManager.GetResource(Guid); return value != null; diff --git a/Voile/Source/UI/Containers/Container.cs b/Voile/Source/UI/Containers/Container.cs index bb6c327..9c1c0f9 100644 --- a/Voile/Source/UI/Containers/Container.cs +++ b/Voile/Source/UI/Containers/Container.cs @@ -44,7 +44,6 @@ public abstract class Container : UIElement, IParentableElement { if (child is not IUpdatableElement updatable) continue; - updatable.MarkDirty(); updatable.Update(); if (child is IAnchorableElement anchorable) diff --git a/Voile/Source/UI/Containers/Frame.cs b/Voile/Source/UI/Containers/Frame.cs index fcc4985..4cbcd09 100644 --- a/Voile/Source/UI/Containers/Frame.cs +++ b/Voile/Source/UI/Containers/Frame.cs @@ -50,7 +50,12 @@ public class Frame : Container protected override void OnUpdate() { base.OnUpdate(); + + if (Size == _lastParentSize) return; + Size = _lastParentSize; + + Console.WriteLine("Updating"); } private Rect _lastParentSize = Rect.Zero; diff --git a/Voile/Source/UI/UISystem.cs b/Voile/Source/UI/UISystem.cs index 47081e2..875a3fc 100644 --- a/Voile/Source/UI/UISystem.cs +++ b/Voile/Source/UI/UISystem.cs @@ -9,6 +9,8 @@ public class UISystem : IUpdatableSystem, IRenderableSystem public IReadOnlyList Elements => _elements; 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) { @@ -50,21 +52,43 @@ public class UISystem : IUpdatableSystem, IRenderableSystem { if (element is IRenderableElement renderable) { - renderable.Render(renderer, _style.Value); - - if (!RenderDebugRects) return; - renderable.DrawSize(renderer); - } - - if (element is IParentableElement parentable) - { - foreach (var child in parentable.Children) + // TODO: normally you'd load a default style if the one supplied is empty, + // but for now this will do. + if (!_style.TryGetValue(out var value)) { - if (child is not IRenderableElement renderableChild) continue; - - if (!RenderDebugRects) return; - renderableChild.DrawSize(renderer); + value = new Style(string.Empty); } + + renderable.Render(renderer, value); + } + } + + if (!RenderDebugRects) return; + + foreach (var element in _elements) + { + if (element is not UIElement uiElement) continue; + DrawDebugForElement(renderer, uiElement); + } + } + + 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); } } }