Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
ComboBox API additions for minetest#13814
Browse files Browse the repository at this point in the history
- Allow reliably detecting when the user opens a combobox.
- Allow preventing comboboxes from opening.
- Allow sending a combobox change event.
  • Loading branch information
grorp committed Sep 30, 2023
1 parent d767d27 commit ea1b583
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
4 changes: 4 additions & 0 deletions include/IEventReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ namespace irr
//! A checkbox has changed its check state.
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.
/** NOTE: You also get this event currently when the same item was clicked again after more than 500 ms. */
EGET_LISTBOX_CHANGED,
Expand Down
4 changes: 4 additions & 0 deletions include/IGUIComboBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ namespace gui
//! Sets the selected item. Set this to -1 if no item should be selected
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
/** \param horizontal: EGUIA_UPPERLEFT for left justified (default),
EGUIA_LOWERRIGHT for right justified, or EGUIA_CENTER for centered text.
Expand Down
32 changes: 26 additions & 6 deletions source/Irrlicht/CGUIComboBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace gui
CGUIComboBox::CGUIComboBox(IGUIEnvironment* environment, IGUIElement* parent,
s32 id, core::rect<s32> 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),
ActiveFont(0)
ActiveFont(nullptr)
{
#ifdef _DEBUG
setDebugName("CGUIComboBox");
Expand Down Expand Up @@ -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.
bool CGUIComboBox::OnEvent(const SEvent& event)
{
Expand All @@ -208,7 +217,7 @@ bool CGUIComboBox::OnEvent(const SEvent& event)
openCloseMenu();
}

ListButton->setPressed(ListBox == 0);
ListButton->setPressed(ListBox == nullptr);

return true;
}
Expand Down Expand Up @@ -361,7 +370,7 @@ void CGUIComboBox::sendSelectionChangedEvent()

event.EventType = EET_GUI_EVENT;
event.GUIEvent.Caller = this;
event.GUIEvent.Element = 0;
event.GUIEvent.Element = nullptr;
event.GUIEvent.EventType = EGET_COMBO_BOX_CHANGED;
Parent->OnEvent(event);
}
Expand Down Expand Up @@ -447,12 +456,23 @@ void CGUIComboBox::openCloseMenu()
// close list box
Environment->setFocus(this);
ListBox->remove();
ListBox = 0;
ListBox = nullptr;
}
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);
}

IGUISkin* skin = Environment->getSkin();
u32 h = Items.size();
Expand Down
4 changes: 4 additions & 0 deletions source/Irrlicht/CGUIComboBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ namespace gui
//! sets the selected item. Set this to -1 if no item should be selected
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
void setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical) override;

Expand Down

0 comments on commit ea1b583

Please sign in to comment.