TGUI  0.7.8
Knob.hpp
1
2//
3// TGUI - Texus's Graphical User Interface
4// Copyright (C) 2012-2017 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
33
34namespace tgui
35{
36 class KnobRenderer;
37
49 class TGUI_API Knob : public Widget
50 {
51 public:
52
53 typedef std::shared_ptr<Knob> Ptr;
54 typedef std::shared_ptr<const Knob> ConstPtr;
55
56
58 // Default constructor
60 Knob();
61
62
69 static Knob::Ptr create();
70
71
81
82
89 std::shared_ptr<KnobRenderer> getRenderer() const
90 {
91 return std::static_pointer_cast<KnobRenderer>(m_renderer);
92 }
93
94
107 virtual void setPosition(const Layout2d& position) override;
109
110
117 virtual void setSize(const Layout2d& size) override;
119
120
129 virtual sf::Vector2f getFullSize() const override;
130
131
140 void setStartRotation(float startRotation);
141
142
151 void setEndRotation(float endRotation);
152
153
161 {
162 return m_startRotation;
163 }
164
165
173 {
174 return m_endRotation;
175 }
176
177
192 virtual void setMinimum(int minimum);
193
194
209 virtual void setMaximum(int maximum);
210
211
223 virtual void setValue(int value);
224
225
236 int getMinimum() const
237 {
238 return m_minimum;
239 }
240
241
252 int getMaximum() const
253 {
254 return m_maximum;
255 }
256
257
266 int getValue() const
267 {
268 return m_value;
269 }
270
271
278 void setClockwiseTurning(bool clockwise);
279
280
288 {
289 return m_clockwiseTurning;
290 }
291
292
299 virtual void setOpacity(float opacity) override;
300
301
310 virtual sf::Vector2f getWidgetOffset() const override;
311
312
316 virtual bool mouseOnWidget(float x, float y) const override;
317
321 virtual void leftMousePressed(float x, float y) override;
322
326 virtual void leftMouseReleased(float x, float y) override;
327
331 virtual void mouseMoved(float x, float y) override;
332
336 virtual void widgetFocused() override;
337
338
340 protected:
341
343 // Recalculates the rotation of the knob.
345 void recalculateRotation();
346
347
360 virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
361
362
364 // Makes a copy of the widget
366 virtual Widget::Ptr clone() const override
367 {
368 return std::make_shared<Knob>(*this);
369 }
370
371
373 // Draws the widget on the render target.
375 virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
376
377
379 protected:
380
381 bool m_clockwiseTurning = true; // Does rotating clockwise increment the value?
382 float m_startRotation = 270;
383 float m_endRotation = 270;
384
385 int m_minimum = 0;
386 int m_value = 0;
387 int m_maximum = 360;
388
389 float m_angle = 270;
390
391 friend class KnobRenderer;
392 };
393
394
396
397 class TGUI_API KnobRenderer : public WidgetRenderer, public WidgetBorders
398 {
399 public:
400
407 KnobRenderer(Knob* knob) : m_knob{knob} {}
408
409
419 virtual void setProperty(std::string property, const std::string& value) override;
420
421
432 virtual void setProperty(std::string property, ObjectConverter&& value) override;
433
434
444 virtual ObjectConverter getProperty(std::string property) const override;
445
446
453 virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
454
455
465 void setImageRotation(float rotation);
466
467
476 void setBackgroundColor(const Color& color);
477
478
487 void setThumbColor(const Color& color);
488
489
498 void setBorderColor(const Color& color);
499
500
510 void setBackgroundTexture(const Texture& texture);
511
512
524 void setForegroundTexture(const Texture& texture);
525
526
528 // Draws the widget on the render target.
530 void draw(sf::RenderTarget& target, sf::RenderStates states) const;
531
532
534 private:
535
537 // Makes a copy of the renderer
539 virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
540
541
543 protected:
544
545 Knob* m_knob;
546
547 float m_imageRotation = 0;
548 Texture m_backgroundTexture;
549 Texture m_foregroundTexture;
550
551 sf::Color m_backgroundColor;
552 sf::Color m_thumbColor;
553
554 sf::Color m_borderColor;
555
556 friend class Knob;
557
559 };
560
562}
563
565
566#endif // TGUI_KNOB_HPP
567
Implicit converter for colors.
Definition: Color.hpp:40
Definition: Knob.hpp:398
virtual void setProperty(std::string property, ObjectConverter &&value) override
Change a property of the renderer.
virtual std::map< std::string, ObjectConverter > getPropertyValuePairs() const override
Get a map with all properties and their values.
KnobRenderer(Knob *knob)
Constructor.
Definition: Knob.hpp:407
void setForegroundTexture(const Texture &texture)
Change the foreground image of the knob.
void setThumbColor(const Color &color)
Changes the color of the thumb which you can move around the circle.
void setBorderColor(const Color &color)
Changes the color of the borders that are optionally drawn around the knob.
virtual void setProperty(std::string property, const std::string &value) override
Change a property of the renderer.
void setImageRotation(float rotation)
Changes the default rotation of the foreground image.
virtual ObjectConverter getProperty(std::string property) const override
Retrieve the value of a certain property.
void setBackgroundTexture(const Texture &texture)
Change the background image of the knob.
void setBackgroundColor(const Color &color)
Changes the color of the background.
Knob widget.
Definition: Knob.hpp:50
virtual sf::Vector2f getFullSize() const override
Returns the full size of the knob.
static Knob::Ptr create()
Creates a new knob widget.
std::shared_ptr< const Knob > ConstPtr
Shared constant widget pointer.
Definition: Knob.hpp:54
std::shared_ptr< Knob > Ptr
Shared widget pointer.
Definition: Knob.hpp:53
virtual void setPosition(const Layout2d &position) override
Set the position of the knob.
int getMinimum() const
Returns the value when the knob would be rotated in the direction of StartRotation.
Definition: Knob.hpp:236
void setEndRotation(float endRotation)
Set the end rotation, which is the place where the value should be maximal.
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: Knob.hpp:366
virtual void setOpacity(float opacity) override
Changes the opacity of the widget.
bool getClockwiseTurning()
Returns whether the value increases when turning the knob clockwise?
Definition: Knob.hpp:287
virtual void setMaximum(int maximum)
Sets the value for when the knob would be rotated in the direction of EndRotation.
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?
int getValue() const
Returns the current value.
Definition: Knob.hpp:266
virtual sf::Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
std::shared_ptr< KnobRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Definition: Knob.hpp:89
virtual void setSize(const Layout2d &size) override
Changes the size of the knob.
virtual void setMinimum(int minimum)
Sets the value for when the knob would be rotated in the direction of StartRotation.
float getStartRotation()
Returns the start rotation, which is the place where the value should be minimal.
Definition: Knob.hpp:160
virtual void reload(const std::string &primary="", const std::string &secondary="", bool force=false) override
Reload the widget.
void setStartRotation(float startRotation)
Set the start rotation, which is the place where the value should be minimal.
virtual void setValue(int value)
Changes the current value.
int getMaximum() const
Returns the value when the knob would be rotated in the direction of EndRotation.
Definition: Knob.hpp:252
float getEndRotation()
Set the end rotation, which is the place where the value should be maximal.
Definition: Knob.hpp:172
Class to store the position or size of a widget.
Definition: Layout.hpp:255
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:43
Definition: Texture.hpp:45
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
virtual void setPosition(const Layout2d &position)
set the position of the widget
Parent class for every widget that has borders.
Definition: Borders.hpp:137
Base class for all renderer classes.
Definition: Widget.hpp:683
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:34