1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-25 15:32:56 +08:00
osu-lazer/osu.Game/Storyboards/Drawables/DrawableStoryboard.cs

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

177 lines
5.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 17:19:50 +08:00
using System;
using System.Collections.Generic;
using System.IO;
2020-05-19 03:12:14 +08:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2017-09-08 05:55:05 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2017-09-08 05:55:05 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using osu.Framework.Platform;
using osu.Game.Database;
using osu.Game.Rulesets.Mods;
2019-03-18 14:25:54 +08:00
using osu.Game.Screens.Play;
using osuTK;
2018-04-13 17:19:50 +08:00
2017-09-08 05:55:05 +08:00
namespace osu.Game.Storyboards.Drawables
{
2017-09-13 17:22:24 +08:00
public partial class DrawableStoryboard : Container<DrawableStoryboardLayer>
2017-09-08 05:55:05 +08:00
{
[Cached(typeof(Storyboard))]
public Storyboard Storyboard { get; }
2018-04-13 17:19:50 +08:00
/// <summary>
/// Whether the storyboard is considered finished.
/// </summary>
public IBindable<bool> HasStoryboardEnded => hasStoryboardEnded;
private readonly BindableBool hasStoryboardEnded = new BindableBool(true);
2019-11-12 17:45:42 +08:00
protected override Container<DrawableStoryboardLayer> Content { get; }
2018-04-13 17:19:50 +08:00
protected override Vector2 DrawScale => new Vector2(Parent!.DrawHeight / 480);
2018-04-13 17:19:50 +08:00
2017-09-08 05:55:05 +08:00
private bool passing = true;
2019-02-28 12:31:40 +08:00
2017-09-08 05:55:05 +08:00
public bool Passing
{
get => passing;
2017-09-08 05:55:05 +08:00
set
{
if (passing == value) return;
2019-02-28 12:31:40 +08:00
2017-09-08 05:55:05 +08:00
passing = value;
updateLayerVisibility();
}
}
2018-04-13 17:19:50 +08:00
2018-03-04 00:48:00 +08:00
public override bool RemoveCompletedTransforms => false;
2018-04-13 17:19:50 +08:00
private double? lastEventEndTime;
[Cached(typeof(IReadOnlyList<Mod>))]
public IReadOnlyList<Mod> Mods { get; }
[Resolved]
private GameHost host { get; set; } = null!;
[Resolved]
private RealmAccess realm { get; set; } = null!;
private DependencyContainer dependencies = null!;
2019-02-28 12:31:40 +08:00
2018-07-11 16:07:14 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) =>
dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
2018-04-13 17:19:50 +08:00
public DrawableStoryboard(Storyboard storyboard, IReadOnlyList<Mod>? mods = null)
2017-09-08 05:55:05 +08:00
{
2017-09-13 17:22:24 +08:00
Storyboard = storyboard;
Mods = mods ?? Array.Empty<Mod>();
2017-09-08 05:55:05 +08:00
Size = new Vector2(640, 480);
bool onlyHasVideoElements = Storyboard.Layers.SelectMany(l => l.Elements).All(e => e is StoryboardVideo);
Width = Height * (storyboard.BeatmapInfo.WidescreenStoryboard || onlyHasVideoElements ? 16 / 9f : 4 / 3f);
2017-09-08 05:55:05 +08:00
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
2018-04-13 17:19:50 +08:00
2019-11-12 17:45:42 +08:00
AddInternal(Content = new Container<DrawableStoryboardLayer>
2017-09-14 20:28:53 +08:00
{
RelativeSizeAxes = Axes.Both,
2017-09-14 20:28:53 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
});
2017-09-08 05:55:05 +08:00
}
2018-04-13 17:19:50 +08:00
2019-03-18 14:50:34 +08:00
[BackgroundDependencyLoader(true)]
private void load(IGameplayClock? clock, CancellationToken? cancellationToken)
2017-09-08 05:55:05 +08:00
{
2019-03-18 17:19:59 +08:00
if (clock != null)
2019-03-18 14:25:54 +08:00
Clock = clock;
dependencies.CacheAs(typeof(TextureStore),
new TextureStore(host.Renderer, host.CreateTextureLoaderStore(
CreateResourceLookupStore()
), false, scaleAdjust: 1));
2018-04-13 17:19:50 +08:00
2017-09-13 17:22:24 +08:00
foreach (var layer in Storyboard.Layers)
{
2019-05-10 16:42:45 +08:00
cancellationToken?.ThrowIfCancellationRequested();
2017-09-13 17:22:24 +08:00
Add(layer.CreateDrawable());
}
2020-05-19 03:12:14 +08:00
lastEventEndTime = Storyboard.LatestEventTime;
2017-09-08 05:55:05 +08:00
}
protected virtual IResourceStore<byte[]> CreateResourceLookupStore() => new StoryboardResourceLookupStore(Storyboard, realm, host);
protected override void Update()
{
base.Update();
hasStoryboardEnded.Value = lastEventEndTime == null || Time.Current >= lastEventEndTime;
}
public DrawableStoryboardLayer OverlayLayer => Children.Single(layer => layer.Name == "Overlay");
private void updateLayerVisibility()
{
foreach (var layer in Children)
layer.Enabled = passing ? layer.Layer.VisibleWhenPassing : layer.Layer.VisibleWhenFailing;
}
private class StoryboardResourceLookupStore : IResourceStore<byte[]>
{
private readonly IResourceStore<byte[]> realmFileStore;
private readonly Storyboard storyboard;
public StoryboardResourceLookupStore(Storyboard storyboard, RealmAccess realm, GameHost host)
{
realmFileStore = new RealmFileStore(realm, host.Storage).Store;
this.storyboard = storyboard;
}
public void Dispose() =>
realmFileStore.Dispose();
public byte[] Get(string name)
{
string? storagePath = storyboard.GetStoragePathFromStoryboardPath(name);
return string.IsNullOrEmpty(storagePath)
? null!
: realmFileStore.Get(storagePath);
}
public Task<byte[]> GetAsync(string name, CancellationToken cancellationToken = new CancellationToken())
{
string? storagePath = storyboard.GetStoragePathFromStoryboardPath(name);
return string.IsNullOrEmpty(storagePath)
? Task.FromResult<byte[]>(null!)
: realmFileStore.GetAsync(storagePath, cancellationToken);
}
public Stream? GetStream(string name)
{
string? storagePath = storyboard.GetStoragePathFromStoryboardPath(name);
return string.IsNullOrEmpty(storagePath)
? null
: realmFileStore.GetStream(storagePath);
}
public IEnumerable<string> GetAvailableResources() =>
realmFileStore.GetAvailableResources();
}
2017-09-08 05:55:05 +08:00
}
}