From 9ba613c363fe4a88d006a1653315ca5af7d911a5 Mon Sep 17 00:00:00 2001 From: DragonWrangler1 Date: Tue, 10 Jun 2025 18:50:16 -0500 Subject: [PATCH 1/2] add configurable world close keybinding --- builtin/settingtypes.txt | 2 ++ src/client/inputhandler.cpp | 5 +++-- src/client/keys.h | 1 + src/defaultsettings.cpp | 1 + src/settings_translation_file.cpp | 3 +++ 5 files changed, 10 insertions(+), 2 deletions(-) diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index 37cd7d5e2..6e43d4cb6 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -182,6 +182,8 @@ invert_hotbar_mouse_wheel (Hotbar: Invert mouse wheel direction) bool false [**Keybindings] +keymap_close_game (Close game) key + keymap_forward (Move forward) key SYSTEM_SCANCODE_26 # Will also disable autoforward, when active. diff --git a/src/client/inputhandler.cpp b/src/client/inputhandler.cpp index d79bee4d6..fc8a46d39 100644 --- a/src/client/inputhandler.cpp +++ b/src/client/inputhandler.cpp @@ -73,6 +73,8 @@ void MyEventReceiver::reloadKeybindings() keybindings[KeyType::SLOT_1 + i] = getKeySetting(slot_key_name.c_str()); } + keybindings[KeyType::CLOSE_GAME] = getKeySetting("keymap_close_game"); + // First clear all keys, then re-add the ones we listen for keysListenedFor.clear(); for (int i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) { @@ -150,8 +152,7 @@ bool MyEventReceiver::OnEvent(const SEvent &event) } fullscreen_is_down = event.KeyInput.PressedDown; return true; - } else if (keyCode == EscapeKey && - event.KeyInput.PressedDown && event.KeyInput.Shift) { + } else if (keyCode == getKeySetting("keymap_close_game") && event.KeyInput.PressedDown) { g_gamecallback->disconnect(); return true; } diff --git a/src/client/keys.h b/src/client/keys.h index 4bce131d4..9f2c7c062 100644 --- a/src/client/keys.h +++ b/src/client/keys.h @@ -97,6 +97,7 @@ public: SLOT_32, // Fake keycode for array size and internal checks + CLOSE_GAME, INTERNAL_ENUM_COUNT }; diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 916ce9e51..4475a305f 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -134,6 +134,7 @@ void set_default_settings() #else #define USEKEY2(name, _, value) settings->setDefault(name, value) #endif + settings->setDefault("keymap_close_game", ""); USEKEY2("keymap_forward", "SYSTEM_SCANCODE_26", "KEY_KEY_W"); settings->setDefault("keymap_autoforward", ""); USEKEY2("keymap_backward", "SYSTEM_SCANCODE_22", "KEY_KEY_S"); diff --git a/src/settings_translation_file.cpp b/src/settings_translation_file.cpp index 15ca64825..0c0d1cafd 100644 --- a/src/settings_translation_file.cpp +++ b/src/settings_translation_file.cpp @@ -1082,6 +1082,9 @@ fake_function() { gettext("Chat message format"); gettext("Format of player chat messages. The following strings are valid placeholders:\n@name, @message, @timestamp (optional)"); gettext("Chat command time message threshold"); + gettext("Close game"); + gettext("Key for closing the game directly."); +} gettext("If the execution of a chat command takes longer than this specified time in\nseconds, add the time information to the chat command message"); gettext("Shutdown message"); gettext("A message to be displayed to all clients when the server shuts down."); From dc813bd6515bc9b89047e7650f10de8c0797e1a8 Mon Sep 17 00:00:00 2001 From: DragonWrangler1 Date: Tue, 10 Jun 2025 19:36:36 -0500 Subject: [PATCH 2/2] rename settings, make keybindings work properly set default secondary keybind address review fix whitespace fix review comments address reviews Apply a compromise remove excess newline in components.lua address most review requests fix whitespace errors --- builtin/common/settings/components.lua | 42 ++++++++++++++++++++++---- builtin/settingtypes.txt | 6 ++-- src/client/inputhandler.cpp | 13 ++++++-- src/client/inputhandler.h | 3 ++ src/client/keys.h | 1 - src/defaultsettings.cpp | 2 +- src/settings_translation_file.cpp | 3 -- 7 files changed, 54 insertions(+), 16 deletions(-) diff --git a/builtin/common/settings/components.lua b/builtin/common/settings/components.lua index 99fb0cd76..4052ee01c 100644 --- a/builtin/common/settings/components.lua +++ b/builtin/common/settings/components.lua @@ -438,11 +438,41 @@ function make.key(setting) if value == "" then return height end + + local critical_keys = { + keymap_chat = true, + keymap_drop = true, + keymap_inventory = true, + keymap_cmd = true, + keymap_cmd_local = true, + keymap_dig = true, + keymap_place = true, + } + for _, o in ipairs(core.full_settingtypes) do - if o.type == "key" and o.name ~= setting.name and core.are_keycodes_equal(core.settings:get(o.name), value) then - table.insert(fs, ("label[0,%f;%s]"):format(height + 0.3, - core.colorize(mt_color_orange, fgettext([[Conflicts with "$1"]], fgettext(o.readable_name))))) - height = height + 0.6 + if o.type == "key" and o.name ~= setting.name and + core.are_keycodes_equal(core.settings:get(o.name), value) then + + local is_current_close_world = setting.name == "keymap_close_world" + local is_other_close_world = o.name == "keymap_close_world" + + -- Current means current key you've messed with, other means the other key it conflicts with. + local is_current_critical = critical_keys[setting.name] + local is_other_critical = critical_keys[o.name] + + local show_warning_current = false + + if (is_current_close_world and is_other_critical) + or (is_other_close_world and is_current_critical) + or (not is_current_close_world and not is_other_close_world) then + show_warning_current = true + end + + if show_warning_current then + table.insert(fs, ("label[0,%f;%s]"):format(height + 0.3, + core.colorize(mt_color_orange, fgettext([[Conflicts with "$1"]], fgettext(o.readable_name))))) + height = height + 0.6 + end end end return height @@ -454,12 +484,12 @@ function make.key(setting) get_formspec = function(self, avail_w) self.resettable = core.settings:has(setting.name) - local btn_bind_width = math.max(2.5, avail_w/2) + local btn_bind_width = math.max(2.5, avail_w / 2) local value = core.settings:get(setting.name) local fs = { ("label[0,0.4;%s]"):format(get_label(setting)), ("button_key[%f,0;%f,0.8;%s;%s]"):format( - btn_bind_width, btn_bind_width-0.8, + btn_bind_width, btn_bind_width - 0.8, btn_bind, core.formspec_escape(value)), ("image_button[%f,0;0.8,0.8;%s;%s;]"):format(avail_w - 0.8, core.formspec_escape(defaulttexturedir .. "clear.png"), diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index 6e43d4cb6..9dfcd6375 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -182,8 +182,6 @@ invert_hotbar_mouse_wheel (Hotbar: Invert mouse wheel direction) bool false [**Keybindings] -keymap_close_game (Close game) key - keymap_forward (Move forward) key SYSTEM_SCANCODE_26 # Will also disable autoforward, when active. @@ -339,6 +337,10 @@ keymap_slot31 (Hotbar slot 31) key keymap_slot32 (Hotbar slot 32) key +# Modifier key bind for closing your world. +# Requires ESC + the selected key to work. +keymap_close_world (Return to Main Menu) key SYSTEM_SCANCODE_225 + [*Touchscreen] # Enables the touchscreen controls, allowing you to play the game with a touchscreen. diff --git a/src/client/inputhandler.cpp b/src/client/inputhandler.cpp index fc8a46d39..56bdb184e 100644 --- a/src/client/inputhandler.cpp +++ b/src/client/inputhandler.cpp @@ -73,8 +73,6 @@ void MyEventReceiver::reloadKeybindings() keybindings[KeyType::SLOT_1 + i] = getKeySetting(slot_key_name.c_str()); } - keybindings[KeyType::CLOSE_GAME] = getKeySetting("keymap_close_game"); - // First clear all keys, then re-add the ones we listen for keysListenedFor.clear(); for (int i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) { @@ -139,6 +137,7 @@ bool MyEventReceiver::OnEvent(const SEvent &event) // This is separate from other keyboard handling so that it also works in menus. if (event.EventType == EET_KEY_INPUT_EVENT) { KeyPress keyCode(event.KeyInput); + if (keyCode == getKeySetting("keymap_fullscreen")) { if (event.KeyInput.PressedDown && !fullscreen_is_down) { IrrlichtDevice *device = RenderingEngine::get_raw_device(); @@ -152,7 +151,15 @@ bool MyEventReceiver::OnEvent(const SEvent &event) } fullscreen_is_down = event.KeyInput.PressedDown; return true; - } else if (keyCode == getKeySetting("keymap_close_game") && event.KeyInput.PressedDown) { + + } else if (keyCode == getKeySetting("keymap_close_world")) { + close_world_down = event.KeyInput.PressedDown; + + } else if (keyCode == EscapeKey) { + esc_down = event.KeyInput.PressedDown; + } + + if (esc_down && close_world_down) { g_gamecallback->disconnect(); return true; } diff --git a/src/client/inputhandler.h b/src/client/inputhandler.h index 85da30ff8..ce2c41a63 100644 --- a/src/client/inputhandler.h +++ b/src/client/inputhandler.h @@ -128,6 +128,9 @@ private: // Intentionally not reset by clearInput/releaseAllKeys. bool fullscreen_is_down = false; + bool close_world_down = false; + bool esc_down = false; + PointerType last_pointer_type = PointerType::Mouse; }; diff --git a/src/client/keys.h b/src/client/keys.h index 9f2c7c062..4bce131d4 100644 --- a/src/client/keys.h +++ b/src/client/keys.h @@ -97,7 +97,6 @@ public: SLOT_32, // Fake keycode for array size and internal checks - CLOSE_GAME, INTERNAL_ENUM_COUNT }; diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 4475a305f..f970da060 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -134,7 +134,6 @@ void set_default_settings() #else #define USEKEY2(name, _, value) settings->setDefault(name, value) #endif - settings->setDefault("keymap_close_game", ""); USEKEY2("keymap_forward", "SYSTEM_SCANCODE_26", "KEY_KEY_W"); settings->setDefault("keymap_autoforward", ""); USEKEY2("keymap_backward", "SYSTEM_SCANCODE_22", "KEY_KEY_S"); @@ -220,6 +219,7 @@ void set_default_settings() settings->setDefault("keymap_slot30", ""); settings->setDefault("keymap_slot31", ""); settings->setDefault("keymap_slot32", ""); + USEKEY2("keymap_close_world", "SYSTEM_SCANCODE_225", "KEY_LSHIFT"); #ifndef NDEBUG // Default keybinds for quicktune in debug builds diff --git a/src/settings_translation_file.cpp b/src/settings_translation_file.cpp index 0c0d1cafd..15ca64825 100644 --- a/src/settings_translation_file.cpp +++ b/src/settings_translation_file.cpp @@ -1082,9 +1082,6 @@ fake_function() { gettext("Chat message format"); gettext("Format of player chat messages. The following strings are valid placeholders:\n@name, @message, @timestamp (optional)"); gettext("Chat command time message threshold"); - gettext("Close game"); - gettext("Key for closing the game directly."); -} gettext("If the execution of a chat command takes longer than this specified time in\nseconds, add the time information to the chat command message"); gettext("Shutdown message"); gettext("A message to be displayed to all clients when the server shuts down.");