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/slide_animation.h" | |
6 | |
7 #include <math.h> | |
8 | |
9 // How many frames per second to target. | |
10 static const int kDefaultFramerateHz = 50; | |
11 | |
12 // How long animations should take by default. | |
13 static const int kDefaultDurationMs = 120; | |
14 | |
15 SlideAnimation::SlideAnimation(AnimationDelegate* target) | |
16 : LinearAnimation(kDefaultFramerateHz, target), | |
17 target_(target), | |
18 tween_type_(Tween::EASE_OUT), | |
19 showing_(false), | |
20 value_start_(0), | |
21 value_end_(0), | |
22 value_current_(0), | |
23 slide_duration_(kDefaultDurationMs) { | |
24 } | |
25 | |
26 SlideAnimation::~SlideAnimation() { | |
27 } | |
28 | |
29 void SlideAnimation::Reset() { | |
30 Reset(0); | |
31 } | |
32 | |
33 void SlideAnimation::Reset(double value) { | |
34 Stop(); | |
35 showing_ = static_cast<bool>(value == 1); | |
36 value_current_ = value; | |
37 } | |
38 | |
39 void SlideAnimation::Show() { | |
40 // If we're already showing (or fully shown), we have nothing to do. | |
41 if (showing_) | |
42 return; | |
43 | |
44 showing_ = true; | |
45 value_start_ = value_current_; | |
46 value_end_ = 1.0; | |
47 | |
48 // Make sure we actually have something to do. | |
49 if (slide_duration_ == 0) { | |
50 AnimateToState(1.0); // Skip to the end of the animation. | |
51 return; | |
52 } else if (value_current_ == value_end_) { | |
53 return; | |
54 } | |
55 | |
56 // This will also reset the currently-occuring animation. | |
57 SetDuration(static_cast<int>(slide_duration_ * (1 - value_current_))); | |
58 Start(); | |
59 } | |
60 | |
61 void SlideAnimation::Hide() { | |
62 // If we're already hiding (or hidden), we have nothing to do. | |
63 if (!showing_) | |
64 return; | |
65 | |
66 showing_ = false; | |
67 value_start_ = value_current_; | |
68 value_end_ = 0.0; | |
69 | |
70 // Make sure we actually have something to do. | |
71 if (slide_duration_ == 0) { | |
72 AnimateToState(0.0); // Skip to the end of the animation. | |
73 return; | |
74 } else if (value_current_ == value_end_) { | |
75 return; | |
76 } | |
77 | |
78 // This will also reset the currently-occuring animation. | |
79 SetDuration(static_cast<int>(slide_duration_ * value_current_)); | |
80 Start(); | |
81 } | |
82 | |
83 void SlideAnimation::SetSlideDuration(int duration) { | |
84 slide_duration_ = duration; | |
85 } | |
86 | |
87 double SlideAnimation::GetCurrentValue() const { | |
88 return value_current_; | |
89 } | |
90 | |
91 void SlideAnimation::AnimateToState(double state) { | |
92 if (state > 1.0) | |
93 state = 1.0; | |
94 | |
95 state = Tween::CalculateValue(tween_type_, state); | |
96 | |
97 value_current_ = value_start_ + (value_end_ - value_start_) * state; | |
98 | |
99 // Implement snapping. | |
100 if (tween_type_ == Tween::EASE_OUT_SNAP && | |
101 fabs(value_current_ - value_end_) <= 0.06) | |
102 value_current_ = value_end_; | |
103 | |
104 // Correct for any overshoot (while state may be capped at 1.0, let's not | |
105 // take any rounding error chances. | |
106 if ((value_end_ >= value_start_ && value_current_ > value_end_) || | |
107 (value_end_ < value_start_ && value_current_ < value_end_)) { | |
108 value_current_ = value_end_; | |
109 } | |
110 } | |
OLD | NEW |