5 * Copyright (C) 2005-2013 Team XBMC
8 * This Program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This Program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with XBMC; see the file COPYING. If not, see
20 * <http://www.gnu.org/licenses/>.
24 ///////////////////////////////////////////////////////////////////////
26 // A couple of tweening classes implemented in C++.
27 // ref: http://www.robertpenner.com/easing/
29 // Author: d4rk <d4rk@xbmc.org>
30 ///////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////
34 // Current list of classes:
45 ///////////////////////////////////////////////////////////////////////
50 #define M_PI 3.14159265358979323846f
64 Tweener(TweenerType tweenerType = EASE_OUT) { m_tweenerType = tweenerType; }
65 virtual ~Tweener() {};
67 void SetEasing(TweenerType type) { m_tweenerType = type; }
68 virtual float Tween(float time, float start, float change, float duration)=0;
69 virtual bool HasResumePoint() const { return m_tweenerType == EASE_INOUT; }
71 TweenerType m_tweenerType;
75 class LinearTweener : public Tweener
78 virtual float Tween(float time, float start, float change, float duration)
80 return change * time / duration + start;
82 virtual bool HasResumePoint() const { return false; }
86 class QuadTweener : public Tweener
89 QuadTweener(float a = 1.0f) { _a=a; }
90 virtual float Tween(float time, float start, float change, float duration)
92 switch (m_tweenerType)
96 return change * time * (_a * time + 1 - _a) + start;
101 return -change * time * (_a * time - 1 - _a) + start;
107 return (change) * time * (_a * time + 1 - _a) + start;
109 return (-change) * time * (_a * time - 1 - _a) + start;
112 return change * time * time + start;
119 class CubicTweener : public Tweener
122 virtual float Tween(float time, float start, float change, float duration)
124 switch (m_tweenerType)
128 return change * time * time * time + start;
134 return change * (time * time * time + 1) + start;
140 return (change/2) * time * time * time + start;
142 return (change/2) * (time * time * time + 2) + start;
145 return change * time * time + start;
149 class CircleTweener : public Tweener
152 virtual float Tween(float time, float start, float change, float duration)
154 switch (m_tweenerType)
158 return (-change) * (sqrt(1 - time * time) - 1) + start;
164 return change * sqrt(1 - time * time) + start;
170 return (-change/2) * (sqrt(1 - time * time) - 1) + start;
172 return change/2 * (sqrt(1 - time * time) + 1) + start;
175 return change * sqrt(1 - time * time) + start;
179 class BackTweener : public Tweener
182 BackTweener(float s=1.70158) { _s=s; }
184 virtual float Tween(float time, float start, float change, float duration)
187 switch (m_tweenerType)
191 return change * time * time * ((s + 1) * time - s) + start;
197 return change * (time * time * ((s + 1) * time + s) + 1) + start;
205 return (change/2) * (time * time * ((s + 1) * time - s)) + start;
208 return (change/2) * (time * time * ((s + 1) * time + s) + 2) + start;
211 return change * ((time-1) * time * ((s + 1) * time + s) + 1) + start;
219 class SineTweener : public Tweener
222 virtual float Tween(float time, float start, float change, float duration)
225 switch (m_tweenerType)
228 return change * (1 - cos(time * M_PI / 2.0f)) + start;
232 return change * sin(time * M_PI / 2.0f) + start;
236 return change/2 * (1 - cos(M_PI * time)) + start;
239 return (change/2) * (1 - cos(M_PI * time)) + start;
244 class BounceTweener : public Tweener
247 virtual float Tween(float time, float start, float change, float duration)
249 switch (m_tweenerType)
252 return (change - easeOut(duration - time, 0, change, duration)) + start;
256 return easeOut(time, start, change, duration);
260 if (time < duration/2)
261 return (change - easeOut (duration - (time * 2), 0, change, duration) + start) * .5f + start;
263 return (easeOut (time * 2 - duration, 0, change, duration) * .5f + change * .5f) + start;
267 return easeOut(time, start, change, duration);
270 float easeOut(float time, float start, float change, float duration)
273 if (time < (1/2.75)) {
274 return change * (7.5625f * time * time) + start;
275 } else if (time < (2/2.75)) {
276 time -= (1.5f/2.75f);
277 return change * (7.5625f * time * time + .75f) + start;
278 } else if (time < (2.5/2.75)) {
279 time -= (2.25f/2.75f);
280 return change * (7.5625f * time * time + .9375f) + start;
282 time -= (2.625f/2.75f);
283 return change * (7.5625f * time * time + .984375f) + start;
289 class ElasticTweener : public Tweener
292 ElasticTweener(float a=0.0, float p=0.0) { _a=a; _p=p; }
294 virtual float Tween(float time, float start, float change, float duration)
296 switch (m_tweenerType)
299 return easeIn(time, start, change, duration);
303 return easeOut(time, start, change, duration);
307 return easeInOut(time, start, change, duration);
310 return easeOut(time, start, change, duration);
316 float easeIn(float time, float start, float change, float duration)
326 return start + change;
329 if (!a || a < fabs(change))
336 s = p / (2 * M_PI) * asin (change / a);
339 return -(a * pow(2.0f, 10*time) * sin((time * duration - s) * (2 * M_PI) / p )) + start;
342 float easeOut(float time, float start, float change, float duration)
352 return start + change;
355 if (!a || a < fabs(change))
362 s = p / (2 * M_PI) * asin (change / a);
364 return (a * pow(2.0f, -10*time) * sin((time * duration - s) * (2 * M_PI) / p )) + change + start;
367 float easeInOut(float time, float start, float change, float duration)
377 return start + change;
380 if (!a || a < fabs(change))
387 s = p / (2 * M_PI) * asin (change / a);
393 return -.5f * (a * pow(2.0f, 10 * (time)) * sin((time * duration - s) * (2 * M_PI) / p )) + start;
396 return a * pow(2.0f, -10 * (time)) * sin((time * duration-s) * (2 * M_PI) / p ) * .5f + change + start;
402 #endif // __TWEEN_H__