| Index: ui/gfx/compositor/layer_animator.cc
|
| diff --git a/ui/gfx/compositor/layer_animator.cc b/ui/gfx/compositor/layer_animator.cc
|
| index e30329cc77a1921c4c8931e1496352487b771472..e7eeaec586c15d7870c8ccc8aa20ef7844194dfb 100644
|
| --- a/ui/gfx/compositor/layer_animator.cc
|
| +++ b/ui/gfx/compositor/layer_animator.cc
|
| @@ -7,9 +7,11 @@
|
| #include "base/logging.h"
|
| #include "base/stl_util.h"
|
| #include "ui/base/animation/animation_container.h"
|
| -#include "ui/base/animation/multi_animation.h"
|
| +#include "ui/base/animation/animation.h"
|
| +#include "ui/base/animation/tween.h"
|
| #include "ui/gfx/compositor/compositor.h"
|
| #include "ui/gfx/compositor/layer.h"
|
| +#include "ui/gfx/compositor/layer_animator_delegate.h"
|
| #include "ui/gfx/transform.h"
|
| #include "ui/gfx/rect.h"
|
|
|
| @@ -33,21 +35,24 @@ namespace ui {
|
|
|
| LayerAnimator::LayerAnimator(Layer* layer)
|
| : layer_(layer),
|
| - duration_in_ms_(200),
|
| - animation_type_(ui::Tween::EASE_IN) {
|
| + got_initial_tick_(false) {
|
| }
|
|
|
| LayerAnimator::~LayerAnimator() {
|
| - for (Elements::iterator i = elements_.begin(); i != elements_.end(); ++i)
|
| - delete i->second.animation;
|
| - elements_.clear();
|
| }
|
|
|
| -void LayerAnimator::SetAnimationDurationAndType(int duration,
|
| - ui::Tween::Type tween_type) {
|
| - DCHECK_GT(duration, 0);
|
| - duration_in_ms_ = duration;
|
| - animation_type_ = tween_type;
|
| +void LayerAnimator::SetAnimation(Animation* animation) {
|
| + animation_.reset(animation);
|
| + if (animation_.get()) {
|
| + static ui::AnimationContainer* container = NULL;
|
| + if (!container) {
|
| + container = new AnimationContainer;
|
| + container->AddRef();
|
| + }
|
| + animation_->set_delegate(this);
|
| + animation_->SetContainer(container);
|
| + got_initial_tick_ = false;
|
| + }
|
| }
|
|
|
| void LayerAnimator::AnimateToPoint(const gfx::Point& target) {
|
| @@ -56,12 +61,11 @@ void LayerAnimator::AnimateToPoint(const gfx::Point& target) {
|
| if (target == layer_bounds.origin())
|
| return; // Already there.
|
|
|
| - Element& element = elements_[LOCATION];
|
| - element.params.location.start_x = layer_bounds.origin().x();
|
| - element.params.location.start_y = layer_bounds.origin().y();
|
| - element.params.location.target_x = target.x();
|
| - element.params.location.target_y = target.y();
|
| - element.animation = CreateAndStartAnimation();
|
| + Params& element = elements_[LOCATION];
|
| + element.location.target_x = target.x();
|
| + element.location.target_y = target.y();
|
| + element.location.start_x = layer_bounds.origin().x();
|
| + element.location.start_y = layer_bounds.origin().y();
|
| }
|
|
|
| void LayerAnimator::AnimateTransform(const Transform& transform) {
|
| @@ -70,120 +74,113 @@ void LayerAnimator::AnimateTransform(const Transform& transform) {
|
| if (transform == layer_transform)
|
| return; // Already there.
|
|
|
| - Element& element = elements_[TRANSFORM];
|
| + Params& element = elements_[TRANSFORM];
|
| for (int i = 0; i < 16; ++i) {
|
| - element.params.transform.start[i] =
|
| - GetMatrixElement(layer_transform.matrix(), i);
|
| - element.params.transform.target[i] =
|
| - GetMatrixElement(transform.matrix(), i);
|
| + element.transform.start[i] =
|
| + GetMatrixElement(layer_transform.matrix(), i);
|
| + element.transform.target[i] =
|
| + GetMatrixElement(transform.matrix(), i);
|
| }
|
| - element.animation = CreateAndStartAnimation();
|
| }
|
|
|
| -void LayerAnimator::AnimationProgressed(const ui::Animation* animation) {
|
| - Elements::iterator e = GetElementByAnimation(
|
| - static_cast<const ui::MultiAnimation*>(animation));
|
| - DCHECK(e != elements_.end());
|
| - switch (e->first) {
|
| - case LOCATION: {
|
| - const gfx::Rect& current_bounds(layer_->bounds());
|
| - gfx::Rect new_bounds = e->second.animation->CurrentValueBetween(
|
| - gfx::Rect(gfx::Point(e->second.params.location.start_x,
|
| - e->second.params.location.start_y),
|
| - current_bounds.size()),
|
| - gfx::Rect(gfx::Point(e->second.params.location.target_x,
|
| - e->second.params.location.target_y),
|
| - current_bounds.size()));
|
| - layer_->SetBounds(new_bounds);
|
| - break;
|
| - }
|
| +void LayerAnimator::AnimateOpacity(float target_opacity) {
|
| + StopAnimating(OPACITY);
|
| + if (layer_->opacity() == target_opacity)
|
| + return;
|
|
|
| - case TRANSFORM: {
|
| - Transform transform;
|
| - for (int i = 0; i < 16; ++i) {
|
| - SkMScalar value = e->second.animation->CurrentValueBetween(
|
| - e->second.params.transform.start[i],
|
| - e->second.params.transform.target[i]);
|
| - SetMatrixElement(transform.matrix(), i, value);
|
| - }
|
| - layer_->SetTransform(transform);
|
| - break;
|
| - }
|
| + Params& element = elements_[OPACITY];
|
| + element.opacity.start = layer_->opacity();
|
| + element.opacity.target = target_opacity;
|
| +}
|
|
|
| - default:
|
| - NOTREACHED();
|
| - }
|
| - layer_->GetCompositor()->SchedulePaint();
|
| +gfx::Point LayerAnimator::GetTargetPoint() {
|
| + return IsAnimating(LOCATION) ?
|
| + gfx::Point(elements_[LOCATION].location.target_x,
|
| + elements_[LOCATION].location.target_y) :
|
| + layer_->bounds().origin();
|
| }
|
|
|
| -void LayerAnimator::AnimationEnded(const ui::Animation* animation) {
|
| - Elements::iterator e = GetElementByAnimation(
|
| - static_cast<const ui::MultiAnimation*>(animation));
|
| - DCHECK(e != elements_.end());
|
| - switch (e->first) {
|
| - case LOCATION: {
|
| - gfx::Rect new_bounds(
|
| - gfx::Point(e->second.params.location.target_x,
|
| - e->second.params.location.target_y),
|
| - layer_->bounds().size());
|
| - layer_->SetBounds(new_bounds);
|
| - break;
|
| +float LayerAnimator::GetTargetOpacity() {
|
| + return IsAnimating(OPACITY) ?
|
| + elements_[OPACITY].opacity.target : layer_->opacity();
|
| +}
|
| +
|
| +ui::Transform LayerAnimator::GetTargetTransform() {
|
| + if (IsAnimating(TRANSFORM)) {
|
| + Transform transform;
|
| + for (int i = 0; i < 16; ++i) {
|
| + SetMatrixElement(transform.matrix(), i,
|
| + elements_[TRANSFORM].transform.target[i]);
|
| }
|
| + return transform;
|
| + }
|
| + return layer_->transform();
|
| +}
|
|
|
| - case TRANSFORM: {
|
| - Transform transform;
|
| - for (int i = 0; i < 16; ++i) {
|
| - SetMatrixElement(transform.matrix(),
|
| - i,
|
| - e->second.params.transform.target[i]);
|
| +bool LayerAnimator::IsAnimating(AnimationProperty property) const {
|
| + return elements_.count(property) > 0;
|
| +}
|
| +
|
| +bool LayerAnimator::IsRunning() const {
|
| + return animation_.get() && animation_->is_animating();
|
| +}
|
| +
|
| +void LayerAnimator::AnimationProgressed(const ui::Animation* animation) {
|
| + got_initial_tick_ = true;
|
| + for (Elements::const_iterator i = elements_.begin(); i != elements_.end();
|
| + ++i) {
|
| + switch (i->first) {
|
| + case LOCATION: {
|
| + const gfx::Rect& current_bounds(layer_->bounds());
|
| + gfx::Rect new_bounds = animation_->CurrentValueBetween(
|
| + gfx::Rect(gfx::Point(i->second.location.start_x,
|
| + i->second.location.start_y),
|
| + current_bounds.size()),
|
| + gfx::Rect(gfx::Point(i->second.location.target_x,
|
| + i->second.location.target_y),
|
| + current_bounds.size()));
|
| + delegate()->SetBoundsFromAnimator(new_bounds);
|
| + break;
|
| + }
|
| +
|
| + case TRANSFORM: {
|
| + Transform transform;
|
| + for (int j = 0; j < 16; ++j) {
|
| + SkMScalar value = animation_->CurrentValueBetween(
|
| + i->second.transform.start[j],
|
| + i->second.transform.target[j]);
|
| + SetMatrixElement(transform.matrix(), j, value);
|
| + }
|
| + delegate()->SetTransformFromAnimator(transform);
|
| + break;
|
| }
|
| - layer_->SetTransform(transform);
|
| - break;
|
| - }
|
|
|
| - default:
|
| - NOTREACHED();
|
| + case OPACITY: {
|
| + delegate()->SetOpacityFromAnimator(animation_->CurrentValueBetween(
|
| + i->second.opacity.start, i->second.opacity.target));
|
| + break;
|
| + }
|
| +
|
| + default:
|
| + NOTREACHED();
|
| + }
|
| }
|
| - StopAnimating(e->first);
|
| - // StopAnimating removes from the map, invalidating 'e'.
|
| - e = elements_.end();
|
| layer_->GetCompositor()->SchedulePaint();
|
| }
|
|
|
| +void LayerAnimator::AnimationEnded(const ui::Animation* animation) {
|
| + AnimationProgressed(animation);
|
| +}
|
| +
|
| void LayerAnimator::StopAnimating(AnimationProperty property) {
|
| - if (elements_.count(property) == 0)
|
| + if (!IsAnimating(property))
|
| return;
|
|
|
| - // Reset the delegate so that we don't attempt to update the layer.
|
| - elements_[property].animation->set_delegate(NULL);
|
| - delete elements_[property].animation;
|
| elements_.erase(property);
|
| }
|
|
|
| -ui::MultiAnimation* LayerAnimator::CreateAndStartAnimation() {
|
| - static ui::AnimationContainer* container = NULL;
|
| - if (!container) {
|
| - container = new AnimationContainer;
|
| - container->AddRef();
|
| - }
|
| - ui::MultiAnimation::Parts parts;
|
| - parts.push_back(ui::MultiAnimation::Part(duration_in_ms_, animation_type_));
|
| - ui::MultiAnimation* animation = new ui::MultiAnimation(parts);
|
| - animation->SetContainer(container);
|
| - animation->set_delegate(this);
|
| - animation->set_continuous(false);
|
| - animation->Start();
|
| - return animation;
|
| -}
|
| -
|
| -LayerAnimator::Elements::iterator LayerAnimator::GetElementByAnimation(
|
| - const ui::MultiAnimation* animation) {
|
| - for (Elements::iterator i = elements_.begin(); i != elements_.end(); ++i) {
|
| - if (i->second.animation == animation)
|
| - return i;
|
| - }
|
| - NOTREACHED();
|
| - return elements_.end();
|
| +LayerAnimatorDelegate* LayerAnimator::delegate() {
|
| + return static_cast<LayerAnimatorDelegate*>(layer_);
|
| }
|
|
|
| } // namespace ui
|
|
|