File: easing.h

package info (click to toggle)
tweeny 3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 356 kB
  • sloc: cpp: 406; xml: 182; ansic: 171; makefile: 6
file content (562 lines) | stat: -rw-r--r-- 23,847 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 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.
*/

/**
 * @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 <cmath>
#include <type_traits>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

/**
    * @defgroup easings Easings
    * @brief Bundled easing functions based on
    *        <a href="http://robertpenner.com/easing/">Robert Penner's Easing Functions</a>
    * @details You should plug these functions into @ref tweeny::tween::via function to specify the easing used in a tween.
    * @sa tweeny::easing
    * @{
    *//**
    *   @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 currentPoint.
     *
     * **Example**:
     *
     * @code
     * auto tween = tweeny::from(0).to(100).via(tweeny::easing::linear);
     * @endcode
     */
    class easing {
        public:
            /**
             * @ingroup linear
             * @brief Values change with constant speed.
             */
            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{};

             /**
              * @ingroup quadratic
              * @brief Accelerate initial values with a quadratic equation.
              */
            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{};

            /**
              * @ingroup quadratic
              * @brief Deaccelerate ending values with a quadratic equation.
              */
            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{};

            /**
              * @ingroup quadratic
              * @brief Acceelerate initial and deaccelerate ending values with a quadratic equation.
              */
            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{};

            /**
              * @ingroup cubic
              * @brief Aaccelerate initial values with a cubic equation.
              */
            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{};

            /**
              * @ingroup cubic
              * @brief Deaccelerate ending values with a cubic equation.
              */
            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{};

            /**
              * @ingroup cubic
              * @brief Acceelerate initial and deaccelerate ending values with a cubic equation.
              */
            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{};

            /**
              * @ingroup quartic
              * @brief Acceelerate initial values with a quartic equation.
              */
            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{};

            /**
              * @ingroup quartic
              * @brief Deaccelerate ending values with a quartic equation.
              */
            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{};

            /**
              * @ingroup quartic
              * @brief Acceelerate initial and deaccelerate ending values with a quartic equation.
              */
            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{};

            /**
              * @ingroup quintic
              * @brief Acceelerate initial values with a quintic equation.
              */
            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{};

            /**
              * @ingroup quintic
              * @brief Deaccelerate ending values with a quintic equation.
              */
            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{};

            /**
              * @ingroup quintic
              * @brief Acceelerate initial and deaccelerate ending values with a quintic equation.
              */
            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{};

            /**
              * @ingroup sinusoidal
              * @brief Acceelerate initial values with a sinusoidal equation.
              */
            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{};

            /**
              * @ingroup sinusoidal
              * @brief Deaccelerate ending values with a sinusoidal equation.
              */
            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{};

            /**
              * @ingroup sinusoidal
              * @brief Acceelerate initial and deaccelerate ending values with a sinusoidal equation.
              */
            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{};

            /**
              * @ingroup exponential
              * @brief Acceelerate initial values with an exponential equation.
              */
            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{};

            /**
              * @ingroup exponential
              * @brief Deaccelerate ending values with an exponential equation.
              */
            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{};

            /**
              * @ingroup exponential
              * @brief Acceelerate initial and deaccelerate ending values with an exponential equation.
              */
            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{};

            /**
              * @ingroup circular
              * @brief Acceelerate initial values with a circular equation.
              */
            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{};

            /**
              * @ingroup circular
              * @brief Deaccelerate ending values with a circular equation.
              */
            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{};

            /**
              * @ingroup circular
              * @brief Acceelerate initial and deaccelerate ending values with a circular equation.
              */
            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{};

            /**
              * @ingroup bounce
              * @brief Acceelerate initial values with a "bounce" equation.
              */
            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{};

            /**
              * @ingroup bounce
              * @brief Deaccelerate ending values with a "bounce" equation.
              */
            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{};

            /**
            * @ingroup bounce
            * @brief Acceelerate initial and deaccelerate ending values with a "bounce" equation.
            */
            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{};

            /**
              * @ingroup elastic
              * @brief Acceelerate initial values with an "elastic" equation.
              */
            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{};

            /**
              * @ingroup elastic
              * @brief Deaccelerate ending values with an "elastic" equation.
              */
            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{};

            /**
            * @ingroup elastic
            * @brief Acceelerate initial and deaccelerate ending values with an "elastic" equation.
            */
            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{};

            /**
              * @ingroup back
              * @brief Acceelerate initial values with a "back" equation.
              */
            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{};

            /**
              * @ingroup back
              * @brief Deaccelerate ending values with a "back" equation.
              */
            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{};

            /**
            * @ingroup back
            * @brief Acceelerate initial and deaccelerate ending values with a "back" equation.
            */
            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