1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 03:25:11 +08:00

Add functionality of Random button

This commit is contained in:
mk56-spn 2022-12-01 16:29:52 +01:00
parent 1530495e7c
commit d7cea51551
3 changed files with 266 additions and 13 deletions

View File

@ -1,17 +1,29 @@
// 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.Linq;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Testing;
using osu.Game.Screens.Select.FooterV2;
using osuTK;
using osuTK.Input;
namespace osu.Game.Tests.Visual.SongSelect
{
public partial class TestSceneSongSelectFooterV2 : OsuManualInputManagerTestScene
{
private FooterButtonRandomV2 randomButton = null!;
private bool nextRandomCalled;
private bool previousRandomCalled;
[SetUp]
public void SetUp() => Schedule(() =>
{
nextRandomCalled = false;
previousRandomCalled = false;
FooterV2 footer;
Child = footer = new FooterV2
@ -21,13 +33,75 @@ namespace osu.Game.Tests.Visual.SongSelect
};
footer.AddButton(new FooterButtonModsV2());
footer.AddButton(new FooterButtonRandomV2());
footer.AddButton(randomButton = new FooterButtonRandomV2
{
NextRandom = () => nextRandomCalled = true,
PreviousRandom = () => previousRandomCalled = true
});
footer.AddButton(new FooterButtonOptionsV2());
InputManager.MoveMouseTo(Vector2.Zero);
});
[Test]
public void TestBasic()
public void TestState()
{
AddRepeatStep("toggle options state", () => this.ChildrenOfType<FooterButtonV2>().Last().Enabled.Toggle(), 20);
}
[Test]
public void TestFooterRandom()
{
AddStep("press F2", () => InputManager.Key(Key.F2));
AddAssert("next random invoked", () => nextRandomCalled && !previousRandomCalled);
}
[Test]
public void TestFooterRandomViaMouse()
{
AddStep("click button", () =>
{
InputManager.MoveMouseTo(randomButton);
InputManager.Click(MouseButton.Left);
});
AddAssert("next random invoked", () => nextRandomCalled && !previousRandomCalled);
}
[Test]
public void TestFooterRewind()
{
AddStep("press Shift+F2", () =>
{
InputManager.PressKey(Key.LShift);
InputManager.PressKey(Key.F2);
InputManager.ReleaseKey(Key.F2);
InputManager.ReleaseKey(Key.LShift);
});
AddAssert("previous random invoked", () => previousRandomCalled && !nextRandomCalled);
}
[Test]
public void TestFooterRewindViaShiftMouseLeft()
{
AddStep("shift + click button", () =>
{
InputManager.PressKey(Key.LShift);
InputManager.MoveMouseTo(randomButton);
InputManager.Click(MouseButton.Left);
InputManager.ReleaseKey(Key.LShift);
});
AddAssert("previous random invoked", () => previousRandomCalled && !nextRandomCalled);
}
[Test]
public void TestFooterRewindViaMouseRight()
{
AddStep("right click button", () =>
{
InputManager.MoveMouseTo(randomButton);
InputManager.Click(MouseButton.Right);
});
AddAssert("previous random invoked", () => previousRandomCalled && !nextRandomCalled);
}
}
}

View File

