Skip to content

Commit

Permalink
Merge pull request #17950 from Susko3/ButtonSystem-keys
Browse files Browse the repository at this point in the history
Allow any key to trigger the osu! cookie in the initial state
  • Loading branch information
peppy authored Apr 24, 2022
2 parents a40d140 + 684d88b commit d0fee3c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. 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.Testing;
using osu.Game.Screens.Menu;
using osu.Game.Screens.Select;
using osuTK.Input;

namespace osu.Game.Tests.Visual.Navigation
{
public class TestSceneButtonSystemNavigation : OsuGameTestScene
{
private ButtonSystem buttons => ((MainMenu)Game.ScreenStack.CurrentScreen).ChildrenOfType<ButtonSystem>().Single();

[Test]
public void TestGlobalActionHasPriority()
{
AddAssert("state is initial", () => buttons.State == ButtonSystemState.Initial);

// triggering the cookie in the initial state with any key should only happen if no other action is bound to that key.
// here, F10 is bound to GlobalAction.ToggleGameplayMouseButtons.
AddStep("press F10", () => InputManager.Key(Key.F10));
AddAssert("state is initial", () => buttons.State == ButtonSystemState.Initial);

AddStep("press P", () => InputManager.Key(Key.P));
AddAssert("state is top level", () => buttons.State == ButtonSystemState.TopLevel);
}

[Test]
public void TestShortcutKeys()
{
AddAssert("state is initial", () => buttons.State == ButtonSystemState.Initial);

AddStep("press P", () => InputManager.Key(Key.P));
AddAssert("state is top level", () => buttons.State == ButtonSystemState.TopLevel);

AddStep("press P", () => InputManager.Key(Key.P));
AddAssert("state is play", () => buttons.State == ButtonSystemState.Play);

AddStep("press P", () => InputManager.Key(Key.P));
AddAssert("entered song select", () => Game.ScreenStack.CurrentScreen is PlaySongSelect);
}
}
}
63 changes: 62 additions & 1 deletion osu.Game.Tests/Visual/UserInterface/TestSceneButtonSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
using osu.Game.Screens.Menu;
using osuTK;
using osuTK.Graphics;
using osuTK.Input;

namespace osu.Game.Tests.Visual.UserInterface
{
[TestFixture]
public class TestSceneButtonSystem : OsuTestScene
public class TestSceneButtonSystem : OsuManualInputManagerTestScene
{
private OsuLogo logo;
private ButtonSystem buttons;
Expand Down Expand Up @@ -64,6 +65,66 @@ public void TestSmoothExit()
AddStep("Enter mode", performEnterMode);
}

[TestCase(Key.P, true)]
[TestCase(Key.M, true)]
[TestCase(Key.L, true)]
[TestCase(Key.E, false)]
[TestCase(Key.D, false)]
[TestCase(Key.Q, false)]
[TestCase(Key.O, false)]
public void TestShortcutKeys(Key key, bool entersPlay)
{
int activationCount = -1;
AddStep("set up action", () =>
{
activationCount = 0;
void action() => activationCount++;

switch (key)
{
case Key.P:
buttons.OnSolo = action;
break;

case Key.M:
buttons.OnMultiplayer = action;
break;

case Key.L:
buttons.OnPlaylists = action;
break;

case Key.E:
buttons.OnEdit = action;
break;

case Key.D:
buttons.OnBeatmapListing = action;
break;

case Key.Q:
buttons.OnExit = action;
break;

case Key.O:
buttons.OnSettings = action;
break;
}
});

AddStep($"press {key}", () => InputManager.Key(key));
AddAssert("state is top level", () => buttons.State == ButtonSystemState.TopLevel);

if (entersPlay)
{
AddStep("press P", () => InputManager.Key(Key.P));
AddAssert("state is play", () => buttons.State == ButtonSystemState.Play);
}

AddStep($"press {key}", () => InputManager.Key(key));
AddAssert("action triggered", () => activationCount == 1);
}

private void performEnterMode()
{
buttons.State = ButtonSystemState.EnteringMode;
Expand Down
7 changes: 2 additions & 5 deletions osu.Game/Screens/Menu/ButtonSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,8 @@ protected override bool OnKeyDown(KeyDownEvent e)

if (State == ButtonSystemState.Initial)
{
if (buttonsTopLevel.Any(b => e.Key == b.TriggerKey))
{
logo?.TriggerClick();
return true;
}
logo?.TriggerClick();
return true;
}

return base.OnKeyDown(e);
Expand Down

0 comments on commit d0fee3c

Please sign in to comment.