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#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "doc/cel_data.h"
13
14#include "doc/image.h"
15#include "doc/layer.h"
16#include "doc/layer_tilemap.h"
17#include "doc/sprite.h"
18#include "doc/tileset.h"
19#include "gfx/rect.h"
20
21namespace doc {
22
23CelData::CelData(const ImageRef& image)
24 : WithUserData(ObjectType::CelData)
25 , m_image(image)
26 , m_opacity(255)
27 , m_bounds(0, 0,
28 image ? image->width(): 0,
29 image ? image->height(): 0)
30 , m_boundsF(nullptr)
31{
32}
33
34CelData::CelData(const CelData& celData)
35 : WithUserData(ObjectType::CelData)
36 , m_image(celData.m_image)
37 , m_opacity(celData.m_opacity)
38 , m_bounds(celData.m_bounds)
39 , m_boundsF(celData.m_boundsF ? std::make_unique<gfx::RectF>(*celData.m_boundsF):
40 nullptr)
41{
42}
43
44CelData::~CelData()
45{
46}
47
48void CelData::setImage(const ImageRef& image, Layer* layer)
49{
50 ASSERT(image.get());
51
52 m_image = image;
53 adjustBounds(layer);
54}
55
56void CelData::setPosition(const gfx::Point& pos)
57{
58 m_bounds.setOrigin(pos);
59 if (m_boundsF)
60 m_boundsF->setOrigin(gfx::PointF(pos));
61}
62
63void CelData::adjustBounds(Layer* layer)
64{
65 ASSERT(m_image);
66 if (m_image->pixelFormat() == IMAGE_TILEMAP) {
67 Tileset* tileset = nullptr;
68 if (layer && layer->isTilemap())
69 tileset = static_cast<LayerTilemap*>(layer)->tileset();
70 if (tileset) {
71 gfx::Size canvasSize =
72 tileset->grid().tilemapSizeToCanvas(
73 gfx::Size(m_image->width(),
74 m_image->height()));
75 m_bounds.w = canvasSize.w;
76 m_bounds.h = canvasSize.h;
77 return;
78 }
79 }
80 m_bounds.w = m_image->width();
81 m_bounds.h = m_image->height();
82}
83
84} // namespace doc
85