ManaPlus
Public Member Functions | Protected Types | Protected Attributes | Static Protected Attributes
ListBox Class Reference

#include <listbox.h>

Inheritance diagram for ListBox:
Widget MouseListener KeyListener Widget2 AvatarListBox ColorPage ExtendedListBox ServersListBox ShopListBox SkillListBox

Public Member Functions

 ListBox (const Widget2 *const widget, ListModel *const listModel, const std::string &skin)
 
 ~ListBox ()
 
void postInit ()
 
void draw (Graphics *const graphics)
 
void safeDraw (Graphics *const graphics)
 
void updateAlpha ()
 
void keyPressed (KeyEvent &event)
 
void mouseWheelMovedUp (MouseEvent &event)
 
void mouseWheelMovedDown (MouseEvent &event)
 
void mousePressed (MouseEvent &event)
 
void mouseReleased (MouseEvent &event)
 
void mouseReleased1 (const MouseEvent &event)
 
void mouseDragged (MouseEvent &event)
 
void refocus ()
 
void setDistributeMousePressed (const bool b)
 
virtual void adjustSize ()
 
void logic ()
 
virtual int getSelectionByMouse (const int y) const
 
void setCenter (const bool b)
 
int getPressedIndex () const
 
virtual unsigned int getRowHeight () const
 
void setRowHeight (unsigned int n)
 
int getSelected () const
 
void setSelected (const int selected)
 
void setListModel (ListModel *listModel)
 
ListModelgetListModel () const
 
bool isWrappingEnabled () const
 
void setWrappingEnabled (const bool wrappingEnabled)
 
void addSelectionListener (SelectionListener *const selectionListener)
 
void removeSelectionListener (SelectionListener *const selectionListener)
 
void distributeValueChangedEvent ()
 
- Public Member Functions inherited from Widget
 Widget (const Widget2 *const widget)
 
 ~Widget ()
 
virtual void drawFrame (Graphics *graphics)
 
virtual void safeDrawFrame (Graphics *graphics)
 
void setFrameSize (const unsigned int frameSize)
 
unsigned int getFrameSize () const
 
WidgetgetParent () const
 
void setWidth (const int width)
 
int getWidth () const
 
void setHeight (const int height)
 
int getHeight () const
 
void setSize (const int width, const int height)
 
void setX (const int x)
 
int getX () const
 
void setY (const int y)
 
int getY () const
 
void setPosition (const int x, const int y)
 
void setDimension (const Rect &dimension)
 
const RectgetDimension () const
 
void setFocusable (const bool focusable)
 
bool isFocusable () const
 
virtual bool isFocused () const
 
void setEnabled (const bool enabled)
 
bool isEnabled () const
 
void setVisible (Visible visible)
 
bool isVisible () const
 
void setBaseColor (const Color &color)
 
const ColorgetBaseColor () const
 
void setForegroundColor (const Color &color)
 
const ColorgetForegroundColor () const
 
void setBackgroundColor (const Color &color)
 
const ColorgetBackgroundColor () const
 
virtual void requestFocus ()
 
virtual void requestMoveToTop ()
 
virtual void requestMoveToBottom ()
 
virtual void setFocusHandler (FocusHandler *const focusHandler)
 
FocusHandlergetFocusHandler ()
 
void addActionListener (ActionListener *const actionListener)
 
void removeActionListener (ActionListener *const actionListener)
 
void addDeathListener (WidgetDeathListener *const deathListener)
 
void removeDeathListener (WidgetDeathListener *const deathListener)
 
void addMouseListener (MouseListener *const mouseListener)
 
void removeMouseListener (MouseListener *const mouseListener)
 
void addKeyListener (KeyListener *const keyListener)
 
void removeKeyListener (KeyListener *const keyListener)
 
void addFocusListener (FocusListener *const focusListener)
 
void removeFocusListener (FocusListener *const focusListener)
 
void addWidgetListener (WidgetListener *const widgetListener)
 
void removeWidgetListener (WidgetListener *const widgetListener)
 
void setActionEventId (const std::string &actionEventId)
 
const std::string & getActionEventId () const
 
virtual void getAbsolutePosition (int &x, int &y) const
 
virtual void setParent (Widget *parent)
 
FontgetFont () const
 
void setFont (Font *const font)
 
virtual void fontChanged ()
 
bool isTabInEnabled () const
 
void setTabInEnabled (const bool enabled)
 
bool isTabOutEnabled () const
 
void setTabOutEnabled (const bool enabled)
 
void requestModalFocus ()
 
virtual void requestModalMouseInputFocus ()
 
virtual void releaseModalFocus ()
 
virtual void releaseModalMouseInputFocus ()
 
virtual bool isModalFocused () const
 
virtual bool isModalMouseInputFocused () const
 
virtual WidgetgetWidgetAt (int x, int y)
 
const std::list< MouseListener * > & getMouseListeners () const A_CONST
 
const std::list< KeyListener * > & getKeyListeners () const A_CONST
 
const std::list< FocusListener * > & getFocusListeners () const A_CONST
 
virtual Rect getChildrenArea ()
 
virtual FocusHandlergetInternalFocusHandler ()
 
void setInternalFocusHandler (FocusHandler *const internalFocusHandler)
 
