diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 2559684..55dcb71 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; @@ -19,18 +21,19 @@ namespace MLEM.Ui.Elements { /// A list of all of this element's direct children. /// Use or to manipulate this list while calling all of the necessary callbacks. /// - protected readonly List Children = new List(); - private readonly List sortedChildren = new List(); + protected readonly IList Children; + private readonly List children = new List(); /// /// A sorted version of . The children are sorted by their . /// - protected List SortedChildren { + protected IList SortedChildren { get { this.UpdateSortedChildrenIfDirty(); return this.sortedChildren; } } private bool sortedChildrenDirty; + private IList sortedChildren; private UiSystem system; /// @@ -398,6 +401,8 @@ namespace MLEM.Ui.Elements { this.anchor = anchor; this.size = size; + this.Children = new ReadOnlyCollection(this.children); + this.OnMouseEnter += element => this.IsMouseOver = true; this.OnMouseExit += element => this.IsMouseOver = false; this.OnTouchEnter += element => this.IsMouseOver = true; @@ -408,6 +413,7 @@ namespace MLEM.Ui.Elements { this.GetGamepadNextElement += (dir, next) => next; this.SetAreaDirty(); + this.SetSortedChildrenDirty(); } /// @@ -418,9 +424,9 @@ namespace MLEM.Ui.Elements { /// The type of child to add /// This element, for chaining public T AddChild(T element, int index = -1) where T : Element { - if (index < 0 || index > this.Children.Count) - index = this.Children.Count; - this.Children.Insert(index, element); + if (index < 0 || index > this.children.Count) + index = this.children.Count; + this.children.Insert(index, element); element.Parent = this; element.AndChildren(e => { e.Root = this.Root; @@ -438,7 +444,7 @@ namespace MLEM.Ui.Elements { /// /// The child element to remove public void RemoveChild(Element element) { - this.Children.Remove(element); + this.children.Remove(element); // set area dirty here so that a dirty call is made // upwards to us if the element is auto-positioned element.SetAreaDirty(); @@ -485,10 +491,7 @@ namespace MLEM.Ui.Elements { /// public virtual void ForceUpdateSortedChildren() { this.sortedChildrenDirty = false; - - this.sortedChildren.Clear(); - this.sortedChildren.AddRange(this.Children); - this.sortedChildren.Sort((e1, e2) => e1.Priority.CompareTo(e2.Priority)); + this.sortedChildren = new ReadOnlyCollection(this.Children.OrderBy(e => e.Priority).ToArray()); } /// @@ -803,7 +806,7 @@ namespace MLEM.Ui.Elements { /// A only returns elements that are currently in view here. /// /// This element's relevant children - protected virtual List GetRelevantChildren() { + protected virtual IList GetRelevantChildren() { return this.SortedChildren; } @@ -901,9 +904,9 @@ namespace MLEM.Ui.Elements { return null; if (this.Transform != Matrix.Identity) position = Vector2.Transform(position, Matrix.Invert(this.Transform)); - var children = this.GetRelevantChildren(); - for (var i = children.Count - 1; i >= 0; i--) { - var element = children[i].GetElementUnderPos(position); + var relevant = this.GetRelevantChildren(); + for (var i = relevant.Count - 1; i >= 0; i--) { + var element = relevant[i].GetElementUnderPos(position); if (element != null) return element; } @@ -925,7 +928,7 @@ namespace MLEM.Ui.Elements { /// /// The comparison to sort by public void ReorderChildren(Comparison comparison) { - this.Children.Sort(comparison); + this.children.Sort(comparison); } /// @@ -934,7 +937,7 @@ namespace MLEM.Ui.Elements { /// The index to start reversing at /// The amount of elements to reverse public void ReverseChildren(int index = 0, int? count = null) { - this.Children.Reverse(index, count ?? this.Children.Count); + this.children.Reverse(index, count ?? this.Children.Count); } /// diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 9e60468..cd93684 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -116,7 +116,7 @@ namespace MLEM.Ui.Elements { } /// - protected override List GetRelevantChildren() { + protected override IList GetRelevantChildren() { var relevant = base.GetRelevantChildren(); if (this.scrollOverflow) { if (this.relevantChildrenDirty) { diff --git a/Tests/DataTextureAtlasTests.cs b/Tests/DataTextureAtlasTests.cs index 810ebe6..95db88b 100644 --- a/Tests/DataTextureAtlasTests.cs +++ b/Tests/DataTextureAtlasTests.cs @@ -1,8 +1,5 @@ -using System.Collections.Generic; -using System.IO; using System.Linq; using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; using MLEM.Data; using MLEM.Data.Content; using NUnit.Framework; diff --git a/Tests/DirectionTests.cs b/Tests/DirectionTests.cs index 01e14bc..2331fa1 100644 --- a/Tests/DirectionTests.cs +++ b/Tests/DirectionTests.cs @@ -1,4 +1,3 @@ -using System.Drawing; using Microsoft.Xna.Framework; using MLEM.Misc; using NUnit.Framework;