From 75fb26d3cf3c869707ccf88d7466bd3875db1182 Mon Sep 17 00:00:00 2001 From: Azumgi <4zumgi@gmail.com> Date: Thu, 27 Sep 2018 15:05:48 +0300 Subject: [PATCH] [Keybinds] Change way of getting key data --- .../mods/vmf/modules/core/keybindings.lua | 31 +++++++++++++------ vmf/scripts/mods/vmf/modules/core/options.lua | 2 +- .../modules/ui/options/vmf_options_view.lua | 16 +++++----- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/vmf/scripts/mods/vmf/modules/core/keybindings.lua b/vmf/scripts/mods/vmf/modules/core/keybindings.lua index dcacf72..9406fbe 100644 --- a/vmf/scripts/mods/vmf/modules/core/keybindings.lua +++ b/vmf/scripts/mods/vmf/modules/core/keybindings.lua @@ -180,7 +180,7 @@ end local keyboard_buton_name = Keyboard.button_name local mouse_buton_name = Mouse.button_name -vmf.keys = { +local _keys = { keyboard = { [8] = {"Backspace", "backspace", keyboard_buton_name(8)}, [9] = {"Tab", "tab", keyboard_buton_name(9)}, @@ -327,32 +327,43 @@ vmf.keys = { }]] } -vmf.readable_key_names = {} +local _readable_key_names = {} + + +function vmf.get_key_name(device, key_index) + local key_info = _keys[device][key_index] + return key_info and key_info[2] +end + + +function vmf.get_readable_key_name(key_name) + return _readable_key_names[key_name] +end -- #################################################################################################################### -- ##### Script ####################################################################################################### -- #################################################################################################################### -for _, controller_keys in pairs(vmf.keys) do +for _, controller_keys in pairs(_keys) do for _, key_info in pairs(controller_keys) do - vmf.readable_key_names[key_info[2]] = key_info[1] + _readable_key_names[key_info[2]] = key_info[1] end end -vmf.readable_key_names["ctrl"] = "Ctrl" -vmf.readable_key_names["alt"] = "Alt" -vmf.readable_key_names["shift"] = "Shift" +_readable_key_names["ctrl"] = "Ctrl" +_readable_key_names["alt"] = "Alt" +_readable_key_names["shift"] = "Shift" -for _, key_info in pairs(vmf.keys.keyboard) do +for _, key_info in pairs(_keys.keyboard) do VMFModsKeyMap.win32[key_info[2]] = {"keyboard", key_info[3], "held"} end for i = 0, 4 do - local key_info = vmf.keys.mouse[i] + local key_info = _keys.mouse[i] VMFModsKeyMap.win32[key_info[2]] = {"mouse", key_info[3], "held"} end for i = 10, 13 do - local key_info = vmf.keys.mouse[i] + local key_info = _keys.mouse[i] VMFModsKeyMap.win32[key_info[2]] = {"mouse", key_info[3], "pressed"} end \ No newline at end of file diff --git a/vmf/scripts/mods/vmf/modules/core/options.lua b/vmf/scripts/mods/vmf/modules/core/options.lua index 9913318..ef255bc 100644 --- a/vmf/scripts/mods/vmf/modules/core/options.lua +++ b/vmf/scripts/mods/vmf/modules/core/options.lua @@ -339,7 +339,7 @@ local function validate_keybind_data(data) vmf.throw_error("[widget \"%s\" (keybind)]: table stored in 'default_value' field can't exceed 4 elements", data.setting_id) end - if default_value[1] and (not vmf.readable_key_names[default_value[1]] or allowed_special_keys[default_value[1]]) then + if default_value[1] and (not vmf.get_readable_key_name(default_value[1]) or allowed_special_keys[default_value[1]]) then vmf.throw_error("[widget \"%s\" (keybind)]: 'default_value[1]' must be a valid key name", data.setting_id) end if default_value[2] and not allowed_special_keys[default_value[2]] or diff --git a/vmf/scripts/mods/vmf/modules/ui/options/vmf_options_view.lua b/vmf/scripts/mods/vmf/modules/ui/options/vmf_options_view.lua index 1865cd6..85174e6 100644 --- a/vmf/scripts/mods/vmf/modules/ui/options/vmf_options_view.lua +++ b/vmf/scripts/mods/vmf/modules/ui/options/vmf_options_view.lua @@ -2488,9 +2488,9 @@ local function build_keybind_string(keys) for i, key in ipairs(keys) do if i == 1 then - keybind_string = keybind_string .. vmf.readable_key_names[key] + keybind_string = keybind_string .. vmf.get_readable_key_name(key) else - keybind_string = keybind_string .. " + " .. vmf.readable_key_names[key] + keybind_string = keybind_string .. " + " .. vmf.get_readable_key_name(key) end end @@ -3307,25 +3307,25 @@ VMFOptionsView.callback_setting_keybind = function (self, widget_content) if not widget_content.first_pressed_button and (Keyboard.any_pressed() or Mouse.any_pressed()) then - local first_pressed_button_info = nil + local first_pressed_button_name = nil local first_pressed_button_index = nil local first_pressed_button_type = nil if Keyboard.any_pressed() then - first_pressed_button_info = vmf.keys.keyboard[Keyboard.any_pressed()] + first_pressed_button_name = vmf.get_key_name("keyboard", Keyboard.any_pressed()) first_pressed_button_index = Keyboard.any_pressed() first_pressed_button_type = "keyboard" elseif Mouse.any_pressed() then - first_pressed_button_info = vmf.keys.mouse[Mouse.any_pressed()] + first_pressed_button_name = vmf.get_key_name("mouse", Mouse.any_pressed()) first_pressed_button_index = Mouse.any_pressed() first_pressed_button_type = "mouse" end - if first_pressed_button_info then - widget_content.first_pressed_button = first_pressed_button_info[2] + if first_pressed_button_name then + widget_content.first_pressed_button = first_pressed_button_name widget_content.first_pressed_button_index = first_pressed_button_index widget_content.first_pressed_button_type = first_pressed_button_type end @@ -3336,7 +3336,7 @@ VMFOptionsView.callback_setting_keybind = function (self, widget_content) if widget_content.first_pressed_button then table.insert(pressed_buttons, widget_content.first_pressed_button) - preview_string = vmf.readable_key_names[widget_content.first_pressed_button] + preview_string = vmf.get_readable_key_name(widget_content.first_pressed_button) end if Keyboard.button(Keyboard.button_index("left ctrl")) == 1 then preview_string = preview_string .. " + Ctrl"