-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5966 from bdach/is-prioritised-positional
Make `KeyBindingContainer.Prioritised` work for positional input too
- Loading branch information
Showing
2 changed files
with
105 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
|
@@ -25,7 +23,7 @@ public void TestTriggerWithNoKeyBindings() | |
bool pressedReceived = false; | ||
bool releasedReceived = false; | ||
|
||
TestKeyBindingContainer keyBindingContainer = null; | ||
TestKeyBindingContainer keyBindingContainer = null!; | ||
|
||
AddStep("add container", () => | ||
{ | ||
|
@@ -89,7 +87,7 @@ public void TestKeyHandledByOtherDrawableDoesNotTrigger() | |
List<TestAction> pressedActions = new List<TestAction>(); | ||
List<TestAction> releasedActions = new List<TestAction>(); | ||
|
||
TextBox textBox = null; | ||
TextBox textBox = null!; | ||
|
||
AddStep("add children", () => | ||
{ | ||
|
@@ -218,7 +216,7 @@ public void TestKeyRepeatDoesntFireWhenNotAlive() | |
int pressedReceived = 0; | ||
int repeatedReceived = 0; | ||
bool releasedReceived = false; | ||
TestKeyBindingReceptor receptor = null; | ||
TestKeyBindingReceptor receptor = null!; | ||
|
||
AddStep("add container", () => | ||
{ | ||
|
@@ -299,11 +297,63 @@ public void TestKeyCombinationRepeatEvents() | |
AddStep("release B", () => InputManager.ReleaseKey(Key.B)); | ||
} | ||
|
||
[Test] | ||
public void TestPrioritisedNonPositionalInput([Values] bool prioritised) | ||
{ | ||
bool containerReceivedInput = false; | ||
|
||
AddStep("create content", () => | ||
{ | ||
containerReceivedInput = false; | ||
|
||
Child = new TestKeyBindingContainer(prioritised) | ||
{ | ||
Pressed = a => containerReceivedInput = a == TestAction.ActionA, | ||
Child = new InputBlockingDrawable() | ||
}; | ||
}); | ||
|
||
AddStep("trigger action", () => InputManager.Key(Key.A)); | ||
|
||
if (prioritised) | ||
AddAssert("container received input", () => containerReceivedInput); | ||
else | ||
AddAssert("container did not receive input", () => !containerReceivedInput); | ||
} | ||
|
||
[Test] | ||
public void TestPrioritisedPositionalInput([Values] bool prioritised) | ||
{ | ||
bool containerReceivedInput = false; | ||
|
||
Drawable receptor = null!; | ||
|
||
AddStep("create content", () => | ||
{ | ||
containerReceivedInput = false; | ||
|
||
Child = new TestKeyBindingContainer(prioritised) | ||
{ | ||
RelativeSizeAxes = Axes.Both, | ||
Pressed = a => containerReceivedInput = a == TestAction.ActionMouse4, | ||
Child = receptor = new InputBlockingDrawable() | ||
}; | ||
}); | ||
|
||
AddStep("hover receptor", () => InputManager.MoveMouseTo(receptor)); | ||
AddStep("trigger action", () => InputManager.Click(MouseButton.Button4)); | ||
|
||
if (prioritised) | ||
AddAssert("container received input", () => containerReceivedInput); | ||
else | ||
AddAssert("container did not receive input", () => !containerReceivedInput); | ||
} | ||
|
||
private partial class TestKeyBindingReceptor : Drawable, IKeyBindingHandler<TestAction> | ||
{ | ||
public Action<TestAction> Pressed; | ||
public Action<TestAction> Repeated; | ||
public Action<TestAction> Released; | ||
public Action<TestAction>? Pressed; | ||
public Action<TestAction>? Repeated; | ||
public Action<TestAction>? Released; | ||
|
||
public TestKeyBindingReceptor() | ||
{ | ||
|
@@ -326,23 +376,53 @@ public void OnReleased(KeyBindingReleaseEvent<TestAction> e) | |
} | ||
} | ||
|
||
private partial class TestKeyBindingContainer : KeyBindingContainer<TestAction> | ||
private partial class TestKeyBindingContainer : KeyBindingContainer<TestAction>, IKeyBindingHandler<TestAction> | ||
{ | ||
protected override bool Prioritised { get; } | ||
|
||
public Func<TestAction, bool>? Pressed; | ||
|
||
public TestKeyBindingContainer(bool prioritised = false) | ||
{ | ||
Prioritised = prioritised; | ||
} | ||
|
||
public override IEnumerable<IKeyBinding> DefaultKeyBindings => new IKeyBinding[] | ||
{ | ||
new KeyBinding(InputKey.A, TestAction.ActionA), | ||
new KeyBinding(new KeyCombination(InputKey.A, InputKey.B), TestAction.ActionAB), | ||
new KeyBinding(InputKey.Enter, TestAction.ActionEnter), | ||
new KeyBinding(InputKey.Control, TestAction.ActionControl) | ||
new KeyBinding(InputKey.Control, TestAction.ActionControl), | ||
new KeyBinding(InputKey.ExtraMouseButton4, TestAction.ActionMouse4), | ||
}; | ||
|
||
public bool OnPressed(KeyBindingPressEvent<TestAction> e) | ||
{ | ||
return Pressed?.Invoke(e.Action) == true; | ||
} | ||
|
||
public void OnReleased(KeyBindingReleaseEvent<TestAction> e) | ||
{ | ||
} | ||
} | ||
|
||
private partial class InputBlockingDrawable : Drawable | ||
{ | ||
protected override bool Handle(UIEvent e) => true; | ||
|
||
public InputBlockingDrawable() | ||
{ | ||
RelativeSizeAxes = Axes.Both; | ||
} | ||
} | ||
|
||
private enum TestAction | ||
{ | ||
ActionA, | ||
ActionAB, | ||
ActionEnter, | ||
ActionControl | ||
ActionControl, | ||
ActionMouse4, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters