Diligent Engine API Reference: D:/GitHub/DiligentEngine/DiligentCore/External/Android/ndk_helper/include/gestureDetector.h Source File

Diligent Engine API Reference

gestureDetector.h
1 /*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //--------------------------------------------------------------------------------
18 // gestureDetector.h
19 //--------------------------------------------------------------------------------
20 #ifndef GESTUREDETECTOR_H_
21 #define GESTUREDETECTOR_H_
22
23 #include <vector>
24
25 #include <android/sensor.h>
26 #include <android/log.h>
27 #include <android_native_app_glue.h>
28 #include <android/native_window_jni.h>
29 #include "JNIHelper.h"
30 #include "vecmath.h"
31
32 namespace ndk_helper {
33 //--------------------------------------------------------------------------------
34 // Constants
35 //--------------------------------------------------------------------------------
36 const int32_t DOUBLE_TAP_TIMEOUT = 300 * 1000000;
37 const int32_t TAP_TIMEOUT = 180 * 1000000;
38 const int32_t DOUBLE_TAP_SLOP = 100;
39 const int32_t TOUCH_SLOP = 8;
40
41 enum {
42 GESTURE_STATE_NONE = 0,
43 GESTURE_STATE_START = 1,
44 GESTURE_STATE_MOVE = 2,
45 GESTURE_STATE_END = 4,
46 GESTURE_STATE_ACTION = (GESTURE_STATE_START | GESTURE_STATE_END),
47 };
48 typedef int32_t GESTURE_STATE;
49
50 /******************************************************************
51 * Base class of Gesture Detectors
52 * GestureDetectors handles input events and detect gestures
53 * Note that different detectors may detect gestures with an event at
54 * same time. The caller needs to manage gesture priority accordingly
55 *
56 */
57 class GestureDetector {
58 protected:
59 float dp_factor_;
60
61 public:
62 GestureDetector();
63 virtual ~GestureDetector() {}
64 virtual void SetConfiguration(AConfiguration* config);
65
66 virtual GESTURE_STATE Detect(const AInputEvent* motion_event) = 0;
67 };
68
69 /******************************************************************
70 * Tap gesture detector
71 * Returns GESTURE_STATE_ACTION when a tap gesture is detected
72 *
73 */
74 class TapDetector : public GestureDetector {
75 private:
76 int32_t down_pointer_id_;
77 float down_x_;
78 float down_y_;
79
80 public:
81 TapDetector();
82 virtual ~TapDetector() {}
83 virtual GESTURE_STATE Detect(const AInputEvent* motion_event);
84 };
85
86 /******************************************************************
87 * Pinch gesture detector
88 * Returns GESTURE_STATE_ACTION when a double-tap gesture is detected
89 *
90 */
91 class DoubletapDetector : public GestureDetector {
92 private:
93 TapDetector tap_detector_;
94 int64_t last_tap_time_;
95 float last_tap_x_;
96 float last_tap_y_;
97
98 public:
99 DoubletapDetector();
100 virtual ~DoubletapDetector() {}
101 virtual GESTURE_STATE Detect(const AInputEvent* motion_event);
102 virtual void SetConfiguration(AConfiguration* config);
103 };
104
105 /******************************************************************
106 * Double gesture detector
107 * Returns pinch gesture state when a pinch gesture is detected
108 * The class handles multiple touches more than 2
109 * When the finger 1,2,3 are tapped and then finger 1 is released,
110 * the detector start new pinch gesture with finger 2 & 3.
111 */
112 class PinchDetector : public GestureDetector {
113 private:
114 int32_t FindIndex(const AInputEvent* event, int32_t id);
115 const AInputEvent* event_;
116 std::vector<int32_t> vec_pointers_;
117
118 public:
119 PinchDetector() {}
120 virtual ~PinchDetector() {}
121 virtual GESTURE_STATE Detect(const AInputEvent* event);
122 bool GetPointers(Vec2& v1, Vec2& v2);
123 };
124
125 /******************************************************************
126 * Drag gesture detector
127 * Returns drag gesture state when a drag-tap gesture is detected
128 *
129 */
130 class DragDetector : public GestureDetector {
131 private:
132 int32_t FindIndex(const AInputEvent* event, int32_t id);
133 const AInputEvent* event_;
134 std::vector<int32_t> vec_pointers_;
135
136 public:
137 DragDetector() : event_(nullptr) {}
138 virtual ~DragDetector() {}
139 virtual GESTURE_STATE Detect(const AInputEvent* event);
140 bool GetPointer(Vec2& v);
141 };
142
143 } // namespace ndkHelper
144 #endif /* GESTUREDETECTOR_H_ */
ndk_helper
Definition: gestureDetector.h:32