2011-01-30 23:06:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace BizHawk.MultiClient
|
|
|
|
|
{
|
2011-06-19 02:17:27 +00:00
|
|
|
|
public class InputWidget : TextBox
|
|
|
|
|
{
|
2011-07-09 17:50:52 +00:00
|
|
|
|
//TODO: when binding, make sure that the new key combo is not in one of the other bindings
|
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
int MaxBind = 4; //Max number of bindings allowed
|
2011-07-09 17:50:52 +00:00
|
|
|
|
int pos = 0; //Which mapping the widget will listen for
|
2011-07-09 16:12:56 +00:00
|
|
|
|
private Timer timer = new Timer();
|
|
|
|
|
public bool AutoTab = true;
|
|
|
|
|
string[] Bindings = new string[4];
|
|
|
|
|
string wasPressed = "";
|
|
|
|
|
|
|
|
|
|
public InputWidget()
|
2011-06-19 02:17:27 +00:00
|
|
|
|
{
|
|
|
|
|
this.ContextMenu = new ContextMenu();
|
2011-07-09 16:12:56 +00:00
|
|
|
|
this.timer.Tick += new System.EventHandler(this.Timer_Tick);
|
|
|
|
|
InitializeBindings();
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
2011-01-30 23:06:43 +00:00
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
public InputWidget(int maxBindings)
|
|
|
|
|
{
|
|
|
|
|
this.ContextMenu = new ContextMenu();
|
|
|
|
|
this.timer.Tick += new System.EventHandler(this.Timer_Tick);
|
|
|
|
|
MaxBind = maxBindings;
|
|
|
|
|
Bindings = new string[MaxBind];
|
|
|
|
|
InitializeBindings();
|
|
|
|
|
}
|
2011-01-30 23:06:43 +00:00
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
private void InitializeBindings()
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < MaxBind; x++)
|
|
|
|
|
{
|
|
|
|
|
Bindings[x] = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-07-07 16:48:29 +00:00
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
protected override void OnEnter(EventArgs e)
|
|
|
|
|
{
|
2011-07-09 17:50:52 +00:00
|
|
|
|
pos = 0;
|
2011-07-09 16:12:56 +00:00
|
|
|
|
timer.Start();
|
|
|
|
|
base.OnEnter(e);
|
|
|
|
|
Input.Update();
|
|
|
|
|
wasPressed = Input.GetPressedKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnLeave(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
timer.Stop();
|
|
|
|
|
UpdateLabel();
|
|
|
|
|
base.OnLeave(e);
|
|
|
|
|
}
|
2011-07-07 16:48:29 +00:00
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
private void Timer_Tick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ReadKeys();
|
|
|
|
|
}
|
2011-07-07 16:48:29 +00:00
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
private void ReadKeys()
|
2011-06-19 02:17:27 +00:00
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
Input.Update();
|
|
|
|
|
string TempBindingStr = Input.GetPressedKey();
|
|
|
|
|
if (wasPressed != "" && TempBindingStr == wasPressed) return;
|
|
|
|
|
if (TempBindingStr != null)
|
2011-06-19 02:17:27 +00:00
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
if (TempBindingStr == "Escape")
|
|
|
|
|
{
|
|
|
|
|
ClearBindings();
|
|
|
|
|
Increment();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TempBindingStr == "Alt+F4")
|
|
|
|
|
return;
|
|
|
|
|
|
2011-07-09 18:24:53 +00:00
|
|
|
|
if (!IsDuplicate(TempBindingStr))
|
|
|
|
|
{
|
|
|
|
|
Bindings[pos] = TempBindingStr;
|
|
|
|
|
wasPressed = TempBindingStr;
|
|
|
|
|
UpdateLabel();
|
|
|
|
|
Increment();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsDuplicate(string binding)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < MaxBind; x++)
|
|
|
|
|
{
|
|
|
|
|
if (Bindings[x] == binding)
|
|
|
|
|
return true;
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
2011-07-09 18:24:53 +00:00
|
|
|
|
|
|
|
|
|
return false;
|
2011-07-09 16:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnKeyUp(KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
|
2011-06-19 02:17:27 +00:00
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
base.OnKeyUp(e);
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
2011-07-09 16:12:56 +00:00
|
|
|
|
wasPressed = "";
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
2011-01-30 23:06:43 +00:00
|
|
|
|
|
2011-06-19 02:17:27 +00:00
|
|
|
|
protected override void OnKeyDown(KeyEventArgs e)
|
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
|
2011-06-19 02:17:27 +00:00
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
base.OnKeyDown(e);
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
2011-07-09 16:12:56 +00:00
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
2011-07-05 01:48:48 +00:00
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
// Advances to the next widget or the next binding depending on the autotab setting
|
|
|
|
|
private void Increment()
|
|
|
|
|
{
|
2011-07-04 23:02:37 +00:00
|
|
|
|
if (AutoTab)
|
|
|
|
|
this.Parent.SelectNextControl(this, true, true, true, true);
|
2011-07-09 17:50:52 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (pos == MaxBind - 1)
|
|
|
|
|
pos = 0;
|
|
|
|
|
else
|
|
|
|
|
pos++;
|
|
|
|
|
}
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
2011-01-30 23:06:43 +00:00
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
public void ClearBindings()
|
2011-06-19 03:35:57 +00:00
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
for (int x = 0; x < MaxBind; x++)
|
|
|
|
|
Bindings[x] = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateLabel()
|
|
|
|
|
{
|
|
|
|
|
Text = "";
|
|
|
|
|
for (int x = 0; x < MaxBind; x++)
|
2011-06-19 03:35:57 +00:00
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
if (Bindings[x].Length > 0)
|
2011-06-19 03:35:57 +00:00
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
Text += Bindings[x];
|
|
|
|
|
if (x < MaxBind - 1 && Bindings[x+1].Length > 0)
|
|
|
|
|
Text += ", ";
|
2011-06-19 03:35:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-07-09 16:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetBindings(string bindingsString)
|
|
|
|
|
{
|
|
|
|
|
Text = "";
|
|
|
|
|
ClearBindings();
|
|
|
|
|
string str = bindingsString.Trim();
|
|
|
|
|
int x;
|
|
|
|
|
for (int i = 0; i < MaxBind; i++)
|
2011-06-19 03:35:57 +00:00
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
str = str.Trim();
|
|
|
|
|
x = str.IndexOf(',');
|
|
|
|
|
if (x < 0)
|
2011-06-19 03:35:57 +00:00
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
Bindings[i] = str;
|
|
|
|
|
str = "";
|
2011-06-19 03:35:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
Bindings[i] = str.Substring(0, x);
|
|
|
|
|
str = str.Substring(x + 1, str.Length - x - 1);
|
2011-06-19 03:35:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-07-09 16:12:56 +00:00
|
|
|
|
UpdateLabel();
|
2011-06-19 03:35:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-19 02:17:27 +00:00
|
|
|
|
protected override void OnKeyPress(KeyPressEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
2011-01-30 23:06:43 +00:00
|
|
|
|
|
2011-06-19 02:17:27 +00:00
|
|
|
|
protected override void OnGotFocus(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnGotFocus(e);
|
|
|
|
|
BackColor = Color.Pink;
|
|
|
|
|
}
|
2011-01-30 23:06:43 +00:00
|
|
|
|
|
2011-06-19 02:17:27 +00:00
|
|
|
|
protected override void OnLostFocus(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnLostFocus(e);
|
|
|
|
|
BackColor = SystemColors.Window;
|
|
|
|
|
}
|
2011-02-23 21:30:11 +00:00
|
|
|
|
|
2011-06-19 02:17:27 +00:00
|
|
|
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
|
|
|
|
{
|
2011-07-09 21:10:40 +00:00
|
|
|
|
return true;
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-30 23:06:43 +00:00
|
|
|
|
}
|