1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-06 17:41:04 +00:00

Improve KeyPress handling (#15923)

* Pass KeyPress by value
* TouchControls: add setting change callback for keybindings
This commit is contained in:
y5nw 2025-03-21 12:07:51 +01:00 committed by GitHub
parent ead44a27ca
commit 4ba438a7ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 51 additions and 37 deletions

View file

@ -205,6 +205,8 @@ void GUIKeyChangeMenu::drawMenu()
bool GUIKeyChangeMenu::acceptInput()
{
clearKeyCache();
for (key_setting *k : key_settings) {
std::string default_key;
Settings::getLayer(SL_DEFAULTS)->getNoEx(k->setting_name, default_key);
@ -231,8 +233,6 @@ bool GUIKeyChangeMenu::acceptInput()
g_settings->setBool("autojump", ((gui::IGUICheckBox*)e)->isChecked());
}
clearKeyCache();
g_gamecallback->signalKeyConfigChange();
return true;

View file

@ -31,7 +31,7 @@
TouchControls *g_touchcontrols;
void TouchControls::emitKeyboardEvent(const KeyPress &key, bool pressed)
void TouchControls::emitKeyboardEvent(KeyPress key, bool pressed)
{
SEvent e{};
e.EventType = EET_KEY_INPUT_EVENT;
@ -142,12 +142,8 @@ bool TouchControls::buttonsStep(std::vector<button_info> &buttons, float dtime)
return has_pointers;
}
static const KeyPress &id_to_keypress(touch_gui_button_id id)
static std::string id_to_setting(touch_gui_button_id id)
{
// ESC isn't part of the keymap.
if (id == exit_id)
return EscapeKey;
std::string key = "";
switch (id) {
case dig_id:
@ -204,11 +200,22 @@ static const KeyPress &id_to_keypress(touch_gui_button_id id)
default:
break;
}
assert(!key.empty());
auto &kp = getKeySetting("keymap_" + key);
return key.empty() ? key : "keymap_" + key;
}
static KeyPress id_to_keypress(touch_gui_button_id id)
{
// ESC isn't part of the keymap.
if (id == exit_id)
return EscapeKey;
auto setting_name = id_to_setting(id);
assert(!setting_name.empty());
auto kp = getKeySetting(setting_name);
if (!kp)
warningstream << "TouchControls: Unbound or invalid key for"
<< key << ", hiding button." << std::endl;
warningstream << "TouchControls: Unbound or invalid key for "
<< setting_name << ", hiding button." << std::endl;
return kp;
}
@ -232,6 +239,11 @@ TouchControls::TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc)
readSettings();
for (auto name : setting_names)
g_settings->registerChangedCallback(name, settingChangedCallback, this);
// Also update layout when keybindings change (e.g. for convertibles)
for (u8 id = 0; id < touch_gui_button_id_END; id++)
if (auto name = id_to_setting((touch_gui_button_id)id); !name.empty())
g_settings->registerChangedCallback(name, settingChangedCallback, this);
}
void TouchControls::settingChangedCallback(const std::string &name, void *data)

View file

@ -204,7 +204,7 @@ private:
// for its buttons. We only want static image display, not interactivity,
// from Irrlicht.
void emitKeyboardEvent(const KeyPress &keycode, bool pressed);
void emitKeyboardEvent(KeyPress keycode, bool pressed);
void loadButtonTexture(IGUIImage *gui_button, const std::string &path);
void buttonEmitAction(button_info &btn, bool action);