mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Merge 88d887cf8d
into fdc149f316
This commit is contained in:
commit
63929492ba
14 changed files with 254 additions and 46 deletions
|
@ -432,14 +432,12 @@ end
|
|||
|
||||
function make.key(setting)
|
||||
local btn_bind = "bind_" .. setting.name
|
||||
local btn_clear = "unbind_" .. setting.name
|
||||
local btn_edit = "edit_" .. setting.name
|
||||
local function add_conflict_warnings(fs, height)
|
||||
local value = core.settings:get(setting.name)
|
||||
if value == "" then
|
||||
return height
|
||||
end
|
||||
local value = core.settings:get(setting.name):split("|")
|
||||
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
|
||||
if o.type == "key" and o.name ~= setting.name
|
||||
and has_keybinding_conflict(core.settings:get(o.name):split("|"), 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
|
||||
|
@ -454,30 +452,32 @@ 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 value = core.settings:get(setting.name)
|
||||
local btn_width = math.max(2.5, avail_w/2)
|
||||
local value = core.settings:get(setting.name):split("|")
|
||||
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, 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"),
|
||||
btn_clear),
|
||||
("tooltip[%s;%s]"):format(btn_clear, fgettext("Remove keybinding")),
|
||||
("tooltip[%s;%s]"):format(btn_edit, fgettext("Edit keybindings")),
|
||||
}
|
||||
if #value <= 1 then
|
||||
table.insert(fs, ("button_key[%f,0;%f,0.8;%s;%s]"):format(
|
||||
btn_width, btn_width-0.8, btn_bind, value[1] or ""))
|
||||
table.insert(fs, ("image_button[%f,0;0.8,0.8;%s;%s;]"):format(avail_w - 0.8,
|
||||
core.formspec_escape(defaulttexturedir.."overflow_btn.png"), btn_edit))
|
||||
else
|
||||
table.insert(fs, ("button[%f,0;%f,0.8;%s;%s]"):format(
|
||||
btn_width, btn_width, btn_edit, fgettext("Edit")))
|
||||
end
|
||||
local height = 0.8
|
||||
height = add_conflict_warnings(fs, height)
|
||||
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_clear] then
|
||||
core.settings:set(setting.name, "")
|
||||
return true
|
||||
elseif fields[btn_edit] then
|
||||
return show_change_keybinding_dlg(setting, tabview)
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
|
158
builtin/common/settings/dlg_change_keybinding.lua
Normal file
158
builtin/common/settings/dlg_change_keybinding.lua
Normal file
|
@ -0,0 +1,158 @@
|
|||
-- 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 value = dialogdata.value
|
||||
local selection = dialogdata.selection
|
||||
|
||||
local fs = {
|
||||
"formspec_version[4]",
|
||||
"size[6,9]",
|
||||
("label[0.5,0.8;%s]"):format(readable_name),
|
||||
("button[0.5,5.7;5,0.8;btn_add;%s]"):format(fgettext("Add keybinding")),
|
||||
("button_key[0.5,6.7;4.2,0.8;btn_bind;%s]"):format(core.formspec_escape(value[selection] or "")),
|
||||
("image_button[4.7,6.7;0.8,0.8;%s;btn_clear;]"):format(
|
||||
core.formspec_escape(defaulttexturedir .. "clear.png")),
|
||||
("tooltip[btn_clear;%s]"):format(fgettext("Remove keybinding")),
|
||||
("button[3.1,7.7;2.4,0.8;btn_close;%s]"):format(fgettext("Cancel")),
|
||||
("button[0.5,7.7;2.4,0.8;btn_save;%s]"):format(fgettext("Save")),
|
||||
}
|
||||
|
||||
local warning = ""
|
||||
local cells = {}
|
||||
for idx, key in ipairs(value) do
|
||||
local prefix = ""
|
||||
for _, o in ipairs(core.full_settingtypes) do
|
||||
if o.type == "key" and o.name ~= name
|
||||
and has_keybinding_conflict(core.settings:get(o.name):split("|"), key) then
|
||||
prefix = mt_color_orange
|
||||
if idx == selection then
|
||||
warning = core.colorize(mt_color_orange, fgettext([[Conflicts with "$1"]], fgettext_ne(o.readable_name)))
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
table.insert(cells, core.formspec_escape(prefix .. core.get_keycode_name(key)))
|
||||
end
|
||||
table.insert(fs, ("textlist[0.5,1.3;5,3.8;keylist;%s;%d;false]"):format(table.concat(cells, ","), selection))
|
||||
table.insert(fs, ("label[0.5,5.4;%s]"):format(warning))
|
||||
|
||||
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
|
||||
elseif fields.btn_save then
|
||||
local value = {}
|
||||
for _, v in ipairs(self.data.value) do
|
||||
if v ~= "" then -- filter out "empty" keybindings
|
||||
table.insert(value, v)
|
||||
end
|
||||
end
|
||||
core.settings:set(name, table.concat(value, "|"))
|
||||
self:delete()
|
||||
return true
|
||||
elseif fields.btn_clear then
|
||||
local selection = self.data.selection
|
||||
table.remove(self.data.value, selection)
|
||||
self.data.selection = math.max(1, math.min(selection, #self.data.value))
|
||||
return true
|
||||
elseif fields.btn_add then
|
||||
table.insert(self.data.value, "")
|
||||
self.data.selection = #self.data.value
|
||||
return true
|
||||
elseif fields.btn_bind then
|
||||
self.data.value[self.data.selection] = fields.btn_bind
|
||||
return true
|
||||
elseif fields.keylist then
|
||||
local event = core.explode_textlist_event(fields.keylist)
|
||||
if event.type == "CHG" or event.type == "DCL" then
|
||||
self.data.selection = event.index
|
||||
return true
|
||||
end
|
||||
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
|
||||
dlg.data.value = core.settings:get(setting.name):split("|")
|
||||
dlg.data.selection = 1
|
||||
|
||||
if is_mainmenu then
|
||||
dlg:set_parent(tabview)
|
||||
tabview:hide()
|
||||
end
|
||||
dlg:show()
|
||||
|
||||
return is_mainmenu
|
||||
end
|
||||
|
||||
function has_keybinding_conflict(t1, t2) -- not local as it is also used by the make.key component
|
||||
if type(t2) == "string" then
|
||||
if type(t1) == "string" then
|
||||
return core.are_keycodes_equal(t1, t2)
|
||||
else
|
||||
for _, v1 in pairs(t1) do
|
||||
if core.are_keycodes_equal(v1, t2) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
for _, v2 in pairs(t2) do
|
||||
if has_keybinding_conflict(t1, v2) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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'.
|
||||
|
|
|
@ -74,6 +74,17 @@ local function create_rebind_keys_dlg()
|
|||
return dlg
|
||||
end
|
||||
|
||||
local function normalize_key_setting(str)
|
||||
if str == "|" then
|
||||
return core.normalize_keycode(str)
|
||||
end
|
||||
local t = string.split(str, "|")
|
||||
for k, v in pairs(t) do
|
||||
t[k] = core.normalize_keycode(v)
|
||||
end
|
||||
return table.concat(t, "|")
|
||||
end
|
||||
|
||||
function migrate_keybindings(parent)
|
||||
-- Show migration dialog if the user upgraded from an earlier version
|
||||
-- and this has not yet been shown before, *or* if keys settings had to be changed
|
||||
|
@ -86,7 +97,7 @@ function migrate_keybindings(parent)
|
|||
local settings = core.settings:to_table()
|
||||
for name, value in pairs(settings) do
|
||||
if name:match("^keymap_") then
|
||||
local normalized = core.normalize_keycode(value)
|
||||
local normalized = normalize_key_setting(value)
|
||||
if value ~= normalized then
|
||||
has_migration = true
|
||||
core.settings:set(name, normalized)
|
||||
|
|
|
@ -26,7 +26,7 @@ void MyEventReceiver::reloadKeybindings()
|
|||
keybindings[KeyType::DIG] = getKeySetting("keymap_dig");
|
||||
keybindings[KeyType::PLACE] = getKeySetting("keymap_place");
|
||||
|
||||
keybindings[KeyType::ESC] = EscapeKey;
|
||||
keybindings[KeyType::ESC] = std::vector<KeyPress>{EscapeKey};
|
||||
|
||||
keybindings[KeyType::AUTOFORWARD] = getKeySetting("keymap_autoforward");
|
||||
|
||||
|
@ -76,7 +76,9 @@ void MyEventReceiver::reloadKeybindings()
|
|||
// First clear all keys, then re-add the ones we listen for
|
||||
keysListenedFor.clear();
|
||||
for (int i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
|
||||
listenForKey(keybindings[i], static_cast<GameKeyType>(i));
|
||||
for (auto key: keybindings[i]) {
|
||||
listenForKey(key, static_cast<GameKeyType>(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,13 +87,11 @@ bool MyEventReceiver::setKeyDown(KeyPress keyCode, bool is_down)
|
|||
if (keysListenedFor.find(keyCode) == keysListenedFor.end()) // ignore irrelevant key input
|
||||
return false;
|
||||
auto action = keysListenedFor[keyCode];
|
||||
if (is_down) {
|
||||
if (is_down)
|
||||
physicalKeyDown.insert(keyCode);
|
||||
setKeyDown(action, true);
|
||||
} else {
|
||||
else
|
||||
physicalKeyDown.erase(keyCode);
|
||||
setKeyDown(action, false);
|
||||
}
|
||||
setKeyDown(action, checkKeyDown(action));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -109,6 +109,15 @@ void MyEventReceiver::setKeyDown(GameKeyType action, bool is_down)
|
|||
}
|
||||
}
|
||||
|
||||
bool MyEventReceiver::checkKeyDown(GameKeyType action)
|
||||
{
|
||||
for (auto key: keybindings[action]) {
|
||||
if (physicalKeyDown.find(key) != physicalKeyDown.end())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MyEventReceiver::OnEvent(const SEvent &event)
|
||||
{
|
||||
if (event.EventType == irr::EET_LOG_TEXT_EVENT) {
|
||||
|
@ -137,7 +146,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 (keySettingHasMatch("keymap_fullscreen", keyCode)) {
|
||||
if (event.KeyInput.PressedDown && !fullscreen_is_down) {
|
||||
IrrlichtDevice *device = RenderingEngine::get_raw_device();
|
||||
|
||||
|
|
|
@ -97,13 +97,14 @@ private:
|
|||
|
||||
bool setKeyDown(KeyPress keyCode, bool is_down);
|
||||
void setKeyDown(GameKeyType action, bool is_down);
|
||||
bool checkKeyDown(GameKeyType action);
|
||||
|
||||
/* This is faster than using getKeySetting with the tradeoff that functions
|
||||
* using it must make sure that it's initialised before using it and there is
|
||||
* no error handling (for example bounds checking). This is useful here as the
|
||||
* faster (up to 10x faster) key lookup is an asset.
|
||||
*/
|
||||
std::array<KeyPress, KeyType::INTERNAL_ENUM_COUNT> keybindings;
|
||||
std::array<std::vector<KeyPress>, KeyType::INTERNAL_ENUM_COUNT> keybindings;
|
||||
|
||||
s32 mouse_wheel = 0;
|
||||
|
||||
|
|
|
@ -380,23 +380,32 @@ KeyPress KeyPress::getSpecialKey(const std::string &name)
|
|||
*/
|
||||
|
||||
// A simple cache for quicker lookup
|
||||
static std::unordered_map<std::string, KeyPress> g_key_setting_cache;
|
||||
static std::unordered_map<std::string, std::vector<KeyPress>> g_key_setting_cache;
|
||||
|
||||
KeyPress getKeySetting(const std::string &settingname)
|
||||
const std::vector<KeyPress> &getKeySetting(const std::string &settingname)
|
||||
{
|
||||
auto n = g_key_setting_cache.find(settingname);
|
||||
if (n != g_key_setting_cache.end())
|
||||
return n->second;
|
||||
|
||||
auto keysym = g_settings->get(settingname);
|
||||
auto setting_value = g_settings->get(settingname);
|
||||
auto &ref = g_key_setting_cache[settingname];
|
||||
ref = KeyPress(keysym);
|
||||
if (!keysym.empty() && !ref) {
|
||||
warningstream << "Invalid key '" << keysym << "' for '" << settingname << "'." << std::endl;
|
||||
for (const auto &keysym: str_split(setting_value, '|')) {
|
||||
if (KeyPress kp = keysym) {
|
||||
ref.push_back(kp);
|
||||
} else {
|
||||
warningstream << "Invalid key '" << keysym << "' for '" << settingname << "'." << std::endl;
|
||||
}
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
bool keySettingHasMatch(const std::string &settingname, KeyPress kp)
|
||||
{
|
||||
const auto &keylist = getKeySetting(settingname);
|
||||
return std::find(keylist.begin(), keylist.end(), kp) != keylist.end();
|
||||
}
|
||||
|
||||
void clearKeyCache()
|
||||
{
|
||||
g_key_setting_cache.clear();
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <IEventReceiver.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
/* A key press, consisting of a scancode or a keycode.
|
||||
* This fits into 64 bits, so prefer passing this by value.
|
||||
|
@ -92,7 +93,13 @@ struct std::hash<KeyPress>
|
|||
#define RMBKey KeyPress::getSpecialKey("KEY_RBUTTON")
|
||||
|
||||
// Key configuration getter
|
||||
KeyPress getKeySetting(const std::string &settingname);
|
||||
// Note that the reference may be invalidated by a next call to getKeySetting
|
||||
// or a related function, so the value should either be used immediately or
|
||||
// copied elsewhere before calling this again.
|
||||
const std::vector<KeyPress> &getKeySetting(const std::string &settingname);
|
||||
|
||||
// Check whether the key setting includes a key.
|
||||
bool keySettingHasMatch(const std::string &settingname, KeyPress kp);
|
||||
|
||||
// Clear fast lookup cache
|
||||
void clearKeyCache();
|
||||
|
|
|
@ -436,7 +436,7 @@ bool GUIChatConsole::OnEvent(const SEvent& event)
|
|||
}
|
||||
|
||||
// Key input
|
||||
if (KeyPress(event.KeyInput) == getKeySetting("keymap_console")) {
|
||||
if (keySettingHasMatch("keymap_console", event.KeyInput)) {
|
||||
closeConsole();
|
||||
|
||||
// inhibit open so the_game doesn't reopen immediately
|
||||
|
|
|
@ -3997,7 +3997,7 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
|
|||
if (event.EventType == EET_KEY_INPUT_EVENT) {
|
||||
KeyPress kp(event.KeyInput);
|
||||
if (kp == EscapeKey
|
||||
|| kp == getKeySetting("keymap_inventory")
|
||||
|| keySettingHasMatch("keymap_inventory", kp)
|
||||
|| event.KeyInput.Key==KEY_RETURN) {
|
||||
gui::IGUIElement *focused = Environment->getFocus();
|
||||
if (focused && isMyChild(focused) &&
|
||||
|
@ -4096,17 +4096,17 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
|
|||
}
|
||||
if (event.KeyInput.PressedDown && (
|
||||
(kp == EscapeKey) ||
|
||||
((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
|
||||
((m_client != NULL) && (keySettingHasMatch("keymap_inventory", kp))))) {
|
||||
tryClose();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (m_client != NULL && event.KeyInput.PressedDown &&
|
||||
(kp == getKeySetting("keymap_screenshot"))) {
|
||||
(keySettingHasMatch("keymap_screenshot", kp))) {
|
||||
m_client->makeScreenshot();
|
||||
}
|
||||
|
||||
if (event.KeyInput.PressedDown && kp == getKeySetting("keymap_toggle_debug")) {
|
||||
if (event.KeyInput.PressedDown && keySettingHasMatch("keymap_toggle_debug", kp)) {
|
||||
if (!m_client || m_client->checkPrivilege("debug"))
|
||||
m_show_debug = !m_show_debug;
|
||||
}
|
||||
|
|
|
@ -212,11 +212,11 @@ static KeyPress id_to_keypress(touch_gui_button_id id)
|
|||
auto setting_name = id_to_setting(id);
|
||||
|
||||
assert(!setting_name.empty());
|
||||
auto kp = getKeySetting(setting_name);
|
||||
if (!kp)
|
||||
const auto &keylist = getKeySetting(setting_name);
|
||||
if (keylist.empty())
|
||||
warningstream << "TouchControls: Unbound or invalid key for "
|
||||
<< setting_name << ", hiding button." << std::endl;
|
||||
return kp;
|
||||
return keylist[0];
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -42,6 +42,14 @@ int ModApiMenuCommon::l_normalize_keycode(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int ModApiMenuCommon::l_get_keycode_name(lua_State *L)
|
||||
{
|
||||
auto keystr = luaL_checkstring(L, 1);
|
||||
auto name = KeyPress(keystr).name();
|
||||
lua_pushstring(L, name.empty() ? "" : gettext(name.c_str()));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void ModApiMenuCommon::Initialize(lua_State *L, int top)
|
||||
{
|
||||
|
@ -49,6 +57,7 @@ void ModApiMenuCommon::Initialize(lua_State *L, int top)
|
|||
API_FCT(get_active_driver);
|
||||
API_FCT(irrlicht_device_supports_touch);
|
||||
API_FCT(normalize_keycode);
|
||||
API_FCT(get_keycode_name);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ private:
|
|||
static int l_get_active_driver(lua_State *L);
|
||||
static int l_irrlicht_device_supports_touch(lua_State *L);
|
||||
static int l_normalize_keycode(lua_State *L);
|
||||
static int l_get_keycode_name(lua_State *L);
|
||||
|
||||
public:
|
||||
static void Initialize(lua_State *L, int top);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue