NAP
inputevent.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "keyboard.h"
9 #include "controller.h"
10 
11 // External Includes
12 #include <rtti/rtti.h>
13 #include <nap/numeric.h>
14 #include <nap/event.h>
15 #include <string.h>
16 
17 namespace nap
18 {
19  namespace input
20  {
21  inline constexpr int invalid = -1;
22  }
23 
27  class NAPAPI InputEvent : public Event
28  {
29  RTTI_ENABLE(Event)
30  };
31 
32 
37  class NAPAPI WindowInputEvent : public InputEvent
38  {
39  RTTI_ENABLE(InputEvent)
40  public:
41  WindowInputEvent(int window) : mWindow(window) { }
42  int mWindow;
43  };
44 
45 
47  // Keyboard Input Events
49 
55  class NAPAPI KeyEvent : public WindowInputEvent
56  {
57  RTTI_ENABLE(WindowInputEvent)
58  public:
59  KeyEvent(EKeyCode inKey, KeyModifier inMod, int window) : WindowInputEvent(window),
60  mKey(inKey), mModifier(inMod)
61  { }
62 
65  };
66 
67 
71  class NAPAPI KeyPressEvent : public KeyEvent
72  {
73  RTTI_ENABLE(KeyEvent)
74  public:
75  KeyPressEvent(EKeyCode inKey, KeyModifier inMod, int window) :
76  KeyEvent(inKey, inMod, window)
77  { }
78 
82  nap::uint toUtf8() const { return nap::toUtf8(mKey, mModifier); }
83  };
84 
85 
89  class NAPAPI KeyReleaseEvent : public KeyEvent
90  {
91  RTTI_ENABLE(KeyEvent)
92  public:
93  KeyReleaseEvent(EKeyCode inKey, KeyModifier inMod, int window) :
94  KeyEvent(inKey, inMod, window)
95  { }
96  };
97 
98 
99 
101  // Text input event
103 
107  class NAPAPI TextInputEvent : public WindowInputEvent
108  {
109  RTTI_ENABLE(WindowInputEvent)
110  public:
111  TextInputEvent(const std::string& text, int window) :
112  WindowInputEvent(window), mText(text)
113  { }
114 
115  std::string mText;
116  };
117 
118 
120  // Pointer Input Events, always associated with a with a window
122 
127  class NAPAPI PointerEvent : public WindowInputEvent
128  {
129  RTTI_ENABLE(WindowInputEvent)
130  public:
131 
135  enum class ESource : int8
136  {
137  Mouse = 0,
138  Touch = 1
139  };
140 
141  PointerEvent(int inX, int inY, int window, ESource origin) :
142  WindowInputEvent(window), mX(inX), mY(inY), mSource(origin)
143  { }
144 
145  int mX;
146  int mY;
147  ESource mSource = ESource::Mouse;
148  };
149 
150 
154  class NAPAPI PointerClickEvent : public PointerEvent
155  {
156  RTTI_ENABLE(PointerEvent)
157  public:
158 
162  enum class EButton : int8
163  {
164  UNKNOWN = -1,
165  LEFT = 0,
166  RIGHT = 1,
167  MIDDLE = 2
168  };
169 
170  PointerClickEvent(int inX, int inY, EButton inButton, int window, ESource source) :
171  PointerEvent(inX, inY, window, source),
172  mButton(inButton)
173  { }
174 
176  };
177 
178 
182  class NAPAPI PointerPressEvent : public PointerClickEvent
183  {
184  RTTI_ENABLE(PointerClickEvent)
185  public:
186  PointerPressEvent(int inX, int inY, EButton inButton, int window, ESource source) :
187  PointerClickEvent(inX, inY, inButton, window, source)
188  { }
189  };
190 
191 
196  {
197  RTTI_ENABLE(PointerClickEvent)
198  public:
199  PointerReleaseEvent (int inX, int inY, EButton inButton, int window, ESource source) :
200  PointerClickEvent(inX, inY, inButton, window, source)
201  { }
202  };
203 
204 
208  class NAPAPI PointerMoveEvent : public PointerEvent
209  {
210  RTTI_ENABLE(PointerEvent)
211  public:
212  PointerMoveEvent(int relX, int relY, int inAbsX, int inAbsY, int window, ESource source) :
213  PointerEvent(inAbsX, inAbsY, window, source),
214  mRelX(relX),
215  mRelY(relY)
216  { }
217 
218  int mRelX;
219  int mRelY;
220  };
221 
222 
226  class NAPAPI MouseWheelEvent : public WindowInputEvent
227  {
228  RTTI_ENABLE(WindowInputEvent)
229  public:
230  MouseWheelEvent(int x, int y, int window=0) :
231  WindowInputEvent(window),
232  mX(x),
233  mY(y)
234  { }
235 
236  int mX;
237  int mY;
238  };
239 
240 
242  // Touch Input Events
244 
248  class NAPAPI TouchEvent : public WindowInputEvent
249  {
250  RTTI_ENABLE(WindowInputEvent)
251  public:
252  TouchEvent(int fingerID, int touchID, float x, float y, float pressure, int window = input::invalid, int wx = input::invalid, int wy = input::invalid) :
253  WindowInputEvent(window),
254  mFingerID(fingerID),
255  mTouchID(touchID),
256  mX(x), mY(y),
257  mPressure(pressure),
258  mXCoordinate(wx), mYCoordinate(wy)
259  { }
260 
261  int mFingerID;
262  int mTouchID;
263  float mX;
264  float mY;
265  float mPressure;
268 
273  bool hasWindow() const { return mWindow != input::invalid; }
274  };
275 
276 
280  class NAPAPI TouchPressEvent : public TouchEvent
281  {
282  RTTI_ENABLE(TouchEvent)
283  public:
284  TouchPressEvent(int fingerID, int touchID, float x, float y, float pressure, int window = input::invalid, int wx = input::invalid, int wy = input::invalid) :
285  TouchEvent(fingerID, touchID, x, y, pressure, window, wx, wy)
286  { }
287  };
288 
289 
293  class NAPAPI TouchReleaseEvent : public TouchEvent
294  {
295  RTTI_ENABLE(TouchEvent)
296  public:
297  TouchReleaseEvent(int fingerID, int touchID, float x, float y, float pressure, int window = input::invalid, int wx = input::invalid, int wy = input::invalid) :
298  TouchEvent(fingerID, touchID, x, y, pressure, window, wx, wy)
299  { }
300  };
301 
302 
306  class NAPAPI TouchMoveEvent : public TouchEvent
307  {
308  RTTI_ENABLE(TouchEvent)
309  public:
310  TouchMoveEvent(int fingerID, int touchID, float x, float y, float pressure, float dx, float dy, int window = input::invalid, int wx = input::invalid, int wy = input::invalid) :
311  TouchEvent(fingerID, touchID, x, y, pressure, window, wx, wy),
312  mDX (dx), mDY (dy)
313  { }
314 
315  float mDX;
316  float mDY;
317  };
318 
319 
321  // Controller Input Events
323 
327  class NAPAPI ControllerEvent : public InputEvent
328  {
329  RTTI_ENABLE(InputEvent)
330  public:
331  ControllerEvent(int deviceID) :
332  mDeviceID(deviceID) { }
333 
334  int mDeviceID = -1;
335  };
336 
344  class NAPAPI ControllerAxisEvent : public ControllerEvent
345  {
346  RTTI_ENABLE(ControllerEvent)
347  public:
348  ControllerAxisEvent(int deviceID, EControllerAxis axis, int axisID, double value) :
349  ControllerEvent(deviceID),
350  mAxis(axis),
351  mAxisID(axisID),
352  mValue(value) { }
353 
355  int mAxisID = -1;
356  double mValue = 0.0;
357  };
358 
367  {
368  RTTI_ENABLE(ControllerEvent)
369  public:
370  ControllerButtonEvent(int deviceID, EControllerButton button, int buttonID) :
371  ControllerEvent(deviceID),
372  mButton(button),
373  mButtonID(buttonID) { }
374 
376  int mButtonID = -1;
377  };
378 
379 
385  {
386  RTTI_ENABLE(ControllerButtonEvent)
387  public:
388  ControllerButtonPressEvent(int deviceID, EControllerButton button, int buttonID) :
389  ControllerButtonEvent(deviceID, button, buttonID) { }
390  };
391 
392 
398  {
399  RTTI_ENABLE(ControllerButtonEvent)
400  public:
401  ControllerButtonReleaseEvent(int deviceID, EControllerButton button, int buttonID) :
402  ControllerButtonEvent(deviceID, button, buttonID) { }
403  };
404 
405 
410  {
411  RTTI_ENABLE(ControllerEvent)
412  public:
413  ControllerConnectionEvent(int deviceID, bool connected) :
414  ControllerEvent(deviceID),
415  mConnected(connected) { }
416  bool mConnected = false;
417  };
418 
419 
420  using InputEventPtr = std::unique_ptr<nap::InputEvent>;
421  using InputEventPtrList = std::vector<InputEventPtr>;
422 }
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::ControllerButtonEvent
Definition: inputevent.h:366
nap::EKeyCode
EKeyCode
Definition: keyboard.h:32
nap::TouchMoveEvent::TouchMoveEvent
TouchMoveEvent(int fingerID, int touchID, float x, float y, float pressure, float dx, float dy, int window=input::invalid, int wx=input::invalid, int wy=input::invalid)
Definition: inputevent.h:310
nap::TextInputEvent::TextInputEvent
TextInputEvent(const std::string &text, int window)
Definition: inputevent.h:111
nap::input::invalid
constexpr int invalid
Invalid input ID.
Definition: inputevent.h:21
nap::KeyPressEvent::toUtf8
nap::uint toUtf8() const
Definition: inputevent.h:82
nap::PointerClickEvent::PointerClickEvent
PointerClickEvent(int inX, int inY, EButton inButton, int window, ESource source)
Definition: inputevent.h:170
nap::TouchMoveEvent
Definition: inputevent.h:306
nap::KeyEvent
Definition: inputevent.h:55
nap::InputEventPtrList
std::vector< InputEventPtr > InputEventPtrList
Definition: inputevent.h:421
nap::PointerClickEvent::EButton
EButton
Definition: inputevent.h:162
nap::PointerEvent::mX
int mX
horizontal window coordinate
Definition: inputevent.h:145
nap::KeyPressEvent
Definition: inputevent.h:71
nap::TouchReleaseEvent
Definition: inputevent.h:293
nap::Event
Definition: event.h:17
nap::ControllerButtonEvent::ControllerButtonEvent
ControllerButtonEvent(int deviceID, EControllerButton button, int buttonID)
Definition: inputevent.h:370
nap::KeyReleaseEvent
Definition: inputevent.h:89
nap::PointerEvent::mY
int mY
vertical window coordinate
Definition: inputevent.h:146
nap::KeyPressEvent::KeyPressEvent
KeyPressEvent(EKeyCode inKey, KeyModifier inMod, int window)
Definition: inputevent.h:75
nap::int8
int8_t int8
Definition: numeric.h:15
nap::TouchMoveEvent::mDX
float mDX
The distance moved in the x-axis, normalized (-1-1)
Definition: inputevent.h:315
nap::TouchEvent::mFingerID
int mFingerID
The finger ID.
Definition: inputevent.h:261
nap::ControllerAxisEvent
Definition: inputevent.h:344
nap::PointerReleaseEvent
Definition: inputevent.h:195
nap::TouchPressEvent
Definition: inputevent.h:280
nap::TextInputEvent::mText
std::string mText
text input
Definition: inputevent.h:115
nap::EControllerButton::UNKNOWN
@ UNKNOWN
nap::PointerMoveEvent::mRelY
int mRelY
Vertical relative movement in pixels.
Definition: inputevent.h:219
nap::PointerMoveEvent::PointerMoveEvent
PointerMoveEvent(int relX, int relY, int inAbsX, int inAbsY, int window, ESource source)
Definition: inputevent.h:212
nap::ControllerEvent::ControllerEvent
ControllerEvent(int deviceID)
Definition: inputevent.h:331
nap::PointerEvent::PointerEvent
PointerEvent(int inX, int inY, int window, ESource origin)
Definition: inputevent.h:141
nap::TouchMoveEvent::mDY
float mDY
The distance moved in the y-axis, normalized (-1-1)
Definition: inputevent.h:316
nap::PointerReleaseEvent::PointerReleaseEvent
PointerReleaseEvent(int inX, int inY, EButton inButton, int window, ESource source)
Definition: inputevent.h:199
nap::KeyReleaseEvent::KeyReleaseEvent
KeyReleaseEvent(EKeyCode inKey, KeyModifier inMod, int window)
Definition: inputevent.h:93
nap::PointerEvent
Definition: inputevent.h:127
nap::toUtf8
nap::uint NAPAPI toUtf8(nap::EKeyCode key, KeyModifier mod)
nap::TouchEvent::mX
float mX
The x-axis location of the touch event, normalized(0 - 1)
Definition: inputevent.h:263
nap::EControllerAxis::UNKNOWN
@ UNKNOWN
nap::ControllerAxisEvent::ControllerAxisEvent
ControllerAxisEvent(int deviceID, EControllerAxis axis, int axisID, double value)
Definition: inputevent.h:348
nap::TouchEvent::mYCoordinate
int mYCoordinate
The y-axis window coordinate, if any. -1 otherwise.
Definition: inputevent.h:267
nap::PointerMoveEvent::mRelX
int mRelX
Horizontal relative movement in pixels.
Definition: inputevent.h:218
nap::TouchEvent::TouchEvent
TouchEvent(int fingerID, int touchID, float x, float y, float pressure, int window=input::invalid, int wx=input::invalid, int wy=input::invalid)
Definition: inputevent.h:252
nap::EControllerAxis
EControllerAxis
Definition: controller.h:39
nap::MouseWheelEvent::mX
int mX
Definition: inputevent.h:236
nap::TouchEvent::mY
float mY
The y-axis location of the touch event, normalized(0 - 1)
Definition: inputevent.h:264
nap::WindowInputEvent::WindowInputEvent
WindowInputEvent(int window)
Definition: inputevent.h:41
nap::ControllerButtonPressEvent
Definition: inputevent.h:384
nap::PointerClickEvent
Definition: inputevent.h:154
nap::PointerMoveEvent
Definition: inputevent.h:208
nap::MouseWheelEvent
Definition: inputevent.h:226
nap
Definition: templateapp.h:17
nap::EControllerButton
EControllerButton
Definition: controller.h:15
nap::InputEventPtr
std::unique_ptr< nap::InputEvent > InputEventPtr
Definition: inputevent.h:420
nap::KeyModifier
nap::uint8 KeyModifier
Definition: keyboard.h:16
nap::TextInputEvent
Definition: inputevent.h:107
nap::WindowInputEvent::mWindow
int mWindow
Window ID.
Definition: inputevent.h:42
nap::EMouseButton::LEFT
@ LEFT
nap::ControllerConnectionEvent::ControllerConnectionEvent
ControllerConnectionEvent(int deviceID, bool connected)
Definition: inputevent.h:413
nap::ControllerButtonReleaseEvent::ControllerButtonReleaseEvent
ControllerButtonReleaseEvent(int deviceID, EControllerButton button, int buttonID)
Definition: inputevent.h:401
nap::TouchEvent::hasWindow
bool hasWindow() const
Definition: inputevent.h:273
nap::KeyEvent::KeyEvent
KeyEvent(EKeyCode inKey, KeyModifier inMod, int window)
Definition: inputevent.h:59
nap::EMouseButton::MIDDLE
@ MIDDLE
nap::PointerEvent::ESource
ESource
Definition: inputevent.h:135
nap::EMouseButton::RIGHT
@ RIGHT
nap::TouchPressEvent::TouchPressEvent
TouchPressEvent(int fingerID, int touchID, float x, float y, float pressure, int window=input::invalid, int wx=input::invalid, int wy=input::invalid)
Definition: inputevent.h:284
nap::TouchEvent
Definition: inputevent.h:248
nap::ControllerButtonPressEvent::ControllerButtonPressEvent
ControllerButtonPressEvent(int deviceID, EControllerButton button, int buttonID)
Definition: inputevent.h:388
nap::MouseWheelEvent::mY
int mY
Definition: inputevent.h:237
nap::ControllerButtonReleaseEvent
Definition: inputevent.h:397
nap::TouchEvent::mTouchID
int mTouchID
The touch device ID.
Definition: inputevent.h:262
nap::TouchEvent::mXCoordinate
int mXCoordinate
The x-axis window coordinate, if any. -1 otherwise.
Definition: inputevent.h:266
nap::PointerPressEvent
Definition: inputevent.h:182
nap::InputEvent
Definition: inputevent.h:27
nap::PointerClickEvent::mButton
EButton mButton
clicked mouse button
Definition: inputevent.h:175
nap::WindowInputEvent
Definition: inputevent.h:37
nap::TouchEvent::mPressure
float mPressure
The quantity of the pressure applied, normalized (0 - 1)
Definition: inputevent.h:265
nap::ControllerEvent
Definition: inputevent.h:327
nap::PointerPressEvent::PointerPressEvent
PointerPressEvent(int inX, int inY, EButton inButton, int window, ESource source)
Definition: inputevent.h:186
nap::TouchReleaseEvent::TouchReleaseEvent
TouchReleaseEvent(int fingerID, int touchID, float x, float y, float pressure, int window=input::invalid, int wx=input::invalid, int wy=input::invalid)
Definition: inputevent.h:297
nap::MouseWheelEvent::MouseWheelEvent
MouseWheelEvent(int x, int y, int window=0)
Definition: inputevent.h:230
nap::KeyEvent::mModifier
KeyModifier mModifier
Modifier key bit-mask (shift, ctrl etc.)
Definition: inputevent.h:64
nap::ControllerConnectionEvent
Definition: inputevent.h:409
nap::KeyEvent::mKey
EKeyCode mKey
Associated Key.
Definition: inputevent.h:63