TGUI  0.9-beta
SpinControl.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 Bruno Van de Velde (vdv_b@tgui.eu)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
26 #ifndef TGUI_SPIN_CONTROL_HPP
27 #define TGUI_SPIN_CONTROL_HPP
28 
29 
30 #include <TGUI/SubwidgetContainer.hpp>
31 #include <TGUI/Widgets/SpinButton.hpp>
32 #include <TGUI/Widgets/EditBox.hpp>
33 
35 
36 namespace tgui
37 {
41  class TGUI_API SpinControl : public SubwidgetContainer
42  {
43  public:
44 
45  typedef std::shared_ptr<SpinControl> Ptr;
46  typedef std::shared_ptr<const SpinControl> ConstPtr;
47 
48 
50  // Default constructor
52  SpinControl(float min = 0.0f, float max = 10.0f, float value = 0.0f, unsigned decimal = 0, float step = 1.0f);
53 
54 
66  static SpinControl::Ptr create(float min = 0.0f, float max = 10.0f, float value = 0.0f,
67  unsigned decimal = 0, float step = 1.0f);
68 
69 
78 
79 
85  const SpinButtonRenderer* getSpinButtonSharedRenderer() const;
86 
93  const SpinButtonRenderer* getSpinButtonRenderer() const;
94 
95 
101  const EditBoxRenderer* getSpinTextSharedRenderer() const;
102 
103 
110  const EditBoxRenderer* getSpinTextRenderer() const;
111 
112 
118  void setSize(const Layout2d& size) override;
120 
121 
130  void setMinimum(float minimum);
131 
132 
140  float getMinimum() const;
141 
142 
151  void setMaximum(float maximum);
152 
153 
161  float getMaximum() const;
162 
163 
173  bool setValue(float value);
174 
175 
183  float getValue() const;
184 
185 
191  void setStep(float step);
192 
193 
201  float getStep() const;
202 
203 
210  void setDecimalPlaces(unsigned decimalPlaces);
211 
212 
220  unsigned getDecimalPlaces() const;
221 
222 
224  protected:
225 
227  // Makes a copy of the widget
229  Widget::Ptr clone() const override
230  {
231  return std::make_shared<SpinControl>(*this);
232  }
233 
234 
238  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
239 
240 
244  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
245 
246 
248  private:
249 
251  // Helper function that initializes the widget when constructing a new widget or loading one from a file
253  void init();
254 
255 
257  // Checks whether a value lies between the minimum and maximum
259  bool inRange(const float value) const;
260 
261 
263  // Updates the text in the edit box
265  void setString(const tgui::String& str);
266 
267 
269  public:
270 
271  SignalFloat onValueChange = {"ValueChanged"};
272 
273 
275  protected:
276 
277  unsigned m_decimalPlaces;
278 
280  private:
281  tgui::SpinButton::Ptr m_spinButton;
282  tgui::EditBox::Ptr m_spinText;
283  };
284 
286 }
287 
289 
290 #endif // TGUI_SPIN_CONTROL_HPP
float getMaximum() const
Returns the maximum value.
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition: EditBox.hpp:49
void setMinimum(float minimum)
Sets a minimum value.
Definition: EditBoxRenderer.hpp:37
void setStep(float step)
Changes how much the value changes on each arrow press.
static SpinControl::Ptr copy(SpinControl::ConstPtr SpinCtrl)
Makes a copy of another spin control.
SpinButtonRenderer * getSpinButtonSharedRenderer()
Returns the renderer of spin buttons part of widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Base class for widgets that consist of subwidgets that act together as if they are a single widget.
Definition: SubwidgetContainer.hpp:43
EditBoxRenderer * getSpinTextRenderer()
Returns the renderer of edit box part of widget.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
void setMaximum(float maximum)
Sets a maximum value.
bool setValue(float value)
Changes the current value.
Spin control widget.
Definition: SpinControl.hpp:42
std::shared_ptr< SpinControl > Ptr
Shared widget pointer.
Definition: SpinControl.hpp:45
float getValue() const
Returns the current value.
Definition: SpinButtonRenderer.hpp:37
Wrapper class to store strings.
Definition: String.hpp:74
float getMinimum() const
Returns the minimum value.
std::shared_ptr< const SpinControl > ConstPtr
Shared constant widget pointer.
Definition: SpinControl.hpp:46
void setDecimalPlaces(unsigned decimalPlaces)
Changes the number of decimal places to display.
unsigned getDecimalPlaces() const
Returns the number of decimal places to display.
void setSize(const Layout2d &size) override
Changes the size of the spin control.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
std::shared_ptr< SpinButton > Ptr
Shared widget pointer.
Definition: SpinButton.hpp:45
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
static SpinControl::Ptr create(float min=0.0f, float max=10.0f, float value=0.0f, unsigned decimal=0, float step=1.0f)
Creates a new spin button widget.
float getStep() const
Returns the number of positions the thumb advances with each move.
SpinButtonRenderer * getSpinButtonRenderer()
Returns the renderer of spin buttons part of widget.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: SpinControl.hpp:229
EditBoxRenderer * getSpinTextSharedRenderer()
Returns the renderer of edit box part of widget.