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 "gfx/rect.h" |
| 14 |
| 15 // static |
| 16 double Tween::CalculateValue(Tween::Type type, double state) { |
| 17 DCHECK_GE(state, 0); |
| 18 DCHECK_LE(state, 1); |
| 19 |
| 20 switch (type) { |
| 21 case EASE_IN: |
| 22 return pow(state, 2); |
| 23 |
| 24 case EASE_IN_OUT: |
| 25 if (state < 0.5) |
| 26 return pow(state * 2, 2) / 2.0; |
| 27 return 1.0 - (pow((state - 1.0) * 2, 2) / 2.0); |
| 28 |
| 29 case FAST_IN_OUT: |
| 30 return (pow(state - 0.5, 3) + 0.125) / 0.25; |
| 31 |
| 32 case LINEAR: |
| 33 return state; |
| 34 |
| 35 case EASE_OUT_SNAP: |
| 36 state = 0.95 * (1.0 - pow(1.0 - state, 2)); |
| 37 break; |
| 38 |
| 39 case EASE_OUT: |
| 40 return 1.0 - pow(1.0 - state, 2); |
| 41 |
| 42 case ZERO: |
| 43 return 0; |
| 44 } |
| 45 |
| 46 NOTREACHED(); |
| 47 return state; |
| 48 } |
| 49 |
| 50 // static |
| 51 double Tween::ValueBetween(double value, double start, double target) { |
| 52 return start + (target - start) * value; |
| 53 } |
| 54 |
| 55 // static |
| 56 int Tween::ValueBetween(double value, int start, int target) { |
| 57 if (start == target) |
| 58 return start; |
| 59 double delta = static_cast<double>(target - start); |
| 60 if (delta < 0) |
| 61 delta--; |
| 62 else |
| 63 delta++; |
| 64 #if defined(OS_WIN) |
| 65 return start + static_cast<int>(value * _nextafter(delta, 0)); |
| 66 #else |
| 67 return start + static_cast<int>(value * nextafter(delta, 0)); |
| 68 #endif |
| 69 } |
| 70 |
| 71 // static |
| 72 gfx::Rect Tween::ValueBetween(double value, |
| 73 const gfx::Rect& start_bounds, |
| 74 const gfx::Rect& target_bounds) { |
| 75 return gfx::Rect(ValueBetween(value, start_bounds.x(), target_bounds.x()), |
| 76 ValueBetween(value, start_bounds.y(), target_bounds.y()), |
| 77 ValueBetween(value, start_bounds.width(), |
| 78 target_bounds.width()), |
| 79 ValueBetween(value, start_bounds.height(), |
| 80 target_bounds.height())); |
| 81 } |
OLD | NEW |