CrabUI
Loading...
Searching...
No Matches
CUIDebugLayoutEvent.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 CUIDebugEvent
20 {
21 public CUIComponent Host;
22 public CUIComponent Target;
23 public string Method;
24 public string SProp;
25 public string TProp;
26 public string Value;
27 public CUIDebugEvent(CUIComponent host, CUIComponent target, string method, string sprop, string tprop, string value)
28 {
29 Host = host;
30 Target = target;
31 Method = method ?? "";
32 SProp = sprop ?? "";
33 TProp = tprop ?? "";
34 Value = value ?? "";
35 }
36 }
37
38
39 public class CUIDebugEventComponent : CUIComponent
40 {
41 public static Dictionary<int, Color> CapturedIDs = new Dictionary<int, Color>();
42
43
44
45 private CUIDebugEvent _value; public CUIDebugEvent Value
46 {
47 get => _value;
48 set
49 {
50 _value = value;
51
52 Revealed = value != null;
53 if (value != null)
54 {
55 LastUpdate = Timing.TotalTime;
56 AssignColor();
57 }
58 MakeText();
59 }
60 }
61
62 public void Flush() => Value = null;
63
64 private void MakeText()
65 {
66 if (Value == null)
67 {
68 Line1 = "";
69 Line2 = "";
70 }
71 else
72 {
73 Line1 = $" {Value.Target} in {Value.Host}.{Value.Method}";
74 Line2 = $" {Value.SProp} -> {Value.TProp} {Value.Value}";
75 }
76 }
77
78 public static Random random = new Random();
79
80 private static float NextColor;
81 private static float ColorShift = 0.05f;
82 private void AssignColor()
83 {
84 if (Value.Target == null) return;
85
86 if (CapturedIDs.ContainsKey(Value.Target.ID))
87 {
88 BackgroundColor = CapturedIDs[Value.Target.ID];
89 }
90 else
91 {
92 // float r = random.NextSingle();
93 // float scale = 20;
94 // r = (float)Math.Round(r * scale) / scale;
95
96 CapturedIDs[Value.Target.ID] = GetColor(NextColor);
97
98 NextColor += ColorShift;
99 if (NextColor > 1) NextColor = 0;
100
101 BackgroundColor = CapturedIDs[Value.Target.ID];
102 }
103 }
104
105
106 public string Line1 = "";
107 public string Line2 = "";
108
109 public float UpdateTimer;
110 public double LastUpdate;
111
112 public Color GetColor(float d)
113 {
114 return ToolBox.GradientLerp(d,
115 Color.Cyan * 0.5f,
116 Color.Red * 0.5f,
117 Color.Green * 0.5f,
118 Color.Blue * 0.5f,
119 Color.Magenta * 0.5f,
120 Color.Yellow * 0.5f,
121 Color.Cyan * 0.5f
122 );
123 }
124 public Color GetColor2(float d)
125 {
126 return ToolBox.GradientLerp(Math.Min(0.8f, d),
127 CapturedIDs[Value.Target.ID],
128 Color.Black * 0.5f
129 );
130 }
131
132
133 public override void Draw(SpriteBatch spriteBatch)
134 {
135 BackgroundColor = GetColor2((float)(Timing.TotalTime - LastUpdate));
136
137 base.Draw(spriteBatch);
138
139 GUIStyle.Font.Value.DrawString(spriteBatch, Line1, Real.Position, Color.White, rotation: 0, origin: Vector2.Zero, 0.9f, se: SpriteEffects.None, layerDepth: 0.1f);
140
141 GUIStyle.Font.Value.DrawString(spriteBatch, Line2, Real.Position + new Vector2(0, 20), Color.White, rotation: 0, origin: Vector2.Zero, 0.9f, se: SpriteEffects.None, layerDepth: 0.1f);
142 }
143
144
145 public CUIDebugEventComponent(CUIDebugEvent value = null) : base()
146 {
147 Value = value;
148 IgnoreDebug = true;
149 BackgroundColor = Color.Green;
150 Absolute = new CUINullRect(null, null, null, 40);
151
152
153 }
154 }
155}
bool Revealed
Visible + !IgnoreEvents.
Color BackgroundColor
Color of BackgroundSprite, default is black If you're using custom sprite and don't see it make sur...
CUIRect Real
Calculated prop, position on real screen in pixels Should be fully calculated after CUIMainComponent....
int ID
Global ID, unique for component.
CUINullRect Absolute
Absolute size and position in pixels.