Merge pull request #21292 from peppy/fix-binding-two-keys-single-binding

Fix being able to bind two non-modifier keys to the same binding
This commit is contained in:
Dan Balasescu 2022-11-18 16:19:38 +09:00 committed by GitHub
commit 9d33a6568e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 12 deletions

View File

@ -40,6 +40,43 @@ namespace osu.Game.Tests.Visual.Settings
AddWaitStep("wait for scroll", 5);
}
[Test]
public void TestBindingTwoNonModifiers()
{
AddStep("press j", () => InputManager.PressKey(Key.J));
scrollToAndStartBinding("Increase volume");
AddStep("press k", () => InputManager.Key(Key.K));
AddStep("release j", () => InputManager.ReleaseKey(Key.J));
checkBinding("Increase volume", "K");
}
[Test]
public void TestBindingSingleKey()
{
scrollToAndStartBinding("Increase volume");
AddStep("press k", () => InputManager.Key(Key.K));
checkBinding("Increase volume", "K");
}
[Test]
public void TestBindingSingleModifier()
{
scrollToAndStartBinding("Increase volume");
AddStep("press shift", () => InputManager.PressKey(Key.ShiftLeft));
AddStep("release shift", () => InputManager.ReleaseKey(Key.ShiftLeft));
checkBinding("Increase volume", "LShift");
}
[Test]
public void TestBindingSingleKeyWithModifier()
{
scrollToAndStartBinding("Increase volume");
AddStep("press shift", () => InputManager.PressKey(Key.ShiftLeft));
AddStep("press k", () => InputManager.Key(Key.K));
AddStep("release shift", () => InputManager.ReleaseKey(Key.ShiftLeft));
checkBinding("Increase volume", "LShift-K");
}
[Test]
public void TestBindingMouseWheelToNonGameplay()
{
@ -169,7 +206,8 @@ namespace osu.Game.Tests.Visual.Settings
AddUntilStep("restore button hidden", () => settingsKeyBindingRow.ChildrenOfType<RestoreDefaultValueButton<bool>>().First().Alpha == 0);
AddAssert("binding cleared", () => settingsKeyBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(0).KeyBinding.KeyCombination.Equals(settingsKeyBindingRow.Defaults.ElementAt(0)));
AddAssert("binding cleared",
() => settingsKeyBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(0).KeyBinding.KeyCombination.Equals(settingsKeyBindingRow.Defaults.ElementAt(0)));
}
[Test]
@ -198,7 +236,8 @@ namespace osu.Game.Tests.Visual.Settings
AddUntilStep("restore button hidden", () => settingsKeyBindingRow.ChildrenOfType<RestoreDefaultValueButton<bool>>().First().Alpha == 0);
AddAssert("binding cleared", () => settingsKeyBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(0).KeyBinding.KeyCombination.Equals(settingsKeyBindingRow.Defaults.ElementAt(0)));
AddAssert("binding cleared",
() => settingsKeyBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(0).KeyBinding.KeyCombination.Equals(settingsKeyBindingRow.Defaults.ElementAt(0)));
}
[Test]
@ -256,8 +295,8 @@ namespace osu.Game.Tests.Visual.Settings
var firstRow = panel.ChildrenOfType<KeyBindingRow>().First(r => r.ChildrenOfType<OsuSpriteText>().Any(s => s.Text.ToString() == name));
var firstButton = firstRow.ChildrenOfType<KeyBindingRow.KeyButton>().First();
return firstButton.Text.Text == keyName;
});
return firstButton.Text.Text.ToString();
}, () => Is.EqualTo(keyName));
}
private void scrollToAndStartBinding(string name)

View File

@ -226,7 +226,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input
}
}
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState));
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromMouseButton(e.Button));
return true;
}
@ -252,7 +252,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input
{
if (bindTarget.IsHovered)
{
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState, e.ScrollDelta));
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState, e.ScrollDelta), KeyCombination.FromScrollDelta(e.ScrollDelta).First());
finalise();
return true;
}
@ -263,10 +263,10 @@ namespace osu.Game.Overlays.Settings.Sections.Input
protected override bool OnKeyDown(KeyDownEvent e)
{
if (!HasFocus)
if (!HasFocus || e.Repeat)
return false;
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState));
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromKey(e.Key));
if (!isModifier(e.Key)) finalise();
return true;
@ -288,7 +288,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input
if (!HasFocus)
return false;
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState));
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromJoystickButton(e.Button));
finalise();
return true;
@ -310,7 +310,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input
if (!HasFocus)
return false;
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState));
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromMidiKey(e.Key));
finalise();
return true;
@ -332,7 +332,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input
if (!HasFocus)
return false;
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState));
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromTabletAuxiliaryButton(e.Button));
finalise();
return true;
@ -354,7 +354,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input
if (!HasFocus)
return false;
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState));
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromTabletPenButton(e.Button));
finalise();
return true;
@ -563,6 +563,14 @@ namespace osu.Game.Overlays.Settings.Sections.Input
}
}
/// <summary>
/// Update from a key combination, only allowing a single non-modifier key to be specified.
/// </summary>
/// <param name="fullState">A <see cref="KeyCombination"/> generated from the full input state.</param>
/// <param name="triggerKey">The key which triggered this update, and should be used as the binding.</param>
public void UpdateKeyCombination(KeyCombination fullState, InputKey triggerKey) =>
UpdateKeyCombination(new KeyCombination(fullState.Keys.Where(KeyCombination.IsModifierKey).Append(triggerKey)));
public void UpdateKeyCombination(KeyCombination newCombination)
{
if (KeyBinding.RulesetName != null && !RealmKeyBindingStore.CheckValidForGameplay(newCombination))