1 // Aseprite Document Library
2 // Copyright (c) 2001-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 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include "doc/cel.h"
12
13 #include "gfx/rect.h"
14 #include "doc/image.h"
15 #include "doc/layer.h"
16 #include "doc/sprite.h"
17
18 namespace doc {
19
Cel(frame_t frame,const ImageRef & image)20 Cel::Cel(frame_t frame, const ImageRef& image)
21 : Object(ObjectType::Cel)
22 , m_layer(NULL)
23 , m_frame(frame)
24 , m_data(new CelData(image))
25 {
26 }
27
Cel(frame_t frame,const CelDataRef & celData)28 Cel::Cel(frame_t frame, const CelDataRef& celData)
29 : Object(ObjectType::Cel)
30 , m_layer(NULL)
31 , m_frame(frame)
32 , m_data(celData)
33 {
34 }
35
36 // static
createCopy(const Cel * other)37 Cel* Cel::createCopy(const Cel* other)
38 {
39 Cel* cel = new Cel(other->frame(),
40 ImageRef(Image::createCopy(other->image())));
41
42 cel->setPosition(other->position());
43 cel->setOpacity(other->opacity());
44 return cel;
45 }
46
47 // static
createLink(const Cel * other)48 Cel* Cel::createLink(const Cel* other)
49 {
50 return new Cel(other->frame(), other->dataRef());
51 }
52
setFrame(frame_t frame)53 void Cel::setFrame(frame_t frame)
54 {
55 ASSERT(m_layer == NULL);
56 m_frame = frame;
57 }
58
setDataRef(const CelDataRef & celData)59 void Cel::setDataRef(const CelDataRef& celData)
60 {
61 ASSERT(celData);
62 m_data = celData;
63 }
64
setPosition(int x,int y)65 void Cel::setPosition(int x, int y)
66 {
67 setPosition(gfx::Point(x, y));
68 }
69
setPosition(const gfx::Point & pos)70 void Cel::setPosition(const gfx::Point& pos)
71 {
72 m_data->setPosition(pos);
73 }
74
setBounds(const gfx::Rect & bounds)75 void Cel::setBounds(const gfx::Rect& bounds)
76 {
77 m_data->setBounds(bounds);
78 }
79
setBoundsF(const gfx::RectF & bounds)80 void Cel::setBoundsF(const gfx::RectF& bounds)
81 {
82 m_data->setBoundsF(bounds);
83 }
84
setOpacity(int opacity)85 void Cel::setOpacity(int opacity)
86 {
87 m_data->setOpacity(opacity);
88 }
89
document() const90 Document* Cel::document() const
91 {
92 ASSERT(m_layer);
93 if (m_layer && m_layer->sprite())
94 return m_layer->sprite()->document();
95 else
96 return NULL;
97 }
98
sprite() const99 Sprite* Cel::sprite() const
100 {
101 ASSERT(m_layer);
102 if (m_layer)
103 return m_layer->sprite();
104 else
105 return NULL;
106 }
107
link() const108 Cel* Cel::link() const
109 {
110 ASSERT(m_data);
111 if (m_data.get() == NULL)
112 return NULL;
113
114 if (!m_data.unique()) {
115 for (frame_t fr=0; fr<m_frame; ++fr) {
116 Cel* possible = m_layer->cel(fr);
117 if (possible && possible->dataRef().get() == m_data.get())
118 return possible;
119 }
120 }
121
122 return NULL;
123 }
124
links() const125 std::size_t Cel::links() const
126 {
127 std::size_t links = 0;
128
129 Sprite* sprite = this->sprite();
130 for (frame_t fr=0; fr<sprite->totalFrames(); ++fr) {
131 Cel* cel = m_layer->cel(fr);
132 if (cel && cel != this && cel->dataRef().get() == m_data.get())
133 ++links;
134 }
135
136 return links;
137 }
138
setParentLayer(LayerImage * layer)139 void Cel::setParentLayer(LayerImage* layer)
140 {
141 m_layer = layer;
142 fixupImage();
143 }
144
fixupImage()145 void Cel::fixupImage()
146 {
147 // Change the mask color to the sprite mask color
148 if (m_layer && image())
149 image()->setMaskColor(m_layer->sprite()->transparentColor());
150 }
151
152 } // namespace doc
153