1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 05:22:54 +08:00
osu-lazer/osu.Game/Storyboards/Storyboard.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

124 lines
5.1 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2017-09-13 17:22:24 +08:00
using System.Collections.Generic;
using System.IO;
2017-09-13 17:22:24 +08:00
using System.Linq;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Mods;
using osu.Game.Storyboards.Drawables;
2018-04-13 17:19:50 +08:00
2017-09-13 17:22:24 +08:00
namespace osu.Game.Storyboards
{
2019-05-10 15:31:22 +08:00
public class Storyboard
2017-09-13 17:22:24 +08:00
{
private readonly Dictionary<string, StoryboardLayer> layers = new Dictionary<string, StoryboardLayer>();
public IEnumerable<StoryboardLayer> Layers => layers.Values;
2018-04-13 17:19:50 +08:00
public BeatmapInfo BeatmapInfo = new BeatmapInfo();
public IBeatmap Beatmap { get; set; } = new Beatmap();
2018-04-13 17:19:50 +08:00
/// <summary>
/// Whether the storyboard should prefer textures from the current skin before using local storyboard textures.
/// </summary>
public bool UseSkinSprites { get; set; }
public bool HasDrawable => Layers.Any(l => l.Elements.Any(e => e.IsDrawable));
2018-04-13 17:19:50 +08:00
/// <summary>
/// Across all layers, find the earliest point in time that a storyboard element exists at.
/// Will return null if there are no elements.
/// </summary>
/// <remarks>
/// This iterates all elements and as such should be used sparingly or stored locally.
2023-10-28 08:18:13 +08:00
/// Sample events use their start time as "end time" during this calculation.
Exclude video events from being accounted for when calculating storyboard time bounds Closes https://github.com/ppy/osu/issues/25263. In some circumstances, stable allows skipping twice if a particularly long storyboarded intro is being displayed: https://github.com/peppy/osu-stable-reference/blob/master/osu!/GameModes/Play/Player.cs#L1728-L1736 `AllowDoubleSkip` is calculated thus: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameModes/Play/Player.cs#L1761-L1770 and `leadInTime` is calculated thus: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameModes/Play/Player.cs#L1342-L1351 The key to watch out for here is `{first,last}EventTime`. `EventManager` will calculate it on-the-fly as it adds storyboard elements: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameplayElements/Events/EventManager.cs#L253-L256 However, this pathway is only used for sprite, animation, sample, and break events. Video and background events use the following pathway: https://github.com/peppy/osu-stable-reference/blob/master/osu!/GameplayElements/Events/EventManager.cs#L368 Note that this particular overload does not mutate either bound. Which means that for the purposes of determining where a storyboard starts and ends temporally, a video event's start time is essentially ignored. To reflect that, add a clause that excludes video events from calculations of `{Earliest,Latest}EventTime`.
2023-10-28 04:10:03 +08:00
/// Video and background events are not included to match stable.
/// </remarks>
Exclude video events from being accounted for when calculating storyboard time bounds Closes https://github.com/ppy/osu/issues/25263. In some circumstances, stable allows skipping twice if a particularly long storyboarded intro is being displayed: https://github.com/peppy/osu-stable-reference/blob/master/osu!/GameModes/Play/Player.cs#L1728-L1736 `AllowDoubleSkip` is calculated thus: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameModes/Play/Player.cs#L1761-L1770 and `leadInTime` is calculated thus: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameModes/Play/Player.cs#L1342-L1351 The key to watch out for here is `{first,last}EventTime`. `EventManager` will calculate it on-the-fly as it adds storyboard elements: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameplayElements/Events/EventManager.cs#L253-L256 However, this pathway is only used for sprite, animation, sample, and break events. Video and background events use the following pathway: https://github.com/peppy/osu-stable-reference/blob/master/osu!/GameplayElements/Events/EventManager.cs#L368 Note that this particular overload does not mutate either bound. Which means that for the purposes of determining where a storyboard starts and ends temporally, a video event's start time is essentially ignored. To reflect that, add a clause that excludes video events from calculations of `{Earliest,Latest}EventTime`.
2023-10-28 04:10:03 +08:00
public double? EarliestEventTime => Layers.SelectMany(l => l.Elements)
.Where(e => e is not StoryboardVideo)
.MinBy(e => e.StartTime)?.StartTime;
/// <summary>
/// Across all layers, find the latest point in time that a storyboard element ends at.
/// Will return null if there are no elements.
/// </summary>
2021-04-18 00:34:38 +08:00
/// <remarks>
/// This iterates all elements and as such should be used sparingly or stored locally.
2023-10-28 08:18:13 +08:00
/// Sample events use their start time as "end time" during this calculation.
Exclude video events from being accounted for when calculating storyboard time bounds Closes https://github.com/ppy/osu/issues/25263. In some circumstances, stable allows skipping twice if a particularly long storyboarded intro is being displayed: https://github.com/peppy/osu-stable-reference/blob/master/osu!/GameModes/Play/Player.cs#L1728-L1736 `AllowDoubleSkip` is calculated thus: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameModes/Play/Player.cs#L1761-L1770 and `leadInTime` is calculated thus: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameModes/Play/Player.cs#L1342-L1351 The key to watch out for here is `{first,last}EventTime`. `EventManager` will calculate it on-the-fly as it adds storyboard elements: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameplayElements/Events/EventManager.cs#L253-L256 However, this pathway is only used for sprite, animation, sample, and break events. Video and background events use the following pathway: https://github.com/peppy/osu-stable-reference/blob/master/osu!/GameplayElements/Events/EventManager.cs#L368 Note that this particular overload does not mutate either bound. Which means that for the purposes of determining where a storyboard starts and ends temporally, a video event's start time is essentially ignored. To reflect that, add a clause that excludes video events from calculations of `{Earliest,Latest}EventTime`.
2023-10-28 04:10:03 +08:00
/// Video and background events are not included to match stable.
2021-04-18 00:34:38 +08:00
/// </remarks>
Exclude video events from being accounted for when calculating storyboard time bounds Closes https://github.com/ppy/osu/issues/25263. In some circumstances, stable allows skipping twice if a particularly long storyboarded intro is being displayed: https://github.com/peppy/osu-stable-reference/blob/master/osu!/GameModes/Play/Player.cs#L1728-L1736 `AllowDoubleSkip` is calculated thus: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameModes/Play/Player.cs#L1761-L1770 and `leadInTime` is calculated thus: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameModes/Play/Player.cs#L1342-L1351 The key to watch out for here is `{first,last}EventTime`. `EventManager` will calculate it on-the-fly as it adds storyboard elements: https://github.com/peppy/osu-stable-reference/blob/3ea48705eb67172c430371dcfc8a16a002ed0d3d/osu!/GameplayElements/Events/EventManager.cs#L253-L256 However, this pathway is only used for sprite, animation, sample, and break events. Video and background events use the following pathway: https://github.com/peppy/osu-stable-reference/blob/master/osu!/GameplayElements/Events/EventManager.cs#L368 Note that this particular overload does not mutate either bound. Which means that for the purposes of determining where a storyboard starts and ends temporally, a video event's start time is essentially ignored. To reflect that, add a clause that excludes video events from calculations of `{Earliest,Latest}EventTime`.
2023-10-28 04:10:03 +08:00
public double? LatestEventTime => Layers.SelectMany(l => l.Elements)
.Where(e => e is not StoryboardVideo)
.MaxBy(e => e.GetEndTime())?.GetEndTime();
/// <summary>
/// Depth of the currently front-most storyboard layer, excluding the overlay layer.
/// </summary>
private int minimumLayerDepth;
2017-09-13 17:22:24 +08:00
public Storyboard()
{
2021-05-25 17:50:33 +08:00
layers.Add("Video", new StoryboardVideoLayer("Video", 4, false));
2017-09-13 17:22:24 +08:00
layers.Add("Background", new StoryboardLayer("Background", 3));
2020-03-25 10:08:08 +08:00
layers.Add("Fail", new StoryboardLayer("Fail", 2) { VisibleWhenPassing = false, });
layers.Add("Pass", new StoryboardLayer("Pass", 1) { VisibleWhenFailing = false, });
layers.Add("Foreground", new StoryboardLayer("Foreground", minimumLayerDepth = 0));
layers.Add("Overlay", new StoryboardLayer("Overlay", int.MinValue));
2017-09-13 17:22:24 +08:00
}
2018-04-13 17:19:50 +08:00
2017-09-13 17:22:24 +08:00
public StoryboardLayer GetLayer(string name)
{
2019-11-12 18:22:35 +08:00
if (!layers.TryGetValue(name, out var layer))
layers[name] = layer = new StoryboardLayer(name, --minimumLayerDepth);
2018-04-13 17:19:50 +08:00
2017-09-13 17:22:24 +08:00
return layer;
}
2018-04-13 17:19:50 +08:00
/// <summary>
/// Whether the beatmap's background should be hidden while this storyboard is being displayed.
/// </summary>
public bool ReplacesBackground
{
get
{
string backgroundPath = BeatmapInfo.Metadata.BackgroundFile;
if (string.IsNullOrEmpty(backgroundPath))
return false;
2018-04-13 17:19:50 +08:00
// Importantly, do this after the NullOrEmpty because EF may have stored the non-nullable value as null to the database, bypassing compile-time constraints.
backgroundPath = backgroundPath.ToLowerInvariant();
return GetLayer("Background").Elements.Any(e => e.Path.ToLowerInvariant() == backgroundPath);
}
}
2018-04-13 17:19:50 +08:00
public virtual DrawableStoryboard CreateDrawable(IReadOnlyList<Mod>? mods = null) =>
new DrawableStoryboard(this, mods);
private static readonly string[] image_extensions = { @".png", @".jpg" };
public virtual string? GetStoragePathFromStoryboardPath(string path)
{
string? resolvedPath = null;
if (Path.HasExtension(path))
{
resolvedPath = BeatmapInfo.BeatmapSet?.GetPathForFile(path);
}
else
{
// Some old storyboards don't include a file extension, so let's best guess at one.
foreach (string ext in image_extensions)
{
if ((resolvedPath = BeatmapInfo.BeatmapSet?.GetPathForFile($"{path}{ext}")) != null)
break;
}
}
return resolvedPath;
}
2017-09-13 17:22:24 +08:00
}
}