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.Linq;
using osuTK.Input;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging;
using osu.Game.Input.Bindings;
using osu.Game.Screens.Play;
using osuTK;
@ -22,21 +24,29 @@ public class TestCaseGameplayMenuOverlay : ManualInputManagerTestCase
private FailOverlay failOverlay;
private PauseOverlay pauseOverlay;
[BackgroundDependencyLoader]
private void load()
{
Add(pauseOverlay = new PauseOverlay
{
OnResume = () => Logger.Log(@"Resume"),
OnRetry = () => Logger.Log(@"Retry"),
OnQuit = () => Logger.Log(@"Quit"),
});
private GlobalActionContainer globalActionContainer;
Add(failOverlay = new FailOverlay
[BackgroundDependencyLoader]
private void load(OsuGameBase game)
{
Child = globalActionContainer = new GlobalActionContainer(game)
{
OnRetry = () => Logger.Log(@"Retry"),
OnQuit = () => Logger.Log(@"Quit"),
});
Children = new Drawable[]
{
pauseOverlay = new PauseOverlay
{
OnResume = () => Logger.Log(@"Resume"),
OnRetry = () => Logger.Log(@"Retry"),
OnQuit = () => Logger.Log(@"Quit"),
},
failOverlay = new FailOverlay
{
OnRetry = () => Logger.Log(@"Retry"),
OnQuit = () => Logger.Log(@"Quit"),
}
}
};
var retryCount = 0;
@ -79,12 +89,6 @@ private void testHideResets()
AddAssert("Overlay state is reset", () => !failOverlay.Buttons.Any(b => b.Selected.Value));
}
private void press(Key key)
{
InputManager.PressKey(key);
InputManager.ReleaseKey(key);
}
/// <summary>
/// Tests that pressing enter after an overlay shows doesn't trigger an event because a selection hasn't occurred.
/// </summary>
@ -92,7 +96,7 @@ private void testEnterWithoutSelection()
{
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);
AddStep("Hide overlay", () => pauseOverlay.Hide());
@ -270,5 +274,17 @@ private void testEnterKeySelection()
});
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 @@ public abstract class GameplayMenuOverlay : OverlayContainer, IKeyBindingHandler
/// <summary>
/// Action that is invoked when <see cref="GlobalAction.Back"/> is triggered.
/// </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 Description { get; }
protected internal FillFlowContainer<DialogButton> InternalButtons;
@ -229,16 +235,30 @@ protected override bool OnKeyDown(KeyDownEvent e)
public bool OnPressed(GlobalAction action)
{
if (action == GlobalAction.Back)
switch (action)
{
BackAction.Invoke();
return true;
case GlobalAction.Back:
BackAction.Invoke();
return true;
case GlobalAction.Select:
SelectAction.Invoke();
return true;
}
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)
{
@ -288,15 +308,6 @@ protected override bool OnMouseMove(MouseMoveEvent e)
Selected.Value = true;
return base.OnMouseMove(e);
}
protected override bool OnKeyDown(KeyDownEvent e)
{
if (e.Repeat || e.Key != Key.Enter || !Selected.Value)
return false;
Click();
return true;
}
}
}
}