1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 02:43:19 +08:00

Improve default key binding logic

Defaults will be added to the database as long as the database has unbalanced counts for any actions.
This commit is contained in:
Dean Herbert 2017-08-15 23:59:58 +09:00
parent 3bee36f6a2
commit 2bd0981738
3 changed files with 30 additions and 24 deletions

View File

@ -44,7 +44,7 @@ namespace osu.Game.Input.Bindings
protected override void ReloadMappings() protected override void ReloadMappings()
{ {
KeyBindings = store.GetProcessedList(DefaultMappings, ruleset?.ID, variant); KeyBindings = store.Query(ruleset?.ID, variant);
} }
} }
} }

View File

@ -22,14 +22,11 @@ namespace osu.Game.Input
{ {
var ruleset = info.CreateInstance(); var ruleset = info.CreateInstance();
foreach (var variant in ruleset.AvailableVariants) foreach (var variant in ruleset.AvailableVariants)
GetProcessedList(ruleset.GetDefaultKeyBindings(), info.ID, variant); insertDefaults(ruleset.GetDefaultKeyBindings(), info.ID, variant);
} }
} }
public void Register(KeyBindingInputManager manager) public void Register(KeyBindingInputManager manager) => insertDefaults(manager.DefaultMappings);
{
GetProcessedList(manager.DefaultMappings);
}
protected override int StoreVersion => 3; protected override int StoreVersion => 3;
@ -61,29 +58,37 @@ namespace osu.Game.Input
Connection.CreateTable<DatabasedKeyBinding>(); Connection.CreateTable<DatabasedKeyBinding>();
} }
private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null)
{
var query = Query(rulesetId, variant);
// compare counts in database vs defaults
foreach (var group in defaults.GroupBy(k => k.Action))
{
int count;
while (group.Count() > (count = query.Count(k => (int)k.Action == (int)group.Key)))
{
var insertable = group.Skip(count).First();
// insert any defaults which are missing.
Connection.Insert(new DatabasedKeyBinding
{
KeyCombination = insertable.KeyCombination,
Action = (int)insertable.Action,
RulesetID = rulesetId,
Variant = variant
});
}
}
}
protected override Type[] ValidTypes => new[] protected override Type[] ValidTypes => new[]
{ {
typeof(DatabasedKeyBinding) typeof(DatabasedKeyBinding)
}; };
public IEnumerable<KeyBinding> GetProcessedList(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null) public IEnumerable<KeyBinding> Query(int? rulesetId = null, int? variant = null) =>
{ Query<DatabasedKeyBinding>(b => b.RulesetID == rulesetId && b.Variant == variant);
var databaseEntries = Query<DatabasedKeyBinding>(b => b.RulesetID == rulesetId && b.Variant == variant);
if (!databaseEntries.Any())
{
// if there are no entries for this category in the database, we should populate our defaults.
Connection.InsertAll(defaults.Select(k => new DatabasedKeyBinding
{
KeyCombination = k.KeyCombination,
Action = (int)k.Action,
RulesetID = rulesetId,
Variant = variant
}));
}
return databaseEntries;
}
public void Update(KeyBinding keyBinding) public void Update(KeyBinding keyBinding)
{ {

View File

@ -202,6 +202,7 @@ namespace osu.Game
} }
}); });
KeyBindingStore.Register(globalBinding);
dependencies.Cache(globalBinding); dependencies.Cache(globalBinding);
// TODO: This is temporary until we reimplement the local FPS display. // TODO: This is temporary until we reimplement the local FPS display.