Back to index

nux  3.0.0
Tweening.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010-2012 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "../NuxCore.h"
00024 #include "MathInc.h"
00025 
00026 #include "Tweening.h"
00027 
00028 namespace nux
00029 {
00030 
00031 // Back
00032   double BackEaseIn (double t, double b, double c, double d, double s)
00033   {
00034     //return c*(t/=d)*t*((s+1)*t - s) + b;
00035     t /= d;
00036     return c * t * t * ( (s + 1) * t - s) + b;
00037   }
00038 
00039   double BackEaseOut (double t, double b, double c, double d, double s)
00040   {
00041     //return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
00042     t = t / d - 1;
00043     return c * (t * t * ( (s + 1) * t + s) + 1) + b;
00044   }
00045 
00046   double BackEaseInOut (double t, double b, double c, double d, double s)
00047   {
00048 //     if ((t/=d/2) < 1)
00049 //         return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
00050 //     return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
00051 
00052     if ( (t /= d / 2) < 1)
00053     {
00054       s *= (1.525);
00055       return c / 2 * (t * t * ( (s + 1) * t - s) ) + b;
00056     }
00057 
00058     s *= 1.525;
00059     t -= 2.0;
00060     return c / 2 * (t * t * ( (s + 1) * t + s) + 2) + b;
00061   }
00062 
00063 // Bounce
00064   double BounceEaseOut (double t, double b, double c, double d)
00065   {
00066     if ( (t /= d) < (1 / 2.75) )
00067     {
00068       return c * (7.5625 * t * t) + b;
00069     }
00070     else if (t < (2 / 2.75) )
00071     {
00072       t -= (1.5 / 2.75);
00073       return c * (7.5625 * t * t + .75) + b;
00074     }
00075     else if (t < (2.5 / 2.75) )
00076     {
00077       t -= (2.25 / 2.75);
00078       return c * (7.5625 * t * t + .9375) + b;
00079     }
00080     else
00081     {
00082       t -= (2.625 / 2.75);
00083       return c * (7.5625 * t * t + .984375) + b;
00084     }
00085   }
00086 
00087   double BounceEaseIn (double t, double b, double c, double d)
00088   {
00089     return c - BounceEaseOut (d - t, 0, c, d) + b;
00090   }
00091 
00092   double BounceEaseInOut (double t, double b, double c, double d)
00093   {
00094     if (t < d / 2.0)
00095       return BounceEaseIn (t * 2, 0, c, d) * .5 + b;
00096     else
00097       return BounceEaseOut (t * 2 - d, 0, c, d) * .5 + c * .5 + b;
00098   }
00099 
00100 // Circ
00101   double CircEaseIn (double t, double b, double c, double d)
00102   {
00103 
00104     t /= d;
00105     return -c * (std::sqrt (1 - t * t) - 1) + b;
00106   }
00107 
00108   double CircEaseOut (double t, double b, double c, double d)
00109   {
00110     t = t / d - 1.0;
00111     return c * std::sqrt (1.0 - t * t) + b;
00112   }
00113 
00114   double CircEaseInOut (double t, double b, double c, double d)
00115   {
00116     if ( (t /= d / 2) < 1)
00117     {
00118       return -c / 2 * (std::sqrt (1 - t * t) - 1) + b;
00119     }
00120 
00121     t -= 2.0;
00122     return c / 2 * (std::sqrt (1.0 - t * t) + 1) + b;
00123   }
00124 
00125 // Cubic
00126   double CubicEaseIn (double t, double b, double c, double d)
00127   {
00128     t /= d;
00129     return c * t * t * t + b;
00130   }
00131 
00132   double CubicEaseOut (double t, double b, double c, double d)
00133   {
00134     t = t / d - 1.0;
00135     return c * (t * t * t + 1.0) + b;
00136   }
00137 
00138   double CubicEaseInOut (double t, double b, double c, double d)
00139   {
00140     if ( (t /= d / 2) < 1)
00141     {
00142       return c / 2 * t * t * t + b;
00143     }
00144 
00145     t -= 2.0;
00146     return c / 2 * (t * t * t + 2) + b;
00147   }
00148 
00149 // Elastic
00150   double ElasticEaseIn (double t, double b, double c, double d, double a, double p)
00151   {
00152     double s = 0;
00153 
00154     if (t == 0)
00155       return b;
00156 
00157     if ( (t /= d) == 1)
00158       return b + c;
00159 
00160     if (!p)
00161       p = d * .3;
00162 
00163     if (!a || a < std::abs (c) )
00164     {
00165       a = c;
00166       s = p / 4;
00167     }
00168     else
00169       s = p / (2 * constants::pi) * std::asin (c / a);
00170 
00171     t -= 1.0;
00172     return - (a * std::pow (2, 10 * t) * std::sin ( (t * d - s) * (2 * constants::pi) / p ) ) + b;
00173   }
00174 
00175   double ElasticEaseOut (double t, double b, double c, double d, double a, double p)
00176   {
00177     double s = 0;
00178 
00179     if (t == 0)
00180       return b;
00181 
00182     if ( (t /= d) == 1)
00183       return b + c;
00184 
00185     if (!p)
00186       p = d * .3;
00187 
00188     if (!a || a < std::abs (c) )
00189     {
00190       a = c;
00191       s = p / 4;
00192     }
00193     else
00194       s = p / (2 * constants::pi) * std::asin (c / a);
00195 
00196     return (a * std::pow (2, -10 * t) * std::sin ( (t * d - s) * (2 * constants::pi) / p ) + c + b);
00197   }
00198 
00199   double ElasticEaseInOut (double t, double b, double c, double d, double a, double p)
00200   {
00201     double s = 0;
00202 
00203     if (t == 0)
00204       return b;
00205 
00206     if ( (t /= d / 2) == 2)
00207       return b + c;
00208 
00209     if (!p)
00210       p = d * (.3 * 1.5);
00211 
00212     if (!a || a < std::abs (c) )
00213     {
00214       a = c;
00215       s = p / 4;
00216     }
00217     else
00218     {
00219       s = p / (2 * constants::pi) * std::asin (c / a);
00220     }
00221 
00222     if (t < 1.0)
00223     {
00224       t -= 1;
00225       return -0.5 * (a * std::pow (2, 10.0 * t) * std::sin ( (t * d - s) * (2 * constants::pi) / p ) ) + b;
00226     }
00227 
00228     t -= 1;
00229     return a * std::pow (2, -10 * t) * std::sin ( (t * d - s) * (2 * constants::pi) / p ) * .5 + c + b;
00230   }
00231 
00232 // Expo
00233   double ExpoEaseIn (double t, double b, double c, double d)
00234   {
00235     return (t == 0) ? b : c * std::pow (2, 10 * (t / d - 1) ) + b;
00236   }
00237 
00238   double ExpoEaseOut (double t, double b, double c, double d)
00239   {
00240     return (t == d) ? b + c : c * (-std::pow (2, -10 * t / d) + 1) + b;
00241   }
00242 
00243   double ExpoEaseInOut (double t, double b, double c, double d)
00244   {
00245     if (t == 0)
00246       return b;
00247 
00248     if (t == d)
00249       return b + c;
00250 
00251     if ( (t /= d / 2) < 1)
00252       return c / 2 * std::pow (2, 10 * (t - 1) ) + b;
00253 
00254     return c / 2 * (-std::pow (2, -10 * --t) + 2) + b;
00255   }
00256 
00257 // Linear
00258   double LinearEaseNone (double t, double b, double c, double d)
00259   {
00260     return c * t / d + b;
00261   }
00262 
00263   double LinearEaseIn (double t, double b, double c, double d)
00264   {
00265     return c * t / d + b;
00266   }
00267 
00268   double LinearEaseOut (double t, double b, double c, double d)
00269   {
00270     return c * t / d + b;
00271   }
00272 
00273   double LinearEaseInOut (double t, double b, double c, double d)
00274   {
00275     return c * t / d + b;
00276   }
00277 
00278 // Quad
00279   double QuadEaseIn (double t, double b, double c, double d)
00280   {
00281     t /= d;
00282     return c * t * t + b;
00283   }
00284 
00285   double QuadEaseOut (double t, double b, double c, double d)
00286   {
00287     t /= d;
00288     return -c * t * (t - 2) + b;
00289   }
00290 
00291   double QuadEaseInOut (double t, double b, double c, double d)
00292   {
00293     if ( (t /= d / 2) < 1)
00294     {
00295       return c / 2 * t * t + b;
00296     }
00297 
00298     --t;
00299     return -c / 2 * (t * (t - 2) - 1) + b;
00300   }
00301 
00302 // Quart
00303   double QuartEaseIn (double t, double b, double c, double d)
00304   {
00305     t /= d;
00306     return c * t * t * t * t + b;
00307   }
00308 
00309   double QuartEaseOut (double t, double b, double c, double d)
00310   {
00311     t = t / d - 1;
00312     return -c * (t * t * t * t - 1) + b;
00313   }
00314 
00315   double QuartEaseInOut (double t, double b, double c, double d)
00316   {
00317     if ( (t /= d / 2) < 1)
00318     {
00319       return c / 2 * t * t * t * t + b;
00320     }
00321 
00322     t -= 2.0;
00323     return -c / 2 * (t * t * t * t - 2) + b;
00324   }
00325 
00326 // Quint
00327   double QuintEaseIn (double t, double b, double c, double d)
00328   {
00329     t /= d;
00330     return c * t * t * t * t * t + b;
00331   }
00332 
00333   double QuintEaseOut (double t, double b, double c, double d)
00334   {
00335     t = t / d - 1;
00336     return c * (t * t * t * t * t + 1) + b;
00337   }
00338 
00339   double QuintEaseInOut (double t, double b, double c, double d)
00340   {
00341     if ( (t /= d / 2) < 1)
00342     {
00343       return c / 2 * t * t * t * t * t + b;
00344     }
00345 
00346     t -= 2;
00347     return c / 2 * (t * t * t * t * t + 2) + b;
00348   }
00349 
00350 // Sine
00351   double SineEaseIn (double t, double b, double c, double d)
00352   {
00353     return -c * std::cos (t / d * (constants::pi / 2) ) + c + b;
00354   }
00355 
00356   double SineEaseOut (double t, double b, double c, double d)
00357   {
00358     return c * std::sin (t / d * (constants::pi / 2) ) + b;
00359   }
00360 
00361   double SineEaseInOut (double t, double b, double c, double d)
00362   {
00363     return -c / 2 * (std::cos (constants::pi * t / d) - 1) + b;
00364   }
00365 
00366 }