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 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
|
/*
Disclaimer for Robert Penner's Easing Equations license:
TERMS OF USE - EASING EQUATIONS
Open source under the BSD License.
Copyright © 2001 Robert Penner
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <QtCore/qmath.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_PI_2
#define M_PI_2 (M_PI / 2)
#endif
QT_USE_NAMESPACE
/**
* Easing equation function for a simple linear tweening, with no easing.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeNone(qreal progress)
{
return progress;
}
/**
* Easing equation function for a quadratic (t^2) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInQuad(qreal t)
{
return t*t;
}
/**
* Easing equation function for a quadratic (t^2) easing out: decelerating to zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutQuad(qreal t)
{
return -t*(t-2);
}
/**
* Easing equation function for a quadratic (t^2) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInOutQuad(qreal t)
{
t*=2.0;
if (t < 1) {
return t*t/qreal(2);
} else {
--t;
return -0.5 * (t*(t-2) - 1);
}
}
/**
* Easing equation function for a quadratic (t^2) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutInQuad(qreal t)
{
if (t < 0.5) return easeOutQuad (t*2)/2;
return easeInQuad((2*t)-1)/2 + 0.5;
}
/**
* Easing equation function for a cubic (t^3) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInCubic(qreal t)
{
return t*t*t;
}
/**
* Easing equation function for a cubic (t^3) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutCubic(qreal t)
{
t-=1.0;
return t*t*t + 1;
}
/**
* Easing equation function for a cubic (t^3) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInOutCubic(qreal t)
{
t*=2.0;
if(t < 1) {
return 0.5*t*t*t;
} else {
t -= qreal(2.0);
return 0.5*(t*t*t + 2);
}
}
/**
* Easing equation function for a cubic (t^3) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutInCubic(qreal t)
{
if (t < 0.5) return easeOutCubic (2*t)/2;
return easeInCubic(2*t - 1)/2 + 0.5;
}
/**
* Easing equation function for a quartic (t^4) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInQuart(qreal t)
{
return t*t*t*t;
}
/**
* Easing equation function for a quartic (t^4) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutQuart(qreal t)
{
t-= qreal(1.0);
return - (t*t*t*t- 1);
}
/**
* Easing equation function for a quartic (t^4) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInOutQuart(qreal t)
{
t*=2;
if (t < 1) return 0.5*t*t*t*t;
else {
t -= 2.0f;
return -0.5 * (t*t*t*t- 2);
}
}
/**
* Easing equation function for a quartic (t^4) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutInQuart(qreal t)
{
if (t < 0.5) return easeOutQuart (2*t)/2;
return easeInQuart(2*t-1)/2 + 0.5;
}
/**
* Easing equation function for a quintic (t^5) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInQuint(qreal t)
{
return t*t*t*t*t;
}
/**
* Easing equation function for a quintic (t^5) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutQuint(qreal t)
{
t-=1.0;
return t*t*t*t*t + 1;
}
/**
* Easing equation function for a quintic (t^5) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInOutQuint(qreal t)
{
t*=2.0;
if (t < 1) return 0.5*t*t*t*t*t;
else {
t -= 2.0;
return 0.5*(t*t*t*t*t + 2);
}
}
/**
* Easing equation function for a quintic (t^5) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutInQuint(qreal t)
{
if (t < 0.5) return easeOutQuint (2*t)/2;
return easeInQuint(2*t - 1)/2 + 0.5;
}
/**
* Easing equation function for a sinusoidal (sin(t)) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInSine(qreal t)
{
return (t == 1.0) ? 1.0 : -::qCos(t * M_PI_2) + 1.0;
}
/**
* Easing equation function for a sinusoidal (sin(t)) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutSine(qreal t)
{
return ::qSin(t* M_PI_2);
}
/**
* Easing equation function for a sinusoidal (sin(t)) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInOutSine(qreal t)
{
return -0.5 * (::qCos(M_PI*t) - 1);
}
/**
* Easing equation function for a sinusoidal (sin(t)) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutInSine(qreal t)
{
if (t < 0.5) return easeOutSine (2*t)/2;
return easeInSine(2*t - 1)/2 + 0.5;
}
/**
* Easing equation function for an exponential (2^t) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInExpo(qreal t)
{
return (t==0 || t == 1.0) ? t : ::qPow(2.0, 10 * (t - 1)) - qreal(0.001);
}
/**
* Easing equation function for an exponential (2^t) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutExpo(qreal t)
{
return (t==1.0) ? 1.0 : 1.001 * (-::qPow(2.0f, -10 * t) + 1);
}
/**
* Easing equation function for an exponential (2^t) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInOutExpo(qreal t)
{
if (t==0.0) return qreal(0.0);
if (t==1.0) return qreal(1.0);
t*=2.0;
if (t < 1) return 0.5 * ::qPow(qreal(2.0), 10 * (t - 1)) - 0.0005;
return 0.5 * 1.0005 * (-::qPow(qreal(2.0), -10 * (t - 1)) + 2);
}
/**
* Easing equation function for an exponential (2^t) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutInExpo(qreal t)
{
if (t < 0.5) return easeOutExpo (2*t)/2;
return easeInExpo(2*t - 1)/2 + 0.5;
}
/**
* Easing equation function for a circular (sqrt(1-t^2)) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInCirc(qreal t)
{
return -(::sqrt(1 - t*t) - 1);
}
/**
* Easing equation function for a circular (sqrt(1-t^2)) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutCirc(qreal t)
{
t-= qreal(1.0);
return ::sqrt(1 - t* t);
}
/**
* Easing equation function for a circular (sqrt(1-t^2)) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeInOutCirc(qreal t)
{
t*=qreal(2.0);
if (t < 1) {
return -0.5 * (::sqrt(1 - t*t) - 1);
} else {
t -= qreal(2.0);
return 0.5 * (::sqrt(1 - t*t) + 1);
}
}
/**
* Easing equation function for a circular (sqrt(1-t^2)) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @return The correct value.
*/
static qreal easeOutInCirc(qreal t)
{
if (t < 0.5) return easeOutCirc (2*t)/2;
return easeInCirc(2*t - 1)/2 + 0.5;
}
static qreal easeInElastic_helper(qreal t, qreal b, qreal c, qreal d, qreal a, qreal p)
{
if (t==0) return b;
qreal t_adj = (qreal)t / (qreal)d;
if (t_adj==1) return b+c;
qreal s;
if(a < ::qFabs(c)) {
a = c;
s = p / 4.0f;
} else {
s = p / (2 * M_PI) * ::qAsin(c / a);
}
t_adj -= 1.0f;
return -(a*::qPow(2.0f,10*t_adj) * ::qSin( (t_adj*d-s)*(2*M_PI)/p )) + b;
}
/**
* Easing equation function for an elastic (exponentially decaying sine wave) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param a Amplitude.
* @param p Period.
* @return The correct value.
*/
static qreal easeInElastic(qreal t, qreal a, qreal p)
{
return easeInElastic_helper(t, 0, 1, 1, a, p);
}
static qreal easeOutElastic_helper(qreal t, qreal /*b*/, qreal c, qreal /*d*/, qreal a, qreal p)
{
if (t==0) return 0;
if (t==1) return c;
qreal s;
if(a < c) {
a = c;
s = p / 4.0f;
} else {
s = p / (2 * M_PI) * ::qAsin(c / a);
}
return (a*::qPow(2.0f,-10*t) * ::qSin( (t-s)*(2*M_PI)/p ) + c);
}
/**
* Easing equation function for an elastic (exponentially decaying sine wave) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param a Amplitude.
* @param p Period.
* @return The correct value.
*/
static qreal easeOutElastic(qreal t, qreal a, qreal p)
{
return easeOutElastic_helper(t, 0, 1, 1, a, p);
}
/**
* Easing equation function for an elastic (exponentially decaying sine wave) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param a Amplitude.
* @param p Period.
* @return The correct value.
*/
static qreal easeInOutElastic(qreal t, qreal a, qreal p)
{
if (t==0) return 0.0;
t*=2.0;
if (t==2) return 1.0;
qreal s;
if(a < 1.0) {
a = 1.0;
s = p / 4.0f;
} else {
s = p / (2 * M_PI) * ::qAsin(1.0 / a);
}
if (t < 1) return -.5*(a*::qPow(2.0f,10*(t-1)) * ::qSin( (t-1-s)*(2*M_PI)/p ));
return a*::qPow(2.0f,-10*(t-1)) * ::qSin( (t-1-s)*(2*M_PI)/p )*.5 + 1.0;
}
/**
* Easing equation function for an elastic (exponentially decaying sine wave) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param a Amplitude.
* @param p Period.
* @return The correct value.
*/
static qreal easeOutInElastic(qreal t, qreal a, qreal p)
{
if (t < 0.5) return easeOutElastic_helper(t*2, 0, 0.5, 1.0, a, p);
return easeInElastic_helper(2*t - 1.0, 0.5, 0.5, 1.0, a, p);
}
/**
* Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
* @return The correct value.
*/
static qreal easeInBack(qreal t, qreal s)
{
return t*t*((s+1)*t - s);
}
/**
* Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
* @return The correct value.
*/
static qreal easeOutBack(qreal t, qreal s)
{
t-= qreal(1.0);
return t*t*((s+1)*t+ s) + 1;
}
/**
* Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
* @return The correct value.
*/
static qreal easeInOutBack(qreal t, qreal s)
{
t *= 2.0;
if (t < 1) {
s *= 1.525f;
return 0.5*(t*t*((s+1)*t - s));
} else {
t -= 2;
s *= 1.525f;
return 0.5*(t*t*((s+1)*t+ s) + 2);
}
}
/**
* Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
* @return The correct value.
*/
static qreal easeOutInBack(qreal t, qreal s)
{
if (t < 0.5) return easeOutBack (2*t, s)/2;
return easeInBack(2*t - 1, s)/2 + 0.5;
}
static qreal easeOutBounce_helper(qreal t, qreal c, qreal a)
{
if (t == 1.0) return c;
if (t < (4/11.0)) {
return c*(7.5625*t*t);
} else if (t < (8/11.0)) {
t -= (6/11.0);
return -a * (1. - (7.5625*t*t + .75)) + c;
} else if (t < (10/11.0)) {
t -= (9/11.0);
return -a * (1. - (7.5625*t*t + .9375)) + c;
} else {
t -= (21/22.0);
return -a * (1. - (7.5625*t*t + .984375)) + c;
}
}
/**
* Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out: decelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param a Amplitude.
* @return The correct value.
*/
static qreal easeOutBounce(qreal t, qreal a)
{
return easeOutBounce_helper(t, 1, a);
}
/**
* Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in: accelerating from zero velocity.
*
* @param t Current time (in frames or seconds).
* @param a Amplitude.
* @return The correct value.
*/
static qreal easeInBounce(qreal t, qreal a)
{
return 1.0 - easeOutBounce_helper(1.0-t, 1.0, a);
}
/**
* Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in/out: acceleration until halfway, then deceleration.
*
* @param t Current time (in frames or seconds).
* @param a Amplitude.
* @return The correct value.
*/
static qreal easeInOutBounce(qreal t, qreal a)
{
if (t < 0.5) return easeInBounce (2*t, a)/2;
else return (t == 1.0) ? 1.0 : easeOutBounce (2*t - 1, a)/2 + 0.5;
}
/**
* Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out/in: deceleration until halfway, then acceleration.
*
* @param t Current time (in frames or seconds).
* @param a Amplitude.
* @return The correct value.
*/
static qreal easeOutInBounce(qreal t, qreal a)
{
if (t < 0.5) return easeOutBounce_helper(t*2, 0.5, a);
return 1.0 - easeOutBounce_helper (2.0-2*t, 0.5, a);
}
static inline qreal qt_sinProgress(qreal value)
{
return qSin((value * M_PI) - M_PI_2) / 2 + qreal(0.5);
}
static inline qreal qt_smoothBeginEndMixFactor(qreal value)
{
return qMin(qMax(1 - value * 2 + qreal(0.3), qreal(0.0)), qreal(1.0));
}
// SmoothBegin blends Smooth and Linear Interpolation.
// Progress 0 - 0.3 : Smooth only
// Progress 0.3 - ~ 0.5 : Mix of Smooth and Linear
// Progress ~ 0.5 - 1 : Linear only
/**
* Easing function that starts growing slowly, then increases in speed. At the end of the curve the speed will be constant.
*/
static qreal easeInCurve(qreal t)
{
const qreal sinProgress = qt_sinProgress(t);
const qreal mix = qt_smoothBeginEndMixFactor(t);
return sinProgress * mix + t * (1 - mix);
}
/**
* Easing function that starts growing steadily, then ends slowly. The speed will be constant at the beginning of the curve.
*/
static qreal easeOutCurve(qreal t)
{
const qreal sinProgress = qt_sinProgress(t);
const qreal mix = qt_smoothBeginEndMixFactor(1 - t);
return sinProgress * mix + t * (1 - mix);
}
/**
* Easing function where the value grows sinusoidally. Note that the calculated end value will be 0 rather than 1.
*/
static qreal easeSineCurve(qreal t)
{
return (qSin(((t * M_PI * 2)) - M_PI_2) + 1) / 2;
}
/**
* Easing function where the value grows cosinusoidally. Note that the calculated start value will be 0.5 and the end value will be 0.5
* contrary to the usual 0 to 1 easing curve.
*/
static qreal easeCosineCurve(qreal t)
{
return (qCos(((t * M_PI * 2)) - M_PI_2) + 1) / 2;
}
|