| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/transform.h" | 5 #include "ui/gfx/transform.h" |
| 6 | |
| 7 #include <cmath> | |
| 8 | |
| 9 #include "ui/gfx/point.h" | 6 #include "ui/gfx/point.h" |
| 10 #include "ui/gfx/rect.h" | 7 #include "ui/gfx/rect.h" |
| 11 #include "ui/gfx/skia_util.h" | 8 #include "ui/gfx/skia_util.h" |
| 12 | 9 |
| 13 namespace ui { | 10 namespace ui { |
| 14 | 11 |
| 15 Transform::Transform() { | 12 Transform::Transform() { |
| 16 matrix_.reset(); | 13 matrix_.reset(); |
| 17 } | 14 } |
| 18 | 15 |
| 19 Transform::~Transform() {} | 16 Transform::~Transform() {} |
| 20 | 17 |
| 21 void Transform::SetRotate(float degree) { | 18 void Transform::SetRotate(float degree) { |
| 22 matrix_.setRotate(SkFloatToScalar(degree)); | 19 matrix_.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree)); |
| 23 } | 20 } |
| 24 | 21 |
| 25 void Transform::SetScaleX(float x) { | 22 void Transform::SetScaleX(float x) { |
| 26 matrix_.setScaleX(SkFloatToScalar(x)); | 23 matrix_.setScale( |
| 24 SkFloatToScalar(x), |
| 25 matrix_.get(1,1), |
| 26 matrix_.get(2,2)); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void Transform::SetScaleY(float y) { | 29 void Transform::SetScaleY(float y) { |
| 30 matrix_.setScaleY(SkFloatToScalar(y)); | 30 matrix_.setScale( |
| 31 matrix_.get(0,0), |
| 32 SkFloatToScalar(y), |
| 33 matrix_.get(2,2)); |
| 31 } | 34 } |
| 32 | 35 |
| 33 void Transform::SetScale(float x, float y) { | 36 void Transform::SetScale(float x, float y) { |
| 34 matrix_.setScale(SkFloatToScalar(x), SkFloatToScalar(y)); | 37 matrix_.setScale( |
| 38 SkFloatToScalar(x), |
| 39 SkFloatToScalar(y), |
| 40 matrix_.get(2, 2)); |
| 35 } | 41 } |
| 36 | 42 |
| 37 void Transform::SetTranslateX(float x) { | 43 void Transform::SetTranslateX(float x) { |
| 38 matrix_.setTranslateX(SkFloatToScalar(x)); | 44 matrix_.setTranslate( |
| 45 SkFloatToScalar(x), |
| 46 matrix_.get(1,3), |
| 47 matrix_.get(2,3)); |
| 39 } | 48 } |
| 40 | 49 |
| 41 void Transform::SetTranslateY(float y) { | 50 void Transform::SetTranslateY(float y) { |
| 42 matrix_.setTranslateY(SkFloatToScalar(y)); | 51 matrix_.setTranslate( |
| 52 matrix_.get(0,3), |
| 53 SkFloatToScalar(y), |
| 54 matrix_.get(2,3)); |
| 43 } | 55 } |
| 44 | 56 |
| 45 void Transform::SetTranslate(float x, float y) { | 57 void Transform::SetTranslate(float x, float y) { |
| 46 matrix_.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); | 58 matrix_.setTranslate( |
| 59 SkFloatToScalar(x), |
| 60 SkFloatToScalar(y), |
| 61 matrix_.get(2, 3)); |
| 47 } | 62 } |
| 48 | 63 |
| 49 void Transform::ConcatRotate(float degree) { | 64 void Transform::ConcatRotate(float degree) { |
| 50 matrix_.postRotate(SkFloatToScalar(degree)); | 65 SkMatrix44 rot; |
| 66 rot.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree)); |
| 67 matrix_.postConcat(rot); |
| 51 } | 68 } |
| 52 | 69 |
| 53 void Transform::ConcatScale(float x, float y) { | 70 void Transform::ConcatScale(float x, float y) { |
| 54 matrix_.postScale(SkFloatToScalar(x), SkFloatToScalar(y)); | 71 SkMatrix44 scale; |
| 72 scale.setScale(SkFloatToScalar(x), SkFloatToScalar(y), 1); |
| 73 matrix_.postConcat(scale); |
| 55 } | 74 } |
| 56 | 75 |
| 57 void Transform::ConcatTranslate(float x, float y) { | 76 void Transform::ConcatTranslate(float x, float y) { |
| 58 matrix_.postTranslate(SkFloatToScalar(x), SkFloatToScalar(y)); | 77 SkMatrix44 translate; |
| 78 translate.setTranslate(SkFloatToScalar(x), SkFloatToScalar(y), 0); |
| 79 matrix_.postConcat(translate); |
| 59 } | 80 } |
| 60 | 81 |
| 61 bool Transform::PreconcatTransform(const Transform& transform) { | 82 void Transform::PreconcatTransform(const Transform& transform) { |
| 62 return matrix_.setConcat(matrix_, transform.matrix_); | 83 if (!transform.matrix_.isIdentity()) { |
| 84 matrix_.preConcat(transform.matrix_); |
| 85 } |
| 63 } | 86 } |
| 64 | 87 |
| 65 bool Transform::ConcatTransform(const Transform& transform) { | 88 void Transform::ConcatTransform(const Transform& transform) { |
| 66 return matrix_.setConcat(transform.matrix_, matrix_); | 89 if (!transform.matrix_.isIdentity()) { |
| 90 matrix_.postConcat(transform.matrix_); |
| 91 } |
| 67 } | 92 } |
| 68 | 93 |
| 69 bool Transform::HasChange() const { | 94 bool Transform::HasChange() const { |
| 70 return !matrix_.isIdentity(); | 95 return !matrix_.isIdentity(); |
| 71 } | 96 } |
| 72 | 97 |
| 73 bool Transform::TransformPoint(gfx::Point* point) { | 98 bool Transform::TransformRect(gfx::Rect* rect) const { |
| 74 SkPoint skp; | |
| 75 matrix_.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); | |
| 76 point->SetPoint(static_cast<int>(std::floor(skp.fX)), | |
| 77 static_cast<int>(std::floor(skp.fY))); | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 bool Transform::TransformPointReverse(gfx::Point* point) { | |
| 82 SkMatrix inverse; | |
| 83 // TODO(sad): Try to avoid trying to invert the matrix. | |
| 84 if (matrix_.invert(&inverse)) { | |
| 85 SkPoint skp; | |
| 86 inverse.mapXY(SkIntToScalar(point->x()), SkIntToScalar(point->y()), &skp); | |
| 87 point->SetPoint(static_cast<int>(std::floor(skp.fX)), | |
| 88 static_cast<int>(std::floor(skp.fY))); | |
| 89 return true; | |
| 90 } | |
| 91 return false; | |
| 92 } | |
| 93 | |
| 94 bool Transform::TransformRect(gfx::Rect* rect) { | |
| 95 SkRect src = gfx::RectToSkRect(*rect); | 99 SkRect src = gfx::RectToSkRect(*rect); |
| 96 if (!matrix_.mapRect(&src)) | 100 const SkMatrix& matrix = matrix_; |
| 101 if (!matrix.mapRect(&src)) |
| 97 return false; | 102 return false; |
| 98 *rect = gfx::SkRectToRect(src); | 103 *rect = gfx::SkRectToRect(src); |
| 99 return true; | 104 return true; |
| 100 } | 105 } |
| 101 | 106 |
| 102 bool Transform::TransformRectReverse(gfx::Rect* rect) { | 107 bool Transform::TransformRectReverse(gfx::Rect* rect) const { |
| 103 SkMatrix inverse; | 108 SkMatrix44 inverse; |
| 104 if (!matrix_.invert(&inverse)) | 109 if (!matrix_.invert(&inverse)) |
| 105 return false; | 110 return false; |
| 106 | 111 const SkMatrix& matrix = inverse; |
| 107 SkRect src = gfx::RectToSkRect(*rect); | 112 SkRect src = gfx::RectToSkRect(*rect); |
| 108 if (!inverse.mapRect(&src)) | 113 if (!matrix.mapRect(&src)) |
| 109 return false; | 114 return false; |
| 110 *rect = gfx::SkRectToRect(src); | 115 *rect = gfx::SkRectToRect(src); |
| 111 return true; | 116 return true; |
| 112 } | 117 } |
| 113 | 118 |
| 114 } // namespace ui | 119 } // namespace ui |
| OLD | NEW |