DISTRHO Plugin Framework
ImageWidgets.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any purpose with
6  * or without fee is hereby granted, provided that the above copyright notice and this
7  * permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef DGL_IMAGE_WIDGETS_HPP_INCLUDED
18 #define DGL_IMAGE_WIDGETS_HPP_INCLUDED
19 
20 #include "Image.hpp"
21 #include "ImageBaseWidgets.hpp"
22 #include "SubWidget.hpp"
23 
24 // TODO switch to use templated image type after merging widget-related PRs
25 #if defined(__GNUC__) && (__GNUC__ >= 6)
26 # pragma GCC diagnostic push
27 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
28 #endif
29 
30 START_NAMESPACE_DGL
31 
32 // -----------------------------------------------------------------------
33 
34 class ImageKnob : public SubWidget
35 {
36 public:
37  enum Orientation {
38  Horizontal,
39  Vertical
40  };
41 
42  class Callback
43  {
44  public:
45  virtual ~Callback() {}
46  virtual void imageKnobDragStarted(ImageKnob* imageKnob) = 0;
47  virtual void imageKnobDragFinished(ImageKnob* imageKnob) = 0;
48  virtual void imageKnobValueChanged(ImageKnob* imageKnob, float value) = 0;
49  };
50 
51  explicit ImageKnob(Widget* parentWidget, const Image& image, Orientation orientation = Vertical) noexcept;
52  explicit ImageKnob(const ImageKnob& imageKnob);
53  ImageKnob& operator=(const ImageKnob& imageKnob);
54  ~ImageKnob() override;
55 
56  float getValue() const noexcept;
57 
58  void setDefault(float def) noexcept;
59  void setRange(float min, float max) noexcept;
60  void setStep(float step) noexcept;
61  void setValue(float value, bool sendCallback = false) noexcept;
62  void setUsingLogScale(bool yesNo) noexcept;
63 
64  void setCallback(Callback* callback) noexcept;
65  void setOrientation(Orientation orientation) noexcept;
66  void setRotationAngle(int angle);
67 
68  void setImageLayerCount(uint count) noexcept;
69 
70 protected:
71  void onDisplay() override;
72  bool onMouse(const MouseEvent&) override;
73  bool onMotion(const MotionEvent&) override;
74  bool onScroll(const ScrollEvent&) override;
75 
76 private:
77  Image fImage;
78  float fMinimum;
79  float fMaximum;
80  float fStep;
81  float fValue;
82  float fValueDef;
83  float fValueTmp;
84  bool fUsingDefault;
85  bool fUsingLog;
86  Orientation fOrientation;
87 
88  int fRotationAngle;
89  bool fDragging;
90  int fLastX;
91  int fLastY;
92 
93  Callback* fCallback;
94 
95  bool fIsImgVertical;
96  uint fImgLayerWidth;
97  uint fImgLayerHeight;
98  uint fImgLayerCount;
99  bool fIsReady;
100  GLuint fTextureId;
101 
102  float _logscale(float value) const;
103  float _invlogscale(float value) const;
104 
105  DISTRHO_LEAK_DETECTOR(ImageKnob)
106 };
107 
108 // -----------------------------------------------------------------------
109 
110 // note set range and step before setting the value
111 
112 class ImageSlider : public SubWidget
113 {
114 public:
115  class Callback
116  {
117  public:
118  virtual ~Callback() {}
119  virtual void imageSliderDragStarted(ImageSlider* imageSlider) = 0;
120  virtual void imageSliderDragFinished(ImageSlider* imageSlider) = 0;
121  virtual void imageSliderValueChanged(ImageSlider* imageSlider, float value) = 0;
122  };
123 
124  explicit ImageSlider(Widget* parentWidget, const Image& image) noexcept;
125 
126  float getValue() const noexcept;
127  void setValue(float value, bool sendCallback = false) noexcept;
128  void setDefault(float def) noexcept;
129 
130  void setStartPos(const Point<int>& startPos) noexcept;
131  void setStartPos(int x, int y) noexcept;
132  void setEndPos(const Point<int>& endPos) noexcept;
133  void setEndPos(int x, int y) noexcept;
134 
135  void setInverted(bool inverted) noexcept;
136  void setRange(float min, float max) noexcept;
137  void setStep(float step) noexcept;
138 
139  void setCallback(Callback* callback) noexcept;
140 
141 protected:
142  void onDisplay() override;
143  bool onMouse(const MouseEvent&) override;
144  bool onMotion(const MotionEvent&) override;
145 
146 private:
147  Image fImage;
148  float fMinimum;
149  float fMaximum;
150  float fStep;
151  float fValue;
152  float fValueDef;
153  float fValueTmp;
154  bool fUsingDefault;
155 
156  bool fDragging;
157  bool fInverted;
158  bool fValueIsSet;
159  int fStartedX;
160  int fStartedY;
161 
162  Callback* fCallback;
163 
164  Point<int> fStartPos;
165  Point<int> fEndPos;
166  Rectangle<double> fSliderArea;
167 
168  void _recheckArea() noexcept;
169 
170  // these should not be used
171  void setAbsoluteX(int) const noexcept {}
172  void setAbsoluteY(int) const noexcept {}
173  void setAbsolutePos(int, int) const noexcept {}
174  void setAbsolutePos(const Point<int>&) const noexcept {}
175 
176  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImageSlider)
177 };
178 
179 // -----------------------------------------------------------------------
180 
181 class ImageSwitch : public SubWidget
182 {
183 public:
184  class Callback
185  {
186  public:
187  virtual ~Callback() {}
188  virtual void imageSwitchClicked(ImageSwitch* imageSwitch, bool down) = 0;
189  };
190 
191  explicit ImageSwitch(Widget* parentWidget, const Image& imageNormal, const Image& imageDown) noexcept;
192  explicit ImageSwitch(const ImageSwitch& imageSwitch) noexcept;
193  ImageSwitch& operator=(const ImageSwitch& imageSwitch) noexcept;
194 
195  bool isDown() const noexcept;
196  void setDown(bool down) noexcept;
197 
198  void setCallback(Callback* callback) noexcept;
199 
200 protected:
201  void onDisplay() override;
202  bool onMouse(const MouseEvent&) override;
203 
204 private:
205  Image fImageNormal;
206  Image fImageDown;
207  bool fIsDown;
208 
209  Callback* fCallback;
210 
211  DISTRHO_LEAK_DETECTOR(ImageSwitch)
212 };
213 
214 // -----------------------------------------------------------------------
215 
216 END_NAMESPACE_DGL
217 
218 #if defined(__GNUC__) && (__GNUC__ >= 6)
219 # pragma GCC diagnostic pop
220 #endif
221 
222 #endif // DGL_IMAGE_WIDGETS_HPP_INCLUDED
Widget::ScrollEvent
Definition: Widget.hpp:206
SubWidget::setAbsolutePos
void setAbsolutePos(int x, int y) noexcept
SubWidget::setAbsoluteY
void setAbsoluteY(int y) noexcept
Rectangle
Definition: Geometry.hpp:30
SubWidget::setAbsoluteX
void setAbsoluteX(int x) noexcept
ImageKnob::Callback
Definition: ImageWidgets.hpp:42
ImageKnob::onScroll
bool onScroll(const ScrollEvent &) override
Widget::MotionEvent
Definition: Widget.hpp:183
ImageSlider::Callback
Definition: ImageWidgets.hpp:115
ImageSwitch
Definition: ImageWidgets.hpp:181
ImageSwitch::Callback
Definition: ImageWidgets.hpp:184
ImageKnob::onMotion
bool onMotion(const MotionEvent &) override
ImageKnob
Definition: ImageWidgets.hpp:34
ImageSlider
Definition: ImageWidgets.hpp:112
ImageKnob::onMouse
bool onMouse(const MouseEvent &) override
Point
Definition: Geometry.hpp:40
SubWidget
Definition: SubWidget.hpp:39
Widget::MouseEvent
Definition: Widget.hpp:164
ImageKnob::onDisplay
void onDisplay() override
Widget
Definition: Widget.hpp:53