2020-09-30 06:45:14 +00: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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-09-30 06:45:14 +00:00
|
|
|
using System.Collections.Generic;
|
2020-12-03 03:32:49 +00:00
|
|
|
using System.Linq;
|
2020-11-19 11:38:36 +00:00
|
|
|
using JetBrains.Annotations;
|
2020-09-30 06:45:14 +00:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2020-11-05 07:14:22 +00:00
|
|
|
using osu.Framework.Threading;
|
2020-09-30 06:45:14 +00:00
|
|
|
using osu.Game.Audio;
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
|
|
|
public partial class PausableSkinnableSound : SkinnableSound
|
|
|
|
{
|
2020-12-04 04:49:08 +00:00
|
|
|
public double Length => !DrawableSamples.Any() ? 0 : DrawableSamples.Max(sample => sample.Length);
|
2020-12-03 03:32:49 +00:00
|
|
|
|
2021-04-20 07:51:00 +00:00
|
|
|
public bool RequestedPlaying { get; private set; }
|
2020-09-30 06:45:14 +00:00
|
|
|
|
2020-11-19 11:38:36 +00:00
|
|
|
public PausableSkinnableSound()
|
2020-09-30 06:45:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-11-19 11:38:36 +00:00
|
|
|
public PausableSkinnableSound([NotNull] IEnumerable<ISampleInfo> samples)
|
2020-11-19 10:51:09 +00:00
|
|
|
: base(samples)
|
2020-09-30 06:45:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-11-19 11:38:36 +00:00
|
|
|
public PausableSkinnableSound([NotNull] ISampleInfo sample)
|
|
|
|
: base(sample)
|
2020-09-30 06:45:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly IBindable<bool> samplePlaybackDisabled = new Bindable<bool>();
|
|
|
|
|
2020-11-05 07:14:22 +00:00
|
|
|
private ScheduledDelegate scheduledStart;
|
|
|
|
|
2020-09-30 06:45:14 +00: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);
|
2021-01-26 05:36:32 +00:00
|
|
|
samplePlaybackDisabled.BindValueChanged(SamplePlaybackDisabledChanged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void SamplePlaybackDisabledChanged(ValueChangedEvent<bool> disabled)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
cancelPendingStart();
|
|
|
|
|
|
|
|
if (disabled.NewValue)
|
|
|
|
base.Stop();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// schedule so we don't start playing a sample which is no longer alive.
|
|
|
|
scheduledStart = Schedule(() =>
|
2020-09-30 06:45:14 +00:00
|
|
|
{
|
2021-01-26 05:36:32 +00:00
|
|
|
if (RequestedPlaying)
|
|
|
|
base.Play();
|
2020-09-30 06:45:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 08:11:40 +00:00
|
|
|
public override void Play()
|
2020-09-30 06:45:14 +00:00
|
|
|
{
|
2020-11-05 07:14:22 +00:00
|
|
|
cancelPendingStart();
|
2020-09-30 06:45:14 +00:00
|
|
|
RequestedPlaying = true;
|
|
|
|
|
|
|
|
if (samplePlaybackDisabled.Value)
|
|
|
|
return;
|
|
|
|
|
2021-01-19 08:11:40 +00:00
|
|
|
base.Play();
|
2020-09-30 06:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Stop()
|
|
|
|
{
|
2020-11-05 07:14:22 +00:00
|
|
|
cancelPendingStart();
|
2020-09-30 06:45:14 +00:00
|
|
|
RequestedPlaying = false;
|
|
|
|
base.Stop();
|
|
|
|
}
|
2020-11-05 07:14:22 +00:00
|
|
|
|
|
|
|
private void cancelPendingStart()
|
|
|
|
{
|
|
|
|
scheduledStart?.Cancel();
|
|
|
|
scheduledStart = null;
|
|
|
|
}
|
2020-09-30 06:45:14 +00:00
|
|
|
}
|
|
|
|
}
|