ManaPlus
sliderlist.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus 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/sliderlist.h"
23 
24 #include "gui/gui.h"
25 
26 #include "gui/fonts/font.h"
27 
28 #include "gui/models/listmodel.h"
29 
30 #include "gui/widgets/button.h"
31 #include "gui/widgets/label.h"
32 
33 #include "debug.h"
34 
35 static const int buttonWidth = 27;
36 static const int buttonSpace = 30;
37 static const int sliderHeight = 30;
38 
39 SliderList::SliderList(const Widget2 *const widget,
40  ListModel *const listModel) :
41  Container(widget),
43  MouseListener(),
44  mButtons(),
45  mLabel(new Label(this)),
46  mListModel(listModel),
47  mPrevEventId(),
48  mNextEventId(),
49  mOldWidth(0),
50  mSelectedIndex(0)
51 {
52  mAllowLogic = false;
54 }
55 
57  const std::string &eventId)
58 {
59  mPrevEventId = eventId + "_prev";
60  mNextEventId = eventId + "_next";
61 
62  mButtons[0] = new Button(this,
63  "<",
66  this);
67  mButtons[1] = new Button(this,
68  ">",
71  this);
72 
73  add(mButtons[0]);
74  add(mLabel);
75  add(mButtons[1]);
76 
77  if (!eventId.empty())
78  setActionEventId(eventId);
79 
80  if (listener != nullptr)
82 
83  updateLabel();
84  addMouseListener(this);
85 }
86 
88 {
89 }
90 
92 {
93  mButtons[0]->updateAlpha();
94  mButtons[1]->updateAlpha();
95 }
96 
98 {
100  event.consume();
101 }
102 
104 {
106  event.consume();
107 }
108 
110 {
111  const int width = getWidth();
112 
114  mLabel->setWidth(width - buttonSpace * 2);
115  mButtons[1]->setPosition(width - buttonSpace + 3, 0);
117  updateLabel();
118 }
119 
120 void SliderList::draw(Graphics *const graphics)
121 {
122  BLOCK_START("SliderList::draw")
123  const int width = mDimension.width;
124  if (mOldWidth != width)
125  {
126  resize();
127  mOldWidth = width;
128  }
129  Container::draw(graphics);
130  BLOCK_END("SliderList::draw")
131 }
132 
133 void SliderList::safeDraw(Graphics *const graphics)
134 {
135  BLOCK_START("SliderList::draw")
136  const int width = mDimension.width;
137  if (mOldWidth != width)
138  {
139  resize();
140  mOldWidth = width;
141  }
142  Container::draw(graphics);
143  BLOCK_END("SliderList::draw")
144 }
145 
147 {
148  if ((mListModel == nullptr) || mSelectedIndex < 0
150  {
151  return;
152  }
153 
155  mLabel->adjustSize();
156  const int space = mDimension.width - buttonSpace * 2;
157  const int labelWidth = mLabel->getWidth();
158  int labelY = (mDimension.height - mLabel->getHeight()) / 2;
159  if (labelY < 0)
160  labelY = 0;
161 
162  if (space < 0 || space < labelWidth)
163  mLabel->setPosition(buttonSpace, labelY);
164  else
165  mLabel->setPosition(buttonSpace + (space - labelWidth) / 2, labelY);
166 }
167 
168 void SliderList::action(const ActionEvent &event)
169 {
170  if (mListModel == nullptr)
171  return;
172 
173  const std::string &eventId = event.getId();
174  if (eventId == mPrevEventId)
175  {
176  mSelectedIndex --;
177  if (mSelectedIndex < 0)
179  }
180  else if (eventId == mNextEventId)
181  {
182  mSelectedIndex ++;
184  mSelectedIndex = 0;
185  }
186  updateLabel();
188 }
189 
190 void SliderList::setSelectedString(const std::string &str)
191 {
192  if (mListModel == nullptr)
193  return;
194 
195  for (int f = 0; f < mListModel->getNumberOfElements(); f ++)
196  {
197  if (mListModel->getElementAt(f) == str)
198  {
199  setSelected(f);
200  break;
201  }
202  }
203 }
204 
205 std::string SliderList::getSelectedString() const
206 {
207  if (mListModel == nullptr)
208  return std::string();
209 
211 }
212 
213 void SliderList::setSelected(const int idx)
214 {
215  if (mListModel == nullptr)
216  return;
217 
218  mSelectedIndex = idx;
219  const int num = mListModel->getNumberOfElements();
220  if (mSelectedIndex >= num)
221  mSelectedIndex = 0;
222  if (mSelectedIndex < 0)
223  mSelectedIndex = num - 1;
224  updateLabel();
225 }
226 
228 {
229  setWidth(getMaxLabelWidth() + 60);
230  updateLabel();
231 }
232 
234 {
235  if ((mListModel == nullptr) || (gui == nullptr))
236  return 1;
237 
238  int maxWidth = 0;
239  const Font *const font = getFont();
240 
241  const int num = mListModel->getNumberOfElements();
242  for (int f = 0; f < num; f ++)
243  {
244  const int w = font->getWidth(mListModel->getElementAt(f));
245  if (w > maxWidth)
246  maxWidth = w;
247  }
248 
249  return maxWidth;
250 }
const std::string BUTTON_SKIN
Definition: button.h:89
virtual void add(Widget *const widget)
void draw(Graphics *const graphics)
Definition: button.h:102
void updateAlpha()
Definition: button.cpp:418
Definition: font.h:90
int getWidth(const std::string &text) const
Definition: font.cpp:334
Definition: label.h:91
void adjustSize()
Definition: label.cpp:200
void setCaption(const std::string &caption)
Definition: label.cpp:264
virtual std::string getElementAt(int i)=0
virtual int getNumberOfElements()=0
int width
Definition: rect.h:219
int height
Definition: rect.h:224
int getMaxLabelWidth() const
Definition: sliderlist.cpp:233
void resize()
Definition: sliderlist.cpp:109
void action(const ActionEvent &event)
Definition: sliderlist.cpp:168
void mouseWheelMovedUp(MouseEvent &event)
Definition: sliderlist.cpp:97
int mSelectedIndex
Definition: sliderlist.h:87
std::string mPrevEventId
Definition: sliderlist.h:84
void safeDraw(Graphics *const graphics)
Definition: sliderlist.cpp:133
void mouseWheelMovedDown(MouseEvent &event)
Definition: sliderlist.cpp:103
void setSelectedString(const std::string &str)
Definition: sliderlist.cpp:190
Label * mLabel
Definition: sliderlist.h:82
std::string mNextEventId
Definition: sliderlist.h:85
Button * mButtons[2]
Definition: sliderlist.h:81
void updateLabel()
Definition: sliderlist.cpp:146
void setSelected(const int idx)
Definition: sliderlist.cpp:213
SliderList(const Widget2 *const widget, ListModel *const listModel)
Definition: sliderlist.cpp:39
void adjustSize()
Definition: sliderlist.cpp:227
std::string getSelectedString() const
Definition: sliderlist.cpp:205
void draw(Graphics *const graphics)
Definition: sliderlist.cpp:120
int mOldWidth
Definition: sliderlist.h:86
void updateAlpha()
Definition: sliderlist.cpp:91
void postInit2(ActionListener *const listener, const std::string &eventId)
Definition: sliderlist.cpp:56
ListModel * mListModel
Definition: sliderlist.h:83
void setWidth(const int width)
Definition: widget.cpp:133
void distributeActionEvent()
Definition: widget.cpp:493
Rect mDimension
Definition: widget.h:1101
bool mAllowLogic
Definition: widget.h:1160
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
void setHeight(const int height)
Definition: widget.cpp:140
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
Font * getFont() const
Definition: widget.cpp:331
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
void setPosition(const int x, const int y)
Definition: widget.cpp:161
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
#define new
Definition: debug_new.h:147
Gui * gui
Definition: gui.cpp:111
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
static const int buttonSpace
Definition: sliderlist.cpp:36
static const int buttonWidth
Definition: sliderlist.cpp:35
static const int sliderHeight
Definition: sliderlist.cpp:37