From c9e28d39e9a05b9168e9ac1b313c886c6400e169 Mon Sep 17 00:00:00 2001 From: y5nw <37980625+y5nw@users.noreply.github.com> Date: Sat, 24 May 2025 23:16:14 +0200 Subject: [PATCH] partial 2 --- builtin/common/settings/components.lua | 4 +- .../common/settings/dlg_change_keybinding.lua | 78 +++++++++++++++++++ builtin/common/settings/dlg_settings.lua | 6 +- builtin/common/settings/init.lua | 1 + 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 builtin/common/settings/dlg_change_keybinding.lua diff --git a/builtin/common/settings/components.lua b/builtin/common/settings/components.lua index c4a065c7e..52440d202 100644 --- a/builtin/common/settings/components.lua +++ b/builtin/common/settings/components.lua @@ -486,10 +486,12 @@ function make.key(setting) return table.concat(fs), height end, - on_submit = function(self, fields) + on_submit = function(self, fields, tabview) if fields[btn_bind] then core.settings:set(setting.name, fields[btn_bind]) return true + elseif fields[btn_edit] then + return show_change_keybinding_dlg(setting, tabview) elseif fields[btn_clear] then core.settings:set(setting.name, "") return true diff --git a/builtin/common/settings/dlg_change_keybinding.lua b/builtin/common/settings/dlg_change_keybinding.lua new file mode 100644 index 000000000..893e65b7e --- /dev/null +++ b/builtin/common/settings/dlg_change_keybinding.lua @@ -0,0 +1,78 @@ +-- Luanti +-- SPDX-License-Identifier: LGPL-2.1-or-later +local function get_formspec(dialogdata) + local name = dialogdata.setting.name + local readable_name = name + if dialogdata.setting.readable_name then + readable_name = ("%s (%s)"):format(fgettext(dialogdata.setting.readable_name), name) + end + + local fs = { + "formspec_version[4]", + "size[6,2]", -- size element; height can only be determined later + ("label[0.5,0.8;%s]"):format(readable_name), + } + + return table.concat(fs) +end + +local function buttonhandler(self, fields) + local name = self.data.setting.name + if fields.quit or fields.btn_close then + self:delete() + return true + end + return false +end + +local formspec_handlers = {} +if INIT == "pause_menu" then + core.register_on_formspec_input(function(formname, fields) + if formspec_handlers[formname] then + formspec_handlers[formname](fields) + return true + end + end) +end + +local is_mainmenu = INIT == "mainmenu" +function show_change_keybinding_dlg(setting, tabview) + local dlg + if is_mainmenu then + dlg = dialog_create("dlg_change_keybinding", + get_formspec, buttonhandler) + else + local name = "__builtin:rebind_" .. setting.name + dlg = { + show = function() + if dlg.removed then + --core.open_settings("controls_keyboard_and_mouse") + tabview:show() + else + core.show_formspec(name, get_formspec(dlg.data)) + end + end, + delete = function() + core.show_formspec(name, "") + formspec_handlers[name] = nil + dlg.removed = true + end, + data = {}, + } + formspec_handlers[name] = function(fields) + if buttonhandler(dlg, fields) then + dlg:show() + end + end + end + + dlg.data.setting = setting + + if is_mainmenu then + dlg:set_parent(tabview) + tabview:hide() + end + dlg:show() + + return is_mainmenu +end diff --git a/builtin/common/settings/dlg_settings.lua b/builtin/common/settings/dlg_settings.lua index 77fc8be3f..f92af751a 100644 --- a/builtin/common/settings/dlg_settings.lua +++ b/builtin/common/settings/dlg_settings.lua @@ -814,7 +814,9 @@ else -- case it's a no-op core.show_formspec("__builtin:settings", "") end - - core.show_formspec("__builtin:settings", get_formspec(dialog.data)) + dialog.show = function() -- Used by the keybinding form + core.show_formspec("__builtin:settings", get_formspec(dialog.data)) + end + dialog:show() end end diff --git a/builtin/common/settings/init.lua b/builtin/common/settings/init.lua index 71a95424b..d61d07492 100644 --- a/builtin/common/settings/init.lua +++ b/builtin/common/settings/init.lua @@ -6,6 +6,7 @@ local path = core.get_builtin_path() .. "common" .. DIR_DELIM .. "settings" .. D dofile(path .. "settingtypes.lua") dofile(path .. "dlg_change_mapgen_flags.lua") +dofile(path .. "dlg_change_keybinding.lua") dofile(path .. "dlg_settings.lua") -- Uncomment to generate 'minetest.conf.example' and 'settings_translation_file.cpp'.