2021-06-02 15:50:58 +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.
|
|
|
|
|
2022-03-03 01:33:46 +08:00
|
|
|
using System.Collections.Generic;
|
2023-11-08 11:37:29 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Threading;
|
2021-06-03 11:33:16 +08:00
|
|
|
using osu.Framework.Allocation;
|
2022-03-03 01:33:46 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-06-03 11:33:16 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-06-04 13:56:10 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-06-02 15:50:58 +08:00
|
|
|
using osu.Framework.Timing;
|
|
|
|
using osu.Game.Beatmaps;
|
2021-12-27 18:28:20 +08:00
|
|
|
using osu.Game.Overlays;
|
2022-03-03 01:33:46 +08:00
|
|
|
using osu.Game.Rulesets.Mods;
|
2023-11-08 11:37:29 +08:00
|
|
|
using osu.Game.Screens;
|
2021-06-02 15:50:58 +08:00
|
|
|
using osu.Game.Storyboards.Drawables;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Backgrounds
|
|
|
|
{
|
|
|
|
public partial class BeatmapBackgroundWithStoryboard : BeatmapBackground
|
|
|
|
{
|
2022-01-12 05:00:04 +08:00
|
|
|
private readonly InterpolatingFramedClock storyboardClock;
|
2021-12-27 18:28:20 +08:00
|
|
|
|
2023-11-08 11:37:29 +08:00
|
|
|
private AudioContainer storyboardContainer = null!;
|
|
|
|
private DrawableStoryboard? drawableStoryboard;
|
|
|
|
private CancellationTokenSource? loadCancellationSource = new CancellationTokenSource();
|
|
|
|
|
2021-12-27 18:28:20 +08:00
|
|
|
[Resolved(CanBeNull = true)]
|
|
|
|
private MusicController? musicController { get; set; }
|
|
|
|
|
2022-03-03 01:33:46 +08:00
|
|
|
[Resolved]
|
|
|
|
private IBindable<IReadOnlyList<Mod>> mods { get; set; } = null!;
|
|
|
|
|
2021-06-02 15:50:58 +08:00
|
|
|
public BeatmapBackgroundWithStoryboard(WorkingBeatmap beatmap, string fallbackTextureName = "Backgrounds/bg1")
|
|
|
|
: base(beatmap, fallbackTextureName)
|
|
|
|
{
|
2022-01-12 05:00:04 +08:00
|
|
|
storyboardClock = new InterpolatingFramedClock();
|
2021-06-02 15:50:58 +08:00
|
|
|
}
|
|
|
|
|
2021-06-03 11:33:16 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
2021-06-02 15:50:58 +08:00
|
|
|
{
|
2023-11-08 11:37:29 +08:00
|
|
|
AddInternal(storyboardContainer = new AudioContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Volume = { Value = 0 },
|
|
|
|
});
|
|
|
|
|
2023-11-11 07:57:17 +08:00
|
|
|
LoadStoryboard(false);
|
2023-11-08 11:37:29 +08:00
|
|
|
}
|
|
|
|
|
2023-11-11 07:57:17 +08:00
|
|
|
public void LoadStoryboard(bool async = true)
|
2023-11-08 11:37:29 +08:00
|
|
|
{
|
|
|
|
Debug.Assert(drawableStoryboard == null);
|
|
|
|
|
2021-06-04 13:56:10 +08:00
|
|
|
if (!Beatmap.Storyboard.HasDrawable)
|
2021-06-03 13:56:14 +08:00
|
|
|
return;
|
|
|
|
|
2023-11-11 07:57:17 +08:00
|
|
|
drawableStoryboard = new DrawableStoryboard(Beatmap.Storyboard, mods.Value)
|
2021-06-04 14:18:16 +08:00
|
|
|
{
|
2023-11-08 11:37:29 +08:00
|
|
|
Clock = storyboardClock
|
2023-11-11 07:57:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (async)
|
|
|
|
LoadComponentAsync(drawableStoryboard, finishLoad, (loadCancellationSource = new CancellationTokenSource()).Token);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LoadComponent(drawableStoryboard);
|
|
|
|
finishLoad(drawableStoryboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
void finishLoad(DrawableStoryboard s)
|
2023-11-08 11:37:29 +08:00
|
|
|
{
|
2023-11-10 04:23:49 +08:00
|
|
|
if (Beatmap.Storyboard.ReplacesBackground)
|
|
|
|
Sprite.FadeOut(BackgroundScreen.TRANSITION_LENGTH, Easing.InQuint);
|
|
|
|
|
2023-11-08 11:37:29 +08:00
|
|
|
storyboardContainer.FadeInFromZero(BackgroundScreen.TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
storyboardContainer.Add(s);
|
2023-11-11 07:57:17 +08:00
|
|
|
}
|
2023-11-08 11:37:29 +08:00
|
|
|
}
|
|
|
|
|
2023-11-11 07:56:16 +08:00
|
|
|
public void UnloadStoryboard()
|
2023-11-08 11:37:29 +08:00
|
|
|
{
|
2023-11-10 03:56:48 +08:00
|
|
|
if (drawableStoryboard == null)
|
|
|
|
return;
|
2023-11-08 11:37:29 +08:00
|
|
|
|
2023-11-11 07:57:17 +08:00
|
|
|
loadCancellationSource?.Cancel();
|
2023-11-08 11:37:29 +08:00
|
|
|
loadCancellationSource = null;
|
|
|
|
|
2023-11-11 07:56:16 +08:00
|
|
|
// clear is intentionally used here for the storyboard to be disposed asynchronously.
|
|
|
|
storyboardContainer.Clear();
|
2023-11-08 11:37:29 +08:00
|
|
|
|
|
|
|
drawableStoryboard = null;
|
2023-11-11 07:56:16 +08:00
|
|
|
|
|
|
|
Sprite.Alpha = 1f;
|
2021-06-02 15:50:58 +08:00
|
|
|
}
|
2021-12-27 18:28:20 +08:00
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
if (musicController != null)
|
|
|
|
musicController.TrackChanged += onTrackChanged;
|
2022-01-12 05:00:04 +08:00
|
|
|
|
|
|
|
updateStoryboardClockSource(Beatmap);
|
2021-12-27 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
2022-01-12 05:00:04 +08:00
|
|
|
private void onTrackChanged(WorkingBeatmap newBeatmap, TrackChangeDirection _) => updateStoryboardClockSource(newBeatmap);
|
|
|
|
|
|
|
|
private void updateStoryboardClockSource(WorkingBeatmap newBeatmap)
|
2021-12-27 18:28:20 +08:00
|
|
|
{
|
|
|
|
if (newBeatmap != Beatmap)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// `MusicController` will sometimes reload the track, even when the working beatmap technically hasn't changed.
|
|
|
|
// ensure that the storyboard's clock is always using the latest track instance.
|
|
|
|
storyboardClock.ChangeSource(newBeatmap.Track);
|
2021-12-28 04:13:04 +08:00
|
|
|
// more often than not, the previous source track's time will be in the future relative to the new source track.
|
|
|
|
// explicitly process a single frame so that `InterpolatingFramedClock`'s interpolation logic is bypassed
|
|
|
|
// and the storyboard clock is correctly rewound to the source track's time exactly.
|
2021-12-27 18:28:20 +08:00
|
|
|
storyboardClock.ProcessFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
if (musicController != null)
|
|
|
|
musicController.TrackChanged -= onTrackChanged;
|
|
|
|
}
|
2021-06-02 15:50:58 +08:00
|
|
|
}
|
|
|
|
}
|