Hotkeys - fix bug where bindings could be lost on save, also clean up the InputWidget code some

This commit is contained in:
adelikat 2013-08-04 20:21:58 +00:00
parent 7cfd6f32b0
commit 311cea39e4
3 changed files with 54 additions and 58 deletions

View File

@ -127,7 +127,7 @@ namespace BizHawk.MultiClient
string s; string s;
if (!RealConfigObject.TryGetValue(buttons[button], out s)) if (!RealConfigObject.TryGetValue(buttons[button], out s))
s = ""; s = "";
Inputs[button].SetBindings(s); Inputs[button].Bindings = s;
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.Windows.Forms; using System.Windows.Forms;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -11,18 +12,19 @@ namespace BizHawk.MultiClient
{ {
//TODO: when binding, make sure that the new key combo is not in one of the other bindings //TODO: when binding, make sure that the new key combo is not in one of the other bindings
int MaxBind = 4; //Max number of bindings allowed private int MaxBind = 4; //Max number of bindings allowed
int pos = 0; //Which mapping the widget will listen for private int pos = 0; //Which mapping the widget will listen for
private Timer timer = new Timer(); private Timer timer = new Timer();
public bool AutoTab = true; private string[] _bindings = new string[4];
string[] Bindings = new string[4]; private string wasPressed = "";
string wasPressed = ""; private ToolTip tooltip1 = new ToolTip();
ToolTip tooltip1 = new ToolTip(); private Color _highlight_color = Color.LightCyan;
public string WidgetName; private Color _no_highlight_color = SystemColors.Window;
Color _highlight_color = Color.LightCyan;
Color _no_highlight_color = SystemColors.Window;
private bool conflicted = false; private bool conflicted = false;
public bool AutoTab = true;
public string WidgetName;
public bool Conflicted public bool Conflicted
{ {
get get
@ -71,7 +73,7 @@ namespace BizHawk.MultiClient
{ {
this.ContextMenu = new ContextMenu(); this.ContextMenu = new ContextMenu();
this.timer.Tick += new System.EventHandler(this.Timer_Tick); this.timer.Tick += new System.EventHandler(this.Timer_Tick);
InitializeBindings(); _clearBindings();
tooltip1.AutoPopDelay = 2000; tooltip1.AutoPopDelay = 2000;
} }
@ -81,8 +83,8 @@ namespace BizHawk.MultiClient
this.ContextMenu = new ContextMenu(); this.ContextMenu = new ContextMenu();
this.timer.Tick += new System.EventHandler(this.Timer_Tick); this.timer.Tick += new System.EventHandler(this.Timer_Tick);
MaxBind = maxBindings; MaxBind = maxBindings;
Bindings = new string[MaxBind]; _bindings = new string[MaxBind];
InitializeBindings(); _clearBindings();
tooltip1.AutoPopDelay = 2000; tooltip1.AutoPopDelay = 2000;
} }
@ -92,11 +94,11 @@ namespace BizHawk.MultiClient
base.OnMouseClick(e); base.OnMouseClick(e);
} }
private void InitializeBindings() private void _clearBindings()
{ {
for (int x = 0; x < MaxBind; x++) for (int i = 0; i < MaxBind; i++)
{ {
Bindings[x] = ""; _bindings[i] = "";
} }
} }
@ -124,7 +126,7 @@ namespace BizHawk.MultiClient
public void EraseMappings() public void EraseMappings()
{ {
ClearBindings(); _clearBindings();
Conflicted = false; Conflicted = false;
Text = ""; Text = "";
} }
@ -141,7 +143,7 @@ namespace BizHawk.MultiClient
{ {
if (TempBindingStr == "Escape") if (TempBindingStr == "Escape")
{ {
ClearBindings(); _clearBindings();
Conflicted = false; Conflicted = false;
Increment(); Increment();
return; return;
@ -152,7 +154,7 @@ namespace BizHawk.MultiClient
if (!IsDuplicate(TempBindingStr)) if (!IsDuplicate(TempBindingStr))
{ {
Bindings[pos] = TempBindingStr; _bindings[pos] = TempBindingStr;
} }
wasPressed = TempBindingStr; wasPressed = TempBindingStr;
@ -164,13 +166,7 @@ namespace BizHawk.MultiClient
//Checks if the key is already mapped to this widget //Checks if the key is already mapped to this widget
private bool IsDuplicate(string binding) private bool IsDuplicate(string binding)
{ {
for (int x = 0; x < MaxBind; x++) return _bindings.FirstOrDefault(x => x == binding) != null;
{
if (Bindings[x] == binding)
return true;
}
return false;
} }
protected override void OnKeyUp(KeyEventArgs e) protected override void OnKeyUp(KeyEventArgs e)
@ -220,33 +216,31 @@ namespace BizHawk.MultiClient
} }
} }
public void ClearBindings()
{
for (int i = 0; i < MaxBind; i++)
{
Bindings[i] = "";
}
}
public void UpdateLabel() public void UpdateLabel()
{ {
Text = ""; Text = "";
for (int x = 0; x < MaxBind; x++) for (int x = 0; x < MaxBind; x++)
{ {
if (Bindings[x].Length > 0) if (_bindings[x].Length > 0)
{ {
Text += Bindings[x]; Text += _bindings[x];
if (x < MaxBind - 1 && Bindings[x+1].Length > 0) if (x < MaxBind - 1 && _bindings[x+1].Length > 0)
Text += ", "; Text += ", ";
} }
} }
} }
public void SetBindings(string bindingsString) public string Bindings
{
get
{
return Text;
}
set
{ {
Text = ""; Text = "";
ClearBindings(); _clearBindings();
string str = bindingsString.Trim(); string str = value.Trim();
int x; int x;
for (int i = 0; i < MaxBind; i++) for (int i = 0; i < MaxBind; i++)
{ {
@ -254,17 +248,19 @@ namespace BizHawk.MultiClient
x = str.IndexOf(','); x = str.IndexOf(',');
if (x < 0) if (x < 0)
{ {
Bindings[i] = str; _bindings[i] = str;
str = ""; str = "";
} }
else else
{ {
Bindings[i] = str.Substring(0, x); _bindings[i] = str.Substring(0, x);
str = str.Substring(x + 1, str.Length - x - 1); str = str.Substring(x + 1, str.Length - x - 1);
} }
} }
UpdateLabel(); UpdateLabel();
} }
}
protected override void OnKeyPress(KeyPressEventArgs e) protected override void OnKeyPress(KeyPressEventArgs e)
{ {

View File

@ -106,7 +106,7 @@ namespace BizHawk.MultiClient
InputWidget w = new InputWidget() InputWidget w = new InputWidget()
{ {
Text = b.Bindings, Bindings = b.Bindings,
Location = new Point(_x + iw_offset_x , _y + iw_offset_y), Location = new Point(_x + iw_offset_x , _y + iw_offset_y),
AutoTab = AutoTabCheckBox.Checked, AutoTab = AutoTabCheckBox.Checked,
Width = iw_width, Width = iw_width,