James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 | #ifndef UI_GFX_ANIMATION_SLIDE_ANIMATION_H_ |
| 6 | #define UI_GFX_ANIMATION_SLIDE_ANIMATION_H_ |
| 7 | |
| 8 | #include "ui/gfx/animation/linear_animation.h" |
| 9 | #include "ui/gfx/animation/tween.h" |
| 10 | |
| 11 | namespace gfx { |
| 12 | |
| 13 | // Slide Animation |
| 14 | // |
| 15 | // Used for reversible animations and as a general helper class. Typical usage: |
| 16 | // |
| 17 | // #include "ui/gfx/animation/slide_animation.h" |
| 18 | // |
| 19 | // class MyClass : public AnimationDelegate { |
| 20 | // public: |
| 21 | // MyClass() { |
| 22 | // animation_.reset(new SlideAnimation(this)); |
| 23 | // animation_->SetSlideDuration(500); |
| 24 | // } |
| 25 | // void OnMouseOver() { |
| 26 | // animation_->Show(); |
| 27 | // } |
| 28 | // void OnMouseOut() { |
| 29 | // animation_->Hide(); |
| 30 | // } |
| 31 | // void AnimationProgressed(const Animation* animation) { |
| 32 | // if (animation == animation_.get()) { |
| 33 | // Layout(); |
| 34 | // SchedulePaint(); |
| 35 | // } else if (animation == other_animation_.get()) { |
| 36 | // ... |
| 37 | // } |
| 38 | // } |
| 39 | // void Layout() { |
| 40 | // if (animation_->is_animating()) { |
| 41 | // hover_image_.SetOpacity(animation_->GetCurrentValue()); |
| 42 | // } |
| 43 | // } |
| 44 | // private: |
| 45 | // scoped_ptr<SlideAnimation> animation_; |
| 46 | // } |
| 47 | class GFX_EXPORT SlideAnimation : public LinearAnimation { |
| 48 | public: |
| 49 | explicit SlideAnimation(AnimationDelegate* target); |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 50 | ~SlideAnimation() override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 51 | |
| 52 | // Set the animation back to the 0 state. |
| 53 | virtual void Reset(); |
| 54 | virtual void Reset(double value); |
| 55 | |
| 56 | // Begin a showing animation or reverse a hiding animation in progress. |
| 57 | virtual void Show(); |
| 58 | |
| 59 | // Begin a hiding animation or reverse a showing animation in progress. |
| 60 | virtual void Hide(); |
| 61 | |
| 62 | // Sets the time a slide will take. Note that this isn't actually |
| 63 | // the amount of time an animation will take as the current value of |
| 64 | // the slide is considered. |
| 65 | virtual void SetSlideDuration(int duration); |
| 66 | int GetSlideDuration() const { return slide_duration_; } |
| 67 | void SetTweenType(Tween::Type tween_type) { tween_type_ = tween_type; } |
| 68 | |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 69 | double GetCurrentValue() const override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 70 | bool IsShowing() const { return showing_; } |
| 71 | bool IsClosing() const { return !showing_ && value_end_ < value_current_; } |
| 72 | |
| 73 | class TestApi; |
| 74 | |
| 75 | private: |
| 76 | // Overridden from Animation. |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 77 | void AnimateToState(double state) override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 78 | |
| 79 | AnimationDelegate* target_; |
| 80 | |
| 81 | Tween::Type tween_type_; |
| 82 | |
| 83 | // Used to determine which way the animation is going. |
| 84 | bool showing_; |
| 85 | |
| 86 | // Animation values. These are a layer on top of Animation::state_ to |
| 87 | // provide the reversability. |
| 88 | double value_start_; |
| 89 | double value_end_; |
| 90 | double value_current_; |
| 91 | |
| 92 | // How long a hover in/out animation will last for. This defaults to |
| 93 | // kHoverFadeDurationMS, but can be overridden with SetDuration. |
| 94 | int slide_duration_; |
| 95 | |
| 96 | DISALLOW_COPY_AND_ASSIGN(SlideAnimation); |
| 97 | }; |
| 98 | |
| 99 | } // namespace gfx |
| 100 | |
| 101 | #endif // UI_GFX_ANIMATION_SLIDE_ANIMATION_H_ |