1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 08:32:57 +08:00

Merge pull request #25105 from bdach/multiple-keys-one-binding

Disallow binding multiple bindings in a single section to one key
This commit is contained in:
Dean Herbert 2023-10-13 20:01:33 +09:00 committed by GitHub
commit 6358a5e210
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 942 additions and 213 deletions

View File

@ -0,0 +1,63 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Bindings;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Overlays;
using osu.Game.Overlays.Settings.Sections.Input;
using osu.Game.Rulesets.Osu;
using osuTK.Input;
namespace osu.Game.Tests.Visual.Settings
{
public partial class TestSceneKeyBindingConflictPopover : OsuTestScene
{
[Cached]
private OverlayColourProvider colourProvider { get; set; } = new OverlayColourProvider(OverlayColourScheme.Aquamarine);
[Test]
public void TestAppearance()
{
ButtonWithConflictPopover button = null!;
AddStep("create content", () =>
{
Child = new PopoverContainer
{
RelativeSizeAxes = Axes.Both,
Child = button = new ButtonWithConflictPopover
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = "Open popover",
Width = 300
}
};
});
AddStep("show popover", () => button.TriggerClick());
}
private partial class ButtonWithConflictPopover : RoundedButton, IHasPopover
{
[BackgroundDependencyLoader]
private void load()
{
Action = this.ShowPopover;
}
public Popover GetPopover() => new KeyBindingConflictPopover(
new KeyBindingRow.KeyBindingConflictInfo(
new KeyBindingRow.ConflictingKeyBinding(Guid.NewGuid(), OsuAction.LeftButton, KeyCombination.FromKey(Key.X), new KeyCombination(InputKey.None)),
new KeyBindingRow.ConflictingKeyBinding(Guid.NewGuid(), OsuAction.RightButton, KeyCombination.FromKey(Key.Z), KeyCombination.FromKey(Key.X))
)
);
}
}
}

View File

