forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiTapDetection.cs
84 lines (70 loc) · 2.2 KB
/
MultiTapDetection.cs
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
#region Copyright & License Information
/*
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using OpenRA.Primitives;
namespace OpenRA.Platforms.Default
{
static class MultiTapDetection
{
static Cache<(Keycode Key, Modifiers Mods), TapHistory> keyHistoryCache =
new Cache<(Keycode, Modifiers), TapHistory>(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1)));
static Cache<byte, TapHistory> clickHistoryCache =
new Cache<byte, TapHistory>(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1)));
public static int DetectFromMouse(byte button, int2 xy)
{
return clickHistoryCache[button].GetTapCount(xy);
}
public static int InfoFromMouse(byte button)
{
return clickHistoryCache[button].LastTapCount();
}
public static int DetectFromKeyboard(Keycode key, Modifiers mods)
{
return keyHistoryCache[(key, mods)].GetTapCount(int2.Zero);
}
public static int InfoFromKeyboard(Keycode key, Modifiers mods)
{
return keyHistoryCache[(key, mods)].LastTapCount();
}
}
class TapHistory
{
public (DateTime Time, int2 Location) FirstRelease, SecondRelease, ThirdRelease;
public TapHistory(DateTime now)
{
FirstRelease = SecondRelease = ThirdRelease = (now, int2.Zero);
}
static bool CloseEnough((DateTime Time, int2 Location) a, (DateTime Time, int2 Location) b)
{
return a.Time - b.Time < TimeSpan.FromMilliseconds(250)
&& (a.Location - b.Location).Length < 4;
}
public int GetTapCount(int2 xy)
{
FirstRelease = SecondRelease;
SecondRelease = ThirdRelease;
ThirdRelease = (DateTime.Now, xy);
if (!CloseEnough(ThirdRelease, SecondRelease))
return 1;
if (!CloseEnough(SecondRelease, FirstRelease))
return 2;
return 3;
}
public int LastTapCount()
{
if (!CloseEnough(ThirdRelease, SecondRelease))
return 1;
if (!CloseEnough(SecondRelease, FirstRelease))
return 2;
return 3;
}
}
}