-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathSKGestureSurfaceView.FlingTracker.shared.cs
More file actions
85 lines (70 loc) · 1.95 KB
/
SKGestureSurfaceView.FlingTracker.shared.cs
File metadata and controls
85 lines (70 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
namespace SkiaSharp.Extended.UI.Controls;
public partial class SKGestureSurfaceView
{
/// <summary>
/// Tracks touch events to calculate fling velocity.
/// </summary>
private sealed class FlingTracker
{
// Use only events from the last 200 ms
private const long ThresholdTicks = 200 * TimeSpan.TicksPerMillisecond;
private const int MaxSize = 2;
private readonly Dictionary<long, Queue<FlingTrackerEvent>> events = new();
public void AddEvent(long id, SKPoint location, long ticks)
{
if (!events.TryGetValue(id, out var queue))
{
queue = new Queue<FlingTrackerEvent>();
events[id] = queue;
}
queue.Enqueue(new FlingTrackerEvent(location.X, location.Y, ticks));
if (queue.Count > MaxSize)
queue.Dequeue();
}
public void RemoveId(long id)
{
events.Remove(id);
}
public void Clear()
{
events.Clear();
}
public SKPoint CalculateVelocity(long id, long now)
{
float velocityX = 0;
float velocityY = 0;
if (!events.TryGetValue(id, out var queue) || queue.Count != 2)
return SKPoint.Empty;
var array = queue.ToArray();
var lastItem = array[0];
var nowItem = array[1];
// Use last 2 events to calculate velocity
if (now - lastItem.TimeTicks < ThresholdTicks)
{
var timeDelta = nowItem.TimeTicks - lastItem.TimeTicks;
if (timeDelta > 0)
{
velocityX = (nowItem.X - lastItem.X) * TimeSpan.TicksPerSecond / timeDelta;
velocityY = (nowItem.Y - lastItem.Y) * TimeSpan.TicksPerSecond / timeDelta;
}
}
// Return the velocity in pixels per second
return new SKPoint(velocityX, velocityY);
}
/// <summary>
/// Represents a single event used for fling velocity calculation.
/// </summary>
private readonly struct FlingTrackerEvent
{
public FlingTrackerEvent(float x, float y, long timeTicks)
{
X = x;
Y = y;
TimeTicks = timeTicks;
}
public float X { get; }
public float Y { get; }
public long TimeTicks { get; }
}
}
}