@ -10,8 +10,11 @@ using osu.Framework.Testing;
using osu.Framework.Threading;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Overlays;
using osu.Game.Overlays.Settings;
using osu.Game.Overlays.Settings.Sections.Input;
using osu.Game.Rulesets.Taiko;
using osuTK.Input;
namespace osu.Game.Tests.Visual.Settings
@ -207,7 +210,7 @@ namespace osu.Game.Tests.Visual.Settings
AddUntilStep("restore button hidden", () => settingsKeyBindingRow.ChildrenOfType<RevertToDefaultButton<bool>>().First().Alpha == 0);
AddAssert("binding cleared",
() => settingsKeyBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(0).KeyBinding.KeyCombination.Equals(settingsKeyBindingRow.Defaults.ElementAt(0)));
() => settingsKeyBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(0).KeyBinding.Value.KeyCombination.Equals(settingsKeyBindingRow.Defaults.ElementAt(0)));
}
[Test]
@ -237,7 +240,7 @@ namespace osu.Game.Tests.Visual.Settings
AddUntilStep("restore button hidden", () => settingsKeyBindingRow.ChildrenOfType<RevertToDefaultButton<bool>>().First().Alpha == 0);
AddAssert("binding cleared",
() => settingsKeyBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(0).KeyBinding.KeyCombination.Equals(settingsKeyBindingRow.Defaults.ElementAt(0)));
() => settingsKeyBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(0).KeyBinding.Value.KeyCombination.Equals(settingsKeyBindingRow.Defaults.ElementAt(0)));
}
[Test]
@ -288,6 +291,106 @@ namespace osu.Game.Tests.Visual.Settings
AddUntilStep("all reset section bindings buttons shown", () => panel.ChildrenOfType<ResetButton>().All(button => button.Alpha == 1));
}
[Test]
public void TestBindingConflictResolvedByRollback()
{
AddStep("reset taiko section to default", () =>
{
var section = panel.ChildrenOfType<VariantBindingsSubsection>().First(section => new TaikoRuleset().RulesetInfo.Equals(section.Ruleset));
section.ChildrenOfType<ResetButton>().Single().TriggerClick();
});
AddStep("move mouse to centre", () => InputManager.MoveMouseTo(panel.ScreenSpaceDrawQuad.Centre));
AddUntilStep("wait for collapsed", () => panel.ChildrenOfType<SettingsSidebar>().Single().Expanded.Value, () => Is.False);
scrollToAndStartBinding("Left (rim)");
AddStep("attempt to bind M1 to two keys", () => InputManager.Click(MouseButton.Left));
KeyBindingConflictPopover popover = null;
AddUntilStep("wait for popover", () => popover = panel.ChildrenOfType<KeyBindingConflictPopover>().SingleOrDefault(), () => Is.Not.Null);
AddStep("click first button", () => popover.ChildrenOfType<RoundedButton>().First().TriggerClick());
checkBinding("Left (centre)", "M1");
checkBinding("Left (rim)", "M2");
}
[Test]
public void TestBindingConflictResolvedByOverwrite()
{
AddStep("reset taiko section to default", () =>
{
var section = panel.ChildrenOfType<VariantBindingsSubsection>().First(section => new TaikoRuleset().RulesetInfo.Equals(section.Ruleset));
section.ChildrenOfType<ResetButton>().Single().TriggerClick();
});
AddStep("move mouse to centre", () => InputManager.MoveMouseTo(panel.ScreenSpaceDrawQuad.Centre));
AddUntilStep("wait for collapsed", () => panel.ChildrenOfType<SettingsSidebar>().Single().Expanded.Value, () => Is.False);
scrollToAndStartBinding("Left (rim)");
AddStep("attempt to bind M1 to two keys", () => InputManager.Click(MouseButton.Left));
KeyBindingConflictPopover popover = null;
AddUntilStep("wait for popover", () => popover = panel.ChildrenOfType<KeyBindingConflictPopover>().SingleOrDefault(), () => Is.Not.Null);
AddStep("click second button", () => popover.ChildrenOfType<RoundedButton>().ElementAt(1).TriggerClick());
checkBinding("Left (centre)", string.Empty);
checkBinding("Left (rim)", "M1");
}
[Test]
public void TestBindingConflictCausedByResetToDefaultOfSingleRow()
{
AddStep("reset taiko section to default", () =>
{
var section = panel.ChildrenOfType<VariantBindingsSubsection>().First(section => new TaikoRuleset().RulesetInfo.Equals(section.Ruleset));
section.ChildrenOfType<ResetButton>().Single().TriggerClick();
});
AddStep("move mouse to centre", () => InputManager.MoveMouseTo(panel.ScreenSpaceDrawQuad.Centre));
AddUntilStep("wait for collapsed", () => panel.ChildrenOfType<SettingsSidebar>().Single().Expanded.Value, () => Is.False);
scrollToAndStartBinding("Left (centre)");
AddStep("clear binding", () =>
{
var row = panel.ChildrenOfType<KeyBindingRow>().First(r => r.ChildrenOfType<OsuSpriteText>().Any(s => s.Text.ToString() == "Left (centre)"));
row.ChildrenOfType<KeyBindingRow.ClearButton>().Single().TriggerClick();
});
scrollToAndStartBinding("Left (rim)");
AddStep("bind M1", () => InputManager.Click(MouseButton.Left));
AddStep("reset Left (centre) to default", () =>
{
var row = panel.ChildrenOfType<KeyBindingRow>().First(r => r.ChildrenOfType<OsuSpriteText>().Any(s => s.Text.ToString() == "Left (centre)"));
row.ChildrenOfType<RevertToDefaultButton<bool>>().Single().TriggerClick();
});
KeyBindingConflictPopover popover = null;
AddUntilStep("wait for popover", () => popover = panel.ChildrenOfType<KeyBindingConflictPopover>().SingleOrDefault(), () => Is.Not.Null);
AddStep("click second button", () => popover.ChildrenOfType<RoundedButton>().ElementAt(1).TriggerClick());
checkBinding("Left (centre)", "M1");
checkBinding("Left (rim)", string.Empty);
}
[Test]
public void TestResettingEntireSectionDoesNotCauseBindingConflicts()
{
AddStep("reset taiko section to default", () =>
{
var section = panel.ChildrenOfType<VariantBindingsSubsection>().First(section => new TaikoRuleset().RulesetInfo.Equals(section.Ruleset));
section.ChildrenOfType<ResetButton>().Single().TriggerClick();
});
AddStep("move mouse to centre", () => InputManager.MoveMouseTo(panel.ScreenSpaceDrawQuad.Centre));
AddUntilStep("wait for collapsed", () => panel.ChildrenOfType<SettingsSidebar>().Single().Expanded.Value, () => Is.False);
scrollToAndStartBinding("Left (centre)");
AddStep("clear binding", () =>
{
var row = panel.ChildrenOfType<KeyBindingRow>().First(r => r.ChildrenOfType<OsuSpriteText>().Any(s => s.Text.ToString() == "Left (centre)"));
row.ChildrenOfType<KeyBindingRow.ClearButton>().Single().TriggerClick();
});
scrollToAndStartBinding("Left (rim)");
AddStep("bind M1", () => InputManager.Click(MouseButton.Left));
AddStep("reset taiko section to default", () =>
{
var section = panel.ChildrenOfType<VariantBindingsSubsection>().First(section => new TaikoRuleset().RulesetInfo.Equals(section.Ruleset));
section.ChildrenOfType<ResetButton>().Single().TriggerClick();
});
AddWaitStep("wait a bit", 3);
AddUntilStep("conflict popover not shown", () => panel.ChildrenOfType<KeyBindingConflictPopover>().SingleOrDefault(), () => Is.Null);
}
private void checkBinding(string name, string keyName)
{
AddAssert($"Check {name} is bound to {keyName}", () =>

View File

@ -0,0 +1,60 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Bindings;
using osu.Framework.Testing;
using osu.Game.Input.Bindings;
using osu.Game.Overlays;
using osu.Game.Overlays.Settings.Sections.Input;
namespace osu.Game.Tests.Visual.Settings
{
public partial class TestSceneKeyBindingRow : OsuTestScene
{
[Cached]
private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Aquamarine);
[Test]
public void TestChangesAfterConstruction()
{
KeyBindingRow row = null!;
AddStep("create row", () => Child = new Container
{
Width = 500,
AutoSizeAxes = Axes.Y,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Child = row = new KeyBindingRow(GlobalAction.Back)
{
Defaults = new[]
{
new KeyCombination(InputKey.Escape),
new KeyCombination(InputKey.ExtraMouseButton1)
}
}
});
AddStep("change key bindings", () =>
{
row.KeyBindings.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.Escape)));
row.KeyBindings.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.ExtraMouseButton1)));
});
AddUntilStep("revert to default button not shown", () => row.ChildrenOfType<RevertToDefaultButton<bool>>().Single().Alpha, () => Is.Zero);
AddStep("change key bindings", () =>
{
row.KeyBindings.Clear();
row.KeyBindings.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.X)));
row.KeyBindings.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.Z)));
row.KeyBindings.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.I)));
});
AddUntilStep("revert to default button not shown", () => row.ChildrenOfType<RevertToDefaultButton<bool>>().Single().Alpha, () => Is.Not.Zero);
}
}
}

