refactor: separate things in KeyCounter

To implement different different sources of input for KeyCounter, it
is now possible to create a Trigger class (to inherit) instead of
inheriting KeyCounter. This eases the creation of more input sources
(like for tests) while allowing to implement different UI variants.

That way, if another variant of the key counter needs to implemented
(for whathever reason), this can be done by only inheriting KeyCounter
and changing how things are arranged visually.
This commit is contained in:
tsrk 2023-02-13 01:24:27 +00:00
parent 8fb72b971d
commit 74a58fb674
No known key found for this signature in database
GPG Key ID: EBD46BB3049B56D6
6 changed files with 177 additions and 126 deletions

View File

@ -166,7 +166,7 @@ namespace osu.Game.Rulesets.UI
.Select(b => b.GetAction<T>()) .Select(b => b.GetAction<T>())
.Distinct() .Distinct()
.OrderBy(action => action) .OrderBy(action => action)
.Select(action => new KeyCounterAction<T>(action))); .Select(action => keyCounter.CreateKeyCounter(new KeyCounterAction<T>(action))));
} }
private partial class ActionReceptor : KeyCounterDisplay.Receptor, IKeyBindingHandler<T> private partial class ActionReceptor : KeyCounterDisplay.Receptor, IKeyBindingHandler<T>
@ -176,11 +176,14 @@ namespace osu.Game.Rulesets.UI
{ {
} }
public bool OnPressed(KeyBindingPressEvent<T> e) => Target.Children.OfType<KeyCounterAction<T>>().Any(c => c.OnPressed(e.Action, Clock.Rate >= 0)); public bool OnPressed(KeyBindingPressEvent<T> e) => Target.Children.Where(c => c.CounterTrigger is KeyCounterAction<T>)
.Select(c => (KeyCounterAction<T>)c.CounterTrigger)
.Any(c => c.OnPressed(e.Action, Clock.Rate >= 0));
public void OnReleased(KeyBindingReleaseEvent<T> e) public void OnReleased(KeyBindingReleaseEvent<T> e)
{ {
foreach (var c in Target.Children.OfType<KeyCounterAction<T>>()) foreach (var c
in Target.Children.Where(c => c.CounterTrigger is KeyCounterAction<T>).Select(c => (KeyCounterAction<T>)c.CounterTrigger))
c.OnReleased(e.Action, Clock.Rate >= 0); c.OnReleased(e.Action, Clock.Rate >= 0);
} }
} }

View File

@ -0,0 +1,105 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Play
{
public partial class DefaultKeyCounter : KeyCounter
{
private Sprite buttonSprite = null!;
private Sprite glowSprite = null!;
private Container textLayer = null!;
private SpriteText countSpriteText = null!;
//further: change default values here and in KeyCounterCollection if needed, instead of passing them in every constructor
public Color4 KeyDownTextColor { get; set; } = Color4.DarkGray;
public Color4 KeyUpTextColor { get; set; } = Color4.White;
public double FadeTime { get; set; }
public DefaultKeyCounter(Trigger trigger)
: base(trigger)
{
}
[BackgroundDependencyLoader(true)]
private void load(TextureStore textures)
{
Children = new Drawable[]
{
buttonSprite = new Sprite
{
Texture = textures.Get(@"KeyCounter/key-up"),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
glowSprite = new Sprite
{
Texture = textures.Get(@"KeyCounter/key-glow"),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Alpha = 0
},
textLayer = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new OsuSpriteText
{
Text = Name,
Font = OsuFont.Numeric.With(size: 12),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.Both,
Position = new Vector2(0, -0.25f),
Colour = KeyUpTextColor
},
countSpriteText = new OsuSpriteText
{
Text = CountPresses.ToString(@"#,0"),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.Both,
Position = new Vector2(0, 0.25f),
Colour = KeyUpTextColor
}
}
}
};
// Set this manually because an element with Alpha=0 won't take it size to AutoSizeContainer,
// so the size can be changing between buttonSprite and glowSprite.
Height = buttonSprite.DrawHeight;
Width = buttonSprite.DrawWidth;
IsLit.BindValueChanged(e => updateGlowSprite(e.NewValue), true);
PressesCount.BindValueChanged(e => countSpriteText.Text = e.NewValue.ToString(@"#,0"), true);
}
private void updateGlowSprite(bool show)
{
if (show)
{
double remainingFadeTime = FadeTime * (1 - glowSprite.Alpha);
glowSprite.FadeIn(remainingFadeTime, Easing.OutQuint);
textLayer.FadeColour(KeyDownTextColor, remainingFadeTime, Easing.OutQuint);
}
else
{
double remainingFadeTime = 8 * FadeTime * glowSprite.Alpha;
glowSprite.FadeOut(remainingFadeTime, Easing.OutQuint);
textLayer.FadeColour(KeyUpTextColor, remainingFadeTime, Easing.OutQuint);
}
}
}
}

