Remove `WorkingDictionary<K, V>`

This commit is contained in:
YoshiRulz 2024-08-23 02:20:08 +10:00
parent 34d71e90bc
commit 7dfb624427
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
10 changed files with 89 additions and 92 deletions

View File

@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -23,21 +24,29 @@ namespace BizHawk.Client.Common
public ControllerDefinition Definition { get; }
public bool IsPressed(string button) => _buttons[button];
public bool IsPressed(string button)
=> _buttons.GetValueOrDefault(button);
public int AxisValue(string name) => _axes[name];
public int AxisValue(string name)
=> _axes.GetValueOrDefault(name);
public IReadOnlyCollection<(string Name, int Strength)> GetHapticsSnapshot()
=> _haptics.Select(kvp => (kvp.Key, kvp.Value)).ToArray();
public void SetHapticChannelStrength(string name, int strength) => _haptics[name] = strength;
private readonly WorkingDictionary<string, List<string>> _bindings = new WorkingDictionary<string, List<string>>();
private readonly WorkingDictionary<string, bool> _buttons = new WorkingDictionary<string, bool>();
private readonly WorkingDictionary<string, int> _axes = new WorkingDictionary<string, int>();
private readonly Dictionary<string, AxisSpec> _axisRanges = new WorkingDictionary<string, AxisSpec>();
private readonly Dictionary<string, List<string>> _bindings = new();
private readonly Dictionary<string, bool> _buttons = new();
private readonly Dictionary<string, int> _axes = new();
private readonly Dictionary<string, AxisSpec> _axisRanges = new();
private readonly Dictionary<string, AnalogBind> _axisBindings = new Dictionary<string, AnalogBind>();
private readonly Dictionary<string, int> _haptics = new WorkingDictionary<string, int>();
private readonly Dictionary<string, int> _haptics = new();
private readonly Dictionary<string, FeedbackBind> _feedbackBindings = new Dictionary<string, FeedbackBind>();
public bool this[string button] => IsPressed(button);
@ -89,7 +98,7 @@ namespace BizHawk.Client.Common
value *= v.Mult;
// -1..1 -> -A..A (where A is the larger "side" of the range e.g. a range of 0..50, neutral=10 would give A=40, and thus a value in -40..40)
var range = _axisRanges[k];
var range = _axisRanges[k]; // this was `GetValueOrPutNew`, but I really hope it was always found, since `new AxisSpec()` isn't valid --yoshi
value *= Math.Max(range.Neutral - range.Min, range.Max - range.Neutral);
// shift the midpoint, so a value of 0 becomes range.Neutral (and, assuming >=1x multiplier, all values in range are reachable)
@ -148,7 +157,7 @@ namespace BizHawk.Client.Common
_axes[button] = controller.AxisValue(button);
}
foreach (var button in controller.InversedButtons) _buttons[button] = !_buttons[button];
foreach (var button in controller.InversedButtons) _buttons[button] = !_buttons.GetValueOrDefault(button);
}
public void BindMulti(string button, string controlString)
@ -161,7 +170,7 @@ namespace BizHawk.Client.Common
var controlBindings = controlString.Split(',');
foreach (var control in controlBindings)
{
_bindings[button].Add(control.Trim());
_bindings.GetValueOrPutNew(button).Add(control.Trim());
}
}

View File

