-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport.lua
509 lines (410 loc) · 11.6 KB
/
import.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
if IsDuplicityVersion() then
error('This resource is client-side only.', 0);
end
if Menu then
error('Menu already exists, ensure that you are not loading this resource twice.', 0);
end
---@return string
local function generateUUID()
local uuid = string.gsub('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', '[xy]', function(c)
local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb);
return string.format('%x', v);
end);
return uuid;
end
local import = exports.ragemenu;
---@param message any
local function SendNUIMessage(message)
import:SendNUIMessage(message);
end
---@param hasFocus boolean
---@param hasCursor boolean
local function SetNuiFocus(hasFocus, hasCursor)
import:SetNuiFocus(hasFocus, hasCursor);
end
---@param keepInput boolean
local function SetNuiFocusKeepInput(keepInput)
import:SetNuiFocusKeepInput(keepInput);
end
---@class MenuClass
Menu = {
__cached = {},
opened = {},
current = nil
};
function Menu:GetById(id)
for _, menu in next, self.__cached do
if menu.id == id then
return menu;
end
end
end
function Menu:GetOpened()
if self.current then
return self:GetById(self.current);
end
end
local function resetNUI()
SetNuiFocus(false, false);
SetNuiFocusKeepInput(false);
SendNUIMessage({ action = 'SetMenu' });
SendNUIMessage({ action = 'SetItems' });
end
---@param menu Menu
function Menu:Open(menu)
if self.current == menu.id then
return;
end
-- get the menu again just in case, anything wasn't updated correctly
menu = self:GetById(menu.id) --[[@as Menu]];
if not menu then
return self:CloseAll();
end
for index, menuId in next, self.opened do
if menuId == menu.id then
table.remove(self.opened, index);
end
end
self.opened[#self.opened + 1] = menu.id;
self.current = menu.id;
SetNuiFocus(true, false);
SetNuiFocusKeepInput(true);
SendNUIMessage({
action = 'SetMenu',
data = menu:toJSON()
});
SendNUIMessage({
action = 'SetItems',
data = menu:componentsToJSON()
});
end
function Menu:Close()
if #self.opened == 0 then
return self:CloseAll();
end
for index, menuId in next, self.opened do
if menuId == self.current then
table.remove(self.opened, index);
end
end
local lastOpened = self:GetById(self.opened[#self.opened]);
if not lastOpened then
return self:CloseAll();
end
if lastOpened then
self:Open(lastOpened);
end
end
function Menu:CloseAll()
if not self.current then
return;
end
self.opened = {};
self.current = nil;
resetNUI();
end
function Menu:Create(menuTitle, menuSubtitle, menuPosition, menuWidth, maxVisibleItems, banner)
---@class Menu
local menu = {
id = generateUUID(),
resource = GetCurrentResourceName(),
title = menuTitle,
subtitle = menuSubtitle,
position = menuPosition or 'top-left',
width = menuWidth,
maxVisibleItems = maxVisibleItems,
banner = banner,
__components = {}
};
function menu:set(key, value)
if key == 'id' then
error('Cannot set id of menu', 0);
end
if key == 'resource' then
error('Cannot set resource of menu', 0);
end
self[key] = value;
if Menu.current == self.id then
SendNUIMessage({
action = 'UpdateMenu',
data = { [key] = value }
});
end
end
function menu:SetTitle(title)
self:set('title', title);
end
function menu:SetSubtitle(subtitle)
self:set('subtitle', subtitle);
end
function menu:RemoveComponent(id)
for index, component in next, self.__components do
if component.id == id then
table.remove(self.__components, index);
SendNUIMessage({
action = 'RemoveItem',
data = id
});
break;
end
end
end
function menu:addComponent(type, label, rightLabel, description, badges, disabled, values, checked, current, iconStyle,
max, min, step)
---@type MenuComponent
local component = {
id = generateUUID(),
type = type,
label = label,
rightLabel = rightLabel,
description = description,
badges = badges,
disabled = disabled,
visible = true,
values = values,
checked = checked,
current = current,
iconStyle = iconStyle,
max = max,
min = min,
step = step,
__events = {}
};
function component:set(key, value)
if key == 'type' then
error('Cannot set type of component', 0);
end
if key == 'id' then
error('Cannot set id of component', 0);
end
self[key] = value;
if Menu.current == menu.id then
SendNUIMessage({
action = 'UpdateItem',
data = {
id = self.id,
[key] = value
}
});
end
end
function component:SetLabel(label)
self:set('label', label);
end
function component:SetDescription(description)
self:set('description', description);
end
function component:SetBadges(badges)
self:set('badges', badges);
end
function component:on(event, func)
if not self.__events[event] then
self.__events[event] = {};
end
self.__events[event][#self.__events[event] + 1] = func;
return function()
for _, eventFunc in next, self.__events[event] do
if eventFunc == func then
table.remove(self.__events[event], _);
end
end
end
end
function component:trigger(event, ...)
local args = { ... };
if not self.__events[event] then
return;
end
for _, func in next, self.__events[event] do
Citizen.CreateThreadNow(function(threadId)
func(table.unpack(args));
if threadId and IsThreadActive(threadId) then
TerminateThread(threadId);
end
end);
end
end
if component.type == 'checkbox' then
function component:SetChecked(checked)
self:set('checked', checked);
end
function component:SetIconStyle(iconStyle)
self:set('iconStyle', iconStyle);
end
function component:OnCheck(func)
return self:on('check', func);
end
end
if component.type == 'list' then
function component:SetValues(values)
self:set('values', values);
end
end
if component.type == 'slider' then
function component:SetMax(max)
self:set('max', max);
end
function component:SetMin(min)
self:set('min', min);
end
function component:SetStep(step)
self:set('step', step);
end
end
if component.type == 'list' or component.type == 'slider' then
function component:SetCurrent(current)
self:set('current', current);
end
function component:OnChange(func)
return self:on('change', func);
end
end
if component.type ~= 'separator' then
function component:OnClick(func)
return self:on('click', func);
end
function component:Disable(disable)
self:set('disabled', disable);
end
function component:SetRightLabel(rightLabel)
self:set('rightLabel', rightLabel);
end
function component:OnSelect(func)
return self:on('select', func);
end
end
function component:ToggleVisiblity(visible)
self:set('visible', visible);
end
function component:Remove()
menu:RemoveComponent(self.id);
end
function component:toJSON()
return {
id = self.id,
type = self.type,
label = self.label,
rightLabel = self.rightLabel,
description = self.description,
badges = self.badges,
disabled = self.disabled,
visible = self.visible,
values = self.values,
checked = self.checked,
current = self.current,
iconStyle = self.iconStyle,
max = self.max,
min = self.min,
step = self.step
};
end
menu.__components[#menu.__components + 1] = component;
if menu:IsOpen() then
SendNUIMessage({
action = 'AddItem',
data = component:toJSON()
});
end
return component;
end
function menu:AddButton(label, rightLabel, description, badges, disabled)
return self:addComponent('button', label, rightLabel, description, badges, disabled);
end
function menu:AddSubmenu(submenu, label, rightLabel, description, badges, disabled)
if not badges or not badges.right then
badges = {
left = badges and badges.left or nil,
right = 'arrowright'
};
end
local button = self:AddButton(label, rightLabel, description, badges --[[@as { left?: BadgeName; right: BadgeName }]],
disabled);
button:OnClick(function()
local subMenu = Menu:GetById(type(submenu) == 'string' and submenu or submenu.id);
if not subMenu then
return;
end
subMenu:Open();
end);
return button;
end
function menu:AddSeparator(label, badges)
return self:addComponent('separator', label, nil, nil, badges);
end
function menu:AddCheckbox(label, description, badges, checked, iconStyle, disabled)
return self:addComponent('checkbox', label, nil, description, badges, disabled, nil, checked, nil, iconStyle);
end
function menu:AddList(label, description, badges, values, current, disabled)
return self:addComponent('list', label, nil, description, badges, disabled, values, nil,
current and math.min(current - 1, 0) or 0);
end
function menu:AddSlider(label, description, badges, max, min, step, current, disabled)
return self:addComponent('slider', label, nil, description, badges, disabled, nil, nil, current or 0, nil, max,
min, step);
end
function menu:Open()
return Menu:Open(self);
end
function menu:Close()
if Menu.current == self.id then
Menu:Close();
end
end
function menu:IsOpen()
return Menu.current == self.id;
end
function menu:toJSON()
return {
id = self.id,
resource = self.resource,
title = self.title,
subtitle = self.subtitle,
position = self.position,
width = self.width,
maxVisibleItems = self.maxVisibleItems,
banner = self.banner
};
end
function menu:componentsToJSON()
local components = {};
for _, component in next, self.__components do
components[#components + 1] = component:toJSON();
end
return components;
end
function menu:getComponentById(id)
for _, component in next, self.__components do
if component.id == id then
return component;
end
end
end
self.__cached[#self.__cached + 1] = menu;
return menu;
end
---@param callback 'OnSelect' | 'OnChange' | 'OnCheck' | 'OnClick' | 'Exit'
---@param req CallbackRequest
exports('Emit', function(callback, req)
local menu = Menu:GetById(req.menu.id);
if not menu then
return;
end
local component = menu:getComponentById(req.selected);
if callback == 'OnSelect' and component then
component:trigger('select', component);
elseif callback == 'OnChange' and component then
component.current = req.current;
if component.type == 'list' then
component:trigger('change', component, req.current + 1, component.values[req.current + 1]);
elseif component.type == 'slider' then
component:trigger('change', component, req.current);
end
elseif callback == 'OnCheck' and component then
component.checked = req.checked;
component:trigger('check', component, req.checked);
elseif callback == 'OnClick' and component then
component:trigger('click', component);
elseif callback == 'Exit' then
menu:Close();
end
end);