Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up KeyBindingRow and related classes #25092

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void TestGameplayKeyBindings()

private KeyBindingsSubsection osuBindingSubsection => keyBindingPanel
.ChildrenOfType<VariantBindingsSubsection>()
.FirstOrDefault(s => s.Ruleset.ShortName == "osu");
.FirstOrDefault(s => s.Ruleset!.ShortName == "osu");

private OsuButton configureBindingsButton => Game.Settings
.ChildrenOfType<BindingSettings>().SingleOrDefault()?
Expand Down
100 changes: 66 additions & 34 deletions osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects;
Expand Down Expand Up @@ -37,16 +37,19 @@ public partial class KeyBindingRow : Container, IFilterable
/// <summary>
/// Invoked when the binding of this row is updated with a change being written.
/// </summary>
public Action<KeyBindingRow> BindingUpdated { get; set; }

private readonly object action;
private readonly IEnumerable<RealmKeyBinding> bindings;
public Action<KeyBindingRow>? BindingUpdated { get; init; }

private const float transition_time = 150;
/// <summary>
/// Whether left and right mouse button clicks should be included in the edited bindings.
/// </summary>
public bool AllowMainMouseButtons { get; init; }

private const float height = 20;
/// <summary>
/// The default key bindings for this row.
/// </summary>
public IEnumerable<KeyCombination> Defaults { get; init; } = Array.Empty<KeyCombination>();

private const float padding = 5;
#region IFilterable

private bool matchingFilter;

Expand All @@ -60,24 +63,45 @@ public bool MatchingFilter
}
}

private Container content;
public bool FilteringActive { get; set; }

public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) =>
content.ReceivePositionalInputAt(screenSpacePos);
public IEnumerable<LocalisableString> FilterTerms => bindings.Select(b => (LocalisableString)keyCombinationProvider.GetReadableString(b.KeyCombination)).Prepend(text.Text);

public bool FilteringActive { get; set; }
#endregion

private readonly object action;
private readonly IEnumerable<RealmKeyBinding> bindings;

private Bindable<bool> isDefault { get; } = new BindableBool(true);

[Resolved]
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; }
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } = null!;

private OsuSpriteText text;
private FillFlowContainer cancelAndClearButtons;
private FillFlowContainer<KeyButton> buttons;
[Resolved]
private RealmAccess realm { get; set; } = null!;

private Bindable<bool> isDefault { get; } = new BindableBool(true);
private Container content = null!;

public IEnumerable<LocalisableString> FilterTerms => bindings.Select(b => (LocalisableString)keyCombinationProvider.GetReadableString(b.KeyCombination)).Prepend(text.Text);
private OsuSpriteText text = null!;
private FillFlowContainer cancelAndClearButtons = null!;
private FillFlowContainer<KeyButton> buttons = null!;

private KeyButton? bindTarget;

private const float transition_time = 150;
private const float height = 20;
private const float padding = 5;

public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) =>
content.ReceivePositionalInputAt(screenSpacePos);

public override bool AcceptsFocus => bindTarget == null;

/// <summary>
/// Creates a new <see cref="KeyBindingRow"/>.
/// </summary>
/// <param name="action">The action that this row contains bindings for.</param>
/// <param name="bindings">The keybindings to display in this row.</param>
public KeyBindingRow(object action, List<RealmKeyBinding> bindings)
{
this.action = action;
Expand All @@ -87,9 +111,6 @@ public KeyBindingRow(object action, List<RealmKeyBinding> bindings)
AutoSizeAxes = Axes.Y;
}

[Resolved]
private RealmAccess realm { get; set; }

[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
Expand Down Expand Up @@ -205,21 +226,18 @@ protected override void OnHoverLost(HoverLostEvent e)
base.OnHoverLost(e);
}

public override bool AcceptsFocus => bindTarget == null;

private KeyButton bindTarget;

public bool AllowMainMouseButtons;

public IEnumerable<KeyCombination> Defaults;

private bool isModifier(Key k) => k < Key.F1;

