| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "app/slide_animation.h" | 5 #include "app/slide_animation.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 // How many frames per second to target. | 9 // How many frames per second to target. |
| 10 static const int kDefaultFramerateHz = 50; | 10 static const int kDefaultFramerateHz = 50; |
| 11 | 11 |
| 12 // How long animations should take by default. | 12 // How long animations should take by default. |
| 13 static const int kDefaultDurationMs = 120; | 13 static const int kDefaultDurationMs = 120; |
| 14 | 14 |
| 15 SlideAnimation::SlideAnimation(AnimationDelegate* target) | 15 SlideAnimation::SlideAnimation(AnimationDelegate* target) |
| 16 : Animation(kDefaultFramerateHz, target), | 16 : LinearAnimation(kDefaultFramerateHz, target), |
| 17 target_(target), | 17 target_(target), |
| 18 tween_type_(EASE_OUT), | 18 tween_type_(Tween::EASE_OUT), |
| 19 showing_(false), | 19 showing_(false), |
| 20 value_start_(0), | 20 value_start_(0), |
| 21 value_end_(0), | 21 value_end_(0), |
| 22 value_current_(0), | 22 value_current_(0), |
| 23 slide_duration_(kDefaultDurationMs) { | 23 slide_duration_(kDefaultDurationMs) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 SlideAnimation::~SlideAnimation() { | 26 SlideAnimation::~SlideAnimation() { |
| 27 } | 27 } |
| 28 | 28 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 | 77 |
| 78 // This will also reset the currently-occuring animation. | 78 // This will also reset the currently-occuring animation. |
| 79 SetDuration(static_cast<int>(slide_duration_ * value_current_)); | 79 SetDuration(static_cast<int>(slide_duration_ * value_current_)); |
| 80 Start(); | 80 Start(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void SlideAnimation::AnimateToState(double state) { | 83 void SlideAnimation::AnimateToState(double state) { |
| 84 if (state > 1.0) | 84 if (state > 1.0) |
| 85 state = 1.0; | 85 state = 1.0; |
| 86 | 86 |
| 87 // Make our animation ease-out. | 87 state = Tween::CalculateValue(tween_type_, state); |
| 88 switch (tween_type_) { | |
| 89 case EASE_IN: | |
| 90 state = pow(state, 2); | |
| 91 break; | |
| 92 case EASE_IN_OUT: | |
| 93 if (state < 0.5) | |
| 94 state = pow(state * 2, 2) / 2.0; | |
| 95 else | |
| 96 state = 1.0 - (pow((state - 1.0) * 2, 2) / 2.0); | |
| 97 break; | |
| 98 case FAST_IN_OUT: | |
| 99 state = (pow(state - 0.5, 3) + 0.125) / 0.25; | |
| 100 break; | |
| 101 case NONE: | |
| 102 // state remains linear. | |
| 103 break; | |
| 104 case EASE_OUT_SNAP: | |
| 105 state = 0.95 * (1.0 - pow(1.0 - state, 2)); | |
| 106 break; | |
| 107 case EASE_OUT: | |
| 108 default: | |
| 109 state = 1.0 - pow(1.0 - state, 2); | |
| 110 break; | |
| 111 } | |
| 112 | 88 |
| 113 value_current_ = value_start_ + (value_end_ - value_start_) * state; | 89 value_current_ = value_start_ + (value_end_ - value_start_) * state; |
| 114 | 90 |
| 115 // Implement snapping. | 91 // Implement snapping. |
| 116 if (tween_type_ == EASE_OUT_SNAP && fabs(value_current_ - value_end_) <= 0.06) | 92 if (tween_type_ == Tween::EASE_OUT_SNAP && |
| 93 fabs(value_current_ - value_end_) <= 0.06) |
| 117 value_current_ = value_end_; | 94 value_current_ = value_end_; |
| 118 | 95 |
| 119 // Correct for any overshoot (while state may be capped at 1.0, let's not | 96 // Correct for any overshoot (while state may be capped at 1.0, let's not |
| 120 // take any rounding error chances. | 97 // take any rounding error chances. |
| 121 if ((value_end_ >= value_start_ && value_current_ > value_end_) || | 98 if ((value_end_ >= value_start_ && value_current_ > value_end_) || |
| 122 (value_end_ < value_start_ && value_current_ < value_end_)) { | 99 (value_end_ < value_start_ && value_current_ < value_end_)) { |
| 123 value_current_ = value_end_; | 100 value_current_ = value_end_; |
| 124 } | 101 } |
| 125 } | 102 } |
| OLD | NEW |