2727#include " app/cmd/clear_cel.h"
2828#include " app/cmd/copy_rect.h"
2929#include " app/cmd/remove_cel.h"
30+ #include " app/cmd/set_cel_data.h"
31+ #include " app/cmd/unlink_cel.h"
3032#include " app/document.h"
3133#include " doc/cel.h"
3234#include " doc/layer.h"
@@ -70,8 +72,11 @@ void CopyCel::onExecute()
7072
7173 // Clear destination cel if it does exist. It'll be overriden by the
7274 // copy of srcCel.
73- if (dstCel)
75+ if (dstCel) {
76+ if (dstCel->links ())
77+ executeAndAdd (new cmd::UnlinkCel (dstCel));
7478 executeAndAdd (new cmd::ClearCel (dstCel));
79+ }
7580
7681 // Add empty frames until newFrame
7782 while (dstSprite->totalFrames () <= m_dstFrame)
@@ -83,35 +88,41 @@ void CopyCel::onExecute()
8388 if (dstCel)
8489 dstImage = dstCel->imageRef ();
8590
91+ bool createLink =
92+ (srcLayer == dstLayer && dstLayer->isContinuous ());
93+
94+ // For background layer
8695 if (dstLayer->isBackground ()) {
87- if (srcCel) {
88- ASSERT (dstImage);
89- if (dstImage) {
90- int blend = (srcLayer->isBackground () ?
91- BLEND_MODE_COPY : BLEND_MODE_NORMAL );
92-
93- ImageRef tmp (Image::createCopy (dstImage));
94- render::composite_image (tmp, srcImage,
95- srcCel->x (), srcCel->y (), 255 , blend);
96- executeAndAdd (new cmd::CopyRect (dstImage, tmp, gfx::Clip (tmp->bounds ())));
97- }
96+ ASSERT (dstCel);
97+ ASSERT (dstImage);
98+ if (!dstCel || !dstImage ||
99+ !srcCel || !srcImage)
100+ return ;
101+
102+ if (createLink) {
103+ executeAndAdd (new cmd::SetCelData (dstCel, srcCel->dataRef ()));
98104 }
99105 else {
100- ASSERT (dstCel);
101- if (dstCel)
102- executeAndAdd (new cmd::ClearCel (dstCel));
106+ int blend = (srcLayer->isBackground () ?
107+ BLEND_MODE_COPY : BLEND_MODE_NORMAL );
108+
109+ ImageRef tmp (Image::createCopy (dstImage));
110+ render::composite_image (tmp, srcImage,
111+ srcCel->x (), srcCel->y (), 255 , blend);
112+ executeAndAdd (new cmd::CopyRect (dstImage, tmp, gfx::Clip (tmp->bounds ())));
103113 }
104114 }
115+ // For transparent layers
105116 else {
106117 if (dstCel)
107118 executeAndAdd (new cmd::RemoveCel (dstCel));
108119
109120 if (srcCel) {
110- dstImage.reset (Image::createCopy (srcImage));
111-
112- dstCel = new Cel (*srcCel);
121+ if (createLink)
122+ dstCel = Cel::createLink (srcCel);
123+ else
124+ dstCel = Cel::createCopy (srcCel);
113125 dstCel->setFrame (m_dstFrame);
114- dstCel->setImage (dstImage);
115126
116127 executeAndAdd (new cmd::AddCel (dstLayer, dstCel));
117128 }
0 commit comments