CrabUI
Loading...
Searching...
No Matches
CUIDebugWindow.cs
1#define CUIDEBUG
2
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Reflection;
7using System.Diagnostics;
8using System.Runtime.CompilerServices;
9using System.IO;
10
11using Barotrauma;
12using Microsoft.Xna.Framework;
13using Microsoft.Xna.Framework.Input;
14using Microsoft.Xna.Framework.Graphics;
15
16
17namespace CrabUI
18{
19 public class CUIDebugWindow : CUIFrame
20 {
21 public static CUIDebugWindow Main;
22
23 public CUIVerticalList EventsComponent;
24 public CUIVerticalList DebugIDsComponent;
25 public CUIPages Pages;
26 public CUIMultiButton PickIDButton;
27
28 public List<CUIDebugEventComponent> Events = new List<CUIDebugEventComponent>();
29 public int target;
30 public bool Loop { get; set; } = true;
31
32
33
34 public void Capture(CUIDebugEvent e)
35 {
36 if (EventsComponent == null) return;
37
38 if (target > 200) return;
39
40 if (Events.Count < target + 1)
41 {
42 CUIDebugEventComponent ec = new CUIDebugEventComponent(e);
43 Events.Add(ec);
44 EventsComponent.Append(ec);
45
46 ec.OnMouseEnter += (m) => ec.Value.Target.DebugHighlight = true;
47 ec.OnMouseLeave += (m) => ec.Value.Target.DebugHighlight = false;
48 }
49 else
50 {
51 Events[target].Value = e;
52 }
53
54 target++;
55 }
56
57 public void Flush()
58 {
59 if (Loop) target = 0;
60 //Events.ForEach(e => e.Flush());
61 }
62
63 public void MakeIDList()
64 {
65 DebugIDsComponent.Visible = false;
66 DebugIDsComponent.RemoveAllChildren();
67
68 List<CUIComponent> l = new List<CUIComponent>();
69
70 if (CUI.Main is not null)
71 {
72 RunRecursiveOn(CUI.Main, (component) =>
73 {
74 if (!component.IgnoreDebug) l.Add(component);
75 });
76 }
77
78 foreach (CUIComponent c in l)
79 {
80 CUIToggleButton b = new CUIToggleButton(c.ToString())
81 {
82 State = c.Debug,
83 IgnoreDebug = true,
84 Style = new CUIStyle(){
85 {"TextAlign", "[0,0]"}
86 },
87 AddOnMouseDown = (m) =>
88 {
89 c.Debug = !c.Debug;
90 MakeIDList();
91 },
92 AddOnMouseEnter = (m) => c.DebugHighlight = true,
93 AddOnMouseLeave = (m) => c.DebugHighlight = false,
94 };
95
96 DebugIDsComponent.Append(b);
97 }
98 DebugIDsComponent.Visible = true;
99 l.Clear();
100 }
101
102 public CUIDebugWindow() : base()
103 {
104 this.ZIndex = 1000;
105 this.Layout = new CUILayoutVerticalList();
106
107 this["handle"] = new CUIComponent()
108 {
109 Absolute = new CUINullRect(null, null, null, 20),
110 };
111
112 this["handle"]["closebutton"] = new CUIButton("X")
113 {
114 Anchor = new Vector2(1, 0.5f),
115 Style = new CUIStyle(){
116 {"MasterColor", "Red"}
117 },
118 AddOnMouseDown = (e) =>
119 {
120 CUIDebugWindow.Close();
121 },
122 };
123
124 this["controls"] = new CUIComponent()
125 {
126 FitContent = new CUIBool2(false, true),
127 };
128
129 this["controls"]["loop"] = new CUIToggleButton("loop")
130 {
131 Relative = new CUINullRect(0, 0, 0.5f, null),
132 AddOnStateChange = (state) =>
133 {
134 Loop = state;
135 Events?.Clear();
136 EventsComponent?.RemoveAllChildren();
137 },
138 State = Loop,
139 };
140
141
142 this["controls"].Append(PickIDButton = new CUIMultiButton()
143 {
144 Relative = new CUINullRect(0.5f, 0, 0.5f, null),
145 Style = new CUIStyle(){
146 {"InactiveColor", "0,0,0,128"},
147 {"MousePressedColor", "0,255,255,64"}
148 },
149 ConsumeDragAndDrop = false,
150
151 Options = new string[]{
152 "Debug events", "Debugged components"
153 }
154 });
155
156 Append(Pages = new CUIPages()
157 {
158 FillEmptySpace = new CUIBool2(false, true),
159 Style = new CUIStyle(){
160 {"BackgroundColor", "0,0,32,128"}
161 },
162 IgnoreDebug = true,
163 });
164
165 EventsComponent = new CUIVerticalList()
166 {
167 Relative = new CUINullRect(0, 0, 1, 1),
168 Scrollable = true,
169 IgnoreDebug = true,
170 };
171
172 DebugIDsComponent = new CUIVerticalList()
173 {
174 Relative = new CUINullRect(0, 0, 1, 1),
175 Scrollable = true,
176 IgnoreDebug = true,
177 };
178
179 PickIDButton.OnSelect += (s) =>
180 {
181 if (PickIDButton.SelectedIndex == 0)
182 {
183 MakeIDList();
184 Pages.Open(EventsComponent);
185 }
186 else Pages.Open(DebugIDsComponent);
187 };
188 PickIDButton.Select(0);
189
190 this["controls"].Get<CUIToggleButton>("loop").State = true;
191
192 IgnoreDebug = true;
193 }
194
195 public static CUIDebugWindow Open()
196 {
197 if (CUI.Main == null) return null;
198
199 CUIDebugWindow w = new CUIDebugWindow()
200 {
201 Absolute = new CUINullRect(10, 370, 500, 370),
202 };
203 CUI.Main.Append(w);
204 CUIDebugWindow.Main = w;
205 CUI.Main.OnTreeChanged += () => w.MakeIDList();
206 return w;
207 }
208
209 public static void Close()
210 {
211 if (CUIDebugWindow.Main == null) return;
212
213 CUIDebugWindow.Main.RemoveSelf();
214 CUIDebugWindow.Main.Revealed = false;
215 CUIDebugWindow.Main = null;
216 }
217
218 public CUIDebugWindow(float? x = null, float? y = null, float? w = null, float? h = null) : this()
219 {
220 Relative = new CUINullRect(x, y, w, h);
221 }
222 }
223}
CUIBool2 FillEmptySpace
Will be resized to fill empty space in list components.
CUINullRect Relative
Relative to parent size and position, [0..1].
virtual CUIComponent Append(CUIComponent child, string name=null, [CallerMemberName] string memberName="")
Adds children to the end of the list.
CUIStyle Style
Allows you to assing parsable string or link to CUIPalette to any prop It's indexable,...
Vector2 Anchor
this point of this component
CUIBool2 FitContent
Will resize itself to fit components with absolute size, e.g. text.
CUINullRect Absolute
Absolute size and position in pixels.
static void RunRecursiveOn(CUIComponent component, Action< CUIComponent > action)
designed to be versatile, in fact never used