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