EventHandlers.hpp (6004B)
1 /* 2 * DISTRHO Plugin Framework (DPF) 3 * Copyright (C) 2012-2024 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 37 class ButtonEventHandler 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 92 class KnobEventHandler 93 { 94 public: 95 enum Orientation { 96 Horizontal, 97 Vertical, 98 Both 99 }; 100 101 // NOTE hover not implemented yet 102 enum State { 103 kKnobStateDefault = 0x0, 104 kKnobStateHover = 0x1, 105 kKnobStateDragging = 0x2, 106 kKnobStateDraggingHover = kKnobStateDragging|kKnobStateHover 107 }; 108 109 class Callback 110 { 111 public: 112 virtual ~Callback() {} 113 virtual void knobDragStarted(SubWidget* widget) = 0; 114 virtual void knobDragFinished(SubWidget* widget) = 0; 115 virtual void knobValueChanged(SubWidget* widget, float value) = 0; 116 virtual void knobDoubleClicked(SubWidget*) {}; 117 }; 118 119 explicit KnobEventHandler(SubWidget* self); 120 explicit KnobEventHandler(SubWidget* self, const KnobEventHandler& other); 121 KnobEventHandler& operator=(const KnobEventHandler& other); 122 virtual ~KnobEventHandler(); 123 124 // if setStep(1) has been called before, this returns true 125 bool isInteger() const noexcept; 126 127 // returns raw value, is assumed to be scaled if using log 128 float getValue() const noexcept; 129 130 // NOTE: value is assumed to be scaled if using log 131 virtual bool setValue(float value, bool sendCallback = false) noexcept; 132 133 // returns 0-1 ranged value, already with log scale as needed 134 float getNormalizedValue() const noexcept; 135 136 // NOTE: value is assumed to be scaled if using log 137 void setDefault(float def) noexcept; 138 139 // NOTE: value is assumed to be scaled if using log 140 void setRange(float min, float max) noexcept; 141 142 void setStep(float step) noexcept; 143 144 void setUsingLogScale(bool yesNo) noexcept; 145 146 Orientation getOrientation() const noexcept; 147 void setOrientation(Orientation orientation) noexcept; 148 149 void setCallback(Callback* callback) noexcept; 150 151 // default 200, higher means slower 152 void setMouseDeceleration(float accel) noexcept; 153 154 bool mouseEvent(const Widget::MouseEvent& ev, double scaleFactor = 1.0); 155 bool motionEvent(const Widget::MotionEvent& ev, double scaleFactor = 1.0); 156 bool scrollEvent(const Widget::ScrollEvent& ev); 157 158 protected: 159 State getState() const noexcept; 160 161 private: 162 struct PrivateData; 163 PrivateData* const pData; 164 165 /* not for use */ 166 #ifdef DISTRHO_PROPER_CPP11_SUPPORT 167 KnobEventHandler(KnobEventHandler& other) = delete; 168 KnobEventHandler(const KnobEventHandler& other) = delete; 169 #else 170 KnobEventHandler(KnobEventHandler& other); 171 KnobEventHandler(const KnobEventHandler& other); 172 #endif 173 174 DISTRHO_LEAK_DETECTOR(KnobEventHandler) 175 }; 176 177 // -------------------------------------------------------------------------------------------------------------------- 178 179 class SliderEventHandler 180 { 181 public: 182 explicit SliderEventHandler(SubWidget* self); 183 virtual ~SliderEventHandler(); 184 185 private: 186 struct PrivateData; 187 PrivateData* const pData; 188 189 DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SliderEventHandler) 190 }; 191 192 // -------------------------------------------------------------------------------------------------------------------- 193 194 END_NAMESPACE_DGL 195 196 #endif // DGL_EVENT_HANDLERS_HPP_INCLUDED 197