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