-
Notifications
You must be signed in to change notification settings - Fork 4
/
warmenu.lua
497 lines (370 loc) · 14.9 KB
/
warmenu.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
WarMenu = { }
WarMenu.debug = false
local menus = { }
local keys = { up = 0x911CB09E, down = 0x4403F97F, left = 0xAD7FCC5B, right = 0x65F9EC5B, select = 0xC7B5340A, back = 0x308588E6 }
local optionCount = 0
local currentKey = nil
local currentMenu = nil
local titleHeight = 0.11
local titleYOffset = 0.03
local titleScale = 1.0
local buttonHeight = 0.038
local buttonFont = 1
local buttonScale = 0.365
local buttonTextXOffset = 0.005
local buttonTextYOffset = 0.005
local function debugPrint(text)
if WarMenu.debug then
Citizen.Trace('[WarMenu] '..tostring(text))
end
end
local function setMenuProperty(id, property, value)
if id and menus[id] then
menus[id][property] = value
debugPrint(id..' menu property changed: { '..tostring(property)..', '..tostring(value)..' }')
end
end
local function isMenuVisible(id)
if id and menus[id] then
return menus[id].visible
else
return false
end
end
local function setMenuVisible(id, visible, holdCurrent)
if id and menus[id] then
setMenuProperty(id, 'visible', visible)
if not holdCurrent and menus[id] then
setMenuProperty(id, 'currentOption', 1)
end
if visible then
if id ~= currentMenu and isMenuVisible(currentMenu) then
setMenuVisible(currentMenu, false)
end
currentMenu = id
end
end
end
local function drawText(text, x, y, font, color, scale, center, shadow, alignRight)
local str = CreateVarString(10, "LITERAL_STRING", text)
if color then
SetTextColor(color.r, color.g, color.b, color.a)
else
SetTextColor(255, 255, 255, 255)
end
SetTextFontForCurrentCommand(font)
SetTextScale(scale, scale)
if shadow then
SetTextDropshadow(1, 0, 0, 0, 255)
end
if center then
SetTextCentre(center)
elseif alignRight then
--DisplayText(menus[currentMenu].x, menus[currentMenu].x + menus[currentMenu].width - buttonTextXOffset)
end
DisplayText(str, x, y)
end
local function drawRect(x, y, width, height, color)
DrawRect(x, y, width, height, color.r, color.g, color.b, color.a)
end
local function drawTitle()
if menus[currentMenu] then
local x = menus[currentMenu].x + menus[currentMenu].width / 2
local y = menus[currentMenu].y + titleHeight / 2
HasStreamedTextureDictLoaded("menu_textures")
DrawSprite("menu_textures", "translate_bg_1a", 0.20, 0.2, 0.35, 0.35, 0.8, 000, 2, 2, 200, 1)
DrawSprite("menu_textures", "translate_bg_1a", 0.20, 0.1, 0.30, 0.1, 0.1, 100, 1, 1, 150, 0)
drawText(menus[currentMenu].title, x, y - titleHeight / 2 + titleYOffset, menus[currentMenu].titleFont, menus[currentMenu].titleColor, titleScale, true)
end
end
local function drawSubTitle()
if menus[currentMenu] then
local x = menus[currentMenu].x + menus[currentMenu].width / 2
local y = menus[currentMenu].y + titleHeight + buttonHeight / 2
local subTitleColor = { r = menus[currentMenu].titleBackgroundColor.r, g = menus[currentMenu].titleBackgroundColor.g, b = menus[currentMenu].titleBackgroundColor.b, a = 255 }
drawRect(x, y, menus[currentMenu].width, buttonHeight, menus[currentMenu].subTitleBackgroundColor)
drawText(menus[currentMenu].subTitle, menus[currentMenu].x + buttonTextXOffset, y - buttonHeight / 2 + buttonTextYOffset, buttonFont, subTitleColor, buttonScale, false)
if optionCount > menus[currentMenu].maxOptionCount then
drawText(tostring(menus[currentMenu].currentOption)..' / '..tostring(optionCount), menus[currentMenu].x-0.05 + menus[currentMenu].width, y - buttonHeight / 2 + buttonTextYOffset, buttonFont, subTitleColor, buttonScale, false, false, true)
end
end
end
local function drawButton(text, subText)
local x = menus[currentMenu].x + menus[currentMenu].width / 2
local multiplier = nil
if menus[currentMenu].currentOption <= menus[currentMenu].maxOptionCount and optionCount <= menus[currentMenu].maxOptionCount then
multiplier = optionCount
elseif optionCount > menus[currentMenu].currentOption - menus[currentMenu].maxOptionCount and optionCount <= menus[currentMenu].currentOption then
multiplier = optionCount - (menus[currentMenu].currentOption - menus[currentMenu].maxOptionCount)
end
if multiplier then
local y = menus[currentMenu].y + titleHeight + buttonHeight + (buttonHeight * multiplier) - buttonHeight / 2
local backgroundColor = nil
local textColor = nil
local subTextColor = nil
local shadow = false
if menus[currentMenu].currentOption == optionCount then
backgroundColor = menus[currentMenu].menuFocusBackgroundColor
textColor = menus[currentMenu].menuFocusTextColor
subTextColor = menus[currentMenu].menuFocusTextColor
else
backgroundColor = menus[currentMenu].menuBackgroundColor
textColor = menus[currentMenu].menuTextColor
subTextColor = menus[currentMenu].menuSubTextColor
shadow = true
end
drawRect(x, y, menus[currentMenu].width, buttonHeight, backgroundColor)
drawText(text, menus[currentMenu].x + buttonTextXOffset, y - (buttonHeight / 2) + buttonTextYOffset, buttonFont, textColor, buttonScale, false, shadow)
if subText then
drawText(subText, menus[currentMenu].x + buttonTextXOffset, y - buttonHeight / 2 + buttonTextYOffset, buttonFont, subTextColor, buttonScale, false, shadow, true)
end
end
end
function WarMenu.CreateMenu(id, title)
-- Default settings
menus[id] = { }
menus[id].title = title
menus[id].subTitle = 'Boat Rentals'
menus[id].visible = false
menus[id].previousMenu = nil
menus[id].aboutToBeClosed = false
menus[id].x = 0.070
menus[id].y = 0.04
menus[id].width = 0.27
menus[id].currentOption = 1
menus[id].maxOptionCount = 4
menus[id].titleFont = 1
menus[id].titleColor = { r = 255, g = 255, b = 255, a = 255 }
menus[id].titleBackgroundColor = { r = 255, g = 255, b = 255, a = 000 }
menus[id].titleBackgroundSprite = nil
menus[id].menuTextColor = { r = 255, g = 255, b = 255, a = 255 }
menus[id].menuSubTextColor = { r = 255, g = 255, b = 255, a = 255 }
menus[id].menuFocusTextColor = { r = 0, g = 0, b = 0, a = 255 }
menus[id].menuFocusBackgroundColor = { r = 255, g = 255, b = 255, a = 255 }
menus[id].menuBackgroundColor = { r = 0, g = 0, b = 0, a = 000 }
menus[id].subTitleBackgroundColor = { r = menus[id].menuBackgroundColor.r, g = menus[id].menuBackgroundColor.g, b = menus[id].menuBackgroundColor.b, a = 255 }
menus[id].buttonPressedSound = { name = "SELECT", set = "HUD_FRONTEND_DEFAULT_SOUNDSET" } --https://pastebin.com/0neZdsZ5
debugPrint(tostring(id)..' menu created')
end
function WarMenu.CreateSubMenu(id, parent, subTitle)
if menus[parent] then
WarMenu.CreateMenu(id, menus[parent].title)
if subTitle then
setMenuProperty(id, 'subTitle', string.upper(subTitle))
else
setMenuProperty(id, 'subTitle', string.upper(menus[parent].subTitle))
end
setMenuProperty(id, 'previousMenu', parent)
setMenuProperty(id, 'x', menus[parent].x)
setMenuProperty(id, 'y', menus[parent].y)
setMenuProperty(id, 'maxOptionCount', menus[parent].maxOptionCount)
setMenuProperty(id, 'titleFont', menus[parent].titleFont)
setMenuProperty(id, 'titleColor', menus[parent].titleColor)
setMenuProperty(id, 'titleBackgroundColor', menus[parent].titleBackgroundColor)
setMenuProperty(id, 'titleBackgroundSprite', menus[parent].titleBackgroundSprite)
setMenuProperty(id, 'menuTextColor', menus[parent].menuTextColor)
setMenuProperty(id, 'menuSubTextColor', menus[parent].menuSubTextColor)
setMenuProperty(id, 'menuFocusTextColor', menus[parent].menuFocusTextColor)
setMenuProperty(id, 'menuFocusBackgroundColor', menus[parent].menuFocusBackgroundColor)
setMenuProperty(id, 'menuBackgroundColor', menus[parent].menuBackgroundColor)
setMenuProperty(id, 'subTitleBackgroundColor', menus[parent].subTitleBackgroundColor)
else
debugPrint('Failed to create '..tostring(id)..' submenu: '..tostring(parent)..' parent menu doesn\'t exist')
end
end
function WarMenu.CurrentMenu()
return currentMenu
end
function WarMenu.OpenMenu(id)
if id and menus[id] then
PlaySoundFrontend(-1, "SELECT", "HUD_FRONTEND_DEFAULT_SOUNDSET", true)
setMenuVisible(id, true)
debugPrint(tostring(id)..' menu opened')
else
debugPrint('Failed to open '..tostring(id)..' menu: it doesn\'t exist')
end
end
function WarMenu.IsMenuOpened(id)
return isMenuVisible(id)
end
function WarMenu.IsAnyMenuOpened()
for id, _ in pairs(menus) do
if isMenuVisible(id) then return true end
end
return false
end
function WarMenu.IsMenuAboutToBeClosed()
if menus[currentMenu] then
return menus[currentMenu].aboutToBeClosed
else
return false
end
end
function WarMenu.CloseMenu()
if menus[currentMenu] then
if menus[currentMenu].aboutToBeClosed then
menus[currentMenu].aboutToBeClosed = false
setMenuVisible(currentMenu, false)
debugPrint(tostring(currentMenu)..' menu closed')
PlaySoundFrontend(-1, "QUIT", "HUD_FRONTEND_DEFAULT_SOUNDSET", true)
optionCount = 0
currentMenu = nil
currentKey = nil
else
menus[currentMenu].aboutToBeClosed = true
debugPrint(tostring(currentMenu)..' menu about to be closed')
end
end
end
function WarMenu.Button(text, subText)
local buttonText = text
if subText then
buttonText = '{ '..tostring(buttonText)..', '..tostring(subText)..' }'
end
if menus[currentMenu] then
optionCount = optionCount + 1
local isCurrent = menus[currentMenu].currentOption == optionCount
drawButton(text, subText)
if isCurrent then
if currentKey == keys.select then
PlaySoundFrontend(-1, menus[currentMenu].buttonPressedSound.name, menus[currentMenu].buttonPressedSound.set, true)
debugPrint(buttonText..' button pressed')
return true
elseif currentKey == keys.left or currentKey == keys.right then
PlaySoundFrontend(-1, "NAV_UP_DOWN", "HUD_FRONTEND_DEFAULT_SOUNDSET", true)
end
end
return false
else
debugPrint('Failed to create '..buttonText..' button: '..tostring(currentMenu)..' menu doesn\'t exist')
return false
end
end
function WarMenu.MenuButton(text, id)
if menus[id] then
if WarMenu.Button(text) then
setMenuVisible(currentMenu, false)
setMenuVisible(id, true, true)
return true
end
else
debugPrint('Failed to create '..tostring(text)..' menu button: '..tostring(id)..' submenu doesn\'t exist')
end
return false
end
function WarMenu.CheckBox(text, checked, callback)
if WarMenu.Button(text, checked and 'On' or 'Off') then
checked = not checked
debugPrint(tostring(text)..' checkbox changed to '..tostring(checked))
if callback then callback(checked) end
return true
end
return false
end
function WarMenu.ComboBox(text, items, currentIndex, selectedIndex, callback)
local itemsCount = #items
local selectedItem = items[currentIndex]
local isCurrent = menus[currentMenu].currentOption == (optionCount + 1)
if itemsCount > 1 and isCurrent then
selectedItem = '← '..tostring(selectedItem)..' →'
end
if WarMenu.Button(text, selectedItem) then
selectedIndex = currentIndex
callback(currentIndex, selectedIndex)
return true
elseif isCurrent then
if currentKey == keys.left then
if currentIndex > 1 then currentIndex = currentIndex - 1 else currentIndex = itemsCount end
elseif currentKey == keys.right then
if currentIndex < itemsCount then currentIndex = currentIndex + 1 else currentIndex = 1 end
end
else
currentIndex = selectedIndex
end
callback(currentIndex, selectedIndex)
return false
end
function WarMenu.Display()
if isMenuVisible(currentMenu) then
if menus[currentMenu].aboutToBeClosed then
WarMenu.CloseMenu()
else
ClearAllHelpMessages()
drawTitle()
drawSubTitle()
currentKey = nil
if IsControlJustReleased(1, keys.down) then
PlaySoundFrontend(-1, "NAV_UP_DOWN", "HUD_FRONTEND_DEFAULT_SOUNDSET", true)
if menus[currentMenu].currentOption < optionCount then
menus[currentMenu].currentOption = menus[currentMenu].currentOption + 1
else
menus[currentMenu].currentOption = 1
end
elseif IsControlJustReleased(1, keys.up) then
PlaySoundFrontend(-1, "NAV_UP_DOWN", "HUD_FRONTEND_DEFAULT_SOUNDSET", true)
if menus[currentMenu].currentOption > 1 then
menus[currentMenu].currentOption = menus[currentMenu].currentOption - 1
else
menus[currentMenu].currentOption = optionCount
end
elseif IsControlJustReleased(1, keys.left) then
currentKey = keys.left
elseif IsControlJustReleased(1, keys.right) then
currentKey = keys.right
elseif IsControlJustReleased(1, keys.select) then
currentKey = keys.select
elseif IsControlJustReleased(1, keys.back) then
if menus[menus[currentMenu].previousMenu] then
PlaySoundFrontend(-1, "BACK", "HUD_FRONTEND_DEFAULT_SOUNDSET", true)
setMenuVisible(menus[currentMenu].previousMenu, true)
else
WarMenu.CloseMenu()
end
end
optionCount = 0
end
end
end
function WarMenu.SetMenuWidth(id, width)
setMenuProperty(id, 'width', width)
end
function WarMenu.SetMenuX(id, x)
setMenuProperty(id, 'x', x)
end
function WarMenu.SetMenuY(id, y)
setMenuProperty(id, 'y', y)
end
function WarMenu.SetMenuMaxOptionCountOnScreen(id, count)
setMenuProperty(id, 'maxOptionCount', count)
end
function WarMenu.SetTitle(id, title)
setMenuProperty(id, 'title', title)
end
function WarMenu.SetTitleColor(id, r, g, b, a)
setMenuProperty(id, 'titleColor', { ['r'] = r, ['g'] = g, ['b'] = b, ['a'] = a or menus[id].titleColor.a })
end
function WarMenu.SetTitleBackgroundColor(id, r, g, b, a)
setMenuProperty(id, 'titleBackgroundColor', { ['r'] = r, ['g'] = g, ['b'] = b, ['a'] = a or menus[id].titleBackgroundColor.a })
end
function WarMenu.SetTitleBackgroundSprite(id, textureDict, textureName)
RequestStreamedTextureDict(textureDict)
setMenuProperty(id, 'titleBackgroundSprite', { dict = textureDict, name = textureName })
end
function WarMenu.SetSubTitle(id, text)
setMenuProperty(id, 'subTitle', string.upper(text))
end
function WarMenu.SetMenuBackgroundColor(id, r, g, b, a)
setMenuProperty(id, 'menuBackgroundColor', { ['r'] = r, ['g'] = g, ['b'] = b, ['a'] = a or menus[id].menuBackgroundColor.a })
end
function WarMenu.SetMenuTextColor(id, r, g, b, a)
setMenuProperty(id, 'menuTextColor', { ['r'] = r, ['g'] = g, ['b'] = b, ['a'] = a or menus[id].menuTextColor.a })
end
function WarMenu.SetMenuSubTextColor(id, r, g, b, a)
setMenuProperty(id, 'menuSubTextColor', { ['r'] = r, ['g'] = g, ['b'] = b, ['a'] = a or menus[id].menuSubTextColor.a })
end
function WarMenu.SetMenuFocusColor(id, r, g, b, a)
setMenuProperty(id, 'menuFocusColor', { ['r'] = r, ['g'] = g, ['b'] = b, ['a'] = a or menus[id].menuFocusColor.a })
end
function WarMenu.SetMenuButtonPressedSound(id, name, set)
setMenuProperty(id, 'buttonPressedSound', { ['name'] = name, ['set'] = set })
end