From fcddac6c072dd6ee4085a059b02af177c0e0e4da Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Wed, 28 May 2025 14:18:06 +0200 Subject: [PATCH] Formspec: Fix malfunctioning 'Proceed' button on sizeless formspecs --- doc/menu_lua_api.md | 4 +-- src/gui/guiFormSpecMenu.cpp | 49 +++++++++++++++++-------------------- src/gui/guiFormSpecMenu.h | 3 +-- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/doc/menu_lua_api.md b/doc/menu_lua_api.md index 0a067764e..56b51f3b9 100644 --- a/doc/menu_lua_api.md +++ b/doc/menu_lua_api.md @@ -23,8 +23,8 @@ Callbacks * `core.button_handler(fields)`: called when a button is pressed. * `fields` = `{name1 = value1, name2 = value2, ...}` * `core.event_handler(event)` - * `event`: `"MenuQuit"`, `"KeyEnter"`, `"ExitButton"`, `"EditBoxEnter"` or - `"FullscreenChange"` + * `event`: `"MenuQuit"` (derived from `quit`) or `"FullscreenChange"` + The main menu may issue custom events, such as `"Refresh"` (server list). * `core.on_before_close()`: called before the menu is closed, either to exit or to join a game diff --git a/src/gui/guiFormSpecMenu.cpp b/src/gui/guiFormSpecMenu.cpp index f1e91bd0b..75832298f 100644 --- a/src/gui/guiFormSpecMenu.cpp +++ b/src/gui/guiFormSpecMenu.cpp @@ -77,6 +77,9 @@ return; \ } +// Element ID of the "Proceed" button shown for sizeless formspecs +constexpr s32 ID_PROCEED_BTN = 257; + /* GUIFormSpecMenu */ @@ -2998,7 +3001,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) gui::IGUIElement *focused_element = Environment->getFocus(); if (focused_element && focused_element->getParent() == this) { s32 focused_id = focused_element->getID(); - if (focused_id > 257) { + if (focused_id > ID_PROCEED_BTN) { for (const GUIFormSpecMenu::FieldSpec &field : m_fields) { if (field.fid == focused_id) { m_focused_element = field.fname; @@ -3308,7 +3311,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) size.X / 2 - 70, pos.Y, size.X / 2 - 70 + 140, pos.Y + m_btn_height * 2 ); - GUIButton::addButton(Environment, mydata.rect, m_tsrc, this, 257, + GUIButton::addButton(Environment, mydata.rect, m_tsrc, this, ID_PROCEED_BTN, wstrgettext("Proceed").c_str()); } } @@ -4031,12 +4034,7 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event) if (m_joystick->wasKeyDown(KeyType::ESC)) { tryClose(); } else if (m_joystick->wasKeyDown(KeyType::JUMP)) { - if (m_allowclose) { - acceptInput(quit_mode_accept); - quitMenu(); - } else { - acceptInput(quit_mode_try); - } + trySubmitClose(); } } return handled; @@ -4056,6 +4054,16 @@ void GUIFormSpecMenu::tryClose() } } +void GUIFormSpecMenu::trySubmitClose() +{ + if (m_allowclose) { + acceptInput(quit_mode_accept); + quitMenu(); + } else { + acceptInput(quit_mode_try); + } +} + bool GUIFormSpecMenu::OnEvent(const SEvent& event) { if (event.EventType==EET_KEY_INPUT_EVENT) { @@ -4099,12 +4107,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event) break; } if (current_keys_pending.key_enter) { - if (m_allowclose) { - acceptInput(quit_mode_accept); - quitMenu(); - } else { - acceptInput(quit_mode_try); - } + trySubmitClose(); } else { acceptInput(); } @@ -4848,10 +4851,8 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event) (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) { s32 caller_id = event.GUIEvent.Caller->getID(); - if (caller_id == 257) { - acceptInput(quit_mode_accept); - m_text_dst->gotText(L"ExitButton"); - quitMenu(); + if (caller_id == ID_PROCEED_BTN) { + trySubmitClose(); return true; } @@ -4881,7 +4882,6 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event) if (s.is_exit) { acceptInput(quit_mode_accept); - m_text_dst->gotText(L"ExitButton"); quitMenu(); return true; } @@ -4931,7 +4931,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event) } if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) { - if (event.GUIEvent.Caller->getID() > 257) { + if (event.GUIEvent.Caller->getID() > ID_PROCEED_BTN) { bool close_on_enter = true; for (GUIFormSpecMenu::FieldSpec &s : m_fields) { if (s.ftype == f_Unknown && @@ -4948,12 +4948,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event) current_keys_pending.key_enter = true; if (close_on_enter) { - if (m_allowclose) { - acceptInput(quit_mode_accept); - quitMenu(); - } else { - acceptInput(quit_mode_try); - } + trySubmitClose(); } else { acceptInput(); } @@ -4963,7 +4958,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event) if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) { int current_id = event.GUIEvent.Caller->getID(); - if (current_id > 257) { + if (current_id > ID_PROCEED_BTN) { // find the element that was clicked for (GUIFormSpecMenu::FieldSpec &s : m_fields) { // if it's a table, set the send field diff --git a/src/gui/guiFormSpecMenu.h b/src/gui/guiFormSpecMenu.h index 8dbfcab93..f279bbb29 100644 --- a/src/gui/guiFormSpecMenu.h +++ b/src/gui/guiFormSpecMenu.h @@ -68,8 +68,6 @@ struct TextDest { virtual ~TextDest() = default; - // This is deprecated I guess? -celeron55 - virtual void gotText(const std::wstring &text) {} virtual void gotText(const StringMap &fields) = 0; std::string m_formname; @@ -493,6 +491,7 @@ private: bool parseMiddleRect(const std::string &value, core::rect *parsed_rect); void tryClose(); + void trySubmitClose(); void showTooltip(const std::wstring &text, const irr::video::SColor &color, const irr::video::SColor &bgcolor);