2011-01-30 23:06:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
2013-08-04 20:21:58 +00:00
|
|
|
|
using System.Linq;
|
2011-07-09 21:55:14 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2014-02-08 16:15:07 +00:00
|
|
|
|
using System.Windows.Forms;
|
2011-01-30 23:06:43 +00:00
|
|
|
|
|
2013-11-03 03:54:37 +00:00
|
|
|
|
namespace BizHawk.Client.EmuHawk
|
2011-01-30 23:06:43 +00:00
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
public sealed class InputWidget : TextBox
|
2011-06-19 02:17:27 +00:00
|
|
|
|
{
|
2014-02-08 16:15:07 +00:00
|
|
|
|
// TODO: when binding, make sure that the new key combo is not in one of the other bindings
|
|
|
|
|
private readonly ToolTip _tooltip1 = new ToolTip();
|
2013-11-27 23:52:21 +00:00
|
|
|
|
private readonly Timer _timer = new Timer();
|
|
|
|
|
private readonly string[] _bindings = new string[4];
|
2014-02-08 16:15:07 +00:00
|
|
|
|
private readonly int _maxBind = 4; // Max number of bindings allowed
|
|
|
|
|
|
|
|
|
|
private int _pos; // Which mapping the widget will listen for
|
|
|
|
|
private string _wasPressed = string.Empty;
|
2011-07-09 21:55:14 +00:00
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
public InputWidget()
|
2011-06-19 02:17:27 +00:00
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
ContextMenu = new ContextMenu();
|
|
|
|
|
_timer.Tick += Timer_Tick;
|
2013-10-10 01:53:18 +00:00
|
|
|
|
ClearBindings();
|
2013-11-27 23:52:21 +00:00
|
|
|
|
_tooltip1.AutoPopDelay = 2000;
|
2014-02-08 16:15:07 +00:00
|
|
|
|
AutoTab = true;
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
2011-01-30 23:06:43 +00:00
|
|
|
|
|
2013-07-28 21:54:42 +00:00
|
|
|
|
public InputWidget(int maxBindings, bool autotab)
|
2011-07-09 23:03:42 +00:00
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
AutoTab = autotab;
|
|
|
|
|
ContextMenu = new ContextMenu();
|
|
|
|
|
_timer.Tick += Timer_Tick;
|
|
|
|
|
_maxBind = maxBindings;
|
|
|
|
|
_bindings = new string[_maxBind];
|
2013-10-10 01:53:18 +00:00
|
|
|
|
ClearBindings();
|
2013-11-27 23:52:21 +00:00
|
|
|
|
_tooltip1.AutoPopDelay = 2000;
|
2011-07-09 23:03:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-08 16:15:07 +00:00
|
|
|
|
public bool AutoTab { get; set; }
|
|
|
|
|
public string WidgetName { get; set; }
|
|
|
|
|
|
|
|
|
|
public string Bindings
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
ClearBindings();
|
|
|
|
|
var newBindings = value.Trim().Split(',');
|
|
|
|
|
for (var i = 0; i < _maxBind; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i < newBindings.Length)
|
|
|
|
|
{
|
|
|
|
|
_bindings[i] = newBindings[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateLabel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport("user32")]
|
|
|
|
|
private static extern bool HideCaret(IntPtr hWnd);
|
|
|
|
|
|
2011-07-09 23:03:42 +00:00
|
|
|
|
protected override void OnMouseClick(MouseEventArgs e)
|
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
HideCaret(Handle);
|
2011-07-09 23:03:42 +00:00
|
|
|
|
base.OnMouseClick(e);
|
2011-07-09 16:12:56 +00:00
|
|
|
|
}
|
2011-01-30 23:06:43 +00:00
|
|
|
|
|
2013-10-10 01:53:18 +00:00
|
|
|
|
private void ClearBindings()
|
2011-07-09 16:12:56 +00:00
|
|
|
|
{
|
2014-02-08 16:15:07 +00:00
|
|
|
|
for (var i = 0; i < _maxBind; i++)
|
2011-07-09 16:12:56 +00:00
|
|
|
|
{
|
2014-02-08 16:15:07 +00:00
|
|
|
|
_bindings[i] = string.Empty;
|
2011-07-09 16:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-07-07 16:48:29 +00:00
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
protected override void OnEnter(EventArgs e)
|
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
_pos = 0;
|
|
|
|
|
_timer.Start();
|
2011-07-09 22:09:39 +00:00
|
|
|
|
|
2013-11-27 23:52:21 +00:00
|
|
|
|
_wasPressed = Input.Instance.GetNextBindEvent();
|
2013-10-11 16:09:36 +00:00
|
|
|
|
BackColor = Color.LightCyan;
|
2011-07-09 16:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnLeave(EventArgs e)
|
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
_timer.Stop();
|
2011-07-09 16:12:56 +00:00
|
|
|
|
UpdateLabel();
|
2013-11-27 23:52:21 +00:00
|
|
|
|
BackColor = SystemColors.Window;
|
2011-07-09 16:12:56 +00:00
|
|
|
|
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
|
|
|
|
|
2012-09-17 22:43:42 +00:00
|
|
|
|
public void EraseMappings()
|
|
|
|
|
{
|
2013-10-10 01:53:18 +00:00
|
|
|
|
ClearBindings();
|
2014-02-08 16:15:07 +00:00
|
|
|
|
Text = string.Empty;
|
2012-09-17 22:43:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-09 16:12:56 +00:00
|
|
|
|
private void ReadKeys()
|
2011-06-19 02:17:27 +00:00
|
|
|
|
{
|
2011-07-09 22:09:39 +00:00
|
|
|
|
Input.Instance.Update();
|
2014-02-08 16:15:07 +00:00
|
|
|
|
var bindingStr = Input.Instance.GetNextBindEvent();
|
|
|
|
|
if (!string.IsNullOrEmpty(_wasPressed) && bindingStr == _wasPressed)
|
2012-10-01 01:55:08 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-02-08 16:15:07 +00:00
|
|
|
|
|
|
|
|
|
if (bindingStr != null)
|
2011-06-19 02:17:27 +00:00
|
|
|
|
{
|
2014-02-08 16:15:07 +00:00
|
|
|
|
if (bindingStr == "Escape")
|
2011-07-09 16:12:56 +00:00
|
|
|
|
{
|
2014-02-08 16:15:07 +00:00
|
|
|
|
EraseMappings();
|
2011-07-09 16:12:56 +00:00
|
|
|
|
Increment();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-02-08 16:15:07 +00:00
|
|
|
|
|
|
|
|
|
if (bindingStr == "Alt+F4")
|
2013-10-10 01:53:18 +00:00
|
|
|
|
{
|
2011-07-09 16:12:56 +00:00
|
|
|
|
return;
|
2013-10-10 01:53:18 +00:00
|
|
|
|
}
|
2011-07-09 16:12:56 +00:00
|
|
|
|
|
2014-02-08 16:15:07 +00:00
|
|
|
|
if (!IsDuplicate(bindingStr))
|
2011-07-09 18:24:53 +00:00
|
|
|
|
{
|
2014-02-08 16:15:07 +00:00
|
|
|
|
_bindings[_pos] = bindingStr;
|
2011-07-09 18:24:53 +00:00
|
|
|
|
}
|
2014-02-08 16:15:07 +00:00
|
|
|
|
|
|
|
|
|
_wasPressed = bindingStr;
|
2011-07-09 21:55:14 +00:00
|
|
|
|
UpdateLabel();
|
|
|
|
|
Increment();
|
2011-07-09 18:24:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-10 17:49:33 +00:00
|
|
|
|
private bool IsDuplicate(string binding)
|
2011-07-09 18:24:53 +00:00
|
|
|
|
{
|
2013-08-04 20:21:58 +00:00
|
|
|
|
return _bindings.FirstOrDefault(x => x == binding) != null;
|
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
|
|
|
|
}
|
2013-10-10 01:53:18 +00:00
|
|
|
|
|
2014-02-08 16:15:07 +00:00
|
|
|
|
_wasPressed = string.Empty;
|
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);
|
2012-01-08 23:01:14 +00:00
|
|
|
|
return;
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
2013-10-10 01:53:18 +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
|
2011-07-09 22:22:45 +00:00
|
|
|
|
public void Increment()
|
2011-07-09 16:12:56 +00:00
|
|
|
|
{
|
2011-07-04 23:02:37 +00:00
|
|
|
|
if (AutoTab)
|
2013-10-10 01:53:18 +00:00
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
Parent.SelectNextControl(this, true, true, true, true);
|
2013-10-10 01:53:18 +00:00
|
|
|
|
}
|
2011-07-09 17:50:52 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
if (_pos < _maxBind)
|
2013-10-10 01:53:18 +00:00
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
_pos++;
|
2013-10-10 01:53:18 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
_pos = 0;
|
2013-10-10 01:53:18 +00:00
|
|
|
|
}
|
2011-07-09 22:22:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Decrement()
|
|
|
|
|
{
|
|
|
|
|
if (AutoTab)
|
2013-10-10 01:53:18 +00:00
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
Parent.SelectNextControl(this, false, true, true, true);
|
2013-10-10 01:53:18 +00:00
|
|
|
|
}
|
2011-07-09 22:22:45 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
if (_pos == 0)
|
2013-10-10 01:53:18 +00:00
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
_pos = _maxBind - 1;
|
2013-10-10 01:53:18 +00:00
|
|
|
|
}
|
2011-07-09 22:22:45 +00:00
|
|
|
|
else
|
2013-10-10 01:53:18 +00:00
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
_pos--;
|
2013-10-10 01:53:18 +00:00
|
|
|
|
}
|
2011-07-09 17:50:52 +00:00
|
|
|
|
}
|
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 UpdateLabel()
|
|
|
|
|
{
|
2014-02-08 16:15:07 +00:00
|
|
|
|
Text = string.Join(",", _bindings.Where(str => !string.IsNullOrWhiteSpace(str)));
|
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-07-09 22:07:13 +00:00
|
|
|
|
protected override void WndProc(ref Message m)
|
|
|
|
|
{
|
|
|
|
|
switch (m.Msg)
|
|
|
|
|
{
|
2014-02-08 16:15:07 +00:00
|
|
|
|
case 0x0201: // WM_LBUTTONDOWN
|
|
|
|
|
Focus();
|
|
|
|
|
return;
|
|
|
|
|
case 0x0203: // WM_LBUTTONDBLCLK
|
|
|
|
|
case 0x0204: // WM_RBUTTONDOWN
|
|
|
|
|
case 0x0205: // WM_RBUTTONUP
|
|
|
|
|
case 0x0206: // WM_RBUTTONDBLCLK
|
|
|
|
|
return;
|
2011-07-09 22:07:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.WndProc(ref m);
|
2011-07-09 22:22:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnMouseWheel(MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Delta > 0)
|
2013-10-10 01:53:18 +00:00
|
|
|
|
{
|
2011-07-09 22:22:45 +00:00
|
|
|
|
Decrement();
|
2013-10-10 01:53:18 +00:00
|
|
|
|
}
|
2011-07-09 22:22:45 +00:00
|
|
|
|
else
|
2013-10-10 01:53:18 +00:00
|
|
|
|
{
|
2011-07-09 22:22:45 +00:00
|
|
|
|
Increment();
|
2013-10-10 01:53:18 +00:00
|
|
|
|
}
|
2014-02-08 16:15:07 +00:00
|
|
|
|
|
2011-07-09 22:22:45 +00:00
|
|
|
|
base.OnMouseWheel(e);
|
|
|
|
|
}
|
2011-07-09 22:07:13 +00:00
|
|
|
|
|
2011-06-19 02:17:27 +00:00
|
|
|
|
protected override void OnGotFocus(EventArgs e)
|
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
HideCaret(Handle);
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
2013-11-27 23:52:21 +00:00
|
|
|
|
return !(keyData.ToString() == "F4" || keyData.ToString().Contains("Alt"));
|
2011-06-19 02:17:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-01-30 23:06:43 +00:00
|
|
|
|
}
|