summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ElementCollection.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-06-03 10:15:45 +0800
committerchai <215380520@qq.com>2024-06-03 10:15:45 +0800
commitacea7b2e728787a0d83bbf83c8c1f042d2c32e7e (patch)
tree0bfec05c1ca2d71be2c337bcd110a0421f19318b /Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ElementCollection.cs
parent88febcb02bf127d961c6471d9e846c0e1315f5c3 (diff)
+ plugins project
Diffstat (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ElementCollection.cs')
-rw-r--r--Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/ElementCollection.cs98
1 files changed, 98 insertions, 0 deletions
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<TChild, TParent> : IList<TChild>
+ where TParent : class, IRectangular
+ where TChild : Element<TParent>
+ {
+ private readonly TParent _parent;
+ private readonly List<TChild> _list = new List<TChild>();
+
+ public Action<TChild> ItemAdded { get; set; }
+ public Action<TChild> ItemRemoved { get; set; }
+
+ protected ElementCollection(TParent parent)
+ {
+ _parent = parent;
+ }
+
+ public IEnumerator<TChild> 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<Control>)_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