1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 05:22:54 +08:00
osu-lazer/osu.Game/Screens/Play/KeyCounterDisplay.cs

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

172 lines
5.1 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
2022-06-17 15:37:17 +08:00
#nullable disable
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;
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;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
2017-01-27 20:57:22 +08:00
namespace osu.Game.Screens.Play
{
public class KeyCounterDisplay : Container<KeyCounter>
{
private const int duration = 100;
private const double key_fade_time = 80;
2018-04-13 17:19:50 +08:00
2020-02-04 00:59:58 +08:00
private readonly Bindable<bool> configVisibility = new Bindable<bool>();
2018-04-13 17:19:50 +08:00
protected readonly FillFlowContainer<KeyCounter> KeyFlow;
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);
public KeyCounterDisplay()
{
InternalChild = KeyFlow = new FillFlowContainer<KeyCounter>
{
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Both,
};
}
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
// Don't use autosize as it will shrink to zero when KeyFlow is hidden.
// In turn this can cause the display to be masked off screen and never become visible again.
Size = KeyFlow.Size;
}
2016-12-03 15:58:27 +08:00
public override void Add(KeyCounter key)
2016-10-06 22:33:09 +08:00
{
if (key == null) throw new ArgumentNullException(nameof(key));
2018-04-13 17:19:50 +08:00
2016-12-03 15:58:27 +08:00
base.Add(key);
key.IsCounting = IsCounting;
key.FadeTime = key_fade_time;
key.KeyDownTextColor = KeyDownTextColor;
key.KeyUpTextColor = KeyUpTextColor;
}
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
2020-02-04 00:59:58 +08:00
config.BindWith(OsuSetting.KeyOverlay, configVisibility);
}
protected override void LoadComplete()
{
base.LoadComplete();
2020-02-04 01:00:43 +08:00
AlwaysVisible.BindValueChanged(_ => updateVisibility());
2020-02-04 00:59:58 +08:00
configVisibility.BindValueChanged(_ => updateVisibility(), true);
}
2018-04-13 17:19:50 +08:00
private bool isCounting = true;
2019-02-28 12:31:40 +08:00
public bool IsCounting
{
get => isCounting;
set
{
if (value == isCounting) return;
2018-04-13 17:19:50 +08:00
isCounting = value;
foreach (var child in Children)
child.IsCounting = value;
}
}
2018-04-13 17:19:50 +08:00
private Color4 keyDownTextColor = Color4.DarkGray;
2019-02-28 12:31:40 +08:00
public Color4 KeyDownTextColor
{
get => keyDownTextColor;
set
{
if (value != keyDownTextColor)
{
keyDownTextColor = value;
2016-12-03 15:58:27 +08:00
foreach (var child in Children)
child.KeyDownTextColor = value;
}
}
}
2018-04-13 17:19:50 +08:00
private Color4 keyUpTextColor = Color4.White;
2019-02-28 12:31:40 +08:00
public Color4 KeyUpTextColor
{
get => keyUpTextColor;
set
{
if (value != keyUpTextColor)
{
keyUpTextColor = value;
2016-12-03 15:58:27 +08:00
foreach (var child in Children)
child.KeyUpTextColor = value;
}
}
}
2018-04-13 17:19:50 +08:00
private void updateVisibility() =>
// Isolate changing visibility of the key counters from fading this component.
2020-02-04 00:59:58 +08:00
KeyFlow.FadeTo(AlwaysVisible.Value || configVisibility.Value ? 1 : 0, duration);
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");
2018-04-13 17:19:50 +08:00
this.receptor = receptor;
}
2018-04-13 17:19:50 +08:00
public 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:
return Target.Children.Any(c => c.TriggerEvent(e));
}
2019-02-28 12:31:40 +08:00
return base.Handle(e);
}
}
}
}