@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -20,11 +21,11 @@ namespace BizHawk.Client.Common
private readonly IEmulator _emulator;
private readonly WorkingDictionary<string, List<string>> _bindings = new WorkingDictionary<string, List<string>>();
private readonly Dictionary<string, List<string>> _bindings = new();
private WorkingDictionary<string, bool> _buttons = new();
private Dictionary<string, bool> _buttons = new();
private readonly WorkingDictionary<string, int> _buttonStarts = new WorkingDictionary<string, int>();
private readonly Dictionary<string, int> _buttonStarts = new();
public int On { get; set; }
public int Off { get; set; }
@ -34,8 +35,8 @@ namespace BizHawk.Client.Common
public ControllerDefinition Definition => _emulator.ControllerDefinition;
public bool IsPressed(string button)
=> _buttons[button]
&& ((internal_frame - _buttonStarts[button]) % (On + Off)) < On;
=> _buttons.GetValueOrDefault(button)
&& ((internal_frame - _buttonStarts.GetValueOrDefault(button)) % (On + Off)) < On;
public void ClearStarts()
{
@ -69,7 +70,7 @@ namespace BizHawk.Client.Common
if (controller.IsPressed(boundBtn))
{
isPressed = true;
if (!buttonStatesPrev[k]) _buttonStarts[k] = internal_frame;
if (!buttonStatesPrev.GetValueOrDefault(k)) _buttonStarts[k] = internal_frame;
}
}
_buttons[k] = isPressed;
@ -83,7 +84,7 @@ namespace BizHawk.Client.Common
var controlBindings = controlString.Split(',');
foreach (var control in controlBindings)
{
_bindings[button].Add(control.Trim());
_bindings.GetValueOrPutNew(button).Add(control.Trim());
}
}
}

View File

@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -15,29 +15,33 @@ namespace BizHawk.Client.Common
public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;
protected WorkingDictionary<string, bool> Buttons { get; private set; } = new WorkingDictionary<string, bool>();
protected WorkingDictionary<string, int> Axes { get; private set; } = new WorkingDictionary<string, int>();
protected WorkingDictionary<string, int> HapticFeedback { get; private set; } = new WorkingDictionary<string, int>();
protected Dictionary<string, int> Axes { get; private set; } = new();
protected Dictionary<string, bool> Buttons { get; private set; } = new();
protected Dictionary<string, int> HapticFeedback { get; private set; } = new();
public SimpleController(ControllerDefinition definition)
=> Definition = definition;
public void Clear()
{
Buttons = new WorkingDictionary<string, bool>();
Axes = new WorkingDictionary<string, int>();
HapticFeedback = new WorkingDictionary<string, int>();
Buttons = new();
Axes = new();
HapticFeedback = new();
}
public bool this[string button]
{
get => Buttons[button];
get => Buttons.GetValueOrDefault(button);
set => Buttons[button] = value;
}
public virtual bool IsPressed(string button) => this[button];
public virtual bool IsPressed(string button)
=> Buttons.GetValueOrDefault(button);
public int AxisValue(string name) => Axes[name];
public int AxisValue(string name)
=> Axes.GetValueOrDefault(name);
public IReadOnlyCollection<(string Name, int Strength)> GetHapticsSnapshot()
=> HapticFeedback.Select(kvp => (kvp.Key, kvp.Value)).ToArray();

