1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 05:27:23 +08:00
osu-lazer/osu.Game/Storyboards/CommandTimeline.cs
2017-09-08 13:04:53 +02:00

70 lines
2.6 KiB
C#

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Caching;
using osu.Framework.Graphics;
using System.Collections.Generic;
using System.Linq;
namespace osu.Game.Storyboards
{
public class CommandTimeline<T> : ICommandTimeline
{
private readonly List<TypedCommand> commands = new List<TypedCommand>();
public IEnumerable<TypedCommand> Commands => commands.OrderBy(c => c.StartTime);
public bool HasCommands => commands.Count > 0;
private Cached<double> startTimeBacking;
public double StartTime => startTimeBacking.IsValid ? startTimeBacking : (startTimeBacking.Value = HasCommands ? commands.Min(c => c.StartTime) : double.MinValue);
private Cached<double> endTimeBacking;
public double EndTime => endTimeBacking.IsValid ? endTimeBacking : (endTimeBacking.Value = HasCommands ? commands.Max(c => c.EndTime) : double.MaxValue);
public T StartValue => HasCommands ? commands.OrderBy(c => c.StartTime).First().StartValue : default(T);
public T EndValue => HasCommands ? commands.OrderByDescending(c => c.EndTime).First().EndValue : default(T);
public void Add(Easing easing, double startTime, double endTime, T startValue, T endValue)
{
if (endTime < startTime)
return;
commands.Add(new TypedCommand { Easing = easing, StartTime = startTime, EndTime = endTime, StartValue = startValue, EndValue = endValue, });
startTimeBacking.Invalidate();
endTimeBacking.Invalidate();
}
public override string ToString()
=> $"{commands.Count} command(s)";
public class TypedCommand : ICommand
{
public Easing Easing { get; set; }
public double StartTime { get; set; }
public double EndTime { get; set; }
public double Duration => EndTime - StartTime;
public T StartValue;
public T EndValue;
public override string ToString()
=> $"{StartTime} -> {EndTime}, {StartValue} -> {EndValue} {Easing}";
}
}
public interface ICommandTimeline
{
double StartTime { get; }
double EndTime { get; }
bool HasCommands { get; }
}
public interface ICommand
{
Easing Easing { get; set; }
double StartTime { get; set; }
double EndTime { get; set; }
double Duration { get; }
}
}