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