Mana
Loading...
Searching...
No Matches
listbox.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "gui/widgets/listbox.h"
23
24#include "gui/gui.h"
25#include "gui/sdlinput.h"
26
27#include "resources/theme.h"
28
29#include <guichan/font.hpp>
30#include <guichan/graphics.hpp>
31#include <guichan/key.hpp>
32#include <guichan/listmodel.hpp>
33
34ListBox::ListBox(gcn::ListModel *listModel):
35 gcn::ListBox(listModel)
36{
37}
38
39void ListBox::draw(gcn::Graphics *graphics)
40{
41 if (!mListModel)
42 return;
43
44 graphics->setFont(getFont());
45
46 const int height = getRowHeight();
47
48 // Draw filled rectangle around the selected list element
49 if (mSelected >= 0)
50 {
51 auto highlightColor = Theme::getThemeColor(Theme::HIGHLIGHT);
52 highlightColor.a = gui->getTheme()->getGuiAlpha();
53 graphics->setColor(highlightColor);
54 graphics->fillRectangle(gcn::Rectangle(0, height * mSelected,
55 getWidth(), height));
56 }
57
58 // Draw the list elements
59 for (int i = 0, y = 0; i < mListModel->getNumberOfElements();
60 ++i, y += height)
61 {
62 if (mSelected == i)
64 else
66
67 graphics->drawText(mListModel->getElementAt(i), 1, y);
68 }
69}
70
71void ListBox::keyPressed(gcn::KeyEvent& keyEvent)
72{
73 const gcn::Key key = keyEvent.getKey();
74
75 if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
76 {
77 distributeActionEvent();
78 keyEvent.consume();
79 }
80 else if (key.getValue() == Key::UP)
81 {
82 if (getSelected() > 0)
83 setSelected(mSelected - 1);
84 else if (getSelected() == 0 && mWrappingEnabled)
85 setSelected(getListModel()->getNumberOfElements() - 1);
86 keyEvent.consume();
87 }
88 else if (key.getValue() == Key::DOWN)
89 {
90 if (getSelected() < (getListModel()->getNumberOfElements() - 1))
91 setSelected(mSelected + 1);
92 else if (getSelected() == (getListModel()->getNumberOfElements() - 1) &&
93 mWrappingEnabled)
94 setSelected(0);
95 keyEvent.consume();
96 }
97 else if (key.getValue() == Key::HOME)
98 {
99 setSelected(0);
100 keyEvent.consume();
101 }
102 else if (key.getValue() == Key::END)
103 {
104 setSelected(getListModel()->getNumberOfElements() - 1);
105 keyEvent.consume();
106 }
107}
108
109void ListBox::mousePressed(gcn::MouseEvent &mouseEvent)
110{
111 if (mouseEvent.getButton() != gcn::MouseEvent::LEFT)
112 return;
113
114 int y = std::max(0, mouseEvent.getY());
115 if (y / (int)getRowHeight() < getListModel()->getNumberOfElements())
116 {
117 setSelected(y / getRowHeight());
118 distributeActionEvent();
119 }
120 else
121 {
122 setSelected(-1);
123 }
124}
125
126void ListBox::mouseWheelMovedUp(gcn::MouseEvent &mouseEvent)
127{
128 // Don't do anything on scrollwheel. ScrollArea will deal with that.
129}
130
131void ListBox::mouseWheelMovedDown(gcn::MouseEvent &mouseEvent)
132{
133 // Don't do anything on scrollwheel. ScrollArea will deal with that.
134}
135
136void ListBox::mouseDragged(gcn::MouseEvent &event)
137{
138 if (event.getButton() != gcn::MouseEvent::LEFT)
139 return;
140
141 // Make list selection update on drag, but guard against negative y
142 int y = std::max(0, event.getY());
143 if (y / (int)getRowHeight() < getListModel()->getNumberOfElements())
144 setSelected(y / getRowHeight());
145}
void drawText(const std::string &text, int x, int y, gcn::Graphics::Alignment alignment, const gcn::Color &color, gcn::Font *font, bool outline=false, bool shadow=false, const std::optional< gcn::Color > &outlineColor={}, const std::optional< gcn::Color > &shadowColor={})
Definition graphics.cpp:176
void setColor(const gcn::Color &color) override
Definition graphics.h:255
Theme * getTheme() const
The global GUI theme.
Definition gui.h:138
A list box, meant to be used inside a scroll area.
Definition listbox.h:36
ListBox(gcn::ListModel *listModel)
Definition listbox.cpp:34
void mouseDragged(gcn::MouseEvent &event) override
Definition listbox.cpp:136
void draw(gcn::Graphics *graphics) override
Draws the list box.
Definition listbox.cpp:39
void keyPressed(gcn::KeyEvent &keyEvent) override
Definition listbox.cpp:71
void mouseWheelMovedUp(gcn::MouseEvent &mouseEvent) override
Definition listbox.cpp:126
void mouseWheelMovedDown(gcn::MouseEvent &mouseEvent) override
Definition listbox.cpp:131
void mousePressed(gcn::MouseEvent &mouseEvent) override
Definition listbox.cpp:109
int getGuiAlpha() const
Get the current GUI alpha value.
Definition theme.h:321
static const gcn::Color & getThemeColor(int type)
Gets the color associated with the type in the default palette (0).
Definition theme.cpp:313
@ HIGHLIGHT_TEXT
Definition theme.h:232
@ HIGHLIGHT
Definition theme.h:231
@ TEXT
Definition theme.h:214
Graphics * graphics
Definition client.cpp:104
Gui * gui
The GUI system.
Definition gui.cpp:50
@ ENTER
Definition sdlinput.h:78
@ SPACE
Definition sdlinput.h:76
@ HOME
Definition sdlinput.h:91
@ UP
Definition sdlinput.h:121
@ DOWN
Definition sdlinput.h:122
@ END
Definition sdlinput.h:94