CrabUI
Loading...
Searching...
No Matches
CUIComponent.Debug.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 Debug --------------------------------------------------------
22 /// <summary>
23 /// Mark component and its children for debug
24 /// Used in debug interface
25 /// </summary>
26 private bool debug; public bool Debug
27 {
28 get => debug;
29 set
30 {
31 debug = value;
32 //foreach (CUIComponent c in Children) { c.Debug = value; }
33 }
34 }
35
36 /// <summary>
37 /// For debug frame itself
38 /// </summary>
39 public bool ignoreDebug; public bool IgnoreDebug
40 {
41 get => ignoreDebug || Unreal;
42 set
43 {
44 ignoreDebug = value;
45 foreach (CUIComponent c in Children) { c.IgnoreDebug = value; }
46 }
47 }
48
49 public void PrintTree(string offset = "")
50 {
51 CUI.Log($"{offset}{this}");
52 foreach (CUIComponent child in Children)
53 {
54 child.PrintTree(offset + "| ");
55 }
56 }
57
58 /// <summary>
59 /// Prints component and then message
60 /// </summary>
61 /// <param name="msg"></param>
62 /// <param name="source"></param>
63 /// <param name="lineNumber"></param>
64 public void Info(object msg, [CallerFilePath] string source = "", [CallerLineNumber] int lineNumber = 0)
65 {
66 var fi = new FileInfo(source);
67
68 CUI.Log($"{fi.Directory.Name}/{fi.Name}:{lineNumber}", Color.Yellow * 0.5f);
69 CUI.Log($"{this} {msg ?? "null"}", Color.Yellow);
70 }
71
72 #endregion
73 #region AKA --------------------------------------------------------
74
75 /// <summary>
76 /// Parent can memorize it's children by their names, AKA
77 /// </summary>
78 [CUISerializable] public string AKA { get; set; }
79 /// <summary>
80 /// All memorized components
81 /// </summary>
82 public Dictionary<string, CUIComponent> NamedComponents { get; set; } = new();
83
84 public CUIComponent Remember(CUIComponent c, string name)
85 {
86 NamedComponents[name] = c;
87 c.AKA = name;
88 return c;
89 }
90 /// <summary>
91 /// If it already has AKA
92 /// </summary>
94 {
95 if (c.AKA != null) NamedComponents[c.AKA] = c;
96 return c;
97 }
98
99 public CUIComponent Forget(string name)
100 {
101 if (name == null) return null;
102 CUIComponent c = NamedComponents.GetValueOrDefault(name);
103 NamedComponents.Remove(name);
104 return c;
105 }
106 /// <summary>
107 /// If it already has AKA
108 /// </summary>
110 {
111 if (c?.AKA != null) NamedComponents.Remove(c.AKA);
112 return c;
113 }
114
115 /// <summary>
116 /// You can access NamedComponents with this indexer
117 /// </summary>
118 /// <param name="name"></param>
119 /// <returns></returns>
120 public CUIComponent this[string name]
121 {
122 get => Get(name);
123 set
124 {
125 if (value.Parent != null) Remember(value, name);
126 else Append(value, name);
127 }
128 }
129
130 /// <summary>
131 /// Returns memorized component by name
132 /// </summary>
133 /// <param name="name"></param>
134 /// <returns></returns>
135 public virtual CUIComponent Get(string name)
136 {
137 if (name == null) return null;
138 if (NamedComponents.ContainsKey(name)) return NamedComponents[name];
139
140 CUIComponent component = this;
141 string[] names = name.Split('.');
142
143 foreach (string n in names)
144 {
145 component = component.NamedComponents.GetValueOrDefault(n);
146
147 if (component == null)
148 {
149 CUI.Warning($"Failed to Get {name} from {this}, there's no {n}");
150 break;
151 }
152 }
153
154 return component;
155 }
156 public T Get<T>(string name) where T : CUIComponent => (T)Get(name);
157
158 #endregion
159 }
160}
Base class for all components.
virtual CUIComponent Get(string name)
Returns memorized component by name.
bool ignoreDebug
For debug frame itself.
Dictionary< string, CUIComponent > NamedComponents
All memorized components.
virtual CUIComponent Append(CUIComponent child, string name=null, [CallerMemberName] string memberName="")
Adds children to the end of the list.
CUIComponent Forget(CUIComponent c)
If it already has AKA.
string AKA
Parent can memorize it's children by their names, AKA.
void Info(object msg, [CallerFilePath] string source="", [CallerLineNumber] int lineNumber=0)
Prints component and then message.
CUIComponent Remember(CUIComponent c)
If it already has AKA.
In fact a static class managing static things.
Definition CUIErrors.cs:16
static void Log(object msg, Color? color=null, [CallerFilePath] string source="", [CallerLineNumber] int lineNumber=0)
Prints a message to console.