Skip to content

Commit

Permalink
Merge pull request #27145 from tsunyoku/legacy-relax-replays-key-presses
Browse files Browse the repository at this point in the history
Create key-presses when watching legacy Relax replays
  • Loading branch information
bdach committed Feb 14, 2024
2 parents 3b8203a + 22e9c4a commit 7f76ff1
Showing 1 changed file with 69 additions and 5 deletions.
74 changes: 69 additions & 5 deletions osu.Game.Rulesets.Osu/Mods/OsuModRelax.cs
@@ -1,4 +1,4 @@
// 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.

using System;
Expand Down Expand Up @@ -38,28 +38,41 @@ public class OsuModRelax : ModRelax, IUpdatableByPlayfield, IApplicableToDrawabl
private ReplayState<OsuAction> state = null!;
private double lastStateChangeTime;

private DrawableOsuRuleset ruleset = null!;
private IPressHandler pressHandler = null!;

private bool hasReplay;
private bool legacyReplay;

public void ApplyToDrawableRuleset(DrawableRuleset<OsuHitObject> drawableRuleset)
{
ruleset = (DrawableOsuRuleset)drawableRuleset;

// grab the input manager for future use.
osuInputManager = ((DrawableOsuRuleset)drawableRuleset).KeyBindingInputManager;
osuInputManager = ruleset.KeyBindingInputManager;
}

public void ApplyToPlayer(Player player)
{
if (osuInputManager.ReplayInputHandler != null)
{
hasReplay = true;

Debug.Assert(ruleset.ReplayScore != null);
legacyReplay = ruleset.ReplayScore.ScoreInfo.IsLegacyScore;

pressHandler = legacyReplay ? new LegacyReplayPressHandler(this) : new PressHandler(this);

return;
}

pressHandler = new PressHandler(this);
osuInputManager.AllowGameplayInputs = false;
}

public void Update(Playfield playfield)
{
if (hasReplay)
if (hasReplay && !legacyReplay)
return;

bool requiresHold = false;
Expand Down Expand Up @@ -132,11 +145,62 @@ void changeState(bool down)

if (down)
{
state.PressedActions.Add(wasLeft ? OsuAction.LeftButton : OsuAction.RightButton);
pressHandler.HandlePress(wasLeft);
wasLeft = !wasLeft;
}
else
{
pressHandler.HandleRelease(wasLeft);
}
}
}

private interface IPressHandler
{
void HandlePress(bool wasLeft);
void HandleRelease(bool wasLeft);
}

private class PressHandler : IPressHandler
{
private readonly OsuModRelax mod;

public PressHandler(OsuModRelax mod)
{
this.mod = mod;
}

state.Apply(osuInputManager.CurrentState, osuInputManager);
public void HandlePress(bool wasLeft)
{
mod.state.PressedActions.Add(wasLeft ? OsuAction.LeftButton : OsuAction.RightButton);
mod.state.Apply(mod.osuInputManager.CurrentState, mod.osuInputManager);
}

public void HandleRelease(bool wasLeft)
{
mod.state.Apply(mod.osuInputManager.CurrentState, mod.osuInputManager);
}
}

// legacy replays do not contain key-presses with Relax mod, so they need to be triggered by themselves.
private class LegacyReplayPressHandler : IPressHandler
{
private readonly OsuModRelax mod;

public LegacyReplayPressHandler(OsuModRelax mod)
{
this.mod = mod;
}

public void HandlePress(bool wasLeft)
{
mod.osuInputManager.KeyBindingContainer.TriggerPressed(wasLeft ? OsuAction.LeftButton : OsuAction.RightButton);
}

public void HandleRelease(bool wasLeft)
{
// this intentionally releases right when `wasLeft` is true because `wasLeft` is set at point of press and not at point of release
mod.osuInputManager.KeyBindingContainer.TriggerReleased(wasLeft ? OsuAction.RightButton : OsuAction.LeftButton);
}
}
}
Expand Down

0 comments on commit 7f76ff1

Please sign in to comment.