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

Rename realm persisted properties to avoid casting necessity

This commit is contained in:
Dean Herbert 2021-01-13 18:07:35 +09:00
parent 9086d75542
commit fcb4a53f37
7 changed files with 36 additions and 36 deletions

View File

@ -45,8 +45,8 @@ namespace osu.Game.Tests.Database
Assert.That(query().Count, Is.EqualTo(3));
Assert.That(query().Where(k => k.Action == (int)GlobalAction.Back).Count, Is.EqualTo(1));
Assert.That(query().Where(k => k.Action == (int)GlobalAction.Select).Count, Is.EqualTo(2));
Assert.That(query().Where(k => k.ActionInt == (int)GlobalAction.Back).Count, Is.EqualTo(1));
Assert.That(query().Where(k => k.ActionInt == (int)GlobalAction.Select).Count, Is.EqualTo(2));
}
private IQueryable<RealmKeyBinding> query() => realmContextFactory.Context.All<RealmKeyBinding>();
@ -58,22 +58,22 @@ namespace osu.Game.Tests.Database
keyBindingStore.Register(testContainer);
var backBinding = query().Single(k => k.Action == (int)GlobalAction.Back);
var backBinding = query().Single(k => k.ActionInt == (int)GlobalAction.Back);
Assert.That(((IKeyBinding)backBinding).KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.Escape }));
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.Escape }));
var binding = backBinding;
realmContextFactory.Context.Write(() =>
{
((IKeyBinding)binding).KeyCombination = new KeyCombination(InputKey.BackSpace);
binding.KeyCombination = new KeyCombination(InputKey.BackSpace);
});
Assert.That(((IKeyBinding)backBinding).KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
// check still correct after re-query.
backBinding = query().Single(k => k.Action == (int)GlobalAction.Back);
Assert.That(((IKeyBinding)backBinding).KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
backBinding = query().Single(k => k.ActionInt == (int)GlobalAction.Back);
Assert.That(backBinding.KeyCombination.Keys, Is.EquivalentTo(new[] { InputKey.BackSpace }));
}
[TearDown]

View File

@ -7,7 +7,7 @@ using Realms;
namespace osu.Game.Input.Bindings
{
[MapTo("KeyBinding")]
[MapTo(nameof(KeyBinding))]
public class RealmKeyBinding : RealmObject, IHasGuidPrimaryKey, IKeyBinding
{
[PrimaryKey]
@ -17,20 +17,22 @@ namespace osu.Game.Input.Bindings
public int? Variant { get; set; }
KeyCombination IKeyBinding.KeyCombination
public KeyCombination KeyCombination
{
get => KeyCombination;
set => KeyCombination = value.ToString();
get => KeyCombinationString;
set => KeyCombinationString = value.ToString();
}
object IKeyBinding.Action
public object Action
{
get => Action;
set => Action = (int)value;
get => ActionInt;
set => ActionInt = (int)value;
}
public int Action { get; set; }
[MapTo(nameof(Action))]
public int ActionInt { get; set; }
public string KeyCombination { get; set; }
[MapTo(nameof(KeyCombination))]
public string KeyCombinationString { get; set; }
}
}

View File

@ -28,9 +28,9 @@ namespace osu.Game.Input
/// <returns>A set of display strings for all the user's key configuration for the action.</returns>
public IEnumerable<string> GetReadableKeyCombinationsFor(GlobalAction globalAction)
{
foreach (var action in RealmFactory.Context.All<RealmKeyBinding>().Where(b => (GlobalAction)b.Action == globalAction))
foreach (var action in RealmFactory.Context.All<RealmKeyBinding>().Where(b => (GlobalAction)b.ActionInt == globalAction))
{
string str = ((IKeyBinding)action).KeyCombination.ReadableString();
string str = action.KeyCombination.ReadableString();
// even if found, the readable string may be empty for an unbound action.
if (str.Length > 0)
@ -66,7 +66,7 @@ namespace osu.Game.Input
// compare counts in database vs defaults
foreach (var group in defaults.GroupBy(k => k.Action))
{
int count = usage.Context.All<RealmKeyBinding>().Count(k => k.RulesetID == rulesetId && k.Variant == variant && k.Action == (int)group.Key);
int count = usage.Context.All<RealmKeyBinding>().Count(k => k.RulesetID == rulesetId && k.Variant == variant && k.ActionInt == (int)group.Key);
int aimCount = group.Count();
if (aimCount <= count)
@ -78,8 +78,8 @@ namespace osu.Game.Input
usage.Context.Add(new RealmKeyBinding
{
ID = Guid.NewGuid().ToString(),
KeyCombination = insertable.KeyCombination.ToString(),
Action = (int)insertable.Action,
KeyCombinationString = insertable.KeyCombination.ToString(),
ActionInt = (int)insertable.Action,
RulesetID = rulesetId,
Variant = variant
});

View File

@ -342,8 +342,8 @@ namespace osu.Game
realm.Context.Add(new RealmKeyBinding
{
ID = Guid.NewGuid().ToString(),
KeyCombination = dkb.KeyCombination.ToString(),
Action = (int)dkb.Action,
KeyCombinationString = dkb.KeyCombination.ToString(),
ActionInt = (int)dkb.Action,
RulesetID = dkb.RulesetID,
Variant = dkb.Variant
});

View File

@ -53,7 +53,7 @@ namespace osu.Game.Overlays.KeyBinding
private FillFlowContainer cancelAndClearButtons;
private FillFlowContainer<KeyButton> buttons;
public IEnumerable<string> FilterTerms => bindings.Select(b => ((IKeyBinding)b).KeyCombination.ReadableString()).Prepend((string)text.Text);
public IEnumerable<string> FilterTerms => bindings.Select(b => b.KeyCombination.ReadableString()).Prepend((string)text.Text);
public KeyBindingRow(object action, List<RealmKeyBinding> bindings)
{
@ -132,7 +132,7 @@ namespace osu.Game.Overlays.KeyBinding
using (var write = realmFactory.GetForWrite())
{
var binding = write.Context.Find<RealmKeyBinding>(((IHasGuidPrimaryKey)button.KeyBinding).ID);
binding.KeyCombination = button.KeyBinding.KeyCombination;
binding.KeyCombinationString = button.KeyBinding.KeyCombinationString;
}
}
}
@ -295,7 +295,7 @@ namespace osu.Game.Overlays.KeyBinding
using (var write = realmFactory.GetForWrite())
{
var binding = write.Context.Find<RealmKeyBinding>(((IHasGuidPrimaryKey)bindTarget.KeyBinding).ID);
binding.KeyCombination = bindTarget.KeyBinding.KeyCombination;
binding.KeyCombinationString = bindTarget.KeyBinding.KeyCombinationString;
}
bindTarget.IsBinding = false;
@ -429,7 +429,7 @@ namespace osu.Game.Overlays.KeyBinding
Margin = new MarginPadding(5),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = ((IKeyBinding)keyBinding).KeyCombination.ReadableString(),
Text = keyBinding.KeyCombination.ReadableString(),
},
};
}
@ -468,10 +468,8 @@ namespace osu.Game.Overlays.KeyBinding
public void UpdateKeyCombination(KeyCombination newCombination)
{
var keyBinding = (IKeyBinding)KeyBinding;
keyBinding.KeyCombination = newCombination;
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;
// one row per valid action.
Add(new KeyBindingRow(defaultGroup.Key, bindings.Where(b => b.Action.Equals(intKey)).ToList())
Add(new KeyBindingRow(defaultGroup.Key, bindings.Where(b => b.ActionInt.Equals(intKey)).ToList())
{
AllowMainMouseButtons = Ruleset != null,
Defaults = defaultGroup.Select(d => d.KeyCombination)

View File

@ -165,13 +165,13 @@ namespace osu.Game.Overlays.Toolbar
if (Hotkey != null)
{
realmKeyBinding = realmFactory.Context.All<RealmKeyBinding>().FirstOrDefault(rkb => rkb.RulesetID == null && rkb.Action == (int)Hotkey.Value);
realmKeyBinding = realmFactory.Context.All<RealmKeyBinding>().FirstOrDefault(rkb => rkb.RulesetID == null && rkb.ActionInt == (int)Hotkey.Value);
if (realmKeyBinding != null)
{
realmKeyBinding.PropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(realmKeyBinding.KeyCombination))
if (args.PropertyName == nameof(realmKeyBinding.KeyCombinationString))
updateKeyBindingTooltip();
};
}
@ -223,7 +223,7 @@ namespace osu.Game.Overlays.Toolbar
{
if (realmKeyBinding != null)
{
KeyCombination? binding = ((IKeyBinding)realmKeyBinding).KeyCombination;
KeyCombination? binding = realmKeyBinding.KeyCombination;
var keyBindingString = binding?.ReadableString();