2019-01-24 16:43:03 +08:00
|
|
|
// 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;
|
2017-08-15 23:08:59 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
2017-08-23 18:26:49 +08:00
|
|
|
using osu.Framework.Graphics;
|
2023-10-11 14:49:52 +08:00
|
|
|
using osu.Framework.Input.Bindings;
|
2022-05-16 13:09:37 +08:00
|
|
|
using osu.Framework.Localisation;
|
2021-01-11 18:47:51 +08:00
|
|
|
using osu.Game.Database;
|
2021-01-13 15:53:04 +08:00
|
|
|
using osu.Game.Input.Bindings;
|
2021-08-12 09:53:31 +08:00
|
|
|
using osu.Game.Localisation;
|
2018-11-20 15:51:59 +08:00
|
|
|
using osuTK;
|
2023-10-12 20:51:19 +08:00
|
|
|
using Realms;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-07-21 12:18:24 +08:00
|
|
|
namespace osu.Game.Overlays.Settings.Sections.Input
|
2017-08-15 23:08:59 +08:00
|
|
|
{
|
2017-08-23 11:49:30 +08:00
|
|
|
public abstract partial class KeyBindingsSubsection : SettingsSubsection
|
2017-08-15 23:08:59 +08:00
|
|
|
{
|
2022-11-17 14:23:20 +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
|
|
|
|
2023-10-11 17:25:19 +08:00
|
|
|
[Resolved]
|
|
|
|
private RealmAccess realm { get; set; } = null!;
|
|
|
|
|
2023-10-12 20:51:19 +08:00
|
|
|
protected KeyBindingsSubsection()
|
2017-08-15 23:08:59 +08:00
|
|
|
{
|
2021-10-10 02:29:44 +08:00
|
|
|
FlowContent.Spacing = new Vector2(0, 3);
|
2017-08-15 23:08:59 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-15 23:08:59 +08:00
|
|
|
[BackgroundDependencyLoader]
|
2023-10-11 17:25:19 +08:00
|
|
|
private void load()
|
2017-08-15 23:08:59 +08:00
|
|
|
{
|
2023-10-11 21:36:27 +08:00
|
|
|
var bindings = getAllBindings();
|
2021-01-14 15:33:55 +08:00
|
|
|
|
|
|
|
foreach (var defaultGroup in Defaults.GroupBy(d => d.Action))
|
2017-08-23 11:49:30 +08:00
|
|
|
{
|
2021-01-14 15:33:55 +08:00
|
|
|
int intKey = (int)defaultGroup.Key;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2023-10-11 17:08:10 +08:00
|
|
|
var row = CreateKeyBindingRow(defaultGroup.Key, defaultGroup)
|
2023-10-11 21:36:27 +08:00
|
|
|
.With(row =>
|
|
|
|
{
|
|
|
|
row.BindingUpdated = onBindingUpdated;
|
2023-10-12 22:30:22 +08:00
|
|
|
row.GetAllSectionBindings = getAllBindings;
|
2023-10-11 21:36:27 +08:00
|
|
|
});
|
2023-10-11 17:08:10 +08:00
|
|
|
row.KeyBindings.AddRange(bindings.Where(b => b.ActionInt.Equals(intKey)));
|
|
|
|
Add(row);
|
2017-08-23 11:49:30 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-23 18:26:49 +08:00
|
|
|
Add(new ResetButton
|
|
|
|
{
|
2023-10-12 18:19:07 +08:00
|
|
|
Action = () =>
|
|
|
|
{
|
|
|
|
realm.Write(r =>
|
|
|
|
{
|
|
|
|
// can't use `RestoreDefaults()` for each key binding row here as it might trigger binding conflicts along the way.
|
|
|
|
foreach (var row in Children.OfType<KeyBindingRow>())
|
|
|
|
{
|
|
|
|
foreach (var (currentBinding, defaultBinding) in row.KeyBindings.Zip(row.Defaults))
|
|
|
|
r.Find<RealmKeyBinding>(currentBinding.ID)!.KeyCombinationString = defaultBinding.ToString();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
reloadAllBindings();
|
|
|
|
}
|
2017-08-23 18:26:49 +08:00
|
|
|
});
|
|
|
|
}
|
2022-11-17 14:23:20 +08:00
|
|
|
|
2023-10-12 20:51:19 +08:00
|
|
|
protected abstract IEnumerable<RealmKeyBinding> GetKeyBindings(Realm realm);
|
|
|
|
|
2023-10-12 22:30:22 +08:00
|
|
|
private List<RealmKeyBinding> getAllBindings() => realm.Run(r =>
|
|
|
|
{
|
|
|
|
r.Refresh();
|
|
|
|
return GetKeyBindings(r).Detach();
|
|
|
|
});
|
2023-10-11 21:36:27 +08:00
|
|
|
|
2023-10-11 17:08:10 +08:00
|
|
|
protected virtual KeyBindingRow CreateKeyBindingRow(object action, IEnumerable<KeyBinding> defaults)
|
|
|
|
=> new KeyBindingRow(action)
|
2023-10-12 20:51:19 +08:00
|
|
|
{
|
|
|
|
AllowMainMouseButtons = false,
|
|
|
|
Defaults = defaults.Select(d => d.KeyCombination),
|
|
|
|
};
|
|
|
|
|
2023-10-11 21:36:27 +08:00
|
|
|
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)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 17:25:19 +08:00
|
|
|
private void onBindingUpdated(KeyBindingRow sender, KeyBindingRow.KeyBindingUpdatedEventArgs args)
|
2022-11-17 14:23:20 +08:00
|
|
|
{
|
2023-10-12 22:30:22 +08:00
|
|
|
if (args.BindingConflictResolved)
|
|
|
|
reloadAllBindings();
|
2023-10-11 17:25:19 +08:00
|
|
|
|
2023-10-12 22:30:22 +08:00
|
|
|
if (AutoAdvanceTarget && args.CanAdvanceToNextBinding)
|
2022-11-17 14:23:20 +08:00
|
|
|
{
|
|
|
|
var next = Children.SkipWhile(c => c != sender).Skip(1).FirstOrDefault();
|
|
|
|
if (next != null)
|
2024-05-27 17:02:51 +08:00
|
|
|
GetContainingFocusManager()?.ChangeFocus(next);
|
2022-11-17 14:23:20 +08:00
|
|
|
}
|
|
|
|
}
|
2017-08-23 18:26:49 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-11 02:32:56 +08:00
|
|
|
public partial class ResetButton : DangerousSettingsButton
|
2017-08-23 18:26:49 +08:00
|
|
|
{
|
|
|
|
[BackgroundDependencyLoader]
|
2021-05-11 10:00:57 +08:00
|
|
|
private void load()
|
2017-08-23 18:26:49 +08:00
|
|
|
{
|
2021-08-12 09:53:31 +08:00
|
|
|
Text = InputSettingsStrings.ResetSectionButton;
|
2017-08-23 18:26:49 +08:00
|
|
|
RelativeSizeAxes = Axes.X;
|
2021-09-13 13:17:45 +08:00
|
|
|
Width = 0.8f;
|
2021-05-18 12:47:39 +08:00
|
|
|
Anchor = Anchor.TopCentre;
|
|
|
|
Origin = Anchor.TopCentre;
|
|
|
|
Margin = new MarginPadding { Top = 15 };
|
|
|
|
Height = 30;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2017-08-23 18:26:49 +08:00
|
|
|
Content.CornerRadius = 5;
|
2017-08-15 23:08:59 +08:00
|
|
|
}
|
2021-10-02 21:10:30 +08:00
|
|
|
|
2021-10-03 01:16:46 +08:00
|
|
|
// Empty FilterTerms so that the ResetButton is visible only when the whole subsection is visible.
|
2022-05-16 13:09:37 +08:00
|
|
|
public override IEnumerable<LocalisableString> FilterTerms => Enumerable.Empty<LocalisableString>();
|
2017-08-15 23:08:59 +08:00
|
|
|
}
|
2017-08-23 18:26:49 +08:00
|
|
|
}
|