1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 15:33:05 +08:00

Merge pull request #4169 from Scotsoo/menu-globalaction-select

Add support for key bindings in gameplay menus

Co-authored-by: Dean Herbert <pe@ppy.sh>
This commit is contained in:
Dean Herbert 2019-03-24 12:29:50 +09:00 committed by GitHub
commit 8c8c761e02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 34 deletions

View File

@ -7,8 +7,10 @@ using System.ComponentModel;
using System.Linq; using System.Linq;
using osuTK.Input; using osuTK.Input;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Game.Input.Bindings;
using osu.Game.Screens.Play; using osu.Game.Screens.Play;
using osuTK; using osuTK;
@ -22,21 +24,29 @@ namespace osu.Game.Tests.Visual
private FailOverlay failOverlay; private FailOverlay failOverlay;
private PauseOverlay pauseOverlay; private PauseOverlay pauseOverlay;
private GlobalActionContainer globalActionContainer;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load(OsuGameBase game)
{ {
Add(pauseOverlay = new PauseOverlay Child = globalActionContainer = new GlobalActionContainer(game)
{
Children = new Drawable[]
{
pauseOverlay = new PauseOverlay
{ {
OnResume = () => Logger.Log(@"Resume"), OnResume = () => Logger.Log(@"Resume"),
OnRetry = () => Logger.Log(@"Retry"), OnRetry = () => Logger.Log(@"Retry"),
OnQuit = () => Logger.Log(@"Quit"), OnQuit = () => Logger.Log(@"Quit"),
}); },
failOverlay = new FailOverlay
Add(failOverlay = new FailOverlay
{ {
OnRetry = () => Logger.Log(@"Retry"), OnRetry = () => Logger.Log(@"Retry"),
OnQuit = () => Logger.Log(@"Quit"), OnQuit = () => Logger.Log(@"Quit"),
}); }
}
};
var retryCount = 0; var retryCount = 0;
@ -79,12 +89,6 @@ namespace osu.Game.Tests.Visual
AddAssert("Overlay state is reset", () => !failOverlay.Buttons.Any(b => b.Selected.Value)); AddAssert("Overlay state is reset", () => !failOverlay.Buttons.Any(b => b.Selected.Value));
} }
private void press(Key key)
{
InputManager.PressKey(key);
InputManager.ReleaseKey(key);
}
/// <summary> /// <summary>
/// Tests that pressing enter after an overlay shows doesn't trigger an event because a selection hasn't occurred. /// Tests that pressing enter after an overlay shows doesn't trigger an event because a selection hasn't occurred.
/// </summary> /// </summary>
@ -92,7 +96,7 @@ namespace osu.Game.Tests.Visual
{ {
AddStep("Show overlay", () => pauseOverlay.Show()); AddStep("Show overlay", () => pauseOverlay.Show());
AddStep("Press enter", () => press(Key.Enter)); AddStep("Press select", () => press(GlobalAction.Select));
AddAssert("Overlay still open", () => pauseOverlay.State == Visibility.Visible); AddAssert("Overlay still open", () => pauseOverlay.State == Visibility.Visible);
AddStep("Hide overlay", () => pauseOverlay.Hide()); AddStep("Hide overlay", () => pauseOverlay.Hide());
@ -270,5 +274,17 @@ namespace osu.Game.Tests.Visual
}); });
AddAssert("Overlay is closed", () => pauseOverlay.State == Visibility.Hidden); AddAssert("Overlay is closed", () => pauseOverlay.State == Visibility.Hidden);
} }
private void press(Key key)
{
InputManager.PressKey(key);
InputManager.ReleaseKey(key);
}
private void press(GlobalAction action)
{
globalActionContainer.TriggerPressed(action);
globalActionContainer.TriggerReleased(action);
}
} }
} }

View File

@ -38,9 +38,15 @@ namespace osu.Game.Screens.Play
/// <summary> /// <summary>
/// Action that is invoked when <see cref="GlobalAction.Back"/> is triggered. /// Action that is invoked when <see cref="GlobalAction.Back"/> is triggered.
/// </summary> /// </summary>
protected virtual Action BackAction => () => InternalButtons.Children.Last().Click(); protected virtual Action BackAction => () => InternalButtons.Children.LastOrDefault()?.Click();
/// <summary>
/// Action that is invoked when <see cref="GlobalAction.Select"/> is triggered.
/// </summary>
protected virtual Action SelectAction => () => InternalButtons.Children.FirstOrDefault(f => f.Selected.Value)?.Click();
public abstract string Header { get; } public abstract string Header { get; }
public abstract string Description { get; } public abstract string Description { get; }
protected internal FillFlowContainer<DialogButton> InternalButtons; protected internal FillFlowContainer<DialogButton> InternalButtons;
@ -229,16 +235,30 @@ namespace osu.Game.Screens.Play
public bool OnPressed(GlobalAction action) public bool OnPressed(GlobalAction action)
{ {
if (action == GlobalAction.Back) switch (action)
{ {
case GlobalAction.Back:
BackAction.Invoke(); BackAction.Invoke();
return true; return true;
case GlobalAction.Select:
SelectAction.Invoke();
return true;
} }
return false; return false;
} }
public bool OnReleased(GlobalAction action) => action == GlobalAction.Back; public bool OnReleased(GlobalAction action)
{
switch (action)
{
case GlobalAction.Back:
case GlobalAction.Select:
return true;
}
return false;
}
private void buttonSelectionChanged(DialogButton button, bool isSelected) private void buttonSelectionChanged(DialogButton button, bool isSelected)
{ {
@ -288,15 +308,6 @@ namespace osu.Game.Screens.Play
Selected.Value = true; Selected.Value = true;
return base.OnMouseMove(e); return base.OnMouseMove(e);
} }
protected override bool OnKeyDown(KeyDownEvent e)
{
if (e.Repeat || e.Key != Key.Enter || !Selected.Value)
return false;
Click();
return true;
}
} }
} }
} }