1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Formspec: Move GUI event handling to switch/case

This commit is contained in:
SmallJoker 2025-05-28 14:30:40 +02:00 committed by sfan5
parent 660b1cf9bf
commit 535d757563

View file

@ -4821,12 +4821,18 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
} }
if (event.EventType == EET_GUI_EVENT) { if (event.EventType == EET_GUI_EVENT) {
if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED const s32 caller_id = event.GUIEvent.Caller->getID();
&& isVisible()) { bool close_on_enter;
// find the element that was clicked
switch (event.GUIEvent.EventType) {
case gui::EGET_TAB_CHANGED:
if (!isVisible())
break;
// find the element that was clicked
for (GUIFormSpecMenu::FieldSpec &s : m_fields) { for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
if ((s.ftype == f_TabHeader) && if (s.ftype == f_TabHeader &&
(s.fid == event.GUIEvent.Caller->getID())) { s.fid == caller_id) {
if (!s.sound.empty() && m_sound_manager) if (!s.sound.empty() && m_sound_manager)
m_sound_manager->playSound(0, SoundSpec(s.sound, 1.0f)); m_sound_manager->playSound(0, SoundSpec(s.sound, 1.0f));
s.send = true; s.send = true;
@ -4835,22 +4841,24 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
return true; return true;
} }
} }
} break;
if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
&& isVisible()) { case gui::EGET_ELEMENT_FOCUS_LOST:
if (!isVisible())
break;
if (!canTakeFocus(event.GUIEvent.Element)) { if (!canTakeFocus(event.GUIEvent.Element)) {
infostream<<"GUIFormSpecMenu: Not allowing focus change." infostream<<"GUIFormSpecMenu: Not allowing focus change."
<<std::endl; <<std::endl;
// Returning true disables focus change // Returning true disables focus change
return true; return true;
} }
} break;
if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
(event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
(event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
(event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
s32 caller_id = event.GUIEvent.Caller->getID();
case gui::EGET_BUTTON_CLICKED:
case gui::EGET_CHECKBOX_CHANGED:
case gui::EGET_COMBO_BOX_CHANGED:
case gui::EGET_SCROLL_BAR_CHANGED:
if (caller_id == ID_PROCEED_BTN) { if (caller_id == ID_PROCEED_BTN) {
trySubmitClose(); trySubmitClose();
return true; return true;
@ -4922,55 +4930,57 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
s.send = false; s.send = false;
} }
} }
}
if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) { if (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED) {
// move scroll_containers // move scroll_containers
for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers) for (const std::pair<std::string, GUIScrollContainer *> &c : m_scroll_containers)
c.second->onScrollEvent(event.GUIEvent.Caller); c.second->onScrollEvent(event.GUIEvent.Caller);
} }
break;
if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) { case gui::EGET_EDITBOX_ENTER:
if (event.GUIEvent.Caller->getID() > ID_PROCEED_BTN) { if (caller_id <= ID_PROCEED_BTN)
bool close_on_enter = true; break;
for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
if (s.ftype == f_Unknown &&
s.fid == event.GUIEvent.Caller->getID()) {
current_field_enter_pending = s.fname;
auto it = field_close_on_enter.find(s.fname);
if (it != field_close_on_enter.end())
close_on_enter = (*it).second;
break; close_on_enter = true;
} for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
if (s.ftype == f_Unknown &&
s.fid == caller_id) {
current_field_enter_pending = s.fname;
auto it = field_close_on_enter.find(s.fname);
if (it != field_close_on_enter.end())
close_on_enter = (*it).second;
break;
} }
}
current_keys_pending.key_enter = true; current_keys_pending.key_enter = true;
if (close_on_enter) { if (close_on_enter)
trySubmitClose(); trySubmitClose();
} else { else
acceptInput();
return true;
case gui::EGET_TABLE_CHANGED:
if (caller_id <= ID_PROCEED_BTN)
break;
// find the element that was clicked
for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
// if it's a table, set the send field
// so lua knows which table was changed
if (s.ftype == f_Table && s.fid == caller_id) {
s.send = true;
acceptInput(); acceptInput();
s.send = false;
} }
return true;
} }
} return true;
if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) { default:
int current_id = event.GUIEvent.Caller->getID(); break;
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
// so lua knows which table was changed
if ((s.ftype == f_Table) && (s.fid == current_id)) {
s.send = true;
acceptInput();
s.send=false;
}
}
return true;
}
} }
} }