Implement duplicate layer (it was removed temporally with the introduction

of Document class).
This commit is contained in:
David Capello 2011-03-27 22:08:19 -03:00
parent e2a6ab7501
commit 92266b2a68
3 changed files with 47 additions and 14 deletions

View File

@ -27,6 +27,7 @@
#include "raster/layer.h"
#include "raster/sprite.h"
#include "undo/undo_history.h"
#include "undo_transaction.h"
//////////////////////////////////////////////////////////////////////
// Duplicate Layer command
@ -52,28 +53,32 @@ DuplicateLayerCommand::DuplicateLayerCommand()
bool DuplicateLayerCommand::onEnabled(Context* context)
{
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
ContextFlags::HasActiveLayer);
ContextFlags::HasActiveLayer |
ContextFlags::ActiveLayerIsImage);
}
void DuplicateLayerCommand::onExecute(Context* context)
{
#if 0 // TODO IMPLEMENT THIS
ActiveDocumentWriter document(context);
Sprite* sprite = document->getSprite();
UndoHistory* undo = document->getUndoHistory();
undo::UndoHistory* undo = document->getUndoHistory();
UndoTransaction undoTransaction(document, "Layer Duplication");
LayerImage* sourceLayer = static_cast<LayerImage*>(sprite->getCurrentLayer());
// Clone the layer
UniquePtr<Layer> newLayerPtr(sprite->getCurrentLayer()->clone());
newLayerPtr->set_background(false);
newLayerPtr->set_moveable(true);
newLayerPtr->setName(layer_copy->getName() + " Copy");
// Create a new layer
UniquePtr<LayerImage> newLayerPtr(new LayerImage(sprite));
// Copy the layer name
newLayerPtr->setName(sourceLayer->getName() + " Copy");
// Copy the layer content (cels + images)
document->copyLayerContent(sourceLayer, newLayerPtr);
// Add the new layer in the sprite.
if (undo->isEnabled())
undo->undo_add_layer(sprite->getCurrentLayer()->get_parent(), newLayerPtr);
undo->undo_add_layer(sourceLayer->get_parent(), newLayerPtr);
sprite->getCurrentLayer()->get_parent()->add_layer(newLayerPtr);
sourceLayer->get_parent()->add_layer(newLayerPtr);
// Release the pointer as it is owned by the sprite now
Layer* newLayer = newLayerPtr.release();
@ -85,11 +90,10 @@ void DuplicateLayerCommand::onExecute(Context* context)
undoTransaction.commit();
sprite->getCurrentLayer()->get_parent()->move_layer(newLayer, sprite->getCurrentLayer());
sourceLayer->get_parent()->move_layer(newLayer, sourceLayer);
sprite->setCurrentLayer(newLayer);
update_screen_for_document(document);
#endif
}
//////////////////////////////////////////////////////////////////////

View File

@ -287,7 +287,34 @@ void Document::setMaskVisible(bool visible)
}
//////////////////////////////////////////////////////////////////////
// Clonning
// Copying
void Document::copyLayerContent(const LayerImage* sourceLayer, LayerImage* destLayer) const
{
// copy cels
CelConstIterator it = sourceLayer->getCelBegin();
CelConstIterator end = sourceLayer->getCelEnd();
for (; it != end; ++it) {
const Cel* sourceCel = *it;
Cel* newCel = cel_new_copy(sourceCel);
ASSERT((sourceCel->image >= 0) &&
(sourceCel->image < sourceLayer->getSprite()->getStock()->size()));
const Image* sourceImage = sourceLayer->getSprite()->getStock()->getImage(sourceCel->image);
ASSERT(sourceImage != NULL);
Image* newImage = image_new_copy(sourceImage);
newCel->image = destLayer->getSprite()->getStock()->addImage(newImage);
if (m_undoHistory->isEnabled())
m_undoHistory->undo_add_image(destLayer->getSprite()->getStock(), newCel->image);
destLayer->addCel(newCel);
}
}
Document* Document::duplicate(DuplicateType type) const
{

View File

@ -29,6 +29,7 @@
class Cel;
class FormatOptions;
class Image;
class LayerImage;
class Mask;
class Mutex;
class Sprite;
@ -143,8 +144,9 @@ public:
void setMaskVisible(bool visible);
//////////////////////////////////////////////////////////////////////
// Clonning
// Copying
void copyLayerContent(const LayerImage* sourceLayer, LayerImage* destLayer) const;
Document* duplicate(DuplicateType type) const;
//////////////////////////////////////////////////////////////////////