virtual void moveToTop (Widget *widget)
 
virtual void moveToBottom (Widget *widget)
 
virtual void focusNext ()
 
virtual void focusPrevious ()
 
virtual void showWidgetPart (Widget *const widget, const Rect &area)
 
void setId (const std::string &id)
 
const std::string & getId () const
 
virtual void showPart (const Rect &rectangle)
 
bool isAllowLogic () const
 
void setMouseConsume (const bool b)
 
bool isMouseConsume () const
 
void setRedraw (const bool b)
 
virtual bool isSelectable () const
 
void setSelectable (const bool selectable)
 
void windowResized ()
 
- Public Member Functions inherited from Widget2
virtual ~Widget2 ()
 
const ColorgetThemeColor (const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
 
const ColorgetThemeCharColor (const signed char c, bool &valid) const A_INLINE
 
virtual void setWidget2 (const Widget2 *const widget)
 
void setPalette (int palette)
 
void checkPalette ()
 
void setForegroundColor2 (const Color &color)
 
WidgetgetWindow () const
 
virtual void setWindow (Widget *const window)
 
- Public Member Functions inherited from MouseListener
virtual ~MouseListener ()
 
virtual void mouseEntered (MouseEvent &event)
 
virtual void mouseExited (MouseEvent &event)
 
virtual void mouseClicked (MouseEvent &event)
 
virtual void mouseMoved (MouseEvent &event)
 
- Public Member Functions inherited from KeyListener
virtual ~KeyListener ()
 
virtual void keyReleased (KeyEvent &event)
 

Protected Types

typedef std::list< SelectionListener * > SelectionListenerList
 
typedef SelectionListenerList::iterator SelectionListenerIterator
 
- Protected Types inherited from Widget
typedef std::list< MouseListener * > MouseListenerList
 
typedef MouseListenerList::iterator MouseListenerIterator
 
typedef std::list< KeyListener * > KeyListenerList
 
typedef KeyListenerList::iterator KeyListenerIterator
 
typedef std::list< ActionListener * > ActionListenerList
 
typedef ActionListenerList::iterator ActionListenerIterator
 
typedef std::list< WidgetDeathListener * > WidgetDeathListenerList
 
typedef WidgetDeathListenerList::iterator WidgetDeathListenerIterator
 
typedef std::list< FocusListener * > FocusListenerList
 
typedef FocusListenerList::iterator FocusListenerIterator
 
typedef std::list< WidgetListener * > WidgetListenerList
 
typedef WidgetListenerList::iterator WidgetListenerIterator
 

Protected Attributes

int mSelected
 
ListModelmListModel
 
bool mWrappingEnabled
 
SelectionListenerList mSelectionListeners
 
Color mHighlightColor
 
Color mForegroundSelectedColor
 
Color mForegroundSelectedColor2
 
int mOldSelected
 
int mPadding
 
int mPressedIndex
 
unsigned int mRowHeight
 
int mItemPadding
 
SkinmSkin
 
bool mDistributeMousePressed
 
bool mCenterText
 
- Protected Attributes inherited from Widget
MouseListenerList mMouseListeners
 
KeyListenerList mKeyListeners
 
ActionListenerList mActionListeners
 
WidgetDeathListenerList mDeathListeners
 
FocusListenerList mFocusListeners
 
WidgetListenerList mWidgetListeners
 
Color mForegroundColor
 
Color mBackgroundColor
 
Color mBaseColor
 
Rect mDimension
 
std::string mActionEventId
 
std::string mId
 
FocusHandlermFocusHandler
 
FocusHandlermInternalFocusHandler
 
WidgetmParent
 
FontmCurrentFont
 
unsigned int mFrameSize
 
bool mFocusable
 
bool mTabIn
 
bool mTabOut
 
bool mEnabled
 
bool mAllowLogic
 
bool mMouseConsume
 
bool mRedraw
 
bool mSelectable
 
- Protected Attributes inherited from Widget2
int mPaletteOffset
 
WidgetmWindow
 
Color mForegroundColor2
 

Static Protected Attributes

static float mAlpha = 1.0
 
- Static Protected Attributes inherited from Widget
static FontmGlobalFont = 0
 

Additional Inherited Members

- Static Public Member Functions inherited from Widget
static void setGlobalFont (Font *const font)
 
static FontgetGloablFont ()
 
static void cleanGlobalFont ()
 
static bool widgetExists (const Widget *const widget)
 
static void distributeWindowResizeEvent ()
 
static WidgetcallPostInit (Widget *const widget)
 
- Data Fields inherited from Widget
Visible mVisible
 
- Protected Member Functions inherited from Widget
void distributeActionEvent ()
 
void distributeResizedEvent ()
 
void distributeMovedEvent ()
 
void distributeHiddenEvent ()
 
void distributeShownEvent ()
 
- Protected Member Functions inherited from Widget2
 Widget2 (const Widget2 *const widget)
 
- Protected Member Functions inherited from MouseListener
 MouseListener ()
 
- Protected Member Functions inherited from KeyListener
 KeyListener ()
 

Detailed Description

A list box, meant to be used inside a scroll area. Same as the Guichan list box except this one doesn't have a background, instead completely relying on the scroll area. It also adds selection listener functionality.

Definition at line 90 of file listbox.h.

Member Typedef Documentation

◆ SelectionListenerIterator

typedef SelectionListenerList::iterator ListBox::SelectionListenerIterator
protected

Typedef.

Definition at line 280 of file listbox.h.

◆ SelectionListenerList

typedef std::list<SelectionListener*> ListBox::SelectionListenerList
protected

Typdef.

Definition at line 270 of file listbox.h.

Constructor & Destructor Documentation

◆ ListBox()

ListBox::ListBox ( const Widget2 *const  widget,
ListModel *const  listModel,
const std::string &  skin 
)

Constructor.

Definition at line 88 of file listbox.cpp.

References Widget::addKeyListener(), Widget::addMouseListener(), CAST_U32, ServerCommandType::font, Widget::getFont(), Font::getHeight(), Skin::getOption(), Skin::getPadding(), Widget2::getThemeColor(), Theme::getThemePath(), ThemeColorId::LISTBOX, ThemeColorId::LISTBOX_OUTLINE, Theme::load(), Widget::mForegroundColor, Widget2::mForegroundColor2, mItemPadding, mPadding, mRowHeight, mSkin, Widget::setFocusable(), Widget::setWidth(), and theme.

90  :
91  Widget(widget),
92  MouseListener(),
93  KeyListener(),
94  mSelected(-1),
95  mListModel(listModel),
96  mWrappingEnabled(false),
100  255U)),
103  mOldSelected(-1),
104  mPadding(0),
105  mPressedIndex(-2),
106  mRowHeight(0),
107  mItemPadding(1),
108  mSkin(nullptr),
110  mCenterText(false)
111 {
112  setWidth(100);
113  setFocusable(true);
114  addMouseListener(this);
115  addKeyListener(this);
116 
119 
120  if (theme != nullptr)
121  {
122  mSkin = theme->load(skin,
123  "listbox.xml",
124  true,
126  }
127 
128  if (mSkin != nullptr)
129  {
131  mItemPadding = mSkin->getOption("itemPadding");
132  }
133 
134  const Font *const font = getFont();
136  font->getHeight() + 2 * mItemPadding);
137 }
#define CAST_U32
Definition: cast.h:30
Font * getFont() const
Definition: widget.cpp:330
int mSelected
Definition: listbox.h:255
Skin * mSkin
Definition: listbox.h:290
int mPadding
Definition: listbox.h:286
Color mForegroundSelectedColor2
Definition: listbox.h:284
void setWidth(const int width)
Definition: widget.cpp:132
Definition: font.h:88
SelectionListenerList mSelectionListeners
Definition: listbox.h:275
unsigned int mRowHeight
Definition: listbox.h:288
static std::string getThemePath()
Definition: theme.h:66
Color mHighlightColor
Definition: listbox.h:282
Skin * load(const std::string &filename, const std::string &filename2, const bool full, const std::string &defaultPath)
Definition: theme.cpp:178
Color mForegroundColor
Definition: widget.h:1085
ListModel * mListModel
Definition: listbox.h:260
int getOption(const std::string &name) const
Definition: skin.h:105
Color mForegroundSelectedColor
Definition: listbox.h:283
int getPadding() const
Definition: skin.h:99
Widget(const Widget2 *const widget)
Definition: widget.cpp:84
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
int mPressedIndex
Definition: listbox.h:287
int mItemPadding
Definition: listbox.h:289
Theme * theme
Definition: theme.cpp:61
void setFocusable(const bool focusable)
Definition: widget.cpp:191
bool mCenterText
Definition: listbox.h:293
Color mForegroundColor2
Definition: widget2.h:112
bool mWrappingEnabled
Definition: listbox.h:265
int mOldSelected
Definition: listbox.h:285
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
int getHeight() const
Definition: font.cpp:360
bool mDistributeMousePressed
Definition: listbox.h:292

