From 84e44c8ccb73e380b6f848286572887b8bd256b0 Mon Sep 17 00:00:00 2001 From: Sean Dempsey Date: Mon, 23 May 2022 16:19:30 -0700 Subject: [PATCH] Add AddRange for NotifyingCollection, expose all elements under a LayoutRoot --- MagicUI/Behaviours/LayoutOrchestrator.cs | 2 ++ MagicUI/Core/LayoutRoot.cs | 5 +++++ MagicUI/Core/NotifyingCollection.cs | 13 +++++++++++++ MagicUI/Elements/GridLayout.cs | 4 ++-- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/MagicUI/Behaviours/LayoutOrchestrator.cs b/MagicUI/Behaviours/LayoutOrchestrator.cs index e067763..56131fd 100644 --- a/MagicUI/Behaviours/LayoutOrchestrator.cs +++ b/MagicUI/Behaviours/LayoutOrchestrator.cs @@ -16,6 +16,8 @@ internal class LayoutOrchestrator : MonoBehaviour public bool shouldRenderDebugBounds = false; + public IEnumerable Elements => elements.AsReadOnly(); + /// /// Registers an element in layout for arrangement and later lookup /// diff --git a/MagicUI/Core/LayoutRoot.cs b/MagicUI/Core/LayoutRoot.cs index ee428bb..971a67c 100644 --- a/MagicUI/Core/LayoutRoot.cs +++ b/MagicUI/Core/LayoutRoot.cs @@ -30,6 +30,11 @@ public class LayoutRoot /// public GameObject Canvas { get => rootCanvas; } + /// + /// A read-only collection of elements registered to this layout + /// + public IEnumerable Elements => layoutOrchestrator.Elements; + /// /// A predicate that determines whether the layout should be visible. By default (i.e. when there is no condition), the layout is /// always visible. diff --git a/MagicUI/Core/NotifyingCollection.cs b/MagicUI/Core/NotifyingCollection.cs index b178c10..c40a66d 100644 --- a/MagicUI/Core/NotifyingCollection.cs +++ b/MagicUI/Core/NotifyingCollection.cs @@ -48,6 +48,19 @@ public void Add(T item) actionOnChange.Notify(owner); } + /// + /// Convenience method to add several items at once + /// + /// The items to add to the collection + public void AddRange(IEnumerable items) + { + foreach (T item in items) + { + this.items.Add(item); + } + actionOnChange.Notify(owner); + } + /// public void Clear() { diff --git a/MagicUI/Elements/GridLayout.cs b/MagicUI/Elements/GridLayout.cs index 6861c90..8a48825 100644 --- a/MagicUI/Elements/GridLayout.cs +++ b/MagicUI/Elements/GridLayout.cs @@ -95,14 +95,14 @@ public class GridLayout : Layout /// Definition of the number of rows, their sizes, and their types. By default this is empty. If the grid is measured /// with empty row definitions, one will be added for you with a minimum height of 0px. /// - public ICollection RowDefinitions => rowDefs; + public NotifyingCollection RowDefinitions => rowDefs; private NotifyingCollection colDefs; /// /// Definition of the number of columns, their sizes, and their types. By default this is empty. If the grid is measured /// with empty column definitions, one will be added for you with a minimum height of 0px. /// - public ICollection ColumnDefinitions => colDefs; + public NotifyingCollection ColumnDefinitions => colDefs; private float minWidth = 0; ///