Loading...
Searching...
No Matches
easing.h
/*
This file is part of the Tweeny library.
Copyright (c) 2016-2018 Leonardo G. Lucena de Freitas
Copyright (c) 2016 Guilherme R. Costa
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef TWEENY_EASING_H
#define TWEENY_EASING_H
#include <cmath>
#include <type_traits>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
namespace tweeny {
class easing {
public:
static constexpr struct linearEasing {
template<typename T>
static typename std::enable_if<std::is_integral<T>::value, T>::type run(float position, T start, T end) {
return static_cast<T>(roundf((end - start) * position + start));
}
template<typename T>
static typename std::enable_if<!std::is_integral<T>::value, T>::type run(float position, T start, T end) {
return static_cast<T>((end - start) * position + start);
}
} linear = linearEasing{};
static constexpr struct quadraticInEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>((end - start) * position * position + start);
}
} quadraticIn = quadraticInEasing{};
static constexpr struct quadraticOutEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>((-(end - start)) * position * (position - 2) + start);
}
} quadraticOut = quadraticOutEasing{};
static constexpr struct quadraticInOutEasing {
template<typename T>
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast<T>(((end - start) / 2) * position * position + start);
}
--position;
return static_cast<T>((-(end - start) / 2) * (position * (position - 2) - 1) + start);
}
} quadraticInOut = quadraticInOutEasing{};
static constexpr struct cubicInEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>((end - start) * position * position * position + start);
}
} cubicIn = cubicInEasing{};
static constexpr struct cubicOutEasing {
template<typename T>
static T run(float position, T start, T end) {
--position;
return static_cast<T>((end - start) * (position * position * position + 1) + start);
}
} cubicOut = cubicOutEasing{};
static constexpr struct cubicInOutEasing {
template<typename T>
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast<T>(((end - start) / 2) * position * position * position + start);
}
position -= 2;
return static_cast<T>(((end - start) / 2) * (position * position * position + 2) + start);
}
} cubicInOut = cubicInOutEasing{};
static constexpr struct quarticInEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>((end - start) * position * position * position * position + start);
}
} quarticIn = quarticInEasing{};
static constexpr struct quarticOutEasing {
template<typename T>
static T run(float position, T start, T end) {
--position;
return static_cast<T>( -(end - start) * (position * position * position * position - 1) + start);
}
} quarticOut = quarticOutEasing{};
static constexpr struct quarticInOutEasing {
template<typename T>
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast<T>(((end - start) / 2) * (position * position * position * position) +
start);
}
position -= 2;
return static_cast<T>((-(end - start) / 2) * (position * position * position * position - 2) +
start);
}
} quarticInOut = quarticInOutEasing{};
static constexpr struct quinticInEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>((end - start) * position * position * position * position * position + start);
}
} quinticIn = quinticInEasing{};
static constexpr struct quinticOutEasing {
template<typename T>
static T run(float position, T start, T end) {
position--;
return static_cast<T>((end - start) * (position * position * position * position * position + 1) +
start);
}
} quinticOut = quinticOutEasing{};
static constexpr struct quinticInOutEasing {
template<typename T>
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast<T>(
((end - start) / 2) * (position * position * position * position * position) +
start);
}
position -= 2;
return static_cast<T>(
((end - start) / 2) * (position * position * position * position * position + 2) +
start);
}
} quinticInOut = quinticInOutEasing{};
static constexpr struct sinusoidalInEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>(-(end - start) * cosf(position * static_cast<float>(M_PI) / 2) + (end - start) + start);
}
} sinusoidalIn = sinusoidalInEasing{};
static constexpr struct sinusoidalOutEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>((end - start) * sinf(position * static_cast<float>(M_PI) / 2) + start);
}
} sinusoidalOut = sinusoidalOutEasing{};
static constexpr struct sinusoidalInOutEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>((-(end - start) / 2) * (cosf(position * static_cast<float>(M_PI)) - 1) + start);
}
} sinusoidalInOut = sinusoidalInOutEasing{};
static constexpr struct exponentialInEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>((end - start) * powf(2, 10 * (position - 1)) + start);
}
} exponentialIn = exponentialInEasing{};
static constexpr struct exponentialOutEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>((end - start) * (-powf(2, -10 * position) + 1) + start);
}
} exponentialOut = exponentialOutEasing{};
static constexpr struct exponentialInOutEasing {
template<typename T>
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast<T>(((end - start) / 2) * powf(2, 10 * (position - 1)) + start);
}
--position;
return static_cast<T>(((end - start) / 2) * (-powf(2, -10 * position) + 2) + start);
}
} exponentialInOut = exponentialInOutEasing{};
static constexpr struct circularInEasing {
template<typename T>
static T run(float position, T start, T end) {
return static_cast<T>( -(end - start) * (sqrtf(1 - position * position) - 1) + start );
}
} circularIn = circularInEasing{};
static constexpr struct circularOutEasing {
template<typename T>
static T run(float position, T start, T end) {
--position;
return static_cast<T>((end - start) * (sqrtf(1 - position * position)) + start);
}
} circularOut = circularOutEasing{};
static constexpr struct circularInOutEasing {
template<typename T>
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast<T>((-(end - start) / 2) * (sqrtf(1 - position * position) - 1) + start);
}
position -= 2;
return static_cast<T>(((end - start) / 2) * (sqrtf(1 - position * position) + 1) + start);
}
} circularInOut = circularInOutEasing{};
static constexpr struct bounceInEasing {
template<typename T>
static T run(float position, T start, T end) {
return (end - start) - bounceOut.run((1 - position), T(), end) + start;
}
} bounceIn = bounceInEasing{};
static constexpr struct bounceOutEasing {
template<typename T>
static T run(float position, T start, T end) {
T c = end - start;
if (position < (1 / 2.75f)) {
return static_cast<T>(c * (7.5625f * position * position) + start);
} else if (position < (2.0f / 2.75f)) {
float postFix = position -= (1.5f / 2.75f);
return static_cast<T>(c * (7.5625f * (postFix) * position + .75f) + start);
} else if (position < (2.5f / 2.75f)) {
float postFix = position -= (2.25f / 2.75f);
return static_cast<T>(c * (7.5625f * (postFix) * position + .9375f) + start);
} else {
float postFix = position -= (2.625f / 2.75f);
return static_cast<T>(c * (7.5625f * (postFix) * position + .984375f) + start);
}
}
} bounceOut = bounceOutEasing{};
static constexpr struct bounceInOutEasing {
template<typename T>
static T run(float position, T start, T end) {
if (position < 0.5f) return static_cast<T>(bounceIn.run(position * 2, T(), end) * .5f + start);
else return static_cast<T>(bounceOut.run((position * 2 - 1), T(), end) * .5f + (end - start) * .5f + start);
}
} bounceInOut = bounceInOutEasing{};
static constexpr struct elasticInEasing {
template<typename T>
static T run(float position, T start, T end) {
if (position <= 0.00001f) return start;
if (position >= 0.999f) return end;
float p = .3f;
float a = end - start;
float s = p / 4;
float postFix =
a * powf(2, 10 * (position -= 1)); // this is a fix, again, with post-increment operators
return static_cast<T>(-(postFix * sinf((position - s) * (2 * static_cast<float>(M_PI)) / p)) + start);
}
} elasticIn = elasticInEasing{};
static constexpr struct elasticOutEasing {
template<typename T>
static T run(float position, T start, T end) {
if (position <= 0.00001f) return start;
if (position >= 0.999f) return end;
float p = .3f;
float a = end - start;
float s = p / 4;
return static_cast<T>(a * powf(2, -10 * position) * sinf((position - s) * (2 * static_cast<float>(M_PI)) / p) + end);
}
} elasticOut = elasticOutEasing{};
static constexpr struct elasticInOutEasing {
template<typename T>
static T run(float position, T start, T end) {
if (position <= 0.00001f) return start;
if (position >= 0.999f) return end;
position *= 2;
float p = (.3f * 1.5f);
float a = end - start;
float s = p / 4;
float postFix;
if (position < 1) {
postFix = a * powf(2, 10 * (position -= 1)); // postIncrement is evil
return static_cast<T>(-0.5f * (postFix * sinf((position - s) * (2 * static_cast<float>(M_PI)) / p)) + start);
}
postFix = a * powf(2, -10 * (position -= 1)); // postIncrement is evil
return static_cast<T>(postFix * sinf((position - s) * (2 * static_cast<float>(M_PI)) / p) * .5f + end);
}
} elasticInOut = elasticInOutEasing{};
static constexpr struct backInEasing {
template<typename T>
static T run(float position, T start, T end) {
float s = 1.70158f;
float postFix = position;
return static_cast<T>((end - start) * (postFix) * position * ((s + 1) * position - s) + start);
}
} backIn = backInEasing{};
static constexpr struct backOutEasing {
template<typename T>
static T run(float position, T start, T end) {
float s = 1.70158f;
position -= 1;
return static_cast<T>((end - start) * ((position) * position * ((s + 1) * position + s) + 1) + start);
}
} backOut = backOutEasing{};
static constexpr struct backInOutEasing {
template<typename T>
static T run(float position, T start, T end) {
float s = 1.70158f;
float t = position;
T b = start;
T c = end - start;
float d = 1;
s *= (1.525f);
if ((t /= d / 2) < 1) return static_cast<T>(c / 2 * (t * t * (((s) + 1) * t - s)) + b);
float postFix = t -= 2;
return static_cast<T>(c / 2 * ((postFix) * t * (((s) + 1) * t + s) + 2) + b);
}
} backInOut = backInOutEasing{};
};
}
#endif //TWEENY_EASING_H
tweeny::easing
The easing class holds all the bundled easings.
Definition easing.h:120
tweeny
The tweeny namespace contains all symbols and names for the Tweeny library.
Definition MANUAL.dox:1
tweeny::easing::backInEasing
Acceelerate initial values with a "back" equation.
Definition easing.h:520
tweeny::easing::backInOutEasing
Acceelerate initial and deaccelerate ending values with a "back" equation.
Definition easing.h:546
tweeny::easing::backOutEasing
Deaccelerate ending values with a "back" equation.
Definition easing.h:533
tweeny::easing::bounceInEasing
Acceelerate initial values with a "bounce" equation.
Definition easing.h:416
tweeny::easing::bounceInOutEasing
Acceelerate initial and deaccelerate ending values with a "bounce" equation.
Definition easing.h:450
tweeny::easing::bounceOutEasing
Deaccelerate ending values with a "bounce" equation.
Definition easing.h:427
tweeny::easing::circularInEasing
Acceelerate initial values with a circular equation.
Definition easing.h:376
tweeny::easing::circularInOutEasing
Acceelerate initial and deaccelerate ending values with a circular equation.
Definition easing.h:399
tweeny::easing::circularOutEasing
Deaccelerate ending values with a circular equation.
Definition easing.h:387
tweeny::easing::cubicInEasing
Aaccelerate initial values with a cubic equation.
Definition easing.h:181
tweeny::easing::cubicInOutEasing
Acceelerate initial and deaccelerate ending values with a cubic equation.
Definition easing.h:204
tweeny::easing::cubicOutEasing
Deaccelerate ending values with a cubic equation.
Definition easing.h:192
tweeny::easing::elasticInEasing
Acceelerate initial values with an "elastic" equation.
Definition easing.h:462
tweeny::easing::elasticInOutEasing
Acceelerate initial and deaccelerate ending values with an "elastic" equation.
Definition easing.h:496
tweeny::easing::elasticOutEasing
Deaccelerate ending values with an "elastic" equation.
Definition easing.h:480
tweeny::easing::exponentialInEasing
Acceelerate initial values with an exponential equation.
Definition easing.h:338
tweeny::easing::exponentialInOutEasing
Acceelerate initial and deaccelerate ending values with an exponential equation.
Definition easing.h:360
tweeny::easing::exponentialOutEasing
Deaccelerate ending values with an exponential equation.
Definition easing.h:349
tweeny::easing::linearEasing
Values change with constant speed.
Definition easing.h:126
tweeny::easing::quadraticInEasing
Accelerate initial values with a quadratic equation.
Definition easing.h:142
tweeny::easing::quadraticInOutEasing
Acceelerate initial and deaccelerate ending values with a quadratic equation.
Definition easing.h:164
tweeny::easing::quadraticOutEasing
Deaccelerate ending values with a quadratic equation.
Definition easing.h:153
tweeny::easing::quarticInEasing
Acceelerate initial values with a quartic equation.
Definition easing.h:220
tweeny::easing::quarticInOutEasing
Acceelerate initial and deaccelerate ending values with a quartic equation.
Definition easing.h:243
tweeny::easing::quarticOutEasing
Deaccelerate ending values with a quartic equation.
Definition easing.h:231
tweeny::easing::quinticInEasing
Acceelerate initial values with a quintic equation.
Definition easing.h:261
tweeny::easing::quinticInOutEasing
Acceelerate initial and deaccelerate ending values with a quintic equation.
Definition easing.h:285
tweeny::easing::quinticOutEasing
Deaccelerate ending values with a quintic equation.
Definition easing.h:272
tweeny::easing::sinusoidalInEasing
Acceelerate initial values with a sinusoidal equation.
Definition easing.h:305
tweeny::easing::sinusoidalInOutEasing
Acceelerate initial and deaccelerate ending values with a sinusoidal equation.
Definition easing.h:327
tweeny::easing::sinusoidalOutEasing
Deaccelerate ending values with a sinusoidal equation.
Definition easing.h:316