| 1 |
/* |
| 2 |
* Copyright (C) 2013-2024 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef _USE_MATH_DEFINES |
| 10 |
#define _USE_MATH_DEFINES |
| 11 |
#endif |
| 12 |
#include "GenericTouchSwipeDetector.h" |
| 13 |
|
| 14 |
#include <math.h> |
| 15 |
#include <stdlib.h> |
| 16 |
|
| 17 |
// maximum time between touch down and up (in nanoseconds) |
| 18 |
#define SWIPE_MAX_TIME 500000000 |
| 19 |
// maximum swipe distance between touch down and up (in multiples of screen DPI) |
| 20 |
#define SWIPE_MIN_DISTANCE 0.5f |
| 21 |
// original maximum variance of the touch movement |
| 22 |
#define SWIPE_MAX_VARIANCE 0.2f |
| 23 |
// tangents of the maximum angle (20 degrees) the touch movement may vary in a |
| 24 |
// direction perpendicular to the swipe direction (in radians) |
| 25 |
// => tan(20 deg) = tan(20 * M_PI / 180) |
| 26 |
#define SWIPE_MAX_VARIANCE_ANGLE 0.36397023f |
| 27 |
|
| 28 |
CGenericTouchSwipeDetector::CGenericTouchSwipeDetector(ITouchActionHandler* handler, float dpi) |
| 29 |
: IGenericTouchGestureDetector(handler, dpi), |
| 30 |
m_directions(TouchMoveDirectionLeft | TouchMoveDirectionRight | TouchMoveDirectionUp | |
| 31 |
TouchMoveDirectionDown) |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
bool CGenericTouchSwipeDetector::OnTouchDown(unsigned int index, const Pointer& pointer) |
| 36 |
{ |
| 37 |
if (index >= MAX_POINTERS) |
| 38 |
return false; |
| 39 |
|
| 40 |
m_size += 1; |
| 41 |
if (m_size > 1) |
| 42 |
return true; |
| 43 |
|
| 44 |
// reset all values |
| 45 |
m_done = false; |
| 46 |
m_swipeDetected = false; |
| 47 |
m_directions = TouchMoveDirectionLeft | TouchMoveDirectionRight | TouchMoveDirectionUp | |
| 48 |
TouchMoveDirectionDown; |
| 49 |
|
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
bool CGenericTouchSwipeDetector::OnTouchUp(unsigned int index, const Pointer& pointer) |
| 54 |
{ |
| 55 |
if (index >= MAX_POINTERS) |
| 56 |
return false; |
| 57 |
|
| 58 |
m_size -= 1; |
| 59 |
if (m_done) |
| 60 |
return false; |
| 61 |
|
| 62 |
m_done = true; |
| 63 |
|
| 64 |
// check if a swipe has been detected and if it has a valid direction |
| 65 |
if (!m_swipeDetected || m_directions == TouchMoveDirectionNone) |
| 66 |
return false; |
| 67 |
|
| 68 |
// check if the swipe has been performed in the proper time span |
| 69 |
if ((pointer.current.time - pointer.down.time) > SWIPE_MAX_TIME) |
| 70 |
return false; |
| 71 |
|
| 72 |
// calculate the velocity of the swipe |
| 73 |
float velocityX = 0.0f; // number of pixels per second |
| 74 |
float velocityY = 0.0f; // number of pixels per second |
| 75 |
pointer.velocity(velocityX, velocityY, false); |
| 76 |
|
| 77 |
// call the OnSwipe() callback |
| 78 |
OnSwipe((TouchMoveDirection)m_directions, pointer.down.x, pointer.down.y, pointer.current.x, |
| 79 |
pointer.current.y, velocityX, velocityY, m_size + 1); |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
bool CGenericTouchSwipeDetector::OnTouchMove(unsigned int index, const Pointer& pointer) |
| 84 |
{ |
| 85 |
if (index >= MAX_POINTERS) |
| 86 |
return false; |
| 87 |
|
| 88 |
// only handle swipes of moved pointers |
| 89 |
if (index >= m_size || m_done || !pointer.moving) |
| 90 |
return false; |
| 91 |
|
| 92 |
float deltaXmovement = pointer.current.x - pointer.last.x; |
| 93 |
float deltaYmovement = pointer.current.y - pointer.last.y; |
| 94 |
|
| 95 |
if (deltaXmovement > 0.0f) |
| 96 |
m_directions &= ~TouchMoveDirectionLeft; |
| 97 |
else if (deltaXmovement < 0.0f) |
| 98 |
m_directions &= ~TouchMoveDirectionRight; |
| 99 |
|
| 100 |
if (deltaYmovement > 0.0f) |
| 101 |
m_directions &= ~TouchMoveDirectionUp; |
| 102 |
else if (deltaYmovement < 0.0f) |
| 103 |
m_directions &= ~TouchMoveDirectionDown; |
| 104 |
|
| 105 |
if (m_directions == TouchMoveDirectionNone) |
| 106 |
{ |
| 107 |
m_done = true; |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
float deltaXabs = fabs(pointer.current.x - pointer.down.x); |
| 112 |
float deltaYabs = fabs(pointer.current.y - pointer.down.y); |
| 113 |
float varXabs = deltaYabs * SWIPE_MAX_VARIANCE_ANGLE + (m_dpi * SWIPE_MAX_VARIANCE) / 2; |
| 114 |
float varYabs = deltaXabs * SWIPE_MAX_VARIANCE_ANGLE + (m_dpi * SWIPE_MAX_VARIANCE) / 2; |
| 115 |
|
| 116 |
if (m_directions & TouchMoveDirectionLeft) |
| 117 |
{ |
| 118 |
// check if the movement went too much in Y direction |
| 119 |
if (deltaYabs > varYabs) |
| 120 |
m_directions &= ~TouchMoveDirectionLeft; |
| 121 |
// check if the movement went far enough in the X direction |
| 122 |
else if (deltaXabs > m_dpi * SWIPE_MIN_DISTANCE) |
| 123 |
m_swipeDetected = true; |
| 124 |
} |
| 125 |
|
| 126 |
if (m_directions & TouchMoveDirectionRight) |
| 127 |
{ |
| 128 |
// check if the movement went too much in Y direction |
| 129 |
if (deltaYabs > varYabs) |
| 130 |
m_directions &= ~TouchMoveDirectionRight; |
| 131 |
// check if the movement went far enough in the X direction |
| 132 |
else if (deltaXabs > m_dpi * SWIPE_MIN_DISTANCE) |
| 133 |
m_swipeDetected = true; |
| 134 |
} |
| 135 |
|
| 136 |
if (m_directions & TouchMoveDirectionUp) |
| 137 |
{ |
| 138 |
// check if the movement went too much in X direction |
| 139 |
if (deltaXabs > varXabs) |
| 140 |
m_directions &= ~TouchMoveDirectionUp; |
| 141 |
// check if the movement went far enough in the Y direction |
| 142 |
else if (deltaYabs > m_dpi * SWIPE_MIN_DISTANCE) |
| 143 |
m_swipeDetected = true; |
| 144 |
} |
| 145 |
|
| 146 |
if (m_directions & TouchMoveDirectionDown) |
| 147 |
{ |
| 148 |
// check if the movement went too much in X direction |
| 149 |
if (deltaXabs > varXabs) |
| 150 |
m_directions &= ~TouchMoveDirectionDown; |
| 151 |
// check if the movement went far enough in the Y direction |
| 152 |
else if (deltaYabs > m_dpi * SWIPE_MIN_DISTANCE) |
| 153 |
m_swipeDetected = true; |
| 154 |
} |
| 155 |
|
| 156 |
if (m_directions == TouchMoveDirectionNone) |
| 157 |
{ |
| 158 |
m_done = true; |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
bool CGenericTouchSwipeDetector::OnTouchUpdate(unsigned int index, const Pointer& pointer) |
| 166 |
{ |
| 167 |
if (index >= MAX_POINTERS) |
| 168 |
return false; |
| 169 |
|
| 170 |
if (m_done) |
| 171 |
return true; |
| 172 |
|
| 173 |
return OnTouchMove(index, pointer); |
| 174 |
} |