Misc controller adapter cleanups

This commit is contained in:
adelikat 2017-05-19 08:51:35 -05:00
parent e0e5eaea44
commit 82ee945782
4 changed files with 56 additions and 76 deletions

View File

@ -2,11 +2,6 @@
{ {
public class AutoPatternBool public class AutoPatternBool
{ {
public readonly bool SkipsLag = true;
public readonly bool[] Pattern;
public readonly int Loop = 0;
private int _index = 0;
public AutoPatternBool() public AutoPatternBool()
{ {
Pattern = new[] { true }; Pattern = new[] { true };
@ -36,6 +31,12 @@
Loop = loop; Loop = loop;
} }
private int _index;
public bool SkipsLag { get; } = true;
public bool[] Pattern { get; }
public int Loop { get; }
/// <summary> /// <summary>
/// Gets the next value and increments index. /// Gets the next value and increments index.
/// </summary> /// </summary>
@ -70,11 +71,6 @@
public class AutoPatternFloat public class AutoPatternFloat
{ {
public readonly bool SkipsLag = true;
public readonly float[] Pattern;
public readonly int Loop = 0;
private int _index;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="AutoPatternFloat"/> class. /// Initializes a new instance of the <see cref="AutoPatternFloat"/> class.
/// Defaults to 0. /// Defaults to 0.
@ -85,11 +81,12 @@
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="AutoPatternFloat"/> class.
/// Simple on/off pattern, using the given values as on/off. /// Simple on/off pattern, using the given values as on/off.
/// </summary> /// </summary>
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; _index = offset;
Loop = loop; Loop = loop;
Pattern = new float[on + off]; 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; Pattern = pattern;
_index = offset; _index = offset;
Loop = loop; Loop = loop;
} }
private int _index;
public bool SkipsLag { get; } = true;
public float[] Pattern { get; }
public int Loop { get; }
/// <summary> /// <summary>
/// Gets the next value and increments index. /// Gets the next value and increments index.
/// </summary> /// </summary>

View File

@ -13,8 +13,8 @@ namespace BizHawk.Client.Common
{ {
public ControllerDefinition Definition { get; set; } public ControllerDefinition Definition { get; set; }
protected WorkingDictionary<string, bool> Buttons = new WorkingDictionary<string, bool>(); protected WorkingDictionary<string, bool> Buttons { get; private set; } = new WorkingDictionary<string, bool>();
protected WorkingDictionary<string, float> Floats = new WorkingDictionary<string, float>(); protected WorkingDictionary<string, float> Floats { get; private set; } = new WorkingDictionary<string, float>();
public void Clear() public void Clear()
{ {
@ -43,14 +43,6 @@ namespace BizHawk.Client.Common
return Buttons; return Buttons;
} }
public void LatchFrom(IController source)
{
foreach (var button in source.Definition.BoolButtons)
{
Buttons[button] = source.IsPressed(button);
}
}
public void AcceptNewFloats(IEnumerable<Tuple<string, float>> newValues) public void AcceptNewFloats(IEnumerable<Tuple<string, float>> newValues)
{ {
foreach (var sv in newValues) foreach (var sv in newValues)

View File

@ -58,7 +58,7 @@ namespace BizHawk.Client.Common
public bool IsPressed(string button) public bool IsPressed(string button)
{ {
var source = Source.IsPressed(button); var source = Source.IsPressed(button);
source ^= stickySet.Contains(button); source ^= _stickySet.Contains(button);
return source; return source;
} }
@ -81,15 +81,13 @@ namespace BizHawk.Client.Common
public IController Source { get; set; } public IController Source { get; set; }
public bool Locked { get; set; } // Pretty much a hack,
private List<string> _justPressed = new List<string>(); private List<string> _justPressed = new List<string>();
protected readonly HashSet<string> stickySet = new HashSet<string>(); private readonly HashSet<string> _stickySet = new HashSet<string>();
// if SetFloat() is called (typically virtual pads), then that float will entirely override the Source input // if SetFloat() is called (typically virtual pads), then that float will entirely override the Source input
// otherwise, the source is passed thru. // otherwise, the source is passed thru.
protected readonly WorkingDictionary<string, float?> _floatSet = new WorkingDictionary<string, float?>(); private readonly WorkingDictionary<string, float?> _floatSet = new WorkingDictionary<string, float?>();
public void SetFloat(string name, float? value) public void SetFloat(string name, float? value)
{ {
@ -112,30 +110,30 @@ namespace BizHawk.Client.Common
{ {
if (isSticky) if (isSticky)
{ {
stickySet.Add(button); _stickySet.Add(button);
} }
else else
{ {
stickySet.Remove(button); _stickySet.Remove(button);
} }
} }
public void Unset(string button) public void Unset(string button)
{ {
stickySet.Remove(button); _stickySet.Remove(button);
_floatSet.Remove(button); _floatSet.Remove(button);
} }
public bool IsSticky(string button) public bool IsSticky(string button)
{ {
return stickySet.Contains(button); return _stickySet.Contains(button);
} }
public HashSet<string> CurrentStickies => stickySet; public HashSet<string> CurrentStickies => _stickySet;
public void ClearStickies() public void ClearStickies()
{ {
stickySet.Clear(); _stickySet.Clear();
_floatSet.Clear(); _floatSet.Clear();
} }
@ -143,13 +141,13 @@ namespace BizHawk.Client.Common
{ {
foreach (var button in buttons.Where(button => !_justPressed.Contains(button))) 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 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? // 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. // 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 _on;
private int Off; private int _off;
public void SetOnOffPatternFromConfig() public void SetOnOffPatternFromConfig()
{ {
On = Global.Config.AutofireOn < 1 ? 0 : Global.Config.AutofireOn; _on = Global.Config.AutofireOn < 1 ? 0 : Global.Config.AutofireOn;
Off = Global.Config.AutofireOff < 1 ? 0 : Global.Config.AutofireOff; _off = Global.Config.AutofireOff < 1 ? 0 : Global.Config.AutofireOff;
} }
private readonly WorkingDictionary<string, AutoPatternBool> _boolPatterns = new WorkingDictionary<string, AutoPatternBool>(); private readonly WorkingDictionary<string, AutoPatternBool> _boolPatterns = new WorkingDictionary<string, AutoPatternBool>();
@ -221,21 +219,19 @@ namespace BizHawk.Client.Common
public AutoFireStickyXorAdapter() public AutoFireStickyXorAdapter()
{ {
On = 1; _on = 1;
Off = 1; _off = 1;
} }
public IController Source { get; set; } public IController Source { get; set; }
public bool Locked { get; set; } // Pretty much a hack,
public void SetFloat(string name, float? value, AutoPatternFloat pattern = null) public void SetFloat(string name, float? value, AutoPatternFloat pattern = null)
{ {
if (value.HasValue) if (value.HasValue)
{ {
if (pattern == null) if (pattern == null)
{ {
pattern = new AutoPatternFloat(value.Value, On, 0, Off); pattern = new AutoPatternFloat(value.Value, _on, 0, _off);
} }
_floatPatterns[name] = pattern; _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) public void SetSticky(string button, bool isSticky, AutoPatternBool pattern = null)
{ {
if (isSticky) if (isSticky)
{ {
if (pattern == null) if (pattern == null)
{ {
pattern = new AutoPatternBool(On, Off); pattern = new AutoPatternBool(_on, _off);
} }
_boolPatterns[button] = pattern; _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) public bool IsSticky(string button)
{ {
return _boolPatterns.ContainsKey(button) || _floatPatterns.ContainsKey(button); return _boolPatterns.ContainsKey(button) || _floatPatterns.ContainsKey(button);

View File

@ -28,14 +28,14 @@ namespace BizHawk.Client.Common
{ {
if (!Source.IsPressed(button)) if (!Source.IsPressed(button))
{ {
Unpresses.Remove(button); _unpresses.Remove(button);
} }
prefix = button.GetPrecedingString("Down"); prefix = button.GetPrecedingString("Down");
string other = prefix + "Up"; string other = prefix + "Up";
if (Source.IsPressed(other)) if (Source.IsPressed(other))
{ {
if (Unpresses.Contains(button)) if (_unpresses.Contains(button))
{ {
return false; return false;
} }
@ -45,11 +45,11 @@ namespace BizHawk.Client.Common
return false; return false;
} }
Unpresses.Add(other); _unpresses.Add(other);
} }
else else
{ {
Unpresses.Remove(button); _unpresses.Remove(button);
} }
} }
@ -57,14 +57,14 @@ namespace BizHawk.Client.Common
{ {
if (!Source.IsPressed(button)) if (!Source.IsPressed(button))
{ {
Unpresses.Remove(button); _unpresses.Remove(button);
} }
prefix = button.GetPrecedingString("Up"); prefix = button.GetPrecedingString("Up");
string other = prefix + "Down"; string other = prefix + "Down";
if (Source.IsPressed(other)) if (Source.IsPressed(other))
{ {
if (Unpresses.Contains(button)) if (_unpresses.Contains(button))
{ {
return false; return false;
} }
@ -74,11 +74,11 @@ namespace BizHawk.Client.Common
return false; return false;
} }
Unpresses.Add(other); _unpresses.Add(other);
} }
else else
{ {
Unpresses.Remove(button); _unpresses.Remove(button);
} }
} }
@ -86,14 +86,14 @@ namespace BizHawk.Client.Common
{ {
if (!Source.IsPressed(button)) if (!Source.IsPressed(button))
{ {
Unpresses.Remove(button); _unpresses.Remove(button);
} }
prefix = button.GetPrecedingString("Right"); prefix = button.GetPrecedingString("Right");
string other = prefix + "Left"; string other = prefix + "Left";
if (Source.IsPressed(other)) if (Source.IsPressed(other))
{ {
if (Unpresses.Contains(button)) if (_unpresses.Contains(button))
{ {
return false; return false;
} }
@ -103,11 +103,11 @@ namespace BizHawk.Client.Common
return false; return false;
} }
Unpresses.Add(other); _unpresses.Add(other);
} }
else else
{ {
Unpresses.Remove(button); _unpresses.Remove(button);
} }
} }
@ -115,14 +115,14 @@ namespace BizHawk.Client.Common
{ {
if (!Source.IsPressed(button)) if (!Source.IsPressed(button))
{ {
Unpresses.Remove(button); _unpresses.Remove(button);
} }
prefix = button.GetPrecedingString("Left"); prefix = button.GetPrecedingString("Left");
string other = prefix + "Right"; string other = prefix + "Right";
if (Source.IsPressed(other)) if (Source.IsPressed(other))
{ {
if (Unpresses.Contains(button)) if (_unpresses.Contains(button))
{ {
return false; return false;
} }
@ -132,11 +132,11 @@ namespace BizHawk.Client.Common
return false; return false;
} }
Unpresses.Add(other); _unpresses.Add(other);
} }
else else
{ {
Unpresses.Remove(button); _unpresses.Remove(button);
} }
} }
@ -149,7 +149,7 @@ namespace BizHawk.Client.Common
return Source.GetFloat(name); return Source.GetFloat(name);
} }
private readonly HashSet<string> Unpresses = new HashSet<string>(); private readonly HashSet<string> _unpresses = new HashSet<string>();
public IController Source { get; set; } public IController Source { get; set; }
} }