1
2 /*
3 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10
11 #include "FlingState.h"
12 #include "SkMatrix.h"
13 #include "SkTime.h"
14
15 #define DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER true
16
17 static const float MAX_FLING_SPEED = 1500;
18
pin_max_fling(float speed)19 static float pin_max_fling(float speed) {
20 if (speed > MAX_FLING_SPEED) {
21 speed = MAX_FLING_SPEED;
22 }
23 return speed;
24 }
25
getseconds()26 static double getseconds() {
27 return SkTime::GetMSecs() * 0.001;
28 }
29
30 // returns +1 or -1, depending on the sign of x
31 // returns +1 if x is zero
SkScalarSign(SkScalar x)32 static SkScalar SkScalarSign(SkScalar x) {
33 SkScalar sign = SK_Scalar1;
34 if (x < 0) {
35 sign = -sign;
36 }
37 return sign;
38 }
39
unit_axis_align(SkVector * unit)40 static void unit_axis_align(SkVector* unit) {
41 const SkScalar TOLERANCE = SkDoubleToScalar(0.15);
42 if (SkScalarAbs(unit->fX) < TOLERANCE) {
43 unit->fX = 0;
44 unit->fY = SkScalarSign(unit->fY);
45 } else if (SkScalarAbs(unit->fY) < TOLERANCE) {
46 unit->fX = SkScalarSign(unit->fX);
47 unit->fY = 0;
48 }
49 }
50
reset(float sx,float sy)51 void FlingState::reset(float sx, float sy) {
52 fActive = true;
53 fDirection.set(sx, sy);
54 fSpeed0 = SkPoint::Normalize(&fDirection);
55 fSpeed0 = pin_max_fling(fSpeed0);
56 fTime0 = getseconds();
57
58 unit_axis_align(&fDirection);
59 // printf("---- speed %g dir %g %g\n", fSpeed0, fDirection.fX, fDirection.fY);
60 }
61
evaluateMatrix(SkMatrix * matrix)62 bool FlingState::evaluateMatrix(SkMatrix* matrix) {
63 if (!fActive) {
64 return false;
65 }
66
67 const float t = getseconds() - fTime0;
68 const float MIN_SPEED = 2;
69 const float K0 = 5.0;
70 const float K1 = 0.02;
71 const float speed = fSpeed0 * (sk_float_exp(- K0 * t) - K1);
72 if (speed <= MIN_SPEED) {
73 fActive = false;
74 return false;
75 }
76 float dist = (fSpeed0 - speed) / K0;
77
78 // printf("---- time %g speed %g dist %g\n", t, speed, dist);
79 float tx = fDirection.fX * dist;
80 float ty = fDirection.fY * dist;
81 if (DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER) {
82 tx = sk_float_round2int(tx);
83 ty = sk_float_round2int(ty);
84 }
85 matrix->setTranslate(tx, ty);
86 // printf("---- evaluate (%g %g)\n", tx, ty);
87
88 return true;
89 }
90
91 ////////////////////////////////////////
92
GrAnimateFloat()93 GrAnimateFloat::GrAnimateFloat() : fTime0(0) {}
94
start(float v0,float v1,float duration)95 void GrAnimateFloat::start(float v0, float v1, float duration) {
96 fValue0 = v0;
97 fValue1 = v1;
98 fDuration = duration;
99 if (duration > 0) {
100 fTime0 = SkTime::GetMSecs();
101 if (!fTime0) {
102 fTime0 = 1; // time0 is our sentinel
103 }
104 } else {
105 fTime0 = 0;
106 }
107 }
108
evaluate()109 float GrAnimateFloat::evaluate() {
110 if (!fTime0) {
111 return fValue1;
112 }
113
114 double elapsed = (SkTime::GetMSecs() - fTime0) * 0.001;
115 if (elapsed >= fDuration) {
116 fTime0 = 0;
117 return fValue1;
118 }
119
120 double t = elapsed / fDuration;
121 if (true) {
122 t = (3 - 2 * t) * t * t;
123 }
124 return fValue0 + t * (fValue1 - fValue0);
125 }
126
127
128