DISTRHO Plugin Framework
ImageWidgets.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2016 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 "Widget.hpp"
22 #include "Window.hpp"
23 
24 START_NAMESPACE_DGL
25 
26 // -----------------------------------------------------------------------
27 
28 class ImageAboutWindow : public Window,
29  public Widget
30 {
31 public:
32  explicit ImageAboutWindow(Window& parent, const Image& image = Image());
33  explicit ImageAboutWindow(Widget* widget, const Image& image = Image());
34 
35  void setImage(const Image& image);
36 
37 protected:
38  void onDisplay() override;
39  bool onKeyboard(const KeyboardEvent&) override;
40  bool onMouse(const MouseEvent&) override;
41  void onReshape(uint width, uint height) override;
42 
43 private:
44  Image fImgBackground;
45 
46  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImageAboutWindow)
47 };
48 
49 // -----------------------------------------------------------------------
50 
51 class ImageButton : public Widget
52 {
53 public:
54  class Callback
55  {
56  public:
57  virtual ~Callback() {}
58  virtual void imageButtonClicked(ImageButton* imageButton, int button) = 0;
59  };
60 
61  explicit ImageButton(Window& parent, const Image& image);
62  explicit ImageButton(Window& parent, const Image& imageNormal, const Image& imageDown);
63  explicit ImageButton(Window& parent, const Image& imageNormal, const Image& imageHover, const Image& imageDown);
64 
65  explicit ImageButton(Widget* widget, const Image& image);
66  explicit ImageButton(Widget* widget, const Image& imageNormal, const Image& imageDown);
67  explicit ImageButton(Widget* widget, const Image& imageNormal, const Image& imageHover, const Image& imageDown);
68 
69  ~ImageButton() override;
70 
71  void setCallback(Callback* callback) noexcept;
72 
73 protected:
74  void onDisplay() override;
75  bool onMouse(const MouseEvent&) override;
76  bool onMotion(const MotionEvent&) override;
77 
78 private:
79  struct PrivateData;
80  PrivateData* const pData;
81 
82  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImageButton)
83 };
84 
85 // -----------------------------------------------------------------------
86 
87 class ImageKnob : public Widget
88 {
89 public:
90  enum Orientation {
91  Horizontal,
92  Vertical
93  };
94 
95  class Callback
96  {
97  public:
98  virtual ~Callback() {}
99  virtual void imageKnobDragStarted(ImageKnob* imageKnob) = 0;
100  virtual void imageKnobDragFinished(ImageKnob* imageKnob) = 0;
101  virtual void imageKnobValueChanged(ImageKnob* imageKnob, float value) = 0;
102  };
103 
104  explicit ImageKnob(Window& parent, const Image& image, Orientation orientation = Vertical) noexcept;
105  explicit ImageKnob(Widget* widget, const Image& image, Orientation orientation = Vertical) noexcept;
106  explicit ImageKnob(const ImageKnob& imageKnob);
107  ImageKnob& operator=(const ImageKnob& imageKnob);
108  ~ImageKnob() override;
109 
110  float getValue() const noexcept;
111 
112  void setDefault(float def) noexcept;
113  void setRange(float min, float max) noexcept;
114  void setStep(float step) noexcept;
115  void setScrollStep(float step) noexcept;
116  void setValue(float value, bool sendCallback = false) noexcept;
117  void setUsingLogScale(bool yesNo) noexcept;
118 
119  void setCallback(Callback* callback) noexcept;
120  void setOrientation(Orientation orientation) noexcept;
121  void setRotationAngle(int angle);
122 
123  void setImageLayerCount(uint count) noexcept;
124 
125 protected:
126  void onDisplay() override;
127  bool onMouse(const MouseEvent&) override;
128  bool onMotion(const MotionEvent&) override;
129  bool onScroll(const ScrollEvent&) override;
130 
131 private:
132  Image fImage;
133  float fMinimum;
134  float fMaximum;
135  float fScrollStep;
136  float fStep;
137  float fValue;
138  float fValueDef;
139  float fValueTmp;
140  bool fUsingDefault;
141  bool fUsingLog;
142  Orientation fOrientation;
143 
144  int fRotationAngle;
145  bool fDragging;
146  int fLastX;
147  int fLastY;
148 
149  Callback* fCallback;
150 
151  bool fIsImgVertical;
152  uint fImgLayerWidth;
153  uint fImgLayerHeight;
154  uint fImgLayerCount;
155  bool fIsReady;
156  GLuint fTextureId;
157 
158  float _logscale(float value) const;
159  float _invlogscale(float value) const;
160 
161  DISTRHO_LEAK_DETECTOR(ImageKnob)
162 };
163 
164 // -----------------------------------------------------------------------
165 
166 // note set range and step before setting the value
167 
168 class ImageSlider : public Widget
169 {
170 public:
171  class Callback
172  {
173  public:
174  virtual ~Callback() {}
175  virtual void imageSliderDragStarted(ImageSlider* imageSlider) = 0;
176  virtual void imageSliderDragFinished(ImageSlider* imageSlider) = 0;
177  virtual void imageSliderValueChanged(ImageSlider* imageSlider, float value) = 0;
178  };
179 
180  explicit ImageSlider(Window& parent, const Image& image) noexcept;
181  explicit ImageSlider(Widget* widget, const Image& image) noexcept;
182 
183  float getValue() const noexcept;
184  void setValue(float value, bool sendCallback = false) noexcept;
185 
186  void setStartPos(const Point<int>& startPos) noexcept;
187  void setStartPos(int x, int y) noexcept;
188  void setEndPos(const Point<int>& endPos) noexcept;
189  void setEndPos(int x, int y) noexcept;
190 
191  void setInverted(bool inverted) noexcept;
192  void setRange(float min, float max) noexcept;
193  void setStep(float step) noexcept;
194 
195  void setCallback(Callback* callback) noexcept;
196 
197 protected:
198  void onDisplay() override;
199  bool onMouse(const MouseEvent&) override;
200  bool onMotion(const MotionEvent&) override;
201 
202 private:
203  Image fImage;
204  float fMinimum;
205  float fMaximum;
206  float fStep;
207  float fValue;
208  float fValueTmp;
209 
210  bool fDragging;
211  bool fInverted;
212  bool fValueIsSet;
213  int fStartedX;
214  int fStartedY;
215 
216  Callback* fCallback;
217 
218  Point<int> fStartPos;
219  Point<int> fEndPos;
220  Rectangle<int> fSliderArea;
221 
222  void _recheckArea() noexcept;
223 
224  // these should not be used
225  void setAbsoluteX(int) const noexcept {}
226  void setAbsoluteY(int) const noexcept {}
227  void setAbsolutePos(int, int) const noexcept {}
228  void setAbsolutePos(const Point<int>&) const noexcept {}
229 
230  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImageSlider)
231 };
232 
233 // -----------------------------------------------------------------------
234 
235 class ImageSwitch : public Widget
236 {
237 public:
238  class Callback
239  {
240  public:
241  virtual ~Callback() {}
242  virtual void imageSwitchClicked(ImageSwitch* imageButton, bool down) = 0;
243  };
244 
245  explicit ImageSwitch(Window& parent, const Image& imageNormal, const Image& imageDown) noexcept;
246  explicit ImageSwitch(Widget* widget, const Image& imageNormal, const Image& imageDown) noexcept;
247  explicit ImageSwitch(const ImageSwitch& imageSwitch) noexcept;
248  ImageSwitch& operator=(const ImageSwitch& imageSwitch) noexcept;
249 
250  bool isDown() const noexcept;
251  void setDown(bool down) noexcept;
252 
253  void setCallback(Callback* callback) noexcept;
254 
255 protected:
256  void onDisplay() override;
257  bool onMouse(const MouseEvent&) override;
258 
259 private:
260  Image fImageNormal;
261  Image fImageDown;
262  bool fIsDown;
263 
264  Callback* fCallback;
265 
266  DISTRHO_LEAK_DETECTOR(ImageSwitch)
267 };
268 
269 // -----------------------------------------------------------------------
270 
271 END_NAMESPACE_DGL
272 
273 #endif // DGL_IMAGE_WIDGETS_HPP_INCLUDED
Definition: ImageWidgets.hpp:28
virtual bool onScroll(const ScrollEvent &)
Definition: Window.hpp:30
Definition: ImageWidgets.hpp:171
Definition: ImageWidgets.hpp:51
Definition: Widget.hpp:83
void setAbsolutePos(int x, int y) noexcept
void setAbsoluteX(int x) noexcept
Definition: ImageWidgets.hpp:95
bool onKeyboard(const KeyboardEvent &) override
Definition: Widget.hpp:136
void setAbsoluteY(int y) noexcept
void onDisplay() override
virtual bool onMotion(const MotionEvent &)
Definition: Widget.hpp:59
Definition: Widget.hpp:118
bool onMouse(const MouseEvent &) override
Definition: Image.hpp:38
Definition: ImageWidgets.hpp:87
Definition: Widget.hpp:151
Definition: ImageWidgets.hpp:54
Definition: ImageWidgets.hpp:238
Definition: ImageWidgets.hpp:168
Definition: ImageWidgets.hpp:235