-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiAnimationT.cs
85 lines (75 loc) · 3.46 KB
/
MultiAnimationT.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Linq;
using System.Collections.Generic;
namespace BrainbeanApps.ValueAnimation
{
/// <summary>
/// Implements single animation using multiple nested animations
/// </summary>
public class MultiAnimation<T> : BaseAnimation<T>, IValueAnimation<T>
where T : struct, IComparable
{
/// <summary>
/// Nested animations.
/// </summary>
public readonly Tuple<float, ValueAnimation<T>>[] NestedAnimations;
public MultiAnimation(params ValueAnimation<T>[] nestedAnimations)
: this(nestedAnimations, ValueAnimation.ValueOperations.For<T>())
{
}
public MultiAnimation(IEnumerable<ValueAnimation<T>> nestedAnimations)
: this(nestedAnimations, ValueAnimation.ValueOperations.For<T>())
{
}
public MultiAnimation(IEnumerable<ValueAnimation<T>> nestedAnimations, IValueOperations<T> valueOperations)
: base(valueOperations)
{
if (nestedAnimations == null)
throw new ArgumentNullException();
if (nestedAnimations.Count() <= 0)
throw new ArgumentException();
var length = 1.0f / nestedAnimations.Count();
NestedAnimations = nestedAnimations.Select(x => new Tuple<float, ValueAnimation<T>>(length, x)).ToArray();
}
public MultiAnimation(IEnumerable<Tuple<float, ValueAnimation<T>>> nestedAnimations)
: this(nestedAnimations, ValueAnimation.ValueOperations.For<T>())
{
}
public MultiAnimation(IEnumerable<Tuple<float, ValueAnimation<T>>> nestedAnimations,
IValueOperations<T> valueOperations)
: base(valueOperations)
{
if (nestedAnimations == null)
throw new ArgumentNullException();
if (nestedAnimations.Count() <= 0)
throw new ArgumentException();
if (nestedAnimations.Any(x => x.Item1 < 0.0f || x.Item1 > 1.0f))
throw new ArgumentException();
if (nestedAnimations.Sum(x => x.Item1) > 1.0f)
throw new ArgumentException();
NestedAnimations = nestedAnimations.ToArray();
}
public T GetValue(float currentTime, float duration, T initialValue, T deltaValue)
{
var progress = currentTime / duration;
int nestedAnimationIndex = 0;
var playedNestedAnimations = 0.0f;
for (; nestedAnimationIndex < NestedAnimations.Length; nestedAnimationIndex++)
{
var length = NestedAnimations[nestedAnimationIndex].Item1;
if (progress <= playedNestedAnimations + length)
break;
playedNestedAnimations += length;
}
if (nestedAnimationIndex == NestedAnimations.Length)
nestedAnimationIndex--;
var nestedAnimation = NestedAnimations[nestedAnimationIndex];
var nestedCurrentTime = currentTime - playedNestedAnimations * duration;
var nestedDuration = nestedAnimation.Item1 * duration;
var nestedInitialValue = ValueOperations.Add(initialValue,
ValueOperations.ScaleByFactor(deltaValue, playedNestedAnimations));
var nestedDeltaValue = ValueOperations.ScaleByFactor(deltaValue, nestedAnimation.Item1);
return nestedAnimation.Item2(nestedCurrentTime, nestedDuration, nestedInitialValue, nestedDeltaValue);
}
}
}