| 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 "GenericTouchPinchDetector.h" |
| 10 |
|
| 11 |
bool CGenericTouchPinchDetector::OnTouchDown(unsigned int index, const Pointer& pointer) |
| 12 |
{ |
| 13 |
if (index >= MAX_POINTERS) |
| 14 |
return false; |
| 15 |
|
| 16 |
if (m_done) |
| 17 |
return true; |
| 18 |
|
| 19 |
m_pointers[index] = pointer; |
| 20 |
return true; |
| 21 |
} |
| 22 |
|
| 23 |
bool CGenericTouchPinchDetector::OnTouchUp(unsigned int index, const Pointer& pointer) |
| 24 |
{ |
| 25 |
if (index >= MAX_POINTERS) |
| 26 |
return false; |
| 27 |
|
| 28 |
if (m_done) |
| 29 |
return true; |
| 30 |
|
| 31 |
// after lifting the primary pointer, the secondary pointer will |
| 32 |
// become the primary pointer in the next event |
| 33 |
if (index == 0) |
| 34 |
{ |
| 35 |
m_pointers[0] = m_pointers[1]; |
| 36 |
index = 1; |
| 37 |
} |
| 38 |
|
| 39 |
m_pointers[index].reset(); |
| 40 |
|
| 41 |
if (!m_pointers[0].valid() && !m_pointers[1].valid()) |
| 42 |
m_done = true; |
| 43 |
|
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
bool CGenericTouchPinchDetector::OnTouchMove(unsigned int index, const Pointer& pointer) |
| 48 |
{ |
| 49 |
if (index >= MAX_POINTERS) |
| 50 |
return false; |
| 51 |
|
| 52 |
if (m_done) |
| 53 |
return true; |
| 54 |
|
| 55 |
// update the internal pointers |
| 56 |
m_pointers[index] = pointer; |
| 57 |
|
| 58 |
const Pointer& primaryPointer = m_pointers[0]; |
| 59 |
const Pointer& secondaryPointer = m_pointers[1]; |
| 60 |
|
| 61 |
if (!primaryPointer.valid() || !secondaryPointer.valid() || |
| 62 |
(!primaryPointer.moving && !secondaryPointer.moving)) |
| 63 |
return false; |
| 64 |
|
| 65 |
// calculate zoom/pinch |
| 66 |
CVector primary = primaryPointer.down; |
| 67 |
CVector secondary = secondaryPointer.down; |
| 68 |
CVector diagonal = primary - secondary; |
| 69 |
|
| 70 |
float baseDiffLength = diagonal.length(); |
| 71 |
if (baseDiffLength != 0.0f) |
| 72 |
{ |
| 73 |
CVector primaryNow = primaryPointer.current; |
| 74 |
CVector secondaryNow = secondaryPointer.current; |
| 75 |
CVector diagonalNow = primaryNow - secondaryNow; |
| 76 |
float curDiffLength = diagonalNow.length(); |
| 77 |
|
| 78 |
float centerX = (primary.x + secondary.x) / 2; |
| 79 |
float centerY = (primary.y + secondary.y) / 2; |
| 80 |
|
| 81 |
float zoom = curDiffLength / baseDiffLength; |
| 82 |
|
| 83 |
OnZoomPinch(centerX, centerY, zoom); |
| 84 |
} |
| 85 |
|
| 86 |
return true; |
| 87 |
} |