/*
This file is part of the Tweeny library.
Copyright (c) 2016-2021 Leonardo Guilherme 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.
*/
/**
* @file tweeny.h
* This file is the main header file for Tweeny. You should not need to include anything else.
*/
/**
* @mainpage Tweeny
*
* Tweeny is an inbetweening library designed for the creation of complex animations for games and other beautiful
* interactive software. It leverages features of modern C++ to empower developers with an intuitive API for
* declaring tweenings of any type of value, as long as they support arithmetic operations.
*
* This document contains Tweeny's API reference. The most interesting parts are:
*
* * The Fine @ref manual
* * The tweeny::from global function, to start a new tween.
* * The tweeny::tween class itself, that has all the interesting methods for a tween.
* * The modules page has a list of type of easings.
*
* This is how the API looks like:
*
* @code
*
* #include "tweeny.h"
*
* using tweeny::easing;
*
* int main() {
* // steps 1% each iteration
* auto tween = tweeny::from(0).to(100).during(100).via(easing::linear);
* while (tween.progress() < 1.0f) tween.step(0.01f);
*
* // a tween with multiple values
* auto tween2 = tweeny::from(0, 1.0f).to(1200, 7.0f).during(1000).via(easing::backInOut, easing::linear);
*
* // a tween with multiple points, different easings and durations
* auto tween3 = tweeny::from(0, 0)
* .to(100, 100).during(100).via(easing::backOut, easing::backOut)
* .to(200, 200).during(500).via(easing::linear);
* return 0;
* }
*
* @endcode
*
* **Examples**
*
* * Check tweeny-demos repository to see demonstration code
*
* **Useful links and references**
* * Tim Groleau's easing function generator (requires flash)
* * Easing cheat sheet (contains graphics!)
*/
#ifndef TWEENY_H
#define TWEENY_H
/*
This file is part of the Tweeny library.
Copyright (c) 2016-2021 Leonardo Guilherme 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.
*/
/**
* @file tween.h
* This file contains the core of tweeny: the main tween class.
*/
#ifndef TWEENY_TWEEN_H
#define TWEENY_TWEEN_H
#include
#include
#include
/*
This file is part of the Tweeny library.
Copyright (c) 2016-2021 Leonardo Guilherme 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.
*/
/*
* This file provides useful typedefs and traits for a tween.
*/
#ifndef TWEENY_TWEENTRAITS_H
#define TWEENY_TWEENTRAITS_H
#include
#include
#include
namespace tweeny {
template class tween;
namespace detail {
template struct equal {};
template struct equal { enum { value = true }; };
template struct equal {
enum { value = std::is_same::value && equal::value && equal::value };
};
template struct first { typedef T type; };
template
struct valuetype { };
template
struct valuetype {
typedef std::tuple type;
};
template
struct valuetype {
typedef std::array::type, sizeof...(Ts)> type;
};
template
struct tweentraits {
typedef std::tuple...> easingCollection;
typedef std::function &, Ts...)> callbackType;
typedef std::function &)> noValuesCallbackType;
typedef std::function noTweenCallbackType;
typedef typename valuetype::value, Ts...>::type valuesType;
typedef std::array durationsArrayType;
typedef tween type;
};
}
}
#endif //TWEENY_TWEENTRAITS_H
/*
This file is part of the Tweeny library.
Copyright (c) 2016-2021 Leonardo Guilherme 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.
*/
/*
* This file provides the declarations for a tween point utility class. A tweenpoint holds the tween values,
* easings and durations.
*/
#ifndef TWEENY_TWEENPOINT_H
#define TWEENY_TWEENPOINT_H
#include
#include
namespace tweeny {
namespace detail {
/*
* The tweenpoint class aids in the management of a tweening point by the tween class.
* This class is private.
*/
template
struct tweenpoint {
typedef detail::tweentraits traits;
typename traits::valuesType values;
typename traits::durationsArrayType durations;
typename traits::easingCollection easings;
typename traits::callbackType onEnterCallbacks;
uint32_t stacked;
/* Constructs a tweenpoint from a set of values, filling their durations and easings */
tweenpoint(Ts... vs);
/* Set the duration for all the values in this point */
template void during(D milis);
/* Sets the duration for each value in this point */
template void during(Ds... vs);
/* Sets the easing functions of each value */
template void via(Fs... fs);
/* Sets the same easing function for all values */
template void via(F f);
/* Returns the highest value in duration array */
uint16_t duration() const;
/* Returns the value of that specific value */
uint16_t duration(size_t i) const;
};
}
}
/*
This file is part of the Tweeny library.
Copyright (c) 2016-2021 Leonardo Guilherme 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.
*/
/*
* This file implements the tweenpoint class
*/
#ifndef TWEENY_TWEENPOINT_TCC
#define TWEENY_TWEENPOINT_TCC
#include
#include
/*
This file is part of the Tweeny library.
Copyright (c) 2016-2021 Leonardo Guilherme 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.
*/
/**
* @file easing.h
* The purpose of this file is to list all bundled easings. All easings are based on Robert Penner's easing
* functions: http://robertpenner.com/easing/
*/
#ifndef TWEENY_EASING_H
#define TWEENY_EASING_H
#include
#include
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/**
* @defgroup easings Easings
* @brief Bundled easing functions based on
* Robert Penner's Easing Functions
* @details You should plug these functions into @ref tweeny::tween::via function to specify the easing used in a tween.
* @sa tweeny::easing
* @{
*//**
* @defgroup stepped Stepped
* @{
* @brief The value does not change. No interpolation is used.
* @}
*//**
* @defgroup default Default
* @{
* @brief A default mode for arithmetic values it will change in constant speed, for non-arithmetic value will be constant.
* @}
*//**
* @defgroup linear Linear
* @{
* @brief The most boring ever easing function. It has no acceleration and change values in constant speed.
* @}
*//**
* @defgroup quadratic Quadratic
* @{
* @brief The most commonly used easing functions.
* @}
*//**
* @defgroup cubic Cubic
* @{
* @brief A bit curvier than the quadratic easing.
* @}
*//**
* @defgroup quartic Quartic
* @{
* @brief A steeper curve. Acceleration changes faster than Cubic.
* @}
*//**
* @defgroup quintic Quintic
* @{
* @brief An even steeper curve. Acceleration changes really fast.
* @}
*//**
* @defgroup sinuisodal Sinuisodal
* @{
* @brief A very gentle curve, gentlier than quadratic.
* @}
*//**
* @defgroup exponential Exponential
* @{
* @brief A very steep curve, based on the `p(t) = 2^(10*(t-1))` equation.
* @}
*//**
* @defgroup circular Circular
* @{
* @brief A smooth, circular slope that resembles the arc of an circle.
* @}
*//**
* @defgroup back Back
* @{
* @brief An easing function that has a "cute" natural coming back effect.
* @}
*//**
* @defgroup elastic Elastic
* @{
* @brief An elastic easing function. Values go a little past the maximum/minimum in an elastic effect.
* @}
*//**
* @defgroup bounce Bounce
* @{
* @brief A bouncing easing function. Values "bounce" around the maximum/minumum.
* @}
*//**
* @}
*/
namespace tweeny {
/**
* @brief The easing class holds all the bundled easings.
*
* You should pass the easing function to the @p tweeny::tween::via method, to set the easing function that will
* be used to interpolate values in a tween point.
*
* **Example**:
*
* @code
* auto tween = tweeny::from(0).to(100).via(tweeny::easing::linear);
* @endcode
*/
class easing {
public:
/**
* @brief Enumerates all easings to aid in runtime when adding easins to a tween using tween::via
*
* The aim of this enum is to help in situations where the easing doesn't come straight from the C++
* code but rather from a configuration file or some sort of external paramenter.
*/
enum class enumerated {
def,
linear,
stepped,
quadraticIn,
quadraticOut,
quadraticInOut,
cubicIn,
cubicOut,
cubicInOut,
quarticIn,
quarticOut,
quarticInOut,
quinticIn,
quinticOut,
quinticInOut,
sinusoidalIn,
sinusoidalOut,
sinusoidalInOut,
exponentialIn,
exponentialOut,
exponentialInOut,
circularIn,
circularOut,
circularInOut,
bounceIn,
bounceOut,
bounceInOut,
elasticIn,
elasticOut,
elasticInOut,
backIn,
backOut,
backInOut
};
/**
* @ingroup stepped
* @brief Value is constant.
*/
static constexpr struct steppedEasing {
template
static T run(float position, T start, T end) {
return start;
}
} stepped = steppedEasing{};
/**
* @ingroup default
* @brief Values change with constant speed for arithmetic type only. The non-arithmetic it will be constant.
*/
static constexpr struct defaultEasing {
template struct voidify { using type = void; };
template using void_t = typename voidify::type;
template
struct supports_arithmetic_operations : std::false_type {};
template
struct supports_arithmetic_operations() + std::declval()),
decltype(std::declval() - std::declval()),
decltype(std::declval() * std::declval()),
decltype(std::declval() * std::declval()),
decltype(std::declval() * std::declval())
>> : std::true_type{};
template
static typename std::enable_if::value, T>::type run(float position, T start, T end) {
return static_cast(roundf((end - start) * position + start));
}
template
static typename std::enable_if::value && !std::is_integral::value, T>::type run(float position, T start, T end) {
return static_cast((end - start) * position + start);
}
template
static typename std::enable_if::value, T>::type run(float position, T start, T end) {
return start;
}
} def = defaultEasing{};
/**
* @ingroup linear
* @brief Values change with constant speed.
*/
static constexpr struct linearEasing {
template
static typename std::enable_if::value, T>::type run(float position, T start, T end) {
return static_cast(roundf((end - start) * position + start));
}
template
static typename std::enable_if::value, T>::type run(float position, T start, T end) {
return static_cast((end - start) * position + start);
}
} linear = linearEasing{};
/**
* @ingroup quadratic
* @brief Accelerate initial values with a quadratic equation.
*/
static constexpr struct quadraticInEasing {
template
static T run(float position, T start, T end) {
return static_cast((end - start) * position * position + start);
}
} quadraticIn = quadraticInEasing{};
/**
* @ingroup quadratic
* @brief Deaccelerate ending values with a quadratic equation.
*/
static constexpr struct quadraticOutEasing {
template
static T run(float position, T start, T end) {
return static_cast((-(end - start)) * position * (position - 2) + start);
}
} quadraticOut = quadraticOutEasing{};
/**
* @ingroup quadratic
* @brief Acceelerate initial and deaccelerate ending values with a quadratic equation.
*/
static constexpr struct quadraticInOutEasing {
template
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast(((end - start) / 2) * position * position + start);
}
--position;
return static_cast((-(end - start) / 2) * (position * (position - 2) - 1) + start);
}
} quadraticInOut = quadraticInOutEasing{};
/**
* @ingroup cubic
* @brief Aaccelerate initial values with a cubic equation.
*/
static constexpr struct cubicInEasing {
template
static T run(float position, T start, T end) {
return static_cast((end - start) * position * position * position + start);
}
} cubicIn = cubicInEasing{};
/**
* @ingroup cubic
* @brief Deaccelerate ending values with a cubic equation.
*/
static constexpr struct cubicOutEasing {
template
static T run(float position, T start, T end) {
--position;
return static_cast((end - start) * (position * position * position + 1) + start);
}
} cubicOut = cubicOutEasing{};
/**
* @ingroup cubic
* @brief Acceelerate initial and deaccelerate ending values with a cubic equation.
*/
static constexpr struct cubicInOutEasing {
template
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast(((end - start) / 2) * position * position * position + start);
}
position -= 2;
return static_cast(((end - start) / 2) * (position * position * position + 2) + start);
}
} cubicInOut = cubicInOutEasing{};
/**
* @ingroup quartic
* @brief Acceelerate initial values with a quartic equation.
*/
static constexpr struct quarticInEasing {
template
static T run(float position, T start, T end) {
return static_cast((end - start) * position * position * position * position + start);
}
} quarticIn = quarticInEasing{};
/**
* @ingroup quartic
* @brief Deaccelerate ending values with a quartic equation.
*/
static constexpr struct quarticOutEasing {
template
static T run(float position, T start, T end) {
--position;
return static_cast( -(end - start) * (position * position * position * position - 1) + start);
}
} quarticOut = quarticOutEasing{};
/**
* @ingroup quartic
* @brief Acceelerate initial and deaccelerate ending values with a quartic equation.
*/
static constexpr struct quarticInOutEasing {
template
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast(((end - start) / 2) * (position * position * position * position) +
start);
}
position -= 2;
return static_cast((-(end - start) / 2) * (position * position * position * position - 2) +
start);
}
} quarticInOut = quarticInOutEasing{};
/**
* @ingroup quintic
* @brief Acceelerate initial values with a quintic equation.
*/
static constexpr struct quinticInEasing {
template
static T run(float position, T start, T end) {
return static_cast((end - start) * position * position * position * position * position + start);
}
} quinticIn = quinticInEasing{};
/**
* @ingroup quintic
* @brief Deaccelerate ending values with a quintic equation.
*/
static constexpr struct quinticOutEasing {
template
static T run(float position, T start, T end) {
position--;
return static_cast((end - start) * (position * position * position * position * position + 1) +
start);
}
} quinticOut = quinticOutEasing{};
/**
* @ingroup quintic
* @brief Acceelerate initial and deaccelerate ending values with a quintic equation.
*/
static constexpr struct quinticInOutEasing {
template
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast(
((end - start) / 2) * (position * position * position * position * position) +
start);
}
position -= 2;
return static_cast(
((end - start) / 2) * (position * position * position * position * position + 2) +
start);
}
} quinticInOut = quinticInOutEasing{};
/**
* @ingroup sinusoidal
* @brief Acceelerate initial values with a sinusoidal equation.
*/
static constexpr struct sinusoidalInEasing {
template
static T run(float position, T start, T end) {
return static_cast(-(end - start) * cosf(position * static_cast(M_PI) / 2) + (end - start) + start);
}
} sinusoidalIn = sinusoidalInEasing{};
/**
* @ingroup sinusoidal
* @brief Deaccelerate ending values with a sinusoidal equation.
*/
static constexpr struct sinusoidalOutEasing {
template
static T run(float position, T start, T end) {
return static_cast((end - start) * sinf(position * static_cast(M_PI) / 2) + start);
}
} sinusoidalOut = sinusoidalOutEasing{};
/**
* @ingroup sinusoidal
* @brief Acceelerate initial and deaccelerate ending values with a sinusoidal equation.
*/
static constexpr struct sinusoidalInOutEasing {
template
static T run(float position, T start, T end) {
return static_cast((-(end - start) / 2) * (cosf(position * static_cast(M_PI)) - 1) + start);
}
} sinusoidalInOut = sinusoidalInOutEasing{};
/**
* @ingroup exponential
* @brief Acceelerate initial values with an exponential equation.
*/
static constexpr struct exponentialInEasing {
template
static T run(float position, T start, T end) {
return static_cast((end - start) * powf(2, 10 * (position - 1)) + start);
}
} exponentialIn = exponentialInEasing{};
/**
* @ingroup exponential
* @brief Deaccelerate ending values with an exponential equation.
*/
static constexpr struct exponentialOutEasing {
template
static T run(float position, T start, T end) {
return static_cast((end - start) * (-powf(2, -10 * position) + 1) + start);
}
} exponentialOut = exponentialOutEasing{};
/**
* @ingroup exponential
* @brief Acceelerate initial and deaccelerate ending values with an exponential equation.
*/
static constexpr struct exponentialInOutEasing {
template
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast(((end - start) / 2) * powf(2, 10 * (position - 1)) + start);
}
--position;
return static_cast(((end - start) / 2) * (-powf(2, -10 * position) + 2) + start);
}
} exponentialInOut = exponentialInOutEasing{};
/**
* @ingroup circular
* @brief Acceelerate initial values with a circular equation.
*/
static constexpr struct circularInEasing {
template
static T run(float position, T start, T end) {
return static_cast( -(end - start) * (sqrtf(1 - position * position) - 1) + start );
}
} circularIn = circularInEasing{};
/**
* @ingroup circular
* @brief Deaccelerate ending values with a circular equation.
*/
static constexpr struct circularOutEasing {
template
static T run(float position, T start, T end) {
--position;
return static_cast((end - start) * (sqrtf(1 - position * position)) + start);
}
} circularOut = circularOutEasing{};
/**
* @ingroup circular
* @brief Acceelerate initial and deaccelerate ending values with a circular equation.
*/
static constexpr struct circularInOutEasing {
template
static T run(float position, T start, T end) {
position *= 2;
if (position < 1) {
return static_cast((-(end - start) / 2) * (sqrtf(1 - position * position) - 1) + start);
}
position -= 2;
return static_cast(((end - start) / 2) * (sqrtf(1 - position * position) + 1) + start);
}
} circularInOut = circularInOutEasing{};
/**
* @ingroup bounce
* @brief Acceelerate initial values with a "bounce" equation.
*/
static constexpr struct bounceInEasing {
template
static T run(float position, T start, T end) {
return (end - start) - bounceOut.run((1 - position), T(), end) + start;
}
} bounceIn = bounceInEasing{};
/**
* @ingroup bounce
* @brief Deaccelerate ending values with a "bounce" equation.
*/
static constexpr struct bounceOutEasing {
template
static T run(float position, T start, T end) {
T c = end - start;
if (position < (1 / 2.75f)) {
return static_cast(c * (7.5625f * position * position) + start);
} else if (position < (2.0f / 2.75f)) {
float postFix = position -= (1.5f / 2.75f);
return static_cast(c * (7.5625f * (postFix) * position + .75f) + start);
} else if (position < (2.5f / 2.75f)) {
float postFix = position -= (2.25f / 2.75f);
return static_cast(c * (7.5625f * (postFix) * position + .9375f) + start);
} else {
float postFix = position -= (2.625f / 2.75f);
return static_cast(c * (7.5625f * (postFix) * position + .984375f) + start);
}
}
} bounceOut = bounceOutEasing{};
/**
* @ingroup bounce
* @brief Acceelerate initial and deaccelerate ending values with a "bounce" equation.
*/
static constexpr struct bounceInOutEasing {
template
static T run(float position, T start, T end) {
if (position < 0.5f) return static_cast(bounceIn.run(position * 2, T(), end) * .5f + start);
else return static_cast(bounceOut.run((position * 2 - 1), T(), end) * .5f + (end - start) * .5f + start);
}
} bounceInOut = bounceInOutEasing{};
/**
* @ingroup elastic
* @brief Acceelerate initial values with an "elastic" equation.
*/
static constexpr struct elasticInEasing {
template
static T run(float position, T start, T end) {
if (position <= 0.00001f) return start;
if (position >= 0.999f) return end;
float p = .3f;
auto 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(-(postFix * sinf((position - s) * (2 * static_cast(M_PI)) / p)) + start);
}
} elasticIn = elasticInEasing{};
/**
* @ingroup elastic
* @brief Deaccelerate ending values with an "elastic" equation.
*/
static constexpr struct elasticOutEasing {
template
static T run(float position, T start, T end) {
if (position <= 0.00001f) return start;
if (position >= 0.999f) return end;
float p = .3f;
auto a = end - start;
float s = p / 4;
return static_cast(a * powf(2, -10 * position) * sinf((position - s) * (2 * static_cast(M_PI)) / p) + end);
}
} elasticOut = elasticOutEasing{};
/**
* @ingroup elastic
* @brief Acceelerate initial and deaccelerate ending values with an "elastic" equation.
*/
static constexpr struct elasticInOutEasing {
template
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);
auto 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(-0.5f * (postFix * sinf((position - s) * (2 * static_cast(M_PI)) / p)) + start);
}
postFix = a * powf(2, -10 * (position -= 1)); // postIncrement is evil
return static_cast(postFix * sinf((position - s) * (2 * static_cast(M_PI)) / p) * .5f + end);
}
} elasticInOut = elasticInOutEasing{};
/**
* @ingroup back
* @brief Acceelerate initial values with a "back" equation.
*/
static constexpr struct backInEasing {
template
static T run(float position, T start, T end) {
float s = 1.70158f;
float postFix = position;
return static_cast((end - start) * (postFix) * position * ((s + 1) * position - s) + start);
}
} backIn = backInEasing{};
/**
* @ingroup back
* @brief Deaccelerate ending values with a "back" equation.
*/
static constexpr struct backOutEasing {
template
static T run(float position, T start, T end) {
float s = 1.70158f;
position -= 1;
return static_cast((end - start) * ((position) * position * ((s + 1) * position + s) + 1) + start);
}
} backOut = backOutEasing{};
/**
* @ingroup back
* @brief Acceelerate initial and deaccelerate ending values with a "back" equation.
*/
static constexpr struct backInOutEasing {
template
static T run(float position, T start, T end) {
float s = 1.70158f;
float t = position;
auto b = start;
auto c = end - start;
float d = 1;
s *= (1.525f);
if ((t /= d / 2) < 1) return static_cast(c / 2 * (t * t * (((s) + 1) * t - s)) + b);
float postFix = t -= 2;
return static_cast(c / 2 * ((postFix) * t * (((s) + 1) * t + s) + 2) + b);
}
} backInOut = backInOutEasing{};
};
}
#endif //TWEENY_EASING_H
/*
This file is part of the Tweeny library.
Copyright (c) 2016-2021 Leonardo Guilherme 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.
*/
/*
* This file provides the easing resolution mechanism so that the library user can mix lambdas and the bundled
* pre-defined easing functions. It shall not be used directly.
* This file is private.
*/
#ifndef TWEENY_EASINGRESOLVE_H
#define TWEENY_EASINGRESOLVE_H
#include
namespace tweeny {
namespace detail {
using std::get;
template
struct easingresolve {
static void impl(FunctionTuple &b, Fs... fs) {
if (sizeof...(Fs) == 0) return;
easingresolve::impl(b, fs...);
}
};
template
struct easingresolve {
static void impl(FunctionTuple &b, F1 f1, Fs... fs) {
get(b) = f1;
easingresolve::impl(b, fs...);
}
};
template
struct easingresolve {
typedef typename std::tuple_element::type ArgType;
static void impl(FunctionTuple &b, easing::steppedEasing, Fs... fs) {
get(b) = easing::stepped.run;
easingresolve::impl(b, fs...);
}
};
template
struct easingresolve {
typedef typename std::tuple_element::type ArgType;
static void impl(FunctionTuple &b, easing::linearEasing, Fs... fs) {
get(b) = easing::linear.run;
easingresolve::impl(b, fs...);
}
};
template
struct easingresolve {
typedef typename std::tuple_element::type ArgType;
static void impl(FunctionTuple &b, easing::defaultEasing, Fs... fs) {
get(b) = easing::def.run;
easingresolve::impl(b, fs...);
}
};
#define DECLARE_EASING_RESOLVE(__EASING_TYPE__) \
template \
struct easingresolve { \
typedef typename std::tuple_element::type ArgType; \
static void impl(FunctionTuple & b, decltype(easing::__EASING_TYPE__ ## In), Fs... fs) { \
get(b) = easing::__EASING_TYPE__ ## In.run; \
easingresolve::impl(b, fs...); \
} \
}; \
\
template \
struct easingresolve { \
typedef typename std::tuple_element::type ArgType; \
static void impl(FunctionTuple & b, decltype(easing::__EASING_TYPE__ ## Out), Fs... fs) { \
get(b) = easing::__EASING_TYPE__ ## Out.run; \
easingresolve::impl(b, fs...); \
} \
}; \
\
template \
struct easingresolve { \
typedef typename std::tuple_element::type ArgType; \
static void impl(FunctionTuple & b, decltype(easing::__EASING_TYPE__ ## InOut), Fs... fs) { \
get(b) = easing::__EASING_TYPE__ ## InOut.run; \
easingresolve::impl(b, fs...); \
} \
}
DECLARE_EASING_RESOLVE(quadratic);
DECLARE_EASING_RESOLVE(cubic);
DECLARE_EASING_RESOLVE(quartic);
DECLARE_EASING_RESOLVE(quintic);
DECLARE_EASING_RESOLVE(sinusoidal);
DECLARE_EASING_RESOLVE(exponential);
DECLARE_EASING_RESOLVE(circular);
DECLARE_EASING_RESOLVE(bounce);
DECLARE_EASING_RESOLVE(elastic);
DECLARE_EASING_RESOLVE(back);
}
}
#endif //TWEENY_EASINGRESOLVE_H
/*
This file is part of the Tweeny library.
Copyright (c) 2016-2021 Leonardo Guilherme 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.
*/
/*
* This file declares a helper struct to create a type from a integer value, to aid in template tricks.
* This file is private.
*/
#ifndef TWEENY_INT2TYPE_H
#define TWEENY_INT2TYPE_H
namespace tweeny {
namespace detail {
template struct int2type { };
}
}
#endif //TWEENY_INT2TYPE_H
namespace tweeny {
namespace detail {
template void easingfill(EasingCollectionT & f, EasingT easing, int2type) {
easingresolve::impl(f, easing);
easingfill(f, easing, int2type{ });
}
template void easingfill(EasingCollectionT & f, EasingT easing, int2type<0>) {
easingresolve<0, TypeTupleT, EasingCollectionT, EasingT>::impl(f, easing);
}
template
struct are_same;
template
struct are_same
{
static const bool value = std::is_same::value && are_same::value;
};
template
struct are_same
{
static const bool value = true;
};
template
inline tweenpoint::tweenpoint(Ts... vs) : values{vs...} {
during(static_cast(0));
via(easing::def);
}
template
template
inline void tweenpoint::during(D milis) {
for (uint16_t & t : durations) { t = static_cast(milis); }
}
template
template
inline void tweenpoint::during(Ds... milis) {
static_assert(sizeof...(Ds) == sizeof...(Ts),
"Amount of durations should be equal to the amount of values in a point");
std::array list = {{ milis... }};
std::copy(list.begin(), list.end(), durations.begin());
}
template
template
inline void tweenpoint::via(Fs... fs) {
static_assert(sizeof...(Fs) == sizeof...(Ts),
"Number of functions passed to via() must be equal the number of values.");
detail::easingresolve<0, std::tuple, typename traits::easingCollection, Fs...>::impl(easings, fs...);
}
template
template
inline void tweenpoint::via(F f) {
easingfill(easings, f, int2type{ });
}
template
inline uint16_t tweenpoint::duration() const {
return *std::max_element(durations.begin(), durations.end());
}
template
inline uint16_t tweenpoint::duration(size_t i) const {
return durations.at(i);
}
}
}
#endif //TWEENY_TWEENPOINT_TCC
#endif //TWEENY_TWEENPOINT_H
namespace tweeny {
/**
* @brief The tween class is the core class of tweeny. It controls the interpolation steps, easings and durations.
*
* It should not be constructed manually but rather from @p tweeny::from, to facilitate template argument
* deduction (and also to keep your code clean).
*/
template
class tween {
public:
/**
* @brief Instantiates a tween from a starting point.
*
* This is a static factory helper function to be used by @p tweeny::from. You should not use this directly.
* @p t The first value in the point
* @p vs The remaining values
*/
static tween from(T t, Ts... vs);
public:
/**
* @brief Default constructor for a tween
*
* This constructor is provided to facilitate the usage of containers of tweens (e.g, std::vector). It
* should not be used manually as the tweening created by it is invalid.
*/
tween();
/**
* @brief Adds a new point in this tweening.
*
* This will add a new tweening point with the specified values. Next calls to @p via and @p during
* will refer to this point.
*
* **Example**
*
* @code
* auto t = tweeny::from(0).to(100).to(200);
* @endcode
*
* @param t, vs Point values
* @returns *this
*/
tween & to(T t, Ts... vs);
/**
* @brief Specifies the easing function for the last added point.
*
* This will specify the easing between the last tween point added by @p to and its previous step. You can
* use any callable object. Additionally, you can use the easing objects specified in the class @p easing.
*
* If it is a multi-value point, you can either specify a single easing function that will be used for
* every value or you can specify an easing function for each value. You can mix and match callable objects,
* lambdas and bundled easing objects.
*
* **Example**:
*
* @code
* // use bundled linear easing
* auto tween1 = tweeny::from(0).to(100).via(tweeny::easing::linear);
*
* // use custom lambda easing
* auto tween2 = tweeny::from(0).to(100).via([](float p, int a, int b) { return (b-a) * p + a; });
* @endcode
*
* @param fs The functions
* @returns *this
* @see tweeny::easing
*/
template tween & via(Fs... fs);
/**
* @brief Specifies the easing function for the last added point, accepting an enumeration.
*
* This will specify the easing between the last tween point added by @p to and its previous step. You can
* use a value from the @p tweeny::easing::enumerated enum. You can then have an enumeration of your own
* poiting to this enumerated enums, or use it directly. You can mix-and-match enumerated easings, functions
* and easing names.
*
* **Example**:
*
* @code
* auto tween1 = tweeny::from(0).to(100).via(tweeny::easing::enumerated::linear);
* auto tween2 = tweeny::from(0.0f, 100.0f).to(100.0f, 0.0f).via(tweeny::easing::linear, "backOut");
*
* @param fs The functions
* @returns *this
* @see tweeny::easing
*/
template tween & via(easing::enumerated enumerated, Fs... fs);
/**
* @brief Specifies the easing function for the last added point, accepting an easing name as a `std::string` value.
*
* This will specify the easing between the last tween point added by @p to and its previous step.
* You can mix-and-match enumerated easings, functions and easing names.
*
* **Example**:
*
* @code
* auto tween = tweeny::from(0.0f, 100.0f).to(100.0f, 0.0f).via(tweeny::easing::linear, "backOut");
*
* @param fs The functions
* @returns *this
* @see tweeny::easing
*/
template tween & via(const std::string & easing, Fs... fs);
/**
* @brief Specifies the easing function for the last added point, accepting an easing name as a `const char *` value.
*
* This will specify the easing between the last tween point added by @p to and its previous step.
* You can mix-and-match enumerated easings, functions and easing names.
*
* **Example**:
*
* @code
* auto tween = tweeny::from(0.0f, 100.0f).to(100.0f, 0.0f).via(tweeny::easing::linear, "backOut");
*
* @param fs The functions
* @returns *this
* @see tweeny::easing
*/
template tween & via(const char * easing, Fs... fs);
/**
* @brief Specifies the easing function for a specific point.
*
* Points starts at index 0. The index 0 refers to the first @p to call.
* Using this function without adding a point with @p to leads to undefined
* behaviour.
*
* @param index The tween point index
* @param fs The functions
* @returns *this
* @see tweeny::easing
*/
template tween & via(int index, Fs... fs);
/**
* @brief Specifies the duration, typically in milliseconds, for the tweening of values in last point.
*
* You can either specify a single duration for all values or give every value its own duration. Value types
* must be convertible to the uint16_t type.
*
* **Example**:
*
* @code
* // Specify that the first point will be reached in 100 milliseconds and the first value in the second
* // point in 100, whereas the second value will be reached in 500.
* auto tween = tweeny::from(0, 0).to(100, 200).during(100).to(200, 300).during(100, 500);
* @endcode
*
* @param ds Duration values
* @returns *this
*/
template tween & during(Ds... ds);
/**
* @brief Steps the animation by the designated delta amount.
*
* You should call this every frame of your application, passing in the amount of delta time that
* you want to animate.
*
* **Example**:
*
* @code
* // tween duration is 100ms
* auto tween = tweeny::from(0).to(100).during(100);
*
* // steps for 16ms
* tween.step(16);
* @endcode
*
* @param dt Delta duration
* @param suppressCallbacks (Optional) Suppress callbacks registered with tween::onStep()
* @returns std::tuple with the current tween values.
*/
const typename detail::tweentraits::valuesType & step(int32_t dt, bool suppressCallbacks = false);
/**
* @brief Steps the animation by the designated delta amount.
*
* You should call this every frame of your application, passing in the amount of delta time that
* you want to animate. This overload exists to match unsigned int arguments.
*
* @param dt Delta duration
* @param suppressCallbacks (Optional) Suppress callbacks registered with tween::onStep()
* @returns std::tuple with the current tween values.
*/
const typename detail::tweentraits::valuesType & step(uint32_t dt, bool suppressCallbacks = false);
/**
* @brief Steps the animation by the designated percentage amount.
*
* You can use this function to step the tweening by a specified percentage delta.
* **Example**:
*
* @code
* // tween duration is 100ms
* auto tween = tweeny::from(0).to(100).during(100);
*
* // steps for 16ms
* tween.step(0.001f);
* @endcode
*
* @param dp Delta percentage, between `0.0f` and `1.0f`
* @param suppressCallbacks (Optional) Suppress callbacks registered with tween::onStep()
* @returns std::tuple with the current tween values.
*/
const typename detail::tweentraits::valuesType & step(float dp, bool suppressCallbacks = false);
/**
* @brief Seeks to a specified point in time based on the currentProgress.
*
* This function sets the current animation time and currentProgress. Callbacks set by @p call will be triggered.
*
* @param p The percentage to seek to, between 0.0f and 1.0f, inclusive.
* @param suppressCallbacks (Optional) Suppress callbacks registered with tween::onSeek()
* @returns std::tuple with the current tween values.
*/
const typename detail::tweentraits::valuesType & seek(float p, bool suppressCallbacks = false);
/**
* @brief Seeks to a specified point in time.
*
* This function sets the current animation time and currentProgress. Callbacks set by @p call will be triggered.
*
* @param d The duration to seek to, between 0 and the total duration.
* @param suppressCallbacks (Optional) Suppress callbacks registered with tween::onSeek()
* @returns std::tuple with the current tween values.
* @see duration
*/
const typename detail::tweentraits