TGUI  0.7.4
Knob.hpp
1 //
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 
34 namespace 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 
80  static Knob::Ptr copy(Knob::ConstPtr knob);
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 
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
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
Implicit converter for colors.
Definition: Color.hpp:39
bool getClockwiseTurning()
Returns whether the value increases when turning the knob clockwise?
Definition: Knob.hpp:287
Class to store the position or size of a widget.
Definition: Layout.hpp:262
The parent class for every widget.
Definition: Widget.hpp:71
Knob widget.
Definition: Knob.hpp:49
KnobRenderer(Knob *knob)
Constructor.
Definition: Knob.hpp:407
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
int getMaximum() const
Returns the value when the knob would be rotated in the direction of EndRotation. ...
Definition: Knob.hpp:252
virtual void setPosition(const Layout2d &position)
set the position of the widget
Parent class for every widget that has borders.
Definition: Borders.hpp:136
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: Knob.hpp:366
float getEndRotation()
Set the end rotation, which is the place where the value should be maximal.
Definition: Knob.hpp:172
Definition: Knob.hpp:397
std::shared_ptr< const Knob > ConstPtr
Shared constant widget pointer.
Definition: Knob.hpp:54
Definition: Texture.hpp:44
int getMinimum() const
Returns the value when the knob would be rotated in the direction of StartRotation.
Definition: Knob.hpp:236
std::shared_ptr< Knob > Ptr
Shared widget pointer.
Definition: Knob.hpp:53
float getStartRotation()
Returns the start rotation, which is the place where the value should be minimal. ...
Definition: Knob.hpp:160
Base class for all renderer classes.
Definition: Widget.hpp:682
int getValue() const
Returns the current value.
Definition: Knob.hpp:266