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 namespace irr
25 {
26  namespace video { class ITexture; }
27 }
28 
29 #include "guiengine/widget.hpp"
30 #include "utils/leak_check.hpp"
31 #include "utils/ptr_vector.hpp"
32 
33 namespace GUIEngine
34 {
35 
39  class SpinnerWidget : public Widget
40  {
41  public:
43  {
44  public:
45  virtual ~ISpinnerConfirmListener() {}
46 
51  virtual EventPropagation onSpinnerConfirmed() = 0;
52  };
53 
54  protected:
55 
56  ISpinnerConfirmListener* m_listener;
57 
58  int m_value, m_min, m_max;
59 
60  int m_spinner_widget_player_id;
61  bool m_use_background_color;
62 
64  std::vector<irr::core::stringw> m_labels;
65 
68 
73  bool m_gauge;
74 
75 
78 
81 
85  core::stringw m_custom_text;
86 
88  virtual EventPropagation transmitEvent(Widget* w,
89  const std::string& originator,
90  const int playerID);
91 
93  virtual EventPropagation rightPressed(const int playerID);
94 
96  virtual EventPropagation leftPressed(const int playerID);
97 
99  virtual EventPropagation onClick();
100 
103  virtual int getWidthNeededAroundLabel() const { return 25; }
104 
107  virtual int getHeightNeededAroundLabel() const { return 8; }
108 
110  irr::video::ITexture* getTexture();
111 
112  public:
113 
114  LEAK_CHECK()
115 
116  SpinnerWidget(const bool gauge=false);
117  virtual ~SpinnerWidget() {}
118  virtual void move(const int x, const int y, const int w, const int h);
119 
120  void addLabel(irr::core::stringw label);
121  void clearLabels();
122 
123  // next four functions are for background colour behind playername in multikart screen selection
124  void setUseBackgroundColor() {m_use_background_color=true; }
125  bool getUseBackgroundColor() {return m_use_background_color; }
126  void setSpinnerWidgetPlayerID(int playerID) {m_spinner_widget_player_id=playerID;}
127  int getSpinnerWidgetPlayerID() {return m_spinner_widget_player_id; }
128  void unsetUseBackgroundColor() {m_use_background_color=false; }
129 
130  void activateSelectedButton();
131  void setSelectedButton(bool right) {m_right_selected = right; }
132  bool isRightButtonSelected() {return m_right_selected; }
133 
134 
135 
136 
137  void setListener(ISpinnerConfirmListener* listener) { m_listener = listener; }
138 
140  virtual void add();
141 
146  void setValue(const int new_value);
147 
153  void setValue(irr::core::stringw new_value);
154 
158  bool isGauge() const { return m_gauge; }
159 
164  int getValue() const { return m_value; }
165 
170  irr::core::stringw getStringValue() const;
171 
176  irr::core::stringw getStringValueFromID(unsigned id) const
177  {
178  if (id > m_labels.size())
179  return L"";
180  return m_labels[id];
181  }
182 
186  // --------------------------------------------------------------------
188  int getMax() const { return m_max; }
189  // --------------------------------------------------------------------
193  void setMax(int n)
194  {
195  m_max = n;
196  if(getValue()>m_max) setValue(m_max);
197  } // setMax
198  // --------------------------------------------------------------------
202  int getMin() const { return m_min; }
203  // --------------------------------------------------------------------
207  void setMin(int n)
208  {
209  m_min = n;
210  if(getValue()<m_min) setValue(m_min);
211  } // setMin
212 
213  // --------------------------------------------------------------------
215  virtual void setActive(bool active = true);
216 
218  void setCustomText(const core::stringw& text);
219  const core::stringw& getCustomText() const { return m_custom_text; }
220  };
221 
222 }
223 
224 #endif
virtual EventPropagation onClick()
implementing method from base class Widget
Definition: spinner_widget.cpp:458
A spinner or gauge widget (to select numbers / percentages).
Definition: spinner_widget.hpp:39
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:31
bool m_right_selected
Whether the right or left arrow is the currently selected one.
Definition: spinner_widget.hpp:80
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:73
virtual EventPropagation transmitEvent(Widget *w, const std::string &originator, const int playerID)
implementing method from base class Widget
Definition: spinner_widget.cpp:294
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:64
irr::core::stringw getStringValue() const
retrieve the current value of the spinner
Definition: spinner_widget.cpp:380
int getMax() const
Returns the maximum value.
Definition: spinner_widget.hpp:188
virtual EventPropagation onSpinnerConfirmed()=0
Invoked when the spinner is selected and "fire" is pressed.
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:107
virtual void setActive(bool active=true)
Override method from base class Widget.
Definition: spinner_widget.cpp:419
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:85
bool m_wrap_around
Whether to wrap back to the first value when going "beyond" the last value.
Definition: spinner_widget.hpp:77
void setMax(int n)
Sets the maximum value for a spinner.
Definition: spinner_widget.hpp:193
irr::video::ITexture * getTexture()
Call only if this spinner is graphical.
Definition: spinner_widget.cpp:189
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:200
Definition: spinner_widget.hpp:42
irr::core::stringw getStringValueFromID(unsigned id) const
retrieve the value of the spinner from id
Definition: spinner_widget.hpp:176
virtual EventPropagation leftPressed(const int playerID)
implementing method from base class Widget
Definition: spinner_widget.cpp:248
bool m_graphical
Whether the value of this spinner is displayed using an icon rather than with a plain label.
Definition: spinner_widget.hpp:67
int getValue() const
retrieve the current value of the spinner
Definition: spinner_widget.hpp:164
bool isGauge() const
Definition: spinner_widget.hpp:158
virtual void add()
implement method from base class Widget
Definition: spinner_widget.cpp:57
void setCustomText(const core::stringw &text)
Display custom text in spinner.
Definition: spinner_widget.cpp:447
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:103
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:138
int getMin() const
Definition: spinner_widget.hpp:202
void setValue(const int new_value)
sets the current value of the spinner
Definition: spinner_widget.cpp:349
void setMin(int n)
Sets the minimum value for a spinner.
Definition: spinner_widget.hpp:207
virtual EventPropagation rightPressed(const int playerID)
implementing method from base class Widget
Definition: spinner_widget.cpp:230