forked from ThymonA/menuv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenuv.lua
More file actions
2486 lines (2030 loc) · 83.9 KB
/
Copy pathmenuv.lua
File metadata and controls
2486 lines (2030 loc) · 83.9 KB
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
----------------------- [ MenuV ] -----------------------
-- GitHub: https://github.com/ThymonA/menuv/
-- License: GNU General Public License v3.0
-- https://choosealicense.com/licenses/gpl-3.0/
-- Author: Thymon Arens <contact@arens.io>
-- Name: MenuV
-- Version: 1.4.1
-- Description: FiveM menu library for creating menu's
----------------------- [ MenuV ] -----------------------
local assert = assert
local pairs = assert(pairs)
local rawget = assert(rawget)
local rawset = assert(rawset)
local insert = assert(table.insert)
local remove = assert(table.remove)
local format = assert(string.format)
local upper = assert(string.upper)
local lower = assert(string.lower)
local setmetatable = assert(setmetatable)
--- FiveM globals
local GET_CURRENT_RESOURCE_NAME = assert(GetCurrentResourceName)
local HAS_STREAMED_TEXTURE_DICT_LOADED = assert(HasStreamedTextureDictLoaded)
local REQUEST_STREAMED_TEXTURE_DICT = assert(RequestStreamedTextureDict)
local REGISTER_KEY_MAPPING = assert(RegisterKeyMapping)
local REGISTER_COMMAND = assert(RegisterCommand)
local GET_HASH_KEY = assert(GetHashKey)
local CreateThread = assert(Citizen.CreateThread)
local Wait = assert(Citizen.Wait)
----------------------- [ MenuV ] -----------------------
-- GitHub: https://github.com/ThymonA/menuv/
-- License: GNU General Public License v3.0
-- https://choosealicense.com/licenses/gpl-3.0/
-- Author: Thymon Arens <contact@arens.io>
-- Name: MenuV
-- Version: 1.4.1
-- Description: FiveM menu library for creating menu's
----------------------- [ MenuV ] -----------------------
Config = {
Language = 'en',
HideInterval = 250,
Sounds = {
UP = {
type = 'native',
name = 'NAV_UP_DOWN',
library = 'HUD_FREEMODE_SOUNDSET'
},
DOWN = {
type = 'native',
name = 'NAV_UP_DOWN',
library = 'HUD_FREEMODE_SOUNDSET'
},
LEFT = {
type = 'native',
name = 'NAV_LEFT_RIGHT',
library = 'HUD_FRONTEND_DEFAULT_SOUNDSET'
},
RIGHT = {
type = 'native',
name = 'NAV_LEFT_RIGHT',
library = 'HUD_FRONTEND_DEFAULT_SOUNDSET'
},
ENTER = {
type = 'native',
name = 'SELECT',
library = 'HUD_FRONTEND_DEFAULT_SOUNDSET'
},
CLOSE = {
type = 'native',
name = 'BACK',
library = 'HUD_FRONTEND_DEFAULT_SOUNDSET'
}
}
}
_G.Config = Config
_ENV.Config = Config
----------------------- [ MenuV ] -----------------------
-- GitHub: https://github.com/ThymonA/menuv/
-- License: GNU General Public License v3.0
-- https://choosealicense.com/licenses/gpl-3.0/
-- Author: Thymon Arens <contact@arens.io>
-- Name: MenuV
-- Version: 1.4.1
-- Description: FiveM menu library for creating menu's
----------------------- [ MenuV ] -----------------------
local assert = assert
local type = assert(type)
local tonumber = assert(tonumber)
local tostring = assert(tostring)
local lower = assert(string.lower)
local upper = assert(string.upper)
local sub = assert(string.sub)
local encode = assert(json.encode)
local decode = assert(json.decode)
local floor = assert(math.floor)
local random = assert(math.random)
local randomseed = assert(math.randomseed)
local rawget = assert(rawget)
local setmetatable = assert(setmetatable)
--- FiveM globals
local GET_GAME_TIMER = assert(GetGameTimer)
local GET_CURRENT_RESOURCE_NAME = assert(GetCurrentResourceName)
--- Utilities for MenuV
---@class Utilities
local Utilities = setmetatable({ __class = 'Utilities' }, {})
--- Returns `true` if `input` starts with `start`, otherwise `false`
---@param input string Checks if this string starts with `start`
---@param start string Checks if `input` starts with this
---@return boolean `true` if `input` starts with `start`, otherwise `false`
function Utilities:StartsWith(input, start)
if (self:Typeof(input) ~= 'string') then return false end
if (self:Typeof(start) == 'number') then start = tostring(start) end
if (self:Typeof(start) ~= 'string') then return false end
return sub(input, 1, #start) == start
end
--- Returns `true` if `input` ends with `ends`, otherwise `false`
---@param input string Checks if this string ends with `ends`
---@param ends string Checks if `input` ends with this
---@return boolean `true` if `input` ends with `ends`, otherwise `false`
function Utilities:EndsWith(input, ends)
if (self:Typeof(input) ~= 'string') then return false end
if (self:Typeof(ends) == 'number') then ends = tostring(ends) end
if (self:Typeof(ends) ~= 'string') then return false end
return sub(input, -#ends) == ends
end
--- Returns the type of given `input`
---@param input any Any input
---@return string Type of given input
function Utilities:Typeof(input)
if (input == nil) then return 'nil' end
local rawType = type(input) or 'nil'
if (rawType ~= 'table') then return rawType end
local isFXFunction = rawget(input, '__cfx_functionReference') ~= nil or
rawget(input, '__cfx_async_retval') ~= nil
if (isFXFunction) then return 'function' end
if (rawget(input, '__cfx_functionSource') ~= nil) then return 'number' end
local rawClass = rawget(input, '__class')
if (rawClass ~= nil) then return type(rawClass) == 'string' and rawClass or 'class' end
local rawTableType = rawget(input, '__type')
if (rawTableType ~= nil) then return type(rawTableType) == 'string' and rawTableType or 'table' end
return rawType
end
local INPUT_GROUPS = {
[0] = "KEYBOARD",
[2] = "CONTROLLER"
}
local INPUT_TYPE_GROUPS = {
["KEYBOARD"] = 0,
["MOUSE_ABSOLUTEAXIS"] = 0,
["MOUSE_CENTEREDAXIS"] = 0,
["MOUSE_RELATIVEAXIS"] = 0,
["MOUSE_SCALEDAXIS"] = 0,
["MOUSE_NORMALIZED"] = 0,
["MOUSE_WHEEL"] = 0,
["MOUSE_BUTTON"] = 0,
["MOUSE_BUTTONANY"] = 0,
["MKB_AXIS"] = 0,
["PAD_AXIS"] = 2,
["PAD_DIGITALBUTTON"] = 2,
["PAD_DIGITALBUTTONANY"] = 2,
["PAD_ANALOGBUTTON"] = 2,
["JOYSTICK_POV"] = 2,
["JOYSTICK_POV_AXIS"] = 2,
["JOYSTICK_BUTTON"] = 2,
["JOYSTICK_AXIS"] = 2,
["JOYSTICK_IAXIS"] = 2,
["JOYSTICK_AXIS_NEGATIVE"] = 2,
["JOYSTICK_AXIS_POSITIVE"] = 2,
["PAD_DEBUGBUTTON"] = 2,
["GAME_CONTROLLED"] = 2,
["DIGITALBUTTON_AXIS"] = 2,
}
function Utilities:GetInputTypeGroup(inputType)
return INPUT_TYPE_GROUPS[inputType] or 0
end
function Utilities:GetInputGroupName(inputTypeGroup)
return INPUT_GROUPS[inputTypeGroup] or "KEYBOARD"
end
--- Transform any `input` to the same type as `defaultValue`
---@type function
---@param input any Transform this `input` to `defaultValue`'s type
---@param defaultValue any Returns this if `input` can't transformed to this type
---@param ignoreDefault boolean Don't return default value if this is true
---@return any Returns `input` matches the `defaultValue` type or `defaultValue`
function Utilities:Ensure(input, defaultValue, ignoreDefault)
ignoreDefault = type(ignoreDefault) == 'boolean' and ignoreDefault or false
if (defaultValue == nil) then return nil end
local requiredType = self:Typeof(defaultValue)
if (requiredType == 'nil') then return nil end
local inputType = self:Typeof(input)
if (inputType == requiredType) then return input end
if (inputType == 'nil') then return defaultValue end
if (requiredType == 'number') then
if (inputType == 'boolean') then return input and 1 or 0 end
return tonumber(input) or (not ignoreDefault and defaultValue or nil)
end
if (requiredType == 'string') then
if (inputType == 'boolean') then return input and 'yes' or 'no' end
if (inputType == 'vector3') then return encode({ x = input.x, y = input.y, z = input.z }) or (not ignoreDefault and defaultValue or nil) end
if (inputType == 'vector2') then return encode({ x = input.x, y = input.y }) or (not ignoreDefault and defaultValue or nil) end
if (inputType == 'table') then return encode(input) or (not ignoreDefault and defaultValue or nil) end
local result = tostring(input)
if (result == 'nil') then
return not ignoreDefault and defaultValue or 'nil'
end
return result
end
if (requiredType == 'boolean') then
if (inputType == 'string') then
input = lower(input)
if (input == 'true' or input == '1' or input == 'yes' or input == 'y') then return true end
if (input == 'false' or input == '0' or input == 'no' or input == 'n') then return false end
return (not ignoreDefault and defaultValue or nil)
end
if (inputType == 'number') then
if (input == 1) then return true end
if (input == 0) then return false end
return (not ignoreDefault and defaultValue or nil)
end
return (not ignoreDefault and defaultValue or nil)
end
if (requiredType == 'table') then
if (inputType == 'string') then
if (self:StartsWith(input, '{') and self:EndsWith(input, '}')) then
return decode(input) or (not ignoreDefault and defaultValue or nil)
end
if (self:StartsWith(input, '[') and self:EndsWith(input, ']')) then
return decode(input) or (not ignoreDefault and defaultValue or nil)
end
return (not ignoreDefault and defaultValue or nil)
end
if (inputType == 'vector3') then return { x = input.x or 0, y = input.y or 0, z = input.z or 0 } end
if (inputType == 'vector2') then return { x = input.x or 0, y = input.y or 0 } end
return (not ignoreDefault and defaultValue or nil)
end
if (requiredType == 'vector3') then
if (inputType == 'table') then
local _x = self:Ensure(input.x, defaultValue.x)
local _y = self:Ensure(input.y, defaultValue.y)
local _z = self:Ensure(input.z, defaultValue.z)
return vector3(_x, _y, _z)
end
if (inputType == 'vector2') then
local _x = self:Ensure(input.x, defaultValue.x)
local _y = self:Ensure(input.y, defaultValue.y)
return vector3(_x, _y, 0)
end
if (inputType == 'number') then
return vector3(input, input, input)
end
return (not ignoreDefault and defaultValue or nil)
end
if (requiredType == 'vector2') then
if (inputType == 'table') then
local _x = self:Ensure(input.x, defaultValue.x)
local _y = self:Ensure(input.y, defaultValue.y)
return vector2(_x, _y)
end
if (inputType == 'vector3') then
local _x = self:Ensure(input.x, defaultValue.x)
local _y = self:Ensure(input.y, defaultValue.y)
return vector2(_x, _y)
end
if (inputType == 'number') then
return vector2(input, input)
end
return (not ignoreDefault and defaultValue or nil)
end
return (not ignoreDefault and defaultValue or nil)
end
--- Checks if input exists in inputs
--- '0' and 0 are both the same '0' == 0 equals `true`
--- 'yes' and true are both the same 'yes' == true equals `true`
---@param input any Any input
---@param inputs any[] Any table
---@param checkType string | "'value'" | "'key'" | "'both'"
---@return boolean Returns `true` if input has been found as `key` and/or `value`
function Utilities:Any(input, inputs, checkType)
if (input == nil) then return false end
if (inputs == nil) then return false end
inputs = self:Ensure(inputs, {})
checkType = lower(self:Ensure(checkType, 'value'))
local checkMethod = 1
if (checkType == 'value' or checkType == 'v') then
checkMethod = 1
elseif (checkType == 'key' or checkType == 'k') then
checkMethod = -1
elseif (checkType == 'both' or checkType == 'b') then
checkMethod = 0
end
for k, v in pairs(inputs) do
if (checkMethod == 0 or checkMethod == -1) then
local checkK = self:Ensure(input, k, true)
if (checkK ~= nil and checkK == k) then return true end
end
if (checkMethod == 0 or checkMethod == 1) then
local checkV = self:Ensure(input, v, true)
if (checkV ~= nil and checkV == v) then return true end
end
end
return false
end
--- Round any `value`
---@param value number Round this value
---@param decimal number Number of decimals
---@return number Rounded number
function Utilities:Round(value, decimal)
value = self:Ensure(value, 0)
decimal = self:Ensure(decimal, 0)
if (decimal > 0) then
return floor((value * 10 ^ decimal) + 0.5) / (10 ^ decimal)
end
return floor(value + 0.5)
end
--- Checks if `item1` equals `item2`
---@param item1 any Item1
---@param item2 any Item2
---@return boolean `true` if both are equal, otherwise `false`
function Utilities:Equal(item1, item2)
if (item1 == nil and item2 == nil) then return true end
if (item1 == nil or item2 == nil) then return false end
if (type(item1) == 'table') then
local item1EQ = rawget(item1, '__eq')
if (item1EQ ~= nil and self:Typeof(item1EQ) == 'function') then
return item1EQ(item1, item2)
end
return item1 == item2
end
if (type(item2) == 'table') then
local item2EQ = rawget(item2, '__eq')
if (item2EQ ~= nil and self:Typeof(item2EQ) == 'function') then
return item2EQ(item2, item1)
end
return item2 == item1
end
return item1 == item2
end
local function tohex(x)
x = Utilities:Ensure(x, 32)
local s, base, d = '', 16
while x > 0 do
d = x % base + 1
x = floor(x / base)
s = sub('0123456789abcdef', d, d) .. s
end
while #s < 2 do s = ('0%s'):format(s) end
return s
end
local function bitwise(x, y, matrix)
x = Utilities:Ensure(x, 32)
y = Utilities:Ensure(y, 16)
matrix = Utilities:Ensure(matrix, {{0,0}, {0, 1}})
local z, pow = 0, 1
while x > 0 or y > 0 do
z = z + (matrix[x %2 + 1][y %2 + 1] * pow)
pow = pow * 2
x = floor(x / 2)
y = floor(y / 2)
end
return z
end
--- Generates a random UUID like: 00000000-0000-0000-0000-000000000000
---@return string Random generated UUID
function Utilities:UUID()
randomseed(GET_GAME_TIMER() + random(30720, 92160))
---@type number[]
local bytes = {
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255),
random(0, 255)
}
bytes[7] = bitwise(bytes[7], 0x0f, {{0,0},{0,1}})
bytes[7] = bitwise(bytes[7], 0x40, {{0,1},{1,1}})
bytes[9] = bitwise(bytes[7], 0x3f, {{0,0},{0,1}})
bytes[9] = bitwise(bytes[7], 0x80, {{0,1},{1,1}})
return upper(('%s%s%s%s-%s%s-%s%s-%s%s-%s%s%s%s%s%s'):format(
tohex(bytes[1]), tohex(bytes[2]), tohex(bytes[3]), tohex(bytes[4]),
tohex(bytes[5]), tohex(bytes[6]),
tohex(bytes[7]), tohex(bytes[8]),
tohex(bytes[9]), tohex(bytes[10]),
tohex(bytes[11]), tohex(bytes[12]), tohex(bytes[13]), tohex(bytes[14]), tohex(bytes[15]), tohex(bytes[16])
))
end
--- Replace a string that contains `this` to `that`
---@param str string String where to replace in
---@param this string Word that's need to be replaced
---@param that string Replace `this` whit given string
---@return string String where `this` has been replaced with `that`
function Utilities:Replace(str, this, that)
local b, e = str:find(this, 1, true)
if b == nil then
return str
else
return str:sub(1, b - 1) .. that .. self:Replace(str:sub(e + 1), this, that)
end
end
_G.Utilities = Utilities
_ENV.Utilities = Utilities
----------------------- [ MenuV ] -----------------------
-- GitHub: https://github.com/ThymonA/menuv/
-- License: GNU General Public License v3.0
-- https://choosealicense.com/licenses/gpl-3.0/
-- Author: Thymon Arens <contact@arens.io>
-- Name: MenuV
-- Version: 1.4.1
-- Description: FiveM menu library for creating menu's
----------------------- [ MenuV ] -----------------------
local assert = assert
---@type Utilities
local U = assert(Utilities)
local type = assert(type)
local pairs = assert(pairs)
local lower = assert(string.lower)
local upper = assert(string.upper)
local sub = assert(string.sub)
local pack = assert(table.pack)
local unpack = assert(table.unpack)
local insert = assert(table.insert)
local rawset = assert(rawset)
local rawget = assert(rawget)
local setmetatable = assert(setmetatable)
--- FiveM globals
local CreateThread = assert(Citizen.CreateThread)
--- Create a new menu item
---@param info table Menu information
---@return Item New item
function CreateMenuItem(info)
info = U:Ensure(info, {})
local item = {
---@type Menu|nil
__menu = U:Ensure(info.__Menu or info.__menu, { __class = 'Menu', __type = 'Menu' }, true) or nil,
---@type string
__event = U:Ensure(info.PrimaryEvent or info.primaryEvent, 'unknown'),
---@type string
UUID = U:UUID(),
---@type string
Icon = U:Ensure(info.Icon or info.icon, 'none'),
---@type string
Label = U:Ensure(info.Label or info.label, ''),
---@type string
Description = U:Ensure(info.Description or info.description, ''),
---@type any
Value = info.Value or info.value,
---@type table[]
Values = {},
---@type number
Min = U:Ensure(info.Min or info.min, 0),
---@type number
Max = U:Ensure(info.Max or info.max, 0),
---@type boolean
Disabled = U:Ensure(info.Disabled or info.disabled, false),
---@type table
Events = U:Ensure(info.Events or info.events, {}),
---@type boolean
SaveOnUpdate = U:Ensure(info.SaveOnUpdate or info.saveOnUpdate, false),
---@param t Item
---@param event string Name of Event
Trigger = function(t, event, ...)
event = lower(U:Ensure(event, 'unknown'))
if (event == 'unknown') then return end
if (U:StartsWith(event, 'on')) then
event = 'On' .. sub(event, 3):gsub('^%l', upper)
else
event = 'On' .. event:gsub('^%l', upper)
end
if (not U:Any(event, (t.Events or {}), 'key')) then
return
end
local args = pack(...)
for _, v in pairs(t.Events[event]) do
CreateThread(function()
v(t, unpack(args))
end)
end
end,
---@param t Item
---@param event string Name of event
---@param func function|Menu Function or Menu to trigger
On = function(t, event, func)
event = lower(U:Ensure(event, 'unknown'))
if (event == 'unknown') then return end
if (U:StartsWith(event, 'on')) then
event = 'On' .. sub(event, 3):gsub('^%l', upper)
else
event = 'On' .. event:gsub('^%l', upper)
end
if (not U:Any(event, (t.Events or {}), 'key')) then
return
end
local _type = U:Typeof(func)
if (_type == 'Menu') then
local menu_t = {
__class = 'function',
__type = 'function',
func = function(t) MenuV:OpenMenu(t.uuid) end,
uuid = func.UUID or func.uuid or U:UUID()
}
local menu_mt = { __index = menu_t, __call = function(t) t:func() end }
local menu_item = setmetatable(menu_t, menu_mt)
insert(t.Events[event], menu_item)
return
end
func = U:Ensure(func, function() end)
insert(t.Events[event], func)
end,
---@param t Item
---@param k string
---@param v string
Validate = U:Ensure(info.Validate or info.validate, function(t, k, v)
return true
end),
---@param t Item
---@param k string
---@param v string
Parser = U:Ensure(info.Parser or info.parser, function(t, k, v)
return v
end),
---@param t Item
---@param k string
---@param v string
NewIndex = U:Ensure(info.NewIndex or info.newIndex, function(t, k, v)
end),
---@param t Item
---@return any
GetValue = function(t)
local itemType = U:Ensure(t.__type, 'unknown')
if (itemType == 'button' or itemType == 'menu' or itemType == 'unknown') then
return t.Value
end
if (itemType == 'checkbox' or itemType == 'confirm') then
return U:Ensure(t.Value, false)
end
if (itemType == 'slider') then
for _, item in pairs(t.Values) do
if (item.Value == t.Value) then
return item.Value
end
end
return nil
end
if (itemType == 'range') then
local rawValue = U:Ensure(t.Value, 0)
if (t.Min > rawValue) then
return t.Min
end
if (t.Max < rawValue) then
return t.Max
end
return rawValue
end
end,
---@return Menu|nil
GetParentMenu = function(t)
return t.__menu or nil
end
}
item.Events.OnEnter = {}
item.Events.OnLeave = {}
item.Events.OnUpdate = {}
item.Events.OnDestroy = {}
local mt = {
__index = function(t, k)
return rawget(t.data, k)
end,
__tostring = function(t)
return t.UUID
end,
__call = function(t, ...)
if (t.Trigger ~= nil and type(t.Trigger) == 'function') then
t:Trigger(t.__event, ...)
end
end,
__newindex = function(t, k, v)
local key = U:Ensure(k, 'unknown')
local oldValue = rawget(t.data, k)
local checkInput = t.Validate ~= nil and type(t.Validate) == 'function'
local inputParser = t.Parser ~= nil and type(t.Parser) == 'function'
local updateIndexTrigger = t.NewIndex ~= nil and type(t.NewIndex) == 'function'
if (checkInput) then
local result = t:Validate(key, v)
result = U:Ensure(result, true)
if (not result) then
return
end
end
if (inputParser) then
local parsedValue = t:Parser(key, v)
v = parsedValue or v
end
rawset(t.data, k, v)
if (updateIndexTrigger) then
t:NewIndex(key, v)
end
if (t.__menu ~= nil and U:Typeof(t.__menu) == 'Menu' and t.__menu.Trigger ~= nil and U:Typeof( t.__menu.Trigger) == 'function') then
t.__menu:Trigger('update', 'UpdateItem', t)
end
if (key == 'Value' and t.Trigger ~= nil and type(t.Trigger) == 'function') then
t:Trigger('update', key, v, oldValue)
end
end,
__metatable = 'MenuV'
}
---@class Item
---@filed private __event string Name of primary event
---@field public UUID string UUID of Item
---@field public Icon string Icon/Emoji for Item
---@field public Label string Label of Item
---@field public Description string Description of Item
---@field public Value any Value of Item
---@field public Values table[] List of values
---@field public Min number Min range value
---@field public Max number Max range value
---@field public Disabled boolean Disabled state of Item
---@field public SaveOnUpdate boolean Save on `update`
---@field private Events table<string, function[]> List of registered `on` events
---@field public Trigger fun(t: Item, event: string)
---@field public On fun(t: Item, event: string, func: function|Menu)
---@field public Validate fun(t: Item, k: string, v:any)
---@field public NewIndex fun(t: Item, k: string, v: any)
---@field public Parser fun(t: Item, k: string, v: any)
---@field public GetValue fun(t: Item):any
---@field public GetParentMenu func(t: Item):Menu|nil
local i = setmetatable({ data = item, __class = 'Item', __type = U:Ensure(info.Type or info.type, 'unknown') }, mt)
for k, v in pairs(info or {}) do
local key = U:Ensure(k, 'unknown')
if (key == 'unknown') then return end
i:On(key, v)
end
return i
end
_ENV.CreateMenuItem = CreateMenuItem
_G.CreateMenuItem = CreateMenuItem
----------------------- [ MenuV ] -----------------------
-- GitHub: https://github.com/ThymonA/menuv/
-- License: GNU General Public License v3.0
-- https://choosealicense.com/licenses/gpl-3.0/
-- Author: Thymon Arens <contact@arens.io>
-- Name: MenuV
-- Version: 1.4.1
-- Description: FiveM menu library for creating menu's
----------------------- [ MenuV ] -----------------------
local assert = assert
---@type Utilities
local U = assert(Utilities)
local type = assert(type)
local next = assert(next)
local pairs = assert(pairs)
local ipairs = assert(ipairs)
local lower = assert(string.lower)
local upper = assert(string.upper)
local sub = assert(string.sub)
local insert = assert(table.insert)
local remove = assert(table.remove)
local pack = assert(table.pack)
local unpack = assert(table.unpack)
local encode = assert(json.encode)
local rawset = assert(rawset)
local rawget = assert(rawget)
local setmetatable = assert(setmetatable)
--- FiveM globals
local GET_CURRENT_RESOURCE_NAME = assert(GetCurrentResourceName)
local GET_INVOKING_RESOURCE = assert(GetInvokingResource)
local HAS_STREAMED_TEXTURE_DICT_LOADED = assert(HasStreamedTextureDictLoaded)
local REQUEST_STREAMED_TEXTURE_DICT = assert(RequestStreamedTextureDict)
--- MenuV local variable
local current_resource = GET_CURRENT_RESOURCE_NAME()
--- Returns default empty table for items
---@returns items
function CreateEmptyItemsTable(data)
data = U:Ensure(data, {})
data.ToTable = function(t)
local tempTable = {}
local index = 0
for _, option in pairs(t) do
index = index + 1
tempTable[index] = {
index = index,
type = option.__type,
uuid = U:Ensure(option.UUID, 'unknown'),
icon = U:Ensure(option.Icon, 'none'),
label = U:Ensure(option.Label, 'Unknown'),
description = U:Ensure(option.Description, ''),
value = 'none',
values = {},
min = U:Ensure(option.Min, 0),
max = U:Ensure(option.Max, 0),
disabled = U:Ensure(option.Disabled, false)
}
if (option.__type == 'button' or option.__type == 'menu') then
tempTable[index].value = 'none'
elseif (option.__type == 'checkbox' or option.__type == 'confirm') then
tempTable[index].value = U:Ensure(option.Value, false)
elseif (option.__type == 'range') then
tempTable[index].value = U:Ensure(option.Value, 0)
if (tempTable[index].value <= tempTable[index].min) then
tempTable[index].value = tempTable[index].min
elseif (tempTable[index].value >= tempTable[index].max) then
tempTable[index].value = tempTable[index].max
end
elseif (option.__type == 'slider') then
tempTable[index].value = 0
end
local _values = U:Ensure(option.Values, {})
local vIndex = 0
for valueIndex, value in pairs(_values) do
vIndex = vIndex + 1
tempTable[index].values[vIndex] = {
label = U:Ensure(value.Label, 'Option'),
description = U:Ensure(value.Description, ''),
value = vIndex
}
if (option.__type == 'slider') then
if (U:Ensure(option.Value, 0) == valueIndex) then
tempTable[index].value = (valueIndex - 1)
end
end
end
end
return tempTable
end
data.ItemToTable = function(t, i)
local tempTable = {}
local index = 0
local uuid = U:Typeof(i) == 'Item' and i.UUID or U:Ensure(i, '00000000-0000-0000-0000-000000000000')
for _, option in pairs(t) do
index = index + 1
if (option.UUID == uuid) then
tempTable = {
index = index,
type = option.__type,
uuid = U:Ensure(option.UUID, 'unknown'),
icon = U:Ensure(option.Icon, 'none'),
label = U:Ensure(option.Label, 'Unknown'),
description = U:Ensure(option.Description, ''),
value = 'none',
values = {},
min = U:Ensure(option.Min, 0),
max = U:Ensure(option.Max, 0),
disabled = U:Ensure(option.Disabled, false)
}
if (option.__type == 'button' or option.__type == 'menu') then
tempTable.value = 'none'
elseif (option.__type == 'checkbox' or option.__type == 'confirm') then
tempTable.value = U:Ensure(option.Value, false)
elseif (option.__type == 'range') then
tempTable.value = U:Ensure(option.Value, 0)
if (tempTable.value <= tempTable.min) then
tempTable.value = tempTable.min
elseif (tempTable.value >= tempTable.max) then
tempTable.value = tempTable.max
end
elseif (option.__type == 'slider') then
tempTable.value = 0
end
local _values = U:Ensure(option.Values, {})
local vIndex = 0
for valueIndex, value in pairs(_values) do
vIndex = vIndex + 1
tempTable.values[vIndex] = {
label = U:Ensure(value.Label, 'Option'),
description = U:Ensure(value.Description, ''),
value = vIndex
}
if (option.__type == 'slider') then
if (U:Ensure(option.Value, 0) == valueIndex) then
tempTable.value = (valueIndex - 1)
end
end
end
return tempTable
end
end
return tempTable
end
data.AddItem = function(t, item)
if (U:Typeof(item) == 'Item') then
local newIndex = #(U:Ensure(rawget(t, 'data'), {})) + 1
rawset(t.data, newIndex, item)
if (t.Trigger ~= nil and type(t.Trigger) == 'function') then
t:Trigger('update', 'AddItem', item)
end
end
return U:Ensure(rawget(t, 'data'), {})
end
local item_pairs = function(t, k)
local _k, _v = next((rawget(t, 'data') or {}), k)
if (_v ~= nil and type(_v) ~= 'table') then
return item_pairs(t, _k)
end
return _k, _v
end
local item_ipairs = function(t, k)
local _k, _v = next((rawget(t, 'data') or {}), k)
if (_v ~= nil and (type(_v) ~= 'table' or type(_k) ~= 'number')) then
return item_ipairs(t, _k)
end
return _k, _v
end
_G.item_pairs = item_pairs
_ENV.item_pairs = item_pairs
---@class items
return setmetatable({ data = data, Trigger = nil }, {
__index = function(t, k)
return rawget(t.data, k)
end,
__newindex = function(t, k, v)
local oldValue = rawget(t.data, k)
rawset(t.data, k, v)
if (t.Trigger ~= nil and type(t.Trigger) == 'function') then
if (oldValue == nil) then
t:Trigger('update', 'AddItem', v)
elseif (oldValue ~= nil and v == nil) then
t:Trigger('update', 'RemoveItem', oldValue)
elseif (oldValue ~= v) then
t:Trigger('update', 'UpdateItem', v, oldValue)
end
end
end,
__call = function(t, func)