2019-01-24 16:43:03 +08:00
|
|
|
|
// 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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2023-02-13 09:24:27 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2016-09-24 09:53:58 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2016-09-23 18:29:25 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2023-03-07 15:28:54 +08:00
|
|
|
|
namespace osu.Game.Screens.Play.HUD
|
2016-09-23 18:29:25 +08:00
|
|
|
|
{
|
2023-03-07 15:31:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// An individual key display which is intended to be displayed within a <see cref="KeyCounterDisplay"/>.
|
|
|
|
|
/// </summary>
|
2016-09-24 10:19:50 +08:00
|
|
|
|
public abstract partial class KeyCounter : Container
|
2016-09-23 18:29:25 +08:00
|
|
|
|
{
|
2023-03-08 04:28:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The <see cref="InputTrigger"/> which activates and deactivates this <see cref="KeyCounter"/>.
|
|
|
|
|
/// </summary>
|
2023-02-14 05:59:17 +08:00
|
|
|
|
public readonly InputTrigger Trigger;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2023-03-08 04:28:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the actions reported by <see cref="Trigger"/> should be counted.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Bindable<bool> IsCounting { get; } = new BindableBool(true);
|
2023-02-13 18:59:10 +08:00
|
|
|
|
|
2023-02-17 06:20:34 +08:00
|
|
|
|
private readonly Bindable<int> countPresses = new BindableInt
|
2016-09-26 14:07:43 +08:00
|
|
|
|
{
|
2023-02-13 09:24:27 +08:00
|
|
|
|
MinValue = 0
|
|
|
|
|
};
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2023-03-08 04:28:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The current count of registered key presses.
|
|
|
|
|
/// </summary>
|
2023-02-17 06:20:34 +08:00
|
|
|
|
public IBindable<int> CountPresses => countPresses;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2023-03-08 04:28:42 +08:00
|
|
|
|
private readonly Container content;
|
|
|
|
|
|
|
|
|
|
protected override Container<Drawable> Content => content;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether this <see cref="KeyCounter"/> is currently in the "activated" state because the associated key is currently pressed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected readonly Bindable<bool> IsActive = new BindableBool();
|
|
|
|
|
|
2023-02-14 05:59:17 +08:00
|
|
|
|
protected KeyCounter(InputTrigger trigger)
|
2023-02-13 18:59:10 +08:00
|
|
|
|
{
|
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
content = new Container
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
|
},
|
2023-02-14 05:59:17 +08:00
|
|
|
|
Trigger = trigger,
|
2023-02-13 18:59:10 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-02-17 07:15:03 +08:00
|
|
|
|
Trigger.OnActivate += Activate;
|
|
|
|
|
Trigger.OnDeactivate += Deactivate;
|
2023-02-13 18:59:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 03:10:37 +08:00
|
|
|
|
private void increment()
|
2019-09-12 14:41:53 +08:00
|
|
|
|
{
|
2023-02-22 22:58:27 +08:00
|
|
|
|
if (!IsCounting.Value)
|
2019-09-12 14:41:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2023-02-17 06:20:34 +08:00
|
|
|
|
countPresses.Value++;
|
2019-09-12 14:41:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 03:10:37 +08:00
|
|
|
|
private void decrement()
|
2019-09-12 14:41:53 +08:00
|
|
|
|
{
|
2023-02-22 22:58:27 +08:00
|
|
|
|
if (!IsCounting.Value)
|
2019-09-12 14:41:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2023-02-17 06:20:34 +08:00
|
|
|
|
countPresses.Value--;
|
2019-09-12 14:41:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 03:02:56 +08:00
|
|
|
|
protected virtual void Activate(bool forwardPlayback = true)
|
2016-09-24 09:53:58 +08:00
|
|
|
|
{
|
2023-02-17 08:40:01 +08:00
|
|
|
|
IsActive.Value = true;
|
2023-02-22 03:02:56 +08:00
|
|
|
|
if (forwardPlayback)
|
2023-02-22 03:10:37 +08:00
|
|
|
|
increment();
|
2023-02-17 05:59:39 +08:00
|
|
|
|
}
|
2023-02-13 09:24:27 +08:00
|
|
|
|
|
2023-02-22 03:02:56 +08:00
|
|
|
|
protected virtual void Deactivate(bool forwardPlayback = true)
|
2023-02-17 05:59:39 +08:00
|
|
|
|
{
|
2023-02-17 08:40:01 +08:00
|
|
|
|
IsActive.Value = false;
|
2023-02-22 03:02:56 +08:00
|
|
|
|
if (!forwardPlayback)
|
2023-02-22 03:10:37 +08:00
|
|
|
|
decrement();
|
2023-02-17 05:59:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
2023-02-17 07:15:03 +08:00
|
|
|
|
Trigger.OnActivate -= Activate;
|
|
|
|
|
Trigger.OnDeactivate -= Deactivate;
|
2023-02-17 05:59:39 +08:00
|
|
|
|
}
|
2016-09-23 18:29:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|