| 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 |
#include "GenericTouchRotateDetector.h" |
| 10 |
|
| 11 |
#include <math.h> |
| 12 |
|
| 13 |
#ifndef M_PI |
| 14 |
#define M_PI 3.1415926535897932384626433832795028842 |
| 15 |
#endif |
| 16 |
|
| 17 |
CGenericTouchRotateDetector::CGenericTouchRotateDetector(ITouchActionHandler* handler, float dpi) |
| 18 |
: IGenericTouchGestureDetector(handler, dpi) |
| 19 |
{ |
| 20 |
} |
| 21 |
|
| 22 |
bool CGenericTouchRotateDetector::OnTouchDown(unsigned int index, const Pointer& pointer) |
| 23 |
{ |
| 24 |
if (index >= MAX_POINTERS) |
| 25 |
return false; |
| 26 |
|
| 27 |
if (m_done) |
| 28 |
return true; |
| 29 |
|
| 30 |
m_pointers[index] = pointer; |
| 31 |
m_angle = 0.0f; |
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
bool CGenericTouchRotateDetector::OnTouchUp(unsigned int index, const Pointer& pointer) |
| 36 |
{ |
| 37 |
if (index >= MAX_POINTERS) |
| 38 |
return false; |
| 39 |
|
| 40 |
if (m_done) |
| 41 |
return true; |
| 42 |
|
| 43 |
// after lifting the primary pointer, the secondary pointer will |
| 44 |
// become the primary pointer in the next event |
| 45 |
if (index == 0) |
| 46 |
{ |
| 47 |
m_pointers[0] = m_pointers[1]; |
| 48 |
index = 1; |
| 49 |
} |
| 50 |
|
| 51 |
m_pointers[index].reset(); |
| 52 |
|
| 53 |
if (!m_pointers[0].valid() && !m_pointers[1].valid()) |
| 54 |
m_done = true; |
| 55 |
|
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
bool CGenericTouchRotateDetector::OnTouchMove(unsigned int index, const Pointer& pointer) |
| 60 |
{ |
| 61 |
if (index >= MAX_POINTERS) |
| 62 |
return false; |
| 63 |
|
| 64 |
if (m_done) |
| 65 |
return true; |
| 66 |
|
| 67 |
// update the internal pointers |
| 68 |
m_pointers[index] = pointer; |
| 69 |
|
| 70 |
Pointer& primaryPointer = m_pointers[0]; |
| 71 |
Pointer& secondaryPointer = m_pointers[1]; |
| 72 |
|
| 73 |
if (!primaryPointer.valid() || !secondaryPointer.valid() || |
| 74 |
(!primaryPointer.moving && !secondaryPointer.moving)) |
| 75 |
return false; |
| 76 |
|
| 77 |
CVector last = primaryPointer.last - secondaryPointer.last; |
| 78 |
CVector current = primaryPointer.current - secondaryPointer.current; |
| 79 |
|
| 80 |
float length = last.length() * current.length(); |
| 81 |
if (length != 0.0f) |
| 82 |
{ |
| 83 |
float centerX = (primaryPointer.current.x + secondaryPointer.current.x) / 2; |
| 84 |
float centerY = (primaryPointer.current.y + secondaryPointer.current.y) / 2; |
| 85 |
float scalar = last.scalar(current); |
| 86 |
float angle = acos(scalar / length) * 180.0f / static_cast<float>(M_PI); |
| 87 |
|
| 88 |
// make sure the result of acos is a valid number |
| 89 |
if (angle == angle) |
| 90 |
{ |
| 91 |
// calculate the direction of the rotation using the |
| 92 |
// z-component of the cross-product of last and current |
| 93 |
float direction = last.x * current.y - current.x * last.y; |
| 94 |
if (direction < 0.0f) |
| 95 |
m_angle -= angle; |
| 96 |
else |
| 97 |
m_angle += angle; |
| 98 |
|
| 99 |
OnRotate(centerX, centerY, m_angle); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
bool CGenericTouchRotateDetector::OnTouchUpdate(unsigned int index, const Pointer& pointer) |
| 107 |
{ |
| 108 |
if (index >= MAX_POINTERS) |
| 109 |
return false; |
| 110 |
|
| 111 |
if (m_done) |
| 112 |
return true; |
| 113 |
|
| 114 |
// update the internal pointers |
| 115 |
m_pointers[index] = pointer; |
| 116 |
return true; |
| 117 |
} |