1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-26 22:43:21 +08:00

Update write usages

This commit is contained in:
Dean Herbert 2021-01-13 17:59:47 +09:00
parent 9d744d629f
commit 9086d75542
4 changed files with 34 additions and 40 deletions

View File

@ -62,7 +62,12 @@ namespace osu.Game.Tests.Database
Assert.That(((IKeyBinding)backBinding).KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.Escape })); Assert.That(((IKeyBinding)backBinding).KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.Escape }));
keyBindingStore.Update(backBinding, binding => binding.KeyCombination = new KeyCombination(InputKey.BackSpace)); var binding = backBinding;
realmContextFactory.Context.Write(() =>
{
((IKeyBinding)binding).KeyCombination = new KeyCombination(InputKey.BackSpace);
});
Assert.That(((IKeyBinding)backBinding).KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace })); Assert.That(((IKeyBinding)backBinding).KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));

View File

@ -59,32 +59,6 @@ namespace osu.Game.Input
} }
} }
/// <summary>
/// Update the database mapping for the provided key binding.
/// </summary>
/// <param name="keyBinding">The key binding to update. Can be detached from the database.</param>
/// <param name="modification">The modification to apply to the key binding.</param>
public void Update(IHasGuidPrimaryKey keyBinding, Action<IKeyBinding> modification)
{
// the incoming instance could already be a live access object.
Live<RealmKeyBinding>? realmBinding = keyBinding as Live<RealmKeyBinding>;
using (var realm = RealmFactory.GetForWrite())
{
if (realmBinding == null)
{
// the incoming instance could be a raw realm object.
if (!(keyBinding is RealmKeyBinding rkb))
// if neither of the above cases succeeded, retrieve a realm object for further processing.
rkb = realm.Context.Find<RealmKeyBinding>(keyBinding.ID);
realmBinding = new Live<RealmKeyBinding>(rkb, RealmFactory);
}
realmBinding.PerformUpdate(modification);
}
}
private void insertDefaults(IEnumerable<IKeyBinding> defaults, int? rulesetId = null, int? variant = null) private void insertDefaults(IEnumerable<IKeyBinding> defaults, int? rulesetId = null, int? variant = null)
{ {
using (var usage = RealmFactory.GetForWrite()) using (var usage = RealmFactory.GetForWrite())

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
@ -16,7 +17,7 @@ using osu.Game.Database;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Input; using osu.Game.Input.Bindings;
using osuTK; using osuTK;
using osuTK.Graphics; using osuTK.Graphics;
using osuTK.Input; using osuTK.Input;
@ -26,7 +27,7 @@ namespace osu.Game.Overlays.KeyBinding
public class KeyBindingRow : Container, IFilterable public class KeyBindingRow : Container, IFilterable
{ {
private readonly object action; private readonly object action;
private readonly IEnumerable<IKeyBinding> bindings; private readonly IEnumerable<RealmKeyBinding> bindings;
private const float transition_time = 150; private const float transition_time = 150;
@ -52,9 +53,9 @@ namespace osu.Game.Overlays.KeyBinding
private FillFlowContainer cancelAndClearButtons; private FillFlowContainer cancelAndClearButtons;
private FillFlowContainer<KeyButton> buttons; private FillFlowContainer<KeyButton> buttons;
public IEnumerable<string> FilterTerms => bindings.Select(b => b.KeyCombination.ReadableString()).Prepend((string)text.Text); public IEnumerable<string> FilterTerms => bindings.Select(b => ((IKeyBinding)b).KeyCombination.ReadableString()).Prepend((string)text.Text);
public KeyBindingRow(object action, IEnumerable<IKeyBinding> bindings) public KeyBindingRow(object action, List<RealmKeyBinding> bindings)
{ {
this.action = action; this.action = action;
this.bindings = bindings; this.bindings = bindings;
@ -67,7 +68,7 @@ namespace osu.Game.Overlays.KeyBinding
} }
[Resolved] [Resolved]
private RealmKeyBindingStore store { get; set; } private RealmContextFactory realmFactory { get; set; }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colours) private void load(OsuColour colours)
@ -127,7 +128,12 @@ namespace osu.Game.Overlays.KeyBinding
{ {
var button = buttons[i++]; var button = buttons[i++];
button.UpdateKeyCombination(d); button.UpdateKeyCombination(d);
store.Update((IHasGuidPrimaryKey)button.KeyBinding, k => k.KeyCombination = button.KeyBinding.KeyCombination);
using (var write = realmFactory.GetForWrite())
{
var binding = write.Context.Find<RealmKeyBinding>(((IHasGuidPrimaryKey)button.KeyBinding).ID);
binding.KeyCombination = button.KeyBinding.KeyCombination;
}
} }
} }
@ -286,7 +292,11 @@ namespace osu.Game.Overlays.KeyBinding
{ {
if (bindTarget != null) if (bindTarget != null)
{ {
store.Update((IHasGuidPrimaryKey)bindTarget.KeyBinding, k => k.KeyCombination = bindTarget.KeyBinding.KeyCombination); using (var write = realmFactory.GetForWrite())
{
var binding = write.Context.Find<RealmKeyBinding>(((IHasGuidPrimaryKey)bindTarget.KeyBinding).ID);
binding.KeyCombination = bindTarget.KeyBinding.KeyCombination;
}
bindTarget.IsBinding = false; bindTarget.IsBinding = false;
Schedule(() => Schedule(() =>
@ -360,7 +370,7 @@ namespace osu.Game.Overlays.KeyBinding
public class KeyButton : Container public class KeyButton : Container
{ {
public readonly IKeyBinding KeyBinding; public readonly RealmKeyBinding KeyBinding;
private readonly Box box; private readonly Box box;
public readonly OsuSpriteText Text; public readonly OsuSpriteText Text;
@ -382,8 +392,11 @@ namespace osu.Game.Overlays.KeyBinding
} }
} }
public KeyButton(IKeyBinding keyBinding) public KeyButton(RealmKeyBinding keyBinding)
{ {
if (keyBinding.IsManaged)
throw new ArgumentException("Key binding should not be attached as we make temporary changes", nameof(keyBinding));
KeyBinding = keyBinding; KeyBinding = keyBinding;
Margin = new MarginPadding(padding); Margin = new MarginPadding(padding);
@ -416,7 +429,7 @@ namespace osu.Game.Overlays.KeyBinding
Margin = new MarginPadding(5), Margin = new MarginPadding(5),
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
Text = keyBinding.KeyCombination.ReadableString(), Text = ((IKeyBinding)keyBinding).KeyCombination.ReadableString(),
}, },
}; };
} }
@ -455,8 +468,10 @@ namespace osu.Game.Overlays.KeyBinding
public void UpdateKeyCombination(KeyCombination newCombination) public void UpdateKeyCombination(KeyCombination newCombination)
{ {
KeyBinding.KeyCombination = newCombination; var keyBinding = (IKeyBinding)KeyBinding;
Text.Text = KeyBinding.KeyCombination.ReadableString();
keyBinding.KeyCombination = newCombination;
Text.Text = keyBinding.KeyCombination.ReadableString();
} }
} }
} }

View File

@ -46,7 +46,7 @@ namespace osu.Game.Overlays.KeyBinding
int intKey = (int)defaultGroup.Key; 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(intKey))) Add(new KeyBindingRow(defaultGroup.Key, bindings.Where(b => b.Action.Equals(intKey)).ToList())
{ {
AllowMainMouseButtons = Ruleset != null, AllowMainMouseButtons = Ruleset != null,
Defaults = defaultGroup.Select(d => d.KeyCombination) Defaults = defaultGroup.Select(d => d.KeyCombination)