CrabUI
Loading...
Searching...
No Matches
CUIPrefab.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Diagnostics;
6
7using Barotrauma;
8using Microsoft.Xna.Framework;
9using Microsoft.Xna.Framework.Input;
10using Microsoft.Xna.Framework.Graphics;
11using HarmonyLib;
12
13namespace CrabUI
14{
15 public static class CUIStylePrefab
16 {
17 public static CUIStyle FrameCaption => new CUIStyle()
18 {
19 {"BackgroundColor", "CUIPalette.Frame.Border"},
20 {"Border", "CUIPalette.Frame.Border"},
21 {"TextColor", "CUIPalette.Frame.Text"},
22 };
23
24 public static CUIStyle Header => new CUIStyle()
25 {
26 {"BackgroundColor", "CUIPalette.Header.Background"},
27 {"Border", "CUIPalette.Header.Border"},
28 {"TextColor", "CUIPalette.Header.Text"},
29 };
30
31 public static CUIStyle Nav => new CUIStyle()
32 {
33 {"BackgroundColor", "CUIPalette.Nav.Background"},
34 {"Border", "CUIPalette.Nav.Border"},
35 {"TextColor", "CUIPalette.Nav.Text"},
36 };
37
38 public static CUIStyle Main => new CUIStyle()
39 {
40 {"BackgroundColor", "CUIPalette.Main.Background"},
41 {"Border", "CUIPalette.Main.Border"},
42 {"TextColor", "CUIPalette.Main.Text"},
43 };
44 }
45
46 //TODO all this stuff is too specific, there should be more flexible way
47 public static class CUIPrefab
48 {
49 public static CUIFrame ListFrame()
50 {
51 CUIFrame frame = new CUIFrame();
52 frame["list"] = new CUIVerticalList() { Relative = new CUINullRect(0, 0, 1, 1), };
53 return frame;
54 }
55
56
57 public static CUIComponent WrapInGroup(string name, CUIComponent content)
58 {
59 CUIVerticalList group = new CUIVerticalList() { FitContent = new CUIBool2(false, true), };
60 group["header"] = new CUITextBlock(name)
61 {
62 TextScale = 1.0f,
63 TextAlign = CUIAnchor.Center,
64 };
65 group["content"] = content;
66 return group;
67 }
68
69 public static CUIComponent Group(string name)
70 {
71 CUIVerticalList group = new CUIVerticalList()
72 {
73 FitContent = new CUIBool2(false, true),
74 };
75
76 group["header"] = new CUITextBlock(name)
77 {
78 TextScale = 1.0f,
79 TextAlign = CUIAnchor.Center,
80 };
81
82 group["content"] = new CUIVerticalList()
83 {
84 FitContent = new CUIBool2(false, true),
85 };
86
87 return group;
88 }
89
90 public static CUIComponent TextAndSliderWithLabel(string name, string command, FloatRange? range = null)
91 {
92 CUIComponent wrapper = new CUIVerticalList()
93 {
94 FitContent = new CUIBool2(false, true),
95 Style = CUIStylePrefab.Main,
96 };
97
98 wrapper["label"] = new CUITextBlock(name);
99 wrapper["controls"] = TextAndSlider(command, range);
100
101 return wrapper;
102 }
103
104 public static CUIComponent TextAndSlider(string command, FloatRange? range = null)
105 {
106 CUIHorizontalList controls = new CUIHorizontalList()
107 {
108 FitContent = new CUIBool2(false, true),
109 RetranslateCommands = true,
110 ReflectCommands = true,
111 };
112
113 controls["text"] = new CUITextInput()
114 {
115 Absolute = new CUINullRect(w: 20.0f),
116 Consumes = command,
117 Command = command,
118 };
119 controls["slider"] = new CUISlider()
120 {
121 Relative = new CUINullRect(h: 1.0f),
122 FillEmptySpace = new CUIBool2(true, false),
123 Consumes = command,
124 Command = command,
125 Range = range ?? new FloatRange(0, 1),
126 };
127
128 return controls;
129 }
130
131 public static CUIFrame NewFrame()
132 {
133 CUIFrame frame = new CUIFrame();
134 Frame(frame);
135 return frame;
136 }
137
138
139 public static CUIVerticalList ListLayout() => new CUIVerticalList() { Relative = new CUINullRect(0, 0, 1, 1) };
140
141 public static CUIHorizontalList FormHandle(string caption = "Caption")
142 {
143 CUIHorizontalList handle = new CUIHorizontalList()
144 {
145 FitContent = new CUIBool2(false, true),
146 Direction = CUIDirection.Reverse,
147 Style = CUIStylePrefab.FrameCaption,
148 };
149
150 handle["close"] = new CUICloseButton();
151
152 handle["caption"] = new CUITextBlock(caption) { FillEmptySpace = new CUIBool2(true, false) };
153
154 return handle;
155 }
156
157 public static void Frame(CUIFrame frame)
158 {
159 frame["layout"] = new CUIVerticalList() { Relative = new CUINullRect(0, 0, 1, 1), };
160 frame["layout"]["handle"] = new CUIHorizontalList()
161 {
162 FitContent = new CUIBool2(false, true),
163 Direction = CUIDirection.Reverse,
164 Style = CUIStylePrefab.FrameCaption,
165 };
166
167 frame["layout"]["handle"]["close"] = new CUICloseButton();
168
169 frame["layout"]["handle"]["caption"] = new CUITextBlock("Caption") { FillEmptySpace = new CUIBool2(true, false) };
170
171 frame["layout"]["main"] = new CUIComponent()
172 {
173 FillEmptySpace = new CUIBool2(false, true),
174 Style = CUIStylePrefab.Main,
175 ConsumeDragAndDrop = true,
176 };
177
178 frame["main"] = frame["layout"]["main"];
179 frame["caption"] = frame["layout"]["handle"]["caption"];
180 }
181
182
183
184 public static CUIHorizontalList TickboxWithLabel(string text, string command, float tickboxSize = 22.0f)
185 {
186 CUIHorizontalList list = new CUIHorizontalList()
187 {
188 FitContent = new CUIBool2(true, true),
189 };
190
191 list["tickbox"] = new CUITickBox()
192 {
193 Absolute = new CUINullRect(w: tickboxSize, h: tickboxSize),
194 Command = command,
195 Consumes = command,
196 };
197
198 list["text"] = new CUITextBlock()
199 {
200 Text = text,
201 TextAlign = CUIAnchor.CenterLeft,
202 };
203
204 return list;
205 }
206
207 //TODO this is now too specific and shouldn't be here
208 public static CUIHorizontalList InputWithValidation(PropertyInfo pi, string command)
209 {
210 string ToUserFriendly(Type T)
211 {
212 if (T == typeof(bool)) return "Boolean";
213 if (T == typeof(int)) return "Integer";
214 if (T == typeof(float)) return "Float";
215 return T.Name;
216 }
217
218 CUIHorizontalList list = new CUIHorizontalList()
219 {
220 FitContent = new CUIBool2(true, true),
221 Border = new CUIBorder(),
222 };
223
224 list["input"] = new CUITextInput()
225 {
226 AbsoluteMin = new CUINullRect(w: 100),
227 Relative = new CUINullRect(w: 0.3f),
228 Command = command,
229 Consumes = command,
230 VatidationType = pi.PropertyType,
231 };
232
233 list["label"] = new CUITextBlock()
234 {
235 FillEmptySpace = new CUIBool2(true, false),
236 Text = $"{ToUserFriendly(pi.PropertyType)} {pi.Name}",
237 TextAlign = CUIAnchor.CenterLeft,
238 BackgroundSprite = new CUISprite("gradient.png"),
239
240 Style = new CUIStyle(){
241 {"BackgroundColor", "CUIPalette.Text3.Background"},
242 {"Border", "CUIPalette.Text3.Border"},
243 {"TextColor", "CUIPalette.Text3.Text"},
244 },
245 };
246
247 return list;
248 }
249
250
251 }
252}