1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 18:47:27 +08:00

Move a lot of code to framework

This commit is contained in:
Dean Herbert 2017-08-11 16:11:46 +09:00
parent 8ae010f62b
commit 48d4ed55e9
19 changed files with 116 additions and 291 deletions

View File

@ -3,12 +3,13 @@
using System.Collections.Generic;
using System.ComponentModel;
using osu.Game.Input;
using osu.Framework.Input.Bindings;
using osu.Game.Input.Bindings;
using OpenTK.Input;
namespace osu.Game.Rulesets.Catch
{
public class CatchInputManager : ActionMappingInputManager<CatchAction>
public class CatchInputManager : DatabasedKeyBindingInputManager<CatchAction>
{
public CatchInputManager(RulesetInfo ruleset) : base(ruleset, concurrencyMode: ConcurrentActionMode.UniqueActions)
{

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.Bindings;
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, IHandleActions<CatchAction>
private class Catcher : Container, IHandleKeyBindings<CatchAction>
{
private Texture texture;

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.Game.Input;
using osu.Framework.Input.Bindings;
using OpenTK;
namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces
{
public class CirclePiece : Container, IHandleActions<OsuAction>
public class CirclePiece : Container, IHandleKeyBindings<OsuAction>
{
private readonly Sprite disc;

View File

@ -4,14 +4,15 @@
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Input;
using osu.Game.Input;
using osu.Framework.Input.Bindings;
using osu.Game.Input.Bindings;
using OpenTK.Input;
using KeyboardState = osu.Framework.Input.KeyboardState;
using MouseState = osu.Framework.Input.MouseState;
namespace osu.Game.Rulesets.Osu
{
public class OsuInputManager : ActionMappingInputManager<OsuAction>
public class OsuInputManager : DatabasedKeyBindingInputManager<OsuAction>
{
public OsuInputManager(RulesetInfo ruleset) : base(ruleset, concurrencyMode: ConcurrentActionMode.UniqueActions)
{

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.Bindings;
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, IHandleActions<OsuAction>
public class GameplayCursor : CursorContainer, IHandleKeyBindings<OsuAction>
{
protected override Drawable CreateCursor() => new OsuCursor();

View File

@ -1,141 +0,0 @@
// 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.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
{
/// <summary>
/// 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
where T : struct
{
private readonly RulesetInfo ruleset;
private readonly int? variant;
private readonly ConcurrentActionMode concurrencyMode;
private readonly List<Binding> mappings = new List<Binding>();
/// <summary>
/// Create a new instance.
/// </summary>
/// <param name="ruleset">A reference to identify the current <see cref="Ruleset"/>. Used to lookup mappings. Null for global mappings.</param>
/// <param name="variant">An optional variant for the specified <see cref="Ruleset"/>. Used when a ruleset has more than one possible keyboard layouts.</param>
/// <param name="concurrencyMode">Specify how to deal with multiple matches of <see cref="KeyCombination"/>s and <see cref="T"/>s.</param>
protected ActionMappingInputManager(RulesetInfo ruleset = null, int? variant = null, ConcurrentActionMode concurrencyMode = ConcurrentActionMode.None)
{
this.ruleset = ruleset;
this.variant = variant;
this.concurrencyMode = concurrencyMode;
}
protected abstract IDictionary<KeyCombination, T> CreateDefaultMappings();
private BindingStore store;
[BackgroundDependencyLoader]
private void load(BindingStore bindings)
{
store = bindings;
ReloadMappings();
}
protected void ReloadMappings()
{
var rulesetId = ruleset?.ID;
mappings.Clear();
foreach (var kvp in CreateDefaultMappings())
mappings.Add(new Binding(kvp.Key, kvp.Value));
if (store != null)
{
foreach (var b in store.Query<Binding>(b => b.RulesetID == rulesetId && b.Variant == variant))
mappings.Add(b);
}
}
private readonly List<Binding> pressedBindings = new List<Binding>();
protected override bool PropagateKeyDown(IEnumerable<Drawable> drawables, InputState state, KeyDownEventArgs args)
{
bool handled = false;
if (args.Repeat)
{
if (pressedBindings.Count > 0)
return true;
return base.PropagateKeyDown(drawables, state, args);
}
if (concurrencyMode == ConcurrentActionMode.None && pressedBindings.Count > 0)
return true;
Binding validBinding;
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))
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 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))
{
// clear the no-longer-valid combination/action.
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.
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 pressed at once. The first action matching a chord will take precedence and no other action will be pressed until it has first been released.
/// </summary>
None,
/// <summary>
/// Unique actions are allowed to be pressed 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 trigger an <see cref="IHandleActions{T}.OnPressed"/>.
/// The last binding to be released will trigger an <see cref="IHandleActions{T}.OnReleased(T)"/>.
/// </summary>
UniqueActions,
/// <summary>
/// Unique actions are allowed to be pressed at the same time. There may therefore be more than one action in an actuated state at once (same as <see cref="UniqueActions"/>).
/// In addition to this, multiple <see cref="IHandleActions{T}.OnPressed"/> are fired for single actions, even if <see cref="IHandleActions{T}.OnReleased(T)"/> has not yet been fired.
/// The same goes for <see cref="IHandleActions{T}.OnReleased(T)"/>, which is fired for each matching binding that is released.
/// </summary>
UniqueAndSameActions,
}
}

View File

@ -1,45 +0,0 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Game.Rulesets;
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
namespace osu.Game.Input
{
public class Binding
{
[ForeignKey(typeof(RulesetInfo))]
public int? RulesetID { get; set; }
[Indexed]
public int? Variant { get; set; }
[Column("Keys")]
public string KeysString
{
get { return Keys.ToString(); }
set { Keys = value; }
}
[Ignore]
public KeyCombination Keys { get; private set; }
public int Action { get; private set; }
public Binding()
{
}
public Binding(KeyCombination keys, object action)
{
Keys = keys;
Action = (int)action;
}
public virtual T GetAction<T>() => (T)(object)Action;
public override string ToString() => $"{KeysString}=>{Action}";
}
}

View File

@ -4,6 +4,7 @@
using System;
using osu.Framework.Platform;
using osu.Game.Database;
using osu.Game.Input.Bindings;
using SQLite.Net;
namespace osu.Game.Input
@ -35,12 +36,12 @@ namespace osu.Game.Input
protected override void Prepare(bool reset = false)
{
Connection.CreateTable<Binding>();
Connection.CreateTable<DatabasedKeyBinding>();
}
protected override Type[] ValidTypes => new[]
{
typeof(Binding)
typeof(DatabasedKeyBinding)
};
}

View File

@ -0,0 +1,31 @@
using osu.Framework.Input.Bindings;
using osu.Game.Rulesets;
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
namespace osu.Game.Input.Bindings
{
[Table("KeyBinding")]
public class DatabasedKeyBinding : KeyBinding
{
[ForeignKey(typeof(RulesetInfo))]
public int? RulesetID { get; set; }
[Indexed]
public int? Variant { get; set; }
[Column("Keys")]
public string KeysString
{
get { return Keys.ToString(); }
private set { Keys = value; }
}
[Column("Action")]
public new int Action
{
get { return base.Action; }
private set { base.Action = value; }
}
}
}

View File

@ -0,0 +1,54 @@
using osu.Framework.Allocation;
using osu.Framework.Input.Bindings;
using osu.Game.Rulesets;
namespace osu.Game.Input.Bindings
{
/// <summary>
/// A KeyBindingInputManager with a database backing for custom overrides.
/// </summary>
/// <typeparam name="T">The type of the custom action.</typeparam>
public abstract class DatabasedKeyBindingInputManager<T> : KeyBindingInputManager<T>
where T : struct
{
private readonly RulesetInfo ruleset;
private readonly int? variant;
private BindingStore store;
/// <summary>
/// Create a new instance.
/// </summary>
/// <param name="ruleset">A reference to identify the current <see cref="Ruleset"/>. Used to lookup mappings. Null for global mappings.</param>
/// <param name="variant">An optional variant for the specified <see cref="Ruleset"/>. Used when a ruleset has more than one possible keyboard layouts.</param>
/// <param name="concurrencyMode">Specify how to deal with multiple matches of <see cref="KeyCombination"/>s and <see cref="T"/>s.</param>
protected DatabasedKeyBindingInputManager(RulesetInfo ruleset = null, int? variant = null, ConcurrentActionMode concurrencyMode = ConcurrentActionMode.None)
: base(concurrencyMode)
{
this.ruleset = ruleset;
this.variant = variant;
}
[BackgroundDependencyLoader]
private void load(BindingStore bindings)
{
store = bindings;
}
protected override void ReloadMappings()
{
// load defaults
base.ReloadMappings();
var rulesetId = ruleset?.ID;
// load from database if present.
if (store != null)
{
foreach (var b in store.Query<DatabasedKeyBinding>(b => b.RulesetID == rulesetId && b.Variant == variant))
Mappings.Add(b);
}
}
}
}

View File

@ -7,16 +7,17 @@ using System.ComponentModel;
using System.Linq;
using osu.Framework.Graphics;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
namespace osu.Game.Input
namespace osu.Game.Input.Bindings
{
public class GlobalActionMappingInputManager : ActionMappingInputManager<GlobalAction>
public class GlobalBindingInputManager : DatabasedKeyBindingInputManager<GlobalAction>
{
private readonly Drawable handler;
public GlobalActionMappingInputManager(OsuGameBase game)
public GlobalBindingInputManager(OsuGameBase game)
{
if (game is IHandleActions<GlobalAction>)
if (game is IHandleKeyBindings<GlobalAction>)
handler = game;
}

View File

@ -1,12 +0,0 @@
// 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

@ -1,66 +0,0 @@
// 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 OpenTK.Input;
namespace osu.Game.Input
{
/// <summary>
/// Represent a combination of more than one <see cref="Key"/>s.
/// </summary>
public class KeyCombination : IEquatable<KeyCombination>
{
public readonly IEnumerable<Key> Keys;
public KeyCombination(params Key[] keys)
{
Keys = keys;
}
public KeyCombination(IEnumerable<Key> keys)
{
Keys = keys;
}
public KeyCombination(string stringRepresentation)
{
Keys = stringRepresentation.Split(',').Select(s => (Key)int.Parse(s));
}
public bool CheckValid(IEnumerable<Key> keys, bool requireExactMatch = false)
{
if (requireExactMatch)
return Keys.SequenceEqual(keys);
else
return !Keys.Except(keys).Any();
}
public bool Equals(KeyCombination other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Keys.SequenceEqual(other.Keys);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((KeyCombination)obj);
}
public override int GetHashCode() => Keys != null ? Keys.Select(k => k.GetHashCode()).Aggregate((h1, h2) => h1 + h2) : 0;
public static implicit operator KeyCombination(Key singleKey) => new KeyCombination(singleKey);
public static implicit operator KeyCombination(string stringRepresentation) => new KeyCombination(stringRepresentation);
public static implicit operator KeyCombination(Key[] keys) => new KeyCombination(keys);
public override string ToString() => Keys.Select(k => ((int)k).ToString()).Aggregate((s1, s2) => $"{s1},{s2}");
}
}

View File

@ -18,6 +18,7 @@ using osu.Game.Screens.Menu;
using OpenTK;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Input.Bindings;
using osu.Framework.Platform;
using osu.Framework.Threading;
using osu.Game.Graphics;
@ -25,11 +26,11 @@ using osu.Game.Rulesets.Scoring;
using osu.Game.Overlays.Notifications;
using osu.Game.Rulesets;
using osu.Game.Screens.Play;
using osu.Game.Input;
using osu.Game.Input.Bindings;
namespace osu.Game
{
public class OsuGame : OsuGameBase, IHandleActions<GlobalAction>
public class OsuGame : OsuGameBase, IHandleKeyBindings<GlobalAction>
{
public Toolbar Toolbar;

View File

@ -20,6 +20,7 @@ using SQLite.Net;
using osu.Framework.Graphics.Performance;
using osu.Game.Database;
using osu.Game.Input;
using osu.Game.Input.Bindings;
using osu.Game.IO;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Scoring;
@ -187,7 +188,7 @@ namespace osu.Game
Children = new Drawable[]
{
Cursor = new MenuCursor(),
new GlobalActionMappingInputManager(this)
new GlobalBindingInputManager(this)
{
RelativeSizeAxes = Axes.Both,
Child = new OsuTooltipContainer(Cursor)

View File

@ -245,7 +245,7 @@ namespace osu.Game.Overlays.Settings.Sections.General
{
RelativeSizeAxes = Axes.X,
Text = "Register new account",
//Action = registerLink
//Binding = registerLink
}
};
}

View File

@ -39,7 +39,7 @@ namespace osu.Game.Screens.Select
/// <param name="text">Text on the button.</param>
/// <param name="colour">Colour of the button.</param>
/// <param name="hotkey">Hotkey of the button.</param>
/// <param name="action">Action the button does.</param>
/// <param name="action">Binding the button does.</param>
/// <param name="depth">
/// <para>Higher depth to be put on the left, and lower to be put on the right.</para>
/// <para>Notice this is different to <see cref="Options.BeatmapOptionsOverlay"/>!</para>

View File

@ -86,7 +86,7 @@ namespace osu.Game.Screens.Select.Options
/// <param name="colour">Colour of the button.</param>
/// <param name="icon">Icon of the button.</param>
/// <param name="hotkey">Hotkey of the button.</param>
/// <param name="action">Action the button does.</param>
/// <param name="action">Binding the button does.</param>
/// <param name="depth">
/// <para>Lower depth to be put on the left, and higher to be put on the right.</para>
/// <para>Notice this is different to <see cref="Footer"/>!</para>

View File

@ -92,11 +92,10 @@
<Compile Include="Graphics\UserInterface\MenuItemType.cs" />
<Compile Include="Graphics\UserInterface\OsuContextMenu.cs" />
<Compile Include="Graphics\UserInterface\OsuContextMenuItem.cs" />
<Compile Include="Input\Binding.cs" />
<Compile Include="Input\Bindings\DatabasedKeyBinding.cs" />
<Compile Include="Input\Bindings\DatabasedKeyBindingInputManager.cs" />
<Compile Include="Input\BindingStore.cs" />
<Compile Include="Input\IHandleActions.cs" />
<Compile Include="Input\KeyCombination.cs" />
<Compile Include="Input\GlobalActionMappingInputManager.cs" />
<Compile Include="Input\Bindings\GlobalBindingInputManager.cs" />
<Compile Include="IO\FileStore.cs" />
<Compile Include="IO\FileInfo.cs" />
<Compile Include="Online\API\Requests\GetUsersRequest.cs" />
@ -123,7 +122,6 @@
<Compile Include="Overlays\Profile\Sections\RecentSection.cs" />
<Compile Include="Graphics\Containers\ConstrainedIconContainer.cs" />
<Compile Include="Rulesets\Mods\IApplicableToDifficulty.cs" />
<Compile Include="Input\ActionMappingInputManager.cs" />
<Compile Include="Users\UserCoverBackground.cs" />
<Compile Include="Overlays\UserProfileOverlay.cs" />
<Compile Include="Overlays\Profile\ProfileHeader.cs" />