OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/gfx/compositor/layer_animation_manager.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/stl_util.h" | |
9 #include "ui/base/animation/animation_container.h" | |
10 #include "ui/base/animation/animation.h" | |
11 #include "ui/base/animation/tween.h" | |
12 #include "ui/gfx/compositor/compositor.h" | |
13 #include "ui/gfx/compositor/layer.h" | |
14 #include "ui/gfx/compositor/layer_animator_delegate.h" | |
15 #include "ui/gfx/transform.h" | |
16 #include "ui/gfx/rect.h" | |
17 | |
18 namespace { | |
19 | |
20 void SetMatrixElement(SkMatrix44& matrix, int index, SkMScalar value) { | |
21 int row = index / 4; | |
22 int col = index % 4; | |
23 matrix.set(row, col, value); | |
24 } | |
25 | |
26 SkMScalar GetMatrixElement(const SkMatrix44& matrix, int index) { | |
27 int row = index / 4; | |
28 int col = index % 4; | |
29 return matrix.get(row, col); | |
30 } | |
31 | |
32 } // anonymous namespace | |
33 | |
34 namespace ui { | |
35 | |
36 LayerAnimationManager::LayerAnimationManager(Layer* layer) | |
37 : layer_(layer), | |
38 got_initial_tick_(false) { | |
39 } | |
40 | |
41 LayerAnimationManager::~LayerAnimationManager() { | |
42 } | |
43 | |
44 void LayerAnimationManager::SetAnimation(Animation* animation) { | |
45 animation_.reset(animation); | |
46 if (animation_.get()) { | |
47 static ui::AnimationContainer* container = NULL; | |
48 if (!container) { | |
49 container = new AnimationContainer; | |
50 container->AddRef(); | |
51 } | |
52 animation_->set_delegate(this); | |
53 animation_->SetContainer(container); | |
54 got_initial_tick_ = false; | |
55 } | |
56 } | |
57 | |
58 void LayerAnimationManager::AnimateToPoint(const gfx::Point& target) { | |
59 StopAnimating(LOCATION); | |
60 const gfx::Rect& layer_bounds = layer_->bounds(); | |
61 if (target == layer_bounds.origin()) | |
62 return; // Already there. | |
63 | |
64 Params& element = elements_[LOCATION]; | |
65 element.location.target_x = target.x(); | |
66 element.location.target_y = target.y(); | |
67 element.location.start_x = layer_bounds.origin().x(); | |
68 element.location.start_y = layer_bounds.origin().y(); | |
69 } | |
70 | |
71 void LayerAnimationManager::AnimateTransform(const Transform& transform) { | |
72 StopAnimating(TRANSFORM); | |
73 const Transform& layer_transform = layer_->transform(); | |
74 if (transform == layer_transform) | |
75 return; // Already there. | |
76 | |
77 Params& element = elements_[TRANSFORM]; | |
78 for (int i = 0; i < 16; ++i) { | |
79 element.transform.start[i] = | |
80 GetMatrixElement(layer_transform.matrix(), i); | |
81 element.transform.target[i] = | |
82 GetMatrixElement(transform.matrix(), i); | |
83 } | |
84 } | |
85 | |
86 void LayerAnimationManager::AnimateOpacity(float target_opacity) { | |
87 StopAnimating(OPACITY); | |
88 if (layer_->opacity() == target_opacity) | |
89 return; | |
90 | |
91 Params& element = elements_[OPACITY]; | |
92 element.opacity.start = layer_->opacity(); | |
93 element.opacity.target = target_opacity; | |
94 } | |
95 | |
96 gfx::Point LayerAnimationManager::GetTargetPoint() { | |
97 return IsAnimating(LOCATION) ? | |
98 gfx::Point(elements_[LOCATION].location.target_x, | |
99 elements_[LOCATION].location.target_y) : | |
100 layer_->bounds().origin(); | |
101 } | |
102 | |
103 float LayerAnimationManager::GetTargetOpacity() { | |
104 return IsAnimating(OPACITY) ? | |
105 elements_[OPACITY].opacity.target : layer_->opacity(); | |
106 } | |
107 | |
108 ui::Transform LayerAnimationManager::GetTargetTransform() { | |
109 if (IsAnimating(TRANSFORM)) { | |
110 Transform transform; | |
111 for (int i = 0; i < 16; ++i) { | |
112 SetMatrixElement(transform.matrix(), i, | |
113 elements_[TRANSFORM].transform.target[i]); | |
114 } | |
115 return transform; | |
116 } | |
117 return layer_->transform(); | |
118 } | |
119 | |
120 bool LayerAnimationManager::IsAnimating(AnimationProperty property) const { | |
121 return elements_.count(property) > 0; | |
122 } | |
123 | |
124 bool LayerAnimationManager::IsRunning() const { | |
125 return animation_.get() && animation_->is_animating(); | |
126 } | |
127 | |
128 void LayerAnimationManager::AnimationProgressed( | |
129 const ui::Animation* animation) { | |
130 got_initial_tick_ = true; | |
131 for (Elements::const_iterator i = elements_.begin(); i != elements_.end(); | |
132 ++i) { | |
133 switch (i->first) { | |
134 case LOCATION: { | |
135 const gfx::Rect& current_bounds(layer_->bounds()); | |
136 gfx::Rect new_bounds = animation_->CurrentValueBetween( | |
137 gfx::Rect(gfx::Point(i->second.location.start_x, | |
138 i->second.location.start_y), | |
139 current_bounds.size()), | |
140 gfx::Rect(gfx::Point(i->second.location.target_x, | |
141 i->second.location.target_y), | |
142 current_bounds.size())); | |
143 delegate()->SetBoundsFromAnimator(new_bounds); | |
144 break; | |
145 } | |
146 | |
147 case TRANSFORM: { | |
148 Transform transform; | |
149 for (int j = 0; j < 16; ++j) { | |
150 SkMScalar value = animation_->CurrentValueBetween( | |
151 i->second.transform.start[j], | |
152 i->second.transform.target[j]); | |
153 SetMatrixElement(transform.matrix(), j, value); | |
154 } | |
155 delegate()->SetTransformFromAnimator(transform); | |
156 break; | |
157 } | |
158 | |
159 case OPACITY: { | |
160 delegate()->SetOpacityFromAnimator(animation_->CurrentValueBetween( | |
161 i->second.opacity.start, i->second.opacity.target)); | |
162 break; | |
163 } | |
164 | |
165 default: | |
166 NOTREACHED(); | |
167 } | |
168 } | |
169 layer_->ScheduleDraw(); | |
170 } | |
171 | |
172 void LayerAnimationManager::AnimationEnded(const ui::Animation* animation) { | |
173 AnimationProgressed(animation); | |
174 if (layer_->delegate()) | |
175 layer_->delegate()->OnLayerAnimationEnded(animation); | |
176 } | |
177 | |
178 void LayerAnimationManager::StopAnimating(AnimationProperty property) { | |
179 if (!IsAnimating(property)) | |
180 return; | |
181 | |
182 elements_.erase(property); | |
183 } | |
184 | |
185 LayerAnimatorDelegate* LayerAnimationManager::delegate() { | |
186 return static_cast<LayerAnimatorDelegate*>(layer_); | |
187 } | |
188 | |
189 } // namespace ui | |
OLD | NEW |