OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. |
| 4 |
| 5 #include "views/animator.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "app/slide_animation.h" |
| 10 #include "views/view.h" |
| 11 |
| 12 namespace views { |
| 13 |
| 14 //////////////////////////////////////////////////////////////////////////////// |
| 15 // Animator, public: |
| 16 |
| 17 Animator::Animator(View* host) |
| 18 : host_(host), |
| 19 delegate_(NULL), |
| 20 direction_(ANIMATE_NONE) { |
| 21 InitAnimation(); |
| 22 } |
| 23 |
| 24 Animator::Animator(View* host, AnimatorDelegate* delegate) |
| 25 : host_(host), |
| 26 animation_(NULL), |
| 27 delegate_(delegate), |
| 28 direction_(ANIMATE_NONE) { |
| 29 InitAnimation(); |
| 30 } |
| 31 |
| 32 Animator::~Animator() { |
| 33 // Explicitly NULL the delegate so we don't call back through to the delegate |
| 34 // when the animation is stopped. The Animator is designed to be owned by a |
| 35 // View and at this point the View is dust. |
| 36 delegate_ = NULL; |
| 37 animation_->Stop(); |
| 38 } |
| 39 |
| 40 bool Animator::IsAnimating() const { |
| 41 return animation_->IsAnimating(); |
| 42 } |
| 43 |
| 44 void Animator::AnimateToBounds(const gfx::Rect& bounds, int direction) { |
| 45 direction_ = direction; |
| 46 start_bounds_ = host_->bounds(); |
| 47 target_bounds_ = bounds; |
| 48 |
| 49 // Stop any running animation before we have a chance to return. |
| 50 animation_->Stop(); |
| 51 |
| 52 if (bounds == host_->bounds()) |
| 53 return; |
| 54 |
| 55 if (direction_ == ANIMATE_NONE) { |
| 56 host_->SetBounds(bounds); |
| 57 return; |
| 58 } |
| 59 |
| 60 if (direction_ & ANIMATE_X) { |
| 61 if (direction_ & ANIMATE_CLAMP) |
| 62 start_bounds_.set_x(GetClampedX()); |
| 63 } else { |
| 64 start_bounds_.set_x(target_bounds_.x()); |
| 65 } |
| 66 |
| 67 if (direction_ & ANIMATE_Y) { |
| 68 if (direction_ & ANIMATE_CLAMP) |
| 69 start_bounds_.set_y(GetClampedY()); |
| 70 } else { |
| 71 start_bounds_.set_y(target_bounds_.y()); |
| 72 } |
| 73 |
| 74 if (!(direction_ & ANIMATE_WIDTH)) |
| 75 start_bounds_.set_width(target_bounds_.width()); |
| 76 if (!(direction_ & ANIMATE_HEIGHT)) |
| 77 start_bounds_.set_height(target_bounds_.height()); |
| 78 |
| 79 // Make sure the host view has the start bounds to avoid a flicker. |
| 80 host_->SetBounds(start_bounds_); |
| 81 |
| 82 // Start the animation from the beginning. |
| 83 animation_->Reset(0.0); |
| 84 animation_->Show(); |
| 85 } |
| 86 |
| 87 //////////////////////////////////////////////////////////////////////////////// |
| 88 // Animator, AnimationDelegate: |
| 89 |
| 90 void Animator::AnimationEnded(const Animation* animation) { |
| 91 // |delegate_| could be NULL if we're called back from the destructor. |
| 92 if (delegate_) |
| 93 delegate_->AnimationCompletedForHost(host_); |
| 94 } |
| 95 |
| 96 void Animator::AnimationProgressed(const Animation* animation) { |
| 97 int delta_x = target_bounds_.x() - start_bounds_.x(); |
| 98 int delta_y = target_bounds_.y() - start_bounds_.y(); |
| 99 int delta_width = target_bounds_.width() - start_bounds_.width(); |
| 100 int delta_height = target_bounds_.height() - start_bounds_.height(); |
| 101 |
| 102 // The current frame's position and size is the animation's progress percent |
| 103 // multiplied by the delta between the start and target position/size... |
| 104 double cv = animation_->GetCurrentValue(); |
| 105 int frame_x = start_bounds_.x() + static_cast<int>(delta_x * cv); |
| 106 int frame_y = start_bounds_.y() + static_cast<int>(delta_y * cv); |
| 107 // ... except for clamped positions, which remain clamped at the right/bottom |
| 108 // edge of the previous view in the layout flow. |
| 109 if (direction_ & ANIMATE_CLAMP && direction_ & ANIMATE_X) |
| 110 frame_x = GetClampedX(); |
| 111 if (direction_ & ANIMATE_CLAMP && direction_ & ANIMATE_Y) |
| 112 frame_y = GetClampedY(); |
| 113 int frame_width = start_bounds_.width() + static_cast<int>(delta_width * cv); |
| 114 int frame_height = |
| 115 start_bounds_.height() + static_cast<int>(delta_height * cv); |
| 116 host_->SetBounds(frame_x, frame_y, frame_width, frame_height); |
| 117 host_->GetParent()->SchedulePaint(); |
| 118 } |
| 119 |
| 120 void Animator::AnimationCanceled(const Animation* animation) { |
| 121 AnimationEnded(animation); |
| 122 } |
| 123 |
| 124 //////////////////////////////////////////////////////////////////////////////// |
| 125 // Animator, private: |
| 126 |
| 127 void Animator::InitAnimation() { |
| 128 animation_.reset(new SlideAnimation(this)); |
| 129 animation_->SetSlideDuration(150); |
| 130 animation_->SetTweenType(SlideAnimation::EASE_OUT); |
| 131 } |
| 132 |
| 133 int Animator::GetClampedX() const { |
| 134 if (delegate_ && direction_ & ANIMATE_CLAMP && direction_ & ANIMATE_X) { |
| 135 View* previous_view = delegate_->GetClampedView(host_); |
| 136 if (previous_view) |
| 137 return previous_view->bounds().right(); |
| 138 } |
| 139 return host_->x(); |
| 140 } |
| 141 |
| 142 int Animator::GetClampedY() const { |
| 143 if (delegate_ && direction_ & ANIMATE_CLAMP && direction_ & ANIMATE_Y) { |
| 144 View* previous_view = delegate_->GetClampedView(host_); |
| 145 if (previous_view) |
| 146 return previous_view->bounds().bottom(); |
| 147 } |
| 148 return host_->y(); |
| 149 } |
| 150 |
| 151 } // namespace views |
OLD | NEW |