osu/osu.Game/Storyboards/Storyboard.cs

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

123 lines
4.9 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 09:19:50 +00:00
2017-09-13 09:22:24 +00:00
using System.Collections.Generic;
using System.IO;
2017-09-13 09:22:24 +00:00
using System.Linq;
using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Mods;
using osu.Game.Storyboards.Drawables;
2018-04-13 09:19:50 +00:00
2017-09-13 09:22:24 +00:00
namespace osu.Game.Storyboards
{
2019-05-10 07:31:22 +00:00
public class Storyboard
2017-09-13 09:22:24 +00:00
{
private readonly Dictionary<string, StoryboardLayer> layers = new Dictionary<string, StoryboardLayer>();
public IEnumerable<StoryboardLayer> Layers => layers.Values;
2018-04-13 09:19:50 +00:00
public BeatmapInfo BeatmapInfo = new BeatmapInfo();
2018-04-13 09:19:50 +00:00
/// <summary>
/// Whether the storyboard can fall back to skin sprites in case no matching storyboard sprites are found.
/// </summary>
public bool UseSkinSprites { get; set; }
public bool HasDrawable => Layers.Any(l => l.Elements.Any(e => e.IsDrawable));
2018-04-13 09:19:50 +00: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.
/// </remarks>
public double? EarliestEventTime => Layers.SelectMany(l => l.Elements).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-17 16:34:38 +00:00
/// <remarks>
/// This iterates all elements and as such should be used sparingly or stored locally.
/// Videos and samples return StartTime as their EndTIme.
/// </remarks>
public double? LatestEventTime => Layers.SelectMany(l => l.Elements).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 09:22:24 +00:00
public Storyboard()
{
2021-05-25 09:50:33 +00:00
layers.Add("Video", new StoryboardVideoLayer("Video", 4, false));
2017-09-13 09:22:24 +00:00
layers.Add("Background", new StoryboardLayer("Background", 3));
2020-03-25 02:08:08 +00: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 09:22:24 +00:00
}
2018-04-13 09:19:50 +00:00
2017-09-13 09:22:24 +00:00
public StoryboardLayer GetLayer(string name)
{
2019-11-12 10:22:35 +00:00
if (!layers.TryGetValue(name, out var layer))
layers[name] = layer = new StoryboardLayer(name, --minimumLayerDepth);
2018-04-13 09:19:50 +00:00
2017-09-13 09:22:24 +00:00
return layer;
}
2018-04-13 09:19:50 +00: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 09:19:50 +00: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 09:19:50 +00:00
public DrawableStoryboard CreateDrawable(IReadOnlyList<Mod>? mods = null) =>
new DrawableStoryboard(this, mods);
private static readonly string[] image_extensions = { @".png", @".jpg" };
public Texture? GetTextureFromPath(string path, TextureStore textureStore)
{
string? resolvedPath = null;
if (Path.HasExtension(path))
{
resolvedPath = BeatmapInfo.BeatmapSet?.GetPathForFile(path);
}
else
{
// Just doing this extension logic locally here for simplicity.
//
// A more "sane" path may be to use the ISkinSource.GetTexture path (which will use the extensions of the underlying TextureStore),
// but comes with potential complexity (what happens if the user has beatmap skins disabled?).
foreach (string ext in image_extensions)
{
if ((resolvedPath = BeatmapInfo.BeatmapSet?.GetPathForFile($"{path}{ext}")) != null)
break;
}
}
if (!string.IsNullOrEmpty(resolvedPath))
return textureStore.Get(resolvedPath);
return null;
}
2017-09-13 09:22:24 +00:00
}
}