Add `SetMembership` and `ToggleMembership` helpers

This commit is contained in:
YoshiRulz 2025-03-19 08:27:54 +10:00
parent 24a82551b9
commit fb1ef7ba7a
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
7 changed files with 64 additions and 59 deletions

View File

@ -1,4 +1,6 @@
using System.Collections.Generic;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -33,27 +35,9 @@ namespace BizHawk.Client.Common
public void Click(string button) => _pressed.Add(button);
public void Toggle(string button)
{
if (IsPressed(button))
{
_pressed.Remove(button);
}
else
{
_pressed.Add(button);
}
}
=> _ = _pressed.ToggleMembership(button);
public void SetBool(string button, bool value)
{
if (value)
{
_pressed.Remove(button);
}
else
{
_pressed.Add(button);
}
}
=> _pressed.SetMembership(button, shouldBeMember: !value);
}
}

View File

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
@ -37,16 +38,7 @@ namespace BizHawk.Client.Common
public void SetHapticChannelStrength(string name, int strength) => throw new NotSupportedException();
public void SetButtonHold(string button, bool enabled)
{
if (enabled)
{
_buttonHolds.Add(button);
}
else
{
_buttonHolds.Remove(button);
}
}
=> _buttonHolds.SetMembership(button, shouldBeMember: enabled);
public void SetAxisHold(string name, int? value)
{
@ -74,14 +66,7 @@ namespace BizHawk.Client.Common
{
foreach (var button in buttons.Where(button => !_justPressed.Contains(button)))
{
if (_buttonHolds.Contains(button))
{
_buttonHolds.Remove(button);
}
else
{
_buttonHolds.Add(button);
}
_ = _buttonHolds.ToggleMembership(button);
}
_justPressed = buttons;

View File

@ -20,8 +20,7 @@ namespace BizHawk.Client.EmuHawk
/// </summary>
public void ControlInputFocus(Control c, HostInputType types, bool wants)
{
if (types.HasFlag(HostInputType.Mouse) && wants) _wantingMouseFocus.Add(c);
if (types.HasFlag(HostInputType.Mouse) && !wants) _wantingMouseFocus.Remove(c);
if (types.HasFlag(HostInputType.Mouse)) _wantingMouseFocus.SetMembership(c, shouldBeMember: wants);
}
private readonly HashSet<Control> _wantingMouseFocus = new HashSet<Control>();

View File

@ -2085,13 +2085,9 @@ namespace BizHawk.Client.EmuHawk
{
ClearHighlighted();
}
else if (_secondaryHighlightedAddresses.Contains(pointedAddress))
{
_secondaryHighlightedAddresses.Remove(pointedAddress);
}
else
{
_secondaryHighlightedAddresses.Add(pointedAddress);
_ = _secondaryHighlightedAddresses.ToggleMembership(pointedAddress);
}
}
else if ((ModifierKeys & Keys.Shift) == Keys.Shift)

View File

@ -992,14 +992,7 @@ namespace BizHawk.Client.EmuHawk
TasView.SelectRow(i, _selectionDragState);
if (AxisEditingMode && (ModifierKeys == Keys.Control || ModifierKeys == Keys.Shift))
{
if (_selectionDragState)
{
_extraAxisRows.Add(i);
}
else
{
_extraAxisRows.Remove(i);
}
_extraAxisRows.SetMembership(i, shouldBeMember: _selectionDragState);
}
}

View File

@ -377,6 +377,30 @@ namespace BizHawk.Common.CollectionExtensions
=> source.OrderByDescending(ReturnSelf);
#endif
/// <returns><see langword="true"/> iff any removed</returns>
public static bool RemoveAll<T>(this ICollection<T> collection, T item)
{
if (collection is ISet<T>) return collection.Remove(item);
#if false // probably not worth it, would need to benchmark
if (collection is IList<T> list)
{
// remove from end
var i = list.LastIndexOf(item);
if (i < 0) return false;
do
{
list.RemoveAt(i);
i = list.LastIndexOf(item);
}
while (i < 0);
return true;
}
#endif
if (!collection.Remove(item)) return false;
while (collection.Remove(item)) {/*noop*/}
return true;
}
/// <inheritdoc cref="List{T}.RemoveAll"/>
/// <remarks>
/// (This is an extension method which reimplements <see cref="List{T}.RemoveAll"/> for other <see cref="ICollection{T}">collections</see>.
@ -423,6 +447,20 @@ namespace BizHawk.Common.CollectionExtensions
return true;
}
/// <summary>
/// if <paramref name="shouldBeMember"/> is <see langword="false"/>,
/// removes every copy of <paramref name="item"/> from <paramref name="collection"/>;
/// else if <paramref name="shouldBeMember"/> is <see langword="true"/>
/// and <paramref name="item"/> is not present in <paramref name="collection"/>, appends one copy;
/// else no-op (does not limit to one copy)
/// </summary>
public static void SetMembership<T>(this ICollection<T> collection, T item, bool shouldBeMember)
{
if (!shouldBeMember) _ = collection.RemoveAll(item);
else if (!collection.Contains(item)) collection.Add(item);
// else noop
}
public static ReadOnlySpan<T> Slice<T>(this ReadOnlySpan<T> span, Range range)
{
var (offset, length) = range.GetOffsetAndLength(span.Length);
@ -447,6 +485,18 @@ namespace BizHawk.Common.CollectionExtensions
=> list.ToDictionary(static kvp => kvp.Key, static kvp => kvp.Value);
#endif
/// <summary>
/// if <paramref name="collection"/> contains <paramref name="item"/>, removes every copy;
/// otherwise appends one copy
/// </summary>
/// <returns>new membership state (<see langword="true"/> iff added)</returns>
public static bool ToggleMembership<T>(this ICollection<T> collection, T item)
{
var removed = collection.RemoveAll(item);
if (!removed) collection.Add(item);
return removed;
}
public static bool IsSortedAsc<T>(this IReadOnlyList<T> list)
where T : IComparable<T>
{

View File

@ -2,6 +2,9 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using BizHawk.Common.CollectionExtensions;
using NymaTypes;
using static BizHawk.Emulation.Cores.Waterbox.NymaCore;
using static BizHawk.Emulation.Cores.Waterbox.NymaCore.NymaSettingsInfo;
@ -418,12 +421,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
}
public override void SetValue(object component, object value)
{
if ((bool)value)
((NymaSettings)component).DisabledLayers.Remove(LayerName);
else
((NymaSettings)component).DisabledLayers.Add(LayerName);
}
=> ((NymaSettings) component).DisabledLayers.SetMembership(LayerName, shouldBeMember: !((bool) value));
public override bool ShouldSerializeValue(object component)
{