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;
|
|
|
|
|
|
2018-02-12 16:55:11 +08:00
|
|
|
|
public KeyBindingStore(DatabaseContextFactory contextFactory, RulesetStore rulesets, Storage storage = null)
|
|
|
|
|
: base(contextFactory, storage)
|
2017-08-09 13:50:10 +08:00
|
|
|
|
{
|
2018-02-12 16:55:11 +08:00
|
|
|
|
using (ContextFactory.GetForWrite())
|
2017-08-14 21:31:23 +08:00
|
|
|
|
{
|
2018-02-12 16:55:11 +08:00
|
|
|
|
foreach (var info in rulesets.AvailableRulesets)
|
|
|
|
|
{
|
|
|
|
|
var ruleset = info.CreateInstance();
|
|
|
|
|
foreach (var variant in ruleset.AvailableVariants)
|
|
|
|
|
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
|
|
|
|
{
|
2018-02-12 16:55:11 +08:00
|
|
|
|
using (var usage = ContextFactory.GetForWrite())
|
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.
|
2018-02-12 18:57:21 +08:00
|
|
|
|
usage.Context.DatabasedKeyBinding.Add(new DatabasedKeyBinding
|
2017-10-22 15:17:55 +08:00
|
|
|
|
{
|
|
|
|
|
KeyCombination = insertable.KeyCombination,
|
|
|
|
|
Action = insertable.Action,
|
|
|
|
|
RulesetID = rulesetId,
|
|
|
|
|
Variant = variant
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
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) =>
|
2018-02-12 16:55:11 +08:00
|
|
|
|
ContextFactory.Get().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)
|
|
|
|
|
{
|
2018-02-12 16:55:11 +08:00
|
|
|
|
using (ContextFactory.GetForWrite())
|
|
|
|
|
{
|
|
|
|
|
var dbKeyBinding = (DatabasedKeyBinding)keyBinding;
|
|
|
|
|
Refresh(ref dbKeyBinding);
|
2018-02-12 18:57:21 +08:00
|
|
|
|
|
|
|
|
|
if (dbKeyBinding.KeyCombination.Equals(keyBinding.KeyCombination))
|
|
|
|
|
return;
|
|
|
|
|
|
2018-02-12 16:55:11 +08:00
|
|
|
|
dbKeyBinding.KeyCombination = keyBinding.KeyCombination;
|
|
|
|
|
}
|
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
|
|
|
|
}
|