Dusk Audio Framework
Loading...
Searching...
No Matches
EventHandlers.hpp
1/*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2025 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
22START_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
37class ButtonEventHandler
38{
39public:
40 enum State {
41 kButtonStateDefault = 0x0,
42 kButtonStateHover = 0x1,
43 kButtonStateActive = 0x2,
44 kButtonStateActiveHover = kButtonStateActive|kButtonStateHover
45 };
46
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 bool isEnabled() const noexcept;
67 void setEnabled(bool enabled, bool appliesToEventInput = true) noexcept;
68
69 Point<double> getLastClickPosition() const noexcept;
70 Point<double> getLastMotionPosition() const noexcept;
71
72 void setCallback(Callback* callback) noexcept;
73
74 bool mouseEvent(const Widget::MouseEvent& ev);
75 bool motionEvent(const Widget::MotionEvent& ev);
76
77protected:
78 State getState() const noexcept;
79 void clearState() noexcept;
80
81 virtual void stateChanged(State state, State oldState);
82
83 void setInternalCallback(Callback* callback) noexcept;
84 void triggerUserCallback(SubWidget* widget, int button);
85
86private:
87 struct PrivateData;
88 PrivateData* const pData;
89
90 DAF_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ButtonEventHandler)
91};
92
93// --------------------------------------------------------------------------------------------------------------------
94
95class KnobEventHandler
96{
97public:
98 enum Orientation {
99 Horizontal,
100 Vertical,
101 Both
102 };
103
104 // NOTE hover not implemented yet
105 enum State {
106 kKnobStateDefault = 0x0,
107 kKnobStateHover = 0x1,
108 kKnobStateDragging = 0x2,
109 kKnobStateDraggingHover = kKnobStateDragging|kKnobStateHover
110 };
111
113 {
114 public:
115 virtual ~Callback() {}
116 virtual void knobDragStarted(SubWidget* widget) = 0;
117 virtual void knobDragFinished(SubWidget* widget) = 0;
118 virtual void knobValueChanged(SubWidget* widget, float value) = 0;
119 virtual void knobDoubleClicked(SubWidget*) {};
120 };
121
122 explicit KnobEventHandler(SubWidget* self);
123 explicit KnobEventHandler(SubWidget* self, const KnobEventHandler& other);
124 KnobEventHandler& operator=(const KnobEventHandler& other);
125 virtual ~KnobEventHandler();
126
127 bool isEnabled() const noexcept;
128 void setEnabled(bool enabled, bool appliesToEventInput = true) noexcept;
129
130 // if setStep(1) has been called before, this returns true
131 bool isInteger() const noexcept;
132
133 // returns raw value, is assumed to be scaled if using log
134 float getValue() const noexcept;
135
136 // NOTE: value is assumed to be scaled if using log
137 virtual bool setValue(float value, bool sendCallback = false) noexcept;
138
139 // returns 0-1 ranged value, already with log scale as needed
140 float getNormalizedValue() const noexcept;
141
142 float getDefault() const noexcept;
143
144 // NOTE: value is assumed to be scaled if using log
145 void setDefault(float def) noexcept;
146
147 float getMinimum() const noexcept;
148 float getMaximum() const noexcept;
149
150 // NOTE: value is assumed to be scaled if using log
151 void setRange(float min, float max) noexcept;
152
153 void setStep(float step) noexcept;
154
155 void setUsingLogScale(bool yesNo) noexcept;
156
157 Orientation getOrientation() const noexcept;
158 void setOrientation(Orientation orientation) noexcept;
159
160 void setCallback(Callback* callback) noexcept;
161
162 // default 200, higher means slower
163 void setMouseDeceleration(float accel) noexcept;
164
165 bool mouseEvent(const Widget::MouseEvent& ev, double scaleFactor = 1.0);
166 bool motionEvent(const Widget::MotionEvent& ev, double scaleFactor = 1.0);
167 bool scrollEvent(const Widget::ScrollEvent& ev);
168
169protected:
170 State getState() const noexcept;
171
172private:
173 struct PrivateData;
174 PrivateData* const pData;
175
176 /* not for use */
177#ifdef DAF_PROPER_CPP11_SUPPORT
178 KnobEventHandler(KnobEventHandler& other) = delete;
179 KnobEventHandler(const KnobEventHandler& other) = delete;
180#else
181 KnobEventHandler(KnobEventHandler& other);
182 KnobEventHandler(const KnobEventHandler& other);
183#endif
184
185 DAF_LEAK_DETECTOR(KnobEventHandler)
186};
187
188// --------------------------------------------------------------------------------------------------------------------
189
190class SliderEventHandler
191{
192public:
193 explicit SliderEventHandler(SubWidget* self);
194 virtual ~SliderEventHandler();
195
196private:
197 struct PrivateData;
198 PrivateData* const pData;
199
200 DAF_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SliderEventHandler)
201};
202
203// --------------------------------------------------------------------------------------------------------------------
204
205END_NAMESPACE_DGL
206
207#endif // DGL_EVENT_HANDLERS_HPP_INCLUDED
208
Definition EventHandlers.hpp:48
Definition EventHandlers.hpp:113
Definition Geometry.hpp:41
Definition SubWidget.hpp:40
Definition Widget.hpp:56