mirror of
https://github.com/osukey/osukey.git
synced 2025-05-01 19:57:25 +09:00
Add and consume IKeyBindingStore interface
This commit is contained in:
parent
a9a3a95991
commit
43f417b53a
@ -74,7 +74,7 @@ namespace osu.Game.Tests.Visual
|
|||||||
typeof(FileStore),
|
typeof(FileStore),
|
||||||
typeof(ScoreManager),
|
typeof(ScoreManager),
|
||||||
typeof(BeatmapManager),
|
typeof(BeatmapManager),
|
||||||
typeof(KeyBindingStore),
|
typeof(IKeyBindingStore),
|
||||||
typeof(SettingsStore),
|
typeof(SettingsStore),
|
||||||
typeof(RulesetConfigCache),
|
typeof(RulesetConfigCache),
|
||||||
typeof(OsuColour),
|
typeof(OsuColour),
|
||||||
|
@ -6,7 +6,6 @@ using System.Collections.Generic;
|
|||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Input.Bindings;
|
using osu.Framework.Input.Bindings;
|
||||||
using osu.Game.Rulesets;
|
using osu.Game.Rulesets;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace osu.Game.Input.Bindings
|
namespace osu.Game.Input.Bindings
|
||||||
{
|
{
|
||||||
@ -21,7 +20,8 @@ namespace osu.Game.Input.Bindings
|
|||||||
|
|
||||||
private readonly int? variant;
|
private readonly int? variant;
|
||||||
|
|
||||||
private KeyBindingStore store;
|
[Resolved]
|
||||||
|
private IKeyBindingStore store { get; set; }
|
||||||
|
|
||||||
public override IEnumerable<KeyBinding> DefaultKeyBindings => ruleset.CreateInstance().GetDefaultKeyBindings(variant ?? 0);
|
public override IEnumerable<KeyBinding> DefaultKeyBindings => ruleset.CreateInstance().GetDefaultKeyBindings(variant ?? 0);
|
||||||
|
|
||||||
@ -42,12 +42,6 @@ namespace osu.Game.Input.Bindings
|
|||||||
throw new InvalidOperationException($"{nameof(variant)} can not be null when a non-null {nameof(ruleset)} is provided.");
|
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()
|
protected override void LoadComplete()
|
||||||
{
|
{
|
||||||
base.LoadComplete();
|
base.LoadComplete();
|
||||||
@ -69,7 +63,7 @@ namespace osu.Game.Input.Bindings
|
|||||||
// fallback to defaults instead.
|
// fallback to defaults instead.
|
||||||
KeyBindings = DefaultKeyBindings;
|
KeyBindings = DefaultKeyBindings;
|
||||||
else
|
else
|
||||||
KeyBindings = store.Query(ruleset?.ID, variant).ToList();
|
KeyBindings = store.Query(ruleset?.ID, variant);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
40
osu.Game/Input/IKeyBindingStore.cs
Normal file
40
osu.Game/Input/IKeyBindingStore.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -12,7 +12,7 @@ using osu.Game.Rulesets;
|
|||||||
|
|
||||||
namespace osu.Game.Input
|
namespace osu.Game.Input
|
||||||
{
|
{
|
||||||
public class KeyBindingStore : DatabaseBackedStore
|
public class KeyBindingStore : DatabaseBackedStore, IKeyBindingStore
|
||||||
{
|
{
|
||||||
public event Action KeyBindingChanged;
|
public event Action KeyBindingChanged;
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ namespace osu.Game.Input
|
|||||||
/// <returns>A set of display strings for all the user's key configuration for the action.</returns>
|
/// <returns>A set of display strings for all the user's key configuration for the action.</returns>
|
||||||
public IEnumerable<string> GetReadableKeyCombinationsFor(GlobalAction globalAction)
|
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();
|
string str = action.KeyCombination.ReadableString();
|
||||||
|
|
||||||
@ -49,6 +49,12 @@ namespace osu.Game.Input
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null)
|
||||||
{
|
{
|
||||||
using (var usage = ContextFactory.GetForWrite())
|
using (var usage = ContextFactory.GetForWrite())
|
||||||
@ -56,7 +62,7 @@ namespace osu.Game.Input
|
|||||||
// compare counts in database vs defaults
|
// compare counts in database vs defaults
|
||||||
foreach (var group in defaults.GroupBy(k => k.Action))
|
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();
|
int aimCount = group.Count();
|
||||||
|
|
||||||
if (aimCount <= count)
|
if (aimCount <= count)
|
||||||
@ -86,7 +92,7 @@ namespace osu.Game.Input
|
|||||||
/// <param name="rulesetId">The ruleset's internal ID.</param>
|
/// <param name="rulesetId">The ruleset's internal ID.</param>
|
||||||
/// <param name="variant">An optional variant.</param>
|
/// <param name="variant">An optional variant.</param>
|
||||||
/// <returns></returns>
|
/// <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();
|
ContextFactory.Get().DatabasedKeyBinding.Where(b => b.RulesetID == rulesetId && b.Variant == variant).ToList();
|
||||||
|
|
||||||
public void Update(KeyBinding keyBinding)
|
public void Update(KeyBinding keyBinding)
|
||||||
|
@ -12,7 +12,7 @@ using osu.Game.Rulesets;
|
|||||||
|
|
||||||
namespace osu.Game.Input
|
namespace osu.Game.Input
|
||||||
{
|
{
|
||||||
public class RealmKeyBindingStore : RealmBackedStore
|
public class RealmKeyBindingStore : RealmBackedStore, IKeyBindingStore
|
||||||
{
|
{
|
||||||
public event Action KeyBindingChanged;
|
public event Action KeyBindingChanged;
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ namespace osu.Game.Input
|
|||||||
/// <returns>A set of display strings for all the user's key configuration for the action.</returns>
|
/// <returns>A set of display strings for all the user's key configuration for the action.</returns>
|
||||||
public IEnumerable<string> GetReadableKeyCombinationsFor(GlobalAction globalAction)
|
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();
|
string str = action.KeyBinding.KeyCombination.ReadableString();
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ namespace osu.Game.Input
|
|||||||
// compare counts in database vs defaults
|
// compare counts in database vs defaults
|
||||||
foreach (var group in defaults.GroupBy(k => k.Action))
|
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();
|
int aimCount = group.Count();
|
||||||
|
|
||||||
if (aimCount <= count)
|
if (aimCount <= count)
|
||||||
@ -87,9 +87,15 @@ namespace osu.Game.Input
|
|||||||
/// <param name="rulesetId">The ruleset's internal ID.</param>
|
/// <param name="rulesetId">The ruleset's internal ID.</param>
|
||||||
/// <param name="variant">An optional variant.</param>
|
/// <param name="variant">An optional variant.</param>
|
||||||
/// <returns></returns>
|
/// <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();
|
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)
|
public void Update(KeyBinding keyBinding)
|
||||||
{
|
{
|
||||||
using (ContextFactory.GetForWrite())
|
using (ContextFactory.GetForWrite())
|
||||||
|
@ -73,7 +73,7 @@ namespace osu.Game
|
|||||||
|
|
||||||
protected FileStore FileStore;
|
protected FileStore FileStore;
|
||||||
|
|
||||||
protected RealmKeyBindingStore KeyBindingStore;
|
protected IKeyBindingStore KeyBindingStore;
|
||||||
|
|
||||||
protected SettingsStore SettingsStore;
|
protected SettingsStore SettingsStore;
|
||||||
|
|
||||||
@ -268,7 +268,7 @@ namespace osu.Game
|
|||||||
// todo: migrate to realm
|
// todo: migrate to realm
|
||||||
// dependencies.Cache(KeyBindingStore = new KeyBindingStore(contextFactory, RulesetStore));
|
// 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(SettingsStore = new SettingsStore(contextFactory));
|
||||||
dependencies.Cache(RulesetConfigCache = new RulesetConfigCache(SettingsStore));
|
dependencies.Cache(RulesetConfigCache = new RulesetConfigCache(SettingsStore));
|
||||||
dependencies.Cache(new SessionStatics());
|
dependencies.Cache(new SessionStatics());
|
||||||
|
@ -66,7 +66,7 @@ namespace osu.Game.Overlays.KeyBinding
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Resolved]
|
[Resolved]
|
||||||
private KeyBindingStore store { get; set; }
|
private IKeyBindingStore store { get; set; }
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
[BackgroundDependencyLoader]
|
||||||
private void load(OsuColour colours)
|
private void load(OsuColour colours)
|
||||||
|
@ -32,7 +32,7 @@ namespace osu.Game.Overlays.KeyBinding
|
|||||||
}
|
}
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
[BackgroundDependencyLoader]
|
||||||
private void load(KeyBindingStore store)
|
private void load(IKeyBindingStore store)
|
||||||
{
|
{
|
||||||
var bindings = store.Query(Ruleset?.ID, variant);
|
var bindings = store.Query(Ruleset?.ID, variant);
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// 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.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System.Linq;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Caching;
|
using osu.Framework.Caching;
|
||||||
using osu.Framework.Extensions.Color4Extensions;
|
using osu.Framework.Extensions.Color4Extensions;
|
||||||
@ -74,7 +75,7 @@ namespace osu.Game.Overlays.Toolbar
|
|||||||
protected FillFlowContainer Flow;
|
protected FillFlowContainer Flow;
|
||||||
|
|
||||||
[Resolved]
|
[Resolved]
|
||||||
private KeyBindingStore keyBindings { get; set; }
|
private IKeyBindingStore keyBindings { get; set; }
|
||||||
|
|
||||||
protected ToolbarButton()
|
protected ToolbarButton()
|
||||||
: base(HoverSampleSet.Loud)
|
: base(HoverSampleSet.Loud)
|
||||||
@ -171,9 +172,16 @@ namespace osu.Game.Overlays.Toolbar
|
|||||||
if (tooltipKeyBinding.IsValid)
|
if (tooltipKeyBinding.IsValid)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var binding = keyBindings.Query().Find(b => (GlobalAction)b.Action == Hotkey);
|
keyBindingTooltip.Text = string.Empty;
|
||||||
var keyBindingString = binding?.KeyCombination.ReadableString();
|
|
||||||
keyBindingTooltip.Text = !string.IsNullOrEmpty(keyBindingString) ? $" ({keyBindingString})" : 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();
|
tooltipKeyBinding.Validate();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user