View File

@ -64,6 +64,26 @@ namespace osu.Game.Localisation
/// </summary>
public static LocalisableString KeyBindingPanelDescription => new TranslatableString(getKey(@"key_binding_panel_description"), @"Customise your keys!");
/// <summary>
/// "The binding you&#39;ve selected conflicts with another existing binding."
/// </summary>
public static LocalisableString KeyBindingConflictDetected => new TranslatableString(getKey(@"key_binding_conflict_detected"), @"The binding you've selected conflicts with another existing binding.");
/// <summary>
/// "Keep existing"
/// </summary>
public static LocalisableString KeepExistingBinding => new TranslatableString(getKey(@"keep_existing_binding"), @"Keep existing");
/// <summary>
/// "Apply new"
/// </summary>
public static LocalisableString ApplyNewBinding => new TranslatableString(getKey(@"apply_new_binding"), @"Apply new");
/// <summary>
/// "(none)"
/// </summary>
public static LocalisableString ActionHasNoKeyBinding => new TranslatableString(getKey(@"action_has_no_key_binding"), @"(none)");
private static string getKey(string key) => $"{prefix}:{key}";
}
}

View File

@ -0,0 +1,299 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Input.Bindings;
using osu.Game.Localisation;
using osuTK;
namespace osu.Game.Overlays.Settings.Sections.Input
{
public partial class KeyBindingConflictPopover : OsuPopover
{
public Action? BindingConflictResolved { get; init; }
private ConflictingKeyBindingPreview newPreview = null!;
private ConflictingKeyBindingPreview existingPreview = null!;
private HoverableRoundedButton keepExistingButton = null!;
private HoverableRoundedButton applyNewButton = null!;
[Resolved]
private RealmAccess realm { get; set; } = null!;
[Resolved]
private OsuColour colours { get; set; } = null!;
private readonly KeyBindingRow.KeyBindingConflictInfo conflictInfo;
public KeyBindingConflictPopover(KeyBindingRow.KeyBindingConflictInfo conflictInfo)
{
this.conflictInfo = conflictInfo;
}
[BackgroundDependencyLoader]
private void load()
{
Child = new FillFlowContainer
{
Width = 250,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(10),
Children = new Drawable[]
{
new OsuTextFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Text = InputSettingsStrings.KeyBindingConflictDetected,
Margin = new MarginPadding { Bottom = 10 }
},
existingPreview = new ConflictingKeyBindingPreview(
conflictInfo.Existing.Action,
conflictInfo.Existing.CombinationWhenChosen,
conflictInfo.Existing.CombinationWhenNotChosen),
newPreview = new ConflictingKeyBindingPreview(
conflictInfo.New.Action,
conflictInfo.New.CombinationWhenChosen,
conflictInfo.New.CombinationWhenNotChosen),
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Margin = new MarginPadding { Top = 10 },
Children = new[]
{
keepExistingButton = new HoverableRoundedButton
{
Text = InputSettingsStrings.KeepExistingBinding,
RelativeSizeAxes = Axes.X,
Width = 0.48f,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Action = Hide
},
applyNewButton = new HoverableRoundedButton
{
Text = InputSettingsStrings.ApplyNewBinding,
BackgroundColour = colours.Pink3,
RelativeSizeAxes = Axes.X,
Width = 0.48f,
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Action = applyNew
}
}
}
}
};
}
private void applyNew()
{
// only "apply new" needs to cause actual realm changes, since the flow in `KeyBindingsSubsection` does not actually make db changes
// if it detects a binding conflict.
// the temporary visual changes will be reverted by calling `Hide()` / `BindingConflictResolved`.
realm.Write(r =>
{
var existingBinding = r.Find<RealmKeyBinding>(conflictInfo.Existing.ID);
existingBinding!.KeyCombinationString = conflictInfo.Existing.CombinationWhenNotChosen.ToString();
var newBinding = r.Find<RealmKeyBinding>(conflictInfo.New.ID);
newBinding!.KeyCombinationString = conflictInfo.Existing.CombinationWhenChosen.ToString();
});
Hide();
}
protected override void PopOut()
{
base.PopOut();
// workaround for `VisibilityContainer.PopOut()` being called in `LoadAsyncComplete()`
if (IsLoaded)
BindingConflictResolved?.Invoke();
}
protected override void LoadComplete()
{
base.LoadComplete();
keepExistingButton.IsHoveredBindable.BindValueChanged(_ => updatePreviews());
applyNewButton.IsHoveredBindable.BindValueChanged(_ => updatePreviews());
updatePreviews();
}
private void updatePreviews()
{
if (!keepExistingButton.IsHovered && !applyNewButton.IsHovered)
{
existingPreview.IsChosen.Value = newPreview.IsChosen.Value = null;
return;
}
existingPreview.IsChosen.Value = keepExistingButton.IsHovered;
newPreview.IsChosen.Value = applyNewButton.IsHovered;
}
private partial class ConflictingKeyBindingPreview : CompositeDrawable
{
private readonly object action;
private readonly KeyCombination combinationWhenChosen;
private readonly KeyCombination combinationWhenNotChosen;
private OsuSpriteText newBindingText = null!;
public Bindable<bool?> IsChosen { get; } = new Bindable<bool?>();
[Resolved]
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } = null!;
[Resolved]
private OsuColour colours { get; set; } = null!;
public ConflictingKeyBindingPreview(object action, KeyCombination combinationWhenChosen, KeyCombination combinationWhenNotChosen)
{
this.action = action;
this.combinationWhenChosen = combinationWhenChosen;
this.combinationWhenNotChosen = combinationWhenNotChosen;
}
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChild = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
CornerRadius = 5,
Masking = true,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background5
},
new GridContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) },
ColumnDimensions = new[]
{
new Dimension(),
new Dimension(GridSizeMode.AutoSize),
},
Content = new[]
{
new Drawable[]
{
new OsuSpriteText
{
Text = action.GetLocalisableDescription(),
Margin = new MarginPadding(7.5f),
},
new Container
{
AutoSizeAxes = Axes.Both,
CornerRadius = 5,
Masking = true,
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
X = -5,
Children = new[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background6
},
Empty().With(d => d.Width = 80), // poor man's min-width
newBindingText = new OsuSpriteText
{
Font = OsuFont.Numeric.With(size: 10),
Margin = new MarginPadding(5),
Anchor = Anchor.Centre,
Origin = Anchor.Centre
}
}
},
}
}
}
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
IsChosen.BindValueChanged(_ => updateState(), true);
}
private void updateState()
{
LocalisableString keyCombinationText;
switch (IsChosen.Value)
{
case true:
keyCombinationText = keyCombinationProvider.GetReadableString(combinationWhenChosen);
newBindingText.Colour = colours.Green1;
break;
case false:
keyCombinationText = keyCombinationProvider.GetReadableString(combinationWhenNotChosen);
newBindingText.Colour = colours.Red1;
break;
case null:
keyCombinationText = keyCombinationProvider.GetReadableString(combinationWhenChosen);
newBindingText.Colour = Colour4.White;
break;
}
if (LocalisableString.IsNullOrEmpty(keyCombinationText))
keyCombinationText = InputSettingsStrings.ActionHasNoKeyBinding;
newBindingText.Text = keyCombinationText;
}
}
private partial class HoverableRoundedButton : RoundedButton
{
public BindableBool IsHoveredBindable { get; set; } = new BindableBool();
protected override bool OnHover(HoverEvent e)
{
IsHoveredBindable.Value = IsHovered;
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
IsHoveredBindable.Value = IsHovered;
base.OnHoverLost(e);
}
}
}
}

