SuperTuxKart
Loading...
Searching...
No Matches
spinner_widget.hpp
1// SuperTuxKart - a fun racing game with go-kart
2// Copyright (C) 2009-2015 Marianne Gagnon
3//
4// This program is free software; you can redistribute it and/or
5// modify it under the terms of the GNU General Public License
6// as published by the Free Software Foundation; either version 3
7// of the License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19
20#ifndef HEADER_SPINNER_HPP
21#define HEADER_SPINNER_HPP
22
23#include <irrString.h>
24#include <functional>
25namespace irr
26{
27 namespace video { class ITexture; }
28}
29
30#include "guiengine/widget.hpp"
31#include "utils/leak_check.hpp"
32#include "utils/ptr_vector.hpp"
33
34namespace GUIEngine
35{
36
40 class SpinnerWidget : public Widget
41 {
42 public:
44 {
45 public:
46 virtual ~ISpinnerConfirmListener() {}
47
52 virtual EventPropagation onSpinnerConfirmed() = 0;
53 };
54
55 protected:
56 std::function<void(SpinnerWidget* spinner)> m_value_updated_callback;
57 ISpinnerConfirmListener* m_listener;
58
59 int m_value, m_min, m_max;
60 float m_step;
61
62 int m_spinner_widget_player_id;
63 bool m_use_background_color;
64
66 std::vector<irr::core::stringw> m_labels;
67
70
75 bool m_gauge;
76
77
80
83
86
89
93 core::stringw m_custom_text;
94
96 virtual EventPropagation transmitEvent(Widget* w,
97 const std::string& originator,
98 const int playerID);
99
101 virtual EventPropagation rightPressed(const int playerID);
102
104 virtual EventPropagation leftPressed(const int playerID);
105
108 virtual int getWidthNeededAroundLabel() const { return 25; }
109
112 virtual int getHeightNeededAroundLabel() const { return 8; }
113
115 irr::video::ITexture* getTexture();
116
117 public:
118
119 LEAK_CHECK()
120
121 SpinnerWidget(const bool gauge=false);
122 virtual ~SpinnerWidget() {}
123 virtual void move(const int x, const int y, const int w, const int h);
124
125 void addLabel(irr::core::stringw label);
126 void clearLabels();
127
128 // next four functions are for background colour behind playername in multikart screen selection
129 void setUseBackgroundColor() {m_use_background_color=true; }
130 bool getUseBackgroundColor() {return m_use_background_color; }
131 void setSpinnerWidgetPlayerID(int playerID) {m_spinner_widget_player_id=playerID;}
132 int getSpinnerWidgetPlayerID() {return m_spinner_widget_player_id; }
133 void unsetUseBackgroundColor() {m_use_background_color=false; }
134
135 void activateSelectedButton();
136 void setSelectedButton(bool right)
137 {
138 if (right)
139 {
140 m_left_selected = false;
141 m_right_selected = true;
142 }
143 else
144 {
145 m_left_selected = true;
146 m_right_selected = false;
147 }
148 }
149 void clearSelected()
150 {
151 m_left_selected = false;
152 m_right_selected = false;
153 }
154 bool isButtonSelected(bool right)
155 {
156 if (right && m_right_selected)
157 return true;
158 else if (!right && m_left_selected)
159 return true;
160 return false;
161 }
162
163 void setListener(ISpinnerConfirmListener* listener) { m_listener = listener; }
164
166 virtual void add();
167
172 void setValue(const int new_value);
173
178 void setFloatValue(const float new_value) { setValue(int(round(new_value/m_step))); }
179
185 void setValue(irr::core::stringw new_value);
186
190 bool isGauge() const { return m_gauge; }
191
195 bool isColorSlider() const { return m_color_slider; }
196
201 int getValue() const { return m_value; }
202
207 float getFloatValue() const { return m_value*m_step; }
208
213 irr::core::stringw getStringValue() const;
214
219 irr::core::stringw getStringValueFromID(unsigned id) const
220 {
221 if (id > m_labels.size())
222 return L"";
223 return m_labels[id];
224 }
225
229 // --------------------------------------------------------------------
231 float getStep() const { return m_step; }
232 // --------------------------------------------------------------------
236 void setStep(float n) { m_step = n; }
237 // --------------------------------------------------------------------
241 // --------------------------------------------------------------------
243 int getMax() const { return m_max; }
244 // --------------------------------------------------------------------
248 void setMax(int n)
249 {
250 m_max = n;
251 if(getValue()>m_max) setValue(m_max);
252 } // setMax
253 // --------------------------------------------------------------------
257 int getMin() const { return m_min; }
258 // --------------------------------------------------------------------
262 void setMin(int n)
263 {
264 m_min = n;
265 if(getValue()<m_min) setValue(m_min);
266 } // setMin
267
268 // --------------------------------------------------------------------
270 virtual void setActive(bool active = true);
271
273 void setCustomText(const core::stringw& text);
274 const core::stringw& getCustomText() const { return m_custom_text; }
275
276 /* Set a spinner with numeric values min <= i <= max, with a precision of defined by step */
277 void setRange(float min, float max, float step);
278 void setRange(int min, int max) { setRange(min, max, 1.0); }
279
280 void onPressed(int x, int y);
281 void doValueUpdatedCallback()
282 {
283 if (m_value_updated_callback)
284 m_value_updated_callback(this);
285 }
286 void setValueUpdatedCallback(
287 std::function<void(SpinnerWidget* spinner)> callback)
288 {
289 m_value_updated_callback = callback;
290 }
291
292 };
293
294}
295
296#endif
Definition: spinner_widget.hpp:44
virtual EventPropagation onSpinnerConfirmed()=0
Invoked when the spinner is selected and "fire" is pressed.
A spinner or gauge widget (to select numbers / percentages).
Definition: spinner_widget.hpp:41
virtual EventPropagation rightPressed(const int playerID)
implementing method from base class Widget
Definition: spinner_widget.cpp:293
std::vector< irr::core::stringw > m_labels
If each value the spinner can take has an associated text, this vector will be non-empty.
Definition: spinner_widget.hpp:66
void setFloatValue(const float new_value)
sets the float value of the spinner
Definition: spinner_widget.hpp:178
virtual void add()
implement method from base class Widget
Definition: spinner_widget.cpp:119
virtual int getWidthNeededAroundLabel() const
When inferring widget size from its label length, this method will be called to if/how much space mus...
Definition: spinner_widget.hpp:108
float getStep() const
Returns the step value.
Definition: spinner_widget.hpp:231
bool m_gauge
Whether this widget is a gauge the behaviour is the same but the look is a bit different,...
Definition: spinner_widget.hpp:75
irr::core::stringw getStringValue() const
retrieve the current value of the spinner
Definition: spinner_widget.cpp:453
int getMax() const
Returns the maximum value.
Definition: spinner_widget.hpp:243
bool m_wrap_around
Whether to wrap back to the first value when going "beyond" the last value.
Definition: spinner_widget.hpp:79
bool m_left_selected
Whether the left arrow is the currently selected one
Definition: spinner_widget.hpp:85
float getFloatValue() const
retrieve the current value of the spinner
Definition: spinner_widget.hpp:207
irr::video::ITexture * getTexture()
Call only if this spinner is graphical.
Definition: spinner_widget.cpp:252
void setMax(int n)
Sets the maximum value for a spinner.
Definition: spinner_widget.hpp:248
virtual void move(const int x, const int y, const int w, const int h)
Call to resize/move the widget.
Definition: spinner_widget.cpp:263
irr::core::stringw getStringValueFromID(unsigned id) const
retrieve the value of the spinner from id
Definition: spinner_widget.hpp:219
int getMin() const
Definition: spinner_widget.hpp:257
virtual void setActive(bool active=true)
Override method from base class Widget.
Definition: spinner_widget.cpp:492
bool isColorSlider() const
Definition: spinner_widget.hpp:195
core::stringw m_custom_text
Keeps track of the custom text in spinner (a text which isn't related to a value) to remember it and ...
Definition: spinner_widget.hpp:93
void setValue(const int new_value)
sets the current value of the spinner
Definition: spinner_widget.cpp:412
void setMin(int n)
Sets the minimum value for a spinner.
Definition: spinner_widget.hpp:262
int getValue() const
retrieve the current value of the spinner
Definition: spinner_widget.hpp:201
void setStep(float n)
Sets the maximum value for a spinner.
Definition: spinner_widget.hpp:236
virtual EventPropagation leftPressed(const int playerID)
implementing method from base class Widget
Definition: spinner_widget.cpp:311
bool isGauge() const
Definition: spinner_widget.hpp:190
bool m_graphical
Whether the value of this spinner is displayed using an icon rather than with a plain label.
Definition: spinner_widget.hpp:69
bool m_color_slider
Whether this widget is a color slider.
Definition: spinner_widget.hpp:82
void setCustomText(const core::stringw &text)
Display custom text in spinner.
Definition: spinner_widget.cpp:511
bool m_right_selected
Whether the right arrow is the currently selected one
Definition: spinner_widget.hpp:88
virtual int getHeightNeededAroundLabel() const
When inferring widget size from its label length, this method will be called to if/how much space mus...
Definition: spinner_widget.hpp:112
virtual EventPropagation transmitEvent(Widget *w, const std::string &originator, const int playerID)
implementing method from base class Widget
Definition: spinner_widget.cpp:355
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:143
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:33