From acea7b2e728787a0d83bbf83c8c1f042d2c32e7e Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Mon, 3 Jun 2024 10:15:45 +0800 Subject: + plugins project --- .../MonoGame.Extended.Gui/ElementCollection.cs | 98 ++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ElementCollection.cs (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ElementCollection.cs') diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ElementCollection.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ElementCollection.cs new file mode 100644 index 0000000..009161d --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ElementCollection.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using MonoGame.Extended.Gui.Controls; + +namespace MonoGame.Extended.Gui +{ + public abstract class ElementCollection : IList + where TParent : class, IRectangular + where TChild : Element + { + private readonly TParent _parent; + private readonly List _list = new List(); + + public Action ItemAdded { get; set; } + public Action ItemRemoved { get; set; } + + protected ElementCollection(TParent parent) + { + _parent = parent; + } + + public IEnumerator GetEnumerator() + { + return _list.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return ((IEnumerable)_list).GetEnumerator(); + } + + public void Add(TChild item) + { + item.Parent = _parent; + _list.Add(item); + ItemAdded?.Invoke(item); + } + + public void Clear() + { + foreach (var child in _list) + { + child.Parent = null; + ItemRemoved?.Invoke(child); + } + + _list.Clear(); + } + + public bool Contains(TChild item) + { + return _list.Contains(item); + } + + public void CopyTo(TChild[] array, int arrayIndex) + { + _list.CopyTo(array, arrayIndex); + } + + public bool Remove(TChild item) + { + item.Parent = null; + ItemRemoved?.Invoke(item); + return _list.Remove(item); + } + + public int Count => _list.Count; + + public bool IsReadOnly => ((ICollection)_list).IsReadOnly; + + public int IndexOf(TChild item) + { + return _list.IndexOf(item); + } + + public void Insert(int index, TChild item) + { + item.Parent = _parent; + _list.Insert(index, item); + ItemAdded?.Invoke(item); + } + + public void RemoveAt(int index) + { + var child = _list[index]; + child.Parent = null; + _list.RemoveAt(index); + ItemRemoved?.Invoke(child); + } + + public TChild this[int index] + { + get { return _list[index]; } + set { _list[index] = value; } + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0