Implement duplicate sprite with all layers (it was removed

temporally with the introduction of Document class).
This commit is contained in:
David Capello 2011-03-28 23:31:16 -03:00
parent 4aa039d40b
commit 236ee6bb7b
3 changed files with 85 additions and 30 deletions

View File

@ -68,11 +68,11 @@ void DuplicateLayerCommand::onExecute(Context* context)
// Create a new layer // Create a new layer
UniquePtr<LayerImage> newLayerPtr(new LayerImage(sprite)); UniquePtr<LayerImage> newLayerPtr(new LayerImage(sprite));
// Copy the layer name
newLayerPtr->setName(sourceLayer->getName() + " Copy");
// Copy the layer content (cels + images) // Copy the layer content (cels + images)
document->copyLayerContent(sourceLayer, newLayerPtr); document->copyLayerContent(sourceLayer, document, newLayerPtr);
// Copy the layer name
newLayerPtr->setName(newLayerPtr->getName() + " Copy");
// Add the new layer in the sprite. // Add the new layer in the sprite.
if (undo->isEnabled()) if (undo->isEnabled())

View File

@ -288,15 +288,22 @@ void Document::setMaskVisible(bool visible)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Copying // Copying
void Document::copyLayerContent(const LayerImage* sourceLayer, LayerImage* destLayer) const void Document::copyLayerContent(const Layer* sourceLayer0, Document* destDoc, Layer* destLayer0) const
{ {
// Copy the layer name
destLayer0->setName(sourceLayer0->getName());
if (sourceLayer0->is_image() && destLayer0->is_image()) {
const LayerImage* sourceLayer = static_cast<const LayerImage*>(sourceLayer0);
LayerImage* destLayer = static_cast<LayerImage*>(destLayer0);
// copy cels // copy cels
CelConstIterator it = sourceLayer->getCelBegin(); CelConstIterator it = sourceLayer->getCelBegin();
CelConstIterator end = sourceLayer->getCelEnd(); CelConstIterator end = sourceLayer->getCelEnd();
for (; it != end; ++it) { for (; it != end; ++it) {
const Cel* sourceCel = *it; const Cel* sourceCel = *it;
Cel* newCel = new Cel(*sourceCel); UniquePtr<Cel> newCel(new Cel(*sourceCel));
ASSERT((sourceCel->getImage() >= 0) && ASSERT((sourceCel->getImage() >= 0) &&
(sourceCel->getImage() < sourceLayer->getSprite()->getStock()->size())); (sourceCel->getImage() < sourceLayer->getSprite()->getStock()->size()));
@ -308,19 +315,60 @@ void Document::copyLayerContent(const LayerImage* sourceLayer, LayerImage* destL
newCel->setImage(destLayer->getSprite()->getStock()->addImage(newImage)); newCel->setImage(destLayer->getSprite()->getStock()->addImage(newImage));
if (m_undoHistory->isEnabled()) if (destDoc->getUndoHistory()->isEnabled())
m_undoHistory->undo_add_image(destLayer->getSprite()->getStock(), newCel->getImage()); destDoc->getUndoHistory()->undo_add_image(destLayer->getSprite()->getStock(),
newCel->getImage());
destLayer->addCel(newCel); destLayer->addCel(newCel);
newCel.release();
}
}
else if (sourceLayer0->is_folder() && destLayer0->is_folder()) {
const LayerFolder* sourceLayer = static_cast<const LayerFolder*>(sourceLayer0);
LayerFolder* destLayer = static_cast<LayerFolder*>(destLayer0);
LayerConstIterator it = sourceLayer->get_layer_begin();
LayerConstIterator end = sourceLayer->get_layer_end();
for (; it != end; ++it) {
Layer* sourceChild = *it;
UniquePtr<Layer> destChild(NULL);
if (sourceChild->is_image()) {
destChild.reset(new LayerImage(destLayer->getSprite()));
copyLayerContent(sourceChild, destDoc, destChild);
}
else if (sourceChild->is_folder()) {
destChild.reset(new LayerFolder(destLayer->getSprite()));
copyLayerContent(sourceChild, destDoc, destChild);
}
else {
ASSERT(false);
}
ASSERT(destChild != NULL);
// Add the new layer in the sprite.
if (destDoc->getUndoHistory()->isEnabled())
destDoc->getUndoHistory()->undo_add_layer(destLayer, destChild);
destLayer->add_layer(destChild);
destChild.release();
}
}
else {
ASSERT(false && "Trying to copy two incompatible layers");
} }
} }
Document* Document::duplicate(DuplicateType type) const Document* Document::duplicate(DuplicateType type) const
{ {
const Sprite* sourceSprite = getSprite(); const Sprite* sourceSprite = getSprite();
UniquePtr<Sprite> spriteCopy(new Sprite(sourceSprite->getImgType(), UniquePtr<Sprite> spriteCopyPtr(new Sprite(sourceSprite->getImgType(),
sourceSprite->getWidth(), sourceSprite->getWidth(),
sourceSprite->getHeight(), sourceSprite->getPalette(0)->size())); sourceSprite->getHeight(), sourceSprite->getPalette(0)->size()));
UniquePtr<Document> documentCopy(new Document(spriteCopyPtr));
Sprite* spriteCopy = spriteCopyPtr.release();
spriteCopy->setTotalFrames(sourceSprite->getTotalFrames()); spriteCopy->setTotalFrames(sourceSprite->getTotalFrames());
spriteCopy->setCurrentFrame(sourceSprite->getCurrentFrame()); spriteCopy->setCurrentFrame(sourceSprite->getCurrentFrame());
@ -342,9 +390,20 @@ Document* Document::duplicate(DuplicateType type) const
switch (type) { switch (type) {
case DuplicateExactCopy: case DuplicateExactCopy:
// Disable the undo
documentCopy->getUndoHistory()->setEnabled(false);
// Copy the layer folder
copyLayerContent(getSprite()->getFolder(), documentCopy, spriteCopy->getFolder());
// Set as current layer the same layer as the source
{ {
// TODO IMPLEMENT THIS int index = sourceSprite->layerToIndex(sourceSprite->getCurrentLayer());
spriteCopy->setCurrentLayer(spriteCopy->indexToLayer(index));
} }
// Re-enable the undo
documentCopy->getUndoHistory()->setEnabled(true);
break; break;
case DuplicateWithFlattenLayers: case DuplicateWithFlattenLayers:
@ -370,9 +429,6 @@ Document* Document::duplicate(DuplicateType type) const
break; break;
} }
UniquePtr<Document> documentCopy(new Document(spriteCopy));
spriteCopy.release();
documentCopy->setMask(getMask()); documentCopy->setMask(getMask());
documentCopy->m_maskVisible = m_maskVisible; documentCopy->m_maskVisible = m_maskVisible;
documentCopy->m_preferred = m_preferred; documentCopy->m_preferred = m_preferred;

View File

@ -29,7 +29,7 @@
class Cel; class Cel;
class FormatOptions; class FormatOptions;
class Image; class Image;
class LayerImage; class Layer;
class Mask; class Mask;
class Mutex; class Mutex;
class Sprite; class Sprite;
@ -146,8 +146,7 @@ public:
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Copying // Copying
void copyLayerContent(const LayerImage* sourceLayer, LayerImage* destLayer) const; void copyLayerContent(const Layer* sourceLayer, Document* destDoc, Layer* destLayer) const;
Document* duplicate(DuplicateType type) const; Document* duplicate(DuplicateType type) const;
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////