tidy multitap code up a lot

This commit is contained in:
Chris Forbes
2011-10-08 15:30:17 +13:00
parent 9b826d91a4
commit 8e3bcb892f
2 changed files with 34 additions and 80 deletions

View File

@@ -9,93 +9,56 @@
#endregion
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Tao.Sdl;
using OpenRA;
using OpenRA.FileFormats;
public static class MultiTapDetection
{
public static bool MultiTapDetected = false;
public static string VirtualKeyNameOfDetectedMultiTap = "";
public static int MouseButtonTapsCounted = 1;
static Cache<string, TapHistory> KeyHistoryCache =
new Cache<string, TapHistory>(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1)));
static Cache<byte, TapHistory> ClickHistoryCache =
new Cache<byte, TapHistory>(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1)));
static Cache<string, TapHistory> KeyHistoryCache = new Cache<string, 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 void DetectFromMouse(byte MBName, int2 xy)
public static int DetectFromMouse(byte MBName, int2 xy)
{
var clickHistory = ClickHistoryCache[MBName];
clickHistory.FirstRelease = clickHistory.SecondRelease;
clickHistory.SecondRelease = clickHistory.ThirdRelease;
clickHistory.ThirdRelease.First = DateTime.Now;
clickHistory.ThirdRelease.Second = xy;
TimeSpan DurationAfterSecondRelease = clickHistory.ThirdRelease.First - clickHistory.SecondRelease.First;
TimeSpan DurationAfterFirstRelease = clickHistory.SecondRelease.First - clickHistory.FirstRelease.First;
if ((DurationAfterSecondRelease.TotalMilliseconds < SystemInformation.DoubleClickTime)
&& ((clickHistory.ThirdRelease.Second - clickHistory.SecondRelease.Second).Length < 4)
&& ((clickHistory.ThirdRelease.Second - clickHistory.SecondRelease.Second).Length < 4))
{
MultiTapDetected = true;
MouseButtonTapsCounted = 2;
if ((DurationAfterFirstRelease.TotalMilliseconds < SystemInformation.DoubleClickTime)
&& ((clickHistory.SecondRelease.Second - clickHistory.FirstRelease.Second).Length < 4)
&& ((clickHistory.SecondRelease.Second - clickHistory.FirstRelease.Second).Length < 4))
{
MouseButtonTapsCounted = 3;
}
}
else
{
MultiTapDetected = false;
MouseButtonTapsCounted = 1;
}
return clickHistory.GetTapCount(xy);
}
public static void DetectFromKeyboard(string KeyName)
static readonly string[] KeyNameModifiers = new [] { "", "", "DoubleTapOf_", "TripleTapOf_" };
public static string DetectFromKeyboard(string KeyName)
{
var keyHistory = KeyHistoryCache[KeyName];
keyHistory.FirstRelease = keyHistory.SecondRelease;
keyHistory.SecondRelease = keyHistory.ThirdRelease;
keyHistory.ThirdRelease.First = DateTime.Now;
TimeSpan DurationAfterSecondRelease = keyHistory.ThirdRelease.First - keyHistory.SecondRelease.First;
TimeSpan DurationAfterFirstRelease = keyHistory.SecondRelease.First - keyHistory.FirstRelease.First;
if (DurationAfterSecondRelease.TotalMilliseconds < SystemInformation.DoubleClickTime)
{
MultiTapDetected = true;
VirtualKeyNameOfDetectedMultiTap = "DoubleTapOf_" + KeyName;
if (DurationAfterFirstRelease.TotalMilliseconds < SystemInformation.DoubleClickTime)
{
VirtualKeyNameOfDetectedMultiTap = "TripleTapOf_" + KeyName;
}
}
else
{
MultiTapDetected = false;
VirtualKeyNameOfDetectedMultiTap = "";
}
var count = keyHistory.GetTapCount(int2.Zero);
return KeyNameModifiers[count];
}
}
class TapHistory
{
public Pair<DateTime, int2> FirstRelease;
public Pair<DateTime, int2> SecondRelease;
public Pair<DateTime, int2> ThirdRelease;
public Pair<DateTime, int2> FirstRelease, SecondRelease, ThirdRelease;
public TapHistory(DateTime now)
{
this.FirstRelease.First = this.SecondRelease.First = this.ThirdRelease.First = now;
FirstRelease = SecondRelease = ThirdRelease = Pair.New( now, int2.Zero );
}
static bool CloseEnough(Pair<DateTime, int2> a, Pair<DateTime, int2> b)
{
return a.First - b.First < TimeSpan.FromMilliseconds( SystemInformation.DoubleClickTime )
&& (a.Second - b.Second).Length < 4;
}
public int GetTapCount(int2 xy)
{
FirstRelease = SecondRelease;
SecondRelease = ThirdRelease;
ThirdRelease = Pair.New(DateTime.Now, xy);
if (!CloseEnough(ThirdRelease, SecondRelease)) return 1;
if (!CloseEnough(SecondRelease, FirstRelease)) return 2;
return 3;
}
}