View File

@ -9,7 +9,6 @@ using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects;
@ -19,15 +18,13 @@ using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Input;
using osu.Game.Input.Bindings;
using osu.Game.Resources.Localisation.Web;
using osu.Game.Rulesets;
using osuTK;
using osuTK.Graphics;
using osuTK.Input;
namespace osu.Game.Overlays.Settings.Sections.Input
@ -37,13 +34,22 @@ namespace osu.Game.Overlays.Settings.Sections.Input
/// <summary>
/// Invoked when the binding of this row is updated with a change being written.
/// </summary>
public Action<KeyBindingRow>? BindingUpdated { get; set; }
public KeyBindingUpdated? BindingUpdated { get; set; }
public delegate void KeyBindingUpdated(KeyBindingRow sender, KeyBindingUpdatedEventArgs args);
public Func<List<RealmKeyBinding>> GetAllSectionBindings { get; set; } = null!;
/// <summary>
/// Whether left and right mouse button clicks should be included in the edited bindings.
/// </summary>
public bool AllowMainMouseButtons { get; init; }
/// <summary>
/// The bindings to display in this row.
/// </summary>
public BindableList<RealmKeyBinding> KeyBindings { get; } = new BindableList<RealmKeyBinding>();
/// <summary>
/// The default key bindings for this row.
/// </summary>
@ -65,20 +71,22 @@ namespace osu.Game.Overlays.Settings.Sections.Input
public bool FilteringActive { get; set; }
public IEnumerable<LocalisableString> FilterTerms => bindings.Select(b => (LocalisableString)keyCombinationProvider.GetReadableString(b.KeyCombination)).Prepend(text.Text);
public IEnumerable<LocalisableString> FilterTerms => KeyBindings.Select(b => (LocalisableString)keyCombinationProvider.GetReadableString(b.KeyCombination)).Prepend(text.Text);
#endregion
private readonly object action;
private readonly IEnumerable<RealmKeyBinding> bindings;
public readonly object Action;
private Bindable<bool> isDefault { get; } = new BindableBool(true);
[Resolved]
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } = null!;
private RealmAccess realm { get; set; } = null!;
[Resolved]
private RealmAccess realm { get; set; } = null!;
private RulesetStore rulesets { get; set; } = null!;
[Resolved]
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } = null!;
private Container content = null!;
@ -101,11 +109,9 @@ namespace osu.Game.Overlays.Settings.Sections.Input
/// Creates a new <see cref="KeyBindingRow"/>.
/// </summary>
/// <param name="action">The action that this row contains bindings for.</param>
/// <param name="bindings">The keybindings to display in this row.</param>
public KeyBindingRow(object action, List<RealmKeyBinding> bindings)
public KeyBindingRow(object action)
{
this.action = action;
this.bindings = bindings;
Action = action;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
@ -161,7 +167,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input
},
text = new OsuSpriteText
{
Text = action.GetLocalisableDescription(),
Text = Action.GetLocalisableDescription(),
Margin = new MarginPadding(1.5f * padding),
},
buttons = new FillFlowContainer<KeyButton>
@ -191,10 +197,11 @@ namespace osu.Game.Overlays.Settings.Sections.Input
}
};
foreach (var b in bindings)
buttons.Add(new KeyButton(b));
updateIsDefaultValue();
KeyBindings.BindCollectionChanged((_, _) =>
{
Scheduler.AddOnce(updateButtons);
updateIsDefaultValue();
}, true);
}
public void RestoreDefaults()
@ -206,7 +213,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input
var button = buttons[i++];
button.UpdateKeyCombination(d);
updateStoreFromButton(button);
tryPersistKeyBinding(button.KeyBinding.Value, advanceToNextBinding: false);
}
isDefault.Value = true;
@ -226,8 +233,6 @@ namespace osu.Game.Overlays.Settings.Sections.Input
base.OnHoverLost(e);
}
private bool isModifier(Key k) => k < Key.F1;
protected override bool OnClick(ClickEvent e) => true;
protected override bool OnMouseDown(MouseDownEvent e)
@ -300,6 +305,8 @@ namespace osu.Game.Overlays.Settings.Sections.Input
if (!isModifier(e.Key)) finalise();
return true;
bool isModifier(Key k) => k < Key.F1;
}
protected override void OnKeyUp(KeyUpEvent e)
@ -409,6 +416,18 @@ namespace osu.Game.Overlays.Settings.Sections.Input
finalise();
}
private void updateButtons()
{
if (buttons.Count > KeyBindings.Count)
buttons.RemoveRange(buttons.Skip(KeyBindings.Count).ToArray(), true);
while (buttons.Count < KeyBindings.Count)
buttons.Add(new KeyButton());
foreach (var (button, binding) in buttons.Zip(KeyBindings))
button.KeyBinding.Value = binding;
}
private void clear()
{
if (bindTarget == null)
@ -418,21 +437,19 @@ namespace osu.Game.Overlays.Settings.Sections.Input
finalise(false);
}
private void finalise(bool hasChanged = true)
private void finalise(bool advanceToNextBinding = true)
{
if (bindTarget != null)
{
updateStoreFromButton(bindTarget);
updateIsDefaultValue();
bindTarget.IsBinding = false;
var bindingToPersist = bindTarget.KeyBinding.Value;
Schedule(() =>
{
// schedule to ensure we don't instantly get focus back on next OnMouseClick (see AcceptFocus impl.)
bindTarget = null;
if (hasChanged)
BindingUpdated?.Invoke(this);
tryPersistKeyBinding(bindingToPersist, advanceToNextBinding);
});
}
@ -461,6 +478,28 @@ namespace osu.Game.Overlays.Settings.Sections.Input
base.OnFocusLost(e);
}
private void tryPersistKeyBinding(RealmKeyBinding keyBinding, bool advanceToNextBinding)
{
List<RealmKeyBinding> bindings = GetAllSectionBindings();
RealmKeyBinding? existingBinding = keyBinding.KeyCombination.Equals(new KeyCombination(InputKey.None))
? null
: bindings.FirstOrDefault(other => other.ID != keyBinding.ID && other.KeyCombination.Equals(keyBinding.KeyCombination));
if (existingBinding == null)
{
realm.WriteAsync(r => r.Find<RealmKeyBinding>(keyBinding.ID)!.KeyCombinationString = keyBinding.KeyCombination.ToString());
BindingUpdated?.Invoke(this, new KeyBindingUpdatedEventArgs(bindingConflictResolved: false, advanceToNextBinding));
return;
}
var keyBindingBeforeUpdate = bindings.Single(other => other.ID == keyBinding.ID);
showBindingConflictPopover(
new KeyBindingConflictInfo(
new ConflictingKeyBinding(existingBinding.ID, existingBinding.GetAction(rulesets), existingBinding.KeyCombination, new KeyCombination(InputKey.None)),
new ConflictingKeyBinding(keyBindingBeforeUpdate.ID, Action, keyBinding.KeyCombination, keyBindingBeforeUpdate.KeyCombination)));
}
/// <summary>
/// Updates the bind target to the currently hovered key button or the first if clicked anywhere else.
/// </summary>
@ -471,12 +510,9 @@ namespace osu.Game.Overlays.Settings.Sections.Input
if (bindTarget != null) bindTarget.IsBinding = true;
}
private void updateStoreFromButton(KeyButton button) =>
realm.WriteAsync(r => r.Find<RealmKeyBinding>(button.KeyBinding.ID)!.KeyCombinationString = button.KeyBinding.KeyCombinationString);
private void updateIsDefaultValue()
{
isDefault.Value = bindings.Select(b => b.KeyCombination).SequenceEqual(Defaults);
isDefault.Value = KeyBindings.Select(b => b.KeyCombination).SequenceEqual(Defaults);
}
private partial class CancelButton : RoundedButton
@ -496,144 +532,5 @@ namespace osu.Game.Overlays.Settings.Sections.Input
Size = new Vector2(80, 20);
}
}
public partial class KeyButton : Container
{
public readonly RealmKeyBinding KeyBinding;
private readonly Box box;
public readonly OsuSpriteText Text;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
[Resolved]
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } = null!;
private bool isBinding;
public bool IsBinding
{
get => isBinding;
set
{
if (value == isBinding) return;
isBinding = value;
updateHoverState();
}
}
public KeyButton(RealmKeyBinding keyBinding)
{
if (keyBinding.IsManaged)
throw new ArgumentException("Key binding should not be attached as we make temporary changes", nameof(keyBinding));
KeyBinding = keyBinding;
Margin = new MarginPadding(padding);
Masking = true;
CornerRadius = padding;
Height = height;
AutoSizeAxes = Axes.X;
Children = new Drawable[]
{
new Container
{
AlwaysPresent = true,
Width = 80,
Height = height,
},
box = new Box
{
RelativeSizeAxes = Axes.Both,
},
Text = new OsuSpriteText
{
Font = OsuFont.Numeric.With(size: 10),
Margin = new MarginPadding(5),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
new HoverSounds()
};
}
protected override void LoadComplete()
{
base.LoadComplete();
keyCombinationProvider.KeymapChanged += updateKeyCombinationText;
updateKeyCombinationText();
}
[BackgroundDependencyLoader]
private void load()
{
updateHoverState();
}
protected override bool OnHover(HoverEvent e)
{
updateHoverState();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
updateHoverState();
base.OnHoverLost(e);
}
private void updateHoverState()
{
if (isBinding)
{
box.FadeColour(colourProvider.Light2, transition_time, Easing.OutQuint);
Text.FadeColour(Color4.Black, transition_time, Easing.OutQuint);
}
else
{
box.FadeColour(IsHovered ? colourProvider.Light4 : colourProvider.Background6, transition_time, Easing.OutQuint);
Text.FadeColour(IsHovered ? Color4.Black : Color4.White, transition_time, Easing.OutQuint);
}
}
/// <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))
return;
KeyBinding.KeyCombination = newCombination;
updateKeyCombinationText();
}
private void updateKeyCombinationText()
{
Scheduler.AddOnce(updateText);
void updateText() => Text.Text = keyCombinationProvider.GetReadableString(KeyBinding.KeyCombination);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (keyCombinationProvider.IsNotNull())
keyCombinationProvider.KeymapChanged -= updateKeyCombinationText;
}
}
}
}

