1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:33:30 +08:00

Add method to unmap RealmKeyBinding.Action to actual enum-typed value

This commit is contained in:
Bartłomiej Dach 2023-10-12 14:06:39 +02:00
parent d2044fc2cc
commit 2a0e4c364d
No known key found for this signature in database
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,48 @@
// 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.
using System;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Input.Bindings;
using osu.Framework.Testing;
using osu.Game.Input.Bindings;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Catch;
using osu.Game.Rulesets.Mania;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Taiko;
using osu.Game.Tests.Visual;
using osuTK.Input;
namespace osu.Game.Tests.Input
{
[HeadlessTest]
public partial class RealmKeyBindingTest : OsuTestScene
{
[Resolved]
private RulesetStore rulesets { get; set; } = null!;
[Test]
public void TestUnmapGlobalAction()
{
var keyBinding = new RealmKeyBinding(GlobalAction.ToggleReplaySettings, KeyCombination.FromKey(Key.Z));
AddAssert("action is integer", () => keyBinding.Action, () => Is.EqualTo((int)GlobalAction.ToggleReplaySettings));
AddAssert("action unmaps correctly", () => keyBinding.GetAction(rulesets), () => Is.EqualTo(GlobalAction.ToggleReplaySettings));
}
[TestCase(typeof(OsuRuleset), OsuAction.Smoke, null)]
[TestCase(typeof(TaikoRuleset), TaikoAction.LeftCentre, null)]
[TestCase(typeof(CatchRuleset), CatchAction.MoveRight, null)]
[TestCase(typeof(ManiaRuleset), ManiaAction.Key7, 7)]
public void TestUnmapRulesetActions(Type rulesetType, object action, int? variant)
{
string rulesetName = ((Ruleset)Activator.CreateInstance(rulesetType)!).ShortName;
var keyBinding = new RealmKeyBinding(action, KeyCombination.FromKey(Key.Z), rulesetName, variant);
AddAssert("action is integer", () => keyBinding.Action, () => Is.EqualTo((int)action));
AddAssert("action unmaps correctly", () => keyBinding.GetAction(rulesets), () => Is.EqualTo(action));
}
}
}

View File

@ -2,9 +2,11 @@
// 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;
using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using osu.Framework.Input.Bindings; using osu.Framework.Input.Bindings;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Rulesets;
using Realms; using Realms;
namespace osu.Game.Input.Bindings namespace osu.Game.Input.Bindings
@ -26,6 +28,13 @@ namespace osu.Game.Input.Bindings
set => KeyCombinationString = value.ToString(); set => KeyCombinationString = value.ToString();
} }
/// <summary>
/// The resultant action which is triggered by this binding.
/// </summary>
/// <remarks>
/// This implementation always returns an integer.
/// If wanting to get the actual enum-typed value, use <see cref="GetAction"/>.
/// </remarks>
[Ignored] [Ignored]
public object Action public object Action
{ {
@ -53,5 +62,20 @@ namespace osu.Game.Input.Bindings
private RealmKeyBinding() private RealmKeyBinding()
{ {
} }
public object GetAction(RulesetStore rulesets)
{
if (string.IsNullOrEmpty(RulesetName))
return (GlobalAction)ActionInt;
var ruleset = rulesets.GetRuleset(RulesetName);
var actionType = ruleset!.CreateInstance()
.GetDefaultKeyBindings(Variant ?? 0)
.First() // let's just assume nobody does something stupid like mix multiple types...
.Action
.GetType();
return Enum.ToObject(actionType, ActionInt);
}
} }
} }