@ -1,20 +1,160 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Input.Bindings;
using osuTK;
using osuTK.Input;
namespace osu.Game.Screens.Select.FooterV2
{
public partial class FooterButtonRandomV2 : FooterButtonV2
{
public Action NextRandom { get; set; } = null!;
public Action PreviousRandom { get; set; } = null!;
private Container persistentText = null!;
private OsuSpriteText randomSpriteText = null!;
private OsuSpriteText rewindSpriteText = null!;
private bool rewindSearch;
[BackgroundDependencyLoader]
private void load()
{
Text = "Random";
//TODO: use https://fontawesome.com/icons/shuffle?s=solid&f=classic when local Fontawesome is updated
Icon = FontAwesome.Solid.Random;
AccentColour = Colour4.FromHex("#66CCFF");
TextContainer.Add(persistentText = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AlwaysPresent = true,
AutoSizeAxes = Axes.Both,
Children = new[]
{
randomSpriteText = new OsuSpriteText
{
Font = OsuFont.TorusAlternate.With(size: 16),
AlwaysPresent = true,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = "Random",
},
rewindSpriteText = new OsuSpriteText
{
Font = OsuFont.TorusAlternate.With(size: 16),
AlwaysPresent = true,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = "Rewind",
Alpha = 0f,
}
}
});
Action = () =>
{
if (rewindSearch)
{
const double fade_time = 500;
OsuSpriteText fallingRewind;
TextContainer.Add(fallingRewind = new OsuSpriteText
{
Alpha = 0,
Text = rewindSpriteText.Text,
AlwaysPresent = true, // make sure the button is sized large enough to always show this
Anchor = Anchor.BottomCentre,
Origin = Anchor.BottomCentre
});
fallingRewind.FadeOutFromOne(fade_time, Easing.In);
fallingRewind.MoveTo(Vector2.Zero).MoveTo(new Vector2(0, 10), fade_time, Easing.In);
fallingRewind.Expire();
persistentText.FadeInFromZero(fade_time, Easing.In);
PreviousRandom.Invoke();
}
else
{
NextRandom.Invoke();
}
};
}
protected override bool OnKeyDown(KeyDownEvent e)
{
updateText(e.ShiftPressed);
return base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyUpEvent e)
{
updateText(e.ShiftPressed);
base.OnKeyUp(e);
}
protected override bool OnClick(ClickEvent e)
{
try
{
// this uses OR to handle rewinding when clicks are triggered by other sources (i.e. right button in OnMouseUp).
rewindSearch |= e.ShiftPressed;
return base.OnClick(e);
}
finally
{
rewindSearch = false;
}
}
protected override void OnMouseUp(MouseUpEvent e)
{
if (e.Button == MouseButton.Right)
{
rewindSearch = true;
TriggerClick();
return;
}
base.OnMouseUp(e);
}
public override bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
rewindSearch = e.Action == GlobalAction.SelectPreviousRandom;
if (e.Action != GlobalAction.SelectNextRandom && e.Action != GlobalAction.SelectPreviousRandom)
{
return false;
}
if (!e.Repeat)
TriggerClick();
return true;
}
public override void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
if (e.Action == GlobalAction.SelectPreviousRandom)
{
rewindSearch = false;
}
}
private void updateText(bool rewind = false)
{
randomSpriteText.Alpha = rewind ? 0 : 1;
rewindSpriteText.Alpha = rewind ? 1 : 0;
}
}
}

View File

@ -10,6 +10,8 @@ using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Input.Bindings;
@ -48,13 +50,14 @@ namespace osu.Game.Screens.Select.FooterV2
set => icon.Icon = value;
}
protected string Text
protected LocalisableString Text
{
set => text.Text = value;
}
private SpriteText text = null!;
private SpriteIcon icon = null!;
private OsuSpriteText text = null!;
protected Container TextContainer = null!;
private Box bar = null!;
[BackgroundDependencyLoader]
@ -86,6 +89,18 @@ namespace osu.Game.Screens.Select.FooterV2
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
TextContainer = new Container
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Position = new Vector2(-SHEAR_WIDTH * (52f / button_height), 42),
AutoSizeAxes = Axes.Both,
Child = text = new OsuSpriteText
{
Font = OsuFont.TorusAlternate.With(size: 16),
AlwaysPresent = true
}
},
icon = new SpriteIcon
{
//We want to offset this by the same amount as the text for aesthetic purposes
@ -94,12 +109,6 @@ namespace osu.Game.Screens.Select.FooterV2
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre
},
text = new OsuSpriteText
{
Position = new Vector2(-SHEAR_WIDTH * (52f / button_height), 42),
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre
},
new Container
{
//Offset the bar to centre it with consideration for the shearing
@ -123,11 +132,41 @@ namespace osu.Game.Screens.Select.FooterV2
public Action HoverLost = null!;
public GlobalAction? Hotkey;
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
protected override void UpdateAfterChildren()
{
}
protected override bool OnHover(HoverEvent e)
{
Hovered?.Invoke();
return true;
}
protected override void OnHoverLost(HoverLostEvent e)
{
HoverLost?.Invoke();
}
protected override bool OnClick(ClickEvent e)
{
if (!Enabled.Value)
return true;
return base.OnClick(e);
}
public virtual bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Action == Hotkey && !e.Repeat)
{
TriggerClick();
return true;
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e) { }
public virtual void OnReleased(KeyBindingReleaseEvent<GlobalAction> e) { }
}
}