Lugdunum  0.1.0
gestureDetector.hpp
Go to the documentation of this file.
1 //--------------------------------------------------------------------------------
2 // gestureDetector.h
3 //--------------------------------------------------------------------------------
4 #ifndef GESTUREDETECTOR_H_
5 #define GESTUREDETECTOR_H_
6 
7 #include <vector>
8 
9 #include <android/sensor.h>
10 #include <android/log.h>
11 // #include <android_native_app_glue.h>
12 #include <android/native_window_jni.h>
13 #include <android/configuration.h>
14 #include <android/input.h>
15 #include <android/keycodes.h>
16 
17 #include <lug/Math/Vector.hpp>
18 
19 // #include "JNIHelper.h"
20 // #include "vecmath.h"
21 
22 namespace ndk_helper {
23 //--------------------------------------------------------------------------------
24 // Constants
25 //--------------------------------------------------------------------------------
26 const int32_t DOUBLE_TAP_TIMEOUT = 300 * 1000000;
27 const int32_t TAP_TIMEOUT = 180 * 1000000;
28 const int32_t DOUBLE_TAP_SLOP = 100;
29 const int32_t TOUCH_SLOP = 8;
30 
31 enum {
37 };
38 typedef int32_t GESTURE_STATE;
39 
40 /******************************************************************
41  * Base class of Gesture Detectors
42  * GestureDetectors handles input events and detect gestures
43  * Note that different detectors may detect gestures with an event at
44  * same time. The caller needs to manage gesture priority accordingly
45  *
46  */
48  protected:
49  float dp_factor_;
50 
51  public:
53  virtual ~GestureDetector() {}
54  virtual void SetConfiguration(AConfiguration* config);
55 
56  virtual GESTURE_STATE Detect(const AInputEvent* motion_event) = 0;
57 };
58 
59 /******************************************************************
60  * Tap gesture detector
61  * Returns GESTURE_STATE_ACTION when a tap gesture is detected
62  *
63  */
64 class TapDetector : public GestureDetector {
65  private:
67  float down_x_;
68  float down_y_;
69 
70  public:
71  TapDetector();
72  virtual ~TapDetector() {}
73  virtual GESTURE_STATE Detect(const AInputEvent* motion_event);
74 };
75 
76 /******************************************************************
77  * Pinch gesture detector
78  * Returns GESTURE_STATE_ACTION when a double-tap gesture is detected
79  *
80  */
82  private:
84  int64_t last_tap_time_{0};
85  float last_tap_x_{0.0f};
86  float last_tap_y_{0.0f};
87 
88  public:
90  virtual ~DoubletapDetector() {}
91  virtual GESTURE_STATE Detect(const AInputEvent* motion_event);
92  virtual void SetConfiguration(AConfiguration* config);
93 };
94 
95 /******************************************************************
96  * Double gesture detector
97  * Returns pinch gesture state when a pinch gesture is detected
98  * The class handles multiple touches more than 2
99  * When the finger 1,2,3 are tapped and then finger 1 is released,
100  * the detector start new pinch gesture with finger 2 & 3.
101  */
103  private:
104  int32_t FindIndex(const AInputEvent* event, int32_t id);
105  const AInputEvent* event_;
106  std::vector<int32_t> vec_pointers_;
107 
108  public:
110  virtual ~PinchDetector() {}
111  virtual GESTURE_STATE Detect(const AInputEvent* event);
112  bool GetPointers(lug::Math::Vec2f& v1, lug::Math::Vec2f& v2);
113 };
114 
115 /******************************************************************
116  * Drag gesture detector
117  * Returns drag gesture state when a drag-tap gesture is detected
118  *
119  */
121  private:
122  int32_t FindIndex(const AInputEvent* event, int32_t id);
123  const AInputEvent* event_;
124  std::vector<int32_t> vec_pointers_;
125 
126  public:
127  DragDetector() : event_(nullptr) {}
128  virtual ~DragDetector() {}
129  virtual GESTURE_STATE Detect(const AInputEvent* event);
130  bool GetPointer(lug::Math::Vec2f& v);
131 };
132 
133 } // namespace ndkHelper
134 #endif /* GESTUREDETECTOR_H_ */
const int32_t TAP_TIMEOUT
const AInputEvent * event_
const int32_t DOUBLE_TAP_SLOP
const int32_t TOUCH_SLOP
const AInputEvent * event_
int32_t GESTURE_STATE
std::vector< int32_t > vec_pointers_
virtual void SetConfiguration(AConfiguration *config)
virtual GESTURE_STATE Detect(const AInputEvent *motion_event)=0
const int32_t DOUBLE_TAP_TIMEOUT
std::vector< int32_t > vec_pointers_