aboutsummaryrefslogtreecommitdiff
path: root/src/input/touch/generic/GenericTouchSwipeDetector.cpp
blob: 2b792724c3db92955097cc34bfa8fa783d53e83d (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 *      Copyright (C) 2013 Team XBMC
 *      http://xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#include <stdlib.h>

#include "GenericTouchSwipeDetector.h"

// maximum time between touch down and up (in nanoseconds)
#define SWIPE_MAX_TIME            500000000
// maxmium swipe distance between touch down and up (in multiples of screen DPI)
#define SWIPE_MIN_DISTANCE        0.5f
// original maximum variance of the touch movement
#define SWIPE_MAX_VARIANCE        0.2f
// tangens of the maximum angle (20 degrees) the touch movement may vary in a
// direction perpendicular to the swipe direction (in radians)
// => tan(20 deg) = tan(20 * M_PI / 180)
#define SWIPE_MAX_VARIANCE_ANGLE  0.36397023f

CGenericTouchSwipeDetector::CGenericTouchSwipeDetector(ITouchActionHandler *handler, float dpi)
  : IGenericTouchGestureDetector(handler, dpi),
    m_directions(TouchMoveDirectionLeft | TouchMoveDirectionRight | TouchMoveDirectionUp | TouchMoveDirectionDown),
    m_swipeDetected(false), m_size(0)
{ }

bool CGenericTouchSwipeDetector::OnTouchDown(unsigned int index, const Pointer &pointer)
{
  if (index < 0 || index >= TOUCH_MAX_POINTERS)
    return false;

  m_size += 1;
  if (m_size > 1)
    return true;

  // reset all values
  m_done = false;
  m_swipeDetected = false;
  m_directions = TouchMoveDirectionLeft | TouchMoveDirectionRight | TouchMoveDirectionUp | TouchMoveDirectionDown;

  return true;
}

bool CGenericTouchSwipeDetector::OnTouchUp(unsigned int index, const Pointer &pointer)
{
  if (index < 0 || index >= TOUCH_MAX_POINTERS)
    return false;

  m_size -= 1;
  if (m_done)
    return false;

  m_done = true;

  // check if a swipe has been detected and if it has a valid direction
  if (!m_swipeDetected || m_directions == TouchMoveDirectionNone)
    return false;

  // check if the swipe has been performed in the proper time span
  if ((pointer.current.time - pointer.down.time) > SWIPE_MAX_TIME)
    return false;

  // calculate the velocity of the swipe
  float velocityX = 0.0f; // number of pixels per second
  float velocityY = 0.0f; // number of pixels per second
  pointer.velocity(velocityX, velocityY, false);

  // call the OnSwipe() callback
  OnSwipe((TouchMoveDirection)m_directions, pointer.down.x, pointer.down.y, pointer.current.x, pointer.current.y, velocityX, velocityY, m_size + 1);
  return true;
}

bool CGenericTouchSwipeDetector::OnTouchMove(unsigned int index, const Pointer &pointer)
{
  if (index < 0 || index >= TOUCH_MAX_POINTERS)
    return false;

  // only handle swipes of moved pointers
  if (index >= m_size || m_done || !pointer.moving)
    return false;

  float deltaXmovement = pointer.current.x - pointer.last.x;
  float deltaYmovement = pointer.current.y - pointer.last.y;

  if (deltaXmovement > 0.0f)
    m_directions &= ~TouchMoveDirectionLeft;
  else if (deltaXmovement < 0.0f)
    m_directions &= ~TouchMoveDirectionRight;

  if (deltaYmovement > 0.0f)
    m_directions &= ~TouchMoveDirectionUp;
  else if (deltaYmovement < 0.0f)
    m_directions &= ~TouchMoveDirectionDown;

  if (m_directions == TouchMoveDirectionNone)
  {
    m_done = true;
    return false;
  }

  float deltaXabs = abs(pointer.current.x - pointer.down.x);
  float deltaYabs = abs(pointer.current.y - pointer.down.y);
  float varXabs = deltaYabs * SWIPE_MAX_VARIANCE_ANGLE + (m_dpi * SWIPE_MAX_VARIANCE) / 2;
  float varYabs = deltaXabs * SWIPE_MAX_VARIANCE_ANGLE + (m_dpi * SWIPE_MAX_VARIANCE) / 2;

  if (m_directions & TouchMoveDirectionLeft)
  {
    // check if the movement went too much in Y direction
    if (deltaYabs > varYabs)
      m_directions &= ~TouchMoveDirectionLeft;
    // check if the movement went far enough in the X direction
    else if (deltaXabs > m_dpi * SWIPE_MIN_DISTANCE)
      m_swipeDetected = true;
  }

  if (m_directions & TouchMoveDirectionRight)
  {
    // check if the movement went too much in Y direction
    if (deltaYabs > varYabs)
      m_directions &= ~TouchMoveDirectionRight;
    // check if the movement went far enough in the X direction
    else if (deltaXabs > m_dpi * SWIPE_MIN_DISTANCE)
      m_swipeDetected = true;
  }
  
  if (m_directions & TouchMoveDirectionUp)
  {
    // check if the movement went too much in X direction
    if (deltaXabs > varXabs)
      m_directions &= ~TouchMoveDirectionUp;
    // check if the movement went far enough in the Y direction
    else if (deltaYabs > m_dpi * SWIPE_MIN_DISTANCE)
      m_swipeDetected = true;
  }

  if (m_directions & TouchMoveDirectionDown)
  {
    // check if the movement went too much in X direction
    if (deltaXabs > varXabs)
      m_directions &= ~TouchMoveDirectionDown;
    // check if the movement went far enough in the Y direction
    else if (deltaYabs > m_dpi * SWIPE_MIN_DISTANCE)
      m_swipeDetected = true;
  }

  if (m_directions == TouchMoveDirectionNone)
  {
    m_done = true;
    return false;
  }
  
  return true;
}

bool CGenericTouchSwipeDetector::OnTouchUpdate(unsigned int index, const Pointer &pointer)
{
  if (index < 0 || index >= TOUCH_MAX_POINTERS)
    return false;

  if (m_done)
    return true;

  return OnTouchMove(index, pointer);
}