1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 01:17:27 +08:00
osu-lazer/osu.Game/Overlays/Settings/Sections/Input/KeyBindingRow.cs

621 lines
20 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
2022-06-17 15:37:17 +08:00
#nullable disable
2021-01-13 16:59:47 +08:00
using System;
2018-04-13 17:19:50 +08:00
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
2021-05-25 19:08:40 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-04-02 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
2018-04-13 17:19:50 +08:00
using osu.Framework.Input.Bindings;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Database;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Graphics.UserInterfaceV2;
2018-04-13 17:19:50 +08:00
using osu.Game.Input;
2021-01-13 16:59:47 +08:00
using osu.Game.Input.Bindings;
using osu.Game.Resources.Localisation.Web;
using osuTK;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
using osuTK.Input;
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 partial class KeyBindingRow : Container, IFilterable
2018-04-13 17:19:50 +08:00
{
/// <summary>
2022-11-18 12:55:37 +08:00
/// Invoked when the binding of this row is updated with a change being written.
/// </summary>
public Action<KeyBindingRow> BindingUpdated { get; init; }
2023-10-11 14:21:19 +08:00
/// <summary>
/// Whether left and right mouse button clicks should be included in the edited bindings.
/// </summary>
public bool AllowMainMouseButtons { get; init; }
2018-04-13 17:19:50 +08:00
2023-10-11 14:21:19 +08:00
/// <summary>
/// The default key bindings for this row.
/// </summary>
public IEnumerable<KeyCombination> Defaults { get; init; }
2018-04-13 17:19:50 +08:00
2023-10-11 14:18:48 +08:00
#region IFilterable
2018-04-13 17:19:50 +08:00
private bool matchingFilter;
public bool MatchingFilter
{
get => matchingFilter;
2018-04-13 17:19:50 +08:00
set
{
matchingFilter = value;
this.FadeTo(!matchingFilter ? 0 : 1);
}
}
2023-10-11 14:18:48 +08:00
public bool FilteringActive { get; set; }
2023-10-11 14:18:48 +08:00
public IEnumerable<LocalisableString> FilterTerms => bindings.Select(b => (LocalisableString)keyCombinationProvider.GetReadableString(b.KeyCombination)).Prepend(text.Text);
2023-10-11 14:18:48 +08:00
#endregion
private readonly object action;
private readonly IEnumerable<RealmKeyBinding> bindings;
private Bindable<bool> isDefault { get; } = new BindableBool(true);
2019-03-28 23:29:07 +08:00
[Resolved]
2021-11-08 17:24:37 +08:00
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; }
2023-10-11 14:18:48 +08:00
[Resolved]
private RealmAccess realm { get; set; }
private Container content;
2018-04-13 17:19:50 +08:00
private OsuSpriteText text;
private FillFlowContainer cancelAndClearButtons;
2020-08-03 14:25:23 +08:00
private FillFlowContainer<KeyButton> buttons;
2018-04-13 17:19:50 +08:00
2023-10-11 14:18:48 +08:00
private KeyButton bindTarget;
2021-05-15 09:24:08 +08:00
2023-10-11 14:18:48 +08:00
private const float transition_time = 150;
private const float height = 20;
private const float padding = 5;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) =>
content.ReceivePositionalInputAt(screenSpacePos);
public override bool AcceptsFocus => bindTarget == null;
2018-04-13 17:19:50 +08:00
2023-10-11 14:21:19 +08:00
/// <summary>
/// Creates a new <see cref="KeyBindingRow"/>.
/// </summary>
/// <param name="action">The action that this row contains bindings for.</param>
/// <param name="bindings">The keybindings to display in this row.</param>
2021-01-13 16:59:47 +08:00
public KeyBindingRow(object action, List<RealmKeyBinding> bindings)
2018-04-13 17:19:50 +08:00
{
this.action = action;
this.bindings = bindings;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
}
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2021-10-19 02:36:47 +08:00
Padding = new MarginPadding { Right = SettingsPanel.CONTENT_MARGINS };
2018-04-13 17:19:50 +08:00
2021-10-19 02:36:47 +08:00
InternalChildren = new Drawable[]
2018-04-13 17:19:50 +08:00
{
2021-10-19 02:36:47 +08:00
new Container
2018-04-13 17:19:50 +08:00
{
2021-10-19 02:36:47 +08:00
RelativeSizeAxes = Axes.Y,
Width = SettingsPanel.CONTENT_MARGINS,
Child = new RevertToDefaultButton<bool>
2021-10-19 02:36:47 +08:00
{
Current = isDefault,
Action = RestoreDefaults,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
},
2021-10-19 02:36:47 +08:00
new Container
{
2021-10-19 02:36:47 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Left = SettingsPanel.CONTENT_MARGINS },
Children = new Drawable[]
{
content = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Masking = true,
CornerRadius = padding,
EdgeEffect = new EdgeEffectParameters
{
Radius = 2,
Colour = colourProvider.Highlight1.Opacity(0),
Type = EdgeEffectType.Shadow,
Hollow = true,
},
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background5,
},
text = new OsuSpriteText
{
Text = action.GetLocalisableDescription(),
Margin = new MarginPadding(1.5f * padding),
},
buttons = new FillFlowContainer<KeyButton>
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight
},
cancelAndClearButtons = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Padding = new MarginPadding(padding) { Top = height + padding * 2 },
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Alpha = 0,
Spacing = new Vector2(5),
Children = new Drawable[]
{
new CancelButton { Action = () => finalise(false) },
new ClearButton { Action = clear },
},
},
new HoverClickSounds()
}
2021-10-19 02:36:47 +08:00
}
}
}
2018-04-13 17:19:50 +08:00
};
foreach (var b in bindings)
2020-08-03 14:25:23 +08:00
buttons.Add(new KeyButton(b));
2021-05-25 19:08:40 +08:00
2021-05-26 17:28:00 +08:00
updateIsDefaultValue();
2018-04-13 17:19:50 +08:00
}
public void RestoreDefaults()
{
int i = 0;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
foreach (var d in Defaults)
{
2020-08-03 14:25:23 +08:00
var button = buttons[i++];
2018-04-13 17:19:50 +08:00
button.UpdateKeyCombination(d);
2021-01-13 16:59:47 +08:00
updateStoreFromButton(button);
2018-04-13 17:19:50 +08:00
}
2021-05-25 19:08:40 +08:00
2021-05-26 17:28:00 +08:00
isDefault.Value = true;
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
content.FadeEdgeEffectTo(1, transition_time, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
return base.OnHover(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
content.FadeEdgeEffectTo(0, transition_time, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
private bool isModifier(Key k) => k < Key.F1;
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e) => true;
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
2018-04-13 17:19:50 +08:00
{
if (!HasFocus || !bindTarget.IsHovered)
2018-10-02 11:02:47 +08:00
return base.OnMouseDown(e);
2018-04-13 17:19:50 +08:00
if (!AllowMainMouseButtons)
{
2018-10-02 11:02:47 +08:00
switch (e.Button)
2018-04-13 17:19:50 +08:00
{
case MouseButton.Left:
case MouseButton.Right:
return true;
}
}
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromMouseButton(e.Button));
2018-04-13 17:19:50 +08:00
return true;
}
protected override void OnMouseUp(MouseUpEvent e)
2018-04-13 17:19:50 +08:00
{
// don't do anything until the last button is released.
if (!HasFocus || e.HasAnyButtonPressed)
{
base.OnMouseUp(e);
return;
}
2018-04-13 17:19:50 +08:00
if (bindTarget.IsHovered)
finalise(false);
// prevent updating bind target before clear button's action
else if (!cancelAndClearButtons.Any(b => b.IsHovered))
2018-04-13 17:19:50 +08:00
updateBindTarget();
}
2018-10-02 11:02:47 +08:00
protected override bool OnScroll(ScrollEvent e)
2018-04-13 17:19:50 +08:00
{
if (HasFocus)
{
if (bindTarget.IsHovered)
{
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState, e.ScrollDelta), KeyCombination.FromScrollDelta(e.ScrollDelta).First());
2018-04-13 17:19:50 +08:00
finalise();
return true;
}
}
2018-10-02 11:02:47 +08:00
return base.OnScroll(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnKeyDown(KeyDownEvent e)
2018-04-13 17:19:50 +08:00
{
if (!HasFocus || e.Repeat)
2018-04-13 17:19:50 +08:00
return false;
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromKey(e.Key));
2018-10-02 11:02:47 +08:00
if (!isModifier(e.Key)) finalise();
2018-04-13 17:19:50 +08:00
return true;
}
protected override void OnKeyUp(KeyUpEvent e)
2018-04-13 17:19:50 +08:00
{
if (!HasFocus)
{
base.OnKeyUp(e);
return;
}
2018-04-13 17:19:50 +08:00
finalise();
}
protected override bool OnJoystickPress(JoystickPressEvent e)
{
if (!HasFocus)
return false;
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromJoystickButton(e.Button));
finalise();
return true;
}
protected override void OnJoystickRelease(JoystickReleaseEvent e)
{
if (!HasFocus)
{
base.OnJoystickRelease(e);
return;
}
finalise();
}
2020-05-10 12:39:20 +08:00
protected override bool OnMidiDown(MidiDownEvent e)
{
if (!HasFocus)
return false;
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromMidiKey(e.Key));
2020-05-10 12:39:20 +08:00
finalise();
return true;
}
protected override void OnMidiUp(MidiUpEvent e)
{
if (!HasFocus)
{
base.OnMidiUp(e);
return;
}
finalise();
}
2022-10-15 15:11:28 +08:00
protected override bool OnTabletAuxiliaryButtonPress(TabletAuxiliaryButtonPressEvent e)
{
if (!HasFocus)
return false;
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromTabletAuxiliaryButton(e.Button));
2022-10-15 15:11:28 +08:00
finalise();
return true;
}
protected override void OnTabletAuxiliaryButtonRelease(TabletAuxiliaryButtonReleaseEvent e)
{
if (!HasFocus)
{
base.OnTabletAuxiliaryButtonRelease(e);
return;
}
finalise();
}
protected override bool OnTabletPenButtonPress(TabletPenButtonPressEvent e)
{
if (!HasFocus)
return false;
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState), KeyCombination.FromTabletPenButton(e.Button));
2022-10-15 15:11:28 +08:00
finalise();
return true;
}
protected override void OnTabletPenButtonRelease(TabletPenButtonReleaseEvent e)
{
if (!HasFocus)
{
base.OnTabletPenButtonRelease(e);
return;
}
finalise();
}
private void clear()
{
if (bindTarget == null)
return;
bindTarget.UpdateKeyCombination(InputKey.None);
finalise(false);
}
2022-11-18 12:55:37 +08:00
private void finalise(bool hasChanged = true)
2018-04-13 17:19:50 +08:00
{
if (bindTarget != null)
{
updateStoreFromButton(bindTarget);
2018-04-13 17:19:50 +08:00
2021-05-25 19:08:40 +08:00
updateIsDefaultValue();
2018-04-13 17:19:50 +08:00
bindTarget.IsBinding = false;
Schedule(() =>
{
// schedule to ensure we don't instantly get focus back on next OnMouseClick (see AcceptFocus impl.)
bindTarget = null;
2022-11-18 12:55:37 +08:00
if (hasChanged)
BindingUpdated?.Invoke(this);
2018-04-13 17:19:50 +08:00
});
}
if (HasFocus)
GetContainingInputManager().ChangeFocus(null);
cancelAndClearButtons.FadeOut(300, Easing.OutQuint);
cancelAndClearButtons.BypassAutoSizeAxes |= Axes.Y;
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnFocus(FocusEvent e)
2018-04-13 17:19:50 +08:00
{
content.AutoSizeDuration = 500;
content.AutoSizeEasing = Easing.OutQuint;
2018-04-13 17:19:50 +08:00
cancelAndClearButtons.FadeIn(300, Easing.OutQuint);
cancelAndClearButtons.BypassAutoSizeAxes &= ~Axes.Y;
2018-04-13 17:19:50 +08:00
updateBindTarget();
2018-10-02 11:02:47 +08:00
base.OnFocus(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnFocusLost(FocusLostEvent e)
2018-04-13 17:19:50 +08:00
{
finalise(false);
2018-10-02 11:02:47 +08:00
base.OnFocusLost(e);
2018-04-13 17:19:50 +08:00
}
2020-08-03 03:26:09 +08:00
/// <summary>
/// Updates the bind target to the currently hovered key button or the first if clicked anywhere else.
/// </summary>
2018-04-13 17:19:50 +08:00
private void updateBindTarget()
{
if (bindTarget != null) bindTarget.IsBinding = false;
2020-08-03 14:25:23 +08:00
bindTarget = buttons.FirstOrDefault(b => b.IsHovered) ?? buttons.FirstOrDefault();
2018-04-13 17:19:50 +08:00
if (bindTarget != null) bindTarget.IsBinding = true;
}
private void updateStoreFromButton(KeyButton button) =>
realm.WriteAsync(r => r.Find<RealmKeyBinding>(button.KeyBinding.ID)!.KeyCombinationString = button.KeyBinding.KeyCombinationString);
private void updateIsDefaultValue()
{
2021-05-26 17:28:00 +08:00
isDefault.Value = bindings.Select(b => b.KeyCombination).SequenceEqual(Defaults);
}
private partial class CancelButton : RoundedButton
{
public CancelButton()
{
Text = CommonStrings.ButtonsCancel;
Size = new Vector2(80, 20);
}
}
public partial class ClearButton : DangerousRoundedButton
{
public ClearButton()
{
Text = CommonStrings.ButtonsClear;
Size = new Vector2(80, 20);
}
}
2022-11-24 13:32:20 +08:00
public partial class KeyButton : Container
2018-04-13 17:19:50 +08:00
{
2021-01-13 16:59:47 +08:00
public readonly RealmKeyBinding KeyBinding;
2018-04-13 17:19:50 +08:00
private readonly Box box;
public readonly OsuSpriteText Text;
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
2018-04-13 17:19:50 +08:00
[Resolved]
2021-11-08 17:24:37 +08:00
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; }
2018-04-13 17:19:50 +08:00
private bool isBinding;
public bool IsBinding
{
get => isBinding;
2018-04-13 17:19:50 +08:00
set
{
if (value == isBinding) return;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
isBinding = value;
updateHoverState();
}
}
2021-01-13 16:59:47 +08:00
public KeyButton(RealmKeyBinding keyBinding)
2018-04-13 17:19:50 +08:00
{
2021-01-13 16:59:47 +08:00
if (keyBinding.IsManaged)
throw new ArgumentException("Key binding should not be attached as we make temporary changes", nameof(keyBinding));
2018-04-13 17:19:50 +08:00
KeyBinding = keyBinding;
Margin = new MarginPadding(padding);
Masking = true;
CornerRadius = padding;
Height = height;
AutoSizeAxes = Axes.X;
Children = new Drawable[]
{
new Container
{
AlwaysPresent = true,
Width = 80,
Height = height,
},
box = new Box
{
RelativeSizeAxes = Axes.Both,
},
Text = new OsuSpriteText
{
Font = OsuFont.Numeric.With(size: 10),
2018-04-13 17:19:50 +08:00
Margin = new MarginPadding(5),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
new HoverSounds()
2018-04-13 17:19:50 +08:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
2021-11-08 17:24:37 +08:00
keyCombinationProvider.KeymapChanged += updateKeyCombinationText;
updateKeyCombinationText();
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load()
2018-04-13 17:19:50 +08:00
{
updateHoverState();
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
updateHoverState();
2018-10-02 11:02:47 +08:00
return base.OnHover(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
updateHoverState();
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
private void updateHoverState()
{
if (isBinding)
{
box.FadeColour(colourProvider.Light2, transition_time, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
Text.FadeColour(Color4.Black, transition_time, Easing.OutQuint);
}
else
{
box.FadeColour(IsHovered ? colourProvider.Light4 : colourProvider.Background6, transition_time, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
Text.FadeColour(IsHovered ? Color4.Black : Color4.White, transition_time, Easing.OutQuint);
}
}
/// <summary>
/// Update from a key combination, only allowing a single non-modifier key to be specified.
/// </summary>
/// <param name="fullState">A <see cref="KeyCombination"/> generated from the full input state.</param>
/// <param name="triggerKey">The key which triggered this update, and should be used as the binding.</param>
public void UpdateKeyCombination(KeyCombination fullState, InputKey triggerKey) =>
UpdateKeyCombination(new KeyCombination(fullState.Keys.Where(KeyCombination.IsModifierKey).Append(triggerKey)));
2018-04-13 17:19:50 +08:00
public void UpdateKeyCombination(KeyCombination newCombination)
{
if (KeyBinding.RulesetName != null && !RealmKeyBindingStore.CheckValidForGameplay(newCombination))
return;
KeyBinding.KeyCombination = newCombination;
updateKeyCombinationText();
}
private void updateKeyCombinationText()
{
Scheduler.AddOnce(updateText);
void updateText() => Text.Text = keyCombinationProvider.GetReadableString(KeyBinding.KeyCombination);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2021-11-08 17:26:12 +08:00
if (keyCombinationProvider != null)
keyCombinationProvider.KeymapChanged -= updateKeyCombinationText;
2018-04-13 17:19:50 +08:00
}
}
}
}