using System; using System.Collections.Generic; namespace BizHawk.MultiClient { public class Controller : IController { private ControllerDefinition type; private Dictionary> bindings = new Dictionary>(); private Dictionary stickyButtons = new Dictionary(); private List unpressedButtons = new List(); private List forcePressedButtons = new List(); private List removeFromForcePressedButtons = new List(); public Controller(ControllerDefinition definition) { type = definition; foreach (var b in type.BoolButtons) { bindings[b] = new List(); stickyButtons[b] = false; } foreach (var f in type.FloatControls) bindings[f] = new List(); } public void BindButton(string button, string control) { bindings[button].Add(control); } public void BindMulti(string button, string controlString) { string[] controlbindings = controlString.Split(','); foreach (string control in controlbindings) bindings[button].Add(control.Trim()); } public ControllerDefinition Type { get { return type; } } public bool this[string button] { get { return IsPressed(button); } } public bool IsPressed(string button) { if (forcePressedButtons.Contains(button)) { removeFromForcePressedButtons.Add(button); return true; } if (unpressedButtons.Contains(button)) { if (IsPressedActually(button) == false) unpressedButtons.Remove(button); return false; } return IsPressedActually(button); } private bool IsPressedActually(string button) { bool sticky = stickyButtons[button]; foreach (var control in bindings[button]) if (Input.IsPressed(control)) return sticky ? false : true; return sticky ? true : false; } public float GetFloat(string name) { throw new NotImplementedException(); } public void UnpressButton(string name) { unpressedButtons.Add(name); } private int frameNumber; public void UpdateControls(int frame) { if (frame != frameNumber) { // update unpressedButtons.RemoveAll(button => IsPressedActually(button) == false); forcePressedButtons.RemoveAll(button => removeFromForcePressedButtons.Contains(button)); removeFromForcePressedButtons.Clear(); } frameNumber = frame; } public void SetSticky(string button, bool sticky) { stickyButtons[button] = sticky; } public bool IsSticky(string button) { return stickyButtons[button]; } public void ForceButton(string button) { forcePressedButtons.Add(button); } } }