SuperTuxKart
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>
25 namespace irr
26 {
27  namespace video { class ITexture; }
28  namespace gui { class IGUIImage; }
29 }
30 
31 #include "guiengine/widget.hpp"
32 #include "utils/leak_check.hpp"
33 #include "utils/ptr_vector.hpp"
34 
35 #include <rect.h>
36 
37 namespace GUIEngine
38 {
39 
43  class SpinnerWidget : public Widget
44  {
45  public:
47  {
48  public:
49  virtual ~ISpinnerConfirmListener() {}
50 
55  virtual EventPropagation onSpinnerConfirmed() = 0;
56  };
57 
58  private:
59  std::function<void(SpinnerWidget* spinner)> m_value_updated_callback;
60  ISpinnerConfirmListener* m_listener;
61 
62  int m_value, m_min, m_max;
63  float m_step;
64 
65  bool m_incorrect;
66  irr::gui::IGUIImage* m_red_mark_widget;
67 
68  int m_spinner_widget_player_id;
69  bool m_use_background_color;
70 
72  std::vector<irr::core::stringw> m_labels;
73 
76 
81  bool m_gauge;
82 
83 
86 
89 
92 
95 
97  core::rect<s32> m_left_arrow;
98 
102  core::stringw m_custom_text;
103 
105  virtual EventPropagation transmitEvent(Widget* w,
106  const std::string& originator,
107  const int playerID);
108 
110  virtual EventPropagation rightPressed(const int playerID);
111 
113  virtual EventPropagation leftPressed(const int playerID);
114 
117  virtual int getWidthNeededAroundLabel() const { return 25; }
118 
121  virtual int getHeightNeededAroundLabel() const { return 8; }
122 
124  irr::video::ITexture* getTexture();
125 
127  void resizeLabel();
128 
129  public:
130 
131  LEAK_CHECK()
132 
133  SpinnerWidget(const bool gauge=false);
134  virtual ~SpinnerWidget() {}
135  virtual void resize();
136 
137  void addLabel(irr::core::stringw label);
138  void clearLabels();
139 
140  // next four functions are for background colour behind playername in multikart screen selection
141  void setUseBackgroundColor() { m_use_background_color=true; }
142  bool getUseBackgroundColor() const { return m_use_background_color; }
143  void setSpinnerWidgetPlayerID(int playerID) { m_spinner_widget_player_id=playerID; }
144  int getSpinnerWidgetPlayerID() const { return m_spinner_widget_player_id; }
145  void unsetUseBackgroundColor() { m_use_background_color=false; }
146 
147  void activateSelectedButton();
148  void setSelectedButton(bool right)
149  {
150  if (right)
151  {
152  m_left_selected = false;
153  m_right_selected = true;
154  }
155  else
156  {
157  m_left_selected = true;
158  m_right_selected = false;
159  }
160  }
161  void clearSelected()
162  {
163  m_left_selected = false;
164  m_right_selected = false;
165  }
166  bool isButtonSelected(bool right)
167  {
168  if (right && m_right_selected)
169  return true;
170  else if (!right && m_left_selected)
171  return true;
172  return false;
173  }
174 
175  void setListener(ISpinnerConfirmListener* listener) { m_listener = listener; }
176 
178  virtual void add();
179 
184  void setValue(const int new_value);
185 
190  void setFloatValue(const float new_value) { setValue(int(round(new_value/m_step))); }
191 
197  void setValue(irr::core::stringw new_value);
198 
202  bool isGauge() const { return m_gauge; }
203 
207  bool isColorSlider() const { return m_color_slider; }
208 
213  int getValue() const { return m_value; }
214 
219  float getFloatValue() const { return m_value*m_step; }
220 
225  irr::core::stringw getStringValue() const;
226 
231  irr::core::stringw getStringValueFromID(unsigned id) const
232  {
233  if (id > m_labels.size())
234  return L"";
235  return m_labels[id];
236  }
237 
241  // --------------------------------------------------------------------
243  float getStep() const { return m_step; }
244  // --------------------------------------------------------------------
248  void setStep(float n) { m_step = n; }
249  // --------------------------------------------------------------------
253  // --------------------------------------------------------------------
255  int getMax() const { return m_max; }
256  // --------------------------------------------------------------------
260  void setMax(int n)
261  {
262  m_max = n;
263  if(getValue()>m_max) setValue(m_max);
264  } // setMax
265  // --------------------------------------------------------------------
269  int getMin() const { return m_min; }
270  // --------------------------------------------------------------------
274  void setMin(int n)
275  {
276  m_min = n;
277  if(getValue()<m_min) setValue(m_min);
278  } // setMin
279 
280  core::rect<s32> getLeftArrow() const { return m_left_arrow; }
281 
282  // ------------------------------------------------------------------------
284  void markAsIncorrect();
285  // ------------------------------------------------------------------------
287  void markAsCorrect();
288 
289  // --------------------------------------------------------------------
291  virtual void setActive(bool active = true);
292 
294  void setCustomText(const core::stringw& text);
295  const core::stringw& getCustomText() const { return m_custom_text; }
296 
297  /* Set a spinner with numeric values min <= i <= max, with a precision of defined by step */
298  void setRange(float min, float max, float step);
299  void setRange(int min, int max) { setRange(min, max, 1.0); }
300 
301  void onPressed(int x, int y);
302  void doValueUpdatedCallback()
303  {
304  if (m_value_updated_callback)
305  m_value_updated_callback(this);
306  }
307  void setValueUpdatedCallback(
308  std::function<void(SpinnerWidget* spinner)> callback)
309  {
310  m_value_updated_callback = callback;
311  }
312  virtual void elementRemoved()
313  {
314  m_incorrect = false;
315  m_red_mark_widget = NULL;
317  }
318 
319  };
320 
321 }
322 
323 #endif
Definition: spinner_widget.hpp:47
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:44
void resizeLabel()
Pick the appropriate font size to display the current spinner label.
Definition: spinner_widget.cpp:321
virtual EventPropagation rightPressed(const int playerID)
implementing method from base class Widget
Definition: spinner_widget.cpp:342
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:72
void setFloatValue(const float new_value)
sets the float value of the spinner
Definition: spinner_widget.hpp:190
virtual void add()
implement method from base class Widget
Definition: spinner_widget.cpp:159
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:117
void markAsIncorrect()
Add a red mark on the spinner to mean "invalid choice".
Definition: spinner_widget.cpp:115
virtual void elementRemoved()
Called when irrLicht widgets cleared.
Definition: spinner_widget.hpp:312
float getStep() const
Returns the step value.
Definition: spinner_widget.hpp:243
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:81
irr::core::stringw getStringValue() const
retrieve the current value of the spinner
Definition: spinner_widget.cpp:506
int getMax() const
Returns the maximum value.
Definition: spinner_widget.hpp:255
bool m_wrap_around
Whether to wrap back to the first value when going "beyond" the last value.
Definition: spinner_widget.hpp:85
core::rect< s32 > m_left_arrow
\the arrow of the spinner
Definition: spinner_widget.hpp:97
bool m_left_selected
Whether the left arrow is the currently selected one
Definition: spinner_widget.hpp:91
float getFloatValue() const
retrieve the current value of the spinner
Definition: spinner_widget.hpp:219
irr::video::ITexture * getTexture()
Call only if this spinner is graphical.
Definition: spinner_widget.cpp:278
void setMax(int n)
Sets the maximum value for a spinner.
Definition: spinner_widget.hpp:260
irr::core::stringw getStringValueFromID(unsigned id) const
retrieve the value of the spinner from id
Definition: spinner_widget.hpp:231
int getMin() const
Definition: spinner_widget.hpp:269
virtual void setActive(bool active=true)
Override method from base class Widget.
Definition: spinner_widget.cpp:542
bool isColorSlider() const
Definition: spinner_widget.hpp:207
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:102
void setValue(const int new_value)
sets the current value of the spinner
Definition: spinner_widget.cpp:464
void setMin(int n)
Sets the minimum value for a spinner.
Definition: spinner_widget.hpp:274
int getValue() const
retrieve the current value of the spinner
Definition: spinner_widget.hpp:213
void setStep(float n)
Sets the maximum value for a spinner.
Definition: spinner_widget.hpp:248
virtual EventPropagation leftPressed(const int playerID)
implementing method from base class Widget
Definition: spinner_widget.cpp:360
void markAsCorrect()
Remove any red mark set with 'markAsIncorrect'.
Definition: spinner_widget.cpp:138
bool isGauge() const
Definition: spinner_widget.hpp:202
bool m_graphical
Whether the value of this spinner is displayed using an icon rather than with a plain label.
Definition: spinner_widget.hpp:75
virtual void resize()
Call to resize the widget after its coordinates are updated.
Definition: spinner_widget.cpp:289
bool m_color_slider
Whether this widget is a color slider.
Definition: spinner_widget.hpp:88
void setCustomText(const core::stringw &text)
Display custom text in spinner.
Definition: spinner_widget.cpp:561
bool m_right_selected
Whether the right arrow is the currently selected one
Definition: spinner_widget.hpp:94
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:121
virtual EventPropagation transmitEvent(Widget *w, const std::string &originator, const int playerID)
implementing method from base class Widget
Definition: spinner_widget.cpp:407
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:147
virtual void elementRemoved()
Called when irrLicht widgets cleared.
Definition: widget.cpp:152
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:33