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
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
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.Bindings;
|
2018-10-02 11:02:47 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2018-04-13 17:19:50 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2019-06-21 12:50:05 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2018-04-13 17:19:50 +08:00
|
|
|
using osu.Game.Input;
|
2019-06-21 12:50:05 +08:00
|
|
|
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.KeyBinding
|
|
|
|
{
|
|
|
|
public class KeyBindingRow : Container, IFilterable
|
|
|
|
{
|
|
|
|
private readonly object action;
|
|
|
|
private readonly IEnumerable<Framework.Input.Bindings.KeyBinding> bindings;
|
|
|
|
|
|
|
|
private const float transition_time = 150;
|
|
|
|
|
|
|
|
private const float height = 20;
|
|
|
|
|
|
|
|
private const float padding = 5;
|
|
|
|
|
|
|
|
private bool matchingFilter;
|
|
|
|
|
|
|
|
public bool MatchingFilter
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
get => matchingFilter;
|
2018-04-13 17:19:50 +08:00
|
|
|
set
|
|
|
|
{
|
|
|
|
matchingFilter = value;
|
|
|
|
this.FadeTo(!matchingFilter ? 0 : 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 23:29:07 +08:00
|
|
|
public bool FilteringActive { get; set; }
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
private OsuSpriteText text;
|
2019-06-21 12:50:05 +08:00
|
|
|
private Drawable pressAKey;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
private FillFlowContainer<KeyButton> buttons;
|
|
|
|
|
2018-09-19 13:07:46 +08:00
|
|
|
public IEnumerable<string> FilterTerms => bindings.Select(b => b.KeyCombination.ReadableString()).Prepend((string)text.Text);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
public KeyBindingRow(object action, IEnumerable<Framework.Input.Bindings.KeyBinding> bindings)
|
|
|
|
{
|
|
|
|
this.action = action;
|
|
|
|
this.bindings = bindings;
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
|
|
|
Masking = true;
|
|
|
|
CornerRadius = padding;
|
|
|
|
}
|
|
|
|
|
|
|
|
private KeyBindingStore store;
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours, KeyBindingStore store)
|
|
|
|
{
|
|
|
|
this.store = store;
|
|
|
|
|
|
|
|
EdgeEffect = new EdgeEffectParameters
|
|
|
|
{
|
|
|
|
Radius = 2,
|
|
|
|
Colour = colours.YellowDark.Opacity(0),
|
|
|
|
Type = EdgeEffectType.Shadow,
|
|
|
|
Hollow = true,
|
|
|
|
};
|
|
|
|
|
2019-06-21 12:50:05 +08:00
|
|
|
Children = new[]
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
|
|
|
new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Colour = Color4.Black,
|
|
|
|
Alpha = 0.6f,
|
|
|
|
},
|
|
|
|
text = new OsuSpriteText
|
|
|
|
{
|
|
|
|
Text = action.GetDescription(),
|
|
|
|
Margin = new MarginPadding(padding),
|
|
|
|
},
|
|
|
|
buttons = new FillFlowContainer<KeyButton>
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight
|
|
|
|
},
|
2019-06-21 12:50:05 +08:00
|
|
|
pressAKey = new FillFlowContainer
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2019-06-21 12:50:05 +08:00
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Padding = new MarginPadding(padding) { Top = height + padding * 2 },
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
2018-04-13 17:19:50 +08:00
|
|
|
Alpha = 0,
|
2019-06-21 12:50:05 +08:00
|
|
|
Spacing = new Vector2(5),
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new CancelButton { Action = finalise },
|
|
|
|
new ClearButton { Action = clear },
|
|
|
|
},
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var b in bindings)
|
|
|
|
buttons.Add(new KeyButton(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
var button = buttons[i++];
|
|
|
|
button.UpdateKeyCombination(d);
|
|
|
|
store.Update(button.KeyBinding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
protected override bool OnHover(HoverEvent e)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
|
|
|
FadeEdgeEffectTo(1, transition_time, Easing.OutQuint);
|
|
|
|
|
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
|
|
|
{
|
|
|
|
FadeEdgeEffectTo(0, transition_time, Easing.OutQuint);
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
base.OnHoverLost(e);
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override bool AcceptsFocus => bindTarget == null;
|
|
|
|
|
|
|
|
private KeyButton bindTarget;
|
|
|
|
|
|
|
|
public bool AllowMainMouseButtons;
|
|
|
|
|
|
|
|
public IEnumerable<KeyCombination> Defaults;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:52:57 +08:00
|
|
|
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState));
|
2018-04-13 17:19:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
protected override bool OnMouseUp(MouseUpEvent e)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
|
|
|
// don't do anything until the last button is released.
|
2018-10-02 11:44:14 +08:00
|
|
|
if (!HasFocus || e.HasAnyButtonPressed)
|
2018-10-02 11:02:47 +08:00
|
|
|
return base.OnMouseUp(e);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
if (bindTarget.IsHovered)
|
|
|
|
finalise();
|
|
|
|
else
|
|
|
|
updateBindTarget();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-09-19 19:52:57 +08:00
|
|
|
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState, e.ScrollDelta));
|
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)
|
|
|
|
return false;
|
|
|
|
|
2018-09-19 19:52:57 +08:00
|
|
|
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState));
|
2018-10-02 11:02:47 +08:00
|
|
|
if (!isModifier(e.Key)) finalise();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
protected override bool OnKeyUp(KeyUpEvent e)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2018-10-02 11:02:47 +08:00
|
|
|
if (!HasFocus) return base.OnKeyUp(e);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
finalise();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:52:57 +08:00
|
|
|
protected override bool OnJoystickPress(JoystickPressEvent e)
|
2018-05-12 23:10:55 +08:00
|
|
|
{
|
|
|
|
if (!HasFocus)
|
|
|
|
return false;
|
|
|
|
|
2018-09-19 19:52:57 +08:00
|
|
|
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(e.CurrentState));
|
2018-05-12 23:10:55 +08:00
|
|
|
finalise();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:52:57 +08:00
|
|
|
protected override bool OnJoystickRelease(JoystickReleaseEvent e)
|
2018-05-12 23:10:55 +08:00
|
|
|
{
|
|
|
|
if (!HasFocus)
|
2018-09-19 19:52:57 +08:00
|
|
|
return base.OnJoystickRelease(e);
|
2018-05-12 23:10:55 +08:00
|
|
|
|
|
|
|
finalise();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-21 12:50:05 +08:00
|
|
|
private void clear()
|
|
|
|
{
|
|
|
|
bindTarget.UpdateKeyCombination(InputKey.None);
|
|
|
|
finalise();
|
|
|
|
}
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
private void finalise()
|
|
|
|
{
|
|
|
|
if (bindTarget != null)
|
|
|
|
{
|
|
|
|
store.Update(bindTarget.KeyBinding);
|
|
|
|
|
|
|
|
bindTarget.IsBinding = false;
|
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
// schedule to ensure we don't instantly get focus back on next OnMouseClick (see AcceptFocus impl.)
|
|
|
|
bindTarget = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasFocus)
|
|
|
|
GetContainingInputManager().ChangeFocus(null);
|
|
|
|
|
|
|
|
pressAKey.FadeOut(300, Easing.OutQuint);
|
2018-07-11 10:24:43 +08:00
|
|
|
pressAKey.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
|
|
|
{
|
|
|
|
AutoSizeDuration = 500;
|
|
|
|
AutoSizeEasing = Easing.OutQuint;
|
|
|
|
|
|
|
|
pressAKey.FadeIn(300, Easing.OutQuint);
|
2018-07-11 10:24:43 +08:00
|
|
|
pressAKey.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();
|
2018-10-02 11:02:47 +08:00
|
|
|
base.OnFocusLost(e);
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updateBindTarget()
|
|
|
|
{
|
|
|
|
if (bindTarget != null) bindTarget.IsBinding = false;
|
|
|
|
bindTarget = buttons.FirstOrDefault(b => b.IsHovered) ?? buttons.FirstOrDefault();
|
|
|
|
if (bindTarget != null) bindTarget.IsBinding = true;
|
|
|
|
}
|
|
|
|
|
2019-06-21 12:50:05 +08:00
|
|
|
private class CancelButton : TriangleButton
|
|
|
|
{
|
|
|
|
public CancelButton()
|
|
|
|
{
|
|
|
|
Text = "Cancel";
|
|
|
|
Size = new Vector2(80, 20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ClearButton : TriangleButton
|
|
|
|
{
|
|
|
|
public ClearButton()
|
|
|
|
{
|
|
|
|
Text = "Clear";
|
|
|
|
Size = new Vector2(80, 20);
|
|
|
|
}
|
|
|
|
|
2019-06-21 13:43:00 +08:00
|
|
|
protected override bool OnMouseUp(MouseUpEvent e)
|
|
|
|
{
|
|
|
|
base.OnMouseUp(e);
|
|
|
|
|
|
|
|
// without this, the mouse up triggers a finalise (and deselection) of the current binding target.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-21 12:50:05 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
|
|
|
BackgroundColour = colours.Pink;
|
|
|
|
|
|
|
|
Triangles.ColourDark = colours.PinkDark;
|
|
|
|
Triangles.ColourLight = colours.PinkLight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
private class KeyButton : Container
|
|
|
|
{
|
|
|
|
public readonly Framework.Input.Bindings.KeyBinding KeyBinding;
|
|
|
|
|
|
|
|
private readonly Box box;
|
|
|
|
public readonly OsuSpriteText Text;
|
|
|
|
|
|
|
|
private Color4 hoverColour;
|
|
|
|
|
|
|
|
private bool isBinding;
|
|
|
|
|
|
|
|
public bool IsBinding
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public KeyButton(Framework.Input.Bindings.KeyBinding keyBinding)
|
|
|
|
{
|
|
|
|
KeyBinding = keyBinding;
|
|
|
|
|
|
|
|
Margin = new MarginPadding(padding);
|
|
|
|
|
|
|
|
// todo: use this in a meaningful way
|
|
|
|
// var isDefault = keyBinding.Action is Enum;
|
|
|
|
|
|
|
|
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,
|
|
|
|
Colour = Color4.Black
|
|
|
|
},
|
|
|
|
Text = new OsuSpriteText
|
|
|
|
{
|
2019-02-22 18:42:09 +08:00
|
|
|
Font = OsuFont.Numeric.With(size: 10),
|
2018-04-13 17:19:50 +08:00
|
|
|
Margin = new MarginPadding(5),
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Text = keyBinding.KeyCombination.ReadableString(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
|
|
|
hoverColour = colours.YellowDark;
|
|
|
|
}
|
|
|
|
|
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(Color4.White, transition_time, Easing.OutQuint);
|
|
|
|
Text.FadeColour(Color4.Black, transition_time, Easing.OutQuint);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box.FadeColour(IsHovered ? hoverColour : Color4.Black, transition_time, Easing.OutQuint);
|
|
|
|
Text.FadeColour(IsHovered ? Color4.Black : Color4.White, transition_time, Easing.OutQuint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdateKeyCombination(KeyCombination newCombination)
|
|
|
|
{
|
|
|
|
KeyBinding.KeyCombination = newCombination;
|
|
|
|
Text.Text = KeyBinding.KeyCombination.ReadableString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|