1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 20:47:28 +08:00

refactor: separate impl of KeyCounterDisplay

This allows for different layouts of display. Idk, maybe someone would
want to mix both variants? (don't do this please). This commit is mostly
prep for further changes.
This commit is contained in:
tsrk 2023-02-13 01:33:09 +00:00
parent 74a58fb674
commit 11d0e185b8
No known key found for this signature in database
GPG Key ID: EBD46BB3049B56D6
8 changed files with 121 additions and 88 deletions

View File

@ -34,9 +34,9 @@ namespace osu.Game.Rulesets.Osu.Tests
[Resolved]
private OsuConfigManager config { get; set; } = null!;
private TestActionKeyCounter leftKeyCounter = null!;
private DefaultKeyCounter leftKeyCounter = null!;
private TestActionKeyCounter rightKeyCounter = null!;
private DefaultKeyCounter rightKeyCounter = null!;
private OsuInputManager osuInputManager = null!;
@ -59,14 +59,14 @@ namespace osu.Game.Rulesets.Osu.Tests
Origin = Anchor.Centre,
Children = new Drawable[]
{
leftKeyCounter = new TestActionKeyCounter(OsuAction.LeftButton)
leftKeyCounter = new DefaultKeyCounter(new TestActionKeyCounter(OsuAction.LeftButton))
{
Anchor = Anchor.Centre,
Origin = Anchor.CentreRight,
Depth = float.MinValue,
X = -100,
},
rightKeyCounter = new TestActionKeyCounter(OsuAction.RightButton)
rightKeyCounter = new DefaultKeyCounter(new TestActionKeyCounter(OsuAction.RightButton))
{
Anchor = Anchor.Centre,
Origin = Anchor.CentreLeft,
@ -579,7 +579,7 @@ namespace osu.Game.Rulesets.Osu.Tests
private void checkNotPressed(OsuAction action) => AddAssert($"Not pressing {action}", () => !osuInputManager.PressedActions.Contains(action));
private void checkPressed(OsuAction action) => AddAssert($"Is pressing {action}", () => osuInputManager.PressedActions.Contains(action));
public partial class TestActionKeyCounter : KeyCounter, IKeyBindingHandler<OsuAction>
public partial class TestActionKeyCounter : KeyCounter.Trigger, IKeyBindingHandler<OsuAction>
{
public OsuAction Action { get; }
@ -593,8 +593,7 @@ namespace osu.Game.Rulesets.Osu.Tests
{
if (e.Action == Action)
{
IsLit = true;
Increment();
Lit();
}
return false;
@ -602,7 +601,8 @@ namespace osu.Game.Rulesets.Osu.Tests
public void OnReleased(KeyBindingReleaseEvent<OsuAction> e)
{
if (e.Action == Action) IsLit = false;
if (e.Action == Action)
Unlit();
}
}

View File

@ -267,7 +267,7 @@ namespace osu.Game.Tests.Visual.Gameplay
hudOverlay = new HUDOverlay(null, Array.Empty<Mod>());
// Add any key just to display the key counter visually.
hudOverlay.KeyCounter.Add(new KeyCounterKeyboard(Key.Space));
hudOverlay.KeyCounter.Add(hudOverlay.KeyCounter.CreateKeyCounter(new KeyCounterKeyboard(Key.Space)));
scoreProcessor.Combo.Value = 1;

View File

@ -17,28 +17,28 @@ namespace osu.Game.Tests.Visual.Gameplay
{
public TestSceneKeyCounter()
{
KeyCounterKeyboard testCounter;
DefaultKeyCounter testCounter;
KeyCounterDisplay kc = new KeyCounterDisplay
KeyCounterDisplay kc = new DefaultKeyCounterDisplay
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Children = new KeyCounter[]
Children = new[]
{
testCounter = new KeyCounterKeyboard(Key.X),
new KeyCounterKeyboard(Key.X),
new KeyCounterMouse(MouseButton.Left),
new KeyCounterMouse(MouseButton.Right),
testCounter = new DefaultKeyCounter(new KeyCounterKeyboard(Key.X)),
new DefaultKeyCounter(new KeyCounterKeyboard(Key.X)),
new DefaultKeyCounter(new KeyCounterMouse(MouseButton.Left)),
new DefaultKeyCounter(new KeyCounterMouse(MouseButton.Right)),
},
};
AddStep("Add random", () =>
{
Key key = (Key)((int)Key.A + RNG.Next(26));
kc.Add(new KeyCounterKeyboard(key));
kc.Add(kc.CreateKeyCounter(new KeyCounterKeyboard(key)));
});
Key testKey = ((KeyCounterKeyboard)kc.Children.First()).Key;
Key testKey = ((KeyCounterKeyboard)kc.Children.First().CounterTrigger).Key;
void addPressKeyStep()
{

View File

@ -53,7 +53,7 @@ namespace osu.Game.Tests.Visual.Gameplay
};
// Add any key just to display the key counter visually.
hudOverlay.KeyCounter.Add(new KeyCounterKeyboard(Key.Space));
hudOverlay.KeyCounter.Add(hudOverlay.KeyCounter.CreateKeyCounter(new KeyCounterKeyboard(Key.Space)));
scoreProcessor.Combo.Value = 1;
return new Container

View File

@ -88,7 +88,7 @@ namespace osu.Game.Tests.Visual.Gameplay
hudOverlay = new HUDOverlay(null, Array.Empty<Mod>());
// Add any key just to display the key counter visually.
hudOverlay.KeyCounter.Add(new KeyCounterKeyboard(Key.Space));
hudOverlay.KeyCounter.Add(hudOverlay.KeyCounter.CreateKeyCounter(new KeyCounterKeyboard(Key.Space)));
action?.Invoke(hudOverlay);

View File

@ -0,0 +1,91 @@
// 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.
using System;
using System.Collections.Generic;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osuTK.Graphics;
namespace osu.Game.Screens.Play
{
public partial class DefaultKeyCounterDisplay : KeyCounterDisplay
{
private const int duration = 100;
private const double key_fade_time = 80;
protected override Container<KeyCounter> Content => KeyFlow;
public new IReadOnlyList<DefaultKeyCounter> Children
{
get => (IReadOnlyList<DefaultKeyCounter>)base.Children;
set => base.Children = value;
}
public DefaultKeyCounterDisplay()
{
InternalChild = KeyFlow = new FillFlowContainer<KeyCounter>
{
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Both,
Alpha = 0,
};
}
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;
}
public override void Add(KeyCounter key)
{
base.Add(key);
if (key is not DefaultKeyCounter defaultKey)
throw new ArgumentException($"{key.GetType()} is not a supported {nameof(KeyCounter)}.", nameof(key));
defaultKey.FadeTime = key_fade_time;
defaultKey.KeyDownTextColor = KeyDownTextColor;
defaultKey.KeyUpTextColor = KeyUpTextColor;
}
protected override void UpdateVisibility() =>
// Isolate changing visibility of the key counters from fading this component.
KeyFlow.FadeTo(AlwaysVisible.Value || ConfigVisibility.Value ? 1 : 0, duration);
private Color4 keyDownTextColor = Color4.DarkGray;
public Color4 KeyDownTextColor
{
get => keyDownTextColor;
set
{
if (value != keyDownTextColor)
{
keyDownTextColor = value;
foreach (var child in Children)
child.KeyDownTextColor = value;
}
}
}
private Color4 keyUpTextColor = Color4.White;
public Color4 KeyUpTextColor
{
get => keyUpTextColor;
set
{
if (value != keyUpTextColor)
{
keyUpTextColor = value;
foreach (var child in Children)
child.KeyUpTextColor = value;
}
}
}
}
}

View File

@ -327,7 +327,7 @@ namespace osu.Game.Screens.Play
ShowHealth = { BindTarget = ShowHealthBar }
};
protected KeyCounterDisplay CreateKeyCounter() => new KeyCounterDisplay
protected KeyCounterDisplay CreateKeyCounter() => new DefaultKeyCounterDisplay
{
Anchor = Anchor.BottomRight,
Origin = Anchor.BottomRight,

View File

@ -12,18 +12,14 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Game.Configuration;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Play
{
public partial class KeyCounterDisplay : Container<KeyCounter>
public abstract partial class KeyCounterDisplay : Container<KeyCounter>
{
private const int duration = 100;
private const double key_fade_time = 80;
protected readonly Bindable<bool> ConfigVisibility = new Bindable<bool>();
private readonly Bindable<bool> configVisibility = new Bindable<bool>();
protected readonly FillFlowContainer<KeyCounter> KeyFlow;
protected FillFlowContainer<KeyCounter> KeyFlow;
protected override Container<KeyCounter> Content => KeyFlow;
@ -33,48 +29,26 @@ namespace osu.Game.Screens.Play
/// </summary>
public readonly Bindable<bool> AlwaysVisible = new Bindable<bool>(true);
public KeyCounterDisplay()
{
InternalChild = KeyFlow = new FillFlowContainer<KeyCounter>
{
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Both,
Alpha = 0,
};
}
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;
}
public override void Add(KeyCounter key)
{
ArgumentNullException.ThrowIfNull(key);
base.Add(key);
key.IsCounting = IsCounting;
key.FadeTime = key_fade_time;
key.KeyDownTextColor = KeyDownTextColor;
key.KeyUpTextColor = KeyUpTextColor;
}
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
config.BindWith(OsuSetting.KeyOverlay, configVisibility);
config.BindWith(OsuSetting.KeyOverlay, ConfigVisibility);
}
protected override void LoadComplete()
{
base.LoadComplete();
AlwaysVisible.BindValueChanged(_ => updateVisibility());
configVisibility.BindValueChanged(_ => updateVisibility(), true);
AlwaysVisible.BindValueChanged(_ => UpdateVisibility());
ConfigVisibility.BindValueChanged(_ => UpdateVisibility(), true);
}
private bool isCounting = true;
@ -92,41 +66,7 @@ namespace osu.Game.Screens.Play
}
}
private Color4 keyDownTextColor = Color4.DarkGray;
public Color4 KeyDownTextColor
{
get => keyDownTextColor;
set
{
if (value != keyDownTextColor)
{
keyDownTextColor = value;
foreach (var child in Children)
child.KeyDownTextColor = value;
}
}
}
private Color4 keyUpTextColor = Color4.White;
public Color4 KeyUpTextColor
{
get => keyUpTextColor;
set
{
if (value != keyUpTextColor)
{
keyUpTextColor = value;
foreach (var child in Children)
child.KeyUpTextColor = value;
}
}
}
private void updateVisibility() =>
// Isolate changing visibility of the key counters from fading this component.
KeyFlow.FadeTo(AlwaysVisible.Value || configVisibility.Value ? 1 : 0, duration);
protected abstract void UpdateVisibility();
public override bool HandleNonPositionalInput => receptor == null;
public override bool HandlePositionalInput => receptor == null;
@ -141,6 +81,8 @@ namespace osu.Game.Screens.Play
this.receptor = receptor;
}
public virtual KeyCounter CreateKeyCounter(KeyCounter.Trigger trigger) => new DefaultKeyCounter(trigger);
public partial class Receptor : Drawable
{
protected readonly KeyCounterDisplay Target;