TGUI  0.9.1
Knob.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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_KNOB_HPP
27 #define TGUI_KNOB_HPP
28 
29 
30 #include <TGUI/Widget.hpp>
31 #include <TGUI/Renderers/KnobRenderer.hpp>
32 
34 
35 namespace tgui
36 {
40  class TGUI_API Knob : public Widget
41  {
42  public:
43 
44  typedef std::shared_ptr<Knob> Ptr;
45  typedef std::shared_ptr<const Knob> ConstPtr;
46 
47 
55  Knob(const char* typeName = "Knob", bool initRenderer = true);
56 
57 
64  static Knob::Ptr create();
65 
66 
76 
77 
83  const KnobRenderer* getSharedRenderer() const;
84 
91  const KnobRenderer* getRenderer() const;
92 
93 
100  void setSize(const Layout2d& size) override;
101  using Widget::setSize;
102 
103 
112  void setStartRotation(float startRotation);
113 
114 
121  float getStartRotation() const;
122 
123 
132  void setEndRotation(float endRotation);
133 
134 
141  float getEndRotation() const;
142 
143 
158  void setMinimum(float minimum);
159 
160 
171  float getMinimum() const;
172 
173 
188  void setMaximum(float maximum);
189 
190 
201  float getMaximum() const;
202 
203 
215  void setValue(float value);
216 
217 
226  float getValue() const;
227 
228 
235  void setClockwiseTurning(bool clockwise);
236 
237 
244  bool getClockwiseTurning() const;
245 
246 
253  bool isMouseOnWidget(Vector2f pos) const override;
254 
258  void leftMousePressed(Vector2f pos) override;
259 
263  void mouseMoved(Vector2f pos) override;
264 
265 
273  void draw(BackendRenderTargetBase& target, RenderStates states) const override;
274 
275 
277  protected:
278 
288  Signal& getSignal(String signalName) override;
289 
290 
297  void rendererChanged(const String& property) override;
298 
299 
303  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
304 
305 
309  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
310 
311 
313  // Returns the size without the borders
315  Vector2f getInnerSize() const;
316 
317 
319  // Recalculates the rotation of the knob.
321  void recalculateRotation();
322 
323 
325  // Makes a copy of the widget
327  Widget::Ptr clone() const override
328  {
329  return std::make_shared<Knob>(*this);
330  }
331 
332 
334  public:
335 
336  SignalFloat onValueChange = {"ValueChanged"};
337 
338 
340  protected:
341 
342  bool m_clockwiseTurning = true; // Does rotating clockwise increment the value?
343  float m_startRotation = 270;
344  float m_endRotation = 270;
345 
346  float m_minimum = 0;
347  float m_value = 0;
348  float m_maximum = 360;
349 
350  float m_angle = 270;
351 
352  Sprite m_spriteBackground;
353  Sprite m_spriteForeground;
354 
355  // Cached renderer properties
356  Borders m_bordersCached;
357  Color m_borderColorCached;
358  Color m_backgroundColorCached;
359  Color m_thumbColorCached;
360  float m_imageRotationCached = 0;
361  };
362 
364 }
365 
367 
368 #endif // TGUI_KNOB_HPP
369 
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Wrapper for colors.
Definition: Color.hpp:52
Definition: KnobRenderer.hpp:37
Knob widget.
Definition: Knob.hpp:41
static Knob::Ptr create()
Creates a new knob widget.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
float getMinimum() const
Returns the value when the knob would be rotated in the direction of StartRotation.
std::shared_ptr< const Knob > ConstPtr
Shared constant widget pointer.
Definition: Knob.hpp:45
KnobRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< Knob > Ptr
Shared widget pointer.
Definition: Knob.hpp:44
void setValue(float value)
Changes the current value.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
float getEndRotation() const
Sets the end rotation, which is the place where the value should be maximal.
float getStartRotation() const
Returns the start rotation, which is the place where the value should be minimal.
void setEndRotation(float endRotation)
Sets the end rotation, which is the place where the value should be maximal.
void draw(BackendRenderTargetBase &target, RenderStates states) const override
Draw the widget to a render target.
float getMaximum() const
Returns the value when the knob would be rotated in the direction of EndRotation.
float getValue() const
Returns the current value.
static Knob::Ptr copy(Knob::ConstPtr knob)
Makes a copy of another knob.
void setClockwiseTurning(bool clockwise)
Should the value increase when turning the knob clockwise?
bool getClockwiseTurning() const
Returns whether the value increases when turning the knob clockwise?
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Knob.hpp:327
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
KnobRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
void setSize(const Layout2d &size) override
Changes the size of the knob.
void setStartRotation(float startRotation)
Sets the start rotation, which is the place where the value should be minimal.
void setMinimum(float minimum)
Sets the value for when the knob would be rotated in the direction of StartRotation.
void setMaximum(float maximum)
Sets the value for when the knob would be rotated in the direction of EndRotation.
Class to store the position or size of a widget.
Definition: Layout.hpp:262
Definition: Outline.hpp:39
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Definition: Sprite.hpp:49
Wrapper class to store strings.
Definition: String.hpp:74
The parent class for every widget.
Definition: Widget.hpp:70
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39