TGUI  1.6.1
Loading...
Searching...
No Matches
Knob.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#ifndef TGUI_KNOB_HPP
26#define TGUI_KNOB_HPP
27
28#include <TGUI/Widget.hpp>
29#include <TGUI/Renderers/KnobRenderer.hpp>
30
32
33TGUI_MODULE_EXPORT namespace tgui
34{
38 class TGUI_API Knob : public Widget
39 {
40 public:
41
42 using Ptr = std::shared_ptr<Knob>;
43 using ConstPtr = std::shared_ptr<const Knob>;
44
45 static constexpr const char StaticWidgetType[] = "Knob";
46
54 Knob(const char* typeName = StaticWidgetType, bool initRenderer = true);
55
61 TGUI_NODISCARD static Knob::Ptr create();
62
70 TGUI_NODISCARD static Knob::Ptr copy(const Knob::ConstPtr& knob);
71
76 TGUI_NODISCARD KnobRenderer* getSharedRenderer() override;
77 TGUI_NODISCARD const KnobRenderer* getSharedRenderer() const override;
78
84 TGUI_NODISCARD KnobRenderer* getRenderer() override;
85
91 void setSize(const Layout2d& size) override;
92 using Widget::setSize;
93
101 void setStartRotation(float startRotation);
102
108 TGUI_NODISCARD float getStartRotation() const;
109
117 void setEndRotation(float endRotation);
118
124 TGUI_NODISCARD float getEndRotation() const;
125
139 void setMinimum(float minimum);
140
150 TGUI_NODISCARD float getMinimum() const;
151
165 void setMaximum(float maximum);
166
176 TGUI_NODISCARD float getMaximum() const;
177
188 void setValue(float value);
189
197 TGUI_NODISCARD float getValue() const;
198
204 void setClockwiseTurning(bool clockwise);
205
211 TGUI_NODISCARD bool getClockwiseTurning() const;
212
218 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
219
223 bool leftMousePressed(Vector2f pos) override;
224
228 void mouseMoved(Vector2f pos) override;
229
236 void draw(BackendRenderTarget& target, RenderStates states) const override;
237
239 protected:
240
250 TGUI_NODISCARD Signal& getSignal(String signalName) override;
251
257 void rendererChanged(const String& property) override;
258
262 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
263
267 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
268
270 // Returns the size without the borders
272 TGUI_NODISCARD Vector2f getInnerSize() const;
273
275 // Recalculates the rotation of the knob.
277 void recalculateRotation();
278
280 // Makes a copy of the widget
282 TGUI_NODISCARD Widget::Ptr clone() const override;
283
285 public:
286
287 SignalFloat onValueChange = {"ValueChanged"};
288
290 protected:
291
292 bool m_clockwiseTurning = true; // Does rotating clockwise increment the value?
293 float m_startRotation = 270;
294 float m_endRotation = 270;
295
296 float m_minimum = 0;
297 float m_value = 0;
298 float m_maximum = 360;
299
300 float m_angle = 270;
301
302 Sprite m_spriteBackground;
303 Sprite m_spriteForeground;
304
305 // Cached renderer properties
306 Borders m_bordersCached;
307 Color m_borderColorCached;
308 Color m_backgroundColorCached;
309 Color m_thumbColorCached;
310 float m_imageRotationCached = 0;
311 };
312
314}
315
317
318#endif // TGUI_KNOB_HPP
319
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:73
Definition KnobRenderer.hpp:35
Knob widget.
Definition Knob.hpp:39
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.
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.
KnobRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
void setEndRotation(float endRotation)
Sets the end rotation, which is the place where the value should be maximal.
void draw(BackendRenderTarget &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.
void setClockwiseTurning(bool clockwise)
Should the value increase when turning the knob clockwise?
KnobRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
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.
static Knob::Ptr copy(const Knob::ConstPtr &knob)
Makes a copy of another knob.
void setSize(const Layout2d &size) override
Changes the size of the knob.
std::shared_ptr< const Knob > ConstPtr
Shared constant widget pointer.
Definition Knob.hpp:43
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 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.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
std::shared_ptr< Knob > Ptr
Shared widget pointer.
Definition Knob.hpp:42
Class to store the position or size of a widget.
Definition Layout.hpp:323
Definition Outline.hpp:38
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
The parent class for every widget.
Definition Widget.hpp:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
States used for drawing.
Definition RenderStates.hpp:38