[email protected] | 31a8245 | 2012-07-13 00:54:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | ffb15d1 | 2013-09-15 17:29:30 | [diff] [blame] | 5 | #include "ui/gfx/animation/slide_animation.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | a5b94a9 | 2008-08-12 23:25:43 | [diff] [blame] | 7 | #include <math.h> |
8 | |||||
Peter Kasting | d67d6bd8 | 2019-05-03 02:31:02 | [diff] [blame] | 9 | #include "base/numerics/ranges.h" |
sangwoo.ko | 2e7bf39 | 2019-01-19 05:14:53 | [diff] [blame] | 10 | #include "ui/gfx/animation/animation_delegate.h" |
11 | |||||
[email protected] | ffb15d1 | 2013-09-15 17:29:30 | [diff] [blame] | 12 | namespace gfx { |
[email protected] | f6767806 | 2011-01-07 17:33:39 | [diff] [blame] | 13 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | SlideAnimation::SlideAnimation(AnimationDelegate* target) |
Peter Kasting | d67d6bd8 | 2019-05-03 02:31:02 | [diff] [blame] | 15 | : LinearAnimation(target), target_(target) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | |
Peter Kasting | d67d6bd8 | 2019-05-03 02:31:02 | [diff] [blame] | 17 | SlideAnimation::~SlideAnimation() = default; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
19 | void SlideAnimation::Reset(double value) { | ||||
Peter Kasting | d67d6bd8 | 2019-05-03 02:31:02 | [diff] [blame] | 20 | showing_ = value == 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | value_current_ = value; |
Peter Kasting | d67d6bd8 | 2019-05-03 02:31:02 | [diff] [blame] | 22 | Stop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | } |
24 | |||||
25 | void SlideAnimation::Show() { | ||||
Peter Kasting | d67d6bd8 | 2019-05-03 02:31:02 | [diff] [blame] | 26 | BeginAnimating(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | } |
28 | |||||
29 | void SlideAnimation::Hide() { | ||||
Peter Kasting | d67d6bd8 | 2019-05-03 02:31:02 | [diff] [blame] | 30 | BeginAnimating(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | } |
32 | |||||
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 33 | void SlideAnimation::SetSlideDuration(int duration) { |
34 | slide_duration_ = duration; | ||||
35 | } | ||||
36 | |||||
Alex Newcomer | 7119776 | 2017-10-17 18:42:59 | [diff] [blame] | 37 | void SlideAnimation::SetDampeningValue(double dampening_value) { |
38 | dampening_value_ = dampening_value; | ||||
39 | } | ||||
40 | |||||
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 41 | double SlideAnimation::GetCurrentValue() const { |
42 | return value_current_; | ||||
43 | } | ||||
44 | |||||
Alex Newcomer | 7119776 | 2017-10-17 18:42:59 | [diff] [blame] | 45 | base::TimeDelta SlideAnimation::GetDuration() { |
46 | const double current_progress = | ||||
47 | showing_ ? value_current_ : 1.0 - value_current_; | ||||
48 | |||||
49 | return base::TimeDelta::FromMillisecondsD( | ||||
50 | slide_duration_ * (1 - pow(current_progress, dampening_value_))); | ||||
51 | } | ||||
52 | |||||
Peter Kasting | d67d6bd8 | 2019-05-03 02:31:02 | [diff] [blame] | 53 | void SlideAnimation::BeginAnimating(bool showing) { |
54 | if (showing_ == showing) | ||||
55 | return; | ||||
56 | |||||
57 | showing_ = showing; | ||||
58 | value_start_ = value_current_; | ||||
59 | value_end_ = showing ? 1.0 : 0.0; | ||||
60 | |||||
61 | // Make sure we actually have something to do. | ||||
62 | if (slide_duration_ == 0) { | ||||
63 | AnimateToState(1.0); // Skip to the end of the animation. | ||||
64 | if (delegate()) { | ||||
65 | delegate()->AnimationProgressed(this); | ||||
66 | delegate()->AnimationEnded(this); | ||||
67 | } | ||||
68 | } else if (value_current_ != value_end_) { | ||||
69 | // This will also reset the currently-occurring animation. | ||||
70 | SetDuration(GetDuration()); | ||||
71 | Start(); | ||||
72 | } | ||||
73 | } | ||||
74 | |||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | void SlideAnimation::AnimateToState(double state) { |
Peter Kasting | d67d6bd8 | 2019-05-03 02:31:02 | [diff] [blame] | 76 | state = |
77 | Tween::CalculateValue(tween_type_, base::ClampToRange(state, 0.0, 1.0)); | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 78 | value_current_ = value_start_ + (value_end_ - value_start_) * state; |
79 | |||||
80 | // Implement snapping. | ||||
[email protected] | 4ce7e15 | 2010-05-05 03:43:55 | [diff] [blame] | 81 | if (tween_type_ == Tween::EASE_OUT_SNAP && |
82 | fabs(value_current_ - value_end_) <= 0.06) | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | value_current_ = value_end_; |
84 | |||||
85 | // Correct for any overshoot (while state may be capped at 1.0, let's not | ||||
86 | // take any rounding error chances. | ||||
Peter Kasting | d67d6bd8 | 2019-05-03 02:31:02 | [diff] [blame] | 87 | if ((value_end_ >= value_start_) ? (value_current_ > value_end_) |
88 | : (value_current_ < value_end_)) { | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | value_current_ = value_end_; |
90 | } | ||||
91 | } | ||||
[email protected] | f6767806 | 2011-01-07 17:33:39 | [diff] [blame] | 92 | |
[email protected] | ffb15d1 | 2013-09-15 17:29:30 | [diff] [blame] | 93 | } // namespace gfx |