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