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 "base/message_loop.h" | |
9 #include "base/task.h" | |
10 #include "ui/base/animation/slide_animation.h" | |
11 #include "ui/gfx/compositor/layer.h" | |
12 #include "ui/gfx/interpolated_transform.h" | |
13 #include "ui/gfx/rect.h" | |
14 #include "ui/gfx/transform.h" | |
15 #include "views/view.h" | |
16 | |
17 namespace { | |
18 const int kDefaultTransitionDurationMs = 500; | |
sky
2011/08/25 03:00:13
500ms is pretty long for an animation, are you sur
| |
19 | |
20 } // namespace | |
21 | |
22 namespace views { | |
23 | |
24 //////////////////////////////////////////////////////////////////////////////// | |
25 // ScreenRotation private: | |
sky
2011/08/25 03:00:13
private -> public
| |
26 // | |
27 | |
28 ScreenRotation::ScreenRotation(View* view, | |
29 float old_degrees, | |
30 float new_degrees) | |
31 : view_(view), | |
32 listener_(NULL), | |
33 old_degrees_(old_degrees), | |
34 new_degrees_(new_degrees), | |
35 last_t_(0.0), | |
36 done_(false), | |
37 duration_(kDefaultTransitionDurationMs), | |
38 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
39 Start(); | |
40 } | |
41 | |
42 ScreenRotation::~ScreenRotation() { | |
43 animation_.reset(); | |
sky
2011/08/25 03:00:13
Why do you need to explicitly stop the animation h
| |
44 } | |
45 | |
46 void ScreenRotation::Finalize() { | |
47 done_ = true; | |
sky
2011/08/25 03:00:13
DCHECK(!done_) ?
| |
48 view_->SetBounds(0, 0, new_size_.width(), new_size_.height()); | |
49 view_->SchedulePaint(); | |
50 if (listener_) | |
51 listener_->OnScreenRotationCompleted(this); | |
52 } | |
53 | |
54 void ScreenRotation::UpdateTarget(float degrees) { | |
55 if (new_degrees_ == degrees) | |
56 return; | |
57 | |
58 new_degrees_ = degrees; | |
59 Init(); | |
60 } | |
61 | |
62 void ScreenRotation::AnimationProgressed(const ui::Animation* anim) { | |
63 if (!interpolated_transform_.get() || !view_->layer()) | |
64 return; | |
65 | |
66 last_t_ = static_cast<float>(anim->GetCurrentValue()); | |
67 if (view_->painting_enabled()) | |
68 view_->set_painting_enabled(false); | |
sky
2011/08/25 03:00:13
Why do you need to disable painting here? Assuming
| |
69 | |
70 view_->layer()->SetTransform(interpolated_transform_->Interpolate(last_t_)); | |
71 view_->layer()->compositor()->SchedulePaint(); | |
72 } | |
73 | |
74 void ScreenRotation::AnimationEnded(const ui::Animation* anim) { | |
75 TRACE_EVENT_END0("ScreenRotation", "Start rotating"); | |
76 // TODO(vollick) massage matrix so that entries sufficiently close | |
77 // to 0, 1, or -1 are clamped to these values. The idea is to fight | |
78 // accumulated numeric error due to successive rotations. | |
79 if (view_->layer()) { | |
80 ui::Transform xform = view_->layer()->transform(); | |
81 gfx::Point origin; | |
82 xform.TransformPoint(origin); | |
83 ui::Transform translation; | |
84 translation.SetTranslate(new_origin_.x() - origin.x(), | |
85 new_origin_.y() - origin.y()); | |
86 xform.ConcatTransform(translation); | |
87 view_->layer()->SetTransform(xform); | |
88 view_->layer()->compositor()->SchedulePaint(); | |
89 } | |
90 view_->set_painting_enabled(true); | |
91 | |
92 // the last call to SchedulePaint may not have been processed yet, so if we | |
93 // set the bounds here, we may hang the ui in an 'almost-rotated' state for | |
94 // a moment while we layout. If we post a task to execute immediately, it | |
95 // will get processed after SchedulePaint, and we should be safe. | |
96 MessageLoop::current()->PostDelayedTask( | |
97 FROM_HERE, | |
98 method_factory_.NewRunnableMethod(&ScreenRotation::Finalize), | |
sky
2011/08/25 03:00:13
ick. This seems easy to mess up, but I don't curre
| |
99 1); | |
100 } | |
101 | |
102 void ScreenRotation::Init() { | |
103 if (!view_->layer()) { | |
104 view_->SetPaintToLayer(true); | |
105 view_->SchedulePaint(); | |
106 } | |
107 | |
108 // can't proceed without a layer. | |
109 if (!view_->layer()) | |
110 return; | |
sky
2011/08/25 03:00:13
If you early return here or at 121 should the anim
| |
111 | |
112 // cache the inital transformation matrix. | |
113 if (!old_transform_.get()) | |
114 old_transform_.reset(new ui::Transform(view_->layer()->transform())); | |
sky
2011/08/25 03:00:13
How come you don't always reset old_transform_?
| |
115 | |
116 int degrees = new_degrees_ - old_degrees_; | |
117 degrees = NormalizeAngle(degrees); | |
118 | |
119 // No rotation required. | |
120 if (degrees == 0) | |
121 return; | |
122 | |
123 gfx::Point old_pivot; | |
124 gfx::Point new_pivot; | |
125 int width = view_->layer()->bounds().width(); | |
126 int height = view_->layer()->bounds().height(); | |
127 | |
128 switch (degrees) { | |
129 case 90: | |
130 new_origin_ = new_pivot = gfx::Point(width, 0); | |
131 new_size_.SetSize(height, width); | |
132 break; | |
133 case -90: | |
134 new_origin_ = new_pivot = gfx::Point(0, height); | |
135 new_size_.SetSize(height, width); | |
136 break; | |
137 case 180: | |
138 duration_ = 750; | |
139 new_pivot = old_pivot = gfx::Point(width / 2, height / 2); | |
140 new_origin_.SetPoint(width, height); | |
141 new_size_.SetSize(width, height); | |
142 break; | |
143 } | |
144 | |
145 // Convert points to world space. | |
146 old_transform_->TransformPoint(old_pivot); | |
147 old_transform_->TransformPoint(new_pivot); | |
148 old_transform_->TransformPoint(new_origin_); | |
149 | |
150 scoped_ptr<ui::InterpolatedTransform> rotation( | |
151 new ui::InterpolatedTransformAboutPivot( | |
152 old_pivot, | |
153 new ui::InterpolatedRotation(0, degrees))); | |
154 | |
155 scoped_ptr<ui::InterpolatedTransform> translation( | |
156 new ui::InterpolatedTranslation( | |
157 gfx::Point(0, 0), | |
158 gfx::Point(new_pivot.x() - old_pivot.x(), | |
159 new_pivot.y() - old_pivot.y()))); | |
160 | |
161 float scale_factor = 0.9f; | |
162 scoped_ptr<ui::InterpolatedTransform> scale_down( | |
163 new ui::InterpolatedScale(1.0f, scale_factor, 0.0f, 0.5f)); | |
164 | |
165 scoped_ptr<ui::InterpolatedTransform> scale_up( | |
166 new ui::InterpolatedScale(1.0f, 1.0f / scale_factor, 0.5f, 1.0f)); | |
167 | |
168 scoped_ptr<ui::InterpolatedTransform> transition( | |
169 new ui::InterpolatedConstantTransform(*old_transform_)); | |
170 | |
171 scale_up->SetChild(scale_down.release()); | |
172 translation->SetChild(scale_up.release()); | |
173 rotation->SetChild(translation.release()); | |
174 transition->SetChild(rotation.release()); | |
175 | |
176 if (interpolated_transform_.get()) { | |
177 // We are in the middle of a transition. In this case, we need to create | |
178 // an interpolated transform that gets us from where we are to the target | |
179 // transform. | |
180 ui::Transform current = view_->layer()->transform(); | |
181 ui::Transform target = transition->Interpolate(1.0); | |
182 interpolated_transform_.reset(new ui::InterpolatedTRSTransform(current, | |
183 target, | |
184 last_t_, | |
185 1.0)); | |
186 } else { | |
187 interpolated_transform_.reset(transition.release()); | |
188 } | |
189 } | |
190 | |
191 void ScreenRotation::Start() { | |
192 TRACE_EVENT_BEGIN0("ScreenRotation", "Start rotating"); | |
193 Init(); | |
194 animation_.reset(new ui::SlideAnimation(this)); | |
195 animation_->SetTweenType(ui::Tween::LINEAR); | |
196 animation_->SetSlideDuration(duration_); | |
197 animation_->Show(); | |
198 } | |
199 | |
200 int ScreenRotation::NormalizeAngle(int degrees) { | |
201 while (degrees <= -180) degrees += 360; | |
202 while (degrees > 180) degrees -= 360; | |
203 return degrees; | |
204 } | |
205 | |
206 | |
207 } // namespace views | |
OLD | NEW |