osu/osu.Game/Storyboards/CommandTimeline.cs

78 lines
2.9 KiB
C#
Raw Normal View History

2018-01-05 11:21:19 +00:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-09-07 21:55:05 +00:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Caching;
using osu.Framework.Graphics;
2017-09-09 09:00:58 +00:00
using System;
2017-09-07 21:55:05 +00:00
using System.Collections.Generic;
using System.Linq;
namespace osu.Game.Storyboards
{
2017-09-08 11:04:53 +00:00
public class CommandTimeline<T> : ICommandTimeline
2017-09-07 21:55:05 +00:00
{
private readonly List<TypedCommand> commands = new List<TypedCommand>();
public IEnumerable<TypedCommand> Commands => commands.OrderBy(c => c.StartTime);
2017-09-07 21:55:05 +00:00
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);
2017-09-07 21:55:05 +00:00
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, });
2017-09-07 21:55:05 +00:00
startTimeBacking.Invalidate();
endTimeBacking.Invalidate();
}
public override string ToString()
=> $"{commands.Count} command(s)";
2017-09-08 11:04:53 +00:00
public class TypedCommand : ICommand
2017-09-07 21:55:05 +00:00
{
public Easing Easing { get; set; }
public double StartTime { get; set; }
public double EndTime { get; set; }
public double Duration => EndTime - StartTime;
2017-09-07 21:55:05 +00:00
public T StartValue;
public T EndValue;
2017-09-09 09:00:58 +00:00
public int CompareTo(ICommand other)
{
var result = StartTime.CompareTo(other.StartTime);
if (result != 0) return result;
return EndTime.CompareTo(other.EndTime);
}
2017-09-07 21:55:05 +00:00
public override string ToString()
=> $"{StartTime} -> {EndTime}, {StartValue} -> {EndValue} {Easing}";
}
}
2017-09-08 11:04:53 +00:00
public interface ICommandTimeline
2017-09-07 21:55:05 +00:00
{
double StartTime { get; }
double EndTime { get; }
bool HasCommands { get; }
}
2017-09-09 09:00:58 +00:00
public interface ICommand : IComparable<ICommand>
{
Easing Easing { get; set; }
double StartTime { get; set; }
double EndTime { get; set; }
double Duration { get; }
}
2017-09-07 21:55:05 +00:00
}