]> git.bts.cx Git - jurassic-tween.git/blob - Scripts/Runtime/Internal/Context.cs
[FEATURE] Initial Release
[jurassic-tween.git] / Scripts / Runtime / Internal / Context.cs
1 //
2 // Context.cs
3 // JurassicTween
4 //
5 // Created by Benjamin Sherratt on 04/05/2021.
6 // Copyright © 2021 SKFX Ltd. All rights reserved.
7 //
8
9 using System;
10 using UnityEngine;
11
12 namespace JurassicTween.Internal {
13     class Context : IDisposable {
14         private Component component;
15         private IComponentStateCaptor componentStateCaptor;
16         private ComponentState startState;
17         private float time;
18         private TweenParameters tweenParameters;
19
20         public Context(Component component, float time, TweenParameters tweenParameters) {
21             this.component = component;
22             componentStateCaptor = component.GetStateCaptor();
23             startState = componentStateCaptor.GetComponentState(component);
24
25             this.time = time;
26             this.tweenParameters = tweenParameters;
27         }
28
29         public void Dispose() {
30             ComponentState endState = componentStateCaptor.GetComponentState(component);
31
32             ComponentStateDelta delta = componentStateCaptor.GenerateComponentStateDelta(startState, endState);
33
34             Animation animation = component.gameObject.GetComponent<Animation>();
35             if (animation == null) {
36                 animation = component.gameObject.AddComponent<Animation>();
37             }
38
39             string currentClipName = "jurassicTween";
40             AnimationClip animationClip = animation.GetClip(currentClipName);
41             if (animationClip == null) {
42                 animationClip = new AnimationClip();
43                 animationClip.legacy = true;
44                 animationClip.name = currentClipName;
45                 animation.AddClip(animationClip, animationClip.name);
46             }
47
48             foreach (string key in delta.rangeByAnimationKey.Keys) {
49                 ComponentStateDelta.Range range = delta.rangeByAnimationKey[key];
50
51                 AnimationCurve curve;
52                 switch (tweenParameters.interpolation) {
53                 case TweenParameters.Interpolation.Linear:
54                     curve = AnimationCurve.Linear(0, range.start, time, range.end);
55                     break;
56
57                 case TweenParameters.Interpolation.EaseInOut:
58                     curve = AnimationCurve.EaseInOut(0, range.start, time, range.end);
59                     break;
60
61                 case TweenParameters.Interpolation.CustomCurve:
62                     curve = new AnimationCurve();
63                     foreach (Keyframe referenceKeyframe in tweenParameters.customCurve.keys) {
64                         Keyframe keyframe = new Keyframe(
65                             Mathf.Lerp(0.0f, time, referenceKeyframe.time),
66                             Mathf.Lerp(range.start, range.end, referenceKeyframe.value),
67                             referenceKeyframe.inTangent,
68                             referenceKeyframe.outTangent,
69                             referenceKeyframe.inWeight,
70                             referenceKeyframe.outWeight);
71                         curve.AddKey(keyframe);
72                     }
73                     break;
74
75                 default:
76                     throw new Exception("We shouldn't be here");
77                 }
78
79                 animationClip.SetCurve("", component.GetType(), key, curve);
80             }
81
82             animation.Play(animationClip.name);
83         }
84     }
85 }