| 1 | // Aseprite Document Library |
|---|---|
| 2 | // Copyright (C) 2019-2022 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-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 DOC_CEL_DATA_H_INCLUDED |
| 9 | #define DOC_CEL_DATA_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "doc/image_ref.h" |
| 13 | #include "doc/object.h" |
| 14 | #include "doc/with_user_data.h" |
| 15 | #include "gfx/rect.h" |
| 16 | |
| 17 | #include <memory> |
| 18 | |
| 19 | namespace doc { |
| 20 | |
| 21 | class Layer; |
| 22 | class Tileset; |
| 23 | |
| 24 | class CelData : public WithUserData { |
| 25 | public: |
| 26 | CelData(const ImageRef& image); |
| 27 | CelData(const CelData& celData); |
| 28 | ~CelData(); |
| 29 | |
| 30 | gfx::Point position() const { return m_bounds.origin(); } |
| 31 | const gfx::Rect& bounds() const { return m_bounds; } |
| 32 | int opacity() const { return m_opacity; } |
| 33 | Image* image() const { return const_cast<Image*>(m_image.get()); }; |
| 34 | ImageRef imageRef() const { return m_image; } |
| 35 | |
| 36 | // Returns a rectangle with the bounds of the image (width/height |
| 37 | // of the image) in the position of the cel (useful to compare |
| 38 | // active tilemap bounds when we have to change the tilemap cel |
| 39 | // bounds). |
| 40 | gfx::Rect imageBounds() const { |
| 41 | return gfx::Rect(m_bounds.x, |
| 42 | m_bounds.y, |
| 43 | m_image->width(), |
| 44 | m_image->height()); |
| 45 | } |
| 46 | |
| 47 | void setImage(const ImageRef& image, Layer* layer); |
| 48 | void setPosition(const gfx::Point& pos); |
| 49 | |
| 50 | void setOpacity(int opacity) { |
| 51 | m_opacity = opacity; |
| 52 | } |
| 53 | |
| 54 | void setBounds(const gfx::Rect& bounds) { |
| 55 | ASSERT(bounds.w > 0); |
| 56 | ASSERT(bounds.h > 0); |
| 57 | m_bounds = bounds; |
| 58 | if (m_boundsF) |
| 59 | *m_boundsF = gfx::RectF(bounds); |
| 60 | } |
| 61 | |
| 62 | void setBoundsF(const gfx::RectF& boundsF) { |
| 63 | if (m_boundsF) |
| 64 | *m_boundsF = boundsF; |
| 65 | else |
| 66 | m_boundsF = std::make_unique<gfx::RectF>(boundsF); |
| 67 | |
| 68 | m_bounds = gfx::Rect(boundsF); |
| 69 | if (m_bounds.w <= 0) m_bounds.w = 1; |
| 70 | if (m_bounds.h <= 0) m_bounds.h = 1; |
| 71 | } |
| 72 | |
| 73 | const gfx::RectF& boundsF() const { |
| 74 | if (!m_boundsF) |
| 75 | m_boundsF = std::make_unique<gfx::RectF>(m_bounds); |
| 76 | return *m_boundsF; |
| 77 | } |
| 78 | |
| 79 | bool hasBoundsF() const { |
| 80 | return m_boundsF != nullptr; |
| 81 | } |
| 82 | |
| 83 | virtual int getMemSize() const override { |
| 84 | ASSERT(m_image); |
| 85 | return sizeof(CelData) + m_image->getMemSize(); |
| 86 | } |
| 87 | |
| 88 | void adjustBounds(Layer* layer); |
| 89 | |
| 90 | private: |
| 91 | ImageRef m_image; |
| 92 | int m_opacity; |
| 93 | gfx::Rect m_bounds; |
| 94 | |
| 95 | // Special bounds for reference layers that can have subpixel |
| 96 | // position. |
| 97 | mutable std::unique_ptr<gfx::RectF> m_boundsF; |
| 98 | }; |
| 99 | |
| 100 | typedef std::shared_ptr<CelData> CelDataRef; |
| 101 | |
| 102 | } // namespace doc |
| 103 | |
| 104 | #endif |
| 105 |