1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:33:30 +08:00

Remove shortcomings, remove InputState.Data usage, make everything amazing

No more casting!
This commit is contained in:
Dean Herbert 2017-08-10 19:28:24 +09:00
parent 1fe273cbc0
commit 798fff00b2
8 changed files with 164 additions and 143 deletions

View File

@ -8,8 +8,8 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input;
using osu.Framework.MathUtils;
using osu.Game.Input;
using osu.Game.Rulesets.Catch.Judgements;
using osu.Game.Rulesets.Catch.Objects;
using osu.Game.Rulesets.Catch.Objects.Drawable;
@ -48,7 +48,7 @@ namespace osu.Game.Rulesets.Catch.UI
catcher.Size = new Vector2(DrawSize.Y);
}
private class Catcher : Container
private class Catcher : Container, IHandleActions<CatchAction>
{
private Texture texture;
@ -104,48 +104,40 @@ namespace osu.Game.Rulesets.Catch.UI
OriginPosition = new Vector2(DrawWidth / 2, 10) //temporary until the sprite is aligned correctly.
};
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
public bool OnPressed(CatchAction action)
{
if (args.Repeat) return true;
if (state.Data is CatchAction)
switch (action)
{
switch ((CatchAction)state.Data)
{
case CatchAction.MoveLeft:
currentDirection--;
return true;
case CatchAction.MoveRight:
currentDirection++;
return true;
case CatchAction.Dash:
Dashing = true;
return true;
}
case CatchAction.MoveLeft:
currentDirection--;
return true;
case CatchAction.MoveRight:
currentDirection++;
return true;
case CatchAction.Dash:
Dashing = true;
return true;
}
return base.OnKeyDown(state, args);
return false;
}
protected override bool OnKeyUp(InputState state, KeyUpEventArgs args)
public bool OnReleased(CatchAction action)
{
if (state.Data is CatchAction)
switch (action)
{
switch ((CatchAction)state.Data)
{
case CatchAction.MoveLeft:
currentDirection++;
return true;
case CatchAction.MoveRight:
currentDirection--;
return true;
case CatchAction.Dash:
Dashing = false;
return true;
}
case CatchAction.MoveLeft:
currentDirection++;
return true;
case CatchAction.MoveRight:
currentDirection--;
return true;
case CatchAction.Dash:
Dashing = false;
return true;
}
return base.OnKeyUp(state, args);
return false;
}
protected override void Update()

View File

@ -7,12 +7,12 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input;
using osu.Game.Input;
using OpenTK;
namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces
{
public class CirclePiece : Container
public class CirclePiece : Container, IHandleActions<OsuAction>
{
private readonly Sprite disc;
@ -49,19 +49,18 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces
disc.Texture = textures.Get(@"Play/osu/disc");
}
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
public bool OnPressed(OsuAction action)
{
if (state.Data is OsuAction)
switch (action)
{
switch ((OsuAction)state.Data)
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
return IsHovered && (Hit?.Invoke() ?? false);
}
case OsuAction.LeftButton:
case OsuAction.RightButton:
return IsHovered && (Hit?.Invoke() ?? false);
}
return false;
}
public bool OnReleased(OsuAction action) => false;
}
}

View File

@ -8,15 +8,15 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Input;
using OpenTK;
using OpenTK.Graphics;
namespace osu.Game.Rulesets.Osu.UI.Cursor
{
public class GameplayCursor : CursorContainer
public class GameplayCursor : CursorContainer, IHandleActions<OsuAction>
{
protected override Drawable CreateCursor() => new OsuCursor();
@ -27,40 +27,6 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor
private int downCount;
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
if (state.Data is OsuAction)
{
switch ((OsuAction)state.Data)
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
downCount++;
ActiveCursor.ScaleTo(1).ScaleTo(1.2f, 100, Easing.OutQuad);
break;
}
}
return false;
}
protected override bool OnKeyUp(InputState state, KeyUpEventArgs args)
{
if (state.Data is OsuAction)
{
switch ((OsuAction)state.Data)
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
if (--downCount == 0)
ActiveCursor.ScaleTo(1, 200, Easing.OutQuad);
break;
}
}
return false;
}
public class OsuCursor : Container
{
private Container cursorContainer;
@ -165,5 +131,33 @@ namespace osu.Game.Rulesets.Osu.UI.Cursor
cursorContainer.Scale = new Vector2(scale);
}
}
public bool OnPressed(OsuAction action)
{
switch (action)
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
downCount++;
ActiveCursor.ScaleTo(1).ScaleTo(1.2f, 100, Easing.OutQuad);
break;
}
return false;
}
public bool OnReleased(OsuAction action)
{
switch (action)
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
if (--downCount == 0)
ActiveCursor.ScaleTo(1, 200, Easing.OutQuad);
break;
}
return false;
}
}
}