View File

@ -0,0 +1,78 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Diagnostics;
using osu.Framework.Extensions;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Bindings;
namespace osu.Game.Overlays.Settings.Sections.Input
{
public partial class KeyBindingRow : IHasPopover
{
private KeyBindingConflictInfo? pendingKeyBindingConflict;
public Popover GetPopover()
{
Debug.Assert(pendingKeyBindingConflict != null);
return new KeyBindingConflictPopover(pendingKeyBindingConflict)
{
BindingConflictResolved = () => BindingUpdated?.Invoke(this, new KeyBindingUpdatedEventArgs(bindingConflictResolved: true, canAdvanceToNextBinding: false))
};
}
private void showBindingConflictPopover(KeyBindingConflictInfo conflictInfo)
{
pendingKeyBindingConflict = conflictInfo;
this.ShowPopover();
}
/// <summary>
/// Contains information about the key binding conflict to be resolved.
/// </summary>
public class KeyBindingConflictInfo
{
public ConflictingKeyBinding Existing { get; }
public ConflictingKeyBinding New { get; }
/// <summary>
/// Contains information about the key binding conflict to be resolved.
/// </summary>
public KeyBindingConflictInfo(ConflictingKeyBinding existingBinding, ConflictingKeyBinding newBinding)
{
Existing = existingBinding;
New = newBinding;
}
}
public class ConflictingKeyBinding
{
public Guid ID { get; }
public object Action { get; }
public KeyCombination CombinationWhenChosen { get; }
public KeyCombination CombinationWhenNotChosen { get; }
public ConflictingKeyBinding(Guid id, object action, KeyCombination combinationWhenChosen, KeyCombination combinationWhenNotChosen)
{
ID = id;
Action = action;
CombinationWhenChosen = combinationWhenChosen;
CombinationWhenNotChosen = combinationWhenNotChosen;
}
}
public class KeyBindingUpdatedEventArgs
{
public bool BindingConflictResolved { get; }
public bool CanAdvanceToNextBinding { get; }
public KeyBindingUpdatedEventArgs(bool bindingConflictResolved, bool canAdvanceToNextBinding)
{
BindingConflictResolved = bindingConflictResolved;
CanAdvanceToNextBinding = canAdvanceToNextBinding;
}
}
}
}

