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 "views/animation/screen_rotation.h" |
| 6 |
| 7 #include "base/debug/trace_event.h" |
| 8 #include "ui/base/animation/slide_animation.h" |
| 9 #include "ui/gfx/interpolated_transform.h" |
| 10 #include "ui/gfx/rect.h" |
| 11 #include "ui/gfx/transform.h" |
| 12 #include "views/view.h" |
| 13 |
| 14 namespace views { |
| 15 |
| 16 //////////////////////////////////////////////////////////////////////////////// |
| 17 // ScreenRotation public: |
| 18 // |
| 19 |
| 20 void ScreenRotation::Perform(views::View* root, |
| 21 float old_degrees, |
| 22 float new_degrees) { |
| 23 if (old_degrees != new_degrees) |
| 24 new ScreenRotation(root, old_degrees, new_degrees); |
| 25 } |
| 26 |
| 27 //////////////////////////////////////////////////////////////////////////////// |
| 28 // ScreenRotation private: |
| 29 // |
| 30 |
| 31 ScreenRotation::ScreenRotation(views::View* root, |
| 32 float old_degrees, |
| 33 float new_degrees) |
| 34 : root_(root), |
| 35 old_degrees_(old_degrees), |
| 36 new_degrees_(new_degrees) { |
| 37 animation_.reset(new ui::SlideAnimation(this)); |
| 38 animation_->SetTweenType(ui::Tween::LINEAR); |
| 39 animation_->SetSlideDuration(1000); |
| 40 Start(); |
| 41 } |
| 42 |
| 43 void ScreenRotation::AnimationProgressed(const ui::Animation* anim) { |
| 44 if (!interpolated_transform_.get()) |
| 45 return; |
| 46 |
| 47 float t = static_cast<float>(anim->GetCurrentValue()); |
| 48 if (root_->IsPaintingEnabled()) |
| 49 root_->SetPaintingEnabled(false); |
| 50 root_->SetTransform(interpolated_transform_->Interpolate(t)); |
| 51 root_->ScheduleComposite(); |
| 52 } |
| 53 |
| 54 void ScreenRotation::AnimationEnded(const ui::Animation* anim) { |
| 55 TRACE_EVENT_END0("ScreenRotation", "Start rotating"); |
| 56 // TODO(vollick) massage matrix so that entries sufficiently close |
| 57 // to 0, 1, or -1 are clamped to these values. The idea is to fight |
| 58 // accumulated numeric error due to successive rotations. |
| 59 ui::Transform xform = root_->GetTransform(); |
| 60 gfx::Point origin; |
| 61 xform.TransformPoint(origin); |
| 62 ui::Transform translation; |
| 63 translation.SetTranslate(new_origin_.x() - origin.x(), |
| 64 new_origin_.y() - origin.y()); |
| 65 xform.ConcatTransform(translation); |
| 66 root_->SetTransform(xform); |
| 67 root_->SetBounds(0, 0, new_size_.width(), new_size_.height()); |
| 68 root_->SetPaintingEnabled(true); |
| 69 root_->SchedulePaint(); |
| 70 delete this; |
| 71 } |
| 72 |
| 73 int ScreenRotation::NormalizeAngle(int degrees) { |
| 74 while (degrees <= -180) degrees += 360; |
| 75 while (degrees > 180) degrees -= 360; |
| 76 return degrees; |
| 77 } |
| 78 |
| 79 void ScreenRotation::Start() { |
| 80 TRACE_EVENT_BEGIN0("ScreenRotation", "Start rotating"); |
| 81 if (!root_->layer()) { |
| 82 root_->SetPaintToLayer(true); |
| 83 root_->SchedulePaint(); |
| 84 } |
| 85 |
| 86 int degrees = new_degrees_ - old_degrees_; |
| 87 degrees = NormalizeAngle(degrees); |
| 88 |
| 89 // No rotation required. |
| 90 if (degrees == 0) |
| 91 return; |
| 92 |
| 93 gfx::Point old_pivot; |
| 94 gfx::Point new_pivot; |
| 95 |
| 96 switch (degrees) { |
| 97 case 90: |
| 98 new_origin_ = old_pivot = gfx::Point(root_->width(), 0); |
| 99 new_pivot.SetPoint(root_->width(), root_->height()); |
| 100 new_size_.SetSize(root_->height(), root_->width()); |
| 101 break; |
| 102 case -90: |
| 103 new_origin_ = new_pivot = gfx::Point(0, root_->height()); |
| 104 new_size_.SetSize(root_->height(), root_->width()); |
| 105 break; |
| 106 case 180: |
| 107 new_pivot = old_pivot = gfx::Point(root_->width() / 2, |
| 108 root_->height() / 2); |
| 109 new_origin_.SetPoint(root_->width(), root_->height()); |
| 110 new_size_.SetSize(root_->width(), root_->height()); |
| 111 break; |
| 112 } |
| 113 |
| 114 // Convert points to world space. |
| 115 const ui::Transform& xform = root_->GetTransform(); |
| 116 xform.TransformPoint(old_pivot); |
| 117 xform.TransformPoint(new_pivot); |
| 118 xform.TransformPoint(new_origin_); |
| 119 |
| 120 scoped_ptr<ui::InterpolatedTransform> rotation( |
| 121 new ui::InterpolatedTransformAboutPivot( |
| 122 old_pivot, |
| 123 new ui::InterpolatedRotation(0, degrees))); |
| 124 |
| 125 scoped_ptr<ui::InterpolatedTransform> translation( |
| 126 new ui::InterpolatedTranslation( |
| 127 gfx::Point(0, 0), |
| 128 gfx::Point(new_pivot.x() - old_pivot.x(), |
| 129 new_pivot.y() - old_pivot.y()))); |
| 130 |
| 131 interpolated_transform_.reset( |
| 132 new ui::InterpolatedConstantTransform(xform)); |
| 133 |
| 134 rotation->SetChild(translation.release()); |
| 135 interpolated_transform_->SetChild(rotation.release()); |
| 136 |
| 137 animation_->Show(); |
| 138 } |
| 139 |
| 140 } // namespace views |
OLD | NEW |