2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Game.Storyboards.Drawables;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Storyboards
|
|
|
|
|
{
|
|
|
|
|
public class StoryboardSprite : IStoryboardElement
|
|
|
|
|
{
|
|
|
|
|
private readonly List<CommandLoop> loops = new List<CommandLoop>();
|
|
|
|
|
private readonly List<CommandTrigger> triggers = new List<CommandTrigger>();
|
|
|
|
|
|
|
|
|
|
public string Path { get; set; }
|
|
|
|
|
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<in T>(Drawable drawable, T value);
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
private delegate void DrawableTransformer<in T>(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<Tuple<CommandTimelineGroup, double>> triggeredGroups = null)
|
|
|
|
|
{
|
|
|
|
|
applyCommands(drawable, getCommands(g => g.X, triggeredGroups), (d, value) => d.X = value, (d, value, duration, easing) => d.MoveToX(value, duration, easing));
|
|
|
|
|
applyCommands(drawable, getCommands(g => g.Y, triggeredGroups), (d, value) => d.Y = value, (d, value, duration, easing) => d.MoveToY(value, duration, easing));
|
|
|
|
|
applyCommands(drawable, getCommands(g => g.Scale, triggeredGroups), (d, value) => d.Scale = value, (d, value, duration, easing) => d.ScaleTo(value, duration, easing));
|
|
|
|
|
applyCommands(drawable, getCommands(g => g.Rotation, triggeredGroups), (d, value) => d.Rotation = value, (d, value, duration, easing) => d.RotateTo(value, duration, easing));
|
|
|
|
|
applyCommands(drawable, getCommands(g => g.Colour, triggeredGroups), (d, value) => d.Colour = value, (d, value, duration, easing) => d.FadeColour(value, duration, easing));
|
|
|
|
|
applyCommands(drawable, getCommands(g => g.Alpha, triggeredGroups), (d, value) => d.Alpha = value, (d, value, duration, easing) => d.FadeTo(value, duration, easing));
|
2019-08-21 12:29:50 +08:00
|
|
|
|
applyCommands(drawable, getCommands(g => g.BlendingParameters, triggeredGroups), (d, value) => d.Blending = value, (d, value, duration, easing) => d.TransformBlendingMode(value, duration), false);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-02-28 13:35:00 +08:00
|
|
|
|
if (drawable is IFlippable flippable)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
applyCommands(drawable, getCommands(g => g.FlipH, triggeredGroups), (d, value) => flippable.FlipH = value, (d, value, duration, easing) => flippable.TransformFlipH(value, duration), false);
|
|
|
|
|
applyCommands(drawable, getCommands(g => g.FlipV, triggeredGroups), (d, value) => flippable.FlipV = value, (d, value, duration, easing) => flippable.TransformFlipV(value, duration), false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void applyCommands<T>(Drawable drawable, IEnumerable<CommandTimeline<T>.TypedCommand> commands, DrawablePropertyInitializer<T> initializeProperty, DrawableTransformer<T> transform, bool alwaysInitialize = true)
|
|
|
|
|
where T : struct
|
|
|
|
|
{
|
|
|
|
|
var initialized = false;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
foreach (var command in commands.OrderBy(l => l))
|
|
|
|
|
{
|
|
|
|
|
if (!initialized)
|
|
|
|
|
{
|
|
|
|
|
if (alwaysInitialize || command.StartTime == command.EndTime)
|
|
|
|
|
initializeProperty.Invoke(drawable, command.StartValue);
|
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using (drawable.BeginAbsoluteSequence(command.StartTime))
|
|
|
|
|
{
|
|
|
|
|
transform(drawable, command.StartValue, 0, Easing.None);
|
|
|
|
|
transform(drawable, command.EndValue, command.Duration, command.Easing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<CommandTimeline<T>.TypedCommand> getCommands<T>(CommandTimelineSelector<T> timelineSelector, IEnumerable<Tuple<CommandTimelineGroup, double>> triggeredGroups)
|
|
|
|
|
{
|
|
|
|
|
var commands = TimelineGroup.GetCommands(timelineSelector);
|
|
|
|
|
foreach (var loop in loops)
|
|
|
|
|
commands = commands.Concat(loop.GetCommands(timelineSelector));
|
2019-11-11 20:05:36 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
if (triggeredGroups != null)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2018-04-13 17:19:50 +08:00
|
|
|
|
foreach (var pair in triggeredGroups)
|
|
|
|
|
commands = commands.Concat(pair.Item1.GetCommands(timelineSelector, pair.Item2));
|
2019-11-11 19:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
return commands;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
=> $"{Path}, {Origin}, {InitialPosition}";
|
|
|
|
|
}
|
|
|
|
|
}
|