◆ ~ListBox()

ListBox::~ListBox ( )

Definition at line 144 of file listbox.cpp.

References gui, mSkin, Gui::removeDragged(), theme, and Theme::unload().

145 {
146  if (gui != nullptr)
147  gui->removeDragged(this);
148 
149  if (theme != nullptr)
150  theme->unload(mSkin);
151 }
Skin * mSkin
Definition: listbox.h:290
void unload(Skin *const skin)
Definition: theme.cpp:249
Gui * gui
Definition: gui.cpp:110
Theme * theme
Definition: theme.cpp:61
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1160

Member Function Documentation

◆ addSelectionListener()

void ListBox::addSelectionListener ( SelectionListener *const  selectionListener)

Adds a selection listener to the list box. When the selection changes an event will be sent to all selection listeners of the list box.

If you delete your selection listener, be sure to also remove it using removeSelectionListener().

Parameters
selectionListenerThe selection listener to add.

Definition at line 434 of file listbox.cpp.

References mSelectionListeners.

Referenced by BuyDialog::init(), TextSelectDialog::postInit(), SellDialog::postInit(), ServerDialog::ServerDialog(), Setup_Colors::Setup_Colors(), setWrappingEnabled(), and ShopWindow::ShopWindow().

435 {
436  mSelectionListeners.push_back(selectionListener);
437 }
SelectionListenerList mSelectionListeners
Definition: listbox.h:275

