// Copyright (c) ppy Pty Ltd . 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; namespace osu.Game.Screens.Play.HUD { /// /// An individual key display which is intended to be displayed within a . /// public abstract partial class KeyCounter : Container { /// /// The which activates and deactivates this . /// public readonly InputTrigger Trigger; /// /// Whether the actions reported by should be counted. /// public Bindable IsCounting { get; } = new BindableBool(true); private readonly Bindable countPresses = new BindableInt { MinValue = 0 }; /// /// The current count of registered key presses. /// public IBindable CountPresses => countPresses; private readonly Container content; protected override Container Content => content; /// /// Whether this is currently in the "activated" state because the associated key is currently pressed. /// protected readonly Bindable IsActive = new BindableBool(); protected KeyCounter(InputTrigger trigger) { InternalChildren = new Drawable[] { content = new Container { RelativeSizeAxes = Axes.Both }, Trigger = trigger, }; Trigger.OnActivate += Activate; Trigger.OnDeactivate += Deactivate; } private void increment() { if (!IsCounting.Value) return; countPresses.Value++; } private void decrement() { if (!IsCounting.Value) return; countPresses.Value--; } protected virtual void Activate(bool forwardPlayback = true) { IsActive.Value = true; if (forwardPlayback) increment(); } protected virtual void Deactivate(bool forwardPlayback = true) { IsActive.Value = false; if (!forwardPlayback) decrement(); } protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); Trigger.OnActivate -= Activate; Trigger.OnDeactivate -= Deactivate; } } }