1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 17:37:24 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/KeyCounterDisplay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
4.0 KiB
C#
Raw Normal View History

// 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
using System;
using System.Collections.Generic;
using System.Linq;
2018-07-22 22:16:17 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
2016-10-22 18:37:27 +08:00
using osu.Framework.Graphics;
2017-01-27 20:57:22 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Game.Configuration;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Play.HUD
{
/// <summary>
/// A flowing display of all gameplay keys. Individual keys can be added using <see cref="InputTrigger"/> implementations.
/// </summary>
public abstract partial class KeyCounterDisplay : CompositeDrawable
{
2020-02-04 01:00:43 +08:00
/// <summary>
/// Whether the key counter should be visible regardless of the configuration value.
/// This is true by default, but can be changed.
/// </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; }
2023-03-08 08:58:54 +08:00
/// <summary>
/// Whether the actions reported by all <see cref="InputTrigger"/>s within this <see cref="KeyCounterDisplay"/> should be counted.
/// </summary>
public Bindable<bool> IsCounting { get; } = new BindableBool(true);
protected readonly Bindable<bool> ConfigVisibility = new Bindable<bool>();
protected abstract void UpdateVisibility();
private Receptor? receptor;
public void SetReceptor(Receptor receptor)
{
if (this.receptor != null)
throw new InvalidOperationException("Cannot set a new receptor when one is already active");
this.receptor = receptor;
}
2023-03-08 08:58:54 +08:00
/// <summary>
/// Add a <see cref="InputTrigger"/> to this display.
2023-03-08 08:58:54 +08:00
/// </summary>
public void Add(InputTrigger trigger)
{
var keyCounter = CreateCounter(trigger);
KeyFlow.Add(keyCounter);
IsCounting.BindTo(keyCounter.IsCounting);
}
2023-03-08 08:58:54 +08:00
/// <summary>
/// Add a range of <see cref="InputTrigger"/> to this display.
2023-03-08 08:58:54 +08:00
/// </summary>
public void AddRange(IEnumerable<InputTrigger> triggers) => triggers.ForEach(Add);
protected abstract KeyCounter CreateCounter(InputTrigger trigger);
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
config.BindWith(OsuSetting.KeyOverlay, ConfigVisibility);
}
protected override void LoadComplete()
{
base.LoadComplete();
AlwaysVisible.BindValueChanged(_ => UpdateVisibility());
ConfigVisibility.BindValueChanged(_ => UpdateVisibility(), true);
}
2018-04-13 17:19:50 +08:00
public override bool HandleNonPositionalInput => receptor == null;
2018-04-13 17:19:50 +08:00
public override bool HandlePositionalInput => receptor == null;
public partial class Receptor : Drawable
{
protected readonly KeyCounterDisplay Target;
2018-04-13 17:19:50 +08:00
public Receptor(KeyCounterDisplay target)
{
RelativeSizeAxes = Axes.Both;
Depth = float.MinValue;
Target = target;
}
2018-04-13 17:19:50 +08:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
2018-04-13 17:19:50 +08:00
protected override bool Handle(UIEvent e)
{
switch (e)
{
2022-06-24 20:25:23 +08:00
case KeyDownEvent:
case KeyUpEvent:
case MouseDownEvent:
case MouseUpEvent:
2023-03-07 15:21:57 +08:00
return Target.InternalChildren.Any(c => c.TriggerEvent(e));
}
2019-02-28 12:31:40 +08:00
return base.Handle(e);
}
}
}
}