View File

@ -1,57 +1,37 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable disable using osu.Framework.Bindables;
using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites; using osu.Framework.Input.Events;
using osu.Framework.Graphics.Textures;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Play namespace osu.Game.Screens.Play
{ {
public abstract partial class KeyCounter : Container public abstract partial class KeyCounter : Container
{ {
private Sprite buttonSprite; public readonly Trigger CounterTrigger;
private Sprite glowSprite;
private Container textLayer;
private SpriteText countSpriteText;
public bool IsCounting { get; set; } = true; protected Bindable<bool> IsCountingBindable = new BindableBool(true);
private int countPresses;
protected Bindable<int> PressesCount = new BindableInt
{
MinValue = 0
};
public bool IsCounting
{
get => IsCountingBindable.Value;
set => IsCountingBindable.Value = value;
}
public int CountPresses public int CountPresses
{ {
get => countPresses; get => PressesCount.Value;
private set private set => PressesCount.Value = value;
{
if (countPresses != value)
{
countPresses = value;
countSpriteText.Text = value.ToString(@"#,0");
}
}
} }
private bool isLit; protected Bindable<bool> IsLit = new BindableBool();
public bool IsLit
{
get => isLit;
protected set
{
if (isLit != value)
{
isLit = value;
updateGlowSprite(value);
}
}
}
public void Increment() public void Increment()
{ {
@ -69,82 +49,51 @@ namespace osu.Game.Screens.Play
CountPresses--; CountPresses--;
} }
//further: change default values here and in KeyCounterCollection if needed, instead of passing them in every constructor protected override void LoadComplete()
public Color4 KeyDownTextColor { get; set; } = Color4.DarkGray; {
public Color4 KeyUpTextColor { get; set; } = Color4.White; Add(CounterTrigger);
public double FadeTime { get; set; } base.LoadComplete();
}
protected KeyCounter(string name) protected override bool Handle(UIEvent e) => CounterTrigger.TriggerEvent(e);
protected KeyCounter(Trigger trigger)
{
CounterTrigger = trigger;
trigger.Target = this;
Name = trigger.Name;
}
public abstract partial class Trigger : Component
{
private KeyCounter? target;
public KeyCounter Target
{
set => target = value;
}
protected Trigger(string name)
{ {
Name = name; Name = name;
} }
[BackgroundDependencyLoader(true)] protected void Lit(bool increment = true)
private void load(TextureStore textures)
{ {
Children = new Drawable[] if (target == null) return;
{
buttonSprite = new Sprite target.IsLit.Value = true;
{ if (increment)
Texture = textures.Get(@"KeyCounter/key-up"), target.Increment();
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
glowSprite = new Sprite
{
Texture = textures.Get(@"KeyCounter/key-glow"),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Alpha = 0
},
textLayer = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new OsuSpriteText
{
Text = Name,
Font = OsuFont.Numeric.With(size: 12),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.Both,
Position = new Vector2(0, -0.25f),
Colour = KeyUpTextColor
},
countSpriteText = new OsuSpriteText
{
Text = CountPresses.ToString(@"#,0"),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.Both,
Position = new Vector2(0, 0.25f),
Colour = KeyUpTextColor
}
}
}
};
// Set this manually because an element with Alpha=0 won't take it size to AutoSizeContainer,
// so the size can be changing between buttonSprite and glowSprite.
Height = buttonSprite.DrawHeight;
Width = buttonSprite.DrawWidth;
} }
private void updateGlowSprite(bool show) protected void Unlit(bool preserve = true)
{ {
if (show) if (target == null) return;
{
double remainingFadeTime = FadeTime * (1 - glowSprite.Alpha); target.IsLit.Value = false;
glowSprite.FadeIn(remainingFadeTime, Easing.OutQuint); if (!preserve)
textLayer.FadeColour(KeyDownTextColor, remainingFadeTime, Easing.OutQuint); target.Decrement();
}
else
{
double remainingFadeTime = 8 * FadeTime * glowSprite.Alpha;
glowSprite.FadeOut(remainingFadeTime, Easing.OutQuint);
textLayer.FadeColour(KeyUpTextColor, remainingFadeTime, Easing.OutQuint);
} }
} }
} }

View File

@ -7,7 +7,7 @@ using System.Collections.Generic;
namespace osu.Game.Screens.Play namespace osu.Game.Screens.Play
{ {
public partial class KeyCounterAction<T> : KeyCounter public partial class KeyCounterAction<T> : KeyCounter.Trigger
where T : struct where T : struct
{ {
public T Action { get; } public T Action { get; }
@ -23,9 +23,7 @@ namespace osu.Game.Screens.Play
if (!EqualityComparer<T>.Default.Equals(action, Action)) if (!EqualityComparer<T>.Default.Equals(action, Action))
return false; return false;
IsLit = true; Lit(forwards);
if (forwards)
Increment();
return false; return false;
} }
@ -34,9 +32,7 @@ namespace osu.Game.Screens.Play
if (!EqualityComparer<T>.Default.Equals(action, Action)) if (!EqualityComparer<T>.Default.Equals(action, Action))
return; return;
IsLit = false; Unlit(forwards);
if (!forwards)
Decrement();
} }
} }
} }

