// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osuTK; using osu.Framework.Graphics; using osu.Game.Storyboards.Drawables; using System; using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; namespace osu.Game.Storyboards { public class StoryboardSprite : IStoryboardElement { private readonly List loops = new List(); private readonly List triggers = new List(); public string Path { get; } public bool IsDrawable => HasCommands; public Anchor Origin; public Vector2 InitialPosition; public readonly CommandTimelineGroup TimelineGroup = new CommandTimelineGroup(); public double StartTime => Math.Min( TimelineGroup.HasCommands ? TimelineGroup.CommandsStartTime : double.MaxValue, loops.Any(l => l.HasCommands) ? loops.Where(l => l.HasCommands).Min(l => l.StartTime) : double.MaxValue); public double EndTime => Math.Max( TimelineGroup.HasCommands ? TimelineGroup.CommandsEndTime : double.MinValue, loops.Any(l => l.HasCommands) ? loops.Where(l => l.HasCommands).Max(l => l.EndTime) : double.MinValue); public bool HasCommands => TimelineGroup.HasCommands || loops.Any(l => l.HasCommands); private delegate void DrawablePropertyInitializer(Drawable drawable, T value); private delegate void DrawableTransformer(Drawable drawable, T value, double duration, Easing easing); public StoryboardSprite(string path, Anchor origin, Vector2 initialPosition) { Path = path; Origin = origin; InitialPosition = initialPosition; } public CommandLoop AddLoop(double startTime, int loopCount) { var loop = new CommandLoop(startTime, loopCount); loops.Add(loop); return loop; } public CommandTrigger AddTrigger(string triggerName, double startTime, double endTime, int groupNumber) { var trigger = new CommandTrigger(triggerName, startTime, endTime, groupNumber); triggers.Add(trigger); return trigger; } public virtual Drawable CreateDrawable() => new DrawableStoryboardSprite(this); public void ApplyTransforms(Drawable drawable, IEnumerable> triggeredGroups = null) { // For performance reasons, we need to apply the commands in order by start time. Not doing so will cause many functions to be interleaved, resulting in O(n^2) complexity. // To achieve this, commands are "generated" as pairs of (command, initFunc, transformFunc) and batched into a contiguous list // The list is then stably-sorted (to preserve command order), and applied to the drawable sequentially. List generated = new List(); generateCommands(generated, getCommands(g => g.X, triggeredGroups), (d, value) => d.X = value, (d, value, duration, easing) => d.MoveToX(value, duration, easing)); generateCommands(generated, getCommands(g => g.Y, triggeredGroups), (d, value) => d.Y = value, (d, value, duration, easing) => d.MoveToY(value, duration, easing)); generateCommands(generated, getCommands(g => g.Scale, triggeredGroups), (d, value) => d.Scale = new Vector2(value), (d, value, duration, easing) => d.ScaleTo(value, duration, easing)); generateCommands(generated, getCommands(g => g.Rotation, triggeredGroups), (d, value) => d.Rotation = value, (d, value, duration, easing) => d.RotateTo(value, duration, easing)); generateCommands(generated, getCommands(g => g.Colour, triggeredGroups), (d, value) => d.Colour = value, (d, value, duration, easing) => d.FadeColour(value, duration, easing)); generateCommands(generated, getCommands(g => g.Alpha, triggeredGroups), (d, value) => d.Alpha = value, (d, value, duration, easing) => d.FadeTo(value, duration, easing)); generateCommands(generated, getCommands(g => g.BlendingParameters, triggeredGroups), (d, value) => d.Blending = value, (d, value, duration, easing) => d.TransformBlendingMode(value, duration), false); if (drawable is IVectorScalable vectorScalable) { generateCommands(generated, getCommands(g => g.VectorScale, triggeredGroups), (d, value) => vectorScalable.VectorScale = value, (d, value, duration, easing) => vectorScalable.VectorScaleTo(value, duration, easing)); } if (drawable is IFlippable flippable) { generateCommands(generated, getCommands(g => g.FlipH, triggeredGroups), (d, value) => flippable.FlipH = value, (d, value, duration, easing) => flippable.TransformFlipH(value, duration), false); generateCommands(generated, getCommands(g => g.FlipV, triggeredGroups), (d, value) => flippable.FlipV = value, (d, value, duration, easing) => flippable.TransformFlipV(value, duration), false); } foreach (var command in generated.OrderBy(g => g.StartTime)) command.ApplyTo(drawable); } private void generateCommands(List resultList, IEnumerable.TypedCommand> commands, DrawablePropertyInitializer initializeProperty, DrawableTransformer transform, bool alwaysInitialize = true) { bool initialized = false; foreach (var command in commands) { DrawablePropertyInitializer initFunc = null; if (!initialized) { if (alwaysInitialize || command.StartTime == command.EndTime) initFunc = initializeProperty; initialized = true; } resultList.Add(new GeneratedCommand(command, initFunc, transform)); } } private IEnumerable.TypedCommand> getCommands(CommandTimelineSelector timelineSelector, IEnumerable> triggeredGroups) { var commands = TimelineGroup.GetCommands(timelineSelector); foreach (var loop in loops) commands = commands.Concat(loop.GetCommands(timelineSelector)); if (triggeredGroups != null) { foreach (var pair in triggeredGroups) commands = commands.Concat(pair.Item1.GetCommands(timelineSelector, pair.Item2)); } return commands; } public override string ToString() => $"{Path}, {Origin}, {InitialPosition}"; private interface IGeneratedCommand { double StartTime { get; } void ApplyTo(Drawable drawable); } private readonly struct GeneratedCommand : IGeneratedCommand { public double StartTime => command.StartTime; private readonly DrawablePropertyInitializer initializeProperty; private readonly DrawableTransformer transform; private readonly CommandTimeline.TypedCommand command; public GeneratedCommand([NotNull] CommandTimeline.TypedCommand command, [CanBeNull] DrawablePropertyInitializer initializeProperty, [NotNull] DrawableTransformer transform) { this.command = command; this.initializeProperty = initializeProperty; this.transform = transform; } public void ApplyTo(Drawable drawable) { initializeProperty?.Invoke(drawable, command.StartValue); using (drawable.BeginAbsoluteSequence(command.StartTime)) { transform(drawable, command.StartValue, 0, Easing.None); transform(drawable, command.EndValue, command.Duration, command.Easing); } } } } }