Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / animation / slide_animation.cc
blobd08ac41d9af33b7205a7e522f8b30139ba8d6c74
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"
7 #include <math.h>
9 namespace gfx {
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),
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) {
28 SlideAnimation::~SlideAnimation() {
31 void SlideAnimation::Reset() {
32 Reset(0);
35 void SlideAnimation::Reset(double value) {
36 Stop();
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.
43 if (showing_)
44 return;
46 showing_ = true;
47 value_start_ = value_current_;
48 value_end_ = 1.0;
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;
58 // This will also reset the currently-occurring animation.
59 SetDuration(static_cast<int>(slide_duration_ * (1 - value_current_)));
60 Start();
63 void SlideAnimation::Hide() {
64 // If we're already hiding (or hidden), we have nothing to do.
65 if (!showing_)
66 return;
68 showing_ = false;
69 value_start_ = value_current_;
70 value_end_ = 0.0;
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;
80 // This will also reset the currently-occurring animation.
81 SetDuration(static_cast<int>(slide_duration_ * value_current_));
82 Start();
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) {
94 if (state > 1.0)
95 state = 1.0;
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_;
114 } // namespace gfx