-
Notifications
You must be signed in to change notification settings - Fork 19
/
UI.lua
433 lines (368 loc) · 11.8 KB
/
UI.lua
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
--[[
UI Library for Lmaobox
Author: LNX (github.com/lnx00)
]]
local UI = {}
UI.Enabled = true
UI.DefaultFont = draw.CreateFont("verdana", 14, 510)
UI.DefaultColor = { R = 200, G = 200, B = 200, A = 255 }
UI.DefaultSpeed = 400
UI._currentID = 1
UI._rectTable = {}
UI._lineTable = {}
UI._textTable = {}
TextAlign = {
LEFT = 1,
CENTER = 2,
RIGHT = 3
}
Colors = {
WHITE = { R = 255, G = 255, B = 255, A = 255 },
BLACK = { R = 0, G = 0, B = 0, A = 255 },
RED = { R = 255, G = 0, B = 0, A = 255 },
GREEN = { R = 0, G = 255, B = 0, A = 255 },
BLUE = { R = 0, G = 0, B = 255, A = 255 },
YELLOW = { R = 255, G = 255, B = 0, A = 255 },
ORANGE = { R = 255, G = 128, B = 0, A = 255 },
PURPLE = { R = 255, G = 0, B = 255, A = 255 },
CYAN = { R = 0, G = 255, B = 255, A = 255 }
}
local ANIM_NONE <const> = 0
local ANIM_FADEIN <const> = 1
local ANIM_FADEOUT <const> = 2
function CopyColor(pColor)
return { R = pColor.R, G = pColor.G, B = pColor.B, A = pColor.A }
end
function CopyPos(pPos)
return { X = pPos.X, Y = pPos.Y }
end
function CopySize(pSize)
return { Width = pSize.Width, Height = pSize.Height }
end
--[[ Rect Element ]]--
local Rect = {
ID = 0,
Visible = true,
Position = { X = 100, Y = 100 },
Size = { Width = 200, Height = 200 },
Filled = true,
Color = CopyColor(UI.DefaultColor),
Speed = 400,
_animation = ANIM_NONE,
_color = CopyColor(UI.DefaultColor), -- Color to restore after animations
_position = { X = 100, Y = 100 },
_size = { Width = 200, Height = 200 }
}
local MetaRect = {}
MetaRect.__index = Rect
function Rect.Create(pPosition, pSize, pFilled, pColor, pVisible, pSpeed)
pColor = pColor or CopyColor(UI.DefaultColor)
pVisible = (pVisible ~= false)
pSpeed = pSpeed or UI.DefaultSpeed
local iRect = setmetatable({}, MetaRect)
iRect.ID = UI._currentID
iRect.Position = pPosition
iRect.Size = pSize
iRect.Filled = pFilled
iRect.Color = pColor
iRect.Speed = pSpeed
-- We don't want a reference of these tables
iRect._color = CopyColor(pColor)
iRect._position = CopyPos(pPosition)
iRect._size = CopySize(pSize)
iRect:SetVisible(pVisible)
table.insert(UI._rectTable, iRect)
UI._currentID = UI._currentID + 1
return iRect
end
function Rect:SetColor(pColor)
self.Color = pColor
self._color = CopyColor(pColor)
end
function Rect:SetPosition(pPosition)
self.Position = pPosition
self._position = CopyPos(pPosition)
end
function Rect:SetSize(pSize)
self.Size = pSize
self._size = CopySize(pSize)
end
function Rect:SetVisible(pState)
self.Visible = pState
if self.Visible then
self.Color.A = self._color.A
else
self.Color.A = 0
end
end
function Rect:SetCoordinated(pX, pY, pX2, pY2)
self.Position = { X = pX, Y = pY }
self.Width = pX + pX2
self.Height = pY + pY2
end
function Rect:FadeIn(pSpeed)
self.Speed = pSpeed or self.Speed
if self.Color.A < self._color.A then
self.Visible = true
self._animation = ANIM_FADEIN
end
end
function Rect:FadeOut(pSpeed)
self.Speed = pSpeed or self.Speed
if self.Color.A > 0 then
self._animation = ANIM_FADEOUT
end
end
function Rect:Transform(pPosition, pSize, pSpeed)
self._position = pPosition or self._position
self._size = pSize or self._size
self.Speed = pSpeed or self.Speed
end
function Rect:Cancel()
self._animation = ANIM_NONE
self.Color = self._color
self.Position = self._position
end
--[[ Line Element ]]--
local Line = {
ID = 0,
Visible = true,
Points = { X = 100, Y = 100, X2 = 200, Y2 = 200 },
Color = CopyColor(UI.DefaultColor),
Speed = 400,
_animation = ANIM_NONE,
_color = CopyColor(UI.DefaultColor), -- Color to restore after animations
}
local MetaLine = {}
MetaLine.__index = Line
function Line.Create(pPoints, pColor, pVisible, pSpeed)
pColor = pColor or CopyColor(UI.DefaultColor)
pVisible = (pVisible ~= false)
pSpeed = pSpeed or 400
local iLine = setmetatable({}, MetaLine)
iLine.ID = UI._currentID
iLine.Points = pPoints
iLine.Color = pColor
iLine.Speed = pSpeed
-- We don't want a reference of these tables
iLine._color = CopyColor(pColor)
iLine:SetVisible(pVisible)
table.insert(UI._lineTable, iLine)
UI._currentID = UI._currentID + 1
return iLine
end
function Line:SetColor(pColor)
self.Color = pColor
self._color = CopyColor(pColor)
end
function Line:SetVisible(pState)
self.Visible = pState
if self.Visible then
self.Color.A = self._color.A
else
self.Color.A = 0
end
end
function Line:FadeIn(pSpeed)
self.Speed = pSpeed or self.Speed
if self.Color.A < self._color.A then
self.Visible = true
self._animation = ANIM_FADEIN
end
end
function Line:FadeOut(pSpeed)
self.Speed = pSpeed or self.Speed
if self.Color.A > 0 then
self._animation = ANIM_FADEOUT
end
end
function Line:Cancel()
self._animation = ANIM_NONE
self.Color = self._color
self.Position = self._position
end
--[[ Text Element ]]--
local Text = {
ID = 0,
Visible = true,
Position = { X = 100, Y = 100 },
Color = CopyColor(UI.DefaultColor),
Shadow = false,
Font = UI.DefaultFont,
Align = TextAlign.LEFT,
Speed = 400,
_animation = ANIM_NONE,
_color = CopyColor(UI.DefaultColor), -- Color to restore after animations
_position = { X = 100, Y = 100 }
}
local MetaText = {}
MetaText.__index = Text
function Text.Create(pPosition, pText, pColor, pShadow, pAlign, pFont, pVisible, pSpeed)
pColor = pColor or CopyColor(UI.DefaultColor)
pShadow = pShadow or false
pFont = pFont or UI.DefaultFont
pAlign = pAlign or TextAlign.LEFT
pVisible = (pVisible ~= false)
pSpeed = pSpeed or UI.DefaultSpeed
local iText = setmetatable({}, MetaText)
iText.ID = UI._currentID
iText.Position = pPosition
iText.Text = pText
iText.Color = pColor
iText.Shadow = pShadow
iText.Font = pFont
iText.Align = pAlign
iText.Speed = pSpeed
-- We don't want a reference of these tables
iText._color = CopyColor(pColor)
iText._position = CopyPos(pPosition)
iText:SetVisible(pVisible)
table.insert(UI._textTable, iText)
UI._currentID = UI._currentID + 1
return iText
end
function Text:SetColor(pColor)
self.Color = pColor
self._color = CopyColor(pColor)
end
function Text:SetPosition(pPosition)
self.Position = pPosition
self._position = CopyPos(pPosition)
end
function Text:SetVisible(pState)
self.Visible = pState
if self.Visible then
self.Color.A = self._color.A
else
self.Color.A = 0
end
end
function Text:FadeIn(pSpeed)
self.Speed = pSpeed or self.Speed
if self.Color.A < self._color.A then
self.Visible = true
self._animation = ANIM_FADEIN
end
end
function Text:FadeOut(pSpeed)
self.Speed = pSpeed or self.Speed
if self.Color.A > 0 then
self._animation = ANIM_FADEOUT
end
end
function Text:Cancel()
self._animation = ANIM_NONE
self.Color = self._color
self.Position = self._position
end
function Text:Transform(pPosition, pSpeed)
self._position = pPosition or self._position
self.Speed = pSpeed or self.Speed
end
--[[ UI ]]--
function UI._Animate(self)
if self._animation == ANIM_FADEIN then
-- Fade in animation
if self.Color.A < self._color.A then
self.Color.A = math.min(self.Color.A + globals.FrameTime() * self.Speed, self._color.A)
else
self._animation = ANIM_NONE
self.Visible = true
end
elseif self._animation == ANIM_FADEOUT then
-- Fade out animation
if self.Color.A > 0 then
self.Color.A = math.max(self.Color.A - globals.FrameTime() * self.Speed, 0)
else
self._animation = ANIM_NONE
self.Visible = false
end
end
if self.Position and self._position then
if self.Position.X < self._position.X or self.Position.Y < self._position.Y then
self.Position.X = math.min(self.Position.X + globals.FrameTime() * self.Speed, self._position.X)
self.Position.Y = math.min(self.Position.Y + globals.FrameTime() * self.Speed, self._position.Y)
end
if self.Position.X > self._position.X or self.Position.Y > self._position.Y then
self.Position.X = math.max(self.Position.X - globals.FrameTime() * self.Speed, self._position.X)
self.Position.Y = math.max(self.Position.Y - globals.FrameTime() * self.Speed, self._position.Y)
end
end
if self.Size and self._size then
if self.Size.Width < self._size.Width or self.Size.Height < self._size.Height then
self.Size.Width = math.min(self.Size.Width + globals.FrameTime() * self.Speed, self._size.Width)
self.Size.Height = math.min(self.Size.Height + globals.FrameTime() * self.Speed, self._size.Height)
end
if self.Size.Width > self._size.Width or self.Size.Height > self._size.Height then
self.Size.Width = math.max(self.Size.Width - globals.FrameTime() * self.Speed, self._size.Width)
self.Size.Height = math.max(self.Size.Height - globals.FrameTime() * self.Speed, self._size.Height)
end
end
end
-- Draw all UI Elements
function UI.Draw()
if not UI.Enabled then
return
end
-- Draw Rects
for k, r in pairs(UI._rectTable) do
if r.Visible then
draw.Color(r.Color.R, r.Color.G, r.Color.B, math.floor(r.Color.A))
if r.Filled then
draw.FilledRect(math.floor(r.Position.X), math.floor(r.Position.Y), math.floor(r.Position.X + r.Size.Width), math.floor(r.Position.Y + r.Size.Height))
else
draw.OutlinedRect(math.floor(r.Position.X), math.floor(r.Position.Y), math.floor(r.Position.X + r.Size.Width), math.floor(r.Position.Y + r.Size.Height))
end
end
UI._Animate(r)
end
-- Draw Lines
for k, l in pairs(UI._lineTable) do
if l.Visible then
draw.Color(l.Color.R, l.Color.G, l.Color.B, math.floor(l.Color.A))
draw.Line(math.floor(l.Points.X), math.floor(l.Points.Y), math.floor(l.Points.X2), math.floor(l.Points.Y2))
end
UI._Animate(l)
end
-- Draw Texts
for k, t in pairs(UI._textTable) do
if t.Visible then
draw.SetFont(t.Font)
draw.Color(t.Color.R, t.Color.G, t.Color.B, math.floor(t.Color.A))
local xPos = t.Position.X
local sizeX, sizeY = draw.GetTextSize(t.Text)
if t.Align == TextAlign.CENTER then
xPos = xPos - (sizeX / 2) + 5
elseif t.Align == TextAlign.RIGHT then
xPos = xPos - sizeX
end
if t.Shadow then
draw.TextShadow(math.floor(xPos), math.floor(t.Position.Y), t.Text)
else
draw.Text(math.floor(xPos), math.floor(t.Position.Y), t.Text)
end
end
UI._Animate(t)
end
end
function UI.AddRect(pX, pY, pWidth, pHeight, pFilled, pColor, pVisible)
return Rect.Create({ X = pX, Y = pY }, { Width = pWidth, Height = pHeight }, pFilled, pColor, pVisible)
end
function UI.RemoveRect(pElement)
table.remove(UI._rectTable, pElement)
end
function UI.AddLine(pX, pY, pX2, pY2, pColor, pVisible)
return Line.Create({ X = pX, Y = pY, X2 = pX2, Y2 = pY2 }, pColor, pVisible)
end
function UI.RemoveLine(pElement)
table.remove(UI._lineTable, pElement)
end
function UI.AddText(pX, pY, pText, pColor, pShadow, pAlign, pFont, pVisible)
return Text.Create({ X = pX, Y = pY }, pText, pColor, pShadow, pAlign, pFont, pVisible)
end
function UI.RemoveText(pElement)
table.remove(UI._textTable, pElement)
end
callbacks.Unregister("Draw", "Draw_UI");
callbacks.Register("Draw", "Draw_UI", UI.Draw)
return UI