◆ adjustSize()

void ListBox::adjustSize ( )
virtual

Reimplemented in ShopListBox, ExtendedListBox, and ColorPage.

Definition at line 373 of file listbox.cpp.

References BLOCK_END, BLOCK_START, CAST_S32, ListModel::getNumberOfElements(), getRowHeight(), mListModel, mPadding, and Widget::setHeight().

Referenced by ExtendedListBox::adjustSize(), PopupList::adjustSize(), logic(), postInit(), setDistributeMousePressed(), and setListModel().

374 {
375  BLOCK_START("ListBox::adjustSize")
376  if (mListModel != nullptr)
377  {
380  }
381  BLOCK_END("ListBox::adjustSize")
382 }
int mPadding
Definition: listbox.h:286
#define BLOCK_START(name)
Definition: perfomance.h:78
if(!vert) return
#define BLOCK_END(name)
Definition: perfomance.h:79
virtual int getNumberOfElements()=0
ListModel * mListModel
Definition: listbox.h:260
#define CAST_S32
Definition: cast.h:29
void setHeight(const int height)
Definition: widget.cpp:139
virtual unsigned int getRowHeight() const
Definition: listbox.h:155

◆ distributeValueChangedEvent()

void ListBox::distributeValueChangedEvent ( )

Distributes a value changed event to all selection listeners of the list box.

Definition at line 445 of file listbox.cpp.

References FOR_EACH, and mSelectionListeners.

Referenced by setSelected(), and setWrappingEnabled().

