1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 05:42:56 +08:00

Merge pull request #13235 from ekrctb/update-lifetime-1

Update hit object lifetime on `HitObject.DefaultsApplied`
This commit is contained in:
Dan Balasescu 2021-05-31 14:44:40 +09:00 committed by GitHub
commit 8c6277f0d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 3 deletions

View File

@ -3,6 +3,8 @@
using NUnit.Framework;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Tests.Visual;
@ -70,15 +72,46 @@ namespace osu.Game.Tests.Gameplay
AddAssert("Lifetime is changed", () => entry.LifetimeStart == double.MinValue && entry.LifetimeEnd == 1000);
}
[Test]
public void TestLifetimeUpdatedOnDefaultApplied()
{
TestLifetimeEntry entry = null;
AddStep("Create entry", () => entry = new TestLifetimeEntry(new HitObject()) { LifetimeStart = 1 });
AddStep("ApplyDefaults", () => entry.HitObject.ApplyDefaults(new ControlPointInfo(), new BeatmapDifficulty()));
AddAssert("Lifetime is updated", () => entry.LifetimeStart == -TestLifetimeEntry.INITIAL_LIFETIME_OFFSET);
TestDrawableHitObject dho = null;
AddStep("Create DHO", () =>
{
dho = new TestDrawableHitObject(null);
dho.Apply(entry);
Child = dho;
dho.SetLifetimeStartOnApply = true;
});
AddStep("ApplyDefaults", () => entry.HitObject.ApplyDefaults(new ControlPointInfo(), new BeatmapDifficulty()));
AddAssert("Lifetime is correct", () => dho.LifetimeStart == TestDrawableHitObject.LIFETIME_ON_APPLY && entry.LifetimeStart == TestDrawableHitObject.LIFETIME_ON_APPLY);
}
private class TestDrawableHitObject : DrawableHitObject
{
public const double INITIAL_LIFETIME_OFFSET = 100;
public const double LIFETIME_ON_APPLY = 222;
protected override double InitialLifetimeOffset => INITIAL_LIFETIME_OFFSET;
public bool SetLifetimeStartOnApply;
public TestDrawableHitObject(HitObject hitObject)
: base(hitObject)
{
}
protected override void OnApply()
{
base.OnApply();
if (SetLifetimeStartOnApply)
LifetimeStart = LIFETIME_ON_APPLY;
}
}
private class TestLifetimeEntry : HitObjectLifetimeEntry

View File

@ -35,7 +35,11 @@ namespace osu.Game.Rulesets.Objects
HitObject = hitObject;
startTimeBindable.BindTo(HitObject.StartTimeBindable);
startTimeBindable.BindValueChanged(onStartTimeChanged, true);
startTimeBindable.BindValueChanged(_ => setInitialLifetime(), true);
// Subscribe to this event before the DrawableHitObject so that the local callback is invoked before the entry is re-applied as a result of DefaultsApplied.
// This way, the DrawableHitObject can use OnApply() to overwrite the LifetimeStart that was set inside setInitialLifetime().
HitObject.DefaultsApplied += _ => setInitialLifetime();
}
// The lifetime, as set by the hitobject.
@ -88,8 +92,8 @@ namespace osu.Game.Rulesets.Objects
protected virtual double InitialLifetimeOffset => 10000;
/// <summary>
/// Resets <see cref="LifetimeEntry.LifetimeStart"/> according to the change in start time of the <see cref="HitObject"/>.
/// Set <see cref="LifetimeEntry.LifetimeStart"/> using <see cref="InitialLifetimeOffset"/>.
/// </summary>
private void onStartTimeChanged(ValueChangedEvent<double> startTime) => LifetimeStart = HitObject.StartTime - InitialLifetimeOffset;
private void setInitialLifetime() => LifetimeStart = HitObject.StartTime - InitialLifetimeOffset;
}
}