1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 14:37:30 +08:00
osu-lazer/osu.Game/Overlays/Settings/Sections/Input/KeyBindingsSubsection.cs

138 lines
5.2 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2023-10-11 14:49:52 +08:00
using System;
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
2023-10-11 14:49:52 +08:00
using osu.Framework.Input.Bindings;
using osu.Framework.Localisation;
using osu.Game.Database;
using osu.Game.Input.Bindings;
using osu.Game.Localisation;
2018-11-20 15:51:59 +08:00
using osuTK;
using Realms;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Settings.Sections.Input
2018-04-13 17:19:50 +08:00
{
2022-11-24 13:32:20 +08:00
public abstract partial class KeyBindingsSubsection : SettingsSubsection
2018-04-13 17:19:50 +08:00
{
/// <summary>
/// After a successful binding, automatically select the next binding row to make quickly
/// binding a large set of keys easier on the user.
/// </summary>
protected virtual bool AutoAdvanceTarget => false;
2023-10-11 14:49:52 +08:00
protected IEnumerable<KeyBinding> Defaults { get; init; } = Array.Empty<KeyBinding>();
2018-04-13 17:19:50 +08:00
[Resolved]
private RealmAccess realm { get; set; } = null!;
protected KeyBindingsSubsection()
2018-04-13 17:19:50 +08:00
{
FlowContent.Spacing = new Vector2(0, 3);
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
private void load()
2018-04-13 17:19:50 +08:00
{
var bindings = getAllBindings();
foreach (var defaultGroup in Defaults.GroupBy(d => d.Action))
2018-04-13 17:19:50 +08:00
{
int intKey = (int)defaultGroup.Key;
2018-04-13 17:19:50 +08:00
var row = CreateKeyBindingRow(defaultGroup.Key, defaultGroup)
.With(row =>
{
row.BindingUpdated = onBindingUpdated;
row.BindingConflictResolved = reloadAllBindings;
});
row.KeyBindings.AddRange(bindings.Where(b => b.ActionInt.Equals(intKey)));
Add(row);
2018-04-13 17:19:50 +08:00
}
Add(new ResetButton
{
Action = () => Children.OfType<KeyBindingRow>().ForEach(k => k.RestoreDefaults())
});
}
protected abstract IEnumerable<RealmKeyBinding> GetKeyBindings(Realm realm);
private List<RealmKeyBinding> getAllBindings() => realm.Run(r => 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 reloadAllBindings()
{
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)
{
var bindings = getAllBindings();
var existingBinding = args.KeyCombination.Equals(new KeyCombination(InputKey.None))
? null
: bindings.FirstOrDefault(kb => kb.ID != args.KeyBindingID && kb.KeyCombination.Equals(args.KeyCombination));
if (existingBinding != null)
{
// `RealmKeyBinding`'s `Action` is just an int, always.
// we need more than that for proper display, so leverage `Defaults` (which have the correct enum-typed object in `Action` inside).
object existingAssignedAction = Defaults.First(binding => (int)binding.Action == existingBinding.ActionInt).Action;
var bindingBeforeUpdate = bindings.Single(binding => binding.ID == args.KeyBindingID);
sender.ShowBindingConflictPopover(
new KeyBindingConflictInfo(
new ConflictingKeyBinding(existingBinding.ID, existingAssignedAction, existingBinding.KeyCombination, new KeyCombination(InputKey.None)),
new ConflictingKeyBinding(bindingBeforeUpdate.ID, args.Action, args.KeyCombination, bindingBeforeUpdate.KeyCombination)));
return;
}
realm.WriteAsync(r => r.Find<RealmKeyBinding>(args.KeyBindingID)!.KeyCombinationString = args.KeyCombination.ToString());
if (AutoAdvanceTarget)
{
var next = Children.SkipWhile(c => c != sender).Skip(1).FirstOrDefault();
if (next != null)
GetContainingInputManager().ChangeFocus(next);
}
}
2018-04-13 17:19:50 +08:00
}
2022-11-24 13:32:20 +08:00
public partial class ResetButton : DangerousSettingsButton
2018-04-13 17:19:50 +08:00
{
[BackgroundDependencyLoader]
private void load()
2018-04-13 17:19:50 +08:00
{
Text = InputSettingsStrings.ResetSectionButton;
2018-04-13 17:19:50 +08:00
RelativeSizeAxes = Axes.X;
Width = 0.8f;
Anchor = Anchor.TopCentre;
Origin = Anchor.TopCentre;
Margin = new MarginPadding { Top = 15 };
Height = 30;
2018-04-13 17:19:50 +08:00
Content.CornerRadius = 5;
}
// Empty FilterTerms so that the ResetButton is visible only when the whole subsection is visible.
public override IEnumerable<LocalisableString> FilterTerms => Enumerable.Empty<LocalisableString>();
2018-04-13 17:19:50 +08:00
}
}