@@ -1366,54 +1366,61 @@ end
1366
1366
-- Menus
1367
1367
1368
1368
local function updateMenuSize (menu )
1369
+ if not menu then return end
1370
+
1369
1371
menu .width = (# menu .items [1 ] > 0 and (menu .leftColumnWidth + menu .rightColumnWidth ) or (menu .leftColumnWidth )) + 2 * MENU_SIDE_PADDING
1370
1372
menu .height = MENU_ITEM_HEIGHT * math.max (# menu .items [0 ], # menu .items [1 ]) + MENU_TOP_PADDING + MENU_BOTTOM_PADDING
1371
1373
end
1372
1374
1373
- function AddMenuItem (id , column , caption )
1374
- local menu = g_Menus [id ]
1375
+ function AddMenuItem (menuID , column , caption )
1376
+ local menu = g_Menus [menuID ]
1377
+ if not menu then return end
1378
+
1375
1379
table.insert (menu .items [column ], caption )
1376
1380
updateMenuSize (menu )
1377
1381
end
1378
1382
1379
- function CreateMenu (id , menu )
1383
+ function CreateMenu (menuID , menu )
1384
+ if not menu then return end
1385
+
1380
1386
menu .x = math.floor (menu .x * screenWidth / 640 )
1381
1387
menu .y = math.floor (menu .y * screenHeight / 480 )
1382
1388
menu .leftColumnWidth = math.floor (menu .leftColumnWidth * screenWidth / 640 )
1383
- menu .rightColumnWidth = math.floor (menu .rightColumnWidth * screenWidth / 480 )
1384
- local id = 1
1385
- while g_TextDraws [' m' .. id ] do
1386
- id = id + 1
1387
- end
1388
- menu .titletextdraw = { text = menu .title , id = ' m' .. id , x = menu .x + MENU_SIDE_PADDING , y = menu .y - 0.5 * MENU_ITEM_HEIGHT , align = 1 , font = 2 }
1389
- initTextDraw (menu .titletextdraw )
1390
- hideTextDraw (menu .titletextdraw )
1389
+ menu .rightColumnWidth = math.floor (menu .rightColumnWidth * screenWidth / 640 )
1390
+
1391
+ g_Menus [menuID ] = menu
1391
1392
updateMenuSize (menu )
1392
- g_Menus [id ] = menu
1393
1393
end
1394
1394
1395
1395
function DisableMenuRow (menuID , rowID )
1396
1396
local menu = g_Menus [menuID ]
1397
+ if not menu then return end
1398
+
1397
1399
menu .disabledrows = menu .disabledrows or {}
1398
1400
table.insert (menu .disabledrows , rowID )
1399
1401
end
1400
1402
1401
1403
function SetMenuColumnHeader (menuID , column , text )
1402
- g_Menus [menuID ].items [column ][13 ] = text
1404
+ local menu = g_Menus [menuID ]
1405
+ if not menu then return end
1406
+
1407
+ menu .items [column ][13 ] = text
1403
1408
end
1404
1409
1405
1410
function ShowMenuForPlayer (menuID )
1411
+ if not g_Menus [menuID ] then return end
1412
+
1406
1413
if g_CurrentMenu and g_CurrentMenu .anim then
1407
1414
g_CurrentMenu .anim :remove ()
1408
1415
g_CurrentMenu .anim = nil
1409
1416
end
1410
1417
1411
1418
local prevMenu = g_CurrentMenu
1412
1419
g_CurrentMenu = g_Menus [menuID ]
1413
- local closebtnSide = screenWidth * (30 / 1024 )
1420
+ local closebtnSide = screenWidth * (27 / 1024 )
1421
+
1414
1422
if not prevMenu then
1415
1423
g_CurrentMenu .alpha = 0
1416
- g_CurrentMenu .titletextdraw .alpha = 0
1417
1424
1418
1425
g_CurrentMenu .closebtn = guiCreateStaticImage (g_CurrentMenu .x + g_CurrentMenu .width - closebtnSide , g_CurrentMenu .y , closebtnSide , closebtnSide , ' client/closebtn.png' , false , nil )
1419
1426
guiSetAlpha (g_CurrentMenu .closebtn , 0 )
@@ -1450,25 +1457,25 @@ function ShowMenuForPlayer(menuID)
1450
1457
{ time = 500 , from = 0 , to = 1 , fn = setMenuAlpha },
1451
1458
function ()
1452
1459
setMenuAlpha (g_CurrentMenu , 1 )
1453
- g_CurrentMenu .titletextdraw .alpha = nil
1454
- g_CurrentMenu .anim = nil
1460
+ if g_CurrentMenu then
1461
+ g_CurrentMenu .anim = nil
1462
+ end
1455
1463
end
1456
1464
)
1457
1465
1458
1466
addEventHandler (' onClientRender' , root , renderMenu )
1459
1467
addEventHandler (' onClientClick' , root , menuClickHandler )
1460
1468
showCursor (true )
1461
1469
else
1462
- hideTextDraw (prevMenu .titletextdraw )
1463
1470
g_CurrentMenu .closebtn = prevMenu .closebtn
1464
1471
prevMenu .closebtn = nil
1465
1472
guiSetPosition (g_CurrentMenu .closebtn , g_CurrentMenu .x + g_CurrentMenu .width - closebtnSide , g_CurrentMenu .y , false )
1466
1473
g_CurrentMenu .closebtnhover = prevMenu .closebtnhover
1467
1474
prevMenu .closebtnhover = nil
1468
1475
guiSetPosition (g_CurrentMenu .closebtnhover , g_CurrentMenu .x + g_CurrentMenu .width - closebtnSide , g_CurrentMenu .y , false )
1469
- g_CurrentMenu . alpha = 1
1476
+ setMenuAlpha ( g_CurrentMenu , 1 )
1470
1477
end
1471
- showTextDraw ( g_CurrentMenu . titletextdraw )
1478
+
1472
1479
bindKey (' enter' , ' down' , OnKeyPress )
1473
1480
end
1474
1481
@@ -1478,29 +1485,39 @@ function HideMenuForPlayer(menuID)
1478
1485
g_CurrentMenu .anim :remove ()
1479
1486
g_CurrentMenu .anim = nil
1480
1487
end
1481
- g_CurrentMenu .anim = Animation .createAndPlay (g_CurrentMenu , { time = 500 , from = 1 , to = 0 , fn = setMenuAlpha }, exitMenu )
1488
+ if menuID then
1489
+ g_CurrentMenu .anim = Animation .createAndPlay (g_CurrentMenu , { time = 500 , from = 1 , to = 0 , fn = setMenuAlpha }, closeMenu )
1490
+ else
1491
+ g_CurrentMenu .anim = Animation .createAndPlay (g_CurrentMenu , { time = 500 , from = 1 , to = 0 , fn = setMenuAlpha }, exitMenu )
1492
+ end
1482
1493
end
1483
1494
end
1484
1495
1485
1496
function DestroyMenu (menuID )
1486
- destroyTextDraw (g_Menus [menuID ].titletextdraw )
1497
+ if not g_Menus [menuID ] then return end
1498
+
1487
1499
if g_CurrentMenu and menuID == g_CurrentMenu .id then
1488
- exitMenu ()
1500
+ closeMenu ()
1489
1501
end
1502
+
1490
1503
g_Menus [menuID ] = nil
1491
1504
end
1492
1505
1493
1506
function setMenuAlpha (menu , alpha )
1507
+ if not menu then return end
1508
+
1494
1509
menu .alpha = alpha
1495
- menu .titletextdraw .alpha = alpha
1496
- guiSetAlpha (menu .closebtn , .75 * alpha )
1497
- guiSetAlpha (menu .closebtnhover , .75 * alpha )
1510
+
1511
+ if menu .closebtn then
1512
+ guiSetAlpha (menu .closebtn , .75 * alpha )
1513
+ end
1514
+ if menu .closebtnhover then
1515
+ guiSetAlpha (menu .closebtnhover , .75 * alpha )
1516
+ end
1498
1517
end
1499
1518
1500
1519
function closeMenu ()
1501
1520
removeEventHandler (' onClientRender' , root , renderMenu )
1502
- hideTextDraw (g_CurrentMenu .titletextdraw )
1503
- g_CurrentMenu .titletextdraw .alpha = nil
1504
1521
removeEventHandler (' onClientClick' , root , menuClickHandler )
1505
1522
g_CurrentMenu .anim = nil
1506
1523
destroyElement (g_CurrentMenu .closebtn )
@@ -1526,7 +1543,19 @@ function renderMenu()
1526
1543
-- background
1527
1544
dxDrawRectangle (menu .x , menu .y , menu .width , menu .height , tocolor (0 , 0 , 0 , 128 * menu .alpha ))
1528
1545
1546
+ if menu .title then
1547
+ local titleX = menu .x + MENU_SIDE_PADDING
1548
+ local titleY = menu .y + MENU_ITEM_HEIGHT - MENU_BOTTOM_PADDING
1549
+ dxDrawText (menu .title , titleX , titleY , titleX , titleY , tocolor (255 , 255 , 255 , 255 * menu .alpha ), 0.9 , ' bankgothic' , ' left' , ' center' , false , false , false , true )
1550
+ end
1551
+
1529
1552
local cursorX , cursorY = getCursorPosition ()
1553
+
1554
+ if not cursorX then
1555
+ closeMenu ()
1556
+ return
1557
+ end
1558
+
1530
1559
cursorY = screenHeight * cursorY
1531
1560
-- selected row
1532
1561
local selectedRow
@@ -1539,8 +1568,7 @@ function renderMenu()
1539
1568
for column = 0 , 1 do
1540
1569
for i , text in pairs (menu .items [column ]) do
1541
1570
local x = menu .x + MENU_SIDE_PADDING + column * menu .leftColumnWidth
1542
- local y
1543
- local color , scale
1571
+ local y , color , scale
1544
1572
if i < 13 then
1545
1573
-- regular item
1546
1574
y = menu .y + MENU_TOP_PADDING + (i - 1 ) * MENU_ITEM_HEIGHT
@@ -1565,18 +1593,21 @@ function menuClickHandler(button, state, clickX, clickY)
1565
1593
if state ~= ' up' then
1566
1594
return
1567
1595
end
1596
+
1568
1597
if not g_CurrentMenu then
1569
1598
return
1570
1599
end
1600
+
1571
1601
local cursorX , cursorY = getCursorPosition ()
1572
1602
cursorY = screenHeight * cursorY
1573
1603
if cursorY < g_CurrentMenu .y + MENU_TOP_PADDING or cursorY > g_CurrentMenu .y + MENU_TOP_PADDING + math.max (# g_CurrentMenu .items [0 ], # g_CurrentMenu .items [1 ]) * MENU_ITEM_HEIGHT then
1574
1604
return
1575
1605
end
1606
+
1576
1607
local selectedRow = math.floor ((clickY - g_CurrentMenu .y - MENU_TOP_PADDING ) / MENU_ITEM_HEIGHT )
1577
1608
if not (g_CurrentMenu .disabledrows and table .find (g_CurrentMenu .disabledrows , selectedRow )) then
1578
1609
serverAMXEvent (' OnPlayerSelectedMenuRow' , g_PlayerID , selectedRow )
1579
- exitMenu ()
1610
+ closeMenu ()
1580
1611
end
1581
1612
end
1582
1613
0 commit comments