blob: 009161db06c659f531ccc398fa22a7db91309b01 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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; }
}
}
}
|