-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLTGUI.cs
277 lines (249 loc) · 6.33 KB
/
LTGUI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using UnityEngine;
public class LTGUI
{
public enum Element_Type
{
Texture,
Label
}
public static int RECT_LEVELS = 5;
public static int RECTS_PER_LEVEL = 10;
public static int BUTTONS_MAX = 24;
private static LTRect[] levels;
private static int[] levelDepths;
private static Rect[] buttons;
private static int[] buttonLevels;
private static int[] buttonLastFrame;
private static LTRect r;
private static Color color = Color.white;
private static bool isGUIEnabled = false;
private static int global_counter = 0;
public static void init()
{
if (levels == null)
{
levels = new LTRect[RECT_LEVELS * RECTS_PER_LEVEL];
levelDepths = new int[RECT_LEVELS];
}
}
public static void initRectCheck()
{
if (buttons == null)
{
buttons = new Rect[BUTTONS_MAX];
buttonLevels = new int[BUTTONS_MAX];
buttonLastFrame = new int[BUTTONS_MAX];
for (int i = 0; i < buttonLevels.Length; i++)
{
buttonLevels[i] = -1;
}
}
}
public static void reset()
{
if (isGUIEnabled)
{
isGUIEnabled = false;
for (int i = 0; i < levels.Length; i++)
{
levels[i] = null;
}
for (int j = 0; j < levelDepths.Length; j++)
{
levelDepths[j] = 0;
}
}
}
public static void update(int updateLevel)
{
if (isGUIEnabled)
{
init();
if (levelDepths[updateLevel] > 0)
{
color = GUI.color;
int num = updateLevel * RECTS_PER_LEVEL;
int num2 = num + levelDepths[updateLevel];
for (int i = num; i < num2; i++)
{
r = levels[i];
if (r != null)
{
if (r.useColor)
{
GUI.color = r.color;
}
if (r.type == Element_Type.Label)
{
if (r.style != null)
{
GUI.skin.label = r.style;
}
if (r.useSimpleScale)
{
GUI.Label(new Rect((r.rect.x + r.margin.x + r.relativeRect.x) * r.relativeRect.width, (r.rect.y + r.margin.y + r.relativeRect.y) * r.relativeRect.height, r.rect.width * r.relativeRect.width, r.rect.height * r.relativeRect.height), r.labelStr);
}
else
{
GUI.Label(new Rect(r.rect.x + r.margin.x, r.rect.y + r.margin.y, r.rect.width, r.rect.height), r.labelStr);
}
}
else if (r.type == Element_Type.Texture && r.texture != null)
{
Vector2 vector = (!r.useSimpleScale) ? new Vector2(r.rect.width, r.rect.height) : new Vector2(0f, r.rect.height * r.relativeRect.height);
if (r.sizeByHeight)
{
vector.x = (float)r.texture.width / (float)r.texture.height * vector.y;
}
if (r.useSimpleScale)
{
GUI.DrawTexture(new Rect((r.rect.x + r.margin.x + r.relativeRect.x) * r.relativeRect.width, (r.rect.y + r.margin.y + r.relativeRect.y) * r.relativeRect.height, vector.x, vector.y), r.texture);
}
else
{
GUI.DrawTexture(new Rect(r.rect.x + r.margin.x, r.rect.y + r.margin.y, vector.x, vector.y), r.texture);
}
}
}
}
GUI.color = color;
}
}
}
public static bool checkOnScreen(Rect rect)
{
bool flag = rect.x + rect.width < 0f;
bool flag2 = rect.x > (float)Screen.width;
bool flag3 = rect.y > (float)Screen.height;
bool flag4 = rect.y + rect.height < 0f;
return !flag && !flag2 && !flag3 && !flag4;
}
public static void destroy(int id)
{
int num = id & 0xFFFF;
int num2 = id >> 16;
if (id >= 0 && levels[num] != null && levels[num].hasInitiliazed && levels[num].counter == num2)
{
levels[num] = null;
}
}
public static LTRect label(Rect rect, string label, int depth)
{
return LTGUI.label(new LTRect(rect), label, depth);
}
public static LTRect label(LTRect rect, string label, int depth)
{
rect.type = Element_Type.Label;
rect.labelStr = label;
return element(rect, depth);
}
public static LTRect texture(Rect rect, Texture texture, int depth)
{
return LTGUI.texture(new LTRect(rect), texture, depth);
}
public static LTRect texture(LTRect rect, Texture texture, int depth)
{
rect.type = Element_Type.Texture;
rect.texture = texture;
return element(rect, depth);
}
public static LTRect element(LTRect rect, int depth)
{
isGUIEnabled = true;
init();
int num = depth * RECTS_PER_LEVEL + RECTS_PER_LEVEL;
int num2 = 0;
if (rect != null)
{
destroy(rect.id);
}
if (rect.type == Element_Type.Label && rect.style != null)
{
Color textColor = rect.style.normal.textColor;
if (textColor.a <= 0f)
{
Debug.LogWarning("Your GUI normal color has an alpha of zero, and will not be rendered.");
}
}
if (rect.relativeRect.width == float.PositiveInfinity)
{
rect.relativeRect = new Rect(0f, 0f, (float)Screen.width, (float)Screen.height);
}
for (int i = depth * RECTS_PER_LEVEL; i < num; i++)
{
r = levels[i];
if (r == null)
{
r = rect;
r.rotateEnabled = true;
r.alphaEnabled = true;
r.setId(i, global_counter);
levels[i] = r;
if (num2 >= levelDepths[depth])
{
levelDepths[depth] = num2 + 1;
}
global_counter++;
return r;
}
num2++;
}
Debug.LogError("You ran out of GUI Element spaces");
return null;
}
public static bool hasNoOverlap(Rect rect, int depth)
{
initRectCheck();
bool result = true;
bool flag = false;
for (int i = 0; i < buttonLevels.Length; i++)
{
if (buttonLevels[i] >= 0)
{
if (buttonLastFrame[i] + 1 < Time.frameCount)
{
buttonLevels[i] = -1;
}
else if (buttonLevels[i] > depth && pressedWithinRect(buttons[i]))
{
result = false;
}
}
if (!flag && buttonLevels[i] < 0)
{
flag = true;
buttonLevels[i] = depth;
buttons[i] = rect;
buttonLastFrame[i] = Time.frameCount;
}
}
return result;
}
public static bool pressedWithinRect(Rect rect)
{
Vector2 vector = firstTouch();
if (vector.x < 0f)
{
return false;
}
float num = (float)Screen.height - vector.y;
return vector.x > rect.x && vector.x < rect.x + rect.width && num > rect.y && num < rect.y + rect.height;
}
public static bool checkWithinRect(Vector2 vec2, Rect rect)
{
vec2.y = (float)Screen.height - vec2.y;
return vec2.x > rect.x && vec2.x < rect.x + rect.width && vec2.y > rect.y && vec2.y < rect.y + rect.height;
}
public static Vector2 firstTouch()
{
if (Input.touchCount > 0)
{
return Input.touches[0].position;
}
if (Input.GetMouseButton(0))
{
return Input.mousePosition;
}
return new Vector2(float.NegativeInfinity, float.NegativeInfinity);
}
}