OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 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 CC_BASE_SCALE_TRANSLATE2D_H_ |
| 6 #define CC_BASE_SCALE_TRANSLATE2D_H_ |
| 7 |
| 8 #include "cc/base/base_export.h" |
| 9 #include "ui/gfx/geometry/rect_f.h" |
| 10 #include "ui/gfx/geometry/vector2d_f.h" |
| 11 |
| 12 class SkCanvas; |
| 13 |
| 14 namespace cc { |
| 15 |
| 16 class CC_BASE_EXPORT ScaleTranslate2d { |
| 17 public: |
| 18 constexpr ScaleTranslate2d() : scale_(1.f) {} |
| 19 constexpr ScaleTranslate2d(float scale, const gfx::Vector2dF& translation) |
| 20 : scale_(scale), translation_(translation) {} |
| 21 |
| 22 bool operator==(const ScaleTranslate2d& other) const { |
| 23 return scale_ == other.scale_ && translation_ == other.translation_; |
| 24 } |
| 25 bool operator!=(const ScaleTranslate2d& other) const { |
| 26 return !(*this == other); |
| 27 } |
| 28 |
| 29 static ScaleTranslate2d PreScale(const ScaleTranslate2d& t, float scale) { |
| 30 return ScaleTranslate2d(t.scale_ * scale, t.translation_); |
| 31 } |
| 32 void PreScale(float scale) { *this = PreScale(*this, scale); } |
| 33 |
| 34 static ScaleTranslate2d PostScale(const ScaleTranslate2d& t, float scale) { |
| 35 return ScaleTranslate2d(t.scale_ * scale, |
| 36 gfx::ScaleVector2d(t.translation_, scale)); |
| 37 } |
| 38 void PostScale(float scale) { *this = PostScale(*this, scale); } |
| 39 |
| 40 static ScaleTranslate2d PreTranslate(const ScaleTranslate2d& t, |
| 41 const gfx::Vector2dF& translation) { |
| 42 return ScaleTranslate2d( |
| 43 t.scale_, gfx::ScaleVector2d(translation, t.scale_) + t.translation_); |
| 44 } |
| 45 void PreTranslate(const gfx::Vector2dF& translation) { |
| 46 *this = PreTranslate(*this, translation); |
| 47 } |
| 48 |
| 49 static ScaleTranslate2d PostTranslate(const ScaleTranslate2d& t, |
| 50 const gfx::Vector2dF& translation) { |
| 51 return ScaleTranslate2d(t.scale_, t.translation_ + translation); |
| 52 } |
| 53 void PostTranslate(const gfx::Vector2dF& translation) { |
| 54 *this = PostTranslate(*this, translation); |
| 55 } |
| 56 |
| 57 static ScaleTranslate2d Concat(const ScaleTranslate2d& post, |
| 58 const ScaleTranslate2d& pre) { |
| 59 return ScaleTranslate2d( |
| 60 pre.scale_ * post.scale_, |
| 61 gfx::ScaleVector2d(pre.translation_, post.scale_) + post.translation_); |
| 62 } |
| 63 void PreConcat(const ScaleTranslate2d& other) { |
| 64 *this = Concat(*this, other); |
| 65 } |
| 66 void PostConcat(const ScaleTranslate2d& other) { |
| 67 *this = Concat(other, *this); |
| 68 } |
| 69 |
| 70 static ScaleTranslate2d Inverse(const ScaleTranslate2d& t) { |
| 71 return ScaleTranslate2d(1 / t.scale_, |
| 72 gfx::ScaleVector2d(-t.translation_, 1 / t.scale_)); |
| 73 } |
| 74 void Invert() { *this = Inverse(*this); } |
| 75 |
| 76 gfx::PointF TransformPoint(const gfx::PointF& p) const { |
| 77 return gfx::ScalePoint(p, scale_) + translation_; |
| 78 } |
| 79 gfx::PointF TransformPointReverse(const gfx::PointF& p) const { |
| 80 return gfx::ScalePoint(p - translation_, 1.f / scale_); |
| 81 } |
| 82 |
| 83 gfx::RectF TransformRect(const gfx::RectF& r) const { |
| 84 return gfx::ScaleRect(r, scale_) + translation_; |
| 85 } |
| 86 gfx::RectF TransformRectReverse(const gfx::RectF& r) const { |
| 87 return gfx::ScaleRect(r - translation_, 1.f / scale_); |
| 88 } |
| 89 |
| 90 void ApplyToCanvas(SkCanvas* canvas) const; |
| 91 |
| 92 float scale() const { return scale_; } |
| 93 gfx::Vector2dF translation() const { return translation_; } |
| 94 |
| 95 std::string ToString() const { return "todo"; } |
| 96 |
| 97 private: |
| 98 // Scale is applied before translation, i.e. |
| 99 // this->Transform(p) == scale_ * p + translation_ |
| 100 float scale_; |
| 101 gfx::Vector2dF translation_; |
| 102 }; |
| 103 |
| 104 } // namespace cc |
| 105 |
| 106 #endif // CC_BASE_SCALE_TRANSLATE2D_H_ |
OLD | NEW |