DISTRHO Plugin Framework
EventHandlers.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_EVENT_HANDLERS_HPP_INCLUDED
18 #define DGL_EVENT_HANDLERS_HPP_INCLUDED
19 
20 #include "Widget.hpp"
21 
22 START_NAMESPACE_DGL
23 
24 /* NOTE none of these classes get assigned to a widget automatically
25  Manual plugging into Widget events is needed, like so:
26 
27  ```
28  bool onMouse(const MouseEvent& ev) override
29  {
30  return ButtonEventHandler::mouseEvent(ev);
31  }
32  ```
33 */
34 
35 // --------------------------------------------------------------------------------------------------------------------
36 
38 {
39 public:
40  enum State {
41  kButtonStateDefault = 0x0,
42  kButtonStateHover = 0x1,
43  kButtonStateActive = 0x2,
44  kButtonStateActiveHover = kButtonStateActive|kButtonStateHover
45  };
46 
47  class Callback
48  {
49  public:
50  virtual ~Callback() {}
51  virtual void buttonClicked(SubWidget* widget, int button) = 0;
52  };
53 
54  explicit ButtonEventHandler(SubWidget* self);
55  virtual ~ButtonEventHandler();
56 
57  bool isActive() noexcept;
58  void setActive(bool active, bool sendCallback) noexcept;
59 
60  bool isChecked() const noexcept;
61  void setChecked(bool checked, bool sendCallback) noexcept;
62 
63  bool isCheckable() const noexcept;
64  void setCheckable(bool checkable) noexcept;
65 
66  Point<double> getLastClickPosition() const noexcept;
67  Point<double> getLastMotionPosition() const noexcept;
68 
69  void setCallback(Callback* callback) noexcept;
70 
71  bool mouseEvent(const Widget::MouseEvent& ev);
72  bool motionEvent(const Widget::MotionEvent& ev);
73 
74 protected:
75  State getState() const noexcept;
76  void clearState() noexcept;
77 
78  virtual void stateChanged(State state, State oldState);
79 
80  void setInternalCallback(Callback* callback) noexcept;
81  void triggerUserCallback(SubWidget* widget, int button);
82 
83 private:
84  struct PrivateData;
85  PrivateData* const pData;
86 
87  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ButtonEventHandler)
88 };
89 
90 // --------------------------------------------------------------------------------------------------------------------
91 
93 {
94 public:
95  enum Orientation {
96  Horizontal,
97  Vertical
98  };
99 
100  // NOTE hover not implemented yet
101  enum State {
102  kKnobStateDefault = 0x0,
103  kKnobStateHover = 0x1,
104  kKnobStateDragging = 0x2,
105  kKnobStateDraggingHover = kKnobStateDragging|kKnobStateHover
106  };
107 
108  class Callback
109  {
110  public:
111  virtual ~Callback() {}
112  virtual void knobDragStarted(SubWidget* widget) = 0;
113  virtual void knobDragFinished(SubWidget* widget) = 0;
114  virtual void knobValueChanged(SubWidget* widget, float value) = 0;
115  };
116 
117  explicit KnobEventHandler(SubWidget* self);
118  explicit KnobEventHandler(SubWidget* self, const KnobEventHandler& other);
119  KnobEventHandler& operator=(const KnobEventHandler& other);
120  virtual ~KnobEventHandler();
121 
122  // returns raw value, is assumed to be scaled if using log
123  float getValue() const noexcept;
124 
125  // NOTE: value is assumed to be scaled if using log
126  virtual bool setValue(float value, bool sendCallback = false) noexcept;
127 
128  // returns 0-1 ranged value, already with log scale as needed
129  float getNormalizedValue() const noexcept;
130 
131  // NOTE: value is assumed to be scaled if using log
132  void setDefault(float def) noexcept;
133 
134  // NOTE: value is assumed to be scaled if using log
135  void setRange(float min, float max) noexcept;
136 
137  void setStep(float step) noexcept;
138 
139  void setUsingLogScale(bool yesNo) noexcept;
140 
141  Orientation getOrientation() const noexcept;
142  void setOrientation(const Orientation orientation) noexcept;
143 
144  void setCallback(Callback* callback) noexcept;
145 
146  bool mouseEvent(const Widget::MouseEvent& ev);
147  bool motionEvent(const Widget::MotionEvent& ev);
148  bool scrollEvent(const Widget::ScrollEvent& ev);
149 
150 protected:
151  State getState() const noexcept;
152 
153 private:
154  struct PrivateData;
155  PrivateData* const pData;
156 
157  /* not for use */
158 #ifdef DISTRHO_PROPER_CPP11_SUPPORT
159  KnobEventHandler(KnobEventHandler& other) = delete;
160  KnobEventHandler(const KnobEventHandler& other) = delete;
161 #else
163  KnobEventHandler(const KnobEventHandler& other);
164 #endif
165 
166  DISTRHO_LEAK_DETECTOR(KnobEventHandler)
167 };
168 
169 // --------------------------------------------------------------------------------------------------------------------
170 
171 END_NAMESPACE_DGL
172 
173 #endif // DGL_EVENT_HANDLERS_HPP_INCLUDED
174 
Definition: EventHandlers.hpp:48
Definition: EventHandlers.hpp:38
Definition: EventHandlers.hpp:109
Definition: EventHandlers.hpp:93
Definition: Geometry.hpp:41
Definition: SubWidget.hpp:40
Definition: Widget.hpp:54
Definition: DistrhoPlugin.hpp:677