kodi
TouchTypes.h
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 
18 class Touch : public CVector
19 {
20 public:
27  bool valid() const { return x >= 0.0f && y >= 0.0f && time >= 0; }
28 
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 
49 class Pointer
50 {
51 public:
52  Pointer() { reset(); }
53 
57  void reset()
58  {
59  down = {};
60  last = {};
61  moving = false;
62  size = 0.0f;
63  }
64 
70  bool valid() const { return down.valid(); }
71 
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 };
bool valid() const
Checks if the "down" touch is valid.
Definition: TouchTypes.h:70
void copy(const Touch &other)
Copies the x/y coordinates and the time from the given touch.
Definition: TouchTypes.h:34
Definition: Vector.h:11
void reset()
Resets the pointer and all its touches.
Definition: TouchTypes.h:57
A class representing a touch pointer interaction consisting of an down touch, the last touch and the ...
Definition: TouchTypes.h:49
bool valid() const
Checks if the touch is valid i.e. if the x/y coordinates and the time are >= 0.
Definition: TouchTypes.h:27
bool velocity(float &velocityX, float &velocityY, bool fromLast=true) const
Calculates the velocity in x/y direction using the "down" and either the "last" or "current" touch...
Definition: TouchTypes.h:83
A class representing a touch consisting of an x and y coordinate and a time.
Definition: TouchTypes.h:18