Add and consume IKeyBindingStore interface

This commit is contained in:
Dean Herbert 2021-01-07 16:38:49 +09:00
parent a9a3a95991
commit 43f417b53a
9 changed files with 80 additions and 26 deletions

View File

@ -74,7 +74,7 @@ public class TestSceneOsuGame : OsuTestScene
typeof(FileStore),
typeof(ScoreManager),
typeof(BeatmapManager),
typeof(KeyBindingStore),
typeof(IKeyBindingStore),
typeof(SettingsStore),
typeof(RulesetConfigCache),
typeof(OsuColour),

View File

@ -6,7 +6,6 @@
using osu.Framework.Allocation;
using osu.Framework.Input.Bindings;
using osu.Game.Rulesets;
using System.Linq;
namespace osu.Game.Input.Bindings
{
@ -21,7 +20,8 @@ public class DatabasedKeyBindingContainer<T> : KeyBindingContainer<T>
private readonly int? variant;
private KeyBindingStore store;
[Resolved]
private IKeyBindingStore store { get; set; }
public override IEnumerable<KeyBinding> DefaultKeyBindings => ruleset.CreateInstance().GetDefaultKeyBindings(variant ?? 0);
@ -42,12 +42,6 @@ public DatabasedKeyBindingContainer(RulesetInfo ruleset = null, int? variant = n
throw new InvalidOperationException($"{nameof(variant)} can not be null when a non-null {nameof(ruleset)} is provided.");
}
[BackgroundDependencyLoader]
private void load(KeyBindingStore keyBindings)
{
store = keyBindings;
}
protected override void LoadComplete()
{
base.LoadComplete();
@ -69,7 +63,7 @@ protected override void ReloadMappings()
// fallback to defaults instead.
KeyBindings = DefaultKeyBindings;
else
KeyBindings = store.Query(ruleset?.ID, variant).ToList();
KeyBindings = store.Query(ruleset?.ID, variant);
}
}
}

View File

@ -0,0 +1,40 @@
// 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.Collections.Generic;
using osu.Framework.Input.Bindings;
using osu.Game.Input.Bindings;
namespace osu.Game.Input
{
public interface IKeyBindingStore
{
event Action KeyBindingChanged;
void Register(KeyBindingContainer manager);
/// <summary>
/// Retrieve all user-defined key combinations (in a format that can be displayed) for a specific action.
/// </summary>
/// <param name="globalAction">The action to lookup.</param>
/// <returns>A set of display strings for all the user's key configuration for the action.</returns>
IEnumerable<string> GetReadableKeyCombinationsFor(GlobalAction globalAction);
/// <summary>
/// Retrieve <see cref="DatabasedKeyBinding"/>s for a specified ruleset/variant content.
/// </summary>
/// <param name="rulesetId">The ruleset's internal ID.</param>
/// <param name="variant">An optional variant.</param>
/// <returns></returns>
List<KeyBinding> Query(int? rulesetId = null, int? variant = null);
/// <summary>
/// Retrieve <see cref="KeyBinding"/>s for the specified action.
/// </summary>
/// <param name="action">The action to lookup.</param>
List<KeyBinding> Query(GlobalAction action);
public void Update(KeyBinding buttonKeyBinding) => throw new NotImplementedException();
}
}

View File

