mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-09-04 08:13:21 +00:00
refactor(menu): minor optimisation tweaks
It's not much since that would require a rewrite, which is not happening.
This commit is contained in:
@@ -1,97 +1,95 @@
|
||||
Citizen.CreateThread(function()
|
||||
local ESX = exports['es_extended']:getSharedObject()
|
||||
local GUI, MenuType = {}, 'default'
|
||||
GUI.Time = 0
|
||||
local ESX = exports.es_extended:getSharedObject()
|
||||
local GUI, MenuType = {}, 'default'
|
||||
GUI.Time = 0
|
||||
|
||||
local openMenu = function(namespace, name, data)
|
||||
SendNUIMessage({
|
||||
action = 'openMenu',
|
||||
namespace = namespace,
|
||||
name = name,
|
||||
data = data
|
||||
})
|
||||
local openMenu = function(namespace, name, data)
|
||||
SendNUIMessage({
|
||||
action = 'openMenu',
|
||||
namespace = namespace,
|
||||
name = name,
|
||||
data = data
|
||||
})
|
||||
end
|
||||
|
||||
local closeMenu = function(namespace, name)
|
||||
SendNUIMessage({
|
||||
action = 'closeMenu',
|
||||
namespace = namespace,
|
||||
name = name,
|
||||
data = data
|
||||
})
|
||||
end
|
||||
|
||||
ESX.UI.Menu.RegisterType(MenuType, openMenu, closeMenu)
|
||||
|
||||
RegisterNUICallback('menu_submit', function(data, cb)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
if menu.submit ~= nil then
|
||||
menu.submit(data, menu)
|
||||
end
|
||||
cb('OK')
|
||||
end)
|
||||
|
||||
RegisterNUICallback('menu_cancel', function(data, cb)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
|
||||
if menu.cancel ~= nil then
|
||||
menu.cancel(data, menu)
|
||||
end
|
||||
cb('OK')
|
||||
end)
|
||||
|
||||
RegisterNUICallback('menu_change', function(data, cb)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
|
||||
for i=1, #data.elements, 1 do
|
||||
menu.setElement(i, 'value', data.elements[i].value)
|
||||
|
||||
if data.elements[i].selected then
|
||||
menu.setElement(i, 'selected', true)
|
||||
else
|
||||
menu.setElement(i, 'selected', false)
|
||||
end
|
||||
end
|
||||
|
||||
if menu.change ~= nil then
|
||||
menu.change(data, menu)
|
||||
end
|
||||
cb('OK')
|
||||
end)
|
||||
|
||||
Citizen.CreateThread(function()
|
||||
while true do
|
||||
Citizen.Wait(15)
|
||||
|
||||
if IsControlPressed(0, 18) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 150 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'ENTER'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
|
||||
if IsControlPressed(0, 177) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 150 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'BACKSPACE'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
|
||||
if IsControlPressed(0, 27) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 200 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'TOP'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
|
||||
if IsControlPressed(0, 173) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 200 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'DOWN'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
|
||||
if IsControlPressed(0, 174) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 150 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'LEFT'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
|
||||
if IsControlPressed(0, 175) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 150 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'RIGHT'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
end
|
||||
|
||||
local closeMenu = function(namespace, name)
|
||||
SendNUIMessage({
|
||||
action = 'closeMenu',
|
||||
namespace = namespace,
|
||||
name = name,
|
||||
data = data
|
||||
})
|
||||
end
|
||||
|
||||
ESX.UI.Menu.RegisterType(MenuType, openMenu, closeMenu)
|
||||
|
||||
RegisterNUICallback('menu_submit', function(data, cb)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
if menu.submit ~= nil then
|
||||
menu.submit(data, menu)
|
||||
end
|
||||
cb('OK')
|
||||
end)
|
||||
|
||||
RegisterNUICallback('menu_cancel', function(data, cb)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
|
||||
if menu.cancel ~= nil then
|
||||
menu.cancel(data, menu)
|
||||
end
|
||||
cb('OK')
|
||||
end)
|
||||
|
||||
RegisterNUICallback('menu_change', function(data, cb)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
|
||||
for i=1, #data.elements, 1 do
|
||||
menu.setElement(i, 'value', data.elements[i].value)
|
||||
|
||||
if data.elements[i].selected then
|
||||
menu.setElement(i, 'selected', true)
|
||||
else
|
||||
menu.setElement(i, 'selected', false)
|
||||
end
|
||||
end
|
||||
|
||||
if menu.change ~= nil then
|
||||
menu.change(data, menu)
|
||||
end
|
||||
cb('OK')
|
||||
end)
|
||||
|
||||
Citizen.CreateThread(function()
|
||||
while true do
|
||||
Citizen.Wait(15)
|
||||
|
||||
if IsControlPressed(0, 18) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 150 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'ENTER'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
|
||||
if IsControlPressed(0, 177) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 150 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'BACKSPACE'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
|
||||
if IsControlPressed(0, 27) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 200 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'TOP'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
|
||||
if IsControlPressed(0, 173) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 200 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'DOWN'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
|
||||
if IsControlPressed(0, 174) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 150 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'LEFT'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
|
||||
if IsControlPressed(0, 175) and IsInputDisabled(0) and (GetGameTimer() - GUI.Time) > 150 then
|
||||
SendNUIMessage({action = 'controlPressed', control = 'RIGHT'})
|
||||
GUI.Time = GetGameTimer()
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -1,105 +1,103 @@
|
||||
local ESX = exports.es_extended:getSharedObject()
|
||||
local Timeouts, OpenedMenus, MenuType = {}, {}, 'dialog'
|
||||
|
||||
local openMenu = function(namespace, name, data)
|
||||
for i=1, #Timeouts, 1 do
|
||||
ESX.ClearTimeout(Timeouts[i])
|
||||
end
|
||||
|
||||
OpenedMenus[namespace .. '_' .. name] = true
|
||||
|
||||
SendNUIMessage({
|
||||
action = 'openMenu',
|
||||
namespace = namespace,
|
||||
name = name,
|
||||
data = data
|
||||
})
|
||||
|
||||
local timeoutId = ESX.SetTimeout(200, function()
|
||||
SetNuiFocus(true, true)
|
||||
end)
|
||||
|
||||
table.insert(Timeouts, timeoutId)
|
||||
end
|
||||
|
||||
local closeMenu = function(namespace, name)
|
||||
OpenedMenus[namespace .. '_' .. name] = nil
|
||||
|
||||
SendNUIMessage({
|
||||
action = 'closeMenu',
|
||||
namespace = namespace,
|
||||
name = name,
|
||||
data = data
|
||||
})
|
||||
|
||||
if ESX.Table.SizeOf(OpenedMenus) == 0 then
|
||||
SetNuiFocus(false)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
ESX.UI.Menu.RegisterType(MenuType, openMenu, closeMenu)
|
||||
|
||||
AddEventHandler('esx_menu_dialog:message:menu_submit', function(data)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
local cancel = false
|
||||
|
||||
if menu.submit then
|
||||
-- is the submitted data a number?
|
||||
if tonumber(data.value) then
|
||||
data.value = ESX.Math.Round(tonumber(data.value))
|
||||
|
||||
-- check for negative value
|
||||
if tonumber(data.value) <= 0 then
|
||||
cancel = true
|
||||
end
|
||||
end
|
||||
|
||||
data.value = ESX.Math.Trim(data.value)
|
||||
|
||||
-- don't submit if the value is negative or if it's 0
|
||||
if cancel then
|
||||
ESX.ShowNotification('That input is not allowed!')
|
||||
else
|
||||
menu.submit(data, menu)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
AddEventHandler('esx_menu_dialog:message:menu_cancel', function(data)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
|
||||
if menu.cancel ~= nil then
|
||||
menu.cancel(data, menu)
|
||||
end
|
||||
end)
|
||||
|
||||
AddEventHandler('esx_menu_dialog:message:menu_change', function(data)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
|
||||
if menu.change ~= nil then
|
||||
menu.change(data, menu)
|
||||
end
|
||||
end)
|
||||
|
||||
Citizen.CreateThread(function()
|
||||
local ESX = exports['es_extended']:getSharedObject()
|
||||
local Timeouts, OpenedMenus, MenuType = {}, {}, 'dialog'
|
||||
while true do
|
||||
Citizen.Wait(10)
|
||||
|
||||
local openMenu = function(namespace, name, data)
|
||||
for i=1, #Timeouts, 1 do
|
||||
ESX.ClearTimeout(Timeouts[i])
|
||||
if ESX.Table.SizeOf(OpenedMenus) > 0 then
|
||||
DisableControlAction(0, 1, true) -- LookLeftRight
|
||||
DisableControlAction(0, 2, true) -- LookUpDown
|
||||
DisableControlAction(0, 142, true) -- MeleeAttackAlternate
|
||||
DisableControlAction(0, 106, true) -- VehicleMouseControlOverride
|
||||
DisableControlAction(0, 12, true) -- WeaponWheelUpDown
|
||||
DisableControlAction(0, 14, true) -- WeaponWheelNext
|
||||
DisableControlAction(0, 15, true) -- WeaponWheelPrev
|
||||
DisableControlAction(0, 16, true) -- SelectNextWeapon
|
||||
DisableControlAction(0, 17, true) -- SelectPrevWeapon
|
||||
else
|
||||
Citizen.Wait(500)
|
||||
end
|
||||
|
||||
OpenedMenus[namespace .. '_' .. name] = true
|
||||
|
||||
SendNUIMessage({
|
||||
action = 'openMenu',
|
||||
namespace = namespace,
|
||||
name = name,
|
||||
data = data
|
||||
})
|
||||
|
||||
local timeoutId = ESX.SetTimeout(200, function()
|
||||
SetNuiFocus(true, true)
|
||||
end)
|
||||
|
||||
table.insert(Timeouts, timeoutId)
|
||||
end
|
||||
|
||||
local closeMenu = function(namespace, name)
|
||||
OpenedMenus[namespace .. '_' .. name] = nil
|
||||
|
||||
SendNUIMessage({
|
||||
action = 'closeMenu',
|
||||
namespace = namespace,
|
||||
name = name,
|
||||
data = data
|
||||
})
|
||||
|
||||
if ESX.Table.SizeOf(OpenedMenus) == 0 then
|
||||
SetNuiFocus(false)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
ESX.UI.Menu.RegisterType(MenuType, openMenu, closeMenu)
|
||||
|
||||
AddEventHandler('esx_menu_dialog:message:menu_submit', function(data)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
local cancel = false
|
||||
|
||||
if menu.submit then
|
||||
-- is the submitted data a number?
|
||||
if tonumber(data.value) then
|
||||
data.value = ESX.Math.Round(tonumber(data.value))
|
||||
|
||||
-- check for negative value
|
||||
if tonumber(data.value) <= 0 then
|
||||
cancel = true
|
||||
end
|
||||
end
|
||||
|
||||
data.value = ESX.Math.Trim(data.value)
|
||||
|
||||
-- don't submit if the value is negative or if it's 0
|
||||
if cancel then
|
||||
ESX.ShowNotification('That input is not allowed!')
|
||||
else
|
||||
menu.submit(data, menu)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
AddEventHandler('esx_menu_dialog:message:menu_cancel', function(data)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
|
||||
if menu.cancel ~= nil then
|
||||
menu.cancel(data, menu)
|
||||
end
|
||||
end)
|
||||
|
||||
AddEventHandler('esx_menu_dialog:message:menu_change', function(data)
|
||||
local menu = ESX.UI.Menu.GetOpened(MenuType, data._namespace, data._name)
|
||||
|
||||
if menu.change ~= nil then
|
||||
menu.change(data, menu)
|
||||
end
|
||||
end)
|
||||
|
||||
Citizen.CreateThread(function()
|
||||
while true do
|
||||
Citizen.Wait(10)
|
||||
|
||||
if ESX.Table.SizeOf(OpenedMenus) > 0 then
|
||||
DisableControlAction(0, 1, true) -- LookLeftRight
|
||||
DisableControlAction(0, 2, true) -- LookUpDown
|
||||
DisableControlAction(0, 142, true) -- MeleeAttackAlternate
|
||||
DisableControlAction(0, 106, true) -- VehicleMouseControlOverride
|
||||
DisableControlAction(0, 12, true) -- WeaponWheelUpDown
|
||||
DisableControlAction(0, 14, true) -- WeaponWheelNext
|
||||
DisableControlAction(0, 15, true) -- WeaponWheelPrev
|
||||
DisableControlAction(0, 16, true) -- SelectNextWeapon
|
||||
DisableControlAction(0, 17, true) -- SelectPrevWeapon
|
||||
else
|
||||
Citizen.Wait(500)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
Reference in New Issue
Block a user