Merge pull request #601 from AstroNext/AstroNext

refactor(esx_menu_default/client/main.lua): Change isControlePress to ESX.RegisterInput
This commit is contained in:
Gellipapa
2023-01-09 22:08:00 +01:00
committed by GitHub
+28 -16
View File
@@ -1,7 +1,8 @@
local GUI, MenuType, OpenedMenus = {}, 'default', 0 local GUI, MenuType, OpenedMenus, CurrentNameSpace = {}, 'default', 0, nil
GUI.Time = 0 GUI.Time = 0
local function openMenu(namespace, name, data) local function openMenu(namespace, name, data)
CurrentNameSpace = namespace
OpenedMenus += 1 OpenedMenus += 1
SendNUIMessage({ SendNUIMessage({
action = 'openMenu', action = 'openMenu',
@@ -12,6 +13,7 @@ local function openMenu(namespace, name, data)
end end
local function closeMenu(namespace, name) local function closeMenu(namespace, name)
CurrentNameSpace = namespace
OpenedMenus -= 1 OpenedMenus -= 1
SendNUIMessage({ SendNUIMessage({
action = 'closeMenu', action = 'closeMenu',
@@ -20,6 +22,14 @@ local function closeMenu(namespace, name)
}) })
end end
AddEventHandler('onResourceStop', function(resource)
if GetCurrentResourceName() == resource and OpenedMenus > 0 then
ESX.UI.Menu.CloseAll()
elseif CurrentNameSpace ~= nil and CurrentNameSpace == resource and OpenedMenus > 0 then
ESX.UI.Menu.CloseAll()
end
end)
ESX.UI.Menu.RegisterType(MenuType, openMenu, closeMenu) ESX.UI.Menu.RegisterType(MenuType, openMenu, closeMenu)
RegisterNUICallback('menu_submit', function(data, cb) RegisterNUICallback('menu_submit', function(data, cb)
@@ -58,60 +68,62 @@ RegisterNUICallback('menu_change', function(data, cb)
cb('OK') cb('OK')
end) end)
CreateThread(function() ESX.RegisterInput('menu_default_enter', 'Submit menu item', 'keyboard', 'RETURN', function()
while true do if OpenedMenus > 0 and (GetGameTimer() - GUI.Time) > 200 then
local Sleep = 500
if OpenedMenus > 0 then
Sleep = 10
if IsControlPressed(0, 18) and IsUsingKeyboard(0) and (GetGameTimer() - GUI.Time) > 200 then
SendNUIMessage({ SendNUIMessage({
action = 'controlPressed', action = 'controlPressed',
control = 'ENTER' control = 'ENTER'
}) })
GUI.Time = GetGameTimer() GUI.Time = GetGameTimer()
end end
end)
if IsControlPressed(0, 177) and IsUsingKeyboard(0) and (GetGameTimer() - GUI.Time) > 200 then ESX.RegisterInput('menu_default_backspace', 'Close menu', 'keyboard', 'BACK', function()
if OpenedMenus > 0 then
SendNUIMessage({ SendNUIMessage({
action = 'controlPressed', action = 'controlPressed',
control = 'BACKSPACE' control = 'BACKSPACE'
}) })
GUI.Time = GetGameTimer() GUI.Time = GetGameTimer()
end end
end)
if IsControlPressed(0, 27) and IsUsingKeyboard(0) and (GetGameTimer() - GUI.Time) > 200 then ESX.RegisterInput('menu_default_top', 'Change menu focus to top item', 'keyboard', 'UP', function()
if OpenedMenus > 0 then
SendNUIMessage({ SendNUIMessage({
action = 'controlPressed', action = 'controlPressed',
control = 'TOP' control = 'TOP'
}) })
GUI.Time = GetGameTimer() GUI.Time = GetGameTimer()
end end
end)
if IsControlPressed(0, 173) and IsUsingKeyboard(0) and (GetGameTimer() - GUI.Time) > 200 then ESX.RegisterInput('menu_default_down', 'Change menu focus to down item', 'keyboard', 'DOWN', function()
if OpenedMenus > 0 then
SendNUIMessage({ SendNUIMessage({
action = 'controlPressed', action = 'controlPressed',
control = 'DOWN' control = 'DOWN'
}) })
GUI.Time = GetGameTimer() GUI.Time = GetGameTimer()
end end
end)
if IsControlPressed(0, 174) and IsUsingKeyboard(0) and (GetGameTimer() - GUI.Time) > 200 then ESX.RegisterInput('menu_default_left', 'Change menu slider to left', 'keyboard', 'LEFT', function()
if OpenedMenus > 0 then
SendNUIMessage({ SendNUIMessage({
action = 'controlPressed', action = 'controlPressed',
control = 'LEFT' control = 'LEFT'
}) })
GUI.Time = GetGameTimer() GUI.Time = GetGameTimer()
end end
end)
if IsControlPressed(0, 175) and IsUsingKeyboard(0) and (GetGameTimer() - GUI.Time) > 200 then ESX.RegisterInput('menu_default_right', 'Change menu slider to right', 'keyboard', 'RIGHT', function()
if OpenedMenus > 0 then
SendNUIMessage({ SendNUIMessage({
action = 'controlPressed', action = 'controlPressed',
control = 'RIGHT' control = 'RIGHT'
}) })
GUI.Time = GetGameTimer() GUI.Time = GetGameTimer()
end end
end
Wait(Sleep)
end
end) end)