446 {
448  {
449  SelectionEvent event(this);
450  (*iter)->valueChanged(event);
451  }
452 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
SelectionListenerList mSelectionListeners
Definition: listbox.h:275
SelectionListenerList::iterator SelectionListenerIterator
Definition: listbox.h:280

◆ draw()

void ListBox::draw ( Graphics *const  graphics)
virtual

Draws the list box.

Implements Widget.

Reimplemented in SkillListBox, ShopListBox, and ServersListBox.

Definition at line 162 of file listbox.cpp.

References Color::a, BLOCK_END, BLOCK_START, CAST_S32, CAST_U32, Font::drawString(), Graphics::fillRectangle(), ServerCommandType::font, ListModel::getElementAt(), Widget::getFont(), ListModel::getNumberOfElements(), getRowHeight(), Font::getWidth(), mAlpha, mCenterText, Widget::mDimension, Widget::mForegroundColor, Widget2::mForegroundColor2, mForegroundSelectedColor, mForegroundSelectedColor2, mHighlightColor, mItemPadding, mListModel, mPadding, mSelected, Graphics::setColor(), ServerCommandType::str, updateAlpha(), Rect::width, and y.

Referenced by safeDraw().

163 {
164  if (mListModel == nullptr)
165  return;
166 
167  BLOCK_START("ListBox::draw")
168  updateAlpha();
169 
170  mHighlightColor.a = CAST_U32(mAlpha * 255.0F);
171  graphics->setColor(mHighlightColor);
172  Font *const font = getFont();
173  const int rowHeight = CAST_S32(getRowHeight());
174  const int width = mDimension.width;
175 
176  if (mCenterText)
177  {
178  // Draw filled rectangle around the selected list element
179  if (mSelected >= 0)
180  {
181  graphics->fillRectangle(Rect(mPadding,
182  rowHeight * mSelected + mPadding,
183  mDimension.width - 2 * mPadding, rowHeight));
184 
185  const std::string str = mListModel->getElementAt(mSelected);
186  font->drawString(graphics,
189  str,
190  (width - font->getWidth(str)) / 2,
191  mSelected * rowHeight + mPadding + mItemPadding);
192  }
193  // Draw the list elements
194  const int sz = mListModel->getNumberOfElements();
195  for (int i = 0, y = mPadding + mItemPadding;
196  i < sz; ++i, y += rowHeight)
197  {
198  if (i != mSelected)
199  {
200  const std::string str = mListModel->getElementAt(i);
201  font->drawString(graphics,
204  str,
205  (width - font->getWidth(str)) / 2, y);
206  }
207  }
208  }
209  else
210  {
211  // Draw filled rectangle around the selected list element
212  if (mSelected >= 0)
213  {
214  graphics->fillRectangle(Rect(mPadding,
215  rowHeight * mSelected + mPadding,
216  mDimension.width - 2 * mPadding, rowHeight));
217 
218  const std::string str = mListModel->getElementAt(mSelected);
219  font->drawString(graphics,
222  str,
223  mPadding,
224  mSelected * rowHeight + mPadding + mItemPadding);
225  }
226  // Draw the list elements
227  const int sz = mListModel->getNumberOfElements();
228  for (int i = 0, y = mPadding + mItemPadding; i < sz;
229  ++i, y += rowHeight)
230  {
231  if (i != mSelected)
232  {
233  const std::string str = mListModel->getElementAt(i);
234  font->drawString(graphics,
237  str,
238  mPadding, y);
239  }
240  }
241  }
242  BLOCK_END("ListBox::draw")
243 }
#define CAST_U32
Definition: cast.h:30
Font * getFont() const
Definition: widget.cpp:330
int mSelected
Definition: listbox.h:255
int width
Definition: rect.h:218
int mPadding
Definition: listbox.h:286
Color mForegroundSelectedColor2
Definition: listbox.h:284
Definition: font.h:88
Definition: rect.h:72
#define BLOCK_START(name)
Definition: perfomance.h:78
Color mHighlightColor
Definition: listbox.h:282
if(!vert) return
#define BLOCK_END(name)
Definition: perfomance.h:79
virtual int getNumberOfElements()=0
Color mForegroundColor
Definition: widget.h:1085
ListModel * mListModel
Definition: listbox.h:260
Color mForegroundSelectedColor
Definition: listbox.h:283
#define CAST_S32
Definition: cast.h:29
void updateAlpha()
Definition: listbox.cpp:153
static float mAlpha
Definition: listbox.h:291
void setColor(const Color &restrict color) override final
virtual std::string getElementAt(int i)=0
int mItemPadding
Definition: listbox.h:289
virtual void fillRectangle(const Rect &rectangle)=0
bool mCenterText
Definition: listbox.h:293
Color mForegroundColor2
Definition: widget2.h:112
virtual unsigned int getRowHeight() const
Definition: listbox.h:155
Rect mDimension
Definition: widget.h:1100

◆ getListModel()

ListModel* ListBox::getListModel ( ) const
inline

Gets the list model used.

Returns
the list model used.
See also
setListModel

Definition at line 193 of file listbox.h.

References mListModel.

Referenced by keyPressed().

194  { return mListModel; }
ListModel * mListModel
Definition: listbox.h:260

◆ getPressedIndex()

int ListBox::getPressedIndex ( ) const
inline

Definition at line 152 of file listbox.h.

References mPressedIndex.

153  { return mPressedIndex; }
int mPressedIndex
Definition: listbox.h:287

◆ getRowHeight()

virtual unsigned int ListBox::getRowHeight ( ) const
inlinevirtual

◆ getSelected()

int ListBox::getSelected ( ) const
inline

◆ getSelectionByMouse()

int ListBox::getSelectionByMouse ( const int  y) const
virtual

Reimplemented in ExtendedListBox.

Definition at line 391 of file listbox.cpp.

References CAST_S32, getRowHeight(), and mPadding.

Referenced by ExtendedListBox::getSelectionByMouse(), mouseDragged(), PopupList::mousePressed(), mousePressed(), PopupList::mouseReleased(), ShopListBox::mouseReleased(), mouseReleased(), mouseReleased1(), and setDistributeMousePressed().

392 {
393  if (y < mPadding)
394  return -1;
395  return (y - mPadding) / CAST_S32(getRowHeight());
396 }
int mPadding
Definition: listbox.h:286
#define CAST_S32
Definition: cast.h:29
virtual unsigned int getRowHeight() const
Definition: listbox.h:155

◆ isWrappingEnabled()

bool ListBox::isWrappingEnabled ( ) const
inline

Checks whether the list box wraps when selecting items with a keyboard.

Wrapping means that the selection of items will be wrapped. That is, if the first item is selected and up is pressed, the last item will get selected. If the last item is selected and down is pressed, the first item will get selected.

Returns
true if wrapping is enabled, fasle otherwise.
See also
setWrappingEnabled

Definition at line 208 of file listbox.h.

References mWrappingEnabled.

209  { return mWrappingEnabled; }
bool mWrappingEnabled
Definition: listbox.h:265

◆ keyPressed()

void ListBox::keyPressed ( KeyEvent event)
virtual

Called if a key is pressed when the widget has keyboard focus. If a key is held down the widget will generate multiple key presses.

Parameters
eventDiscribes the event.

Reimplemented from KeyListener.

Definition at line 245 of file listbox.cpp.

References Widget::distributeActionEvent(), getListModel(), ListModel::getNumberOfElements(), InputAction::GUI_DOWN, InputAction::GUI_END, InputAction::GUI_HOME, InputAction::GUI_SELECT, InputAction::GUI_UP, mSelected, mWrappingEnabled, and setSelected().

Referenced by ServerDialog::keyPressed().

246 {
247  const InputActionT action = event.getActionId();
248  if (action == InputAction::GUI_SELECT)
249  {
251  event.consume();
252  }
253  else if (action == InputAction::GUI_UP)
254  {
255  if (mSelected > 0)
256  {
257  setSelected(mSelected - 1);
258  }
259  else if (mSelected == 0 &&
261  getListModel() != nullptr)
262  {
263  setSelected(getListModel()->getNumberOfElements() - 1);
264  }
265  event.consume();
266  }
267  else if (action == InputAction::GUI_DOWN)
268  {
269  const int num = getListModel()->getNumberOfElements() - 1;
270  if (mSelected < num)
271  setSelected(mSelected + 1);
272  else if (mSelected == num && mWrappingEnabled)
273  setSelected(0);
274  event.consume();
275  }
276  else if (action == InputAction::GUI_HOME)
277  {
278  setSelected(0);
279  event.consume();
280  }
281  else if (action == InputAction::GUI_END && (getListModel() != nullptr))
282  {
283  setSelected(getListModel()->getNumberOfElements() - 1);
284  event.consume();
285  }
286 }
int mSelected
Definition: listbox.h:255
void setSelected(const int selected)
Definition: listbox.cpp:398
ListModel * getListModel() const
Definition: listbox.h:193
virtual int getNumberOfElements()=0
void distributeActionEvent()
Definition: widget.cpp:492
InputAction ::T InputActionT
Definition: inputaction.h:714
bool mWrappingEnabled
Definition: listbox.h:265

◆ logic()

void ListBox::logic ( )
virtual

Called for all widgets in the gui each time Gui::logic is called. You can do logic stuff here like playing an animation.

See also
Gui::logic

Reimplemented from Widget.

Definition at line 384 of file listbox.cpp.

References adjustSize(), BLOCK_END, and BLOCK_START.

Referenced by setDistributeMousePressed().

385 {
386  BLOCK_START("ListBox::logic")
387  adjustSize();
389 }
#define BLOCK_START(name)
Definition: perfomance.h:78
#define BLOCK_END(name)
Definition: perfomance.h:79
virtual void adjustSize()
Definition: listbox.cpp:373
void logic()
Definition: listbox.cpp:384

◆ mouseDragged()

void ListBox::mouseDragged ( MouseEvent event)
virtual

Called when the mouse has moved and the mouse has previously been pressed on the widget.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Reimplemented in SkillListBox.

Definition at line 354 of file listbox.cpp.

References MouseEvent::getButton(), getRowHeight(), getSelectionByMouse(), MouseEvent::getY(), MouseButton::LEFT, and setSelected().

Referenced by SkillListBox::mouseDragged(), and mouseReleased().

355 {
356  if (event.getButton() != MouseButton::LEFT || getRowHeight() == 0)
357  return;
358 
359  // Make list selection update on drag, but guard against negative y
360  if (getRowHeight() != 0u)
361  setSelected(std::max(0, getSelectionByMouse(event.getY())));
362 }
void setSelected(const int selected)
Definition: listbox.cpp:398
MouseButtonT getButton() const
Definition: mouseevent.h:115
virtual int getSelectionByMouse(const int y) const
Definition: listbox.cpp:391
int getY() const
Definition: mouseevent.h:137
virtual unsigned int getRowHeight() const
Definition: listbox.h:155

◆ mousePressed()

void ListBox::mousePressed ( MouseEvent event)
virtual

Called when a mouse button has been pressed on the widget area.

NOTE: A mouse press is NOT equal to a mouse click. Use mouseClickMessage to check for mouse clicks.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Reimplemented in SkillListBox.

Definition at line 303 of file listbox.cpp.

References getSelectionByMouse(), MouseEvent::getY(), Widget::mMouseConsume, and mPressedIndex.

Referenced by SkillListBox::mousePressed().

304 {
306  if (mMouseConsume && mPressedIndex != -1)
307  event.consume();
308 }
virtual int getSelectionByMouse(const int y) const
Definition: listbox.cpp:391
bool mMouseConsume
Definition: widget.h:1161
int mPressedIndex
Definition: listbox.h:287
int getY() const
Definition: mouseevent.h:137

◆ mouseReleased()

void ListBox::mouseReleased ( MouseEvent event)
virtual

Called when a mouse button has been released on the widget area.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Reimplemented in SkillListBox, and ShopListBox.

Definition at line 310 of file listbox.cpp.

References MouseEvent::getClickCount(), getSelectionByMouse(), MouseEvent::getY(), gui, mDistributeMousePressed, mOldSelected, mouseDragged(), mouseReleased1(), mPressedIndex, mSelected, and Gui::resetClickCount().

Referenced by ShopListBox::mouseReleased(), and SkillListBox::mouseReleased().

311 {
312  if (mPressedIndex != getSelectionByMouse(event.getY()))
313  return;
314 
316  {
317  mouseReleased1(event);
318  }
319  else
320  {
321  switch (event.getClickCount())
322  {
323  case 1:
324  mouseDragged(event);
326  break;
327  case 2:
328  if (gui != nullptr)
329  gui->resetClickCount();
330  if (mOldSelected == mSelected)
331  mouseReleased1(event);
332  else
333  mouseDragged(event);
335  break;
336  default:
337  mouseDragged(event);
339  break;
340  }
341  }
342  mPressedIndex = -2;
343 }
int mSelected
Definition: listbox.h:255
Gui * gui
Definition: gui.cpp:110
void mouseDragged(MouseEvent &event)
Definition: listbox.cpp:354
virtual int getSelectionByMouse(const int y) const
Definition: listbox.cpp:391
void mouseReleased1(const MouseEvent &event)
Definition: listbox.cpp:345
void resetClickCount()
Definition: gui.cpp:978
int mPressedIndex
Definition: listbox.h:287
int getY() const
Definition: mouseevent.h:137
int mOldSelected
Definition: listbox.h:285
int getClickCount() const
Definition: mouseevent.h:146
bool mDistributeMousePressed
Definition: listbox.h:292

◆ mouseReleased1()

void ListBox::mouseReleased1 ( const MouseEvent event)

Definition at line 345 of file listbox.cpp.

References Widget::distributeActionEvent(), MouseEvent::getButton(), getSelectionByMouse(), MouseEvent::getY(), MouseButton::LEFT, and setSelected().

Referenced by mouseReleased().

346 {
347  if (event.getButton() == MouseButton::LEFT)
348  {
349  setSelected(std::max(0, getSelectionByMouse(event.getY())));
351  }
352 }
void setSelected(const int selected)
Definition: listbox.cpp:398
MouseButtonT getButton() const
Definition: mouseevent.h:115
void distributeActionEvent()
Definition: widget.cpp:492
virtual int getSelectionByMouse(const int y) const
Definition: listbox.cpp:391
int getY() const
Definition: mouseevent.h:137

◆ mouseWheelMovedDown()

void ListBox::mouseWheelMovedDown ( MouseEvent event)
virtual

Called when the mouse wheel has moved down on the widget area.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 299 of file listbox.cpp.

300 {
301 }

◆ mouseWheelMovedUp()

void ListBox::mouseWheelMovedUp ( MouseEvent event)
virtual

Called when the mouse wheel has moved up on the widget area.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 295 of file listbox.cpp.

296 {
297 }

◆ postInit()

void ListBox::postInit ( )
virtual

Reimplemented from Widget.

Definition at line 139 of file listbox.cpp.

References adjustSize().

140 {
141  adjustSize();
142 }
virtual void adjustSize()
Definition: listbox.cpp:373

◆ refocus()

void ListBox::refocus ( )

Definition at line 364 of file listbox.cpp.

References Widget::isFocusable(), Widget::mFocusHandler, and FocusHandler::requestFocus().

Referenced by NpcDialog::refocus().

365 {
366  if (mFocusHandler == nullptr)
367  return;
368 
369  if (isFocusable())
371 }
FocusHandler * mFocusHandler
Definition: widget.h:1115
void requestFocus(const Widget *const widget)
bool isFocusable() const
Definition: widget.cpp:198

◆ removeSelectionListener()

void ListBox::removeSelectionListener ( SelectionListener *const  selectionListener)

Removes a selection listener from the list box.

Parameters
selectionListenerThe selection listener to remove.

Definition at line 439 of file listbox.cpp.

References mSelectionListeners.

Referenced by setWrappingEnabled().

441 {
442  mSelectionListeners.remove(selectionListener);
443 }
SelectionListenerList mSelectionListeners
Definition: listbox.h:275

◆ safeDraw()

void ListBox::safeDraw ( Graphics *const  graphics)
virtual

Implements Widget.

Reimplemented in SkillListBox, ServersListBox, and ShopListBox.

Definition at line 288 of file listbox.cpp.

References draw().

289 {
290  ListBox::draw(graphics);
291 }
void draw(Graphics *const graphics)
Definition: listbox.cpp:162

◆ setCenter()

void ListBox::setCenter ( const bool  b)
inline

Definition at line 149 of file listbox.h.

References mCenterText.

Referenced by EmoteWindow::postInit().

150  { mCenterText = b; }
bool mCenterText
Definition: listbox.h:293

◆ setDistributeMousePressed()

void ListBox::setDistributeMousePressed ( const bool  b)
inline

◆ setListModel()

void ListBox::setListModel ( ListModel listModel)

Sets the list model to use.

Parameters
listModelthe list model to use.
See also
getListModel

Definition at line 427 of file listbox.cpp.

References adjustSize(), mListModel, and mSelected.

Referenced by getSelected(), and PopupList::setListModel().

428 {
429  mSelected = -1;
430  mListModel = listModel;
431  adjustSize();
432 }
int mSelected
Definition: listbox.h:255
virtual void adjustSize()
Definition: listbox.cpp:373
ListModel * mListModel
Definition: listbox.h:260

◆ setRowHeight()

void ListBox::setRowHeight ( unsigned int  n)
inline

Definition at line 158 of file listbox.h.

References mRowHeight.

Referenced by NpcDialog::NpcDialog(), and QuestsWindow::QuestsWindow().

159  { mRowHeight = n; }
unsigned int mRowHeight
Definition: listbox.h:288

◆ setSelected()

void ListBox::setSelected ( const int  selected)

Sets the selected item. The selected item is represented by an index from the list model.

Parameters
selectedthe selected item as an index from the list model.
See also
getSelected

Definition at line 398 of file listbox.cpp.

References CAST_S32, distributeValueChangedEvent(), ListModel::getNumberOfElements(), getRowHeight(), Rect::height, mListModel, mSelected, Widget::showPart(), and Rect::y.

Referenced by Setup_Input::action(), ServerDialog::action(), NpcDialog::buildLayout(), Setup_Video::cancel(), MailWindow::clear(), EmoteWindow::clearFont(), EmoteWindow::clearText(), getSelected(), ServerDialog::keyPressed(), keyPressed(), ServerDialog::loadServers(), mouseDragged(), AvatarListBox::mousePressed(), ShopListBox::mouseReleased(), mouseReleased1(), NpcDialog::move(), NpcDialog::parseListItems(), ServerDialog::postInit(), QuestsWindow::rebuild(), SellDialog::reset(), BuyDialog::reset(), ColorPage::resetAction(), QuestsWindow::selectQuest(), NpcSellDialog::sellOneItem(), PopupList::setSelected(), Setup_Video::Setup_Video(), MailWindow::viewNext(), MailWindow::viewPrev(), and WorldSelectDialog::WorldSelectDialog().

399 {
400  if (mListModel == nullptr)
401  {
402  mSelected = -1;
403  }
404  else
405  {
406  if (selected < 0)
407  mSelected = -1;
408  else if (selected >= mListModel->getNumberOfElements())
410  else
411  mSelected = selected;
412  }
413 
414  Rect scroll;
415 
416  if (mSelected < 0)
417  scroll.y = 0;
418  else
419  scroll.y = CAST_S32(getRowHeight()) * mSelected;
420 
421  scroll.height = CAST_S32(getRowHeight());
422  showPart(scroll);
423 
425 }
int mSelected
Definition: listbox.h:255
Definition: rect.h:72
virtual int getNumberOfElements()=0
ListModel * mListModel
Definition: listbox.h:260
#define CAST_S32
Definition: cast.h:29
int y
Definition: rect.h:213
int height
Definition: rect.h:223
void distributeValueChangedEvent()
Definition: listbox.cpp:445
virtual void showPart(const Rect &rectangle)
Definition: widget.cpp:510
virtual unsigned int getRowHeight() const
Definition: listbox.h:155

◆ setWrappingEnabled()

void ListBox::setWrappingEnabled ( const bool  wrappingEnabled)
inline

Sets the list box to wrap or not when selecting items with a keyboard.

Wrapping means that the selection of items will be wrapped. That is, if the first item is selected and up is pressed, the last item will get selected. If the last item is selected and down is pressed, the first item will get selected.

See also
isWrappingEnabled

Definition at line 222 of file listbox.h.

References addSelectionListener(), distributeValueChangedEvent(), mWrappingEnabled, and removeSelectionListener().

Referenced by NpcDialog::NpcDialog().

223  { mWrappingEnabled = wrappingEnabled; }
bool mWrappingEnabled
Definition: listbox.h:265

◆ updateAlpha()

void ListBox::updateAlpha ( )

Update the alpha value to the graphic components.

Definition at line 153 of file listbox.cpp.

References Theme::getMinimumOpacity(), Settings::guiAlpha, mAlpha, settings, and theme.

Referenced by ColorPage::draw(), AvatarListBox::draw(), ExtendedListBox::draw(), ServersListBox::draw(), SkillListBox::draw(), draw(), and AvatarListBox::safeDraw().

154 {
155  const float alpha = std::max(settings.guiAlpha,
157 
158  if (mAlpha != alpha)
159  mAlpha = alpha;
160 }
float getMinimumOpacity() const
Definition: theme.h:123
Settings settings
Definition: settings.cpp:31
static float mAlpha
Definition: listbox.h:291
Theme * theme
Definition: theme.cpp:61
float guiAlpha
Definition: settings.h:129

Field Documentation

◆ mAlpha

float ListBox::mAlpha = 1.0
staticprotected

◆ mCenterText

bool ListBox::mCenterText
protected

Definition at line 293 of file listbox.h.

Referenced by draw(), and setCenter().

◆ mDistributeMousePressed

bool ListBox::mDistributeMousePressed
protected

Definition at line 292 of file listbox.h.

Referenced by mouseReleased(), and setDistributeMousePressed().

◆ mForegroundSelectedColor

Color ListBox::mForegroundSelectedColor
protected

Definition at line 283 of file listbox.h.

Referenced by ExtendedListBox::draw(), ServersListBox::draw(), ShopListBox::draw(), and draw().

◆ mForegroundSelectedColor2

Color ListBox::mForegroundSelectedColor2
protected

Definition at line 284 of file listbox.h.

Referenced by ExtendedListBox::draw(), ServersListBox::draw(), ShopListBox::draw(), and draw().

◆ mHighlightColor

Color ListBox::mHighlightColor
protected

◆ mItemPadding

int ListBox::mItemPadding
protected

Definition at line 289 of file listbox.h.

Referenced by ColorPage::ColorPage(), draw(), and ListBox().

◆ mListModel

ListModel* ListBox::mListModel
protected

◆ mOldSelected

int ListBox::mOldSelected
protected

Definition at line 285 of file listbox.h.

Referenced by mouseReleased().

◆ mPadding

int ListBox::mPadding
protected

◆ mPressedIndex

int ListBox::mPressedIndex
protected

Definition at line 287 of file listbox.h.

Referenced by getPressedIndex(), mousePressed(), and mouseReleased().

◆ mRowHeight

unsigned int ListBox::mRowHeight
protected

◆ mSelected

int ListBox::mSelected
protected

◆ mSelectionListeners

SelectionListenerList ListBox::mSelectionListeners
protected

The selection listeners of the list box.

Definition at line 275 of file listbox.h.

Referenced by addSelectionListener(), distributeValueChangedEvent(), and removeSelectionListener().

◆ mSkin

Skin* ListBox::mSkin
protected

Definition at line 290 of file listbox.h.

Referenced by ColorPage::ColorPage(), ListBox(), and ~ListBox().

◆ mWrappingEnabled

bool ListBox::mWrappingEnabled
protected

True if wrapping is enabled, false otherwise.

Definition at line 265 of file listbox.h.

Referenced by isWrappingEnabled(), keyPressed(), and setWrappingEnabled().


The documentation for this class was generated from the following files: