1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-08 09:27:32 +08:00
osu-lazer/osu.Game/Screens/Play/KeyCounter.cs
tsrk 74a58fb674
refactor: separate things in KeyCounter
To implement different different sources of input for KeyCounter, it
is now possible to create a Trigger class (to inherit) instead of
inheriting KeyCounter. This eases the creation of more input sources
(like for tests) while allowing to implement different UI variants.

That way, if another variant of the key counter needs to implemented
(for whathever reason), this can be done by only inheriting KeyCounter
and changing how things are arranged visually.
2023-02-13 01:24:27 +00:00

101 lines
2.4 KiB
C#

// 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 osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
namespace osu.Game.Screens.Play
{
public abstract partial class KeyCounter : Container
{
public readonly Trigger CounterTrigger;
protected Bindable<bool> IsCountingBindable = new BindableBool(true);
protected Bindable<int> PressesCount = new BindableInt
{
MinValue = 0
};
public bool IsCounting
{
get => IsCountingBindable.Value;
set => IsCountingBindable.Value = value;
}
public int CountPresses
{
get => PressesCount.Value;
private set => PressesCount.Value = value;
}
protected Bindable<bool> IsLit = new BindableBool();
public void Increment()
{
if (!IsCounting)
return;
CountPresses++;
}
public void Decrement()
{
if (!IsCounting)
return;
CountPresses--;
}
protected override void LoadComplete()
{
Add(CounterTrigger);
base.LoadComplete();
}
protected override bool Handle(UIEvent e) => CounterTrigger.TriggerEvent(e);
protected KeyCounter(Trigger trigger)
{
CounterTrigger = trigger;
trigger.Target = this;
Name = trigger.Name;
}
public abstract partial class Trigger : Component
{
private KeyCounter? target;
public KeyCounter Target
{
set => target = value;
}
protected Trigger(string name)
{
Name = name;
}
protected void Lit(bool increment = true)
{
if (target == null) return;
target.IsLit.Value = true;
if (increment)
target.Increment();
}
protected void Unlit(bool preserve = true)
{
if (target == null) return;
target.IsLit.Value = false;
if (!preserve)
target.Decrement();
}
}
}
}