1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 11:42:55 +08:00

Refactor InputCountController to not require being added to foreign body via Attach

I've made the flow match `ClicksPerSecondCalculator` as close as
possible. Hopefully this reads better.
This commit is contained in:
Dean Herbert 2023-06-27 16:35:59 +09:00
parent e99de0eb5d
commit e21583ff1b
5 changed files with 29 additions and 59 deletions

View File

@ -48,13 +48,16 @@ namespace osu.Game.Tests.Visual.Gameplay
} }
}; };
controller.AddRange(new InputTrigger[] var inputTriggers = new InputTrigger[]
{ {
new KeyCounterKeyboardTrigger(Key.X), new KeyCounterKeyboardTrigger(Key.X),
new KeyCounterKeyboardTrigger(Key.X), new KeyCounterKeyboardTrigger(Key.X),
new KeyCounterMouseTrigger(MouseButton.Left), new KeyCounterMouseTrigger(MouseButton.Left),
new KeyCounterMouseTrigger(MouseButton.Right), new KeyCounterMouseTrigger(MouseButton.Right),
}); };
AddRange(inputTriggers);
controller.AddRange(inputTriggers);
AddStep("Add random", () => AddStep("Add random", () =>
{ {

View File

@ -162,25 +162,22 @@ namespace osu.Game.Rulesets.UI
public void Attach(InputCountController inputCountController) public void Attach(InputCountController inputCountController)
{ {
KeyBindingContainer.Add(inputCountController); var triggers = KeyBindingContainer.DefaultKeyBindings
inputCountController.AddRange(KeyBindingContainer.DefaultKeyBindings
.Select(b => b.GetAction<T>()) .Select(b => b.GetAction<T>())
.Distinct() .Distinct()
.OrderBy(action => action) .OrderBy(action => action)
.Select(action => new KeyCounterActionTrigger<T>(action))); .Select(action => new KeyCounterActionTrigger<T>(action))
.ToArray();
KeyBindingContainer.AddRange(triggers);
inputCountController.AddRange(triggers);
} }
#endregion #endregion
#region Keys per second Counter Attachment #region Keys per second Counter Attachment
public void Attach(ClicksPerSecondCalculator calculator) public void Attach(ClicksPerSecondCalculator calculator) => KeyBindingContainer.Add(new ActionListener(calculator));
{
var listener = new ActionListener(calculator);
KeyBindingContainer.Add(listener);
}
private partial class ActionListener : Component, IKeyBindingHandler<T> private partial class ActionListener : Component, IKeyBindingHandler<T>
{ {

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions; using osu.Framework.Extensions.IEnumerableExtensions;
@ -17,26 +16,20 @@ namespace osu.Game.Screens.Play.HUD
{ {
public readonly Bindable<bool> IsCounting = new BindableBool(true); public readonly Bindable<bool> IsCounting = new BindableBool(true);
public event Action<InputTrigger>? OnNewTrigger; private readonly BindableList<InputTrigger> triggers = new BindableList<InputTrigger>();
private readonly Container<InputTrigger> triggers; public IBindableList<InputTrigger> Triggers => triggers;
public IReadOnlyList<InputTrigger> Triggers => triggers; public void AddRange(IEnumerable<InputTrigger> triggers) => triggers.ForEach(Add);
public InputCountController()
{
InternalChild = triggers = new Container<InputTrigger>();
}
public void Add(InputTrigger trigger) public void Add(InputTrigger trigger)
{ {
// Note that these triggers are not added to the hierarchy here. It is presumed they are added externally at a
// more correct location (ie. inside a RulesetInputManager).
triggers.Add(trigger); triggers.Add(trigger);
trigger.IsCounting.BindTo(IsCounting); trigger.IsCounting.BindTo(IsCounting);
OnNewTrigger?.Invoke(trigger);
} }
public void AddRange(IEnumerable<InputTrigger> inputTriggers) => inputTriggers.ForEach(Add);
public override bool HandleNonPositionalInput => true; public override bool HandleNonPositionalInput => true;
public override bool HandlePositionalInput => true; public override bool HandlePositionalInput => true;
} }

View File

@ -1,11 +1,9 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic; using System.Collections.Specialized;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Rulesets.UI; using osu.Game.Rulesets.UI;
@ -24,35 +22,17 @@ namespace osu.Game.Screens.Play.HUD
/// </summary> /// </summary>
public Bindable<bool> AlwaysVisible { get; } = new Bindable<bool>(true); public Bindable<bool> AlwaysVisible { get; } = new Bindable<bool>(true);
/// <summary>
/// The <see cref="KeyCounter"/>s contained in this <see cref="KeyCounterDisplay"/>.
/// </summary>
public IEnumerable<KeyCounter> Counters => KeyFlow;
protected abstract FillFlowContainer<KeyCounter> KeyFlow { get; } protected abstract FillFlowContainer<KeyCounter> KeyFlow { get; }
protected readonly Bindable<bool> ConfigVisibility = new Bindable<bool>(); protected readonly Bindable<bool> ConfigVisibility = new Bindable<bool>();
private readonly IBindableList<InputTrigger> triggers = new BindableList<InputTrigger>();
[Resolved] [Resolved]
private InputCountController controller { get; set; } = null!; private InputCountController controller { get; set; } = null!;
protected abstract void UpdateVisibility(); protected abstract void UpdateVisibility();
/// <summary>
/// Add a <see cref="InputTrigger"/> to this display.
/// </summary>
public void Add(InputTrigger trigger)
{
var keyCounter = CreateCounter(trigger);
KeyFlow.Add(keyCounter);
}
/// <summary>
/// Add a range of <see cref="InputTrigger"/> to this display.
/// </summary>
public void AddRange(IEnumerable<InputTrigger> triggers) => triggers.ForEach(Add);
protected abstract KeyCounter CreateCounter(InputTrigger trigger); protected abstract KeyCounter CreateCounter(InputTrigger trigger);
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -68,19 +48,18 @@ namespace osu.Game.Screens.Play.HUD
{ {
base.LoadComplete(); base.LoadComplete();
controller.OnNewTrigger += Add; triggers.BindTo(controller.Triggers);
AddRange(controller.Triggers); triggers.BindCollectionChanged(triggersChanged, true);
AlwaysVisible.BindValueChanged(_ => UpdateVisibility()); AlwaysVisible.BindValueChanged(_ => UpdateVisibility());
ConfigVisibility.BindValueChanged(_ => UpdateVisibility(), true); ConfigVisibility.BindValueChanged(_ => UpdateVisibility(), true);
} }
protected override void Dispose(bool isDisposing) private void triggersChanged(object? sender, NotifyCollectionChangedEventArgs e)
{ {
base.Dispose(isDisposing); KeyFlow.Clear();
foreach (var trigger in controller.Triggers)
if (controller.IsNotNull()) KeyFlow.Add(CreateCounter(trigger));
controller.OnNewTrigger -= Add;
} }
public bool UsesFixedAnchor { get; set; } public bool UsesFixedAnchor { get; set; }

View File

@ -111,9 +111,6 @@ namespace osu.Game.Screens.Play
RelativeSizeAxes = Axes.Both; RelativeSizeAxes = Axes.Both;
// intentionally not added to hierarchy here as it will be attached via `BindDrawableRuleset()`.
InputCountController = new InputCountController();
Children = new[] Children = new[]
{ {
CreateFailingLayer(), CreateFailingLayer(),
@ -161,6 +158,7 @@ namespace osu.Game.Screens.Play
Spacing = new Vector2(5) Spacing = new Vector2(5)
}, },
clicksPerSecondCalculator = new ClicksPerSecondCalculator(), clicksPerSecondCalculator = new ClicksPerSecondCalculator(),
InputCountController = new InputCountController(),
}; };
hideTargets = new List<Drawable> { mainComponents, rulesetComponents, topRightElements }; hideTargets = new List<Drawable> { mainComponents, rulesetComponents, topRightElements };