[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 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 | #ifndef UI_GFX_TRANSFORM_H_ |
| 6 | #define UI_GFX_TRANSFORM_H_ |
| 7 | #pragma once |
| 8 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 9 | #include "third_party/skia/include/utils/SkMatrix44.h" |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 10 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 11 | namespace gfx { |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 12 | class Rect; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 13 | class Point; |
| 14 | class Point3f; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | namespace ui { |
| 18 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 19 | // 4x4 transformation matrix. Transform is cheap and explicitly allows |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 20 | // copy/assign. |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 21 | class Transform { |
| 22 | public: |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 23 | Transform(); |
| 24 | ~Transform(); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 25 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 26 | bool operator==(const Transform& rhs) const; |
| 27 | bool operator!=(const Transform& rhs) const; |
| 28 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 29 | // NOTE: The 'Set' functions overwrite the previously set transformation |
| 30 | // parameters. The 'Concat' functions apply a transformation (e.g. rotation, |
| 31 | // scale, translate) on top of the existing transforms, instead of overwriting |
| 32 | // them. |
| 33 | |
| 34 | // NOTE: The order of the 'Set' function calls do not matter. However, the |
| 35 | // order of the 'Concat' function calls do matter, especially when combined |
| 36 | // with the 'Set' functions. |
| 37 | |
| 38 | // Sets the rotation of the transformation. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 39 | void SetRotate(float degree); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 40 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 41 | // Sets the scaling parameters. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 42 | void SetScaleX(float x); |
| 43 | void SetScaleY(float y); |
| 44 | void SetScale(float x, float y); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 45 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 46 | // Sets the translation parameters. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 47 | void SetTranslateX(float x); |
| 48 | void SetTranslateY(float y); |
| 49 | void SetTranslate(float x, float y); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 50 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 51 | // Applies rotation on the current transformation. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 52 | void ConcatRotate(float degree); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 53 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 54 | // Applies scaling on current transform. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 55 | void ConcatScale(float x, float y); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 56 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 57 | // Applies translation on current transform. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 58 | void ConcatTranslate(float x, float y); |
| 59 | |
| 60 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 61 | // (i.e. 'this = this * transform;'). |
| 62 | void PreconcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 63 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 64 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 65 | // (i.e. 'this = transform * this;'). |
| 66 | void ConcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 67 | |
| 68 | // Does the transformation change anything? |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 69 | bool HasChange() const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 70 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 71 | // Applies the transformation on the point. Returns true if the point is |
| 72 | // transformed successfully. |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 73 | void TransformPoint(gfx::Point3f& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 74 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 75 | // Applies the transformation on the point. Returns true if the point is |
| 76 | // transformed successfully. Rounds the result to the nearest point. |
| 77 | void TransformPoint(gfx::Point& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 78 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 79 | // Applies the reverse transformation on the point. Returns true if the |
| 80 | // transformation can be inverted. |
| 81 | bool TransformPointReverse(gfx::Point3f& point) const; |
[email protected] | 463eb0e | 2011-05-10 03:11:04 | [diff] [blame] | 82 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 83 | // Applies the reverse transformation on the point. Returns true if the |
| 84 | // transformation can be inverted. Rounds the result to the nearest point. |
| 85 | bool TransformPointReverse(gfx::Point& point) const; |
| 86 | |
| 87 | // Applies transformation on the rectangle. Returns true if the transformed |
| 88 | // rectangle was axis aligned. If it returns false, rect will be the |
| 89 | // smallest axis aligned bounding box containg the transformed rect. |
| 90 | void TransformRect(gfx::Rect* rect) const; |
| 91 | |
| 92 | // Applies the reverse transformation on the rectangle. Returns true if |
| 93 | // the transformed rectangle was axis aligned. If it returns false, |
| 94 | // rect will be the smallest axis aligned bounding box containg the |
| 95 | // transformed rect. |
[email protected] | 3d19a53 | 2011-06-17 17:19:50 | [diff] [blame] | 96 | bool TransformRectReverse(gfx::Rect* rect) const; |
[email protected] | 277c7b7 | 2011-06-06 15:23:09 | [diff] [blame] | 97 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 98 | // Returns the underlying matrix. |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 99 | const SkMatrix44& matrix() const { return matrix_; } |
| 100 | SkMatrix44& matrix() { return matrix_; } |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 101 | |
| 102 | private: |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 103 | void TransformPointInternal(const SkMatrix44& xform, |
| 104 | gfx::Point& point) const; |
| 105 | |
| 106 | void TransformPointInternal(const SkMatrix44& xform, |
| 107 | gfx::Point3f& point) const; |
| 108 | |
| 109 | SkMatrix44 matrix_; |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 110 | |
| 111 | // copy/assign are allowed. |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 112 | }; |
| 113 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame^] | 114 | }// namespace ui |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 115 | |
| 116 | #endif // UI_GFX_TRANSFORM_H_ |