2018-01-05 11:21:19 +00:00
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-08-15 15:08:59 +00:00
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
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;
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
using osu.Framework.Input;
|
2017-08-18 09:22:00 +00:00
|
|
|
using osu.Framework.Input.Bindings;
|
2017-08-15 15:08:59 +00:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osu.Game.Input;
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
2017-08-16 08:19:27 +00:00
|
|
|
namespace osu.Game.Overlays.KeyBinding
|
2017-08-15 15:08:59 +00:00
|
|
|
{
|
2017-11-21 02:49:42 +00:00
|
|
|
public class KeyBindingRow : Container, IFilterable
|
2017-08-15 15:08:59 +00:00
|
|
|
{
|
2017-08-23 03:46:28 +00:00
|
|
|
private readonly object action;
|
2017-08-16 08:19:27 +00:00
|
|
|
private readonly IEnumerable<Framework.Input.Bindings.KeyBinding> bindings;
|
2017-08-15 15:08:59 +00:00
|
|
|
|
|
|
|
private const float transition_time = 150;
|
|
|
|
|
|
|
|
private const float height = 20;
|
|
|
|
|
|
|
|
private const float padding = 5;
|
|
|
|
|
|
|
|
private bool matchingFilter;
|
|
|
|
|
|
|
|
public bool MatchingFilter
|
|
|
|
{
|
|
|
|
get { return matchingFilter; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
matchingFilter = value;
|
|
|
|
this.FadeTo(!matchingFilter ? 0 : 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private OsuSpriteText text;
|
|
|
|
private OsuSpriteText pressAKey;
|
|
|
|
|
|
|
|
private FillFlowContainer<KeyButton> buttons;
|
|
|
|
|
2017-09-14 11:18:50 +00:00
|
|
|
public IEnumerable<string> FilterTerms => new[] { text.Text }.Concat(bindings.Select(b => b.KeyCombination.ReadableString()));
|
2017-08-15 15:08:59 +00:00
|
|
|
|
2017-08-23 03:46:28 +00:00
|
|
|
public KeyBindingRow(object action, IEnumerable<Framework.Input.Bindings.KeyBinding> bindings)
|
2017-08-15 15:08:59 +00:00
|
|
|
{
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
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
|
|
|
|
},
|
|
|
|
pressAKey = new OsuSpriteText
|
|
|
|
{
|
|
|
|
Text = "Press a key to change binding, DEL to delete, ESC to cancel.",
|
|
|
|
Y = height,
|
|
|
|
Margin = new MarginPadding(padding),
|
|
|
|
Alpha = 0,
|
|
|
|
Colour = colours.YellowDark
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var b in bindings)
|
|
|
|
buttons.Add(new KeyButton(b));
|
|
|
|
}
|
|
|
|
|
2017-08-23 10:26:49 +00:00
|
|
|
public void RestoreDefaults()
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
foreach (var d in Defaults)
|
|
|
|
{
|
|
|
|
var button = buttons[i++];
|
|
|
|
button.UpdateKeyCombination(d);
|
|
|
|
store.Update(button.KeyBinding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 15:08:59 +00:00
|
|
|
protected override bool OnHover(InputState state)
|
|
|
|
{
|
2017-08-25 05:36:14 +00:00
|
|
|
FadeEdgeEffectTo(1, transition_time, Easing.OutQuint);
|
2017-08-15 15:08:59 +00:00
|
|
|
|
|
|
|
return base.OnHover(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(InputState state)
|
|
|
|
{
|
2017-08-25 05:36:14 +00:00
|
|
|
FadeEdgeEffectTo(0, transition_time, Easing.OutQuint);
|
2017-08-15 15:08:59 +00:00
|
|
|
|
|
|
|
base.OnHoverLost(state);
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:22:00 +00:00
|
|
|
public override bool AcceptsFocus => bindTarget == null;
|
2017-08-15 15:08:59 +00:00
|
|
|
|
|
|
|
private KeyButton bindTarget;
|
|
|
|
|
2017-08-18 09:22:00 +00:00
|
|
|
public bool AllowMainMouseButtons;
|
2017-08-15 15:08:59 +00:00
|
|
|
|
2017-08-23 10:26:49 +00:00
|
|
|
public IEnumerable<KeyCombination> Defaults;
|
|
|
|
|
2017-08-18 09:22:00 +00:00
|
|
|
private bool isModifier(Key k) => k < Key.F1;
|
2017-08-15 15:08:59 +00:00
|
|
|
|
2017-08-18 09:22:00 +00:00
|
|
|
protected override bool OnClick(InputState state) => true;
|
|
|
|
|
|
|
|
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
|
|
|
|
{
|
2017-08-22 09:21:00 +00:00
|
|
|
if (!HasFocus || !bindTarget.IsHovered)
|
|
|
|
return base.OnMouseDown(state, args);
|
|
|
|
|
|
|
|
if (!AllowMainMouseButtons)
|
2017-08-18 09:22:00 +00:00
|
|
|
{
|
2017-08-22 09:21:00 +00:00
|
|
|
switch (args.Button)
|
2017-08-18 09:22:00 +00:00
|
|
|
{
|
2017-08-22 09:21:00 +00:00
|
|
|
case MouseButton.Left:
|
|
|
|
case MouseButton.Right:
|
|
|
|
return true;
|
2017-08-18 09:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:21:00 +00:00
|
|
|
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(state));
|
|
|
|
return true;
|
2017-08-15 15:08:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 09:22:00 +00:00
|
|
|
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
|
|
|
|
{
|
2017-08-22 09:21:00 +00:00
|
|
|
// don't do anything until the last button is released.
|
|
|
|
if (!HasFocus || state.Mouse.Buttons.Any())
|
|
|
|
return base.OnMouseUp(state, args);
|
2017-08-18 09:22:00 +00:00
|
|
|
|
2017-08-22 09:21:00 +00:00
|
|
|
if (bindTarget.IsHovered)
|
|
|
|
finalise();
|
|
|
|
else
|
|
|
|
updateBindTarget();
|
|
|
|
return true;
|
2017-08-18 09:22:00 +00:00
|
|
|
}
|
2017-08-15 15:08:59 +00:00
|
|
|
|
2017-08-22 05:42:49 +00:00
|
|
|
protected override bool OnWheel(InputState state)
|
|
|
|
{
|
|
|
|
if (HasFocus)
|
|
|
|
{
|
|
|
|
if (bindTarget.IsHovered)
|
|
|
|
{
|
|
|
|
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(state));
|
|
|
|
finalise();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return base.OnWheel(state);
|
|
|
|
}
|
|
|
|
|
2017-08-15 15:08:59 +00:00
|
|
|
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
|
|
|
|
{
|
2017-08-18 09:22:00 +00:00
|
|
|
if (!HasFocus)
|
|
|
|
return false;
|
|
|
|
|
2017-08-16 14:01:10 +00:00
|
|
|
switch (args.Key)
|
2017-08-15 15:08:59 +00:00
|
|
|
{
|
2017-08-16 14:01:10 +00:00
|
|
|
case Key.Escape:
|
2017-08-18 09:22:00 +00:00
|
|
|
finalise();
|
2017-08-16 14:01:10 +00:00
|
|
|
return true;
|
|
|
|
case Key.Delete:
|
2017-08-18 09:22:00 +00:00
|
|
|
bindTarget.UpdateKeyCombination(InputKey.None);
|
|
|
|
finalise();
|
2017-08-16 14:01:10 +00:00
|
|
|
return true;
|
2017-08-15 15:08:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 09:22:00 +00:00
|
|
|
bindTarget.UpdateKeyCombination(KeyCombination.FromInputState(state));
|
|
|
|
if (!isModifier(args.Key)) finalise();
|
2017-08-16 10:17:42 +00:00
|
|
|
|
2017-08-18 09:22:00 +00:00
|
|
|
return true;
|
2017-08-15 15:08:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 10:17:42 +00:00
|
|
|
protected override bool OnKeyUp(InputState state, KeyUpEventArgs args)
|
|
|
|
{
|
2017-08-22 09:21:00 +00:00
|
|
|
if (!HasFocus) return base.OnKeyUp(state, args);
|
2017-08-16 10:17:42 +00:00
|
|
|
|
2017-08-22 09:21:00 +00:00
|
|
|
finalise();
|
|
|
|
return true;
|
2017-08-16 10:17:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void finalise()
|
|
|
|
{
|
2017-08-18 09:22:00 +00:00
|
|
|
if (bindTarget != null)
|
|
|
|
{
|
|
|
|
store.Update(bindTarget.KeyBinding);
|
2017-08-16 10:17:42 +00:00
|
|
|
|
2017-08-18 09:22:00 +00:00
|
|
|
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);
|
2017-08-15 15:08:59 +00:00
|
|
|
|
|
|
|
pressAKey.FadeOut(300, Easing.OutQuint);
|
|
|
|
pressAKey.Padding = new MarginPadding { Bottom = -pressAKey.DrawHeight };
|
2017-08-18 09:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnFocus(InputState state)
|
|
|
|
{
|
|
|
|
AutoSizeDuration = 500;
|
|
|
|
AutoSizeEasing = Easing.OutQuint;
|
|
|
|
|
|
|
|
pressAKey.FadeIn(300, Easing.OutQuint);
|
|
|
|
pressAKey.Padding = new MarginPadding();
|
|
|
|
|
|
|
|
updateBindTarget();
|
|
|
|
base.OnFocus(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnFocusLost(InputState state)
|
|
|
|
{
|
|
|
|
finalise();
|
2017-08-15 15:08:59 +00:00
|
|
|
base.OnFocusLost(state);
|
|
|
|
}
|
|
|
|
|
2017-08-18 09:22:00 +00:00
|
|
|
private void updateBindTarget()
|
2017-08-15 15:08:59 +00:00
|
|
|
{
|
|
|
|
if (bindTarget != null) bindTarget.IsBinding = false;
|
|
|
|
bindTarget = buttons.FirstOrDefault(b => b.IsHovered) ?? buttons.FirstOrDefault();
|
|
|
|
if (bindTarget != null) bindTarget.IsBinding = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class KeyButton : Container
|
|
|
|
{
|
2017-08-16 08:19:27 +00:00
|
|
|
public readonly Framework.Input.Bindings.KeyBinding KeyBinding;
|
2017-08-15 15:08:59 +00:00
|
|
|
|
|
|
|
private readonly Box box;
|
|
|
|
public readonly OsuSpriteText Text;
|
|
|
|
|
|
|
|
private Color4 hoverColour;
|
|
|
|
|
|
|
|
private bool isBinding;
|
|
|
|
|
|
|
|
public bool IsBinding
|
|
|
|
{
|
|
|
|
get { return isBinding; }
|
|
|
|
set
|
|
|
|
{
|
2017-08-16 10:17:42 +00:00
|
|
|
if (value == isBinding) return;
|
2017-08-15 15:08:59 +00:00
|
|
|
isBinding = value;
|
|
|
|
|
2017-08-16 23:43:45 +00:00
|
|
|
updateHoverState();
|
2017-08-15 15:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 08:19:27 +00:00
|
|
|
public KeyButton(Framework.Input.Bindings.KeyBinding keyBinding)
|
2017-08-15 15:08:59 +00:00
|
|
|
{
|
|
|
|
KeyBinding = keyBinding;
|
|
|
|
|
|
|
|
Margin = new MarginPadding(padding);
|
|
|
|
|
2017-08-16 08:34:49 +00:00
|
|
|
// todo: use this in a meaningful way
|
|
|
|
// var isDefault = keyBinding.Action is Enum;
|
2017-08-15 15:08:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
Font = "Venera",
|
|
|
|
TextSize = 10,
|
|
|
|
Margin = new MarginPadding(5),
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Text = keyBinding.KeyCombination.ReadableString(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
|
|
|
hoverColour = colours.YellowDark;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnHover(InputState state)
|
|
|
|
{
|
2017-08-16 23:43:45 +00:00
|
|
|
updateHoverState();
|
2017-08-15 15:08:59 +00:00
|
|
|
return base.OnHover(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(InputState state)
|
|
|
|
{
|
2017-08-16 23:43:45 +00:00
|
|
|
updateHoverState();
|
2017-08-16 10:17:42 +00:00
|
|
|
base.OnHoverLost(state);
|
|
|
|
}
|
2017-08-15 15:08:59 +00:00
|
|
|
|
2017-08-16 10:17:42 +00:00
|
|
|
private void updateHoverState()
|
|
|
|
{
|
2017-08-16 23:43:45 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-08-16 10:17:42 +00:00
|
|
|
}
|
2017-08-15 15:08:59 +00:00
|
|
|
|
2017-08-18 09:22:00 +00:00
|
|
|
public void UpdateKeyCombination(KeyCombination newCombination)
|
2017-08-16 10:17:42 +00:00
|
|
|
{
|
|
|
|
KeyBinding.KeyCombination = newCombination;
|
|
|
|
Text.Text = KeyBinding.KeyCombination.ReadableString();
|
2017-08-15 15:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 14:18:02 +00:00
|
|
|
}
|