| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/screen_rotation.h" | |
| 6 | |
| 7 #include "base/debug/trace_event.h" | |
| 8 #include "base/time.h" | |
| 9 #include "ui/gfx/compositor/layer_animation_delegate.h" | |
| 10 #include "ui/gfx/interpolated_transform.h" | |
| 11 #include "ui/gfx/rect.h" | |
| 12 #include "ui/gfx/transform.h" | |
| 13 | |
| 14 namespace ui { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const int k90DegreeTransitionDurationMs = 350; | |
| 19 const int k180DegreeTransitionDurationMs = 550; | |
| 20 const int k360DegreeTransitionDurationMs = 750; | |
| 21 | |
| 22 base::TimeDelta GetTransitionDuration(int degrees) { | |
| 23 if (degrees == 360) | |
| 24 return base::TimeDelta::FromMilliseconds(k360DegreeTransitionDurationMs); | |
| 25 if (degrees == 180) | |
| 26 return base::TimeDelta::FromMilliseconds(k180DegreeTransitionDurationMs); | |
| 27 if (degrees == 0) | |
| 28 return base::TimeDelta::FromMilliseconds(0); | |
| 29 return base::TimeDelta::FromMilliseconds(k90DegreeTransitionDurationMs); | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 ScreenRotation::ScreenRotation(int degrees) | |
| 35 : LayerAnimationElement(GetProperties(), GetTransitionDuration(degrees)), | |
| 36 degrees_(degrees) { | |
| 37 } | |
| 38 | |
| 39 ScreenRotation::~ScreenRotation() { | |
| 40 } | |
| 41 | |
| 42 void ScreenRotation::OnStart(LayerAnimationDelegate* delegate) { | |
| 43 // No rotation required. | |
| 44 if (degrees_ == 0) | |
| 45 return; | |
| 46 | |
| 47 const Transform& current_transform = delegate->GetTransformForAnimation(); | |
| 48 const gfx::Rect& bounds = delegate->GetBoundsForAnimation(); | |
| 49 | |
| 50 gfx::Point old_pivot; | |
| 51 gfx::Point new_pivot; | |
| 52 | |
| 53 int width = bounds.width(); | |
| 54 int height = bounds.height(); | |
| 55 | |
| 56 switch (degrees_) { | |
| 57 case 90: | |
| 58 new_origin_ = new_pivot = gfx::Point(width, 0); | |
| 59 break; | |
| 60 case -90: | |
| 61 new_origin_ = new_pivot = gfx::Point(0, height); | |
| 62 break; | |
| 63 case 180: | |
| 64 case 360: | |
| 65 new_pivot = old_pivot = gfx::Point(width / 2, height / 2); | |
| 66 new_origin_.SetPoint(width, height); | |
| 67 break; | |
| 68 } | |
| 69 | |
| 70 // Convert points to world space. | |
| 71 current_transform.TransformPoint(old_pivot); | |
| 72 current_transform.TransformPoint(new_pivot); | |
| 73 current_transform.TransformPoint(new_origin_); | |
| 74 | |
| 75 scoped_ptr<InterpolatedTransform> rotation( | |
| 76 new InterpolatedTransformAboutPivot( | |
| 77 old_pivot, | |
| 78 new InterpolatedRotation(0, degrees_))); | |
| 79 | |
| 80 scoped_ptr<InterpolatedTransform> translation( | |
| 81 new InterpolatedTranslation( | |
| 82 gfx::Point(0, 0), | |
| 83 gfx::Point(new_pivot.x() - old_pivot.x(), | |
| 84 new_pivot.y() - old_pivot.y()))); | |
| 85 | |
| 86 float scale_factor = 0.9f; | |
| 87 scoped_ptr<InterpolatedTransform> scale_down( | |
| 88 new InterpolatedScale(1.0f, scale_factor, 0.0f, 0.5f)); | |
| 89 | |
| 90 scoped_ptr<InterpolatedTransform> scale_up( | |
| 91 new InterpolatedScale(1.0f, 1.0f / scale_factor, 0.5f, 1.0f)); | |
| 92 | |
| 93 interpolated_transform_.reset( | |
| 94 new InterpolatedConstantTransform(current_transform)); | |
| 95 | |
| 96 scale_up->SetChild(scale_down.release()); | |
| 97 translation->SetChild(scale_up.release()); | |
| 98 rotation->SetChild(translation.release()); | |
| 99 interpolated_transform_->SetChild(rotation.release()); | |
| 100 } | |
| 101 | |
| 102 bool ScreenRotation::OnProgress(double t, | |
| 103 LayerAnimationDelegate* delegate) { | |
| 104 delegate->SetTransformFromAnimation(interpolated_transform_->Interpolate(t)); | |
| 105 return true; | |
| 106 } | |
| 107 | |
| 108 void ScreenRotation::OnGetTarget(TargetValue* target) const { | |
| 109 target->transform = interpolated_transform_->Interpolate(1.0); | |
| 110 } | |
| 111 | |
| 112 void ScreenRotation::OnAbort() { | |
| 113 } | |
| 114 | |
| 115 // static | |
| 116 const LayerAnimationElement::AnimatableProperties& | |
| 117 ScreenRotation::GetProperties() { | |
| 118 static LayerAnimationElement::AnimatableProperties properties; | |
| 119 if (properties.empty()) | |
| 120 properties.insert(LayerAnimationElement::TRANSFORM); | |
| 121 return properties; | |
| 122 } | |
| 123 | |
| 124 } // namespace ui | |
| OLD | NEW |