Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
WidgetsList.cs
浏览该文件的文档.
1using System.Reflection;
2
3namespace Game {
4 public class WidgetsList : IEnumerable<Widget> {
5 public struct Enumerator : IEnumerator<Widget> {
7
9
10 public int m_index;
11
12 public int m_version;
13
15
16 object IEnumerator.Current => m_current;
17
18 public Enumerator(WidgetsList collection) {
19 m_collection = collection;
20 m_current = null;
21 m_index = 0;
22 m_version = collection.m_version;
23 }
24
25 public void Dispose() { }
26
27 public bool MoveNext() {
28 if (m_index < m_collection.m_widgets.Count) {
29 m_current = m_collection.m_widgets[m_index];
30 m_index++;
31 return true;
32 }
33 m_current = null;
34 return false;
35 }
36
37 public void Reset() {
38 m_index = 0;
39 m_current = null;
40 }
41 }
42
44
45 public List<Widget> m_widgets = [];
46
47 public int m_version;
48
49 public int Count => m_widgets.Count;
50
51 public Widget this[int index] => m_widgets[index];
52
53 public WidgetsList(ContainerWidget containerWidget) => m_containerWidget = containerWidget;
54
55 public void Add(Widget widget) {
56 Insert(Count, widget);
57 }
58
59 public void Add(params Widget[] widgets) {
60 AddRange(widgets);
61 }
62
63 public void AddRange(IEnumerable<Widget> widgets) {
64 foreach (Widget widget in widgets) {
65 Add(widget);
66 }
67 }
68
69 public void Insert(int index, Widget widget) {
70 if (m_widgets.Contains(widget)) {
71 return;
72 }
73 if (index < 0
74 || index > m_widgets.Count) {
75 throw new InvalidOperationException("Widget index out of range.");
76 }
78 m_widgets.Insert(index, widget);
79 m_containerWidget.WidgetAdded(widget);
80 m_version++;
81 }
82
83 public void InsertBefore(Widget beforeWidget, Widget widget) {
84 int num = m_widgets.IndexOf(beforeWidget);
85 if (num < 0) {
86 throw new InvalidOperationException("Widget not present in container.");
87 }
88 Insert(num, widget);
89 }
90
91 public void InsertAfter(Widget afterWidget, Widget widget) {
92 int num = m_widgets.IndexOf(afterWidget);
93 if (num < 0) {
94 throw new InvalidOperationException("Widget not present in container.");
95 }
96 Insert(num + 1, widget);
97 }
98
99 public void Remove(Widget widget) {
100 int num = IndexOf(widget);
101 if (num >= 0) {
102 RemoveAt(num);
103 }
104 }
105
106 public void RemoveAt(int index) {
107 if (index < 0
108 || index >= m_widgets.Count) {
109 throw new InvalidOperationException("Widget index out of range.");
110 }
111 Widget widget = m_widgets[index];
112 widget.ChangeParent(null);
113 m_widgets.RemoveAt(index);
114 m_containerWidget.WidgetRemoved(widget);
115 m_version--;
116 }
117
118 public void Clear() {
119 while (Count > 0) {
120 RemoveAt(Count - 1);
121 }
122 }
123
124 public int IndexOf(Widget widget) => m_widgets.IndexOf(widget);
125
126 public bool Contains(Widget widget) => m_widgets.Contains(widget);
127
128 public Widget Find(string name, Type type, bool throwIfNotFound = true) {
129 foreach (Widget widget2 in m_widgets) {
130 if ((name == null || (widget2.Name != null && widget2.Name == name))
131 && (type == null || type == widget2.GetType() || widget2.GetType().GetTypeInfo().IsSubclassOf(type))) {
132 return widget2;
133 }
134 if (widget2 is ContainerWidget containerWidget) {
135 Widget widget = containerWidget.Children.Find(name, type, false);
136 if (widget != null) {
137 return widget;
138 }
139 }
140 }
141 if (throwIfNotFound) {
142 throw new Exception($"Required widget \"{name}\" of type \"{type}\" not found.");
143 }
144 return null;
145 }
146
147 public Widget Find(string name, bool throwIfNotFound = true) => Find(name, null, throwIfNotFound);
148
149 public T Find<T>(string name, bool throwIfNotFound = true) where T : class => Find(name, typeof(T), throwIfNotFound) as T;
150
151 public T Find<T>(bool throwIfNotFound = true) where T : class => Find(null, typeof(T), throwIfNotFound) as T;
152
153 public Enumerator GetEnumerator() => new(this);
154
155 IEnumerator<Widget> IEnumerable<Widget>.GetEnumerator() => new Enumerator(this);
156
157 IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
158 }
159}
virtual string Name
virtual void ChangeParent(ContainerWidget parentWidget)
void InsertAfter(Widget afterWidget, Widget widget)
Widget Find(string name, Type type, bool throwIfNotFound=true)
List< Widget > m_widgets
bool Contains(Widget widget)
void InsertBefore(Widget beforeWidget, Widget widget)
void Add(Widget widget)
void Insert(int index, Widget widget)
T Find< T >(string name, bool throwIfNotFound=true)
void Add(params Widget[] widgets)
void Remove(Widget widget)
int IndexOf(Widget widget)
Enumerator GetEnumerator()
void AddRange(IEnumerable< Widget > widgets)
Widget Find(string name, bool throwIfNotFound=true)
WidgetsList(ContainerWidget containerWidget)
ContainerWidget m_containerWidget
void RemoveAt(int index)
Enumerator(WidgetsList collection)