2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-09-08 05:55:05 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Game.Storyboards.Drawables;
|
2017-09-09 00:00:17 +08:00
|
|
|
|
using System;
|
2017-09-08 05:55:05 +08:00
|
|
|
|
using System.Collections.Generic;
|
2017-09-08 18:11:57 +08:00
|
|
|
|
using System.Linq;
|
2017-09-08 05:55:05 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Storyboards
|
|
|
|
|
{
|
2017-09-13 17:22:24 +08:00
|
|
|
|
public class StoryboardSprite : IStoryboardElement
|
2017-09-08 05:55:05 +08:00
|
|
|
|
{
|
2017-09-11 03:25:23 +08:00
|
|
|
|
private readonly List<CommandLoop> loops = new List<CommandLoop>();
|
|
|
|
|
private readonly List<CommandTrigger> triggers = new List<CommandTrigger>();
|
|
|
|
|
|
2017-09-08 19:04:53 +08:00
|
|
|
|
public string Path { get; set; }
|
2017-09-15 17:23:37 +08:00
|
|
|
|
public bool IsDrawable => HasCommands;
|
|
|
|
|
|
2017-09-08 05:55:05 +08:00
|
|
|
|
public Anchor Origin;
|
|
|
|
|
public Vector2 InitialPosition;
|
|
|
|
|
|
2017-09-11 03:25:23 +08:00
|
|
|
|
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);
|
2017-09-08 18:11:57 +08:00
|
|
|
|
|
2017-09-09 03:23:24 +08:00
|
|
|
|
private delegate void DrawablePropertyInitializer<in T>(Drawable drawable, T value);
|
|
|
|
|
private delegate void DrawableTransformer<in T>(Drawable drawable, T value, double duration, Easing easing);
|
2017-09-09 00:00:17 +08:00
|
|
|
|
|
2017-09-13 17:22:24 +08:00
|
|
|
|
public StoryboardSprite(string path, Anchor origin, Vector2 initialPosition)
|
2017-09-08 05:55:05 +08:00
|
|
|
|
{
|
|
|
|
|
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()
|
2017-09-13 17:22:24 +08:00
|
|
|
|
=> new DrawableStoryboardSprite(this);
|
2017-09-08 05:55:05 +08:00
|
|
|
|
|
2017-09-09 00:00:17 +08:00
|
|
|
|
public void ApplyTransforms(Drawable drawable, IEnumerable<Tuple<CommandTimelineGroup, double>> triggeredGroups = null)
|
|
|
|
|
{
|
2017-09-11 03:25:23 +08:00
|
|
|
|
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));
|
2017-09-11 18:23:32 +08:00
|
|
|
|
applyCommands(drawable, getCommands(g => g.BlendingMode, triggeredGroups), (d, value) => d.Blending = value, (d, value, duration, easing) => d.TransformBlendingMode(value, duration), false);
|
2017-09-09 00:00:17 +08:00
|
|
|
|
|
|
|
|
|
var flippable = drawable as IFlippable;
|
|
|
|
|
if (flippable != null)
|
|
|
|
|
{
|
2017-09-11 03:25:23 +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);
|
2017-09-09 00:00:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-11 03:25:23 +08:00
|
|
|
|
private void applyCommands<T>(Drawable drawable, IEnumerable<CommandTimeline<T>.TypedCommand> commands, DrawablePropertyInitializer<T> initializeProperty, DrawableTransformer<T> transform, bool alwaysInitialize = true)
|
2017-09-09 21:34:26 +08:00
|
|
|
|
where T : struct
|
2017-09-09 00:00:17 +08:00
|
|
|
|
{
|
|
|
|
|
var initialized = false;
|
2017-09-11 03:25:23 +08:00
|
|
|
|
foreach (var command in commands.OrderBy(l => l))
|
2017-09-09 00:00:17 +08:00
|
|
|
|
{
|
|
|
|
|
if (!initialized)
|
|
|
|
|
{
|
2017-09-09 21:34:26 +08:00
|
|
|
|
if (alwaysInitialize || command.StartTime == command.EndTime)
|
|
|
|
|
initializeProperty.Invoke(drawable, command.StartValue);
|
2017-09-09 00:00:17 +08:00
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
|
|
|
|
using (drawable.BeginAbsoluteSequence(command.StartTime))
|
|
|
|
|
{
|
|
|
|
|
transform(drawable, command.StartValue, 0, Easing.None);
|
|
|
|
|
transform(drawable, command.EndValue, command.Duration, command.Easing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-11 03:25:23 +08:00
|
|
|
|
private IEnumerable<CommandTimeline<T>.TypedCommand> getCommands<T>(CommandTimelineSelector<T> timelineSelector, IEnumerable<Tuple<CommandTimelineGroup, double>> triggeredGroups)
|
2017-09-08 05:55:05 +08:00
|
|
|
|
{
|
2017-09-11 03:25:23 +08:00
|
|
|
|
var commands = TimelineGroup.GetCommands(timelineSelector);
|
|
|
|
|
foreach (var loop in loops)
|
|
|
|
|
commands = commands.Concat(loop.GetCommands(timelineSelector));
|
2017-09-09 00:00:17 +08:00
|
|
|
|
if (triggeredGroups != null)
|
|
|
|
|
foreach (var pair in triggeredGroups)
|
|
|
|
|
commands = commands.Concat(pair.Item1.GetCommands(timelineSelector, pair.Item2));
|
2017-09-09 17:00:58 +08:00
|
|
|
|
return commands;
|
2017-09-08 05:55:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
=> $"{Path}, {Origin}, {InitialPosition}";
|
|
|
|
|
}
|
|
|
|
|
}
|