View File

@ -1,36 +1,17 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Input;
using osu.Game.Rulesets;
namespace osu.Game.Input
{
public enum ConcurrentActionMode
{
/// <summary>
/// One action can be actuated at once. The first action matching a chord will take precedence and no other action will be actuated until it has been released.
/// </summary>
None,
/// <summary>
/// Unique actions are allowed to be fired at the same time. There may therefore be more than one action in an actuated state at once.
/// If one action has multiple bindings, only the first will add actuation data, and the last to be released will add de-actuation data.
/// </summary>
UniqueActions,
/// <summary>
/// Both unique actions and the same action can be concurrently actuated.
/// Same as <see cref="UniqueActions"/>, but multiple bindings for the same action will individually add actuation and de-actuation data to events.
/// </summary>
UniqueAndSameActions,
}
/// <summary>
/// Maps custom action data of type <see cref="T"/> and stores to <see cref="InputState.Data"/>.
/// Maps input actions to custom action data of type <see cref="T"/>. Use in conjunction with <see cref="Drawable"/>s implementing <see cref="IHandleActions{T}"/>.
/// </summary>
/// <typeparam name="T">The type of the custom action.</typeparam>
public abstract class ActionMappingInputManager<T> : PassThroughInputManager
@ -82,37 +63,35 @@ namespace osu.Game.Input
foreach (var b in store.Query<Binding>(b => b.RulesetID == rulesetId && b.Variant == variant))
mappings.Add(b);
}
if (concurrencyMode > ConcurrentActionMode.None)
{
// ensure we have no overlapping bindings.
foreach (var m in mappings)
foreach (var colliding in mappings.Where(k => !k.Keys.Equals(m.Keys) && k.Keys.CheckValid(m.Keys.Keys)))
throw new InvalidOperationException($"Multiple partially overlapping bindings are not supported ({m} and {colliding} are colliding)!");
}
}
private readonly List<Binding> pressedBindings = new List<Binding>();
protected override void PopulateDataKeyDown(InputState state, KeyDownEventArgs args)
protected override bool PropagateKeyDown(IEnumerable<Drawable> drawables, InputState state, KeyDownEventArgs args)
{
bool handled = false;
if (!args.Repeat && (concurrencyMode > ConcurrentActionMode.None || pressedBindings.Count == 0))
{
Binding validBinding;
if ((validBinding = mappings.Except(pressedBindings).LastOrDefault(m => m.Keys.CheckValid(state.Keyboard.Keys, concurrencyMode == ConcurrentActionMode.None))) != null)
while ((validBinding = mappings.Except(pressedBindings).LastOrDefault(m => m.Keys.CheckValid(state.Keyboard.Keys, concurrencyMode == ConcurrentActionMode.None))) != null)
{
if (concurrencyMode == ConcurrentActionMode.UniqueAndSameActions || pressedBindings.All(p => p.Action != validBinding.Action))
state.Data = validBinding.GetAction<T>();
handled = drawables.OfType<IHandleActions<T>>().Any(d => d.OnPressed(validBinding.GetAction<T>()));
// store both the pressed combination and the resulting action, just in case the assignments change while we are actuated.
pressedBindings.Add(validBinding);
}
}
return handled || base.PropagateKeyDown(drawables, state, args);
}
protected override void PopulateDataKeyUp(InputState state, KeyUpEventArgs args)
protected override bool PropagateKeyUp(IEnumerable<Drawable> drawables, InputState state, KeyUpEventArgs args)
{
bool handled = false;
foreach (var binding in pressedBindings.ToList())
{
if (!binding.Keys.CheckValid(state.Keyboard.Keys, concurrencyMode == ConcurrentActionMode.None))
@ -121,10 +100,32 @@ namespace osu.Game.Input
pressedBindings.Remove(binding);
if (concurrencyMode == ConcurrentActionMode.UniqueAndSameActions || pressedBindings.All(p => p.Action != binding.Action))
{
// set data as KeyUp if we're all done with this action.
state.Data = binding.GetAction<T>();
handled = drawables.OfType<IHandleActions<T>>().Any(d => d.OnReleased(binding.GetAction<T>()));
}
}
}
return handled || base.PropagateKeyUp(drawables, state, args);
}
}
public enum ConcurrentActionMode
{
/// <summary>
/// One action can be actuated at once. The first action matching a chord will take precedence and no other action will be actuated until it has been released.
/// </summary>
None,
/// <summary>
/// Unique actions are allowed to be fired at the same time. There may therefore be more than one action in an actuated state at once.
/// If one action has multiple bindings, only the first will add actuation data, and the last to be released will add de-actuation data.
/// </summary>
UniqueActions,
/// <summary>
/// Both unique actions and the same action can be concurrently actuated.
/// Same as <see cref="UniqueActions"/>, but multiple bindings for the same action will individually add actuation and de-actuation data to events.
/// </summary>
UniqueAndSameActions,
}
}

