// Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using System.Collections.Generic; using osu.Framework.Allocation; using osu.Framework.Input; using osu.Framework.Platform; using osu.Game.Database; using osu.Game.Rulesets; using OpenTK.Input; using SQLite.Net; using SQLiteNetExtensions.Attributes; namespace osu.Game.Input { public class ActionMappingInputManager : PassThroughInputManager where T : struct { private readonly RulesetInfo ruleset; protected ActionMappingInputManager(RulesetInfo ruleset = null) { this.ruleset = ruleset; } protected IDictionary Mappings { get; set; } [BackgroundDependencyLoader] private void load(BindingStore bindings) { var rulesetId = ruleset?.ID; foreach (var b in bindings.Query(b => b.RulesetID == rulesetId)) Mappings[b.Key] = (T)(object)b.Action; } protected override bool OnKeyDown(InputState state, KeyDownEventArgs args) { mapKey(state, args.Key); return base.OnKeyDown(state, args); } protected override bool OnKeyUp(InputState state, KeyUpEventArgs args) { mapKey(state, args.Key); return base.OnKeyUp(state, args); } private void mapKey(InputState state, Key key) { T mappedData; if (Mappings.TryGetValue(key, out mappedData)) state.Data = mappedData; } private T parseStringRepresentation(string str) { T res; if (Enum.TryParse(str, out res)) return res; return default(T); } } public class Binding { [ForeignKey(typeof(RulesetInfo))] public int? RulesetID { get; set; } public Key Key { get; set; } public int Action { get; set; } } public class BindingStore : DatabaseBackedStore { public BindingStore(SQLiteConnection connection, Storage storage = null) : base(connection, storage) { } protected override void Prepare(bool reset = false) { Connection.CreateTable(); } protected override Type[] ValidTypes => new[] { typeof(Binding) }; } }