From 7c9d6c9c83a4f070d205527cc5d0bb852a030862 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 14 Aug 2017 20:19:25 +0900 Subject: [PATCH] Initial refactoring of key binding logic --- osu.Game.Rulesets.Catch/CatchInputManager.cs | 12 ---------- osu.Game.Rulesets.Catch/CatchRuleset.cs | 11 ++++++++++ .../UI/CatchRulesetContainer.cs | 2 +- osu.Game.Rulesets.Osu/OsuInputManager.cs | 12 +++------- osu.Game.Rulesets.Osu/OsuRuleset.cs | 11 +++++++++- .../UI/OsuRulesetContainer.cs | 2 +- .../Input/Bindings/DatabasedKeyBinding.cs | 2 +- .../DatabasedKeyBindingInputManager.cs | 22 +++++++------------ .../Bindings/GlobalBindingInputManager.cs | 2 +- .../{BindingStore.cs => KeyBindingStore.cs} | 19 +++++++++++++--- osu.Game/OsuGameBase.cs | 10 ++++++--- osu.Game/Rulesets/Ruleset.cs | 13 +++++++++++ osu.Game/Rulesets/UI/RulesetContainer.cs | 4 ++-- osu.Game/osu.Game.csproj | 2 +- 14 files changed, 75 insertions(+), 49 deletions(-) rename osu.Game/Input/{BindingStore.cs => KeyBindingStore.cs} (61%) diff --git a/osu.Game.Rulesets.Catch/CatchInputManager.cs b/osu.Game.Rulesets.Catch/CatchInputManager.cs index 446f9b2787..683724b7b8 100644 --- a/osu.Game.Rulesets.Catch/CatchInputManager.cs +++ b/osu.Game.Rulesets.Catch/CatchInputManager.cs @@ -1,11 +1,9 @@ // Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE -using System.Collections.Generic; using System.ComponentModel; using osu.Framework.Input.Bindings; using osu.Game.Input.Bindings; -using OpenTK.Input; namespace osu.Game.Rulesets.Catch { @@ -15,16 +13,6 @@ namespace osu.Game.Rulesets.Catch : base(ruleset, simultaneousMode: SimultaneousBindingMode.Unique) { } - - protected override IEnumerable CreateDefaultMappings() => new[] - { - new KeyBinding(Key.Z, CatchAction.MoveLeft), - new KeyBinding(Key.Left, CatchAction.MoveLeft), - new KeyBinding(Key.X, CatchAction.MoveRight), - new KeyBinding(Key.Right, CatchAction.MoveRight), - new KeyBinding(Key.LShift, CatchAction.Dash), - new KeyBinding(Key.RShift, CatchAction.Dash), - }; } public enum CatchAction diff --git a/osu.Game.Rulesets.Catch/CatchRuleset.cs b/osu.Game.Rulesets.Catch/CatchRuleset.cs index 8d45ea8fa2..b486566b7d 100644 --- a/osu.Game.Rulesets.Catch/CatchRuleset.cs +++ b/osu.Game.Rulesets.Catch/CatchRuleset.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using osu.Framework.Graphics; using osu.Game.Rulesets.Catch.Scoring; using osu.Game.Rulesets.Scoring; +using osu.Framework.Input.Bindings; namespace osu.Game.Rulesets.Catch { @@ -20,6 +21,16 @@ namespace osu.Game.Rulesets.Catch { public override RulesetContainer CreateRulesetContainerWith(WorkingBeatmap beatmap, bool isForCurrentRuleset) => new CatchRulesetContainer(this, beatmap, isForCurrentRuleset); + public override IEnumerable GetDefaultKeyBindings(int variant = 0) => new[] + { + new KeyBinding(Key.Z, CatchAction.MoveLeft), + new KeyBinding(Key.Left, CatchAction.MoveLeft), + new KeyBinding(Key.X, CatchAction.MoveRight), + new KeyBinding(Key.Right, CatchAction.MoveRight), + new KeyBinding(Key.LShift, CatchAction.Dash), + new KeyBinding(Key.RShift, CatchAction.Dash), + }; + public override IEnumerable GetModsFor(ModType type) { switch (type) diff --git a/osu.Game.Rulesets.Catch/UI/CatchRulesetContainer.cs b/osu.Game.Rulesets.Catch/UI/CatchRulesetContainer.cs index 8469be24dd..1d037deaef 100644 --- a/osu.Game.Rulesets.Catch/UI/CatchRulesetContainer.cs +++ b/osu.Game.Rulesets.Catch/UI/CatchRulesetContainer.cs @@ -28,7 +28,7 @@ namespace osu.Game.Rulesets.Catch.UI protected override Playfield CreatePlayfield() => new CatchPlayfield(); - protected override PassThroughInputManager CreateActionMappingInputManager() => new CatchInputManager(Ruleset?.RulesetInfo); + public override PassThroughInputManager CreateKeyBindingInputManager() => new CatchInputManager(Ruleset?.RulesetInfo); protected override DrawableHitObject GetVisualRepresentation(CatchBaseHit h) { diff --git a/osu.Game.Rulesets.Osu/OsuInputManager.cs b/osu.Game.Rulesets.Osu/OsuInputManager.cs index c1ee19d8c5..06836994d2 100644 --- a/osu.Game.Rulesets.Osu/OsuInputManager.cs +++ b/osu.Game.Rulesets.Osu/OsuInputManager.cs @@ -1,7 +1,7 @@ // Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE -using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using osu.Framework.Input; using osu.Framework.Input.Bindings; @@ -33,19 +33,13 @@ namespace osu.Game.Rulesets.Osu keyboard.Keys = keyboard.Keys.Concat(new[] { Key.LastKey + 2 }); } } - - protected override IEnumerable CreateDefaultMappings() => new[] - { - new KeyBinding(Key.Z, OsuAction.LeftButton), - new KeyBinding(Key.X, OsuAction.RightButton), - new KeyBinding(Key.LastKey + 1, OsuAction.LeftButton), - new KeyBinding(Key.LastKey + 2, OsuAction.RightButton), - }; } public enum OsuAction { + [Description("Left Button")] LeftButton, + [Description("Right Button")] RightButton } } diff --git a/osu.Game.Rulesets.Osu/OsuRuleset.cs b/osu.Game.Rulesets.Osu/OsuRuleset.cs index 212c634aaf..75b7be01a4 100644 --- a/osu.Game.Rulesets.Osu/OsuRuleset.cs +++ b/osu.Game.Rulesets.Osu/OsuRuleset.cs @@ -17,6 +17,7 @@ using osu.Framework.Graphics; using osu.Game.Rulesets.Osu.Scoring; using osu.Game.Rulesets.Scoring; using osu.Game.Overlays.Settings; +using osu.Framework.Input.Bindings; namespace osu.Game.Rulesets.Osu { @@ -24,8 +25,16 @@ namespace osu.Game.Rulesets.Osu { public override RulesetContainer CreateRulesetContainerWith(WorkingBeatmap beatmap, bool isForCurrentRuleset) => new OsuRulesetContainer(this, beatmap, isForCurrentRuleset); - public override IEnumerable GetBeatmapStatistics(WorkingBeatmap beatmap) => new[] + public override IEnumerable GetDefaultKeyBindings(int variant = 0) => new[] { + new KeyBinding(Key.Z, OsuAction.LeftButton), + new KeyBinding(Key.X, OsuAction.RightButton), + new KeyBinding(Key.LastKey + 1, OsuAction.LeftButton), + new KeyBinding(Key.LastKey + 2, OsuAction.RightButton), + }; + + public override IEnumerable GetBeatmapStatistics(WorkingBeatmap beatmap) => new[] + { new BeatmapStatistic { Name = @"Circle count", diff --git a/osu.Game.Rulesets.Osu/UI/OsuRulesetContainer.cs b/osu.Game.Rulesets.Osu/UI/OsuRulesetContainer.cs index 538b94603c..917322de44 100644 --- a/osu.Game.Rulesets.Osu/UI/OsuRulesetContainer.cs +++ b/osu.Game.Rulesets.Osu/UI/OsuRulesetContainer.cs @@ -31,7 +31,7 @@ namespace osu.Game.Rulesets.Osu.UI protected override Playfield CreatePlayfield() => new OsuPlayfield(); - protected override PassThroughInputManager CreateActionMappingInputManager() => new OsuInputManager(Ruleset?.RulesetInfo); + public override PassThroughInputManager CreateKeyBindingInputManager() => new OsuInputManager(Ruleset?.RulesetInfo); protected override DrawableHitObject GetVisualRepresentation(OsuHitObject h) { diff --git a/osu.Game/Input/Bindings/DatabasedKeyBinding.cs b/osu.Game/Input/Bindings/DatabasedKeyBinding.cs index 278033899c..3870cc5b04 100644 --- a/osu.Game/Input/Bindings/DatabasedKeyBinding.cs +++ b/osu.Game/Input/Bindings/DatabasedKeyBinding.cs @@ -27,7 +27,7 @@ namespace osu.Game.Input.Bindings [Column("Action")] public new int Action { - get { return base.Action; } + get { return (int)base.Action; } private set { base.Action = value; } } } diff --git a/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs b/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs index 5c62f1ddc8..52e5af152c 100644 --- a/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs +++ b/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs @@ -1,6 +1,7 @@ // Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE +using System.Collections.Generic; using osu.Framework.Allocation; using osu.Framework.Input.Bindings; using osu.Game.Rulesets; @@ -18,7 +19,9 @@ namespace osu.Game.Input.Bindings private readonly int? variant; - private BindingStore store; + private KeyBindingStore store; + + public override IEnumerable DefaultMappings => ruleset.CreateInstance().GetDefaultKeyBindings(); /// /// Create a new instance. @@ -34,24 +37,15 @@ namespace osu.Game.Input.Bindings } [BackgroundDependencyLoader] - private void load(BindingStore bindings) + private void load(KeyBindingStore keyBindings) { - store = bindings; + store = keyBindings; } 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(b => b.RulesetID == rulesetId && b.Variant == variant)) - KeyBindings.Add(b); - } + KeyBindings.Clear(); + KeyBindings.AddRange(store.GetProcessedList(DefaultMappings, ruleset?.ID, variant)); } } } \ No newline at end of file diff --git a/osu.Game/Input/Bindings/GlobalBindingInputManager.cs b/osu.Game/Input/Bindings/GlobalBindingInputManager.cs index 60a4faa8cd..d00ed588ae 100644 --- a/osu.Game/Input/Bindings/GlobalBindingInputManager.cs +++ b/osu.Game/Input/Bindings/GlobalBindingInputManager.cs @@ -21,7 +21,7 @@ namespace osu.Game.Input.Bindings handler = game; } - protected override IEnumerable CreateDefaultMappings() => new[] + public override IEnumerable DefaultMappings => new[] { new KeyBinding(Key.F8, GlobalAction.ToggleChat), new KeyBinding(Key.F9, GlobalAction.ToggleSocial), diff --git a/osu.Game/Input/BindingStore.cs b/osu.Game/Input/KeyBindingStore.cs similarity index 61% rename from osu.Game/Input/BindingStore.cs rename to osu.Game/Input/KeyBindingStore.cs index 25a997e29d..e26942a06c 100644 --- a/osu.Game/Input/BindingStore.cs +++ b/osu.Game/Input/KeyBindingStore.cs @@ -2,6 +2,8 @@ // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; +using System.Collections.Generic; +using osu.Framework.Input.Bindings; using osu.Framework.Platform; using osu.Game.Database; using osu.Game.Input.Bindings; @@ -9,9 +11,9 @@ using SQLite.Net; namespace osu.Game.Input { - public class BindingStore : DatabaseBackedStore + public class KeyBindingStore : DatabaseBackedStore { - public BindingStore(SQLiteConnection connection, Storage storage = null) + public KeyBindingStore(SQLiteConnection connection, Storage storage = null) : base(connection, storage) { } @@ -44,5 +46,16 @@ namespace osu.Game.Input typeof(DatabasedKeyBinding) }; + public List GetProcessedList(IEnumerable defaults, int? rulesetId, int? variant) + { + //todo: cache and share reference + List bindings = new List(defaults); + + // load from database if present. + foreach (var b in Query(b => b.RulesetID == rulesetId && b.Variant == variant)) + bindings.Add(b); + + return bindings; + } } -} \ No newline at end of file +} diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index 344b23cca4..d72716b5d6 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -39,7 +39,7 @@ namespace osu.Game protected ScoreStore ScoreStore; - protected BindingStore BindingStore; + protected KeyBindingStore KeyBindingStore; protected override string MainResourceFile => @"osu.Game.Resources.dll"; @@ -108,7 +108,7 @@ namespace osu.Game dependencies.Cache(FileStore = new FileStore(connection, Host.Storage)); dependencies.Cache(BeatmapManager = new BeatmapManager(Host.Storage, FileStore, connection, RulesetStore, Host)); dependencies.Cache(ScoreStore = new ScoreStore(Host.Storage, connection, Host, BeatmapManager)); - dependencies.Cache(BindingStore = new BindingStore(connection)); + dependencies.Cache(KeyBindingStore = new KeyBindingStore(connection)); dependencies.Cache(new OsuColour()); //this completely overrides the framework default. will need to change once we make a proper FontStore. @@ -183,12 +183,14 @@ namespace osu.Game { base.LoadComplete(); + GlobalBindingInputManager globalBinding; + base.Content.Add(new RatioAdjust { Children = new Drawable[] { Cursor = new MenuCursor(), - new GlobalBindingInputManager(this) + globalBinding = new GlobalBindingInputManager(this) { RelativeSizeAxes = Axes.Both, Child = new OsuTooltipContainer(Cursor) @@ -200,6 +202,8 @@ namespace osu.Game } }); + dependencies.Cache(globalBinding); + // TODO: This is temporary until we reimplement the local FPS display. // It's just to allow end-users to access the framework FPS display without knowing the shortcut key. fpsDisplayVisible = LocalConfig.GetBindable(OsuSetting.ShowFpsDisplay); diff --git a/osu.Game/Rulesets/Ruleset.cs b/osu.Game/Rulesets/Ruleset.cs index ba040403ba..e5fa7117f3 100644 --- a/osu.Game/Rulesets/Ruleset.cs +++ b/osu.Game/Rulesets/Ruleset.cs @@ -8,6 +8,7 @@ using osu.Game.Rulesets.UI; using osu.Game.Screens.Play; using System.Collections.Generic; using osu.Framework.Graphics; +using osu.Framework.Input.Bindings; using osu.Game.Rulesets.Scoring; using osu.Game.Overlays.Settings; @@ -53,5 +54,17 @@ namespace osu.Game.Rulesets /// Do not override this unless you are a legacy mode. /// public virtual int LegacyID => -1; + + /// + /// A list of available variant ids. + /// + public virtual IEnumerable AvailableVariants => new[] { 0 }; + + /// + /// Get a list of default keys for the specified variant. + /// + /// A variant. + /// A list of valid s. + public virtual IEnumerable GetDefaultKeyBindings(int variant = 0) => new KeyBinding[] { }; } } diff --git a/osu.Game/Rulesets/UI/RulesetContainer.cs b/osu.Game/Rulesets/UI/RulesetContainer.cs index 387fa15191..8512d4b5e1 100644 --- a/osu.Game/Rulesets/UI/RulesetContainer.cs +++ b/osu.Game/Rulesets/UI/RulesetContainer.cs @@ -76,7 +76,7 @@ namespace osu.Game.Rulesets.UI internal RulesetContainer(Ruleset ruleset) { Ruleset = ruleset; - KeyConversionInputManager = CreateActionMappingInputManager(); + KeyConversionInputManager = CreateKeyBindingInputManager(); KeyConversionInputManager.RelativeSizeAxes = Axes.Both; } @@ -95,7 +95,7 @@ namespace osu.Game.Rulesets.UI /// Creates a key conversion input manager. /// /// The input manager. - protected virtual PassThroughInputManager CreateActionMappingInputManager() => new PassThroughInputManager(); + public virtual PassThroughInputManager CreateKeyBindingInputManager() => new PassThroughInputManager(); protected virtual FramedReplayInputHandler CreateReplayInputHandler(Replay replay) => new FramedReplayInputHandler(replay); diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 13c4938850..b324fb9f92 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -94,7 +94,7 @@ - +