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

116 lines
3.6 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.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;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
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
{
public abstract partial class KeyCounterDisplay : Container<KeyCounter>
2018-04-13 17:19:50 +08:00
{
protected readonly Bindable<bool> ConfigVisibility = new Bindable<bool>();
2018-04-13 17:19:50 +08:00
protected FillFlowContainer<KeyCounter> KeyFlow = new FillFlowContainer<KeyCounter>();
protected override Container<KeyCounter> Content => KeyFlow;
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 readonly Bindable<bool> AlwaysVisible = new Bindable<bool>(true);
2018-04-13 17:19:50 +08:00
public override void Add(KeyCounter key)
{
2023-02-16 06:06:35 +08:00
if (!CheckType(key))
throw new ArgumentException($"{key.GetType()} is not a supported {nameof(KeyCounter)}.", nameof(key));
2018-04-13 17:19:50 +08:00
base.Add(key);
key.IsCounting = IsCounting;
}
2023-02-16 06:06:35 +08:00
protected virtual bool CheckType(KeyCounter key) => true;
2018-04-13 17:19:50 +08:00
[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
}
private bool isCounting = true;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public bool IsCounting
{
get => isCounting;
2018-04-13 17:19:50 +08:00
set
{
if (value == isCounting) return;
isCounting = value;
foreach (var child in Children)
child.IsCounting = value;
}
}
protected abstract void UpdateVisibility();
public override bool HandleNonPositionalInput => receptor == null;
public override bool HandlePositionalInput => receptor == null;
2018-04-13 17:19:50 +08:00
private Receptor? receptor;
2018-04-13 17:19:50 +08:00
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;
}
public virtual KeyCounter CreateKeyCounter(KeyCounter.InputTrigger trigger) => new DefaultKeyCounter(trigger);
2022-11-24 13:32:20 +08:00
public partial class Receptor : Drawable
2018-04-13 17:19:50 +08:00
{
protected readonly KeyCounterDisplay Target;
2018-04-13 17:19:50 +08:00
public Receptor(KeyCounterDisplay target)
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both;
Depth = float.MinValue;
Target = target;
}
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:
return Target.Children.Any(c => c.TriggerEvent(e));
}
2019-02-28 12:31:40 +08:00
return base.Handle(e);
}
2018-04-13 17:19:50 +08:00
}
}
}