tidy multitap code up a lot
This commit is contained in:
@@ -9,93 +9,56 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Tao.Sdl;
|
|
||||||
using OpenRA;
|
using OpenRA;
|
||||||
using OpenRA.FileFormats;
|
using OpenRA.FileFormats;
|
||||||
|
|
||||||
public static class MultiTapDetection
|
public static class MultiTapDetection
|
||||||
{
|
{
|
||||||
public static bool MultiTapDetected = false;
|
static Cache<string, TapHistory> KeyHistoryCache =
|
||||||
public static string VirtualKeyNameOfDetectedMultiTap = "";
|
new Cache<string, TapHistory>(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1)));
|
||||||
public static int MouseButtonTapsCounted = 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)));
|
public static int DetectFromMouse(byte MBName, int2 xy)
|
||||||
static Cache<byte, TapHistory> ClickHistoryCache = new Cache<byte, TapHistory>(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1)));
|
|
||||||
|
|
||||||
|
|
||||||
public static void DetectFromMouse(byte MBName, int2 xy)
|
|
||||||
{
|
{
|
||||||
var clickHistory = ClickHistoryCache[MBName];
|
var clickHistory = ClickHistoryCache[MBName];
|
||||||
|
return clickHistory.GetTapCount(xy);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void DetectFromKeyboard(string KeyName)
|
static readonly string[] KeyNameModifiers = new [] { "", "", "DoubleTapOf_", "TripleTapOf_" };
|
||||||
|
|
||||||
|
public static string DetectFromKeyboard(string KeyName)
|
||||||
{
|
{
|
||||||
var keyHistory = KeyHistoryCache[KeyName];
|
var keyHistory = KeyHistoryCache[KeyName];
|
||||||
|
var count = keyHistory.GetTapCount(int2.Zero);
|
||||||
keyHistory.FirstRelease = keyHistory.SecondRelease;
|
return KeyNameModifiers[count];
|
||||||
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 = "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TapHistory
|
class TapHistory
|
||||||
{
|
{
|
||||||
public Pair<DateTime, int2> FirstRelease;
|
public Pair<DateTime, int2> FirstRelease, SecondRelease, ThirdRelease;
|
||||||
public Pair<DateTime, int2> SecondRelease;
|
|
||||||
public Pair<DateTime, int2> ThirdRelease;
|
|
||||||
|
|
||||||
public TapHistory(DateTime now)
|
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 );
|
var button = MakeButton( e.button.button );
|
||||||
lastButtonBits &= ~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(
|
inputHandler.OnMouseInput( new MouseInput(
|
||||||
MouseInputEvent.Up, button, new int2( e.button.x, e.button.y ), mods,
|
MouseInputEvent.Up, button, pos, mods,
|
||||||
MultiTapDetection.MouseButtonTapsCounted ) );
|
MultiTapDetection.DetectFromMouse( e.button.button, pos )));
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case Sdl.SDL_MOUSEMOTION:
|
case Sdl.SDL_MOUSEMOTION:
|
||||||
@@ -122,18 +121,10 @@ namespace OpenRA.Renderer.SdlCommon
|
|||||||
Event = KeyInputEvent.Up,
|
Event = KeyInputEvent.Up,
|
||||||
Modifiers = mods,
|
Modifiers = mods,
|
||||||
UnicodeChar = (char)e.key.keysym.unicode,
|
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
|
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 );
|
inputHandler.OnKeyInput( keyEvent );
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user