@ -12,7 +12,7 @@
namespace osu.Game.Input
{
public class KeyBindingStore : DatabaseBackedStore
public class KeyBindingStore : DatabaseBackedStore, IKeyBindingStore
{
public event Action KeyBindingChanged;
@ -39,7 +39,7 @@ public KeyBindingStore(DatabaseContextFactory contextFactory, RulesetStore rules
/// <returns>A set of display strings for all the user's key configuration for the action.</returns>
public IEnumerable<string> GetReadableKeyCombinationsFor(GlobalAction globalAction)
{
foreach (var action in Query().Where(b => (GlobalAction)b.Action == globalAction))
foreach (var action in query().Where(b => (GlobalAction)b.Action == globalAction))
{
string str = action.KeyCombination.ReadableString();
@ -49,6 +49,12 @@ public IEnumerable<string> GetReadableKeyCombinationsFor(GlobalAction globalActi
}
}
public List<KeyBinding> Query(int? rulesetId = null, int? variant = null)
=> query(rulesetId, variant).OfType<KeyBinding>().ToList();
public List<KeyBinding> Query(GlobalAction action)
=> query(null, null).Where(dkb => (GlobalAction)dkb.Action == action).OfType<KeyBinding>().ToList();
private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null)
{
using (var usage = ContextFactory.GetForWrite())
@ -56,7 +62,7 @@ private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = n
// compare counts in database vs defaults
foreach (var group in defaults.GroupBy(k => k.Action))
{
int count = Query(rulesetId, variant).Count(k => (int)k.Action == (int)group.Key);
int count = query(rulesetId, variant).Count(k => (int)k.Action == (int)group.Key);
int aimCount = group.Count();
if (aimCount <= count)
@ -86,7 +92,7 @@ private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = n
/// <param name="rulesetId">The ruleset's internal ID.</param>
/// <param name="variant">An optional variant.</param>
/// <returns></returns>
public List<DatabasedKeyBinding> Query(int? rulesetId = null, int? variant = null) =>
private List<DatabasedKeyBinding> query(int? rulesetId = null, int? variant = null) =>
ContextFactory.Get().DatabasedKeyBinding.Where(b => b.RulesetID == rulesetId && b.Variant == variant).ToList();
public void Update(KeyBinding keyBinding)

View File

@ -12,7 +12,7 @@
namespace osu.Game.Input
{
public class RealmKeyBindingStore : RealmBackedStore
public class RealmKeyBindingStore : RealmBackedStore, IKeyBindingStore
{
public event Action KeyBindingChanged;
@ -37,7 +37,7 @@ public RealmKeyBindingStore(RealmContextFactory contextFactory, RulesetStore rul
/// <returns>A set of display strings for all the user's key configuration for the action.</returns>
public IEnumerable<string> GetReadableKeyCombinationsFor(GlobalAction globalAction)
{
foreach (var action in Query().Where(b => (GlobalAction)b.KeyBinding.Action == globalAction))
foreach (var action in query().Where(b => (GlobalAction)b.KeyBinding.Action == globalAction))
{
string str = action.KeyBinding.KeyCombination.ReadableString();
@ -56,7 +56,7 @@ private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = n
// compare counts in database vs defaults
foreach (var group in defaults.GroupBy(k => k.Action))
{
int count = Query(rulesetId, variant).Count(k => k.KeyBinding.Action == group.Key);
int count = query(rulesetId, variant).Count(k => (int)k.KeyBinding.Action == (int)group.Key);
int aimCount = group.Count();
if (aimCount <= count)
@ -87,9 +87,15 @@ private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = n
/// <param name="rulesetId">The ruleset's internal ID.</param>
/// <param name="variant">An optional variant.</param>
/// <returns></returns>
public List<RealmKeyBinding> Query(int? rulesetId = null, int? variant = null) =>
private List<RealmKeyBinding> query(int? rulesetId = null, int? variant = null) =>
ContextFactory.Get().All<RealmKeyBinding>().Where(b => b.RulesetID == rulesetId && b.Variant == variant).ToList();
public List<KeyBinding> Query(int? rulesetId = null, int? variant = null)
=> query(rulesetId, variant).Select(k => k.KeyBinding).ToList();
public List<KeyBinding> Query(GlobalAction action)
=> query(null, null).Where(rkb => rkb.KeyBindingString.StartsWith($"{(int)action}:", StringComparison.Ordinal)).Select(k => k.KeyBinding).ToList();
public void Update(KeyBinding keyBinding)
{
using (ContextFactory.GetForWrite())

View File

@ -73,7 +73,7 @@ public class OsuGameBase : Framework.Game, ICanAcceptFiles
protected FileStore FileStore;
protected RealmKeyBindingStore KeyBindingStore;
protected IKeyBindingStore KeyBindingStore;
protected SettingsStore SettingsStore;
@ -268,7 +268,7 @@ List<ScoreInfo> getBeatmapScores(BeatmapSetInfo set)
// todo: migrate to realm
// dependencies.Cache(KeyBindingStore = new KeyBindingStore(contextFactory, RulesetStore));
dependencies.Cache(KeyBindingStore = new RealmKeyBindingStore(realmFactory, RulesetStore));
dependencies.CacheAs(KeyBindingStore = new RealmKeyBindingStore(realmFactory, RulesetStore));
dependencies.Cache(SettingsStore = new SettingsStore(contextFactory));
dependencies.Cache(RulesetConfigCache = new RulesetConfigCache(SettingsStore));
dependencies.Cache(new SessionStatics());

View File

@ -66,7 +66,7 @@ public KeyBindingRow(object action, IEnumerable<Framework.Input.Bindings.KeyBind
}
[Resolved]
private KeyBindingStore store { get; set; }
private IKeyBindingStore store { get; set; }
[BackgroundDependencyLoader]
private void load(OsuColour colours)

View File

@ -32,7 +32,7 @@ protected KeyBindingsSubsection(int? variant)
}
[BackgroundDependencyLoader]
private void load(KeyBindingStore store)
private void load(IKeyBindingStore store)
{
var bindings = store.Query(Ruleset?.ID, variant);

View File

@ -1,6 +1,7 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Caching;
using osu.Framework.Extensions.Color4Extensions;
@ -74,7 +75,7 @@ public string TooltipSub
protected FillFlowContainer Flow;
[Resolved]
private KeyBindingStore keyBindings { get; set; }
private IKeyBindingStore keyBindings { get; set; }
protected ToolbarButton()
: base(HoverSampleSet.Loud)
@ -171,9 +172,16 @@ private void updateKeyBindingTooltip()
if (tooltipKeyBinding.IsValid)
return;
var binding = keyBindings.Query().Find(b => (GlobalAction)b.Action == Hotkey);
var keyBindingString = binding?.KeyCombination.ReadableString();
keyBindingTooltip.Text = !string.IsNullOrEmpty(keyBindingString) ? $" ({keyBindingString})" : string.Empty;
keyBindingTooltip.Text = string.Empty;
if (Hotkey != null)
{
KeyCombination? binding = keyBindings.Query(Hotkey.Value).FirstOrDefault()?.KeyCombination;
var keyBindingString = binding?.ReadableString();
if (!string.IsNullOrEmpty(keyBindingString))
keyBindingTooltip.Text = $" ({keyBindingString})";
}
tooltipKeyBinding.Validate();
}