1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 23:13:20 +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 KeyCounterMouseTrigger(MouseButton.Left),
new KeyCounterMouseTrigger(MouseButton.Right),
});
};
AddRange(inputTriggers);
controller.AddRange(inputTriggers);
AddStep("Add random", () =>
{

View File

@ -162,25 +162,22 @@ namespace osu.Game.Rulesets.UI
public void Attach(InputCountController inputCountController)
{
KeyBindingContainer.Add(inputCountController);
var triggers = KeyBindingContainer.DefaultKeyBindings
.Select(b => b.GetAction<T>())
.Distinct()
.OrderBy(action => action)
.Select(action => new KeyCounterActionTrigger<T>(action))
.ToArray();
inputCountController.AddRange(KeyBindingContainer.DefaultKeyBindings
.Select(b => b.GetAction<T>())
.Distinct()
.OrderBy(action => action)
.Select(action => new KeyCounterActionTrigger<T>(action)));
KeyBindingContainer.AddRange(triggers);
inputCountController.AddRange(triggers);
}
#endregion
#region Keys per second Counter Attachment
public void Attach(ClicksPerSecondCalculator calculator)
{
var listener = new ActionListener(calculator);
KeyBindingContainer.Add(listener);
}
public void Attach(ClicksPerSecondCalculator calculator) => KeyBindingContainer.Add(new ActionListener(calculator));
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.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
@ -17,26 +16,20 @@ namespace osu.Game.Screens.Play.HUD
{
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 InputCountController()
{
InternalChild = triggers = new Container<InputTrigger>();
}
public void AddRange(IEnumerable<InputTrigger> triggers) => triggers.ForEach(Add);
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);
trigger.IsCounting.BindTo(IsCounting);
OnNewTrigger?.Invoke(trigger);
}
public void AddRange(IEnumerable<InputTrigger> inputTriggers) => inputTriggers.ForEach(Add);
public override bool HandleNonPositionalInput => 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.
// 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.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics.Containers;
using osu.Game.Configuration;
using osu.Game.Rulesets.UI;
@ -24,35 +22,17 @@ namespace osu.Game.Screens.Play.HUD
/// </summary>
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 readonly Bindable<bool> ConfigVisibility = new Bindable<bool>();
private readonly IBindableList<InputTrigger> triggers = new BindableList<InputTrigger>();
[Resolved]
private InputCountController controller { get; set; } = null!;
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);
[BackgroundDependencyLoader]
@ -68,19 +48,18 @@ namespace osu.Game.Screens.Play.HUD
{
base.LoadComplete();
controller.OnNewTrigger += Add;
AddRange(controller.Triggers);
triggers.BindTo(controller.Triggers);
triggers.BindCollectionChanged(triggersChanged, true);
AlwaysVisible.BindValueChanged(_ => UpdateVisibility());
ConfigVisibility.BindValueChanged(_ => UpdateVisibility(), true);
}
protected override void Dispose(bool isDisposing)
private void triggersChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
base.Dispose(isDisposing);
if (controller.IsNotNull())
controller.OnNewTrigger -= Add;
KeyFlow.Clear();
foreach (var trigger in controller.Triggers)
KeyFlow.Add(CreateCounter(trigger));
}
public bool UsesFixedAnchor { get; set; }

View File

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