diff --git a/BizHawk.Client.EmuHawk/config/InputWidget.cs b/BizHawk.Client.EmuHawk/config/InputWidget.cs index f6192af2c2d..b0fcb954643 100644 --- a/BizHawk.Client.EmuHawk/config/InputWidget.cs +++ b/BizHawk.Client.EmuHawk/config/InputWidget.cs @@ -1,23 +1,21 @@ using System; using System.Drawing; using System.Linq; -using System.Collections.Generic; using System.Windows.Forms; using System.Runtime.InteropServices; -using System.Text; namespace BizHawk.Client.EmuHawk { - public class InputWidget : TextBox + public sealed class InputWidget : TextBox { //TODO: when binding, make sure that the new key combo is not in one of the other bindings - private int MaxBind = 4; //Max number of bindings allowed - private int pos = 0; //Which mapping the widget will listen for - private Timer timer = new Timer(); - private string[] _bindings = new string[4]; - private string wasPressed = String.Empty; - private ToolTip tooltip1 = new ToolTip(); + private readonly int _maxBind = 4; //Max number of bindings allowed + private int _pos; //Which mapping the widget will listen for + private readonly Timer _timer = new Timer(); + private readonly string[] _bindings = new string[4]; + private string _wasPressed = String.Empty; + private readonly ToolTip _tooltip1 = new ToolTip(); public bool AutoTab = true; public string WidgetName; @@ -27,32 +25,32 @@ public class InputWidget : TextBox public InputWidget() { - this.ContextMenu = new ContextMenu(); - this.timer.Tick += new System.EventHandler(this.Timer_Tick); + ContextMenu = new ContextMenu(); + _timer.Tick += Timer_Tick; ClearBindings(); - tooltip1.AutoPopDelay = 2000; + _tooltip1.AutoPopDelay = 2000; } public InputWidget(int maxBindings, bool autotab) { - this.AutoTab = autotab; - this.ContextMenu = new ContextMenu(); - this.timer.Tick += new System.EventHandler(this.Timer_Tick); - MaxBind = maxBindings; - _bindings = new string[MaxBind]; + AutoTab = autotab; + ContextMenu = new ContextMenu(); + _timer.Tick += Timer_Tick; + _maxBind = maxBindings; + _bindings = new string[_maxBind]; ClearBindings(); - tooltip1.AutoPopDelay = 2000; + _tooltip1.AutoPopDelay = 2000; } protected override void OnMouseClick(MouseEventArgs e) { - HideCaret(this.Handle); + HideCaret(Handle); base.OnMouseClick(e); } private void ClearBindings() { - for (int i = 0; i < MaxBind; i++) + for (int i = 0; i < _maxBind; i++) { _bindings[i] = String.Empty; } @@ -60,18 +58,18 @@ private void ClearBindings() protected override void OnEnter(EventArgs e) { - pos = 0; - timer.Start(); + _pos = 0; + _timer.Start(); - wasPressed = Input.Instance.GetNextBindEvent(); + _wasPressed = Input.Instance.GetNextBindEvent(); BackColor = Color.LightCyan; } protected override void OnLeave(EventArgs e) { - timer.Stop(); + _timer.Stop(); UpdateLabel(); - BackColor = SystemColors.Control; + BackColor = SystemColors.Window; base.OnLeave(e); } @@ -90,8 +88,8 @@ public void EraseMappings() private void ReadKeys() { Input.Instance.Update(); - string TempBindingStr = Input.Instance.GetNextBindEvent(); - if (!String.IsNullOrEmpty(wasPressed) && TempBindingStr == wasPressed) + var TempBindingStr = Input.Instance.GetNextBindEvent(); + if (!String.IsNullOrEmpty(_wasPressed) && TempBindingStr == _wasPressed) { return; } @@ -110,9 +108,9 @@ private void ReadKeys() if (!IsDuplicate(TempBindingStr)) { - _bindings[pos] = TempBindingStr; + _bindings[_pos] = TempBindingStr; } - wasPressed = TempBindingStr; + _wasPressed = TempBindingStr; UpdateLabel(); Increment(); @@ -131,7 +129,7 @@ protected override void OnKeyUp(KeyEventArgs e) base.OnKeyUp(e); } - wasPressed = String.Empty; + _wasPressed = String.Empty; } protected override void OnKeyDown(KeyEventArgs e) @@ -150,17 +148,17 @@ public void Increment() { if (AutoTab) { - this.Parent.SelectNextControl(this, true, true, true, true); + Parent.SelectNextControl(this, true, true, true, true); } else { - if (pos < MaxBind) + if (_pos < _maxBind) { - pos++; + _pos++; } else { - pos = 0; + _pos = 0; } } } @@ -169,17 +167,17 @@ public void Decrement() { if (AutoTab) { - this.Parent.SelectNextControl(this, false, true, true, true); + Parent.SelectNextControl(this, false, true, true, true); } else { - if (pos == 0) + if (_pos == 0) { - pos = MaxBind - 1; + _pos = _maxBind - 1; } else { - pos--; + _pos--; } } } @@ -197,26 +195,15 @@ public string Bindings } set { - Text = String.Empty; ClearBindings(); - string str = value.Trim(); - int x; - for (int i = 0; i < MaxBind; i++) + var newBindings = value.Trim().Split(','); + for (int i = 0; i < _maxBind; i++) { - str = str.Trim(); - x = str.IndexOf(','); - if (x < 0) + if (i < newBindings.Length) { - _bindings[i] = str; - str = String.Empty; - } - else - { - _bindings[i] = str.Substring(0, x); - str = str.Substring(x + 1, str.Length - x - 1); + _bindings[i] = newBindings[i]; } } - UpdateLabel(); } } @@ -232,13 +219,9 @@ protected override void WndProc(ref Message m) { case 0x0201: //WM_LBUTTONDOWN { - this.Focus(); + Focus(); return; } - //case 0x0202://WM_LBUTTONUP - //{ - // return; - //} case 0x0203://WM_LBUTTONDBLCLK { return; @@ -275,20 +258,12 @@ protected override void OnMouseWheel(MouseEventArgs e) protected override void OnGotFocus(EventArgs e) { - HideCaret(this.Handle); - } - - protected override void OnLostFocus(EventArgs e) - { - base.OnLostFocus(e); + HideCaret(Handle); } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { - if (keyData.ToString() == "F4" || keyData.ToString().Contains("Alt")) - return false; - else - return true; + return !(keyData.ToString() == "F4" || keyData.ToString().Contains("Alt")); } } } \ No newline at end of file