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.h" | 5 #include "ui/gfx/compositor/layer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "ui/base/animation/animation.h" | 11 #include "ui/gfx/compositor/layer_animator.h" |
12 #include "ui/gfx/compositor/layer_animation_manager.h" | |
13 #include "ui/gfx/canvas_skia.h" | 12 #include "ui/gfx/canvas_skia.h" |
14 #include "ui/gfx/interpolated_transform.h" | 13 #include "ui/gfx/interpolated_transform.h" |
15 #include "ui/gfx/point3.h" | 14 #include "ui/gfx/point3.h" |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 const float EPSILON = 1e-3f; | 18 const float EPSILON = 1e-3f; |
20 | 19 |
21 bool IsApproximateMultilpleOf(float value, float base) { | 20 bool IsApproximateMultilpleOf(float value, float base) { |
22 float remainder = fmod(fabs(value), base); | 21 float remainder = fmod(fabs(value), base); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 } | 98 } |
100 | 99 |
101 bool Layer::Contains(const Layer* other) const { | 100 bool Layer::Contains(const Layer* other) const { |
102 for (const Layer* parent = other; parent; parent = parent->parent()) { | 101 for (const Layer* parent = other; parent; parent = parent->parent()) { |
103 if (parent == this) | 102 if (parent == this) |
104 return true; | 103 return true; |
105 } | 104 } |
106 return false; | 105 return false; |
107 } | 106 } |
108 | 107 |
109 void Layer::SetAnimation(Animation* animation) { | 108 void Layer::SetAnimator(LayerAnimator* animator) { |
110 if (animation) { | 109 animator->SetDelegate(this); |
sky
2011/10/20 20:30:30
Seems like you should allow NULL here to reset to
| |
111 if (!animator_.get()) | 110 animator_.reset(animator); |
112 animator_.reset(new LayerAnimationManager(this)); | 111 } |
113 animation->Start(); | 112 |
114 animator_->SetAnimation(animation); | 113 LayerAnimator* Layer::GetAnimator() { |
115 } else { | 114 if (!animator_.get()) |
116 animator_.reset(); | 115 SetAnimator(LayerAnimator::CreateDefaultAnimator()); |
117 } | 116 return animator_.get(); |
118 } | 117 } |
119 | 118 |
120 void Layer::SetTransform(const ui::Transform& transform) { | 119 void Layer::SetTransform(const ui::Transform& transform) { |
121 StopAnimatingIfNecessary(LayerAnimationManager::TRANSFORM); | 120 if (animator_.get()) |
122 if (animator_.get() && animator_->IsRunning()) { | 121 animator_->SetTransform(transform); |
123 animator_->AnimateTransform(transform); | 122 else |
124 return; | 123 SetTransformImmediately(transform); |
125 } | 124 } |
126 SetTransformImmediately(transform); | 125 |
126 Transform Layer::GetTargetTransform() const { | |
127 if (animator_.get() && animator_->is_animating()) | |
128 return animator_->GetTargetTransform(); | |
129 return transform_; | |
127 } | 130 } |
128 | 131 |
129 void Layer::SetBounds(const gfx::Rect& bounds) { | 132 void Layer::SetBounds(const gfx::Rect& bounds) { |
130 StopAnimatingIfNecessary(LayerAnimationManager::LOCATION); | 133 if (animator_.get()) |
131 if (animator_.get() && animator_->IsRunning() && | 134 animator_->SetBounds(bounds); |
132 bounds.size() == bounds_.size()) { | 135 else |
133 animator_->AnimateToPoint(bounds.origin()); | 136 SetBoundsImmediately(bounds); |
134 return; | |
135 } | |
136 SetBoundsImmediately(bounds); | |
137 } | 137 } |
138 | 138 |
139 gfx::Rect Layer::GetTargetBounds() const { | 139 gfx::Rect Layer::GetTargetBounds() const { |
140 if (animator_.get() && animator_->IsRunning()) | 140 if (animator_.get() && animator_->is_animating()) |
141 return gfx::Rect(animator_->GetTargetPoint(), bounds_.size()); | 141 return animator_->GetTargetBounds(); |
142 return bounds_; | 142 return bounds_; |
143 } | 143 } |
144 | 144 |
145 void Layer::SetOpacity(float opacity) { | 145 void Layer::SetOpacity(float opacity) { |
146 StopAnimatingIfNecessary(LayerAnimationManager::OPACITY); | 146 if (animator_.get()) |
147 if (animator_.get() && animator_->IsRunning()) { | 147 animator_->SetOpacity(opacity); |
148 animator_->AnimateOpacity(opacity); | 148 else |
149 return; | 149 SetOpacityImmediately(opacity); |
150 } | 150 } |
151 SetOpacityImmediately(opacity); | 151 |
152 float Layer::GetTargetOpacity() const { | |
153 if (animator_.get() && animator_->is_animating()) | |
154 return animator_->GetTargetOpacity(); | |
155 return opacity_; | |
152 } | 156 } |
153 | 157 |
154 void Layer::SetVisible(bool visible) { | 158 void Layer::SetVisible(bool visible) { |
155 if (visible_ == visible) | 159 if (visible_ == visible) |
156 return; | 160 return; |
157 | 161 |
158 bool was_drawn = IsDrawn(); | 162 bool was_drawn = IsDrawn(); |
159 visible_ = visible; | 163 visible_ = visible; |
160 bool is_drawn = IsDrawn(); | 164 bool is_drawn = IsDrawn(); |
161 if (was_drawn == is_drawn) | 165 if (was_drawn == is_drawn) |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 const Layer* p = this; | 435 const Layer* p = this; |
432 for (; p && p != ancestor; p = p->parent()) { | 436 for (; p && p != ancestor; p = p->parent()) { |
433 if (p->transform().HasChange()) | 437 if (p->transform().HasChange()) |
434 transform->ConcatTransform(p->transform()); | 438 transform->ConcatTransform(p->transform()); |
435 transform->ConcatTranslate(static_cast<float>(p->bounds().x()), | 439 transform->ConcatTranslate(static_cast<float>(p->bounds().x()), |
436 static_cast<float>(p->bounds().y())); | 440 static_cast<float>(p->bounds().y())); |
437 } | 441 } |
438 return p == ancestor; | 442 return p == ancestor; |
439 } | 443 } |
440 | 444 |
441 void Layer::StopAnimatingIfNecessary( | |
442 LayerAnimationManager::AnimationProperty property) { | |
443 if (!animator_.get() || !animator_->IsRunning() || | |
444 !animator_->got_initial_tick()) { | |
445 return; | |
446 } | |
447 | |
448 if (property != LayerAnimationManager::LOCATION && | |
449 animator_->IsAnimating(LayerAnimationManager::LOCATION)) { | |
450 SetBoundsImmediately( | |
451 gfx::Rect(animator_->GetTargetPoint(), bounds_.size())); | |
452 } | |
453 if (property != LayerAnimationManager::OPACITY && | |
454 animator_->IsAnimating(LayerAnimationManager::OPACITY)) { | |
455 SetOpacityImmediately(animator_->GetTargetOpacity()); | |
456 } | |
457 if (property != LayerAnimationManager::TRANSFORM && | |
458 animator_->IsAnimating(LayerAnimationManager::TRANSFORM)) { | |
459 SetTransformImmediately(animator_->GetTargetTransform()); | |
460 } | |
461 animator_.reset(); | |
462 } | |
463 | |
464 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { | 445 void Layer::SetBoundsImmediately(const gfx::Rect& bounds) { |
465 bounds_ = bounds; | 446 bounds_ = bounds; |
466 | 447 |
467 if (parent()) | 448 if (parent()) |
468 parent()->RecomputeHole(); | 449 parent()->RecomputeHole(); |
469 } | 450 } |
470 | 451 |
471 void Layer::SetTransformImmediately(const ui::Transform& transform) { | 452 void Layer::SetTransformImmediately(const ui::Transform& transform) { |
472 transform_ = transform; | 453 transform_ = transform; |
473 | 454 |
(...skipping 16 matching lines...) Expand all Loading... | |
490 while (!to_process.empty()) { | 471 while (!to_process.empty()) { |
491 Layer* current = to_process.front(); | 472 Layer* current = to_process.front(); |
492 to_process.pop(); | 473 to_process.pop(); |
493 current->RecomputeHole(); | 474 current->RecomputeHole(); |
494 for (size_t i = 0; i < current->children_.size(); ++i) | 475 for (size_t i = 0; i < current->children_.size(); ++i) |
495 to_process.push(current->children_.at(i)); | 476 to_process.push(current->children_.at(i)); |
496 } | 477 } |
497 } | 478 } |
498 } | 479 } |
499 | 480 |
500 void Layer::SetBoundsFromAnimator(const gfx::Rect& bounds) { | 481 void Layer::SetBoundsFromAnimation(const gfx::Rect& bounds) { |
501 SetBoundsImmediately(bounds); | 482 SetBoundsImmediately(bounds); |
502 } | 483 } |
503 | 484 |
504 void Layer::SetTransformFromAnimator(const Transform& transform) { | 485 void Layer::SetTransformFromAnimation(const Transform& transform) { |
505 SetTransformImmediately(transform); | 486 SetTransformImmediately(transform); |
506 } | 487 } |
507 | 488 |
508 void Layer::SetOpacityFromAnimator(float opacity) { | 489 void Layer::SetOpacityFromAnimation(float opacity) { |
509 SetOpacityImmediately(opacity); | 490 SetOpacityImmediately(opacity); |
510 } | 491 } |
511 | 492 |
493 void Layer::ScheduleDrawForAnimation() { | |
494 ScheduleDraw(); | |
495 } | |
496 | |
497 const gfx::Rect& Layer::GetBoundsForAnimation() const { | |
498 return bounds(); | |
499 } | |
500 | |
501 const Transform& Layer::GetTransformForAnimation() const { | |
502 return transform(); | |
503 } | |
504 | |
505 float Layer::GetOpacityForAnimation() const { | |
506 return opacity(); | |
507 } | |
508 | |
512 } // namespace ui | 509 } // namespace ui |
OLD | NEW |