OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/interpolated_transform.h" | 5 #include "ui/gfx/interpolated_transform.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #ifndef M_PI | 9 #ifndef M_PI |
10 #define M_PI 3.14159265358979323846 | 10 #define M_PI 3.14159265358979323846 |
11 #endif | 11 #endif |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "ui/base/animation/tween.h" | 14 #include "ui/base/animation/tween.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 static const float EPSILON = 1e-6f; | 18 static const float EPSILON = 1e-6f; |
19 | 19 |
| 20 bool IsMultipleOfNinetyDegrees(float degrees) |
| 21 { |
| 22 float remainder = fabs(fmod(degrees, 90.0f)); |
| 23 return remainder < EPSILON || 90.0f - remainder < EPSILON; |
| 24 } |
| 25 |
| 26 // Returns false if |degrees| is not a multiple of ninety degrees or if |
| 27 // |rotation| is NULL. It does not affect |rotation| in this case. Otherwise |
| 28 // *rotation is set to be the appropriate sanitized rotation matrix. That is, |
| 29 // the rotation matrix corresponding to |degrees| which has entries that are all |
| 30 // either 0, 1 or -1. |
| 31 bool MassageRotationIfMultipleOfNinetyDegrees(ui::Transform* rotation, |
| 32 float degrees) |
| 33 { |
| 34 if (!IsMultipleOfNinetyDegrees(degrees) || !rotation) |
| 35 return false; |
| 36 |
| 37 ui::Transform transform; |
| 38 SkMatrix44& m = transform.matrix(); |
| 39 float degrees_by_ninety = degrees / 90.0f; |
| 40 |
| 41 int n = static_cast<int>(degrees_by_ninety > 0 |
| 42 ? floor(degrees_by_ninety + 0.5f) |
| 43 : ceil(degrees_by_ninety - 0.5f)); |
| 44 |
| 45 n %= 4; |
| 46 if (n < 0) |
| 47 n += 4; |
| 48 |
| 49 // n should now be in the range [0, 3] |
| 50 if (n == 1) { |
| 51 m.set3x3( 0, 1, 0, |
| 52 -1, 0, 0, |
| 53 0, 0, 1); |
| 54 } else if (n == 2) { |
| 55 m.set3x3(-1, 0, 0, |
| 56 0, -1, 0, |
| 57 0, 0, 1); |
| 58 } else if (n == 3) { |
| 59 m.set3x3( 0, -1, 0, |
| 60 1, 0, 0, |
| 61 0, 0, 1); |
| 62 } |
| 63 |
| 64 *rotation = transform; |
| 65 return true; |
| 66 } |
| 67 |
20 } // namespace | 68 } // namespace |
21 | 69 |
22 namespace ui { | 70 namespace ui { |
23 | 71 |
24 /////////////////////////////////////////////////////////////////////////////// | 72 /////////////////////////////////////////////////////////////////////////////// |
25 // InterpolatedTransform | 73 // InterpolatedTransform |
26 // | 74 // |
27 | 75 |
28 InterpolatedTransform::InterpolatedTransform() | 76 InterpolatedTransform::InterpolatedTransform() |
29 : start_time_(0.0f), | 77 : start_time_(0.0f), |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 float end_time) | 198 float end_time) |
151 : InterpolatedTransform(start_time, end_time), | 199 : InterpolatedTransform(start_time, end_time), |
152 start_degrees_(start_degrees), | 200 start_degrees_(start_degrees), |
153 end_degrees_(end_degrees) { | 201 end_degrees_(end_degrees) { |
154 } | 202 } |
155 | 203 |
156 InterpolatedRotation::~InterpolatedRotation() {} | 204 InterpolatedRotation::~InterpolatedRotation() {} |
157 | 205 |
158 ui::Transform InterpolatedRotation::InterpolateButDoNotCompose(float t) const { | 206 ui::Transform InterpolatedRotation::InterpolateButDoNotCompose(float t) const { |
159 ui::Transform result; | 207 ui::Transform result; |
160 result.SetRotate(ValueBetween(t, start_degrees_, end_degrees_)); | 208 float interpolated_degrees = ValueBetween(t, start_degrees_, end_degrees_); |
| 209 result.SetRotate(interpolated_degrees); |
| 210 if (t == 0.0f || t == 1.0f) |
| 211 MassageRotationIfMultipleOfNinetyDegrees(&result, interpolated_degrees); |
161 return result; | 212 return result; |
162 } | 213 } |
163 | 214 |
164 /////////////////////////////////////////////////////////////////////////////// | 215 /////////////////////////////////////////////////////////////////////////////// |
165 // InterpolatedAxisAngleRotation | 216 // InterpolatedAxisAngleRotation |
166 // | 217 // |
167 | 218 |
168 InterpolatedAxisAngleRotation::InterpolatedAxisAngleRotation( | 219 InterpolatedAxisAngleRotation::InterpolatedAxisAngleRotation( |
169 gfx::Point3f axis, | 220 gfx::Point3f axis, |
170 float start_degrees, | 221 float start_degrees, |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 } | 312 } |
262 | 313 |
263 InterpolatedTranslation::~InterpolatedTranslation() {} | 314 InterpolatedTranslation::~InterpolatedTranslation() {} |
264 | 315 |
265 ui::Transform | 316 ui::Transform |
266 InterpolatedTranslation::InterpolateButDoNotCompose(float t) const { | 317 InterpolatedTranslation::InterpolateButDoNotCompose(float t) const { |
267 ui::Transform result; | 318 ui::Transform result; |
268 // TODO(vollick) 3d xforms. | 319 // TODO(vollick) 3d xforms. |
269 result.SetTranslate(ValueBetween(t, start_pos_.x(), end_pos_.x()), | 320 result.SetTranslate(ValueBetween(t, start_pos_.x(), end_pos_.x()), |
270 ValueBetween(t, start_pos_.y(), end_pos_.y())); | 321 ValueBetween(t, start_pos_.y(), end_pos_.y())); |
271 | |
272 return result; | 322 return result; |
273 } | 323 } |
274 | 324 |
275 /////////////////////////////////////////////////////////////////////////////// | 325 /////////////////////////////////////////////////////////////////////////////// |
276 // InterpolatedConstantTransform | 326 // InterpolatedConstantTransform |
277 // | 327 // |
278 | 328 |
279 InterpolatedConstantTransform::InterpolatedConstantTransform( | 329 InterpolatedConstantTransform::InterpolatedConstantTransform( |
280 const ui::Transform& transform) | 330 const ui::Transform& transform) |
281 : InterpolatedTransform(), | 331 : InterpolatedTransform(), |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 | 439 |
390 rotation->SetChild(translation.release()); | 440 rotation->SetChild(translation.release()); |
391 scale->SetChild(rotation.release()); | 441 scale->SetChild(rotation.release()); |
392 transform_.reset(scale.release()); | 442 transform_.reset(scale.release()); |
393 } else { | 443 } else { |
394 transform_.reset(new InterpolatedConstantTransform(end_transform)); | 444 transform_.reset(new InterpolatedConstantTransform(end_transform)); |
395 } | 445 } |
396 } | 446 } |
397 | 447 |
398 } // namespace ui | 448 } // namespace ui |
OLD | NEW |