| 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 |
#pragma once |
| 10 |
|
| 11 |
#include "utils/Vector.h" |
| 12 |
|
| 13 |
/*! |
| 14 |
* \ingroup touch |
| 15 |
* \brief A class representing a touch consisting of an x and y coordinate and |
| 16 |
* a time |
| 17 |
*/ |
| 18 |
class Touch : public CVector |
| 19 |
{ |
| 20 |
public: |
| 21 |
/*! |
| 22 |
* \brief Checks if the touch is valid i.e. if the x/y coordinates and the |
| 23 |
* time are >= 0 |
| 24 |
* |
| 25 |
* \return True if the touch is valid otherwise false |
| 26 |
*/ |
| 27 |
bool valid() const { return x >= 0.0f && y >= 0.0f && time >= 0; } |
| 28 |
|
| 29 |
/*! |
| 30 |
* \brief Copies the x/y coordinates and the time from the given touch |
| 31 |
* |
| 32 |
* \param other Touch to copy x/y coordinates and time from |
| 33 |
*/ |
| 34 |
void copy(const Touch& other) |
| 35 |
{ |
| 36 |
x = other.x; |
| 37 |
y = other.y; |
| 38 |
time = other.time; |
| 39 |
} |
| 40 |
|
| 41 |
int64_t time = -1; // in nanoseconds |
| 42 |
}; |
| 43 |
|
| 44 |
/*! |
| 45 |
* \ingroup touch |
| 46 |
* \brief A class representing a touch pointer interaction consisting of an |
| 47 |
* down touch, the last touch and the current touch. |
| 48 |
*/ |
| 49 |
class Pointer |
| 50 |
{ |
| 51 |
public: |
| 52 |
Pointer() { reset(); } |
| 53 |
|
| 54 |
/*! |
| 55 |
* \brief Resets the pointer and all its touches |
| 56 |
*/ |
| 57 |
void reset() |
| 58 |
{ |
| 59 |
down = {}; |
| 60 |
last = {}; |
| 61 |
moving = false; |
| 62 |
size = 0.0f; |
| 63 |
} |
| 64 |
|
| 65 |
/*! |
| 66 |
* \brief Checks if the "down" touch is valid |
| 67 |
* |
| 68 |
* \return True if the "down" touch is valid otherwise false |
| 69 |
*/ |
| 70 |
bool valid() const { return down.valid(); } |
| 71 |
|
| 72 |
/*! |
| 73 |
* \brief Calculates the velocity in x/y direction using the "down" and |
| 74 |
* either the "last" or "current" touch |
| 75 |
* |
| 76 |
* \param velocityX Placeholder for velocity in x direction |
| 77 |
* \param velocityY Placeholder for velocity in y direction |
| 78 |
* \param fromLast Whether to calculate the velocity with the "last" or |
| 79 |
* the "current" touch |
| 80 |
* |
| 81 |
* \return True if the velocity is valid otherwise false |
| 82 |
*/ |
| 83 |
bool velocity(float& velocityX, float& velocityY, bool fromLast = true) const |
| 84 |
{ |
| 85 |
int64_t fromTime = last.time; |
| 86 |
float fromX = last.x; |
| 87 |
float fromY = last.y; |
| 88 |
if (!fromLast) |
| 89 |
{ |
| 90 |
fromTime = down.time; |
| 91 |
fromX = down.x; |
| 92 |
fromY = down.y; |
| 93 |
} |
| 94 |
|
| 95 |
velocityX = 0.0f; // number of pixels per second |
| 96 |
velocityY = 0.0f; // number of pixels per second |
| 97 |
|
| 98 |
int64_t timeDiff = current.time - fromTime; |
| 99 |
if (timeDiff <= 0) |
| 100 |
return false; |
| 101 |
|
| 102 |
velocityX = ((current.x - fromX) * 1000000000) / timeDiff; |
| 103 |
velocityY = ((current.y - fromY) * 1000000000) / timeDiff; |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
Touch down; |
| 108 |
Touch last; |
| 109 |
Touch current; |
| 110 |
bool moving; |
| 111 |
float size; |
| 112 |
}; |