2020-09-30 14:45:14 +08:00
|
|
|
// 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.Collections.Generic;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2020-11-05 15:14:22 +08:00
|
|
|
using osu.Framework.Threading;
|
2020-09-30 14:45:14 +08:00
|
|
|
using osu.Game.Audio;
|
|
|
|
using osu.Game.Screens.Play;
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
|
|
|
public class PausableSkinnableSound : SkinnableSound
|
|
|
|
{
|
|
|
|
protected bool RequestedPlaying { get; private set; }
|
|
|
|
|
2020-11-19 18:51:09 +08:00
|
|
|
public PausableSkinnableSound(ISampleInfo sample)
|
|
|
|
: base(sample)
|
2020-09-30 14:45:14 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:51:09 +08:00
|
|
|
public PausableSkinnableSound(IEnumerable<ISampleInfo> samples)
|
|
|
|
: base(samples)
|
2020-09-30 14:45:14 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly IBindable<bool> samplePlaybackDisabled = new Bindable<bool>();
|
|
|
|
|
2020-11-05 15:14:22 +08:00
|
|
|
private ScheduledDelegate scheduledStart;
|
|
|
|
|
2020-09-30 14:45:14 +08:00
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
private void load(ISamplePlaybackDisabler samplePlaybackDisabler)
|
|
|
|
{
|
|
|
|
// if in a gameplay context, pause sample playback when gameplay is paused.
|
|
|
|
if (samplePlaybackDisabler != null)
|
|
|
|
{
|
|
|
|
samplePlaybackDisabled.BindTo(samplePlaybackDisabler.SamplePlaybackDisabled);
|
|
|
|
samplePlaybackDisabled.BindValueChanged(disabled =>
|
|
|
|
{
|
2020-10-05 14:07:46 +08:00
|
|
|
if (!RequestedPlaying) return;
|
|
|
|
|
|
|
|
// let non-looping samples that have already been started play out to completion (sounds better than abruptly cutting off).
|
|
|
|
if (!Looping) return;
|
|
|
|
|
2020-11-05 15:14:22 +08:00
|
|
|
cancelPendingStart();
|
|
|
|
|
2020-10-05 14:07:46 +08:00
|
|
|
if (disabled.NewValue)
|
|
|
|
base.Stop();
|
|
|
|
else
|
2020-09-30 14:45:14 +08:00
|
|
|
{
|
2020-10-05 14:07:46 +08:00
|
|
|
// schedule so we don't start playing a sample which is no longer alive.
|
2020-11-05 15:14:22 +08:00
|
|
|
scheduledStart = Schedule(() =>
|
2020-10-01 15:54:56 +08:00
|
|
|
{
|
2020-10-05 14:07:46 +08:00
|
|
|
if (RequestedPlaying)
|
|
|
|
base.Play();
|
|
|
|
});
|
2020-09-30 14:45:14 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Play()
|
|
|
|
{
|
2020-11-05 15:14:22 +08:00
|
|
|
cancelPendingStart();
|
2020-09-30 14:45:14 +08:00
|
|
|
RequestedPlaying = true;
|
|
|
|
|
|
|
|
if (samplePlaybackDisabled.Value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
base.Play();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Stop()
|
|
|
|
{
|
2020-11-05 15:14:22 +08:00
|
|
|
cancelPendingStart();
|
2020-09-30 14:45:14 +08:00
|
|
|
RequestedPlaying = false;
|
|
|
|
base.Stop();
|
|
|
|
}
|
2020-11-05 15:14:22 +08:00
|
|
|
|
|
|
|
private void cancelPendingStart()
|
|
|
|
{
|
|
|
|
scheduledStart?.Cancel();
|
|
|
|
scheduledStart = null;
|
|
|
|
}
|
2020-09-30 14:45:14 +08:00
|
|
|
}
|
|
|
|
}
|