/
GenericTouchSwipeDetector.cpp
157 lines (130 loc) · 5.03 KB
/
GenericTouchSwipeDetector.cpp
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
/*
* Copyright (C) 2013 Team XBMC
* http://www.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/>.
*
*/
#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.5
// maximum distance the touch movement may vary in a direction transversal to
// the swipe direction
#define SWIPE_MAX_VARIANCE 30.0
CGenericTouchSwipeDetector::CGenericTouchSwipeDetector(ITouchActionHandler *handler, float dpi)
: IGenericTouchGestureDetector(handler, dpi),
m_directions(TouchMoveDirectionLeft | TouchMoveDirectionRight | TouchMoveDirectionUp | TouchMoveDirectionDown),
m_swipeDetected(false)
{ }
bool CGenericTouchSwipeDetector::OnTouchDown(unsigned int index, const Pointer &pointer)
{
if (index < 0 || index >= TOUCH_MAX_POINTERS)
return false;
// only handle one-finger swipes
if (index > 0)
return false;
// 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;
// only handle one-finger swipes
if (index > 0 || 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, 1);
return true;
}
bool CGenericTouchSwipeDetector::OnTouchMove(unsigned int index, const Pointer &pointer)
{
if (index < 0 || index >= TOUCH_MAX_POINTERS)
return false;
// only handle one-finger swipes of moved pointers
if (index > 0 || 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 &= ~TouchMoveDirectionDown;
else if (deltaYmovement < 0.0f)
m_directions &= ~TouchMoveDirectionUp;
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);
if (m_directions & TouchMoveDirectionLeft)
{
// check if the movement went too much in Y direction
if (deltaYabs > SWIPE_MAX_VARIANCE)
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 > SWIPE_MAX_VARIANCE)
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 > SWIPE_MAX_VARIANCE)
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 > SWIPE_MAX_VARIANCE)
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;
}
return true;
}