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,
62  "<",
65  this);
66  mButtons[1] = new Button(this,
67  ">",
70  this);
71 
72  add(mButtons[0]);
73  add(mLabel);
74  add(mButtons[1]);
75 
76  if (!eventId.empty())
77  setActionEventId(eventId);
78 
79  if (listener != nullptr)
80  addActionListener(listener);
81 
82  updateLabel();
83  addMouseListener(this);
84 }
85 
87 {
88 }
89 
91 {
92  mButtons[0]->updateAlpha();
93  mButtons[1]->updateAlpha();
94 }
95 
97 {
99  event.consume();
100 }
101 
103 {
105  event.consume();
106 }
107 
109 {
110  const int width = getWidth();
111 
113  mLabel->setWidth(width - buttonSpace * 2);
114  mButtons[1]->setPosition(width - buttonSpace + 3, 0);
116  updateLabel();
117 }
118 
119 void SliderList::draw(Graphics *const graphics)
120 {
121  BLOCK_START("SliderList::draw")
122  const int width = mDimension.width;
123  if (mOldWidth != width)
124  {
125  resize();
126  mOldWidth = width;
127  }
128  Container::draw(graphics);
129  BLOCK_END("SliderList::draw")
130 }
131 
132 void SliderList::safeDraw(Graphics *const graphics)
133 {
134  BLOCK_START("SliderList::draw")
135  const int width = mDimension.width;
136  if (mOldWidth != width)
137  {
138  resize();
139  mOldWidth = width;
140  }
141  Container::draw(graphics);
142  BLOCK_END("SliderList::draw")
143 }
144 
146 {
147  if ((mListModel == nullptr) || mSelectedIndex < 0
149  {
150  return;
151  }
152 
154  mLabel->adjustSize();
155  const int space = mDimension.width - buttonSpace * 2;
156  const int labelWidth = mLabel->getWidth();
157  int labelY = (mDimension.height - mLabel->getHeight()) / 2;
158  if (labelY < 0)
159  labelY = 0;
160 
161  if (space < 0 || space < labelWidth)
162  mLabel->setPosition(buttonSpace, labelY);
163  else
164  mLabel->setPosition(buttonSpace + (space - labelWidth) / 2, labelY);
165 }
166 
167 void SliderList::action(const ActionEvent &event)
168 {
169  if (mListModel == nullptr)
170  return;
171 
172  const std::string &eventId = event.getId();
173  if (eventId == mPrevEventId)
174  {
175  mSelectedIndex --;
176  if (mSelectedIndex < 0)
178  }
179  else if (eventId == mNextEventId)
180  {
181  mSelectedIndex ++;
183  mSelectedIndex = 0;
184  }
185  updateLabel();
187 }
188 
189 void SliderList::setSelectedString(const std::string &str)
190 {
191  if (mListModel == nullptr)
192  return;
193 
194  for (int f = 0; f < mListModel->getNumberOfElements(); f ++)
195  {
196  if (mListModel->getElementAt(f) == str)
197  {
198  setSelected(f);
199  break;
200  }
201  }
202 }
203 
204 std::string SliderList::getSelectedString() const
205 {
206  if (mListModel == nullptr)
207  return std::string();
208 
210 }
211 
212 void SliderList::setSelected(const int idx)
213 {
214  if (mListModel == nullptr)
215  return;
216 
217  mSelectedIndex = idx;
218  const int num = mListModel->getNumberOfElements();
219  if (mSelectedIndex >= num)
220  mSelectedIndex = 0;
221  if (mSelectedIndex < 0)
222  mSelectedIndex = num - 1;
223  updateLabel();
224 }
225 
227 {
228  setWidth(getMaxLabelWidth() + 60);
229  updateLabel();
230 }
231 
233 {
234  if ((mListModel == nullptr) || (gui == nullptr))
235  return 1;
236 
237  int maxWidth = 0;
238  const Font *const font = getFont();
239 
240  const int num = mListModel->getNumberOfElements();
241  for (int f = 0; f < num; f ++)
242  {
243  const int w = font->getWidth(mListModel->getElementAt(f));
244  if (w > maxWidth)
245  maxWidth = w;
246  }
247 
248  return maxWidth;
249 }
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:132
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:226
void setWidth(const int width)
Definition: widget.cpp:132
Gui * gui
Definition: gui.cpp:110
Definition: font.h:88
void resize()
Definition: sliderlist.cpp:108
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:212
#define BLOCK_START(name)
Definition: perfomance.h:78
Definition: button.h:96
#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:167
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:232
Button * mButtons[2]
Definition: sliderlist.h:80
void draw(Graphics *const graphics)
Definition: sliderlist.cpp:119
const std::string BUTTON_SKIN
Definition: button.h:88
void setSelectedString(const std::string &str)
Definition: sliderlist.cpp:189
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
void updateAlpha()
Definition: button.cpp:417
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:90
void updateLabel()
Definition: sliderlist.cpp:145
static const int sliderHeight
Definition: sliderlist.cpp:36
void mouseWheelMovedUp(MouseEvent &event)
Definition: sliderlist.cpp:96
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:102
std::string getSelectedString() const
Definition: sliderlist.cpp:204
Rect mDimension
Definition: widget.h:1100
ListModel * mListModel
Definition: sliderlist.h:82
void adjustSize()
Definition: label.cpp:197