From 82ee945782fe691ba9b505a3920a5e4e96b4a3fb Mon Sep 17 00:00:00 2001 From: adelikat Date: Fri, 19 May 2017 08:51:35 -0500 Subject: [PATCH] Misc controller adapter cleanups --- .../inputAdapters/AutoPattern.cs | 31 ++++++----- .../inputAdapters/SimpleController.cs | 12 +--- .../inputAdapters/StickyAdapters.cs | 55 +++++++------------ .../inputAdapters/UDLRController.cs | 34 ++++++------ 4 files changed, 56 insertions(+), 76 deletions(-) diff --git a/BizHawk.Client.Common/inputAdapters/AutoPattern.cs b/BizHawk.Client.Common/inputAdapters/AutoPattern.cs index 13d26191ad..eac3c16eb8 100644 --- a/BizHawk.Client.Common/inputAdapters/AutoPattern.cs +++ b/BizHawk.Client.Common/inputAdapters/AutoPattern.cs @@ -2,11 +2,6 @@ { public class AutoPatternBool { - public readonly bool SkipsLag = true; - public readonly bool[] Pattern; - public readonly int Loop = 0; - private int _index = 0; - public AutoPatternBool() { Pattern = new[] { true }; @@ -36,6 +31,12 @@ Loop = loop; } + private int _index; + + public bool SkipsLag { get; } = true; + public bool[] Pattern { get; } + public int Loop { get; } + /// /// Gets the next value and increments index. /// @@ -70,11 +71,6 @@ public class AutoPatternFloat { - public readonly bool SkipsLag = true; - public readonly float[] Pattern; - public readonly int Loop = 0; - private int _index; - /// /// Initializes a new instance of the class. /// Defaults to 0. @@ -85,11 +81,12 @@ } /// + /// Initializes a new instance of the class. /// Simple on/off pattern, using the given values as on/off. /// - public AutoPatternFloat(float valueOn, int on, float valueOff, int off, bool skip_lag = true, int offset = 0, int loop = 0) + public AutoPatternFloat(float valueOn, int on, float valueOff, int off, bool skipLag = true, int offset = 0, int loop = 0) { - SkipsLag = skip_lag; + SkipsLag = skipLag; _index = offset; Loop = loop; Pattern = new float[on + off]; @@ -104,14 +101,20 @@ } } - public AutoPatternFloat(float[] pattern, bool skip_lag = true, int offset = 0, int loop = 0) + public AutoPatternFloat(float[] pattern, bool skipLag = true, int offset = 0, int loop = 0) { - SkipsLag = skip_lag; + SkipsLag = skipLag; Pattern = pattern; _index = offset; Loop = loop; } + private int _index; + + public bool SkipsLag { get; } = true; + public float[] Pattern { get; } + public int Loop { get; } + /// /// Gets the next value and increments index. /// diff --git a/BizHawk.Client.Common/inputAdapters/SimpleController.cs b/BizHawk.Client.Common/inputAdapters/SimpleController.cs index 4f44267885..e38fb133db 100644 --- a/BizHawk.Client.Common/inputAdapters/SimpleController.cs +++ b/BizHawk.Client.Common/inputAdapters/SimpleController.cs @@ -13,8 +13,8 @@ namespace BizHawk.Client.Common { public ControllerDefinition Definition { get; set; } - protected WorkingDictionary Buttons = new WorkingDictionary(); - protected WorkingDictionary Floats = new WorkingDictionary(); + protected WorkingDictionary Buttons { get; private set; } = new WorkingDictionary(); + protected WorkingDictionary Floats { get; private set; } = new WorkingDictionary(); public void Clear() { @@ -43,14 +43,6 @@ namespace BizHawk.Client.Common return Buttons; } - public void LatchFrom(IController source) - { - foreach (var button in source.Definition.BoolButtons) - { - Buttons[button] = source.IsPressed(button); - } - } - public void AcceptNewFloats(IEnumerable> newValues) { foreach (var sv in newValues) diff --git a/BizHawk.Client.Common/inputAdapters/StickyAdapters.cs b/BizHawk.Client.Common/inputAdapters/StickyAdapters.cs index a92d6289dc..bf663c38df 100644 --- a/BizHawk.Client.Common/inputAdapters/StickyAdapters.cs +++ b/BizHawk.Client.Common/inputAdapters/StickyAdapters.cs @@ -58,7 +58,7 @@ namespace BizHawk.Client.Common public bool IsPressed(string button) { var source = Source.IsPressed(button); - source ^= stickySet.Contains(button); + source ^= _stickySet.Contains(button); return source; } @@ -81,15 +81,13 @@ namespace BizHawk.Client.Common public IController Source { get; set; } - public bool Locked { get; set; } // Pretty much a hack, - private List _justPressed = new List(); - protected readonly HashSet stickySet = new HashSet(); + private readonly HashSet _stickySet = new HashSet(); // if SetFloat() is called (typically virtual pads), then that float will entirely override the Source input // otherwise, the source is passed thru. - protected readonly WorkingDictionary _floatSet = new WorkingDictionary(); + private readonly WorkingDictionary _floatSet = new WorkingDictionary(); public void SetFloat(string name, float? value) { @@ -112,30 +110,30 @@ namespace BizHawk.Client.Common { if (isSticky) { - stickySet.Add(button); + _stickySet.Add(button); } else { - stickySet.Remove(button); + _stickySet.Remove(button); } } public void Unset(string button) { - stickySet.Remove(button); + _stickySet.Remove(button); _floatSet.Remove(button); } public bool IsSticky(string button) { - return stickySet.Contains(button); + return _stickySet.Contains(button); } - public HashSet CurrentStickies => stickySet; + public HashSet CurrentStickies => _stickySet; public void ClearStickies() { - stickySet.Clear(); + _stickySet.Clear(); _floatSet.Clear(); } @@ -143,13 +141,13 @@ namespace BizHawk.Client.Common { foreach (var button in buttons.Where(button => !_justPressed.Contains(button))) { - if (stickySet.Contains(button)) + if (_stickySet.Contains(button)) { - stickySet.Remove(button); + _stickySet.Remove(button); } else { - stickySet.Add(button); + _stickySet.Add(button); } } @@ -207,13 +205,13 @@ namespace BizHawk.Client.Common // TODO: Change the AutoHold adapter to be one of these, with an 'Off' value of 0? // Probably would have slightly lower performance, but it seems weird to have such a similar class that is only used once. - private int On; - private int Off; + private int _on; + private int _off; public void SetOnOffPatternFromConfig() { - On = Global.Config.AutofireOn < 1 ? 0 : Global.Config.AutofireOn; - Off = Global.Config.AutofireOff < 1 ? 0 : Global.Config.AutofireOff; + _on = Global.Config.AutofireOn < 1 ? 0 : Global.Config.AutofireOn; + _off = Global.Config.AutofireOff < 1 ? 0 : Global.Config.AutofireOff; } private readonly WorkingDictionary _boolPatterns = new WorkingDictionary(); @@ -221,21 +219,19 @@ namespace BizHawk.Client.Common public AutoFireStickyXorAdapter() { - On = 1; - Off = 1; + _on = 1; + _off = 1; } public IController Source { get; set; } - public bool Locked { get; set; } // Pretty much a hack, - public void SetFloat(string name, float? value, AutoPatternFloat pattern = null) { if (value.HasValue) { if (pattern == null) { - pattern = new AutoPatternFloat(value.Value, On, 0, Off); + pattern = new AutoPatternFloat(value.Value, _on, 0, _off); } _floatPatterns[name] = pattern; @@ -246,18 +242,13 @@ namespace BizHawk.Client.Common } } - public void ClearStickyFloats() - { - _floatPatterns.Clear(); - } - public void SetSticky(string button, bool isSticky, AutoPatternBool pattern = null) { if (isSticky) { if (pattern == null) { - pattern = new AutoPatternBool(On, Off); + pattern = new AutoPatternBool(_on, _off); } _boolPatterns[button] = pattern; @@ -268,12 +259,6 @@ namespace BizHawk.Client.Common } } - public void Unset(string button) - { - _boolPatterns.Remove(button); - _floatPatterns.Remove(button); - } - public bool IsSticky(string button) { return _boolPatterns.ContainsKey(button) || _floatPatterns.ContainsKey(button); diff --git a/BizHawk.Client.Common/inputAdapters/UDLRController.cs b/BizHawk.Client.Common/inputAdapters/UDLRController.cs index e29629cf7d..38222679a3 100644 --- a/BizHawk.Client.Common/inputAdapters/UDLRController.cs +++ b/BizHawk.Client.Common/inputAdapters/UDLRController.cs @@ -28,14 +28,14 @@ namespace BizHawk.Client.Common { if (!Source.IsPressed(button)) { - Unpresses.Remove(button); + _unpresses.Remove(button); } prefix = button.GetPrecedingString("Down"); string other = prefix + "Up"; if (Source.IsPressed(other)) { - if (Unpresses.Contains(button)) + if (_unpresses.Contains(button)) { return false; } @@ -45,11 +45,11 @@ namespace BizHawk.Client.Common return false; } - Unpresses.Add(other); + _unpresses.Add(other); } else { - Unpresses.Remove(button); + _unpresses.Remove(button); } } @@ -57,14 +57,14 @@ namespace BizHawk.Client.Common { if (!Source.IsPressed(button)) { - Unpresses.Remove(button); + _unpresses.Remove(button); } prefix = button.GetPrecedingString("Up"); string other = prefix + "Down"; if (Source.IsPressed(other)) { - if (Unpresses.Contains(button)) + if (_unpresses.Contains(button)) { return false; } @@ -74,11 +74,11 @@ namespace BizHawk.Client.Common return false; } - Unpresses.Add(other); + _unpresses.Add(other); } else { - Unpresses.Remove(button); + _unpresses.Remove(button); } } @@ -86,14 +86,14 @@ namespace BizHawk.Client.Common { if (!Source.IsPressed(button)) { - Unpresses.Remove(button); + _unpresses.Remove(button); } prefix = button.GetPrecedingString("Right"); string other = prefix + "Left"; if (Source.IsPressed(other)) { - if (Unpresses.Contains(button)) + if (_unpresses.Contains(button)) { return false; } @@ -103,11 +103,11 @@ namespace BizHawk.Client.Common return false; } - Unpresses.Add(other); + _unpresses.Add(other); } else { - Unpresses.Remove(button); + _unpresses.Remove(button); } } @@ -115,14 +115,14 @@ namespace BizHawk.Client.Common { if (!Source.IsPressed(button)) { - Unpresses.Remove(button); + _unpresses.Remove(button); } prefix = button.GetPrecedingString("Left"); string other = prefix + "Right"; if (Source.IsPressed(other)) { - if (Unpresses.Contains(button)) + if (_unpresses.Contains(button)) { return false; } @@ -132,11 +132,11 @@ namespace BizHawk.Client.Common return false; } - Unpresses.Add(other); + _unpresses.Add(other); } else { - Unpresses.Remove(button); + _unpresses.Remove(button); } } @@ -149,7 +149,7 @@ namespace BizHawk.Client.Common return Source.GetFloat(name); } - private readonly HashSet Unpresses = new HashSet(); + private readonly HashSet _unpresses = new HashSet(); public IController Source { get; set; } }