Merge pull request #10169 from frenzibyte/fix-new-samples-starting-while-gameplay-paused

This commit is contained in:
Dean Herbert
2020-09-22 18:23:41 +09:00
committed by GitHub
2 changed files with 62 additions and 3 deletions

View File

@ -1,12 +1,17 @@
// 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq; using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Audio; using osu.Framework.Graphics.Audio;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.OpenGL.Textures;
using osu.Framework.Graphics.Textures;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Framework.Timing; using osu.Framework.Timing;
using osu.Game.Audio; using osu.Game.Audio;
@ -20,6 +25,7 @@ namespace osu.Game.Tests.Visual.Gameplay
[Cached] [Cached]
private GameplayClock gameplayClock = new GameplayClock(new FramedClock()); private GameplayClock gameplayClock = new GameplayClock(new FramedClock());
private TestSkinSourceContainer skinSource;
private SkinnableSound skinnableSound; private SkinnableSound skinnableSound;
[SetUp] [SetUp]
@ -29,7 +35,7 @@ namespace osu.Game.Tests.Visual.Gameplay
Children = new Drawable[] Children = new Drawable[]
{ {
new Container skinSource = new TestSkinSourceContainer
{ {
Clock = gameplayClock, Clock = gameplayClock,
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
@ -101,5 +107,55 @@ namespace osu.Game.Tests.Visual.Gameplay
AddAssert("sample not playing", () => !sample.Playing); AddAssert("sample not playing", () => !sample.Playing);
AddAssert("sample not playing", () => !sample.Playing); AddAssert("sample not playing", () => !sample.Playing);
} }
[Test]
public void TestSkinChangeDoesntPlayOnPause()
{
DrawableSample sample = null;
AddStep("start sample", () =>
{
skinnableSound.Play();
sample = skinnableSound.ChildrenOfType<DrawableSample>().Single();
});
AddAssert("sample playing", () => sample.Playing);
AddStep("pause gameplay clock", () => gameplayClock.IsPaused.Value = true);
AddUntilStep("wait for sample to stop playing", () => !sample.Playing);
AddStep("trigger skin change", () => skinSource.TriggerSourceChanged());
AddAssert("retrieve and ensure current sample is different", () =>
{
DrawableSample oldSample = sample;
sample = skinnableSound.ChildrenOfType<DrawableSample>().Single();
return sample != oldSample;
});
AddAssert("new sample stopped", () => !sample.Playing);
AddStep("resume gameplay clock", () => gameplayClock.IsPaused.Value = false);
AddWaitStep("wait a bit", 5);
AddAssert("new sample not played", () => !sample.Playing);
}
[Cached(typeof(ISkinSource))]
private class TestSkinSourceContainer : Container, ISkinSource
{
[Resolved]
private ISkinSource source { get; set; }
public event Action SourceChanged;
public Drawable GetDrawableComponent(ISkinComponent component) => source?.GetDrawableComponent(component);
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT) => source?.GetTexture(componentName, wrapModeS, wrapModeT);
public SampleChannel GetSample(ISampleInfo sampleInfo) => source?.GetSample(sampleInfo);
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => source?.GetConfig<TLookup, TValue>(lookup);
public void TriggerSourceChanged()
{
SourceChanged?.Invoke();
}
}
} }
} }

View File

@ -114,6 +114,8 @@ namespace osu.Game.Skinning
protected override void SkinChanged(ISkinSource skin, bool allowFallback) protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{ {
bool wasPlaying = IsPlaying;
var channels = hitSamples.Select(s => var channels = hitSamples.Select(s =>
{ {
var ch = skin.GetSample(s); var ch = skin.GetSample(s);
@ -138,8 +140,9 @@ namespace osu.Game.Skinning
samplesContainer.ChildrenEnumerable = channels.Select(c => new DrawableSample(c)); samplesContainer.ChildrenEnumerable = channels.Select(c => new DrawableSample(c));
if (requestedPlaying) // Start playback internally for the new samples if the previous ones were playing beforehand.
Play(); if (wasPlaying)
play();
} }
#region Re-expose AudioContainer #region Re-expose AudioContainer