From a1124e3519cd8fcdfa4f6589a2d70ddee56a2cf0 Mon Sep 17 00:00:00 2001 From: FireSiku Date: Wed, 6 Jun 2018 19:37:26 -0400 Subject: [PATCH] Keybindings: move input_service:get(special_key) calls out of the loop. The function is called whenever a key is pressed, therefore we only need to check for ctrl, alt and shift once. Each of them was called twice, nested inside two loops. The same thing can be found in Custom Menus, the same block of three lines and six conditionals. Good target for refactoring. --- vmf/scripts/mods/vmf/modules/core/keybindings.lua | 10 +++++++--- vmf/scripts/mods/vmf/modules/gui/custom_menus.lua | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/vmf/scripts/mods/vmf/modules/core/keybindings.lua b/vmf/scripts/mods/vmf/modules/core/keybindings.lua index 18ef0f2..68a9f18 100644 --- a/vmf/scripts/mods/vmf/modules/core/keybindings.lua +++ b/vmf/scripts/mods/vmf/modules/core/keybindings.lua @@ -127,13 +127,17 @@ vmf.check_pressed_keybinds = function() end local key_has_active_keybind = false + local input_ctrl = input_service:get("ctrl") + local input_shift = input_service:get("shift") + local input_alt = input_service:get("alt") for key, key_bindings in pairs(_optimized_keybinds) do if input_service:get(key) then + for _, binding_info in ipairs(key_bindings) do - if (not binding_info[3] and not input_service:get("ctrl") or binding_info[3] and input_service:get("ctrl")) and - (not binding_info[4] and not input_service:get("alt") or binding_info[4] and input_service:get("alt")) and - (not binding_info[5] and not input_service:get("shift") or binding_info[5] and input_service:get("shift")) then + if (not binding_info[3] and not input_ctrl or binding_info[3] and input_ctrl) and + (not binding_info[4] and not input_alt or binding_info[4] and input_alt) and + (not binding_info[5] and not input_shift or binding_info[5] and input_shift) then local mod = get_mod(binding_info[1]) diff --git a/vmf/scripts/mods/vmf/modules/gui/custom_menus.lua b/vmf/scripts/mods/vmf/modules/gui/custom_menus.lua index cdd069b..f1e3fc5 100644 --- a/vmf/scripts/mods/vmf/modules/gui/custom_menus.lua +++ b/vmf/scripts/mods/vmf/modules/gui/custom_menus.lua @@ -138,12 +138,16 @@ vmf.check_custom_menus_close_keybinds = function() opening_keybind_is_pressed = false end + local input_ctrl = input_service:get("ctrl") + local input_shift = input_service:get("shift") + local input_alt = input_service:get("alt") + local close_menu = false if not opening_keybind_is_pressed then if input_service:get(close_keybind[1]) and - (not close_keybind[2] and not input_service:get("ctrl") or close_keybind[2] and input_service:get("ctrl")) and - (not close_keybind[3] and not input_service:get("alt") or close_keybind[3] and input_service:get("alt")) and - (not close_keybind[4] and not input_service:get("shift") or close_keybind[4] and input_service:get("shift")) then + (not close_keybind[2] and not input_ctrl or close_keybind[2] and input_ctrl) and + (not close_keybind[3] and not input_alt or close_keybind[3] and input_alt) and + (not close_keybind[4] and not input_shift or close_keybind[4] and input_shift) then close_menu = not ingame_ui.views[ingame_ui.current_view]:input_service():is_blocked() end