DISTRHO Plugin Framework
ImageWidgets.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2015 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_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 setValue(float value, bool sendCallback = false) noexcept;
116  void setUsingLogScale(bool yesNo) noexcept;
117 
118  void setCallback(Callback* callback) noexcept;
119  void setOrientation(Orientation orientation) noexcept;
120  void setRotationAngle(int angle);
121 
122  void setImageLayerCount(uint count) noexcept;
123 
124 protected:
125  void onDisplay() override;
126  bool onMouse(const MouseEvent&) override;
127  bool onMotion(const MotionEvent&) override;
128  bool onScroll(const ScrollEvent&) override;
129 
130 private:
131  Image fImage;
132  float fMinimum;
133  float fMaximum;
134  float fStep;
135  float fValue;
136  float fValueDef;
137  float fValueTmp;
138  bool fUsingDefault;
139  bool fUsingLog;
140  Orientation fOrientation;
141 
142  int fRotationAngle;
143  bool fDragging;
144  int fLastX;
145  int fLastY;
146 
147  Callback* fCallback;
148 
149  bool fIsImgVertical;
150  uint fImgLayerWidth;
151  uint fImgLayerHeight;
152  uint fImgLayerCount;
153  bool fIsReady;
154  GLuint fTextureId;
155 
156  float _logscale(float value) const;
157  float _invlogscale(float value) const;
158 
159  DISTRHO_LEAK_DETECTOR(ImageKnob)
160 };
161 
162 // -----------------------------------------------------------------------
163 
164 // note set range and step before setting the value
165 
166 class ImageSlider : public Widget
167 {
168 public:
169  class Callback
170  {
171  public:
172  virtual ~Callback() {}
173  virtual void imageSliderDragStarted(ImageSlider* imageSlider) = 0;
174  virtual void imageSliderDragFinished(ImageSlider* imageSlider) = 0;
175  virtual void imageSliderValueChanged(ImageSlider* imageSlider, float value) = 0;
176  };
177 
178  explicit ImageSlider(Window& parent, const Image& image) noexcept;
179  explicit ImageSlider(Widget* widget, const Image& image) noexcept;
180 
181  float getValue() const noexcept;
182  void setValue(float value, bool sendCallback = false) noexcept;
183 
184  void setStartPos(const Point<int>& startPos) noexcept;
185  void setStartPos(int x, int y) noexcept;
186  void setEndPos(const Point<int>& endPos) noexcept;
187  void setEndPos(int x, int y) noexcept;
188 
189  void setInverted(bool inverted) noexcept;
190  void setRange(float min, float max) noexcept;
191  void setStep(float step) noexcept;
192 
193  void setCallback(Callback* callback) noexcept;
194 
195 protected:
196  void onDisplay() override;
197  bool onMouse(const MouseEvent&) override;
198  bool onMotion(const MotionEvent&) override;
199 
200 private:
201  Image fImage;
202  float fMinimum;
203  float fMaximum;
204  float fStep;
205  float fValue;
206  float fValueTmp;
207 
208  bool fDragging;
209  bool fInverted;
210  bool fValueIsSet;
211  int fStartedX;
212  int fStartedY;
213 
214  Callback* fCallback;
215 
216  Point<int> fStartPos;
217  Point<int> fEndPos;
218  Rectangle<int> fSliderArea;
219 
220  void _recheckArea() noexcept;
221 
222  // these should not be used
223  void setAbsoluteX(int) const noexcept {}
224  void setAbsoluteY(int) const noexcept {}
225  void setAbsolutePos(int, int) const noexcept {}
226  void setAbsolutePos(const Point<int>&) const noexcept {}
227 
228  DISTRHO_LEAK_DETECTOR(ImageSlider)
229 };
230 
231 // -----------------------------------------------------------------------
232 
233 class ImageSwitch : public Widget
234 {
235 public:
236  class Callback
237  {
238  public:
239  virtual ~Callback() {}
240  virtual void imageSwitchClicked(ImageSwitch* imageButton, bool down) = 0;
241  };
242 
243  explicit ImageSwitch(Window& parent, const Image& imageNormal, const Image& imageDown) noexcept;
244  explicit ImageSwitch(Widget* widget, const Image& imageNormal, const Image& imageDown) noexcept;
245  explicit ImageSwitch(const ImageSwitch& imageSwitch) noexcept;
246  ImageSwitch& operator=(const ImageSwitch& imageSwitch) noexcept;
247 
248  bool isDown() const noexcept;
249  void setDown(bool down) noexcept;
250 
251  void setCallback(Callback* callback) noexcept;
252 
253 protected:
254  void onDisplay() override;
255  bool onMouse(const MouseEvent&) override;
256 
257 private:
258  Image fImageNormal;
259  Image fImageDown;
260  bool fIsDown;
261 
262  Callback* fCallback;
263 
264  DISTRHO_LEAK_DETECTOR(ImageSwitch)
265 };
266 
267 // -----------------------------------------------------------------------
268 
269 END_NAMESPACE_DGL
270 
271 #endif // DGL_IMAGE_WIDGETS_HPP_INCLUDED
Definition: ImageWidgets.hpp:28
void onDisplay() override
Definition: Window.hpp:30
bool onMouse(const MouseEvent &) override
void onDisplay() override
Definition: ImageWidgets.hpp:169
Definition: Geometry.hpp:40
Definition: ImageWidgets.hpp:51
Definition: Widget.hpp:83
void setAbsolutePos(int x, int y) noexcept
void setAbsoluteX(int x) noexcept
Definition: ImageWidgets.hpp:95
Definition: Geometry.hpp:30
bool onMouse(const MouseEvent &) override
bool onKeyboard(const KeyboardEvent &) override
Definition: Widget.hpp:136
void setAbsoluteY(int y) noexcept
bool onMotion(const MotionEvent &) override
void onDisplay() override
Definition: Widget.hpp:59
Definition: Widget.hpp:118
bool onMouse(const MouseEvent &) override
Definition: Image.hpp:38
bool onScroll(const ScrollEvent &) override
Definition: ImageWidgets.hpp:87
bool onMotion(const MotionEvent &) override
Definition: Widget.hpp:151
Definition: ImageWidgets.hpp:54
Definition: ImageWidgets.hpp:236
Definition: ImageWidgets.hpp:166
Definition: ImageWidgets.hpp:233