View File

@ -0,0 +1,168 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Input;
using osu.Game.Input.Bindings;
using osuTK.Graphics;
namespace osu.Game.Overlays.Settings.Sections.Input
{
public partial class KeyBindingRow
{
public partial class KeyButton : Container
{
public Bindable<RealmKeyBinding> KeyBinding { get; } = new Bindable<RealmKeyBinding>();
private readonly Box box;
public readonly OsuSpriteText Text;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
[Resolved]
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } = null!;
private bool isBinding;
public bool IsBinding
{
get => isBinding;
set
{
if (value == isBinding) return;
isBinding = value;
updateHoverState();
}
}
public KeyButton()
{
Margin = new MarginPadding(padding);
Masking = true;
CornerRadius = padding;
Height = height;
AutoSizeAxes = Axes.X;
Children = new Drawable[]
{
new Container
{
AlwaysPresent = true,
Width = 80,
Height = height,
},
box = new Box
{
RelativeSizeAxes = Axes.Both,
},
Text = new OsuSpriteText
{
Font = OsuFont.Numeric.With(size: 10),
Margin = new MarginPadding(5),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
new HoverSounds()
};
}
protected override void LoadComplete()
{
base.LoadComplete();
KeyBinding.BindValueChanged(_ =>
{
if (KeyBinding.Value.IsManaged)
throw new ArgumentException("Key binding should not be attached as we make temporary changes", nameof(KeyBinding));
updateKeyCombinationText();
});
keyCombinationProvider.KeymapChanged += updateKeyCombinationText;
updateKeyCombinationText();
}
[BackgroundDependencyLoader]
private void load()
{
updateHoverState();
FinishTransforms(true);
}
protected override bool OnHover(HoverEvent e)
{
updateHoverState();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
updateHoverState();
base.OnHoverLost(e);
}
private void updateHoverState()
{
if (isBinding)
{
box.FadeColour(colourProvider.Light2, transition_time, Easing.OutQuint);
Text.FadeColour(Color4.Black, transition_time, Easing.OutQuint);
}
else
{
box.FadeColour(IsHovered ? colourProvider.Light4 : colourProvider.Background6, transition_time, Easing.OutQuint);
Text.FadeColour(IsHovered ? Color4.Black : Color4.White, transition_time, Easing.OutQuint);
}
}
/// <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.Value.RulesetName != null && !RealmKeyBindingStore.CheckValidForGameplay(newCombination))
return;
KeyBinding.Value.KeyCombination = newCombination;
updateKeyCombinationText();
}
private void updateKeyCombinationText()
{
Scheduler.AddOnce(updateText);
void updateText() => Text.Text = keyCombinationProvider.GetReadableString(KeyBinding.Value.KeyCombination);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (keyCombinationProvider.IsNotNull())
keyCombinationProvider.KeymapChanged -= updateKeyCombinationText;
}
}
}
}

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Input.Bindings;
using osu.Framework.Localisation;
@ -27,46 +26,83 @@ namespace osu.Game.Overlays.Settings.Sections.Input
protected IEnumerable<KeyBinding> Defaults { get; init; } = Array.Empty<KeyBinding>();
[Resolved]
private RealmAccess realm { get; set; } = null!;
protected KeyBindingsSubsection()
{
FlowContent.Spacing = new Vector2(0, 3);
}
[BackgroundDependencyLoader]
private void load(RealmAccess realm)
private void load()
{
var bindings = realm.Run(r => GetKeyBindings(r).Detach());
var bindings = getAllBindings();
foreach (var defaultGroup in Defaults.GroupBy(d => d.Action))
{
int intKey = (int)defaultGroup.Key;
// one row per valid action.
Add(CreateKeyBindingRow(
defaultGroup.Key,
bindings.Where(b => b.ActionInt.Equals(intKey)).ToList(),
defaultGroup)
.With(row => row.BindingUpdated = onBindingUpdated));
var row = CreateKeyBindingRow(defaultGroup.Key, defaultGroup)
.With(row =>
{
row.BindingUpdated = onBindingUpdated;
row.GetAllSectionBindings = getAllBindings;
});
row.KeyBindings.AddRange(bindings.Where(b => b.ActionInt.Equals(intKey)));
Add(row);
}
Add(new ResetButton
{
Action = () => Children.OfType<KeyBindingRow>().ForEach(k => k.RestoreDefaults())
Action = () =>
{
realm.Write(r =>
{
// can't use `RestoreDefaults()` for each key binding row here as it might trigger binding conflicts along the way.
foreach (var row in Children.OfType<KeyBindingRow>())
{
foreach (var (currentBinding, defaultBinding) in row.KeyBindings.Zip(row.Defaults))
r.Find<RealmKeyBinding>(currentBinding.ID)!.KeyCombinationString = defaultBinding.ToString();
}
});
reloadAllBindings();
}
});
}
protected abstract IEnumerable<RealmKeyBinding> GetKeyBindings(Realm realm);
protected virtual KeyBindingRow CreateKeyBindingRow(object action, IEnumerable<RealmKeyBinding> keyBindings, IEnumerable<KeyBinding> defaults)
=> new KeyBindingRow(action, keyBindings.ToList())
private List<RealmKeyBinding> getAllBindings() => realm.Run(r =>
{
r.Refresh();
return GetKeyBindings(r).Detach();
});
protected virtual KeyBindingRow CreateKeyBindingRow(object action, IEnumerable<KeyBinding> defaults)
=> new KeyBindingRow(action)
{
AllowMainMouseButtons = false,
Defaults = defaults.Select(d => d.KeyCombination),
};
private void onBindingUpdated(KeyBindingRow sender)
private void reloadAllBindings()
{
if (AutoAdvanceTarget)
var bindings = getAllBindings();
foreach (var row in Children.OfType<KeyBindingRow>())
{
row.KeyBindings.Clear();
row.KeyBindings.AddRange(bindings.Where(b => b.ActionInt.Equals((int)row.Action)));
}
}
private void onBindingUpdated(KeyBindingRow sender, KeyBindingRow.KeyBindingUpdatedEventArgs args)
{
if (args.BindingConflictResolved)
reloadAllBindings();
if (AutoAdvanceTarget && args.CanAdvanceToNextBinding)
{
var next = Children.SkipWhile(c => c != sender).Skip(1).FirstOrDefault();
if (next != null)

View File

@ -39,8 +39,8 @@ namespace osu.Game.Overlays.Settings.Sections.Input
.Where(b => b.RulesetName == rulesetName && b.Variant == variant);
}
protected override KeyBindingRow CreateKeyBindingRow(object action, IEnumerable<RealmKeyBinding> keyBindings, IEnumerable<KeyBinding> defaults)
=> new KeyBindingRow(action, keyBindings.ToList())
protected override KeyBindingRow CreateKeyBindingRow(object action, IEnumerable<KeyBinding> defaults)
=> new KeyBindingRow(action)
{
AllowMainMouseButtons = true,
Defaults = defaults.Select(d => d.KeyCombination),

View File

@ -14,6 +14,7 @@ using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shapes;
@ -106,39 +107,43 @@ namespace osu.Game.Overlays
}
};
Add(SectionsContainer = new SettingsSectionsContainer
Add(new PopoverContainer
{
Masking = true,
EdgeEffect = new EdgeEffectParameters
{
Colour = Color4.Black.Opacity(0),
Type = EdgeEffectType.Shadow,
Hollow = true,
Radius = 10
},
MaskingSmoothness = 0,
RelativeSizeAxes = Axes.Both,
ExpandableHeader = CreateHeader(),
SelectedSection = { BindTarget = CurrentSection },
FixedHeader = new Container
Child = SectionsContainer = new SettingsSectionsContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
Masking = true,
EdgeEffect = new EdgeEffectParameters
{
Vertical = 20,
Horizontal = CONTENT_MARGINS
Colour = Color4.Black.Opacity(0),
Type = EdgeEffectType.Shadow,
Hollow = true,
Radius = 10
},
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Child = searchTextBox = new SeekLimitedSearchTextBox
MaskingSmoothness = 0,
RelativeSizeAxes = Axes.Both,
ExpandableHeader = CreateHeader(),
SelectedSection = { BindTarget = CurrentSection },
FixedHeader = new Container
{
RelativeSizeAxes = Axes.X,
Origin = Anchor.TopCentre,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
Vertical = 20,
Horizontal = CONTENT_MARGINS
},
Anchor = Anchor.TopCentre,
}
},
Footer = CreateFooter().With(f => f.Alpha = 0)
Origin = Anchor.TopCentre,
Child = searchTextBox = new SeekLimitedSearchTextBox
{
RelativeSizeAxes = Axes.X,
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
}
},
Footer = CreateFooter().With(f => f.Alpha = 0)
}
});
if (showSidebar)