-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport.lua
431 lines (343 loc) · 9.23 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
if IsDuplicityVersion() then
error('This resource is client-side only.', 0);
end
---@return string
local function generateUUID()
local uuid <const> = 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 <const> = 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',
data = {}
});
end
---@param menu Menu
function Menu:Open(menu)
if self.current == menu.id then
return;
end
if self.current ~= nil then
local oldMenu = self:GetById(self.current);
if oldMenu then
oldMenu:Close();
self.opened[#self.opened + 1] = self.current;
end
end
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 not self.current then
return;
end
if #self.opened == 0 then
self.current = nil;
return resetNUI();
end
local lastOpened = self:GetById(self.opened[#self.opened]);
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)
---@class Menu
local menu = {
id = generateUUID(),
resource = GetCurrentResourceName(),
title = menuTitle,
subtitle = menuSubtitle,
__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;
SendNUIMessage({
action = 'UpdateMenu',
data = { [key] = value }
});
end
function menu:SetTitle(title)
self:set('title', title);
end
function menu:SetSubtitle(subtitle)
self:set('subtitle', subtitle);
end
function menu:addComponent(type, label, description, badges, values, checked, current, iconStyle, max, min, step)
---@type MenuComponent
local component = {
id = generateUUID(),
type = type,
label = label,
description = description,
badges = badges,
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;
SendNUIMessage({
action = 'UpdateItem',
data = {
id = self.id,
[key] = value
}
});
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, ...)
if not self.__events[event] then
return;
end
for _, func in next, self.__events[event] do
func(...);
end
end
function component:OnSelect(func)
return self:on('select', func);
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
else
function component:OnClick(func)
return self:on('click', 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
function component:toJSON()
return {
id = self.id,
type = self.type,
label = self.label,
description = self.description,
badges = self.badges,
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, description, badges)
return self:addComponent('button', label, description, badges);
end
function menu:AddSubmenu(submenu, label, description, badges)
local button = self:AddButton(label, description, badges);
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, badges);
end
function menu:AddCheckbox(label, description, badges, checked, iconStyle)
return self:addComponent('checkbox', label, description, badges, nil, checked, nil, iconStyle);
end
function menu:AddList(label, description, badges, values, current)
return self:addComponent('list', label, description, badges, values, nil, current);
end
function menu:AddSlider(label, description, badges, max, min, step, current)
return self:addComponent('slider', label, description, badges, 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
};
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, component.values[req.current]);
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);