1 // Copyright (c) 2012 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.
5 #include "ui/gfx/animation/slide_animation.h"
11 // How many frames per second to target.
12 static const int kDefaultFrameRateHz
= 60;
14 // How long animations should take by default.
15 static const int kDefaultDurationMs
= 120;
17 SlideAnimation::SlideAnimation(AnimationDelegate
* target
)
18 : LinearAnimation(kDefaultFrameRateHz
, target
),
20 tween_type_(Tween::EASE_OUT
),
25 slide_duration_(kDefaultDurationMs
) {
28 SlideAnimation::~SlideAnimation() {
31 void SlideAnimation::Reset() {
35 void SlideAnimation::Reset(double value
) {
37 showing_
= static_cast<bool>(value
== 1);
38 value_current_
= value
;
41 void SlideAnimation::Show() {
42 // If we're already showing (or fully shown), we have nothing to do.
47 value_start_
= value_current_
;
50 // Make sure we actually have something to do.
51 if (slide_duration_
== 0) {
52 AnimateToState(1.0); // Skip to the end of the animation.
54 } else if (value_current_
== value_end_
) {
58 // This will also reset the currently-occurring animation.
59 SetDuration(static_cast<int>(slide_duration_
* (1 - value_current_
)));
63 void SlideAnimation::Hide() {
64 // If we're already hiding (or hidden), we have nothing to do.
69 value_start_
= value_current_
;
72 // Make sure we actually have something to do.
73 if (slide_duration_
== 0) {
74 AnimateToState(0.0); // Skip to the end of the animation.
76 } else if (value_current_
== value_end_
) {
80 // This will also reset the currently-occurring animation.
81 SetDuration(static_cast<int>(slide_duration_
* value_current_
));
85 void SlideAnimation::SetSlideDuration(int duration
) {
86 slide_duration_
= duration
;
89 double SlideAnimation::GetCurrentValue() const {
90 return value_current_
;
93 void SlideAnimation::AnimateToState(double state
) {
97 state
= Tween::CalculateValue(tween_type_
, state
);
99 value_current_
= value_start_
+ (value_end_
- value_start_
) * state
;
101 // Implement snapping.
102 if (tween_type_
== Tween::EASE_OUT_SNAP
&&
103 fabs(value_current_
- value_end_
) <= 0.06)
104 value_current_
= value_end_
;
106 // Correct for any overshoot (while state may be capped at 1.0, let's not
107 // take any rounding error chances.
108 if ((value_end_
>= value_start_
&& value_current_
> value_end_
) ||
109 (value_end_
< value_start_
&& value_current_
< value_end_
)) {
110 value_current_
= value_end_
;