tidy multitap code up a lot
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -84,11 +84,10 @@ namespace OpenRA.Renderer.SdlCommon
|
||||
var button = MakeButton( e.button.button );
|
||||
lastButtonBits &= ~button;
|
||||
|
||||
MultiTapDetection.DetectFromMouse( e.button.button, new int2( e.button.x , e.button.y ) );
|
||||
|
||||
var pos = new int2( e.button.x, e.button.y );
|
||||
inputHandler.OnMouseInput( new MouseInput(
|
||||
MouseInputEvent.Up, button, new int2( e.button.x, e.button.y ), mods,
|
||||
MultiTapDetection.MouseButtonTapsCounted ) );
|
||||
MouseInputEvent.Up, button, pos, mods,
|
||||
MultiTapDetection.DetectFromMouse( e.button.button, pos )));
|
||||
} break;
|
||||
|
||||
case Sdl.SDL_MOUSEMOTION:
|
||||
@@ -122,18 +121,10 @@ namespace OpenRA.Renderer.SdlCommon
|
||||
Event = KeyInputEvent.Up,
|
||||
Modifiers = mods,
|
||||
UnicodeChar = (char)e.key.keysym.unicode,
|
||||
KeyName = Sdl.SDL_GetKeyName( e.key.keysym.sym ),
|
||||
KeyName = MultiTapDetection.DetectFromKeyboard(Sdl.SDL_GetKeyName( e.key.keysym.sym )),
|
||||
VirtKey = e.key.keysym.sym
|
||||
};
|
||||
|
||||
MultiTapDetection.DetectFromKeyboard( Sdl.SDL_GetKeyName( e.key.keysym.sym ) );
|
||||
|
||||
if ( MultiTapDetection.MultiTapDetected )
|
||||
{
|
||||
keyEvent.KeyName = MultiTapDetection.VirtualKeyNameOfDetectedMultiTap;
|
||||
// More info-changes here.
|
||||
}
|
||||
|
||||
inputHandler.OnKeyInput( keyEvent );
|
||||
} break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user