Home | History | Annotate | Download | only in ndk_helper
      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 //--------------------------------------------------------------------------------
     35 // Constants
     36 //--------------------------------------------------------------------------------
     37 const int32_t DOUBLE_TAP_TIMEOUT = 300 * 1000000;
     38 const int32_t TAP_TIMEOUT = 180 * 1000000;
     39 const int32_t DOUBLE_TAP_SLOP = 100;
     40 const int32_t TOUCH_SLOP = 8;
     41 
     42 enum
     43 {
     44     GESTURE_STATE_NONE = 0,
     45     GESTURE_STATE_START = 1,
     46     GESTURE_STATE_MOVE = 2,
     47     GESTURE_STATE_END = 4,
     48     GESTURE_STATE_ACTION = (GESTURE_STATE_START | GESTURE_STATE_END),
     49 };
     50 typedef int32_t GESTURE_STATE;
     51 
     52 /******************************************************************
     53  * Base class of Gesture Detectors
     54  * GestureDetectors handles input events and detect gestures
     55  * Note that different detectors may detect gestures with an event at
     56  * same time. The caller needs to manage gesture priority accordingly
     57  *
     58  */
     59 class GestureDetector
     60 {
     61 protected:
     62     float dp_factor_;
     63 public:
     64     GestureDetector();
     65     virtual ~GestureDetector()
     66     {
     67     }
     68     virtual void SetConfiguration( AConfiguration* config );
     69 
     70     virtual GESTURE_STATE Detect( const AInputEvent* motion_event ) = 0;
     71 };
     72 
     73 /******************************************************************
     74  * Tap gesture detector
     75  * Returns GESTURE_STATE_ACTION when a tap gesture is detected
     76  *
     77  */
     78 class TapDetector: public GestureDetector
     79 {
     80 private:
     81     int32_t down_pointer_id_;
     82     float down_x_;
     83     float down_y_;
     84 public:
     85     TapDetector()
     86     {
     87     }
     88     virtual ~TapDetector()
     89     {
     90     }
     91     virtual GESTURE_STATE Detect( const AInputEvent* motion_event );
     92 };
     93 
     94 /******************************************************************
     95  * Pinch gesture detector
     96  * Returns GESTURE_STATE_ACTION when a double-tap gesture is detected
     97  *
     98  */
     99 class DoubletapDetector: public GestureDetector
    100 {
    101 private:
    102     TapDetector tap_detector_;
    103     int64_t last_tap_time_;
    104     float last_tap_x_;
    105     float last_tap_y_;
    106 
    107 public:
    108     DoubletapDetector()
    109     {
    110     }
    111     virtual ~DoubletapDetector()
    112     {
    113     }
    114     virtual GESTURE_STATE Detect( const AInputEvent* motion_event );
    115     virtual void SetConfiguration( AConfiguration* config );
    116 };
    117 
    118 /******************************************************************
    119  * Double gesture detector
    120  * Returns pinch gesture state when a pinch gesture is detected
    121  * The class handles multiple touches more than 2
    122  * When the finger 1,2,3 are tapped and then finger 1 is released,
    123  * the detector start new pinch gesture with finger 2 & 3.
    124  */
    125 class PinchDetector: public GestureDetector
    126 {
    127 private:
    128     int32_t FindIndex( const AInputEvent* event, int32_t id );
    129     const AInputEvent* event_;
    130     std::vector<int32_t> vec_pointers_;
    131 
    132 public:
    133     PinchDetector()
    134     {
    135     }
    136     virtual ~PinchDetector()
    137     {
    138     }
    139     virtual GESTURE_STATE Detect( const AInputEvent* event );
    140     bool GetPointers( Vec2& v1, Vec2& v2 );
    141 };
    142 
    143 /******************************************************************
    144  * Drag gesture detector
    145  * Returns drag gesture state when a drag-tap gesture is detected
    146  *
    147  */
    148 class DragDetector: public GestureDetector
    149 {
    150 private:
    151     int32_t FindIndex( const AInputEvent* event, int32_t id );
    152     const AInputEvent* event_;
    153     std::vector<int32_t> vec_pointers_;
    154 public:
    155     DragDetector()
    156     {
    157     }
    158     virtual ~DragDetector()
    159     {
    160     }
    161     virtual GESTURE_STATE Detect( const AInputEvent* event );
    162     bool GetPointer( Vec2& v );
    163 };
    164 
    165 }   //namespace ndkHelper
    166 #endif /* GESTUREDETECTOR_H_ */
    167