Some cleanups in Input.cs

This commit is contained in:
J.D. Purcell 2019-10-19 17:50:24 -04:00
parent bd0c81d791
commit 4ed50200c8
1 changed files with 40 additions and 41 deletions

View File

@ -3,8 +3,6 @@ using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using SlimDX.DirectInput;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Client.Common; using BizHawk.Client.Common;
@ -213,31 +211,25 @@ namespace BizHawk.Client.EmuHawk
private readonly WorkingDictionary<string, object> ModifierState = new WorkingDictionary<string, object>(); private readonly WorkingDictionary<string, object> ModifierState = new WorkingDictionary<string, object>();
private readonly WorkingDictionary<string, bool> LastState = new WorkingDictionary<string, bool>(); private readonly WorkingDictionary<string, bool> LastState = new WorkingDictionary<string, bool>();
private readonly WorkingDictionary<string, bool> UnpressState = new WorkingDictionary<string, bool>(); private readonly WorkingDictionary<string, bool> UnpressState = new WorkingDictionary<string, bool>();
private readonly HashSet<string> IgnoreKeys = new HashSet<string>(new[] { "LeftShift", "RightShift", "LeftControl", "RightControl", "LeftAlt", "RightAlt" });
private readonly WorkingDictionary<string, float> FloatValues = new WorkingDictionary<string, float>(); private readonly WorkingDictionary<string, float> FloatValues = new WorkingDictionary<string, float>();
private readonly WorkingDictionary<string, float> FloatDeltas = new WorkingDictionary<string, float>(); private readonly WorkingDictionary<string, float> FloatDeltas = new WorkingDictionary<string, float>();
private bool trackdeltas = false; private bool trackdeltas = false;
void HandleButton(string button, bool newState, InputFocus source) void HandleButton(string button, bool newState, InputFocus source)
{ {
bool isModifier = IgnoreKeys.Contains(button); ModifierKey currentModifier = ButtonToModifierKey(button);
if (EnableIgnoreModifiers && isModifier) return; if (EnableIgnoreModifiers && currentModifier != ModifierKey.None) return;
if (LastState[button] && newState) return; if (LastState[button] == newState) return;
if (!LastState[button] && !newState) return;
//apply //apply
//NOTE: this is not quite right. if someone held leftshift+rightshift it would be broken. seems unlikely, though. //NOTE: this is not quite right. if someone held leftshift+rightshift it would be broken. seems unlikely, though.
if (button == "LeftShift") if (currentModifier != ModifierKey.None)
{ {
_Modifiers &= ~ModifierKey.Shift;
if (newState) if (newState)
_Modifiers |= ModifierKey.Shift; _Modifiers |= currentModifier;
else
_Modifiers &= ~currentModifier;
} }
if (button == "RightShift") { _Modifiers &= ~ModifierKey.Shift; if (newState) _Modifiers |= ModifierKey.Shift; }
if (button == "LeftControl") { _Modifiers &= ~ModifierKey.Control; if (newState) _Modifiers |= ModifierKey.Control; }
if (button == "RightControl") { _Modifiers &= ~ModifierKey.Control; if (newState) _Modifiers |= ModifierKey.Control; }
if (button == "LeftAlt") { _Modifiers &= ~ModifierKey.Alt; if (newState) _Modifiers |= ModifierKey.Alt; }
if (button == "RightAlt") { _Modifiers &= ~ModifierKey.Alt; if (newState) _Modifiers |= ModifierKey.Alt; }
if (UnpressState.ContainsKey(button)) if (UnpressState.ContainsKey(button))
{ {
@ -248,15 +240,10 @@ namespace BizHawk.Client.EmuHawk
return; return;
} }
//dont generate events for things like Ctrl+LeftControl //dont generate events for things like Ctrl+LeftControl
ModifierKey mods = _Modifiers; ModifierKey mods = _Modifiers;
if (button == "LeftShift") mods &= ~ModifierKey.Shift; if (currentModifier != ModifierKey.None)
if (button == "RightShift") mods &= ~ModifierKey.Shift; mods &= ~currentModifier;
if (button == "LeftControl") mods &= ~ModifierKey.Control;
if (button == "RightControl") mods &= ~ModifierKey.Control;
if (button == "LeftAlt") mods &= ~ModifierKey.Alt;
if (button == "RightAlt") mods &= ~ModifierKey.Alt;
var ie = new InputEvent var ie = new InputEvent
{ {
@ -297,7 +284,21 @@ namespace BizHawk.Client.EmuHawk
_NewEvents.Add(ie); _NewEvents.Add(ie);
} }
ModifierKey _Modifiers; private static ModifierKey ButtonToModifierKey(string button)
{
switch (button)
{
case "LeftShift": return ModifierKey.Shift;
case "RightShift": return ModifierKey.Shift;
case "LeftControl": return ModifierKey.Control;
case "RightControl": return ModifierKey.Control;
case "LeftAlt": return ModifierKey.Alt;
case "RightAlt": return ModifierKey.Alt;
}
return ModifierKey.None;
}
private ModifierKey _Modifiers;
private readonly List<InputEvent> _NewEvents = new List<InputEvent>(); private readonly List<InputEvent> _NewEvents = new List<InputEvent>();
//do we need this? //do we need this?
@ -433,9 +434,10 @@ namespace BizHawk.Client.EmuHawk
//HandleButton("WMouse 1", false); //HandleButton("WMouse 1", false);
//HandleButton("WMouse 2", false); //HandleButton("WMouse 2", false);
} }
} }
if (_NewEvents.Count != 0)
{
//WHAT!? WE SHOULD NOT BE SO NAIVELY TOUCHING MAINFORM FROM THE INPUTTHREAD. ITS BUSY RUNNING. //WHAT!? WE SHOULD NOT BE SO NAIVELY TOUCHING MAINFORM FROM THE INPUTTHREAD. ITS BUSY RUNNING.
AllowInput allowInput = GlobalWin.MainForm.AllowInput(false); AllowInput allowInput = GlobalWin.MainForm.AllowInput(false);
@ -443,12 +445,13 @@ namespace BizHawk.Client.EmuHawk
{ {
//events are swallowed in some cases: //events are swallowed in some cases:
if (ie.LogicalButton.Alt && ShouldSwallow(GlobalWin.MainForm.AllowInput(true), ie)) if (ie.LogicalButton.Alt && ShouldSwallow(GlobalWin.MainForm.AllowInput(true), ie))
{ } continue;
else if (ie.EventType == InputEventType.Press && ShouldSwallow(allowInput, ie)) if (ie.EventType == InputEventType.Press && ShouldSwallow(allowInput, ie))
{ } continue;
else
EnqueueEvent(ie); EnqueueEvent(ie);
} }
}
} //lock(this) } //lock(this)
//arbitrary selection of polling frequency: //arbitrary selection of polling frequency:
@ -518,13 +521,10 @@ namespace BizHawk.Client.EmuHawk
if (ShouldSwallow(allowInput, ie)) continue; if (ShouldSwallow(allowInput, ie)) continue;
//as a special perk, we'll accept escape immediately //ignore presses, but as a special perk, we'll accept escape immediately
if (ie.EventType == InputEventType.Press && ie.LogicalButton.Button == "Escape") if (ie.EventType == InputEventType.Press && ie.LogicalButton.Button != "Escape")
goto ACCEPT; continue;
if (ie.EventType == InputEventType.Press) continue;
ACCEPT:
Console.WriteLine("Bind Event: {0} ", ie); Console.WriteLine("Bind Event: {0} ", ie);
foreach (var kvp in LastState) foreach (var kvp in LastState)
@ -556,6 +556,5 @@ namespace BizHawk.Client.EmuHawk
UnpressState[keystr] = true; UnpressState[keystr] = true;
LastState[keystr] = true; LastState[keystr] = true;
} }
} }
} }