1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 17:23:22 +08:00

Correct and simplify KeyBindingStore

This commit is contained in:
Dean Herbert 2017-10-16 17:58:17 +09:00
parent acc299c7b9
commit 9ee6d1e3f9
2 changed files with 19 additions and 15 deletions

View File

@ -38,11 +38,13 @@ namespace osu.Game.Input
// compare counts in database vs defaults // compare counts in database vs defaults
foreach (var group in defaults.GroupBy(k => k.Action)) foreach (var group in defaults.GroupBy(k => k.Action))
{ {
int count; int count = Query(rulesetId, variant).Count(k => (int)k.Action == (int)group.Key);
while (group.Count() > (count = Query(rulesetId, variant).Count(k => (int)k.Action == (int)group.Key))) int aimCount = group.Count();
{
var insertable = group.Skip(count).First();
if (aimCount <= count)
continue;
foreach (var insertable in group.Skip(count).Take(aimCount - count))
// insert any defaults which are missing. // insert any defaults which are missing.
Connection.DatabasedKeyBinding.Add(new DatabasedKeyBinding Connection.DatabasedKeyBinding.Add(new DatabasedKeyBinding
{ {
@ -51,22 +53,22 @@ namespace osu.Game.Input
RulesetID = rulesetId, RulesetID = rulesetId,
Variant = variant Variant = variant
}); });
Connection.SaveChanges();
}
} }
Connection.SaveChanges();
} }
public List<KeyBinding> Query(int? rulesetId = null, int? variant = null) => /// <summary>
new List<KeyBinding>(Connection.DatabasedKeyBinding.Where(b => b.RulesetID == rulesetId && b.Variant == variant)); /// Retrieve <see cref="KeyBinding"/>s for a specified ruleset/variant content.
/// </summary>
/// <param name="rulesetId">The ruleset's internal ID.</param>
/// <param name="variant">An optional variant.</param>
/// <returns></returns>
public IEnumerable<KeyBinding> Query(int? rulesetId = null, int? variant = null) => Connection.DatabasedKeyBinding.Where(b => b.RulesetID == rulesetId && b.Variant == variant);
public void Update(KeyBinding keyBinding) public void Update(KeyBinding keyBinding)
{ {
var dbKeyBinding = Connection.DatabasedKeyBinding.FirstOrDefault(kb => kb.ToString() == keyBinding.ToString()); Connection.Update(keyBinding);
if (dbKeyBinding != null)
{
dbKeyBinding.KeyCombination = keyBinding.KeyCombination;
dbKeyBinding.Action = keyBinding.Action;
}
Connection.SaveChanges(); Connection.SaveChanges();
} }
} }

View File

@ -37,8 +37,10 @@ namespace osu.Game.Overlays.KeyBinding
foreach (var defaultGroup in Defaults.GroupBy(d => d.Action)) foreach (var defaultGroup in Defaults.GroupBy(d => d.Action))
{ {
int intKey = (int)defaultGroup.Key;
// one row per valid action. // one row per valid action.
Add(new KeyBindingRow(defaultGroup.Key, bindings.Where(b => b.Action.Equals((int)defaultGroup.Key))) Add(new KeyBindingRow(defaultGroup.Key, bindings.Where(b => ((int)b.Action).Equals(intKey)))
{ {
AllowMainMouseButtons = Ruleset != null, AllowMainMouseButtons = Ruleset != null,
Defaults = defaultGroup.Select(d => d.KeyCombination) Defaults = defaultGroup.Select(d => d.KeyCombination)