2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-08-09 14:15:41 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
2017-10-17 14:00:27 +08:00
|
|
|
|
using System;
|
2017-08-14 19:19:25 +08:00
|
|
|
|
using System.Collections.Generic;
|
2017-08-14 21:31:23 +08:00
|
|
|
|
using System.Linq;
|
2017-08-14 19:19:25 +08:00
|
|
|
|
using osu.Framework.Input.Bindings;
|
2017-08-09 13:50:10 +08:00
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
using osu.Game.Database;
|
2017-08-11 15:11:46 +08:00
|
|
|
|
using osu.Game.Input.Bindings;
|
2017-08-14 21:31:23 +08:00
|
|
|
|
using osu.Game.Rulesets;
|
2017-08-09 13:50:10 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Input
|
|
|
|
|
{
|
2017-08-14 19:19:25 +08:00
|
|
|
|
public class KeyBindingStore : DatabaseBackedStore
|
2017-08-09 13:50:10 +08:00
|
|
|
|
{
|
2017-10-21 22:39:31 +08:00
|
|
|
|
public event Action KeyBindingChanged;
|
|
|
|
|
|
2017-10-23 15:35:35 +08:00
|
|
|
|
public KeyBindingStore(Func<OsuDbContext> createContext, RulesetStore rulesets, Storage storage = null)
|
|
|
|
|
: base(createContext, storage)
|
2017-08-09 13:50:10 +08:00
|
|
|
|
{
|
2017-10-16 16:02:31 +08:00
|
|
|
|
foreach (var info in rulesets.AvailableRulesets)
|
2017-08-14 21:31:23 +08:00
|
|
|
|
{
|
|
|
|
|
var ruleset = info.CreateInstance();
|
|
|
|
|
foreach (var variant in ruleset.AvailableVariants)
|
2017-10-14 13:28:25 +08:00
|
|
|
|
insertDefaults(ruleset.GetDefaultKeyBindings(variant), info.ID, variant);
|
2017-08-14 21:31:23 +08:00
|
|
|
|
}
|
2017-08-09 13:50:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 22:03:31 +08:00
|
|
|
|
public void Register(KeyBindingContainer manager) => insertDefaults(manager.DefaultKeyBindings);
|
2017-08-14 21:31:23 +08:00
|
|
|
|
|
2017-08-15 22:59:58 +08:00
|
|
|
|
private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null)
|
2017-08-14 19:19:25 +08:00
|
|
|
|
{
|
2017-10-23 15:35:35 +08:00
|
|
|
|
var context = GetContext();
|
|
|
|
|
|
2017-10-23 13:53:13 +08:00
|
|
|
|
using (var transaction = context.BeginTransaction())
|
2017-08-14 21:31:23 +08:00
|
|
|
|
{
|
2017-10-22 15:17:55 +08:00
|
|
|
|
// compare counts in database vs defaults
|
|
|
|
|
foreach (var group in defaults.GroupBy(k => k.Action))
|
|
|
|
|
{
|
2017-10-23 15:35:35 +08:00
|
|
|
|
int count = Query(rulesetId, variant).Count(k => (int)k.Action == (int)group.Key);
|
2017-10-22 15:17:55 +08:00
|
|
|
|
int aimCount = group.Count();
|
2017-08-14 19:19:25 +08:00
|
|
|
|
|
2017-10-22 15:17:55 +08:00
|
|
|
|
if (aimCount <= count)
|
|
|
|
|
continue;
|
2017-10-16 16:58:17 +08:00
|
|
|
|
|
2017-10-22 15:17:55 +08:00
|
|
|
|
foreach (var insertable in group.Skip(count).Take(aimCount - count))
|
|
|
|
|
// insert any defaults which are missing.
|
|
|
|
|
context.DatabasedKeyBinding.Add(new DatabasedKeyBinding
|
|
|
|
|
{
|
|
|
|
|
KeyCombination = insertable.KeyCombination,
|
|
|
|
|
Action = insertable.Action,
|
|
|
|
|
RulesetID = rulesetId,
|
|
|
|
|
Variant = variant
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-10-16 16:58:17 +08:00
|
|
|
|
|
2017-10-22 15:17:55 +08:00
|
|
|
|
context.SaveChanges(transaction);
|
|
|
|
|
}
|
2017-08-14 21:31:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-16 16:58:17 +08:00
|
|
|
|
/// <summary>
|
2017-10-25 22:56:10 +08:00
|
|
|
|
/// Retrieve <see cref="DatabasedKeyBinding"/>s for a specified ruleset/variant content.
|
2017-10-16 16:58:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="rulesetId">The ruleset's internal ID.</param>
|
|
|
|
|
/// <param name="variant">An optional variant.</param>
|
|
|
|
|
/// <returns></returns>
|
2017-10-25 22:53:09 +08:00
|
|
|
|
public List<DatabasedKeyBinding> Query(int? rulesetId = null, int? variant = null) =>
|
2017-10-25 16:03:10 +08:00
|
|
|
|
GetContext().DatabasedKeyBinding.Where(b => b.RulesetID == rulesetId && b.Variant == variant).ToList();
|
2017-08-15 22:59:58 +08:00
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
|
public void Update(KeyBinding keyBinding)
|
|
|
|
|
{
|
2017-10-25 17:59:15 +08:00
|
|
|
|
var dbKeyBinding = (DatabasedKeyBinding)keyBinding;
|
|
|
|
|
|
2017-10-17 14:50:42 +08:00
|
|
|
|
var context = GetContext();
|
2017-10-25 17:59:15 +08:00
|
|
|
|
|
|
|
|
|
Refresh(ref dbKeyBinding);
|
|
|
|
|
|
2017-10-25 22:21:47 +08:00
|
|
|
|
dbKeyBinding.KeyCombination = keyBinding.KeyCombination;
|
|
|
|
|
|
2017-10-17 14:50:42 +08:00
|
|
|
|
context.SaveChanges();
|
2017-10-21 22:39:31 +08:00
|
|
|
|
|
|
|
|
|
KeyBindingChanged?.Invoke();
|
2017-10-05 03:52:12 +08:00
|
|
|
|
}
|
2017-08-09 13:50:10 +08:00
|
|
|
|
}
|
2017-08-14 19:19:25 +08:00
|
|
|
|
}
|