From c3069ad002cb4e4a298b0a0da1e9419a6bb3c08a Mon Sep 17 00:00:00 2001 From: Susko3 <16479013+Susko3@users.noreply.github.com> Date: Mon, 8 Nov 2021 06:55:26 +0100 Subject: [PATCH 1/7] Change to use `ReadableKeyCombinationProvider` Changes all usages of `KeyCombination.ReadableString()` to `ReadableKeyCombinationProvider.GetReadableString()`. Subscribing to `KeymapChanged` is only required in `KeyButton`. All other places query `GetReadableString()` every time. --- .../Database/TestRealmKeyBindingStore.cs | 3 +- osu.Game/Input/RealmKeyBindingStore.cs | 7 ++-- osu.Game/OsuGameBase.cs | 4 +-- .../Settings/Sections/Input/KeyBindingRow.cs | 32 +++++++++++++++++-- osu.Game/Overlays/Toolbar/ToolbarButton.cs | 6 +++- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/osu.Game.Tests/Database/TestRealmKeyBindingStore.cs b/osu.Game.Tests/Database/TestRealmKeyBindingStore.cs index f10b11733e..860828ae81 100644 --- a/osu.Game.Tests/Database/TestRealmKeyBindingStore.cs +++ b/osu.Game.Tests/Database/TestRealmKeyBindingStore.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using NUnit.Framework; +using osu.Framework.Input; using osu.Framework.Input.Bindings; using osu.Framework.Platform; using osu.Game.Database; @@ -33,7 +34,7 @@ namespace osu.Game.Tests.Database storage = new NativeStorage(directory.FullName); realmContextFactory = new RealmContextFactory(storage, "test"); - keyBindingStore = new RealmKeyBindingStore(realmContextFactory); + keyBindingStore = new RealmKeyBindingStore(realmContextFactory, new ReadableKeyCombinationProvider()); } [Test] diff --git a/osu.Game/Input/RealmKeyBindingStore.cs b/osu.Game/Input/RealmKeyBindingStore.cs index c65e36e478..677a7baf62 100644 --- a/osu.Game/Input/RealmKeyBindingStore.cs +++ b/osu.Game/Input/RealmKeyBindingStore.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; +using osu.Framework.Input; using osu.Framework.Input.Bindings; using osu.Game.Database; using osu.Game.Input.Bindings; @@ -16,10 +17,12 @@ namespace osu.Game.Input public class RealmKeyBindingStore { private readonly RealmContextFactory realmFactory; + private readonly ReadableKeyCombinationProvider readableKeyCombinationProvider; - public RealmKeyBindingStore(RealmContextFactory realmFactory) + public RealmKeyBindingStore(RealmContextFactory realmFactory, ReadableKeyCombinationProvider readableKeyCombinationProvider) { this.realmFactory = realmFactory; + this.readableKeyCombinationProvider = readableKeyCombinationProvider; } /// @@ -35,7 +38,7 @@ namespace osu.Game.Input { foreach (var action in context.All().Where(b => b.RulesetID == null && (GlobalAction)b.ActionInt == globalAction)) { - string str = action.KeyCombination.ReadableString(); + string str = readableKeyCombinationProvider.GetReadableString(action.KeyCombination); // even if found, the readable string may be empty for an unbound action. if (str.Length > 0) diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index f6ec22a536..f23a0433e6 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -167,7 +167,7 @@ namespace osu.Game } [BackgroundDependencyLoader] - private void load() + private void load(ReadableKeyCombinationProvider readableKeyCombinationProvider) { try { @@ -316,7 +316,7 @@ namespace osu.Game base.Content.Add(CreateScalingContainer().WithChildren(mainContent)); - KeyBindingStore = new RealmKeyBindingStore(realmFactory); + KeyBindingStore = new RealmKeyBindingStore(realmFactory, readableKeyCombinationProvider); KeyBindingStore.Register(globalBindings, RulesetStore.AvailableRulesets); dependencies.Cache(globalBindings); diff --git a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs index f44f02d0ed..758f93692e 100644 --- a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs +++ b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs @@ -12,6 +12,7 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Effects; using osu.Framework.Graphics.Shapes; +using osu.Framework.Input; using osu.Framework.Input.Bindings; using osu.Framework.Input.Events; using osu.Game.Database; @@ -57,13 +58,16 @@ namespace osu.Game.Overlays.Settings.Sections.Input public bool FilteringActive { get; set; } + [Resolved] + private ReadableKeyCombinationProvider readableKeyCombinationProvider { get; set; } + private OsuSpriteText text; private FillFlowContainer cancelAndClearButtons; private FillFlowContainer buttons; private Bindable isDefault { get; } = new BindableBool(true); - public IEnumerable FilterTerms => bindings.Select(b => b.KeyCombination.ReadableString()).Prepend(text.Text.ToString()); + public IEnumerable FilterTerms => bindings.Select(b => readableKeyCombinationProvider.GetReadableString(b.KeyCombination)).Prepend(text.Text.ToString()); public KeyBindingRow(object action, List bindings) { @@ -422,6 +426,9 @@ namespace osu.Game.Overlays.Settings.Sections.Input [Resolved] private OverlayColourProvider colourProvider { get; set; } + [Resolved] + private ReadableKeyCombinationProvider readableKeyCombinationProvider { get; set; } + private bool isBinding; public bool IsBinding @@ -470,12 +477,19 @@ namespace osu.Game.Overlays.Settings.Sections.Input Margin = new MarginPadding(5), Anchor = Anchor.Centre, Origin = Anchor.Centre, - Text = keyBinding.KeyCombination.ReadableString(), }, new HoverSounds() }; } + protected override void LoadComplete() + { + base.LoadComplete(); + + readableKeyCombinationProvider.KeymapChanged += updateKeyCombinationText; + updateKeyCombinationText(); + } + [BackgroundDependencyLoader] private void load() { @@ -508,13 +522,25 @@ namespace osu.Game.Overlays.Settings.Sections.Input } } + private void updateKeyCombinationText() + { + Text.Text = readableKeyCombinationProvider.GetReadableString(KeyBinding.KeyCombination); + } + public void UpdateKeyCombination(KeyCombination newCombination) { if (KeyBinding.RulesetID != null && !RealmKeyBindingStore.CheckValidForGameplay(newCombination)) return; KeyBinding.KeyCombination = newCombination; - Text.Text = KeyBinding.KeyCombination.ReadableString(); + updateKeyCombinationText(); + } + + protected override void Dispose(bool isDisposing) + { + base.Dispose(isDisposing); + + readableKeyCombinationProvider.KeymapChanged -= updateKeyCombinationText; } } } diff --git a/osu.Game/Overlays/Toolbar/ToolbarButton.cs b/osu.Game/Overlays/Toolbar/ToolbarButton.cs index ab37b3b355..0886bb8f8c 100644 --- a/osu.Game/Overlays/Toolbar/ToolbarButton.cs +++ b/osu.Game/Overlays/Toolbar/ToolbarButton.cs @@ -11,6 +11,7 @@ using osu.Framework.Graphics.Effects; using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Textures; +using osu.Framework.Input; using osu.Framework.Input.Bindings; using osu.Framework.Input.Events; using osu.Game.Database; @@ -39,6 +40,9 @@ namespace osu.Game.Overlays.Toolbar [Resolved] private TextureStore textures { get; set; } + [Resolved] + private ReadableKeyCombinationProvider readableKeyCombinationProvider { get; set; } + public void SetIcon(string texture) => SetIcon(new Sprite { @@ -207,7 +211,7 @@ namespace osu.Game.Overlays.Toolbar if (realmKeyBinding != null) { - string keyBindingString = realmKeyBinding.KeyCombination.ReadableString(); + string keyBindingString = readableKeyCombinationProvider.GetReadableString(realmKeyBinding.KeyCombination); if (!string.IsNullOrEmpty(keyBindingString)) keyBindingTooltip.Text = $" ({keyBindingString})"; From cc286f165d9e7a693377acd8f5347d7d13e5bb31 Mon Sep 17 00:00:00 2001 From: Susko3 <16479013+Susko3@users.noreply.github.com> Date: Mon, 8 Nov 2021 06:56:57 +0100 Subject: [PATCH 2/7] Change font to default Non-english letters look tiny with size 10 and don't fit into the look. --- osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs index 758f93692e..fe0905d76f 100644 --- a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs +++ b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs @@ -473,7 +473,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input }, Text = new OsuSpriteText { - Font = OsuFont.Numeric.With(size: 10), + Font = OsuFont.Default, Margin = new MarginPadding(5), Anchor = Anchor.Centre, Origin = Anchor.Centre, From c1e7fcd2d744edc62311bd8b4e9084888cab8af2 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 8 Nov 2021 18:15:51 +0900 Subject: [PATCH 3/7] Update framework --- osu.Android.props | 2 +- osu.Game/osu.Game.csproj | 2 +- osu.iOS.props | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/osu.Android.props b/osu.Android.props index 4f9f83f199..752eb160ed 100644 --- a/osu.Android.props +++ b/osu.Android.props @@ -52,7 +52,7 @@ - + diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 7811de5764..df3c9b355a 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -36,7 +36,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/osu.iOS.props b/osu.iOS.props index 9d95b7c765..1852957e87 100644 --- a/osu.iOS.props +++ b/osu.iOS.props @@ -70,7 +70,7 @@ - + @@ -93,7 +93,7 @@ - + From 82f24b0502272f48520f259100720c1356c16d55 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 8 Nov 2021 18:16:06 +0900 Subject: [PATCH 4/7] Revert "Change font to default" This reverts commit cc286f165d9e7a693377acd8f5347d7d13e5bb31. --- osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs index fe0905d76f..758f93692e 100644 --- a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs +++ b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs @@ -473,7 +473,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input }, Text = new OsuSpriteText { - Font = OsuFont.Default, + Font = OsuFont.Numeric.With(size: 10), Margin = new MarginPadding(5), Anchor = Anchor.Centre, Origin = Anchor.Centre, From f5842e75875feaf736d376dd33dfe5e47b8d7acd Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 8 Nov 2021 18:24:37 +0900 Subject: [PATCH 5/7] Shorten variable names --- osu.Game/Input/RealmKeyBindingStore.cs | 8 ++++---- osu.Game/OsuGameBase.cs | 4 ++-- .../Settings/Sections/Input/KeyBindingRow.cs | 12 ++++++------ osu.Game/Overlays/Toolbar/ToolbarButton.cs | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/osu.Game/Input/RealmKeyBindingStore.cs b/osu.Game/Input/RealmKeyBindingStore.cs index 677a7baf62..046969579c 100644 --- a/osu.Game/Input/RealmKeyBindingStore.cs +++ b/osu.Game/Input/RealmKeyBindingStore.cs @@ -17,12 +17,12 @@ namespace osu.Game.Input public class RealmKeyBindingStore { private readonly RealmContextFactory realmFactory; - private readonly ReadableKeyCombinationProvider readableKeyCombinationProvider; + private readonly ReadableKeyCombinationProvider keyCombinationProvider; - public RealmKeyBindingStore(RealmContextFactory realmFactory, ReadableKeyCombinationProvider readableKeyCombinationProvider) + public RealmKeyBindingStore(RealmContextFactory realmFactory, ReadableKeyCombinationProvider keyCombinationProvider) { this.realmFactory = realmFactory; - this.readableKeyCombinationProvider = readableKeyCombinationProvider; + this.keyCombinationProvider = keyCombinationProvider; } /// @@ -38,7 +38,7 @@ namespace osu.Game.Input { foreach (var action in context.All().Where(b => b.RulesetID == null && (GlobalAction)b.ActionInt == globalAction)) { - string str = readableKeyCombinationProvider.GetReadableString(action.KeyCombination); + string str = keyCombinationProvider.GetReadableString(action.KeyCombination); // even if found, the readable string may be empty for an unbound action. if (str.Length > 0) diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index 0dc511c361..10166daf00 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -170,7 +170,7 @@ namespace osu.Game } [BackgroundDependencyLoader] - private void load(ReadableKeyCombinationProvider readableKeyCombinationProvider) + private void load(ReadableKeyCombinationProvider keyCombinationProvider) { try { @@ -309,7 +309,7 @@ namespace osu.Game base.Content.Add(CreateScalingContainer().WithChildren(mainContent)); - KeyBindingStore = new RealmKeyBindingStore(realmFactory, readableKeyCombinationProvider); + KeyBindingStore = new RealmKeyBindingStore(realmFactory, keyCombinationProvider); KeyBindingStore.Register(globalBindings, RulesetStore.AvailableRulesets); dependencies.Cache(globalBindings); diff --git a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs index 758f93692e..f9856f30ce 100644 --- a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs +++ b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs @@ -59,7 +59,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input public bool FilteringActive { get; set; } [Resolved] - private ReadableKeyCombinationProvider readableKeyCombinationProvider { get; set; } + private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } private OsuSpriteText text; private FillFlowContainer cancelAndClearButtons; @@ -67,7 +67,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input private Bindable isDefault { get; } = new BindableBool(true); - public IEnumerable FilterTerms => bindings.Select(b => readableKeyCombinationProvider.GetReadableString(b.KeyCombination)).Prepend(text.Text.ToString()); + public IEnumerable FilterTerms => bindings.Select(b => keyCombinationProvider.GetReadableString(b.KeyCombination)).Prepend(text.Text.ToString()); public KeyBindingRow(object action, List bindings) { @@ -427,7 +427,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input private OverlayColourProvider colourProvider { get; set; } [Resolved] - private ReadableKeyCombinationProvider readableKeyCombinationProvider { get; set; } + private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } private bool isBinding; @@ -486,7 +486,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input { base.LoadComplete(); - readableKeyCombinationProvider.KeymapChanged += updateKeyCombinationText; + keyCombinationProvider.KeymapChanged += updateKeyCombinationText; updateKeyCombinationText(); } @@ -524,7 +524,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input private void updateKeyCombinationText() { - Text.Text = readableKeyCombinationProvider.GetReadableString(KeyBinding.KeyCombination); + Text.Text = keyCombinationProvider.GetReadableString(KeyBinding.KeyCombination); } public void UpdateKeyCombination(KeyCombination newCombination) @@ -540,7 +540,7 @@ namespace osu.Game.Overlays.Settings.Sections.Input { base.Dispose(isDisposing); - readableKeyCombinationProvider.KeymapChanged -= updateKeyCombinationText; + keyCombinationProvider.KeymapChanged -= updateKeyCombinationText; } } } diff --git a/osu.Game/Overlays/Toolbar/ToolbarButton.cs b/osu.Game/Overlays/Toolbar/ToolbarButton.cs index 0886bb8f8c..b2252a5575 100644 --- a/osu.Game/Overlays/Toolbar/ToolbarButton.cs +++ b/osu.Game/Overlays/Toolbar/ToolbarButton.cs @@ -41,7 +41,7 @@ namespace osu.Game.Overlays.Toolbar private TextureStore textures { get; set; } [Resolved] - private ReadableKeyCombinationProvider readableKeyCombinationProvider { get; set; } + private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } public void SetIcon(string texture) => SetIcon(new Sprite @@ -211,7 +211,7 @@ namespace osu.Game.Overlays.Toolbar if (realmKeyBinding != null) { - string keyBindingString = readableKeyCombinationProvider.GetReadableString(realmKeyBinding.KeyCombination); + string keyBindingString = keyCombinationProvider.GetReadableString(realmKeyBinding.KeyCombination); if (!string.IsNullOrEmpty(keyBindingString)) keyBindingTooltip.Text = $" ({keyBindingString})"; From b4225804ed5c44196db5df5dd7a07552f6e24561 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 8 Nov 2021 18:26:12 +0900 Subject: [PATCH 6/7] Add missing null check --- osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs index f9856f30ce..40bf74cbf1 100644 --- a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs +++ b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs @@ -540,7 +540,8 @@ namespace osu.Game.Overlays.Settings.Sections.Input { base.Dispose(isDisposing); - keyCombinationProvider.KeymapChanged -= updateKeyCombinationText; + if (keyCombinationProvider != null) + keyCombinationProvider.KeymapChanged -= updateKeyCombinationText; } } } From 49c26a465c75aabd628561e6a117243fe2fda6d2 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 8 Nov 2021 18:29:11 +0900 Subject: [PATCH 7/7] Debounce and schedule updates to key combinations --- .../Settings/Sections/Input/KeyBindingRow.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs index 40bf74cbf1..e6472dffeb 100644 --- a/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs +++ b/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs @@ -522,11 +522,6 @@ namespace osu.Game.Overlays.Settings.Sections.Input } } - private void updateKeyCombinationText() - { - Text.Text = keyCombinationProvider.GetReadableString(KeyBinding.KeyCombination); - } - public void UpdateKeyCombination(KeyCombination newCombination) { if (KeyBinding.RulesetID != null && !RealmKeyBindingStore.CheckValidForGameplay(newCombination)) @@ -536,6 +531,13 @@ namespace osu.Game.Overlays.Settings.Sections.Input updateKeyCombinationText(); } + private void updateKeyCombinationText() + { + Scheduler.AddOnce(updateText); + + void updateText() => Text.Text = keyCombinationProvider.GetReadableString(KeyBinding.KeyCombination); + } + protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing);