introduce overrideable bool instead of copying event logic entirely

This commit is contained in:
Mysfit 2021-01-22 12:09:40 -05:00
parent 5b1bdfbdc5
commit e0f8f6a23f
2 changed files with 30 additions and 43 deletions

View File

@ -18,7 +18,7 @@ namespace osu.Game.Skinning
protected bool RequestedPlaying { get; private set; } protected bool RequestedPlaying { get; private set; }
private readonly IBindable<bool> samplePlaybackDisabled = new Bindable<bool>(); protected virtual bool AllowNonLoopingCutOff => false;
public PausableSkinnableSound() public PausableSkinnableSound()
{ {
@ -34,6 +34,8 @@ namespace osu.Game.Skinning
{ {
} }
private readonly IBindable<bool> samplePlaybackDisabled = new Bindable<bool>();
private ScheduledDelegate scheduledStart; private ScheduledDelegate scheduledStart;
[BackgroundDependencyLoader(true)] [BackgroundDependencyLoader(true)]
@ -43,28 +45,34 @@ namespace osu.Game.Skinning
if (samplePlaybackDisabler != null) if (samplePlaybackDisabler != null)
{ {
samplePlaybackDisabled.BindTo(samplePlaybackDisabler.SamplePlaybackDisabled); samplePlaybackDisabled.BindTo(samplePlaybackDisabler.SamplePlaybackDisabled);
samplePlaybackDisabled.BindValueChanged(SamplePlaybackDisabledChanged); samplePlaybackDisabled.BindValueChanged(disabled =>
}
}
protected virtual void SamplePlaybackDisabledChanged(ValueChangedEvent<bool> disabled)
{ {
if (!RequestedPlaying) return; if (!RequestedPlaying) return;
// let non-looping samples that have already been started play out to completion (sounds better than abruptly cutting off). // if the sample is non-looping, and non-looping cut off is not allowed,
if (!Looping) return; // let the sample play out to completion (sounds better than abruptly cutting off).
if (!Looping && !AllowNonLoopingCutOff) return;
CancelPendingStart(); cancelPendingStart();
if (disabled.NewValue) if (disabled.NewValue)
base.Stop(); base.Stop();
else else
ScheduleStart(); {
// schedule so we don't start playing a sample which is no longer alive.
scheduledStart = Schedule(() =>
{
if (RequestedPlaying)
base.Play();
});
}
});
}
} }
public override void Play(bool restart = true) public override void Play(bool restart = true)
{ {
CancelPendingStart(); cancelPendingStart();
RequestedPlaying = true; RequestedPlaying = true;
if (samplePlaybackDisabled.Value) if (samplePlaybackDisabled.Value)
@ -75,25 +83,15 @@ namespace osu.Game.Skinning
public override void Stop() public override void Stop()
{ {
CancelPendingStart(); cancelPendingStart();
RequestedPlaying = false; RequestedPlaying = false;
base.Stop(); base.Stop();
} }
protected void CancelPendingStart() private void cancelPendingStart()
{ {
scheduledStart?.Cancel(); scheduledStart?.Cancel();
scheduledStart = null; scheduledStart = null;
} }
protected void ScheduleStart()
{
// schedule so we don't start playing a sample which is no longer alive.
scheduledStart = Schedule(() =>
{
if (RequestedPlaying)
base.Play();
});
}
} }
} }

View File

@ -21,6 +21,8 @@ namespace osu.Game.Storyboards.Drawables
public override bool RemoveWhenNotAlive => false; public override bool RemoveWhenNotAlive => false;
protected override bool AllowNonLoopingCutOff => true;
public DrawableStoryboardSample(StoryboardSampleInfo sampleInfo) public DrawableStoryboardSample(StoryboardSampleInfo sampleInfo)
: base(sampleInfo) : base(sampleInfo)
{ {
@ -31,19 +33,6 @@ namespace osu.Game.Storyboards.Drawables
[Resolved] [Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; } private IBindable<IReadOnlyList<Mod>> mods { get; set; }
protected override void SamplePlaybackDisabledChanged(ValueChangedEvent<bool> disabled)
{
if (!RequestedPlaying) return;
if (disabled.NewValue)
Stop();
else
{
CancelPendingStart();
ScheduleStart();
}
}
protected override void SkinChanged(ISkinSource skin, bool allowFallback) protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{ {
base.SkinChanged(skin, allowFallback); base.SkinChanged(skin, allowFallback);