Skip to content

Commit

Permalink
Add method to unmap RealmKeyBinding.Action to actual enum-typed value
Browse files Browse the repository at this point in the history
  • Loading branch information
bdach committed Oct 12, 2023
1 parent d2044fc commit 2a0e4c3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
48 changes: 48 additions & 0 deletions osu.Game.Tests/Input/RealmKeyBindingTest.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
24 changes: 24 additions & 0 deletions osu.Game/Input/Bindings/RealmKeyBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Input.Bindings;
using osu.Game.Database;
using osu.Game.Rulesets;
using Realms;

namespace osu.Game.Input.Bindings
Expand All @@ -26,6 +28,13 @@ public KeyCombination KeyCombination
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]
public object Action
{
Expand Down Expand Up @@ -53,5 +62,20 @@ public RealmKeyBinding(object action, KeyCombination keyCombination, string? rul
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);
}
}
}

0 comments on commit 2a0e4c3

Please sign in to comment.