View File

@ -0,0 +1,24 @@
using System;
using osu.Framework.Graphics;
namespace osu.Game.Input
{
/// <summary>
/// A simple placeholder container which allows handling keyboard input at a higher level than otherwise possible.
/// </summary>
public class GlobalHotkeys : Drawable, IHandleActions<GlobalAction>
{
public Func<GlobalAction, bool> Handler;
public override bool HandleInput => true;
public GlobalHotkeys()
{
RelativeSizeAxes = Axes.Both;
}
public bool OnPressed(GlobalAction action) => Handler(action);
public bool OnReleased(GlobalAction action) => false;
}
}

View File

@ -0,0 +1,12 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
namespace osu.Game.Input
{
public interface IHandleActions<in T>
where T : struct
{
bool OnPressed(T action);
bool OnReleased(T action);
}
}

View File

@ -168,7 +168,7 @@ namespace osu.Game
volume = new VolumeControl(),
overlayContent = new Container { RelativeSizeAxes = Axes.Both },
new OnScreenDisplay(),
new GlobalHotkeys //exists because UserInputManager is at a level below us.
new Input.GlobalHotkeys //exists because UserInputManager is at a level below us.
{
Handler = globalHotkeyPressed
}
@ -251,39 +251,36 @@ namespace osu.Game
Cursor.State = Visibility.Hidden;
}
private bool globalHotkeyPressed(InputState state, KeyDownEventArgs args)
private bool globalHotkeyPressed(GlobalAction action)
{
if (args.Repeat || intro == null) return false;
if (intro == null) return false;
if (state.Data is GlobalAction)
switch (action)
{
switch ((GlobalAction)state.Data)
{
case GlobalAction.ToggleChat:
chat.ToggleVisibility();
return true;
case GlobalAction.ToggleSocial:
social.ToggleVisibility();
return true;
case GlobalAction.ResetInputSettings:
var sensitivity = frameworkConfig.GetBindable<double>(FrameworkSetting.CursorSensitivity);
case GlobalAction.ToggleChat:
chat.ToggleVisibility();
return true;
case GlobalAction.ToggleSocial:
social.ToggleVisibility();
return true;
case GlobalAction.ResetInputSettings:
var sensitivity = frameworkConfig.GetBindable<double>(FrameworkSetting.CursorSensitivity);
sensitivity.Disabled = false;
sensitivity.Value = 1;
sensitivity.Disabled = true;
sensitivity.Disabled = false;
sensitivity.Value = 1;
sensitivity.Disabled = true;
frameworkConfig.Set(FrameworkSetting.ActiveInputHandlers, string.Empty);
return true;
case GlobalAction.ToggleToolbar:
Toolbar.ToggleVisibility();
return true;
case GlobalAction.ToggleSettings:
settings.ToggleVisibility();
return true;
case GlobalAction.ToggleDirect:
direct.ToggleVisibility();
return true;
}
frameworkConfig.Set(FrameworkSetting.ActiveInputHandlers, string.Empty);
return true;
case GlobalAction.ToggleToolbar:
Toolbar.ToggleVisibility();
return true;
case GlobalAction.ToggleSettings:
settings.ToggleVisibility();
return true;
case GlobalAction.ToggleDirect:
direct.ToggleVisibility();
return true;
}
return false;

View File

@ -94,6 +94,8 @@
<Compile Include="Graphics\UserInterface\OsuContextMenuItem.cs" />
<Compile Include="Input\Binding.cs" />
<Compile Include="Input\BindingStore.cs" />
<Compile Include="Input\GlobalHotkeys.cs" />
<Compile Include="Input\IHandleActions.cs" />
<Compile Include="Input\KeyCombination.cs" />
<Compile Include="Input\GlobalAction.cs" />
<Compile Include="Input\GlobalActionMappingInputManager.cs" />