1 | // Aseprite Document Library |
2 | // Copyright (c) 2019-2020 Igara Studio S.A. |
3 | // |
4 | // This file is released under the terms of the MIT license. |
5 | // Read LICENSE.txt for more information. |
6 | |
7 | #ifndef DOC_GRID_H_INCLUDED |
8 | #define DOC_GRID_H_INCLUDED |
9 | #pragma once |
10 | |
11 | #include "doc/image_ref.h" |
12 | #include "gfx/fwd.h" |
13 | #include "gfx/point.h" |
14 | #include "gfx/size.h" |
15 | |
16 | namespace doc { |
17 | |
18 | class Grid { |
19 | public: |
20 | Grid(const gfx::Size& sz = gfx::Size(16, 16)) |
21 | : m_tileSize(sz) |
22 | , m_origin(0, 0) |
23 | , m_tileCenter(gfx::Point(sz.w/2, sz.h/2)) |
24 | , m_tileOffset(gfx::Point(sz.w, sz.h)) |
25 | , m_oddRowOffset(0, 0) |
26 | , m_oddColOffset(0, 0) |
27 | , m_mask(nullptr) { } |
28 | |
29 | static Grid MakeRect(const gfx::Size& sz); |
30 | |
31 | bool isEmpty() const { return m_tileSize.w == 0 || m_tileSize.h == 0; } |
32 | |
33 | gfx::Size tileSize() const { return m_tileSize; } |
34 | gfx::Point origin() const { return m_origin; } |
35 | gfx::Point tileCenter() const { return m_tileCenter; } |
36 | gfx::Point tileOffset() const { return m_tileOffset; } |
37 | gfx::Point oddRowOffset() const { return m_oddRowOffset; } |
38 | gfx::Point oddColOffset() const { return m_oddColOffset; } |
39 | |
40 | void tileSize(const gfx::Size& tileSize) { m_tileSize = tileSize; } |
41 | void origin(const gfx::Point& origin) { m_origin = origin; } |
42 | void tileCenter(const gfx::Point& tileCenter) { m_tileCenter = tileCenter; } |
43 | void tileOffset(const gfx::Point& tileOffset) { m_tileOffset = tileOffset; } |
44 | void oddRowOffset(const gfx::Point& oddRowOffset) { m_oddRowOffset = oddRowOffset; } |
45 | void oddColOffset(const gfx::Point& oddColOffset) { m_oddColOffset = oddColOffset; } |
46 | |
47 | // Converts a tile position into a canvas position |
48 | gfx::Point tileToCanvas(const gfx::Point& tile) const; |
49 | gfx::Rect tileToCanvas(const gfx::Rect& tileBounds) const; |
50 | gfx::Region tileToCanvas(const gfx::Region& tileRgn); |
51 | |
52 | // Converts a canvas position/rectangle into a tile position |
53 | gfx::Point canvasToTile(const gfx::Point& canvasPoint) const; |
54 | gfx::Rect canvasToTile(const gfx::Rect& canvasBounds) const; |
55 | gfx::Region canvasToTile(const gfx::Region& canvasRgn); |
56 | |
57 | gfx::Size tilemapSizeToCanvas(const gfx::Size& tilemapSize) const; |
58 | gfx::Rect tileBoundsInCanvas(const gfx::Point& tile) const; |
59 | gfx::Rect alignBounds(const gfx::Rect& bounds) const; |
60 | |
61 | bool hasMask() const { return m_mask.get() != nullptr; } |
62 | doc::ImageRef mask() const { return m_mask; } |
63 | void setMask(const doc::ImageRef& mask) { m_mask = mask; } |
64 | |
65 | // Returns an array of tile positions that are touching the given region in the canvas |
66 | std::vector<gfx::Point> tilesInCanvasRegion(const gfx::Region& rgn) const; |
67 | |
68 | private: |
69 | gfx::Size m_tileSize; |
70 | gfx::Point m_origin; |
71 | gfx::Point m_tileCenter; |
72 | gfx::Point m_tileOffset; |
73 | gfx::Point m_oddRowOffset; |
74 | gfx::Point m_oddColOffset; |
75 | doc::ImageRef m_mask; |
76 | }; |
77 | |
78 | } // namespace doc |
79 | |
80 | #endif |
81 | |