| 1 | // Aseprite Render Library |
| 2 | // Copyright (c) 2020 Igara Studio S.A. |
| 3 | // Copyright (c) 2016 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef RENDER_PROJECTION_H_INCLUDED |
| 9 | #define RENDER_PROJECTION_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "doc/pixel_ratio.h" |
| 13 | #include "gfx/matrix.h" |
| 14 | #include "render/zoom.h" |
| 15 | |
| 16 | namespace render { |
| 17 | |
| 18 | class Projection { |
| 19 | public: |
| 20 | Projection() |
| 21 | : m_pixelRatio(1, 1), |
| 22 | m_zoom(1, 1) { |
| 23 | } |
| 24 | |
| 25 | Projection(const doc::PixelRatio& pixelRatio, |
| 26 | const Zoom& zoom) |
| 27 | : m_pixelRatio(pixelRatio), |
| 28 | m_zoom(zoom) { |
| 29 | } |
| 30 | |
| 31 | const doc::PixelRatio& pixelRatio() const { return m_pixelRatio; } |
| 32 | const Zoom& zoom() const { return m_zoom; } |
| 33 | |
| 34 | void setPixelRatio(const doc::PixelRatio& pixelRatio) { m_pixelRatio = pixelRatio; } |
| 35 | void setZoom(const Zoom& zoom) { m_zoom = zoom; } |
| 36 | |
| 37 | double scaleX() const { return m_zoom.scale() * m_pixelRatio.w; } |
| 38 | double scaleY() const { return m_zoom.scale() * m_pixelRatio.h; } |
| 39 | |
| 40 | template<typename T> |
| 41 | T applyX(T x) const { return m_zoom.apply<T>(x * T(m_pixelRatio.w)); } |
| 42 | |
| 43 | template<typename T> |
| 44 | T applyY(T y) const { return m_zoom.apply<T>(y * T(m_pixelRatio.h)); } |
| 45 | |
| 46 | template<typename T> |
| 47 | T removeX(T x) const { return m_zoom.remove<T>(x) / T(m_pixelRatio.w); } |
| 48 | |
| 49 | template<typename T> |
| 50 | T removeY(T y) const { return m_zoom.remove<T>(y) / T(m_pixelRatio.h); } |
| 51 | |
| 52 | template<typename T> |
| 53 | T removeXCeiling(T x) const { return T(m_zoom.removeCeiling(x)) / T(m_pixelRatio.w); } |
| 54 | |
| 55 | template<typename T> |
| 56 | T removeYCeiling(T y) const { return T(m_zoom.removeCeiling(y)) / T(m_pixelRatio.h); } |
| 57 | |
| 58 | gfx::Rect apply(const gfx::Rect& r) const { |
| 59 | int u = applyX(r.x); |
| 60 | int v = applyY(r.y); |
| 61 | return gfx::Rect(u, v, |
| 62 | applyX(r.x+r.w) - u, |
| 63 | applyY(r.y+r.h) - v); |
| 64 | } |
| 65 | |
| 66 | gfx::RectF apply(const gfx::RectF& r) const { |
| 67 | double u = applyX(r.x); |
| 68 | double v = applyY(r.y); |
| 69 | return gfx::RectF(u, v, |
| 70 | applyX(r.x+r.w) - u, |
| 71 | applyY(r.y+r.h) - v); |
| 72 | } |
| 73 | |
| 74 | gfx::Rect remove(const gfx::Rect& r) const { |
| 75 | int u = removeX(r.x); |
| 76 | int v = removeY(r.y); |
| 77 | return gfx::Rect(u, v, |
| 78 | removeX(r.x+r.w) - u, |
| 79 | removeY(r.y+r.h) - v); |
| 80 | } |
| 81 | |
| 82 | gfx::RectF remove(const gfx::RectF& r) const { |
| 83 | double u = removeX(r.x); |
| 84 | double v = removeY(r.y); |
| 85 | return gfx::RectF(u, v, |
| 86 | removeX(r.x+r.w) - u, |
| 87 | removeY(r.y+r.h) - v); |
| 88 | } |
| 89 | |
| 90 | gfx::Matrix scaleMatrix() const { |
| 91 | return gfx::Matrix::MakeScale(scaleX(), scaleY()); |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | doc::PixelRatio m_pixelRatio; |
| 96 | Zoom m_zoom; |
| 97 | }; |
| 98 | |
| 99 | } // namespace render |
| 100 | |
| 101 | #endif |
| 102 | |