mirror of
https://github.com/ppy/osu.git
synced 2026-05-22 22:20:53 +08:00
Refactor key binding panel leveraging new helper methods
This commit is contained in:
@@ -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 osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Framework.Localisation;
|
||||
@@ -18,92 +19,19 @@ namespace osu.Game.Overlays.Settings.Sections.Input
|
||||
|
||||
public override LocalisableString Header => InputSettingsStrings.GlobalKeyBindingHeader;
|
||||
|
||||
public GlobalKeyBindingsSection(GlobalActionContainer manager)
|
||||
[BackgroundDependencyLoader]
|
||||
private void load()
|
||||
{
|
||||
Add(new DefaultBindingsSubsection(manager));
|
||||
Add(new OverlayBindingsSubsection(manager));
|
||||
Add(new AudioControlKeyBindingsSubsection(manager));
|
||||
Add(new SongSelectKeyBindingSubsection(manager));
|
||||
Add(new InGameKeyBindingsSubsection(manager));
|
||||
Add(new ReplayKeyBindingsSubsection(manager));
|
||||
Add(new EditorKeyBindingsSubsection(manager));
|
||||
}
|
||||
|
||||
private partial class DefaultBindingsSubsection : KeyBindingsSubsection
|
||||
{
|
||||
protected override LocalisableString Header => string.Empty;
|
||||
|
||||
public DefaultBindingsSubsection(GlobalActionContainer manager)
|
||||
: base(null)
|
||||
AddRange(new[]
|
||||
{
|
||||
Defaults = GlobalActionContainer.GlobalKeyBindings;
|
||||
}
|
||||
}
|
||||
|
||||
private partial class OverlayBindingsSubsection : KeyBindingsSubsection
|
||||
{
|
||||
protected override LocalisableString Header => InputSettingsStrings.OverlaysSection;
|
||||
|
||||
public OverlayBindingsSubsection(GlobalActionContainer manager)
|
||||
: base(null)
|
||||
{
|
||||
Defaults = GlobalActionContainer.OverlayKeyBindings;
|
||||
}
|
||||
}
|
||||
|
||||
private partial class SongSelectKeyBindingSubsection : KeyBindingsSubsection
|
||||
{
|
||||
protected override LocalisableString Header => InputSettingsStrings.SongSelectSection;
|
||||
|
||||
public SongSelectKeyBindingSubsection(GlobalActionContainer manager)
|
||||
: base(null)
|
||||
{
|
||||
Defaults = GlobalActionContainer.SongSelectKeyBindings;
|
||||
}
|
||||
}
|
||||
|
||||
private partial class InGameKeyBindingsSubsection : KeyBindingsSubsection
|
||||
{
|
||||
protected override LocalisableString Header => InputSettingsStrings.InGameSection;
|
||||
|
||||
public InGameKeyBindingsSubsection(GlobalActionContainer manager)
|
||||
: base(null)
|
||||
{
|
||||
Defaults = GlobalActionContainer.InGameKeyBindings;
|
||||
}
|
||||
}
|
||||
|
||||
private partial class ReplayKeyBindingsSubsection : KeyBindingsSubsection
|
||||
{
|
||||
protected override LocalisableString Header => InputSettingsStrings.ReplaySection;
|
||||
|
||||
public ReplayKeyBindingsSubsection(GlobalActionContainer manager)
|
||||
: base(null)
|
||||
{
|
||||
Defaults = GlobalActionContainer.ReplayKeyBindings;
|
||||
}
|
||||
}
|
||||
|
||||
private partial class AudioControlKeyBindingsSubsection : KeyBindingsSubsection
|
||||
{
|
||||
protected override LocalisableString Header => InputSettingsStrings.AudioSection;
|
||||
|
||||
public AudioControlKeyBindingsSubsection(GlobalActionContainer manager)
|
||||
: base(null)
|
||||
{
|
||||
Defaults = GlobalActionContainer.AudioControlKeyBindings;
|
||||
}
|
||||
}
|
||||
|
||||
private partial class EditorKeyBindingsSubsection : KeyBindingsSubsection
|
||||
{
|
||||
protected override LocalisableString Header => InputSettingsStrings.EditorSection;
|
||||
|
||||
public EditorKeyBindingsSubsection(GlobalActionContainer manager)
|
||||
: base(null)
|
||||
{
|
||||
Defaults = GlobalActionContainer.EditorKeyBindings;
|
||||
}
|
||||
new GlobalKeyBindingsSubsection(string.Empty, GlobalActionCategory.General),
|
||||
new GlobalKeyBindingsSubsection(InputSettingsStrings.OverlaysSection, GlobalActionCategory.Overlays),
|
||||
new GlobalKeyBindingsSubsection(InputSettingsStrings.AudioSection, GlobalActionCategory.AudioControl),
|
||||
new GlobalKeyBindingsSubsection(InputSettingsStrings.SongSelectSection, GlobalActionCategory.SongSelect),
|
||||
new GlobalKeyBindingsSubsection(InputSettingsStrings.InGameSection, GlobalActionCategory.InGame),
|
||||
new GlobalKeyBindingsSubsection(InputSettingsStrings.ReplaySection, GlobalActionCategory.Replay),
|
||||
new GlobalKeyBindingsSubsection(InputSettingsStrings.EditorSection, GlobalActionCategory.Editor),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
// 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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework.Localisation;
|
||||
using osu.Game.Database;
|
||||
using osu.Game.Input.Bindings;
|
||||
using Realms;
|
||||
|
||||
namespace osu.Game.Overlays.Settings.Sections.Input
|
||||
{
|
||||
public partial class GlobalKeyBindingsSubsection : KeyBindingsSubsection
|
||||
{
|
||||
protected override LocalisableString Header { get; }
|
||||
|
||||
private readonly GlobalActionCategory category;
|
||||
|
||||
public GlobalKeyBindingsSubsection(LocalisableString header, GlobalActionCategory category)
|
||||
{
|
||||
Header = header;
|
||||
this.category = category;
|
||||
Defaults = GlobalActionContainer.GetDefaultBindingsFor(category);
|
||||
}
|
||||
|
||||
protected override IEnumerable<RealmKeyBinding> GetKeyBindings(Realm realm)
|
||||
{
|
||||
var bindings = realm.All<RealmKeyBinding>()
|
||||
.Where(b => b.RulesetName == null && b.Variant == null)
|
||||
.Detach();
|
||||
|
||||
var actionsInSection = GlobalActionContainer.GetGlobalActionsFor(category).Cast<int>().ToHashSet();
|
||||
return bindings.Where(kb => actionsInSection.Contains(kb.ActionInt));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Game.Input.Bindings;
|
||||
using osu.Game.Localisation;
|
||||
using osu.Game.Rulesets;
|
||||
|
||||
@@ -14,9 +13,9 @@ namespace osu.Game.Overlays.Settings.Sections.Input
|
||||
protected override Drawable CreateHeader() => new SettingsHeader(InputSettingsStrings.KeyBindingPanelHeader, InputSettingsStrings.KeyBindingPanelDescription);
|
||||
|
||||
[BackgroundDependencyLoader(permitNulls: true)]
|
||||
private void load(RulesetStore rulesets, GlobalActionContainer global)
|
||||
private void load(RulesetStore rulesets)
|
||||
{
|
||||
AddSection(new GlobalKeyBindingsSection(global));
|
||||
AddSection(new GlobalKeyBindingsSection());
|
||||
|
||||
foreach (var ruleset in rulesets.AvailableRulesets)
|
||||
AddSection(new RulesetBindingsSection(ruleset));
|
||||
|
||||
@@ -37,7 +37,7 @@ 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; init; }
|
||||
public Action<KeyBindingRow>? BindingUpdated { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether left and right mouse button clicks should be included in the edited bindings.
|
||||
|
||||
@@ -11,9 +11,9 @@ using osu.Framework.Input.Bindings;
|
||||
using osu.Framework.Localisation;
|
||||
using osu.Game.Database;
|
||||
using osu.Game.Input.Bindings;
|
||||
using osu.Game.Rulesets;
|
||||
using osu.Game.Localisation;
|
||||
using osuTK;
|
||||
using Realms;
|
||||
|
||||
namespace osu.Game.Overlays.Settings.Sections.Input
|
||||
{
|
||||
@@ -27,37 +27,26 @@ namespace osu.Game.Overlays.Settings.Sections.Input
|
||||
|
||||
protected IEnumerable<KeyBinding> Defaults { get; init; } = Array.Empty<KeyBinding>();
|
||||
|
||||
public RulesetInfo? Ruleset { get; protected set; }
|
||||
|
||||
private readonly int? variant;
|
||||
|
||||
protected KeyBindingsSubsection(int? variant)
|
||||
protected KeyBindingsSubsection()
|
||||
{
|
||||
this.variant = variant;
|
||||
|
||||
FlowContent.Spacing = new Vector2(0, 3);
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(RealmAccess realm)
|
||||
{
|
||||
string? rulesetName = Ruleset?.ShortName;
|
||||
|
||||
var bindings = realm.Run(r => r.All<RealmKeyBinding>()
|
||||
.Where(b => b.RulesetName == rulesetName && b.Variant == variant)
|
||||
.Detach());
|
||||
var bindings = realm.Run(r => GetKeyBindings(r).Detach());
|
||||
|
||||
foreach (var defaultGroup in Defaults.GroupBy(d => d.Action))
|
||||
{
|
||||
int intKey = (int)defaultGroup.Key;
|
||||
|
||||
// one row per valid action.
|
||||
Add(new KeyBindingRow(defaultGroup.Key, bindings.Where(b => b.ActionInt.Equals(intKey)).ToList())
|
||||
{
|
||||
AllowMainMouseButtons = Ruleset != null,
|
||||
Defaults = defaultGroup.Select(d => d.KeyCombination),
|
||||
BindingUpdated = onBindingUpdated
|
||||
});
|
||||
Add(CreateKeyBindingRow(
|
||||
defaultGroup.Key,
|
||||
bindings.Where(b => b.ActionInt.Equals(intKey)).ToList(),
|
||||
defaultGroup)
|
||||
.With(row => row.BindingUpdated = onBindingUpdated));
|
||||
}
|
||||
|
||||
Add(new ResetButton
|
||||
@@ -66,6 +55,15 @@ namespace osu.Game.Overlays.Settings.Sections.Input
|
||||
});
|
||||
}
|
||||
|
||||
protected abstract IEnumerable<RealmKeyBinding> GetKeyBindings(Realm realm);
|
||||
|
||||
protected virtual KeyBindingRow CreateKeyBindingRow(object action, IEnumerable<RealmKeyBinding> keyBindings, IEnumerable<KeyBinding> defaults)
|
||||
=> new KeyBindingRow(action, keyBindings.ToList())
|
||||
{
|
||||
AllowMainMouseButtons = false,
|
||||
Defaults = defaults.Select(d => d.KeyCombination),
|
||||
};
|
||||
|
||||
private void onBindingUpdated(KeyBindingRow sender)
|
||||
{
|
||||
if (AutoAdvanceTarget)
|
||||
|
||||
@@ -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 osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Localisation;
|
||||
using osu.Game.Rulesets;
|
||||
@@ -18,7 +19,11 @@ namespace osu.Game.Overlays.Settings.Sections.Input
|
||||
public RulesetBindingsSection(RulesetInfo ruleset)
|
||||
{
|
||||
this.ruleset = ruleset;
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load()
|
||||
{
|
||||
var r = ruleset.CreateInstance();
|
||||
|
||||
foreach (int variant in r.AvailableVariants)
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
// 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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework.Input.Bindings;
|
||||
using osu.Framework.Localisation;
|
||||
using osu.Game.Input.Bindings;
|
||||
using osu.Game.Rulesets;
|
||||
using Realms;
|
||||
|
||||
namespace osu.Game.Overlays.Settings.Sections.Input
|
||||
{
|
||||
@@ -12,15 +17,33 @@ namespace osu.Game.Overlays.Settings.Sections.Input
|
||||
|
||||
protected override LocalisableString Header { get; }
|
||||
|
||||
public RulesetInfo Ruleset { get; }
|
||||
private readonly int variant;
|
||||
|
||||
public VariantBindingsSubsection(RulesetInfo ruleset, int variant)
|
||||
: base(variant)
|
||||
{
|
||||
Ruleset = ruleset;
|
||||
this.variant = variant;
|
||||
|
||||
var rulesetInstance = ruleset.CreateInstance();
|
||||
|
||||
Header = rulesetInstance.GetVariantName(variant);
|
||||
Defaults = rulesetInstance.GetDefaultKeyBindings(variant);
|
||||
}
|
||||
|
||||
protected override IEnumerable<RealmKeyBinding> GetKeyBindings(Realm realm)
|
||||
{
|
||||
string rulesetName = Ruleset.ShortName;
|
||||
|
||||
return realm.All<RealmKeyBinding>()
|
||||
.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())
|
||||
{
|
||||
AllowMainMouseButtons = true,
|
||||
Defaults = defaults.Select(d => d.KeyCombination),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user