1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 22:02:54 +08:00
osu-lazer/osu.Game/Input/KeyBindingStore.cs

87 lines
3.2 KiB
C#
Raw Normal View History

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
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Input.Bindings;
using osu.Framework.Platform;
using osu.Game.Database;
2017-08-11 15:11:46 +08:00
using osu.Game.Input.Bindings;
using osu.Game.Rulesets;
namespace osu.Game.Input
{
public class KeyBindingStore : DatabaseBackedStore
{
2017-10-21 22:39:31 +08:00
public event Action KeyBindingChanged;
public KeyBindingStore(Func<OsuDbContext> createContext, RulesetStore rulesets, Storage storage = null)
: base(createContext, storage)
{
2017-10-16 16:02:31 +08:00
foreach (var info in rulesets.AvailableRulesets)
{
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);
}
}
public void Register(KeyBindingContainer manager) => insertDefaults(manager.DefaultKeyBindings);
private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null)
{
var context = GetContext();
using (var transaction = context.BeginTransaction())
{
// compare counts in database vs defaults
foreach (var group in defaults.GroupBy(k => k.Action))
{
int count = Query(rulesetId, variant).Count(k => (int)k.Action == (int)group.Key);
int aimCount = group.Count();
if (aimCount <= count)
continue;
2017-10-16 16:58:17 +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
context.SaveChanges(transaction);
}
}
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) =>
GetContext().DatabasedKeyBinding.Where(b => b.RulesetID == rulesetId && b.Variant == variant).ToList();
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);
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
}
}
}