View File

@ -8,7 +8,7 @@ using osuTK.Input;
namespace osu.Game.Screens.Play namespace osu.Game.Screens.Play
{ {
public partial class KeyCounterKeyboard : KeyCounter public partial class KeyCounterKeyboard : KeyCounter.Trigger
{ {
public Key Key { get; } public Key Key { get; }
@ -21,17 +21,16 @@ namespace osu.Game.Screens.Play
protected override bool OnKeyDown(KeyDownEvent e) protected override bool OnKeyDown(KeyDownEvent e)
{ {
if (e.Key == Key) if (e.Key == Key)
{ Lit();
IsLit = true;
Increment();
}
return base.OnKeyDown(e); return base.OnKeyDown(e);
} }
protected override void OnKeyUp(KeyUpEvent e) protected override void OnKeyUp(KeyUpEvent e)
{ {
if (e.Key == Key) IsLit = false; if (e.Key == Key)
Unlit();
base.OnKeyUp(e); base.OnKeyUp(e);
} }
} }

View File

@ -9,7 +9,7 @@ using osuTK;
namespace osu.Game.Screens.Play namespace osu.Game.Screens.Play
{ {
public partial class KeyCounterMouse : KeyCounter public partial class KeyCounterMouse : KeyCounter.Trigger
{ {
public MouseButton Button { get; } public MouseButton Button { get; }
@ -39,17 +39,16 @@ namespace osu.Game.Screens.Play
protected override bool OnMouseDown(MouseDownEvent e) protected override bool OnMouseDown(MouseDownEvent e)
{ {
if (e.Button == Button) if (e.Button == Button)
{ Lit();
IsLit = true;
Increment();
}
return base.OnMouseDown(e); return base.OnMouseDown(e);
} }
protected override void OnMouseUp(MouseUpEvent e) protected override void OnMouseUp(MouseUpEvent e)
{ {
if (e.Button == Button) IsLit = false; if (e.Button == Button)
Unlit();
base.OnMouseUp(e); base.OnMouseUp(e);
} }
} }