View File

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -26,13 +25,7 @@ namespace BizHawk.Client.Common
public int AxisValue(string name)
{
var val = _axisSet[name];
if (val.HasValue)
{
return val.Value;
}
if (_axisSet.TryGetValue(name, out var i)) return i;
if (Source == null)
{
return 0;
@ -51,13 +44,13 @@ namespace BizHawk.Client.Common
// if SetAxis() is called (typically virtual pads), then that axis will entirely override the Source input
// otherwise, the source is passed thru.
private readonly WorkingDictionary<string, int?> _axisSet = new WorkingDictionary<string, int?>();
private readonly Dictionary<string, int> _axisSet = new();
public void SetAxis(string name, int? value)
{
if (value.HasValue)
if (value is int i)
{
_axisSet[name] = value;
_axisSet[name] = i;
}
else
{
@ -154,8 +147,9 @@ namespace BizHawk.Client.Common
_off = off < 0 ? 0 : off;
}
private readonly WorkingDictionary<string, AutoPatternBool> _boolPatterns = new WorkingDictionary<string, AutoPatternBool>();
private readonly WorkingDictionary<string, AutoPatternAxis> _axisPatterns = new WorkingDictionary<string, AutoPatternAxis>();
private readonly Dictionary<string, AutoPatternAxis> _axisPatterns = new();
private readonly Dictionary<string, AutoPatternBool> _boolPatterns = new();
public AutoFireStickyXorAdapter()
{

View File

@ -3,14 +3,16 @@ using System.Diagnostics;
using System.Linq;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
internal class Bk2Controller : IMovieController
{
private readonly WorkingDictionary<string, bool> _myBoolButtons = new();
private readonly WorkingDictionary<string, int> _myAxisControls = new();
private readonly Dictionary<string, int> _myAxisControls = new();
private readonly Dictionary<string, bool> _myBoolButtons = new();
private readonly Bk2ControllerDefinition _type;
@ -45,8 +47,11 @@ namespace BizHawk.Client.Common
public ControllerDefinition Definition => _type;
public bool IsPressed(string button) => _myBoolButtons[button];
public int AxisValue(string name) => _myAxisControls[name];
public int AxisValue(string name)
=> _myAxisControls.GetValueOrDefault(name);
public bool IsPressed(string button)
=> _myBoolButtons.GetValueOrDefault(button);
public IReadOnlyCollection<(string Name, int Strength)> GetHapticsSnapshot() => Array.Empty<(string, int)>();

View File

@ -1,6 +1,6 @@
using System.Collections.Generic;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -39,14 +39,10 @@ namespace BizHawk.Client.Common
public ControllerDefinition Definition { get; set; }
public bool IsPressed(string button)
{
return _myBoolButtons[button];
}
=> _myBoolButtons.GetValueOrDefault(button);
public int AxisValue(string name)
{
return _myAxisControls[name];
}
=> _myAxisControls.GetValueOrDefault(name);
public IReadOnlyCollection<(string Name, int Strength)> GetHapticsSnapshot() => throw new NotImplementedException(); // no idea --yoshi
@ -212,8 +208,9 @@ namespace BizHawk.Client.Common
}
}
private readonly WorkingDictionary<string, bool> _myBoolButtons = new WorkingDictionary<string, bool>();
private readonly WorkingDictionary<string, int> _myAxisControls = new WorkingDictionary<string, int>();
private readonly Dictionary<string, int> _myAxisControls = new();
private readonly Dictionary<string, bool> _myBoolButtons = new();
private bool IsGenesis6Button() => Definition.BoolButtons.Contains("P1 X");

View File

@ -58,9 +58,12 @@ namespace BizHawk.Client.EmuHawk
_updateThread.Start();
}
private readonly WorkingDictionary<string, bool> _lastState = new WorkingDictionary<string, bool>();
private readonly WorkingDictionary<string, int> _axisValues = new WorkingDictionary<string, int>();
private readonly WorkingDictionary<string, float> _axisDeltas = new WorkingDictionary<string, float>();
private readonly Dictionary<string, float> _axisDeltas = new();
private readonly Dictionary<string, int> _axisValues = new();
private readonly Dictionary<string, bool> _lastState = new();
private bool _trackDeltas;
private bool _ignoreEventsNextPoll;
@ -99,7 +102,7 @@ namespace BizHawk.Client.EmuHawk
var modIndex = _currentConfig.ModifierKeysEffective.IndexOf(button1);
var currentModifier = modIndex is -1 ? 0U : 1U << modIndex;
if (EnableIgnoreModifiers && currentModifier is not 0U) return;
if (newState == _lastState[button1]) return;
if (newState == _lastState.GetValueOrDefault(button1)) return;
if (currentModifier is not 0U)
{
@ -135,7 +138,11 @@ namespace BizHawk.Client.EmuHawk
if (ShouldSwallow(MainFormInputAllowedCallback(false), ClientInputFocus.Pad))
return;
if (_trackDeltas) _axisDeltas[axis] += Math.Abs(newValue - _axisValues[axis]);
if (_trackDeltas)
{
_axisDeltas[axis] = _axisDeltas.GetValueOrDefault(axis)
+ Math.Abs(newValue - _axisValues.GetValueOrDefault(axis));
}
_axisValues[axis] = newValue;
}
@ -217,8 +224,11 @@ namespace BizHawk.Client.EmuHawk
if (_trackDeltas)
{
// these are relative to screen coordinates, but that's not terribly important
_axisDeltas["WMouse X"] += Math.Abs(mousePos.X - _axisValues["WMouse X"]) * 50;
_axisDeltas["WMouse Y"] += Math.Abs(mousePos.Y - _axisValues["WMouse Y"]) * 50;
const float MOUSE_DELTA_SCALE = 50.0f;
_axisDeltas["WMouse X"] = _axisDeltas.GetValueOrDefault("WMouse X")
+ MOUSE_DELTA_SCALE * Math.Abs(mousePos.X - _axisValues.GetValueOrDefault("WMouse X"));
_axisDeltas["WMouse Y"] = _axisDeltas.GetValueOrDefault("WMouse Y")
+ MOUSE_DELTA_SCALE * Math.Abs(mousePos.Y - _axisValues.GetValueOrDefault("WMouse Y"));
}
// coordinate translation happens later
_axisValues["WMouse X"] = mousePos.X;

View File

@ -1,9 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using BizHawk.Common.CollectionExtensions;
namespace BizHawk.Common
{
@ -103,22 +99,4 @@ namespace BizHawk.Common
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
/// <summary>A dictionary whose index getter creates an entry if the requested key isn't part of the collection, making it always safe to use the returned value. The new entry's value will be the result of the default constructor of <typeparamref name="TValue"/>.</summary>
[Serializable]
public class WorkingDictionary<TKey, TValue> : Dictionary<TKey, TValue>
where TKey : notnull
where TValue : new()
{
public WorkingDictionary() {}
protected WorkingDictionary(SerializationInfo info, StreamingContext context) : base(info, context) {}
[property: MaybeNull]
public new TValue this[TKey key]
{
get => this.GetValueOrPutNew(key);
set => base[key] = value;
}
}
}

View File

@ -91,7 +91,9 @@ namespace BizHawk.Emulation.Common
case CircularAxisConstraint circular:
var xAxis = k;
var yAxis = circular.PairedAxis;
(axes[xAxis], axes[yAxis]) = circular.ApplyTo(axes[xAxis], axes[yAxis]);
(axes[xAxis], axes[yAxis]) = circular.ApplyTo(
CollectionExtensions.GetValueOrDefault(axes, xAxis),
CollectionExtensions.GetValueOrDefault(axes, yAxis));
break;
}
}

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.IO;
using BizHawk.Common;
using BizHawk.Common.CollectionExtensions;
namespace BizHawk.Emulation.Common
{
@ -12,7 +13,7 @@ namespace BizHawk.Emulation.Common
/// </summary>
public class SaveController : IController
{
private readonly WorkingDictionary<string, int> _buttons = new WorkingDictionary<string, int>();
private readonly Dictionary<string, int> _buttons = new();
public IInputDisplayGenerator InputDisplayGenerator { get; set; } = null;
@ -35,10 +36,10 @@ namespace BizHawk.Emulation.Common
public void Serialize(BinaryWriter b)
{
b.Write(_buttons.Keys.Count);
foreach (var k in _buttons.Keys)
foreach (var (k, v) in _buttons)
{
b.Write(k);
b.Write(_buttons[k]);
b.Write(v);
}
}
@ -90,14 +91,10 @@ namespace BizHawk.Emulation.Common
}
public bool IsPressed(string button)
{
return _buttons[button] != 0;
}
=> _buttons.GetValueOrDefault(button) is not 0;
public int AxisValue(string name)
{
return _buttons[name];
}
=> _buttons.GetValueOrDefault(name);
public IReadOnlyCollection<(string Name, int Strength)> GetHapticsSnapshot() => Array.Empty<(string, int)>();