blob: cb634cc9765f306a589f0f2c1d6dd3d3de56ec3e [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#include "FlingState.h"
19#include "SkMatrix.h"
20#include "SkTime.h"
21
22#define DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER true
23
24static const float MAX_FLING_SPEED = 1500;
25
26static float pin_max_fling(float speed) {
27 if (speed > MAX_FLING_SPEED) {
28 speed = MAX_FLING_SPEED;
29 }
30 return speed;
31}
32
33static double getseconds() {
34 return SkTime::GetMSecs() * 0.001;
35}
36
37// returns +1 or -1, depending on the sign of x
38// returns +1 if x is zero
39static SkScalar SkScalarSign(SkScalar x) {
40 SkScalar sign = SK_Scalar1;
41 if (x < 0) {
42 sign = -sign;
43 }
44 return sign;
45}
46
47static void unit_axis_align(SkVector* unit) {
48 const SkScalar TOLERANCE = SkDoubleToScalar(0.15);
49 if (SkScalarAbs(unit->fX) < TOLERANCE) {
50 unit->fX = 0;
51 unit->fY = SkScalarSign(unit->fY);
52 } else if (SkScalarAbs(unit->fY) < TOLERANCE) {
53 unit->fX = SkScalarSign(unit->fX);
54 unit->fY = 0;
55 }
56}
57
58void FlingState::reset(float sx, float sy) {
59 fActive = true;
60 fDirection.set(sx, sy);
61 fSpeed0 = SkPoint::Normalize(&fDirection);
62 fSpeed0 = pin_max_fling(fSpeed0);
63 fTime0 = getseconds();
64
65 unit_axis_align(&fDirection);
66// printf("---- speed %g dir %g %g\n", fSpeed0, fDirection.fX, fDirection.fY);
67}
68
69bool FlingState::evaluateMatrix(SkMatrix* matrix) {
70 if (!fActive) {
71 return false;
72 }
73
74 const float t = getseconds() - fTime0;
75 const float MIN_SPEED = 2;
76 const float K0 = 5.0;
77 const float K1 = 0.02;
78 const float speed = fSpeed0 * (sk_float_exp(- K0 * t) - K1);
79 if (speed <= MIN_SPEED) {
80 fActive = false;
81 return false;
82 }
83 float dist = (fSpeed0 - speed) / K0;
84
85// printf("---- time %g speed %g dist %g\n", t, speed, dist);
86 float tx = fDirection.fX * dist;
87 float ty = fDirection.fY * dist;
88 if (DISCRETIZE_TRANSLATE_TO_AVOID_FLICKER) {
89 tx = sk_float_round2int(tx);
90 ty = sk_float_round2int(ty);
91 }
92 matrix->setTranslate(tx, ty);
93// printf("---- evaluate (%g %g)\n", tx, ty);
94
95 return true;
96}
97
98////////////////////////////////////////
99
100GrAnimateFloat::GrAnimateFloat() : fTime0(0) {}
101
102void GrAnimateFloat::start(float v0, float v1, float duration) {
103 fValue0 = v0;
104 fValue1 = v1;
105 fDuration = duration;
106 if (duration > 0) {
107 fTime0 = SkTime::GetMSecs();
108 if (!fTime0) {
109 fTime0 = 1; // time0 is our sentinel
110 }
111 } else {
112 fTime0 = 0;
113 }
114}
115
116float GrAnimateFloat::evaluate() {
117 if (!fTime0) {
118 return fValue1;
119 }
120
121 double elapsed = (SkTime::GetMSecs() - fTime0) * 0.001;
122 if (elapsed >= fDuration) {
123 fTime0 = 0;
124 return fValue1;
125 }
126
127 double t = elapsed / fDuration;
128 if (true) {
129 t = (3 - 2 * t) * t * t;
130 }
131 return fValue0 + t * (fValue1 - fValue0);
132}
133
134