ComboBox API additions for minetest#13814

- Allow reliably detecting when the user opens a combobox.
- Allow preventing comboboxes from opening.
- Allow sending a combobox change event.
This commit is contained in:
Gregor Parzefall 2023-09-30 16:46:30 +02:00 committed by GitHub
parent d767d27ca8
commit ea1b58387e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 6 deletions

@ -263,6 +263,10 @@ namespace irr
//! A checkbox has changed its check state. //! A checkbox has changed its check state.
EGET_CHECKBOX_CHANGED, EGET_CHECKBOX_CHANGED,
//! A listbox would like to open.
/** You can prevent the listbox from opening by absorbing the event. */
EGET_LISTBOX_OPENED,
//! A new item in a listbox was selected. //! A new item in a listbox was selected.
/** NOTE: You also get this event currently when the same item was clicked again after more than 500 ms. */ /** NOTE: You also get this event currently when the same item was clicked again after more than 500 ms. */
EGET_LISTBOX_CHANGED, EGET_LISTBOX_CHANGED,

@ -52,6 +52,10 @@ namespace gui
//! Sets the selected item. Set this to -1 if no item should be selected //! Sets the selected item. Set this to -1 if no item should be selected
virtual void setSelected(s32 idx) = 0; virtual void setSelected(s32 idx) = 0;
//! Sets the selected item and emits a change event.
/** Set this to -1 if no item should be selected */
virtual void setAndSendSelected(s32 idx) = 0;
//! Sets text justification of the text area //! Sets text justification of the text area
/** \param horizontal: EGUIA_UPPERLEFT for left justified (default), /** \param horizontal: EGUIA_UPPERLEFT for left justified (default),
EGUIA_LOWERRIGHT for right justified, or EGUIA_CENTER for centered text. EGUIA_LOWERRIGHT for right justified, or EGUIA_CENTER for centered text.

@ -22,9 +22,9 @@ namespace gui
CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent, CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent,
s32 id, core::rect<s32> rectangle) s32 id, core::rect<s32> rectangle)
: IGUIComboBox(environment, parent, id, rectangle), : IGUIComboBox(environment, parent, id, rectangle),
ListButton(0), SelectedText(0), ListBox(0), LastFocus(0), ListButton(nullptr), SelectedText(nullptr), ListBox(nullptr), LastFocus(nullptr),
Selected(-1), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER), MaxSelectionRows(5), HasFocus(false), Selected(-1), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER), MaxSelectionRows(5), HasFocus(false),
ActiveFont(0) ActiveFont(nullptr)
{ {
#ifdef _DEBUG #ifdef _DEBUG
setDebugName("CGUIComboBox"); setDebugName("CGUIComboBox");
@ -185,6 +185,15 @@ void CGUIComboBox::setSelected(s32 idx)
} }
//! Sets the selected item and emits a change event.
/** Set this to -1 if no item should be selected */
void CGUIComboBox::setAndSendSelected(s32 idx)
{
setSelected(idx);
sendSelectionChangedEvent();
}
//! called if an event happened. //! called if an event happened.
bool CGUIComboBox::OnEvent(const SEvent& event) bool CGUIComboBox::OnEvent(const SEvent& event)
{ {
@ -208,7 +217,7 @@ bool CGUIComboBox::OnEvent(const SEvent& event)
openCloseMenu(); openCloseMenu();
} }
ListButton->setPressed(ListBox == 0); ListButton->setPressed(ListBox == nullptr);
return true; return true;
} }
@ -361,7 +370,7 @@ void CGUIComboBox::sendSelectionChangedEvent()
event.EventType = EET_GUI_EVENT; event.EventType = EET_GUI_EVENT;
event.GUIEvent.Caller = this; event.GUIEvent.Caller = this;
event.GUIEvent.Element = 0; event.GUIEvent.Element = nullptr;
event.GUIEvent.EventType = EGET_COMBO_BOX_CHANGED; event.GUIEvent.EventType = EGET_COMBO_BOX_CHANGED;
Parent->OnEvent(event); Parent->OnEvent(event);
} }
@ -447,12 +456,23 @@ void CGUIComboBox::openCloseMenu()
// close list box // close list box
Environment->setFocus(this); Environment->setFocus(this);
ListBox->remove(); ListBox->remove();
ListBox = 0; ListBox = nullptr;
} }
else else
{ {
if (Parent) if (Parent) {
SEvent event;
event.EventType = EET_GUI_EVENT;
event.GUIEvent.Caller = this;
event.GUIEvent.Element = nullptr;
event.GUIEvent.EventType = EGET_LISTBOX_OPENED;
// Allow to prevent the listbox from opening.
if (Parent->OnEvent(event))
return;
Parent->bringToFront(this); Parent->bringToFront(this);
}
IGUISkin* skin = Environment->getSkin(); IGUISkin* skin = Environment->getSkin();
u32 h = Items.size(); u32 h = Items.size();

@ -55,6 +55,10 @@ namespace gui
//! sets the selected item. Set this to -1 if no item should be selected //! sets the selected item. Set this to -1 if no item should be selected
void setSelected(s32 idx) override; void setSelected(s32 idx) override;
//! Sets the selected item and emits a change event.
/** Set this to -1 if no item should be selected */
void setAndSendSelected(s32 idx) override;
//! sets the text alignment of the text part //! sets the text alignment of the text part
void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical) override; void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical) override;