ManaPlus
sliderlist.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2018 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "gui/widgets/sliderlist.h"
22 
23 #include "gui/gui.h"
24 
25 #include "gui/fonts/font.h"
26 
27 #include "gui/models/listmodel.h"
28 
29 #include "gui/widgets/button.h"
30 #include "gui/widgets/label.h"
31 
32 #include "debug.h"
33 
34 static const int buttonWidth = 27;
35 static const int buttonSpace = 30;
36 static const int sliderHeight = 30;
37 
38 SliderList::SliderList(const Widget2 *const widget,
39  ListModel *const listModel) :
40  Container(widget),
42  MouseListener(),
43  mButtons(),
44  mLabel(new Label(this)),
45  mListModel(listModel),
46  mPrevEventId(),
47  mNextEventId(),
48  mOldWidth(0),
49  mSelectedIndex(0)
50 {
51  mAllowLogic = false;
53 }
54 
56  const std::string &eventId)
57 {
58  mPrevEventId = eventId + "_prev";
59  mNextEventId = eventId + "_next";
60 
61  mButtons[0] = new Button(this, "<", mPrevEventId, this);
62  mButtons[1] = new Button(this, ">", mNextEventId, this);
63 
64  add(mButtons[0]);
65  add(mLabel);
66  add(mButtons[1]);
67 
68  if (!eventId.empty())
69  setActionEventId(eventId);
70 
71  if (listener != nullptr)
72  addActionListener(listener);
73 
74  updateLabel();
75  addMouseListener(this);
76 }
77 
79 {
80 }
81 
83 {
85 }
86 
88 {
90  event.consume();
91 }
92 
94 {
96  event.consume();
97 }
98 
100 {
101  const int width = getWidth();
102 
104  mLabel->setWidth(width - buttonSpace * 2);
105  mButtons[1]->setPosition(width - buttonSpace + 3, 0);
107  updateLabel();
108 }
109 
110 void SliderList::draw(Graphics *const graphics)
111 {
112  BLOCK_START("SliderList::draw")
113  const int width = mDimension.width;
114  if (mOldWidth != width)
115  {
116  resize();
117  mOldWidth = width;
118  }
119  Container::draw(graphics);
120  BLOCK_END("SliderList::draw")
121 }
122 
123 void SliderList::safeDraw(Graphics *const graphics)
124 {
125  BLOCK_START("SliderList::draw")
126  const int width = mDimension.width;
127  if (mOldWidth != width)
128  {
129  resize();
130  mOldWidth = width;
131  }
132  Container::draw(graphics);
133  BLOCK_END("SliderList::draw")
134 }
135 
137 {
138  if ((mListModel == nullptr) || mSelectedIndex < 0
140  {
141  return;
142  }
143 
145  mLabel->adjustSize();
146  const int space = mDimension.width - buttonSpace * 2;
147  const int labelWidth = mLabel->getWidth();
148  int labelY = (mDimension.height - mLabel->getHeight()) / 2;
149  if (labelY < 0)
150  labelY = 0;
151 
152  if (space < 0 || space < labelWidth)
153  mLabel->setPosition(buttonSpace, labelY);
154  else
155  mLabel->setPosition(buttonSpace + (space - labelWidth) / 2, labelY);
156 }
157 
158 void SliderList::action(const ActionEvent &event)
159 {
160  if (mListModel == nullptr)
161  return;
162 
163  const std::string &eventId = event.getId();
164  if (eventId == mPrevEventId)
165  {
166  mSelectedIndex --;
167  if (mSelectedIndex < 0)
169  }
170  else if (eventId == mNextEventId)
171  {
172  mSelectedIndex ++;
174  mSelectedIndex = 0;
175  }
176  updateLabel();
178 }
179 
180 void SliderList::setSelectedString(const std::string &str)
181 {
182  if (mListModel == nullptr)
183  return;
184 
185  for (int f = 0; f < mListModel->getNumberOfElements(); f ++)
186  {
187  if (mListModel->getElementAt(f) == str)
188  {
189  setSelected(f);
190  break;
191  }
192  }
193 }
194 
195 std::string SliderList::getSelectedString() const
196 {
197  if (mListModel == nullptr)
198  return std::string();
199 
201 }
202 
203 void SliderList::setSelected(const int idx)
204 {
205  if (mListModel == nullptr)
206  return;
207 
208  mSelectedIndex = idx;
209  const int num = mListModel->getNumberOfElements();
210  if (mSelectedIndex >= num)
211  mSelectedIndex = 0;
212  if (mSelectedIndex < 0)
213  mSelectedIndex = num - 1;
214  updateLabel();
215 }
216 
218 {
219  setWidth(getMaxLabelWidth() + 60);
220  updateLabel();
221 }
222 
224 {
225  if ((mListModel == nullptr) || (gui == nullptr))
226  return 1;
227 
228  int maxWidth = 0;
229  const Font *const font = getFont();
230 
231  const int num = mListModel->getNumberOfElements();
232  for (int f = 0; f < num; f ++)
233  {
234  const int w = font->getWidth(mListModel->getElementAt(f));
235  if (w > maxWidth)
236  maxWidth = w;
237  }
238 
239  return maxWidth;
240 }
static const int buttonWidth
Definition: sliderlist.cpp:34
Font * getFont() const
Definition: widget.cpp:330
std::string mNextEventId
Definition: sliderlist.h:84
void safeDraw(Graphics *const graphics)
Definition: sliderlist.cpp:123
std::string mPrevEventId
Definition: sliderlist.h:83
int width
Definition: rect.h:218
SliderList(const Widget2 *const widget, ListModel *const listModel)
Definition: sliderlist.cpp:38
int getWidth() const
Definition: widget.h:220
void adjustSize()
Definition: sliderlist.cpp:217
void setWidth(const int width)
Definition: widget.cpp:132
Gui * gui
Definition: gui.cpp:110
Definition: font.h:88
void resize()
Definition: sliderlist.cpp:99
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
void postInit2(ActionListener *const listener, const std::string &eventId)
Definition: sliderlist.cpp:55
void setSelected(const int idx)
Definition: sliderlist.cpp:203
#define BLOCK_START(name)
Definition: perfomance.h:78
Definition: button.h:94
#define BLOCK_END(name)
Definition: perfomance.h:79
virtual int getNumberOfElements()=0
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
void action(const ActionEvent &event)
Definition: sliderlist.cpp:158
void draw(Graphics *const graphics)
static const int buttonSpace
Definition: sliderlist.cpp:35
void distributeActionEvent()
Definition: widget.cpp:492
#define new
Definition: debug_new.h:147
int getMaxLabelWidth() const
Definition: sliderlist.cpp:223
Button * mButtons[2]
Definition: sliderlist.h:80
void draw(Graphics *const graphics)
Definition: sliderlist.cpp:110
void setSelectedString(const std::string &str)
Definition: sliderlist.cpp:180
void setCaption(const std::string &caption)
Definition: label.cpp:261
Definition: label.h:87
int mOldWidth
Definition: sliderlist.h:85
virtual void add(Widget *const widget)
bool mAllowLogic
Definition: widget.h:1159
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
void setPosition(const int x, const int y)
Definition: widget.cpp:160
int height
Definition: rect.h:223
static void updateAlpha()
Definition: button.cpp:448
Label * mLabel
Definition: sliderlist.h:81
virtual std::string getElementAt(int i)=0
void setHeight(const int height)
Definition: widget.cpp:139
void updateAlpha()
Definition: sliderlist.cpp:82
void updateLabel()
Definition: sliderlist.cpp:136
static const int sliderHeight
Definition: sliderlist.cpp:36
void mouseWheelMovedUp(MouseEvent &event)
Definition: sliderlist.cpp:87
int getWidth(const std::string &text) const
Definition: font.cpp:333
int mSelectedIndex
Definition: sliderlist.h:86
int getHeight() const
Definition: widget.h:239
void mouseWheelMovedDown(MouseEvent &event)
Definition: sliderlist.cpp:93
std::string getSelectedString() const
Definition: sliderlist.cpp:195
Rect mDimension
Definition: widget.h:1100
ListModel * mListModel
Definition: sliderlist.h:82
void adjustSize()
Definition: label.cpp:197