From 0d088bb15b924a3b8daa13dcca702825b509b273 Mon Sep 17 00:00:00 2001 From: beirich Date: Fri, 14 Jan 2011 03:38:26 +0000 Subject: [PATCH] update controls to support sticky buttons, & force-pressing buttons. implement Soft Reset menu item. --- .../Interfaces/Base Implementations/Movies.cs | 34 ++++++++--- .../Base Implementations/NullController.cs | 10 +++- BizHawk.Emulation/Interfaces/IController.cs | 11 +++- .../Sound/Utilities/SoundMixer.cs | 1 - .../Input/ControllerBinding.cs | 57 ++++++++++++++----- BizHawk.MultiClient/MainForm.cs | 3 +- 6 files changed, 87 insertions(+), 29 deletions(-) diff --git a/BizHawk.Emulation/Interfaces/Base Implementations/Movies.cs b/BizHawk.Emulation/Interfaces/Base Implementations/Movies.cs index 98b7d10ead..14450b7ae0 100644 --- a/BizHawk.Emulation/Interfaces/Base Implementations/Movies.cs +++ b/BizHawk.Emulation/Interfaces/Base Implementations/Movies.cs @@ -23,14 +23,14 @@ namespace BizHawk get { return baseController.Type; } } - public bool this[string name] + public bool this[string button] { - get { return baseController[name]; } + get { return baseController[button]; } } - public bool IsPressed(string name) + public bool IsPressed(string button) { - return baseController[name]; + return baseController[button]; } public float GetFloat(string name) @@ -43,6 +43,11 @@ namespace BizHawk baseController.UnpressButton(name); } + public void ForceButton(string button) + { + baseController.ForceButton(button); + } + private int frame; public int FrameNumber { @@ -71,6 +76,16 @@ namespace BizHawk writer.Seek(frame*2, SeekOrigin.Begin); writer.Write((ushort)encodedValue); } + + public void SetSticky(string button, bool sticky) + { + baseController.SetSticky(button, sticky); + } + + public bool IsSticky(string button) + { + return baseController.IsSticky(button); + } } public class InputPlayback : IController @@ -92,19 +107,19 @@ namespace BizHawk get { return def; } } - public bool this[string name] + public bool this[string button] { - get { return IsPressed(name); } + get { return IsPressed(button); } } - public bool IsPressed(string name) + public bool IsPressed(string button) { if (FrameNumber >= input.Length) return false; for (int i = 0; i < def.BoolButtons.Count; i++) { - if (def.BoolButtons[i] == name) + if (def.BoolButtons[i] == button) { return (input[FrameNumber] & (1 << i)) != 0; } @@ -118,6 +133,9 @@ namespace BizHawk } public void UnpressButton(string name) {} + public void ForceButton(string button) { } + public void SetSticky(string button, bool sticky) { } + public bool IsSticky(string button) { return false; } public int FrameNumber { get; set; } public bool MovieEnded { get { return FrameNumber >= input.Length; } } diff --git a/BizHawk.Emulation/Interfaces/Base Implementations/NullController.cs b/BizHawk.Emulation/Interfaces/Base Implementations/NullController.cs index 821c07bc9a..0c58700773 100644 --- a/BizHawk.Emulation/Interfaces/Base Implementations/NullController.cs +++ b/BizHawk.Emulation/Interfaces/Base Implementations/NullController.cs @@ -3,12 +3,16 @@ public class NullController : IController { public ControllerDefinition Type { get { return null; } } - public bool this[string name] { get { return false; } } - public bool IsPressed(string name) { return false; } + public bool this[string button] { get { return false; } } + public bool IsPressed(string button) { return false; } public float GetFloat(string name) { return 0f; } - public void UnpressButton(string name) { } + public void UnpressButton(string button) { } + public void ForceButton(string button) { } public int FrameNumber { get; set; } + public void SetSticky(string button, bool sticky) { } + public bool IsSticky(string button) { return false; } + private static NullController nullController = new NullController(); public static NullController GetNullController() { return nullController; } } diff --git a/BizHawk.Emulation/Interfaces/IController.cs b/BizHawk.Emulation/Interfaces/IController.cs index a4a4fe64a0..7381f6fdbe 100644 --- a/BizHawk.Emulation/Interfaces/IController.cs +++ b/BizHawk.Emulation/Interfaces/IController.cs @@ -13,10 +13,15 @@ namespace BizHawk { ControllerDefinition Type { get; } - bool this[string name] { get; } - bool IsPressed(string name); + bool this[string button] { get; } + bool IsPressed(string button); float GetFloat(string name); - void UnpressButton(string name); + + void SetSticky(string button, bool sticky); + bool IsSticky(string button); + + void UnpressButton(string button); + void ForceButton(string button); int FrameNumber { get; set; } } diff --git a/BizHawk.Emulation/Sound/Utilities/SoundMixer.cs b/BizHawk.Emulation/Sound/Utilities/SoundMixer.cs index 46cfaaeb1f..56a38c239b 100644 --- a/BizHawk.Emulation/Sound/Utilities/SoundMixer.cs +++ b/BizHawk.Emulation/Sound/Utilities/SoundMixer.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using BizHawk.Emulation.Sound; namespace BizHawk.Emulation.Sound { diff --git a/BizHawk.MultiClient/Input/ControllerBinding.cs b/BizHawk.MultiClient/Input/ControllerBinding.cs index d4da2747a4..2cdb62798a 100644 --- a/BizHawk.MultiClient/Input/ControllerBinding.cs +++ b/BizHawk.MultiClient/Input/ControllerBinding.cs @@ -7,14 +7,20 @@ namespace BizHawk.MultiClient { 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(); @@ -37,31 +43,38 @@ namespace BizHawk.MultiClient get { return type; } } - public bool this[string name] + public bool this[string button] { - get { return IsPressed(name); } + get { return IsPressed(button); } } - public bool IsPressed(string name) + public bool IsPressed(string button) { - if (unpressedButtons.Contains(name)) + if (forcePressedButtons.Contains(button)) { - if (IsPressedActually(name) == false) - unpressedButtons.Remove(name); + removeFromForcePressedButtons.Add(button); + return true; + } + if (unpressedButtons.Contains(button)) + { + if (IsPressedActually(button) == false) + unpressedButtons.Remove(button); return false; } - return IsPressedActually(name); + return IsPressedActually(button); } - private bool IsPressedActually(string name) + private bool IsPressedActually(string button) { - foreach (var control in bindings[name]) - if (Input.IsPressed(control)) - return true; + bool sticky = stickyButtons[button]; - return false; + foreach (var control in bindings[button]) + if (Input.IsPressed(control)) + return sticky ? false : true; + + return sticky ? true : false; } public float GetFloat(string name) @@ -87,9 +100,27 @@ namespace BizHawk.MultiClient { // update unpressedButtons.RemoveAll(button => IsPressedActually(button) == false); + forcePressedButtons.RemoveAll(button => removeFromForcePressedButtons.Contains(button)); + removeFromForcePressedButtons.Clear(); } frameNumber = value; } } + + + 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); + } } -} +} \ No newline at end of file diff --git a/BizHawk.MultiClient/MainForm.cs b/BizHawk.MultiClient/MainForm.cs index 65a78510d9..ade928c12c 100644 --- a/BizHawk.MultiClient/MainForm.cs +++ b/BizHawk.MultiClient/MainForm.cs @@ -395,7 +395,8 @@ namespace BizHawk.MultiClient private void resetToolStripMenuItem_Click(object sender, EventArgs e) { - + if (Global.Emulator.ControllerDefinition.BoolButtons.Contains("Reset")) + Global.Emulator.Controller.ForceButton("Reset"); } private void pauseToolStripMenuItem_Click(object sender, EventArgs e)