OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "app/tween.h" | |
6 | |
7 #include <math.h> | |
8 | |
9 #if defined(OS_WIN) | |
10 #include <float.h> | |
11 #endif | |
12 | |
13 #include "base/logging.h" | |
14 #include "gfx/rect.h" | |
15 | |
16 // static | |
17 double Tween::CalculateValue(Tween::Type type, double state) { | |
18 DCHECK_GE(state, 0); | |
19 DCHECK_LE(state, 1); | |
20 | |
21 switch (type) { | |
22 case EASE_IN: | |
23 return pow(state, 2); | |
24 | |
25 case EASE_IN_OUT: | |
26 if (state < 0.5) | |
27 return pow(state * 2, 2) / 2.0; | |
28 return 1.0 - (pow((state - 1.0) * 2, 2) / 2.0); | |
29 | |
30 case FAST_IN_OUT: | |
31 return (pow(state - 0.5, 3) + 0.125) / 0.25; | |
32 | |
33 case LINEAR: | |
34 return state; | |
35 | |
36 case EASE_OUT_SNAP: | |
37 state = 0.95 * (1.0 - pow(1.0 - state, 2)); | |
38 break; | |
39 | |
40 case EASE_OUT: | |
41 return 1.0 - pow(1.0 - state, 2); | |
42 | |
43 case ZERO: | |
44 return 0; | |
45 } | |
46 | |
47 NOTREACHED(); | |
48 return state; | |
49 } | |
50 | |
51 // static | |
52 double Tween::ValueBetween(double value, double start, double target) { | |
53 return start + (target - start) * value; | |
54 } | |
55 | |
56 // static | |
57 int Tween::ValueBetween(double value, int start, int target) { | |
58 if (start == target) | |
59 return start; | |
60 double delta = static_cast<double>(target - start); | |
61 if (delta < 0) | |
62 delta--; | |
63 else | |
64 delta++; | |
65 #if defined(OS_WIN) | |
66 return start + static_cast<int>(value * _nextafter(delta, 0)); | |
67 #else | |
68 return start + static_cast<int>(value * nextafter(delta, 0)); | |
69 #endif | |
70 } | |
71 | |
72 // static | |
73 gfx::Rect Tween::ValueBetween(double value, | |
74 const gfx::Rect& start_bounds, | |
75 const gfx::Rect& target_bounds) { | |
76 return gfx::Rect(ValueBetween(value, start_bounds.x(), target_bounds.x()), | |
77 ValueBetween(value, start_bounds.y(), target_bounds.y()), | |
78 ValueBetween(value, start_bounds.width(), | |
79 target_bounds.width()), | |
80 ValueBetween(value, start_bounds.height(), | |
81 target_bounds.height())); | |
82 } | |
OLD | NEW |