CrabUI
Loading...
Searching...
No Matches
CUIComponent.Tree.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Diagnostics;
6using System.Runtime.CompilerServices;
7using System.IO;
8
9using Barotrauma;
10using Microsoft.Xna.Framework;
11using Microsoft.Xna.Framework.Input;
12using Microsoft.Xna.Framework.Graphics;
13
14using System.Xml;
15using System.Xml.Linq;
16
17namespace CrabUI
18{
19 public partial class CUIComponent
20 {
21 #region Tree --------------------------------------------------------
22
23 public List<CUIComponent> Children { get; set; } = new();
24
25 private CUIComponent? parent; public CUIComponent? Parent
26 {
27 get => parent;
28 set => SetParent(value);
29 }
30
31
32
33 internal void SetParent(CUIComponent? value, [CallerMemberName] string memberName = "")
34 {
35 if (parent != null)
36 {
37 TreeChanged = true;
38 OnPropChanged();
39 parent.Forget(this);
40 parent.Children.Remove(this);
41 parent.OnChildRemoved?.Invoke(this);
42 }
43
44 parent = value;
45
46 CUIDebug.Capture(null, this, "SetParent", memberName, "parent", $"{parent}");
47
48 if (parent != null)
49 {
50 if (parent is CUIMainComponent main) MainComponent = main;
51 if (parent?.MainComponent != null) MainComponent = parent.MainComponent;
52
53 //parent.Children.Add(this);
54 TreeChanged = true;
55 if (AKA != null) parent.Remember(this, AKA);
56 parent.PassPropsToChild(this);
57 OnPropChanged();
58 parent.OnChildAdded?.Invoke(this);
59 }
60 }
61
62
63 private bool treeChanged = true; internal bool TreeChanged
64 {
65 get => treeChanged;
66 set
67 {
68 treeChanged = value;
69 if (value)
70 {
71 OnTreeChanged?.Invoke();
72 if (Parent != null) Parent.TreeChanged = true;
73 }
74 }
75 }
76
77 /// <summary>
78 /// Allows you to add array of children
79 /// </summary>
80 public IEnumerable<CUIComponent> AddChildren { set { foreach (CUIComponent c in value) { Append(c); } } }
81
82 public event Action<CUIComponent> OnChildAdded;
83 public event Action<CUIComponent> OnChildRemoved;
84
85
86
87 /// <summary>
88 /// Adds children to the end of the list
89 /// </summary>
90 /// <param name="child"></param>
91 /// <param name="name"> AKA </param>
92 /// <returns> child </returns>
93 public virtual CUIComponent Append(CUIComponent child, string name = null, [CallerMemberName] string memberName = "")
94 {
95 if (child == null) return child;
96
97 child.Parent = this;
98 Children.Add(child);
99 if (name != null) Remember(child, name);
100
101 return child;
102 }
103
104 /// <summary>
105 /// Adds children to the begining of the list
106 /// </summary>
107 /// <param name="child"></param>
108 /// <param name="name"> AKA </param>
109 /// <returns> child </returns>
110 public virtual CUIComponent Prepend(CUIComponent child, string name = null, [CallerMemberName] string memberName = "")
111 {
112 if (child == null) return child;
113
114 child.Parent = this;
115 Children.Insert(0, child);
116 if (name != null) Remember(child, name);
117
118 return child;
119 }
120
121 public virtual CUIComponent Insert(CUIComponent child, int index, string name = null, [CallerMemberName] string memberName = "")
122 {
123 if (child == null) return child;
124
125 child.Parent = this;
126 index = Math.Clamp(index, 0, Children.Count);
127 Children.Insert(index, child);
128 if (name != null) Remember(child, name);
129
130 return child;
131 }
132
133 //TODO DRY
134 public void RemoveSelf() => Parent?.RemoveChild(this);
135 public CUIComponent RemoveChild(CUIComponent child, [CallerMemberName] string memberName = "")
136 {
137 if (child == null || !Children.Contains(child)) return child;
138
139 if (this != null) // kek
140 {
141 child.TreeChanged = true;
142 child.OnPropChanged();
143 //HACK i'm sure it doesn't belong here, find a better place
144 forsedSize = new CUINullVector2();
145 OnAbsolutePropChanged();
146 // Forget(child);
147 Children.Remove(child);
148 OnChildRemoved?.Invoke(child);
149 }
150
151 child.parent = null;
152
153 CUIDebug.Capture(null, this, "RemoveChild", memberName, "child", $"{child}");
154
155 return child;
156 }
157
158
159 //TODO DRY
160 public void RemoveAllChildren([CallerMemberName] string memberName = "")
161 {
162 foreach (CUIComponent c in Children)
163 {
164 if (this != null) // kek
165 {
166 c.TreeChanged = true;
167 c.OnPropChanged();
168 //Forget(c);
169 //Children.Remove(c);
170 OnChildRemoved?.Invoke(c);
171 }
172
173 c.parent = null;
174
175 CUIDebug.Capture(null, this, "RemoveAllChildren", memberName, "child", $"{c}");
176 }
177
178 NamedComponents.Clear();
179 Children.Clear();
180 }
181
182
183 /// <summary>
184 /// Pass props like ZIndex, Visible to a new child
185 /// </summary>
186 /// <param name="child"></param>
187 protected virtual void PassPropsToChild(CUIComponent child)
188 {
189 if (!ShouldPassPropsToChildren) return;
190
191 //child.Palette = Palette;
192 if (ZIndex.HasValue && !child.IgnoreParentZIndex) child.ZIndex = ZIndex.Value + 1;
193 if (IgnoreEvents && !child.IgnoreParentEventIgnorance) child.IgnoreEvents = true;
194 if (!Visible && !child.IgnoreParentVisibility) child.Visible = false;
195 }
196
197 /// <summary>
198 /// Moves component to front in Parent.Children
199 /// which makes it render after other childs
200 /// </summary>
201 public void MoveToFront()
202 {
203 if (Parent == null) return;
204
205 Parent.Children.Remove(this);
206 Parent.Children.Add(this);
207 TreeChanged = true;
208 }
209
210 #endregion
211 }
212}
Base class for all components.
void MoveToFront()
Moves component to front in Parent.Children which makes it render after other childs.
virtual CUIComponent Prepend(CUIComponent child, string name=null, [CallerMemberName] string memberName="")
Adds children to the begining of the list.
Dictionary< string, CUIComponent > NamedComponents
All memorized components.
virtual void PassPropsToChild(CUIComponent child)
Pass props like ZIndex, Visible to a new child.
CUIMainComponent MainComponent
Link to CUIMainComponent, passed to children.
virtual CUIComponent Append(CUIComponent child, string name=null, [CallerMemberName] string memberName="")
Adds children to the end of the list.
IEnumerable< CUIComponent > AddChildren
Allows you to add array of children.
string AKA
Parent can memorize it's children by their names, AKA.
int? ZIndex
Components are drawn in order of their ZIndex Normally it's derived from component position in the ...
bool Visible
Invisible components are not drawn, but still can be interacted with.
bool IgnoreEvents
Won't react to mouse events.
bool ShouldPassPropsToChildren
Some props (like visible) are autopassed to all new childs see PassPropsToChild.