aboutsummaryrefslogtreecommitdiff
path: root/sources/android/ndk_helper/gestureDetector.h
blob: 6c8683bf7278dd24fb672c9e63e7736456e183d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <vector>

#include <android/sensor.h>
#include <android/log.h>
#include <android_native_app_glue.h>
#include <android/native_window_jni.h>
#include "JNIHelper.h"
#include "vecmath.h"

namespace ndk_helper
{
//--------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------
const int32_t DOUBLE_TAP_TIMEOUT = 300 * 1000000;
const int32_t TAP_TIMEOUT = 180 * 1000000;
const int32_t DOUBLE_TAP_SLOP = 100;
const int32_t TOUCH_SLOP = 8;

enum
{
    GESTURE_STATE_NONE = 0,
    GESTURE_STATE_START = 1,
    GESTURE_STATE_MOVE = 2,
    GESTURE_STATE_END = 4,
    GESTURE_STATE_ACTION = (GESTURE_STATE_START | GESTURE_STATE_END),
};
typedef int32_t GESTURE_STATE;

/******************************************************************
 * Base class of Gesture Detectors
 * GestureDetectors handles input events and detect gestures
 * Note that different detectors may detect gestures with an event at
 * same time. The caller needs to manage gesture priority accordingly
 *
 */
class GestureDetector
{
protected:
    float dp_factor_;
public:
    GestureDetector();
    virtual ~GestureDetector()
    {
    }
    virtual void SetConfiguration( AConfiguration* config );

    virtual GESTURE_STATE Detect( const AInputEvent* motion_event ) = 0;
};

/******************************************************************
 * Tap gesture detector
 * Returns GESTURE_STATE_ACTION when a tap gesture is detected
 *
 */
class TapDetector: public GestureDetector
{
private:
    int32_t down_pointer_id_;
    float down_x_;
    float down_y_;
public:
    TapDetector()
    {
    }
    virtual ~TapDetector()
    {
    }
    virtual GESTURE_STATE Detect( const AInputEvent* motion_event );
};

/******************************************************************
 * Pinch gesture detector
 * Returns GESTURE_STATE_ACTION when a double-tap gesture is detected
 *
 */
class DoubletapDetector: public GestureDetector
{
private:
    TapDetector tap_detector_;
    int64_t last_tap_time_;
    float last_tap_x_;
    float last_tap_y_;

public:
    DoubletapDetector()
    {
    }
    virtual ~DoubletapDetector()
    {
    }
    virtual GESTURE_STATE Detect( const AInputEvent* motion_event );
    virtual void SetConfiguration( AConfiguration* config );
};

/******************************************************************
 * Double gesture detector
 * Returns pinch gesture state when a pinch gesture is detected
 * The class handles multiple touches more than 2
 * When the finger 1,2,3 are tapped and then finger 1 is released,
 * the detector start new pinch gesture with finger 2 & 3.
 */
class PinchDetector: public GestureDetector
{
private:
    int32_t FindIndex( const AInputEvent* event, int32_t id );
    const AInputEvent* event_;
    std::vector<int32_t> vec_pointers_;

public:
    PinchDetector()
    {
    }
    virtual ~PinchDetector()
    {
    }
    virtual GESTURE_STATE Detect( const AInputEvent* event );
    bool GetPointers( Vec2& v1, Vec2& v2 );
};

/******************************************************************
 * Drag gesture detector
 * Returns drag gesture state when a drag-tap gesture is detected
 *
 */
class DragDetector: public GestureDetector
{
private:
    int32_t FindIndex( const AInputEvent* event, int32_t id );
    const AInputEvent* event_;
    std::vector<int32_t> vec_pointers_;
public:
    DragDetector()
    {
    }
    virtual ~DragDetector()
    {
    }
    virtual GESTURE_STATE Detect( const AInputEvent* event );
    bool GetPointer( Vec2& v );
};

}   //namespace ndkHelper