protected override bool OnClick(ClickEvent e) => true;

protected override bool OnMouseDown(MouseDownEvent e)
{
if (!HasFocus || !bindTarget.IsHovered)
if (!HasFocus)
return base.OnMouseDown(e);

Debug.Assert(bindTarget != null);

if (!bindTarget.IsHovered)
return base.OnMouseDown(e);

if (!AllowMainMouseButtons)
Expand All @@ -245,6 +263,8 @@ protected override void OnMouseUp(MouseUpEvent e)
return;
}

Debug.Assert(bindTarget != null);

if (bindTarget.IsHovered)
finalise(false);
// prevent updating bind target before clear button's action
Expand All @@ -256,6 +276,8 @@ protected override bool OnScroll(ScrollEvent e)
{
if (HasFocus)
{
Debug.Assert(bindTarget != null);

if (bindTarget.IsHovered)
{
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState, e.ScrollDelta), KeyCombination.FromScrollDelta(e.ScrollDelta).First());
Expand All @@ -272,6 +294,8 @@ protected override bool OnKeyDown(KeyDownEvent e)
if (!HasFocus || e.Repeat)
return false;

Debug.Assert(bindTarget != null);

bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromKey(e.Key));
if (!isModifier(e.Key)) finalise();

Expand All @@ -294,6 +318,8 @@ protected override bool OnJoystickPress(JoystickPressEvent e)
if (!HasFocus)
return false;

Debug.Assert(bindTarget != null);

bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromJoystickButton(e.Button));
finalise();

Expand All @@ -316,6 +342,8 @@ protected override bool OnMidiDown(MidiDownEvent e)
if (!HasFocus)
return false;

Debug.Assert(bindTarget != null);

bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromMidiKey(e.Key));
finalise();

Expand All @@ -338,6 +366,8 @@ protected override bool OnTabletAuxiliaryButtonPress(TabletAuxiliaryButtonPressE
if (!HasFocus)
return false;

Debug.Assert(bindTarget != null);

bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromTabletAuxiliaryButton(e.Button));
finalise();

Expand All @@ -360,6 +390,8 @@ protected override bool OnTabletPenButtonPress(TabletPenButtonPressEvent e)
if (!HasFocus)
return false;

Debug.Assert(bindTarget != null);

bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromTabletPenButton(e.Button));
finalise();

Expand Down Expand Up @@ -473,10 +505,10 @@ public partial class KeyButton : Container
public readonly OsuSpriteText Text;

[Resolved]
private OverlayColourProvider colourProvider { get; set; }
private OverlayColourProvider colourProvider { get; set; } = null!;

[Resolved]
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; }
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } = null!;

private bool isBinding;

Expand Down Expand Up @@ -599,7 +631,7 @@ protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);

if (keyCombinationProvider != null)
if (keyCombinationProvider.IsNotNull())
keyCombinationProvider.KeymapChanged -= updateKeyCombinationText;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Input.Bindings;
using osu.Framework.Localisation;
using osu.Game.Database;
using osu.Game.Input.Bindings;
Expand All @@ -25,9 +25,9 @@ public abstract partial class KeyBindingsSubsection : SettingsSubsection
/// </summary>
protected virtual bool AutoAdvanceTarget => false;

protected IEnumerable<Framework.Input.Bindings.KeyBinding> Defaults;
protected IEnumerable<KeyBinding> Defaults { get; init; } = Array.Empty<KeyBinding>();

public RulesetInfo Ruleset { get; protected set; }
public RulesetInfo? Ruleset { get; protected set; }

private readonly int? variant;

Expand All @@ -41,7 +41,7 @@ protected KeyBindingsSubsection(int? variant)
[BackgroundDependencyLoader]
private void load(RealmAccess realm)
{
string rulesetName = Ruleset?.ShortName;
string? rulesetName = Ruleset?.ShortName;

var bindings = realm.Run(r => r.All<RealmKeyBinding>()
.Where(b => b.RulesetName == rulesetName && b.Variant == variant)
Expand Down