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.
4
5 #include "ui/gfx/animation/slide_animation.h"
6
7 #include <math.h>
8
9 namespace gfx {
10
11 // How many frames per second to target.
12 static const int kDefaultFrameRateHz = 60;
13
14 // How long animations should take by default.
15 static const int kDefaultDurationMs = 120;
16
SlideAnimation(AnimationDelegate * target)17 SlideAnimation::SlideAnimation(AnimationDelegate* target)
18 : LinearAnimation(kDefaultFrameRateHz, target),
19 target_(target),
20 tween_type_(Tween::EASE_OUT),
21 showing_(false),
22 value_start_(0),
23 value_end_(0),
24 value_current_(0),
25 slide_duration_(kDefaultDurationMs) {
26 }
27
~SlideAnimation()28 SlideAnimation::~SlideAnimation() {
29 }
30
Reset()31 void SlideAnimation::Reset() {
32 Reset(0);
33 }
34
Reset(double value)35 void SlideAnimation::Reset(double value) {
36 Stop();
37 showing_ = static_cast<bool>(value == 1);
38 value_current_ = value;
39 }
40
Show()41 void SlideAnimation::Show() {
42 // If we're already showing (or fully shown), we have nothing to do.
43 if (showing_)
44 return;
45
46 showing_ = true;
47 value_start_ = value_current_;
48 value_end_ = 1.0;
49
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.
53 return;
54 } else if (value_current_ == value_end_) {
55 return;
56 }
57
58 // This will also reset the currently-occurring animation.
59 SetDuration(static_cast<int>(slide_duration_ * (1 - value_current_)));
60 Start();
61 }
62
Hide()63 void SlideAnimation::Hide() {
64 // If we're already hiding (or hidden), we have nothing to do.
65 if (!showing_)
66 return;
67
68 showing_ = false;
69 value_start_ = value_current_;
70 value_end_ = 0.0;
71
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.
75 return;
76 } else if (value_current_ == value_end_) {
77 return;
78 }
79
80 // This will also reset the currently-occurring animation.
81 SetDuration(static_cast<int>(slide_duration_ * value_current_));
82 Start();
83 }
84
SetSlideDuration(int duration)85 void SlideAnimation::SetSlideDuration(int duration) {
86 slide_duration_ = duration;
87 }
88
GetCurrentValue() const89 double SlideAnimation::GetCurrentValue() const {
90 return value_current_;
91 }
92
AnimateToState(double state)93 void SlideAnimation::AnimateToState(double state) {
94 if (state > 1.0)
95 state = 1.0;
96
97 state = Tween::CalculateValue(tween_type_, state);
98
99 value_current_ = value_start_ + (value_end_ - value_start_) * state;
100
101 // Implement snapping.
102 if (tween_type_ == Tween::EASE_OUT_SNAP &&
103 fabs(value_current_ - value_end_) <= 0.06)
104 value_current_ = value_end_;
105
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_;
111 }
112 }
113
114 } // namespace gfx
115