| 1 | using System; |
| 2 | using System.Threading; |
| 3 | using System.Collections; |
| 4 | using System.Collections.Generic; |
| 5 | using System.Diagnostics; |
| 6 | |
| 7 | /** |
| 8 | * Tweening |
| 9 | * Created by: Tim Falken |
| 10 | * |
| 11 | * Usage: |
| 12 | * 1: Create a tween using: Tween newTween = new Tween(); |
| 13 | * You can supply an ease function as parameter. |
| 14 | * Either use a built-in from the Easing class, |
| 15 | * or make one yourself using for example: |
| 16 | * ...new Tween((t)=> { return t * t }); |
| 17 | * |
| 18 | * 2: define what you want to do with the tween results: |
| 19 | * newTween.onChange = (t)=> |
| 20 | * { |
| 21 | * Debug.Log(t); |
| 22 | * } |
| 23 | * |
| 24 | * 3: Start the tween: |
| 25 | * newTween.Start(from, to, duration); |
| 26 | * |
| 27 | * You can optionaly also define an action to |
| 28 | * be performed upon completion; |
| 29 | * |
| 30 | * newTween.onComplete = ()=> |
| 31 | * { |
| 32 | * Debug.Log("Done!"); |
| 33 | * }; |
| 34 | **/ |
| 35 | |
| 36 | public class Tween |
| 37 | { |
| 38 | public Easing.Ease EaseFunction; |
| 39 | public TweenOnCompleteEvent onComplete; |
| 40 | public TweenOnChangeEvent onChange; |
| 41 | |
| 42 | /** |
| 43 | * Use to see if the tween is currently active. When the tween completes, this will return to being false. |
| 44 | **/ |
| 45 | public bool active { get; private set; } |
| 46 | |
| 47 | private static List<ActiveTween> _tweens = new List<ActiveTween>(); |
| 48 | |
| 49 | /** |
| 50 | * Create a new tween with a linear ease function. |
| 51 | **/ |
| 52 | public Tween() |
| 53 | { |
| 54 | EaseFunction = Easing.Linear; |
| 55 | active = false; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Create a new tween with a given ease function. |
| 60 | **/ |
| 61 | public Tween(Easing.Ease ease) |
| 62 | { |
| 63 | EaseFunction = ease; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | *Start the tween. `from` and `to` will be used as the initial and end value of the tween, respectively. |
| 68 | * |
| 69 | *The last onChance call is guaranteed to have `t` equal to the given `to` value. |
| 70 | * |
| 71 | *`time` is measured in seconds. |
| 72 | **/ |
| 73 | public void Start(float from, float to, float time) |
| 74 | { |
| 75 | ActiveTween newTween = new ActiveTween (); |
| 76 | |
| 77 | newTween.tween = this; |
| 78 | newTween.progress = 0; |
| 79 | newTween.from = from; |
| 80 | newTween.to = to; |
| 81 | newTween.time = time; |
| 82 | |
| 83 | _tweens.Add (newTween); |
| 84 | |
| 85 | active = true; |
| 86 | } |
| 87 | |
| 88 | private float Lerp(float t, float from, float to) |
| 89 | { |
| 90 | return from + (to - from) * t; |
| 91 | } |
| 92 | |
| 93 | private void UpdateTween(ActiveTween settings) |
| 94 | { |
| 95 | float result = settings.time > 0 ? Lerp (EaseFunction (settings.progress / settings.time), settings.from, settings.to) : settings.to; |
| 96 | |
| 97 | if (settings.progress >= settings.time) |
| 98 | { |
| 99 | result = settings.to; |
| 100 | } |
| 101 | |
| 102 | if (!float.IsNaN (result)) |
| 103 | { |
| 104 | if (onChange != null) |
| 105 | { |
| 106 | onChange (result); |
| 107 | } |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | throw new DivideByZeroException ("NaN received from easing function. (Divide by zero?)"); |
| 112 | } |
| 113 | |
| 114 | if (settings.progress >= settings.time) |
| 115 | { |
| 116 | active = false; |
| 117 | |
| 118 | if (onComplete != null) |
| 119 | { |
| 120 | onComplete (); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Immediately abort the tween (will NOT execute OnComplete) |
| 127 | **/ |
| 128 | public void Abort() |
| 129 | { |
| 130 | _tweens.RemoveAll (o => o.tween == this); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Immediately complete the tween (WILL execute OnComplete) |
| 135 | **/ |
| 136 | public void InstantComplete() |
| 137 | { |
| 138 | ActiveTween t = _tweens.Find (o => o.tween == this); |
| 139 | |
| 140 | t.CompleteTween(); |
| 141 | |
| 142 | _tweens.Remove(t); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Update all active tweens with the given `deltaTime` value. |
| 147 | **/ |
| 148 | public static void UpdateTweens(float deltaTime) |
| 149 | { |
| 150 | if (_tweens.Count > 0) |
| 151 | { |
| 152 | foreach (ActiveTween tween in _tweens.FindAll(o => o.progress >= o.time)) |
| 153 | { |
| 154 | try |
| 155 | { |
| 156 | tween.CompleteTween (); |
| 157 | } |
| 158 | catch(Exception e) |
| 159 | { |
| 160 | tween.tween.Abort (); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | _tweens.RemoveAll (o => o.progress >= o.time); |
| 165 | |
| 166 | foreach (ActiveTween tween in _tweens) |
| 167 | { |
| 168 | try |
| 169 | { |
| 170 | tween.UpdateTween (deltaTime); |
| 171 | } |
| 172 | catch(Exception e) |
| 173 | { |
| 174 | tween.tween.Abort (); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | private class ActiveTween |
| 181 | { |
| 182 | public Tween tween; |
| 183 | public float progress; |
| 184 | public float from, to, time; |
| 185 | |
| 186 | public void CompleteTween() |
| 187 | { |
| 188 | progress = time; |
| 189 | UpdateTween (0); |
| 190 | |
| 191 | tween = null; |
| 192 | } |
| 193 | |
| 194 | public void UpdateTween(float deltaTime) |
| 195 | { |
| 196 | tween.UpdateTween (this); |
| 197 | |
| 198 | progress += deltaTime; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | public delegate void TweenOnChangeEvent(float t); |
| 203 | public delegate void TweenOnCompleteEvent(); |
| 204 | } |