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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "gfxeasing.h"
#include <math.h>
#include <QHash>
#include <QPainter>
#include <QVariant>
#include <QDebug>
#include <QStringList>
#include <QMouseEvent>
#include "gfxtimeline.h"
QT_BEGIN_NAMESPACE
typedef QHash<QString, qreal> GfxEasingProperties;
class GfxEasingFunction
{
public:
virtual ~GfxEasingFunction() {}
virtual float value(float t, float b, float c, float d) = 0;
virtual GfxEasingFunction *copy() const = 0;
};
#include "../3rdparty/easing.cpp"
struct ElasticEase : public GfxEasingFunction
{
enum Type { In, Out };
ElasticEase(Type t) : _t(t), _p(0.0f), _a(0.0f) {}
Type _t;
qreal _p;
qreal _a;
GfxEasingFunction *copy() const
{
ElasticEase *rv = new ElasticEase(_t);
rv->_p = _p;
rv->_a = _a;
return rv;
}
float value(float t, float b, float c, float d)
{
if (t==0) return b;
float t_adj = (float)t / (float)d;
if (t_adj==1) return b+c;
qreal p = _p?_p:(d * 0.3f);
qreal a;
qreal s;
if(!_a || _a < ::fabs(c)) {
a = c;
s = p / 4.0f;
} else {
a = _a;
s = p / (2 * M_PI) * ::asin(c / a);
}
if(_t == In)
t_adj -= 1.0f;
return (a*::pow(2,-10*t_adj) * ::sin( (t_adj*d-s)*(2*M_PI)/p ) + c + b);
}
};
struct BounceEase : public GfxEasingFunction
{
enum Type { In, Out };
BounceEase(Type t) : _t(t), _a(-1.0) {}
Type _t;
qreal _a;
GfxEasingFunction *copy() const
{
BounceEase *rv = new BounceEase(_t);
rv->_t = _t;
rv->_a = _a;
return rv;
}
float value(float t, float b, float c, float d)
{
if(In == _t)
return c - bounce(d - t, 0, c, d) + b;
else
return bounce(t, b, c, d);
}
float bounce(float t, float b, float c, float d)
{
float t_adj = (float)t / (float)d;
float amp = (_a == -1.0)?c:_a;
if ((t_adj) < (1/2.75)) {
if(c == 0. && _a != -1.0) {
t_adj -= (0.5f/2.75f);
return -amp * (1. - (30.25*t_adj*t_adj)) + b;
} else {
return c*(7.5625*t_adj*t_adj) + b;
}
} else if (t_adj < (2/2.75)) {
t_adj -= (1.5f/2.75f);
return -amp * (1. - (7.5625*t_adj*t_adj + .75)) + (b + c);
} else if (t_adj < (2.5/2.75)) {
t_adj -= (2.25f/2.75f);
return -amp * (1. - (7.5625*t_adj*t_adj + .9375)) + (b + c);
} else {
t_adj -= (2.65f/2.75f);
return -amp * (1. - (7.5625*t_adj*t_adj + .984375)) + (b + c);
}
}
};
static GfxEasingFunction *easeInElasticC(const GfxEasingProperties &p)
{
ElasticEase *rv = new ElasticEase(ElasticEase::In);
rv->_p = p[QLatin1String("period")];
rv->_a = p[QLatin1String("amplitude")];
return rv;
}
static GfxEasingFunction *easeOutElasticC(const GfxEasingProperties &p)
{
ElasticEase *rv = new ElasticEase(ElasticEase::Out);
rv->_p = p[QLatin1String("period")];
rv->_a = p[QLatin1String("amplitude")];
return rv;
}
static GfxEasingFunction *easeOutBounceC(const GfxEasingProperties &p)
{
BounceEase *rv = new BounceEase(BounceEase::Out);
rv->_a = p[QLatin1String("amplitude")];
return rv;
}
static GfxEasingFunction *easeInBounceC(const GfxEasingProperties &p)
{
BounceEase *rv = new BounceEase(BounceEase::Out);
rv->_a = p[QLatin1String("amplitude")];
return rv;
}
struct SimpleConfig : public GfxEasingFunction
{
GfxEasing::Function func;
float value(float t, float b, float c, float d)
{
return func(t, b, c, d);
}
GfxEasingFunction *copy() const
{
SimpleConfig *rv = new SimpleConfig;
rv->func = func;
return rv;
}
};
GfxEasing::Function curveToFunc(GfxEasing::Curve curve)
{
switch(curve)
{
case GfxEasing::None:
return &easeNone;
case GfxEasing::InQuad:
return &easeInQuad;
case GfxEasing::OutQuad:
return &easeOutQuad;
case GfxEasing::InOutQuad:
return &easeInOutQuad;
case GfxEasing::OutInQuad:
return &easeOutInQuad;
case GfxEasing::InCubic:
return &easeInCubic;
case GfxEasing::OutCubic:
return &easeOutCubic;
case GfxEasing::InOutCubic:
return &easeInOutCubic;
case GfxEasing::OutInCubic:
return &easeOutInCubic;
case GfxEasing::InQuart:
return &easeInQuart;
case GfxEasing::OutQuart:
return &easeOutQuart;
case GfxEasing::InOutQuart:
return &easeInOutQuart;
case GfxEasing::OutInQuart:
return &easeOutInQuart;
case GfxEasing::InQuint:
return &easeInQuint;
case GfxEasing::OutQuint:
return &easeOutQuint;
case GfxEasing::InOutQuint:
return &easeInOutQuint;
case GfxEasing::OutInQuint:
return &easeOutInQuint;
case GfxEasing::InSine:
return &easeInSine;
case GfxEasing::OutSine:
return &easeOutSine;
case GfxEasing::InOutSine:
return &easeInOutSine;
case GfxEasing::OutInSine:
return &easeOutInSine;
case GfxEasing::InExpo:
return &easeInExpo;
case GfxEasing::OutExpo:
return &easeOutExpo;
case GfxEasing::InOutExpo:
return &easeInOutExpo;
case GfxEasing::OutInExpo:
return &easeOutInExpo;
case GfxEasing::InCirc:
return &easeInCirc;
case GfxEasing::OutCirc:
return &easeOutCirc;
case GfxEasing::InOutCirc:
return &easeInOutCirc;
case GfxEasing::OutInCirc:
return &easeOutInCirc;
case GfxEasing::InElastic:
return &easeInElastic;
case GfxEasing::OutElastic:
return &easeOutElastic;
case GfxEasing::InOutElastic:
return &easeInOutElastic;
case GfxEasing::OutInElastic:
return &easeOutInElastic;
case GfxEasing::InBack:
return &easeInBack;
case GfxEasing::OutBack:
return &easeOutBack;
case GfxEasing::InOutBack:
return &easeInOutBack;
case GfxEasing::OutInBack:
return &easeOutInBack;
case GfxEasing::InBounce:
return &easeInBounce;
case GfxEasing::OutBounce:
return &easeOutBounce;
case GfxEasing::InOutBounce:
return &easeInOutBounce;
case GfxEasing::OutInBounce:
return &easeOutInBounce;
default:
return 0;
};
}
struct NameFunctionMap : public QHash<QString, GfxEasing::Function>
{
NameFunctionMap()
{
insert(QLatin1String("easeNone"), easeNone);
insert(QLatin1String("easeInQuad"), easeInQuad);
insert(QLatin1String("easeOutQuad"), easeOutQuad);
insert(QLatin1String("easeInOutQuad"), easeInOutQuad);
insert(QLatin1String("easeOutInQuad"), easeOutInQuad);
insert(QLatin1String("easeInCubic"), easeInCubic);
insert(QLatin1String("easeOutCubic"), easeOutCubic);
insert(QLatin1String("easeInOutCubic"), easeInOutCubic);
insert(QLatin1String("easeOutInCubic"), easeOutInCubic);
insert(QLatin1String("easeInQuart"), easeInQuart);
insert(QLatin1String("easeOutQuart"), easeOutQuart);
insert(QLatin1String("easeInOutQuart"), easeInOutQuart);
insert(QLatin1String("easeOutInQuart"), easeOutInQuart);
insert(QLatin1String("easeInQuint"), easeInQuint);
insert(QLatin1String("easeOutQuint"), easeOutQuint);
insert(QLatin1String("easeInOutQuint"), easeInOutQuint);
insert(QLatin1String("easeOutInQuint"), easeOutInQuint);
insert(QLatin1String("easeInSine"), easeInSine);
insert(QLatin1String("easeOutSine"), easeOutSine);
insert(QLatin1String("easeInOutSine"), easeInOutSine);
insert(QLatin1String("easeOutInSine"), easeOutInSine);
insert(QLatin1String("easeInExpo"), easeInExpo);
insert(QLatin1String("easeOutExpo"), easeOutExpo);
insert(QLatin1String("easeInOutExpo"), easeInOutExpo);
insert(QLatin1String("easeOutInExpo"), easeOutInExpo);
insert(QLatin1String("easeInCirc"), easeInCirc);
insert(QLatin1String("easeOutCirc"), easeOutCirc);
insert(QLatin1String("easeInOutCirc"), easeInOutCirc);
insert(QLatin1String("easeOutInCirc"), easeOutInCirc);
insert(QLatin1String("easeInElastic"), easeInElastic);
insert(QLatin1String("easeOutElastic"), easeOutElastic);
insert(QLatin1String("easeInOutElastic"), easeInOutElastic);
insert(QLatin1String("easeOutInElastic"), easeOutInElastic);
insert(QLatin1String("easeInBack"), easeInBack);
insert(QLatin1String("easeOutBack"), easeOutBack);
insert(QLatin1String("easeInOutBack"), easeInOutBack);
insert(QLatin1String("easeOutInBack"), easeOutInBack);
insert(QLatin1String("easeInBounce"), easeInBounce);
insert(QLatin1String("easeOutBounce"), easeOutBounce);
insert(QLatin1String("easeInOutBounce"), easeInOutBounce);
insert(QLatin1String("easeOutInBounce"), easeOutInBounce);
}
};
Q_GLOBAL_STATIC(NameFunctionMap, nameFunctionMap);
typedef GfxEasingFunction *(*ConfigurableFunction)(const GfxEasingProperties &);
struct ConfigFunctionMap : public QHash<QString, ConfigurableFunction>
{
ConfigFunctionMap()
{
insert(QLatin1String("easeInElastic"), easeInElasticC);
insert(QLatin1String("easeOutElastic"), easeOutElasticC);
insert(QLatin1String("easeInBounce"), easeInBounceC);
insert(QLatin1String("easeOutBounce"), easeOutBounceC);
}
};
Q_GLOBAL_STATIC(ConfigFunctionMap, configFunctionMap);
/*!
\class GfxEasing
\ingroup group_animation
\brief The GfxEasing class provides easing curves for controlling animation.
Easing curves describe a function that controls how a value changes over
time. Easing curves allow transitions from one value to another to appear
more natural than a simple linear motion would allow. The GfxEasing class
is usually used in conjunction with the GfxTimeLine class, but can be used
on its own.
To calculate the value at a given time, the easing curve function requires
the starting value, the final value and the total time to change from the
starting to the final value. When using the GfxEasing class with
GfxTimeLine, these values are supplied by the GfxTimeLine. When using
the GfxEasing class on its own, the programmer must specify them using
the GfxEasing::setFrom(), GfxEasing::setTo() and GfxEasing::setLength()
methods, or by passing them explicitly to the GfxEasing::valueAt() method.
For example,
\code
GfxEasing easing(GfxEasing::InOutQuad);
easing.setFrom(0);
easing.setTo(1000);
easing.setLength(1000);
for(int milliseconds = 0; milliseconds < 1000; ++milliseconds)
qWarning() << "Value at" << milliseconds << "milliseconds is
<< easing.valueAt(milliseconds);
\endcode
will print the value at each millisecond for an InOutQuad transition from
0 to 1000 over 1 second.
When using a GfxTimeLine, the values are communicated implicitly.
\code
GfxTimeLine timeline;
GfxValue value(0);
timeline.move(value, 1000, GfxEasing(GfxEasing::InOutQuad), 1000);
\endcode
In this case, any values set using the previous setter methods would be
ignored.
*/
/*!
\qmlproperty string NumericAnimation::easing
\brief the easing curve used for the transition.
Available values are:
\list
\i \e easeNone - Easing equation function for a simple linear tweening, with no easing.
\i \e easeInQuad - Easing equation function for a quadratic (t^2) easing in: accelerating from zero velocity.
\i \e easeOutQuad - Easing equation function for a quadratic (t^2) easing out: decelerating to zero velocity.
\i \e easeInOutQuad - Easing equation function for a quadratic (t^2) easing in/out: acceleration until halfway, then deceleration.
\i \e easeOutInQuad - Easing equation function for a quadratic (t^2) easing out/in: deceleration until halfway, then acceleration.
\i \e easeInCubic - Easing equation function for a cubic (t^3) easing in: accelerating from zero velocity.
\i \e easeOutCubic - Easing equation function for a cubic (t^3) easing out: decelerating from zero velocity.
\i \e easeInOutCubic - Easing equation function for a cubic (t^3) easing in/out: acceleration until halfway, then deceleration.
\i \e easeOutInCubic - Easing equation function for a cubic (t^3) easing out/in: deceleration until halfway, then acceleration.
\i \e easeInQuart - Easing equation function for a quartic (t^4) easing in: accelerating from zero velocity.
\i \e easeOutQuart - Easing equation function for a quartic (t^4) easing out: decelerating from zero velocity.
\i \e easeInOutQuart - Easing equation function for a quartic (t^4) easing in/out: acceleration until halfway, then deceleration.
\i \e easeOutInQuart - Easing equation function for a quartic (t^4) easing out/in: deceleration until halfway, then acceleration.
\i \e easeInQuint - Easing equation function for a quintic (t^5) easing in: accelerating from zero velocity.
\i \e easeOutQuint - Easing equation function for a quintic (t^5) easing out: decelerating from zero velocity.
\i \e easeInOutQuint - Easing equation function for a quintic (t^5) easing in/out: acceleration until halfway, then deceleration.
\i \e easeOutInQuint - Easing equation function for a quintic (t^5) easing out/in: deceleration until halfway, then acceleration.
\i \e easeInSine - Easing equation function for a sinusoidal (sin(t)) easing in: accelerating from zero velocity.
\i \e easeOutSine - Easing equation function for a sinusoidal (sin(t)) easing out: decelerating from zero velocity.
\i \e easeInOutSine - Easing equation function for a sinusoidal (sin(t)) easing in/out: acceleration until halfway, then deceleration.
\i \e easeOutInSine - Easing equation function for a sinusoidal (sin(t)) easing out/in: deceleration until halfway, then acceleration.
\i \e easeInExpo - Easing equation function for an exponential (2^t) easing in: accelerating from zero velocity.
\i \e easeOutExpo - Easing equation function for an exponential (2^t) easing out: decelerating from zero velocity.
\i \e easeInOutExpo - Easing equation function for an exponential (2^t) easing in/out: acceleration until halfway, then deceleration.
\i \e easeOutInExpo - Easing equation function for an exponential (2^t) easing out/in: deceleration until halfway, then acceleration.
\i \e easeInCirc - Easing equation function for a circular (sqrt(1-t^2)) easing in: accelerating from zero velocity.
\i \e easeOutCirc - Easing equation function for a circular (sqrt(1-t^2)) easing out: decelerating from zero velocity.
\i \e easeInOutCirc - Easing equation function for a circular (sqrt(1-t^2)) easing in/out: acceleration until halfway, then deceleration.
\i \e easeOutInCirc - Easing equation function for a circular (sqrt(1-t^2)) easing out/in: deceleration until halfway, then acceleration.
\i \e easeInElastic - Easing equation function for an elastic (exponentially decaying sine wave) easing in: accelerating from zero velocity. The peak amplitude can be set with the \i amplitude parameter, and the period of decay by the \i period parameter.
\i \e easeOutElastic - Easing equation function for an elastic (exponentially decaying sine wave) easing out: decelerating from zero velocity. The peak amplitude can be set with the \i amplitude parameter, and the period of decay by the \i period parameter.
\i \e easeInOutElastic - Easing equation function for an elastic (exponentially decaying sine wave) easing in/out: acceleration until halfway, then deceleration.
\i \e easeOutInElastic - Easing equation function for an elastic (exponentially decaying sine wave) easing out/in: deceleration until halfway, then acceleration.
\i \e easeInBack - Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in: accelerating from zero velocity.
\i \e easeOutBack - Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out: decelerating from zero velocity.
\i \e easeInOutBack - Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out: acceleration until halfway, then deceleration.
\i \e easeOutInBack - Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in: deceleration until halfway, then acceleration.
\i \e easeOutBounce - Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out: decelerating from zero velocity.
\i \e easeInBounce - Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in: accelerating from zero velocity.
\i \e easeInOutBounce - Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in/out: acceleration until halfway, then deceleration.
\i \e easeOutInBounce - Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out/in: deceleration until halfway, then acceleration.
\endlist
*/
/*!
\enum GfxEasing::Curve
The type of easing curve.
\value None
Easing equation function for a simple linear tweening, with no easing.
\value InQuad
Easing equation function for a quadratic (t^2) easing in: accelerating from zero velocity.
\value OutQuad
Easing equation function for a quadratic (t^2) easing out: decelerating to zero velocity.
\value InOutQuad
Easing equation function for a quadratic (t^2) easing in/out: acceleration until halfway, then deceleration.
\value OutInQuad
Easing equation function for a quadratic (t^2) easing out/in: deceleration until halfway, then acceleration.
\value InCubic
Easing equation function for a cubic (t^3) easing in: accelerating from zero velocity.
\value OutCubic
Easing equation function for a cubic (t^3) easing out: decelerating from zero velocity.
\value InOutCubic
Easing equation function for a cubic (t^3) easing in/out: acceleration until halfway, then deceleration.
\value OutInCubic
Easing equation function for a cubic (t^3) easing out/in: deceleration until halfway, then acceleration.
\value InQuart
Easing equation function for a quartic (t^4) easing in: accelerating from zero velocity.
\value OutQuart
Easing equation function for a quartic (t^4) easing out: decelerating from zero velocity.
\value InOutQuart
Easing equation function for a quartic (t^4) easing in/out: acceleration until halfway, then deceleration.
\value OutInQuart
Easing equation function for a quartic (t^4) easing out/in: deceleration until halfway, then acceleration.
\value InQuint
Easing equation function for a quintic (t^5) easing in: accelerating from zero velocity.
\value OutQuint
Easing equation function for a quintic (t^5) easing out: decelerating from zero velocity.
\value InOutQuint
Easing equation function for a quintic (t^5) easing in/out: acceleration until halfway, then deceleration.
\value OutInQuint
Easing equation function for a quintic (t^5) easing out/in: deceleration until halfway, then acceleration.
\value InSine
Easing equation function for a sinusoidal (sin(t)) easing in: accelerating from zero velocity.
\value OutSine
Easing equation function for a sinusoidal (sin(t)) easing out: decelerating from zero velocity.
\value InOutSine
Easing equation function for a sinusoidal (sin(t)) easing in/out: acceleration until halfway, then deceleration.
\value OutInSine
Easing equation function for a sinusoidal (sin(t)) easing out/in: deceleration until halfway, then acceleration.
\value InExpo
Easing equation function for an exponential (2^t) easing in: accelerating from zero velocity.
\value OutExpo
Easing equation function for an exponential (2^t) easing out: decelerating from zero velocity.
\value InOutExpo
Easing equation function for an exponential (2^t) easing in/out: acceleration until halfway, then deceleration.
\value OutInExpo
Easing equation function for an exponential (2^t) easing out/in: deceleration until halfway, then acceleration.
\value InCirc
Easing equation function for a circular (sqrt(1-t^2)) easing in: accelerating from zero velocity.
\value OutCirc
Easing equation function for a circular (sqrt(1-t^2)) easing out: decelerating from zero velocity.
\value InOutCirc
Easing equation function for a circular (sqrt(1-t^2)) easing in/out: acceleration until halfway, then deceleration.
\value OutInCirc
Easing equation function for a circular (sqrt(1-t^2)) easing out/in: deceleration until halfway, then acceleration.
\value InElastic
Easing equation function for an elastic (exponentially decaying
sine wave) easing in: accelerating from zero velocity. The peak
amplitude can be set with the \e amplitude parameter, and the
period of decay by the \e period parameter.
\value OutElastic
Easing equation function for an elastic (exponentially decaying
sine wave) easing out: decelerating from zero velocity. The peak
amplitude can be set with the \e amplitude parameter, and the
period of decay by the \e period parameter.
\value InOutElastic
Easing equation function for an elastic (exponentially decaying sine wave) easing in/out: acceleration until halfway, then deceleration.
\value OutInElastic
Easing equation function for an elastic (exponentially decaying sine wave) easing out/in: deceleration until halfway, then acceleration.
\value InBack
Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in: accelerating from zero velocity.
\value OutBack
Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out: decelerating from zero velocity.
\value InOutBack
Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out: acceleration until halfway, then deceleration.
\value OutInBack
Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in: deceleration until halfway, then acceleration.
\value OutBounce
Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out: decelerating from zero velocity.
\value InBounce
Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in: accelerating from zero velocity.
\value InOutBounce
Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in/out: acceleration until halfway, then deceleration.
\value OutInBounce
Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out/in: deceleration until halfway, then acceleration.
*/
/*!
Construct a linear easing object. This is equivalent to \c {GfxEasing(GfxEasing::None)}.
*/
GfxEasing::GfxEasing()
: _config(0), _func(&easeNone), _b(0.), _c(1.), _d(1.)
{
}
/*!
Construct an easing object with the given \a curve.
*/
GfxEasing::GfxEasing(Curve curve)
: _config(0), _func(0), _b(0.), _c(1.), _d(1.)
{
_func = curveToFunc(curve);
if(!_func) {
qWarning("GfxEasing: Invalid curve type %d", curve);
_func = &easeNone;
}
}
/*!
Construct an easing object with the given \a curve. If \a curve does not
describe a legal curve, a linear easing object is constructed.
Curve names have the form
\c {ease<CurveName>[(<arg>: <arg value>[, <arg2>: <arg value>])]}. The
\e CurveName is equivalent to the GfxEasing::Curve enum name. Some more
advanced curves can take arguments to further refine their behaviour. Where
applicable, these parameters are described in the corresponding
GfxEasing::Curve value documentation.
For example,
\code
GfxEasing easing("easeInOutQuad");
GfxEasing easing2("easeInElastic(period: 5, amplitude: 100)");
\endcode
*/
GfxEasing::GfxEasing(const QString &curve)
: _config(0), _func(&easeNone), _b(0.), _c(1.), _d(1.)
{
if(curve.contains(QLatin1Char('('))) {
QString easeName = curve.trimmed();
GfxEasingProperties prop;
if(!easeName.endsWith(QLatin1Char(')'))) {
qWarning("GfxEasing: Unmatched perenthesis in easing function '%s'",
curve.toLatin1().constData());
return;
}
int idx = easeName.indexOf(QLatin1Char('('));
QString prop_str =
easeName.mid(idx + 1, easeName.length() - 1 - idx - 1);
easeName = easeName.left(idx);
QStringList props = prop_str.split(QLatin1Char(','));
foreach(QString str, props) {
int sep = str.indexOf(QLatin1Char(':'));
if(sep == -1) {
qWarning("GfxEasing: Improperly specified property in easing function '%s'",
curve.toLatin1().constData());
return;
}
QString propName = str.left(sep).trimmed();
bool isOk;
qreal propValue = str.mid(sep + 1).trimmed().toDouble(&isOk);
if(propName.isEmpty() || !isOk) {
qWarning("GfxEasing: Improperly specified property in easing function '%s'",
curve.toLatin1().constData());
return;
}
prop.insert(propName, propValue);
}
QHash<QString, ConfigurableFunction>::Iterator iter =
configFunctionMap()->find(easeName);
if(iter != configFunctionMap()->end())
_config = (*iter)(prop);
} else {
if(nameFunctionMap()->contains(curve))
_func = *(nameFunctionMap()->find(curve));
}
if(!_func && !_config) {
qWarning("GfxEasing: Unknown easing curve '%s'",
curve.toLatin1().constData());
_func = &easeNone;
}
}
/*!
Construct a copy of \a other.
*/
GfxEasing::GfxEasing(const GfxEasing &other)
: _config(0), _func(other._func), _b(other._b), _c(other._c), _d(other._d)
{
if(other._config) _config = other._config->copy();
}
/*!
Copy \a other.
*/
GfxEasing &GfxEasing::operator=(const GfxEasing &other)
{
if(_config) { delete _config; _config = 0; }
if(other._config) _config = other._config->copy();
_func = other._func;
_b = other._b;
_c = other._c;
_d = other._d;
return *this;
}
/*!
Returns true if this is a linear easing object.
*/
bool GfxEasing::isLinear() const
{
return !_config && _func == &easeNone;
}
/*!
Return the starting value for the easing curve. By default this is 0.
*/
qreal GfxEasing::from() const
{
return _b;
}
/*!
Set the starting value for the easing curve to \a from.
*/
void GfxEasing::setFrom(qreal from)
{
_b = from;
}
/*!
Return the final value for the easing curve. By default this is 0.
*/
qreal GfxEasing::to() const
{
return _c;
}
/*!
Set the final value for the easing curve to \a to.
*/
void GfxEasing::setTo(qreal to)
{
_c = to;
}
/*!
Return the length of the easing curve, in milliseconds. By default this
is 1.
*/
qreal GfxEasing::length() const
{
return _d;
}
/*!
Set the \a length of the easing curve, in milliseconds.
*/
void GfxEasing::setLength(qreal length)
{
Q_ASSERT(length > 0);
_d = length;
}
/*!
Return the value for the easing curve at time \a t milliseconds, based on
the parameters returned by GfxEasing::from(), GfxEasing::to() and
GfxEasing::length().
\a t is clamped to (0, GfxEasing::length()).
*/
qreal GfxEasing::valueAt(qreal t) const
{
if(t < 0) t = 0;
else if(t > length()) t = length();
if(_config)
return _config->value(t, _b, _c - _b, _d);
else
return _func(t, _b, _c - _b, _d);
}
/*
Return the value for the easing curve at time \a t milliseconds, based on
the provided parameters \a from, \a to and \a length. The values returned
from the object's GfxEasing::from(), GfxEasing::to() and GfxEasing::length()
methods are ignored.
\a t is clamped to (0, \a length).
*/
qreal GfxEasing::valueAt(qreal t, qreal from, qreal to, qreal length) const
{
if(t < 0) t = 0;
else if(t > length) t = length;
if(_config)
return _config->value(t, from, to - from, length);
else
return _func(t, from, to - from, length);
}
/*!
\internal
*/
QStringList GfxEasing::curves()
{
return nameFunctionMap()->keys();
}
QT_END_NAMESPACE
|