| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/compositor/layer_animator.h" | 5 #include "ui/gfx/compositor/layer_animator.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util-inl.h" | 8 #include "base/stl_util-inl.h" |
| 9 #include "ui/base/animation/animation_container.h" | 9 #include "ui/base/animation/animation_container.h" |
| 10 #include "ui/base/animation/multi_animation.h" | 10 #include "ui/base/animation/multi_animation.h" |
| 11 #include "ui/gfx/compositor/compositor.h" | 11 #include "ui/gfx/compositor/compositor.h" |
| 12 #include "ui/gfx/compositor/layer.h" | 12 #include "ui/gfx/compositor/layer.h" |
| 13 #include "ui/gfx/transform.h" | 13 #include "ui/gfx/transform.h" |
| 14 #include "ui/gfx/rect.h" | 14 #include "ui/gfx/rect.h" |
| 15 | 15 |
| 16 namespace { |
| 17 |
| 18 void SetMatrixElement(SkMatrix44& matrix, int index, SkMScalar value) { |
| 19 int row = index / 4; |
| 20 int col = index % 4; |
| 21 matrix.set(row, col, value); |
| 22 } |
| 23 |
| 24 SkMScalar GetMatrixElement(const SkMatrix44& matrix, int index) { |
| 25 int row = index / 4; |
| 26 int col = index % 4; |
| 27 return matrix.get(row, col); |
| 28 } |
| 29 |
| 30 } // anonymous namespace |
| 31 |
| 16 namespace ui { | 32 namespace ui { |
| 17 | 33 |
| 18 LayerAnimator::LayerAnimator(Layer* layer) | 34 LayerAnimator::LayerAnimator(Layer* layer) |
| 19 : layer_(layer), | 35 : layer_(layer), |
| 20 duration_in_ms_(200), | 36 duration_in_ms_(200), |
| 21 animation_type_(ui::Tween::EASE_IN) { | 37 animation_type_(ui::Tween::EASE_IN) { |
| 22 } | 38 } |
| 23 | 39 |
| 24 LayerAnimator::~LayerAnimator() { | 40 LayerAnimator::~LayerAnimator() { |
| 25 for (Elements::iterator i = elements_.begin(); i != elements_.end(); ++i) | 41 for (Elements::iterator i = elements_.begin(); i != elements_.end(); ++i) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 44 element.params.location.start_x = layer_bounds.origin().x(); | 60 element.params.location.start_x = layer_bounds.origin().x(); |
| 45 element.params.location.start_y = layer_bounds.origin().y(); | 61 element.params.location.start_y = layer_bounds.origin().y(); |
| 46 element.params.location.target_x = target.x(); | 62 element.params.location.target_x = target.x(); |
| 47 element.params.location.target_y = target.y(); | 63 element.params.location.target_y = target.y(); |
| 48 element.animation = CreateAndStartAnimation(); | 64 element.animation = CreateAndStartAnimation(); |
| 49 } | 65 } |
| 50 | 66 |
| 51 void LayerAnimator::AnimateTransform(const Transform& transform) { | 67 void LayerAnimator::AnimateTransform(const Transform& transform) { |
| 52 StopAnimating(TRANSFORM); | 68 StopAnimating(TRANSFORM); |
| 53 const Transform& layer_transform = layer_->transform(); | 69 const Transform& layer_transform = layer_->transform(); |
| 54 bool all_equal = true; | 70 if (transform == layer_transform) |
| 55 // TODO: replace with == when we Transform supports ==. | |
| 56 for (int i = 0; i < 9; ++i) { | |
| 57 if (transform.matrix()[i] != layer_transform.matrix()[i]) { | |
| 58 all_equal = false; | |
| 59 break; | |
| 60 } | |
| 61 } | |
| 62 if (all_equal) | |
| 63 return; // Already there. | 71 return; // Already there. |
| 64 | 72 |
| 65 Element& element = elements_[TRANSFORM]; | 73 Element& element = elements_[TRANSFORM]; |
| 66 for (int i = 0; i < 9; ++i) { | 74 for (int i = 0; i < 16; ++i) { |
| 67 element.params.transform.start[i] = layer_transform.matrix()[i]; | 75 element.params.transform.start[i] = |
| 68 element.params.transform.target[i] = transform.matrix()[i]; | 76 GetMatrixElement(layer_transform.matrix(), i); |
| 77 element.params.transform.target[i] = |
| 78 GetMatrixElement(transform.matrix(), i); |
| 69 } | 79 } |
| 70 element.animation = CreateAndStartAnimation(); | 80 element.animation = CreateAndStartAnimation(); |
| 71 } | 81 } |
| 72 | 82 |
| 73 void LayerAnimator::AnimationProgressed(const ui::Animation* animation) { | 83 void LayerAnimator::AnimationProgressed(const ui::Animation* animation) { |
| 74 Elements::iterator e = GetElementByAnimation( | 84 Elements::iterator e = GetElementByAnimation( |
| 75 static_cast<const ui::MultiAnimation*>(animation)); | 85 static_cast<const ui::MultiAnimation*>(animation)); |
| 76 DCHECK(e != elements_.end()); | 86 DCHECK(e != elements_.end()); |
| 77 switch (e->first) { | 87 switch (e->first) { |
| 78 case LOCATION: { | 88 case LOCATION: { |
| 79 const gfx::Rect& current_bounds(layer_->bounds()); | 89 const gfx::Rect& current_bounds(layer_->bounds()); |
| 80 gfx::Rect new_bounds = e->second.animation->CurrentValueBetween( | 90 gfx::Rect new_bounds = e->second.animation->CurrentValueBetween( |
| 81 gfx::Rect(gfx::Point(e->second.params.location.start_x, | 91 gfx::Rect(gfx::Point(e->second.params.location.start_x, |
| 82 e->second.params.location.start_y), | 92 e->second.params.location.start_y), |
| 83 current_bounds.size()), | 93 current_bounds.size()), |
| 84 gfx::Rect(gfx::Point(e->second.params.location.target_x, | 94 gfx::Rect(gfx::Point(e->second.params.location.target_x, |
| 85 e->second.params.location.target_y), | 95 e->second.params.location.target_y), |
| 86 current_bounds.size())); | 96 current_bounds.size())); |
| 87 layer_->set_bounds(new_bounds); | 97 layer_->set_bounds(new_bounds); |
| 88 break; | 98 break; |
| 89 } | 99 } |
| 90 | 100 |
| 91 case TRANSFORM: { | 101 case TRANSFORM: { |
| 92 Transform transform; | 102 Transform transform; |
| 93 for (int i = 0; i < 9; ++i) { | 103 for (int i = 0; i < 16; ++i) { |
| 94 transform.matrix()[i] = e->second.animation->CurrentValueBetween( | 104 SkMScalar value = e->second.animation->CurrentValueBetween( |
| 95 e->second.params.transform.start[i], | 105 e->second.params.transform.start[i], |
| 96 e->second.params.transform.target[i]); | 106 e->second.params.transform.target[i]); |
| 107 SetMatrixElement(transform.matrix(), i, value); |
| 97 } | 108 } |
| 98 layer_->set_transform(transform); | 109 layer_->set_transform(transform); |
| 99 break; | 110 break; |
| 100 } | 111 } |
| 101 | 112 |
| 102 default: | 113 default: |
| 103 NOTREACHED(); | 114 NOTREACHED(); |
| 104 } | 115 } |
| 105 layer_->compositor()->SchedulePaint(); | 116 layer_->compositor()->SchedulePaint(); |
| 106 } | 117 } |
| 107 | 118 |
| 108 void LayerAnimator::AnimationEnded(const ui::Animation* animation) { | 119 void LayerAnimator::AnimationEnded(const ui::Animation* animation) { |
| 109 Elements::iterator e = GetElementByAnimation( | 120 Elements::iterator e = GetElementByAnimation( |
| 110 static_cast<const ui::MultiAnimation*>(animation)); | 121 static_cast<const ui::MultiAnimation*>(animation)); |
| 111 DCHECK(e != elements_.end()); | 122 DCHECK(e != elements_.end()); |
| 112 switch (e->first) { | 123 switch (e->first) { |
| 113 case LOCATION: { | 124 case LOCATION: { |
| 114 gfx::Rect new_bounds( | 125 gfx::Rect new_bounds( |
| 115 gfx::Point(e->second.params.location.target_x, | 126 gfx::Point(e->second.params.location.target_x, |
| 116 e->second.params.location.target_y), | 127 e->second.params.location.target_y), |
| 117 layer_->bounds().size()); | 128 layer_->bounds().size()); |
| 118 layer_->set_bounds(new_bounds); | 129 layer_->set_bounds(new_bounds); |
| 119 break; | 130 break; |
| 120 } | 131 } |
| 121 | 132 |
| 122 case TRANSFORM: { | 133 case TRANSFORM: { |
| 123 Transform transform; | 134 Transform transform; |
| 124 for (int i = 0; i < 9; ++i) | 135 for (int i = 0; i < 16; ++i) { |
| 125 transform.matrix()[i] = e->second.params.transform.target[i]; | 136 SetMatrixElement(transform.matrix(), |
| 137 i, |
| 138 e->second.params.transform.target[i]); |
| 139 } |
| 126 layer_->set_transform(transform); | 140 layer_->set_transform(transform); |
| 127 break; | 141 break; |
| 128 } | 142 } |
| 129 | 143 |
| 130 default: | 144 default: |
| 131 NOTREACHED(); | 145 NOTREACHED(); |
| 132 } | 146 } |
| 133 StopAnimating(e->first); | 147 StopAnimating(e->first); |
| 134 // StopAnimating removes from the map, invalidating 'e'. | 148 // StopAnimating removes from the map, invalidating 'e'. |
| 135 e = elements_.end(); | 149 e = elements_.end(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 166 const ui::MultiAnimation* animation) { | 180 const ui::MultiAnimation* animation) { |
| 167 for (Elements::iterator i = elements_.begin(); i != elements_.end(); ++i) { | 181 for (Elements::iterator i = elements_.begin(); i != elements_.end(); ++i) { |
| 168 if (i->second.animation == animation) | 182 if (i->second.animation == animation) |
| 169 return i; | 183 return i; |
| 170 } | 184 } |
| 171 NOTREACHED(); | 185 NOTREACHED(); |
| 172 return elements_.end(); | 186 return elements_.end(); |
| 173 } | 187 } |
| 174 | 188 |
| 175 } // namespace ui | 189 } // namespace ui |
| OLD | NEW |