aseprite/src/commands/cmd_duplicate_layer.cpp

115 lines
3.3 KiB
C++
Raw Normal View History

2007-11-16 18:25:45 +00:00
/* ASE - Allegro Sprite Editor
2011-01-18 23:49:53 +00:00
* Copyright (C) 2001-2011 David Capello
2007-09-23 20:13:58 +00:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "gui/jinete.h"
2007-09-23 20:13:58 +00:00
#include "commands/command.h"
#include "console.h"
2009-12-11 14:53:05 +00:00
#include "app.h"
#include "modules/gui.h"
#include "raster/layer.h"
2007-09-23 20:13:58 +00:00
#include "raster/sprite.h"
#include "raster/undo.h"
#include "sprite_wrappers.h"
2007-09-23 20:13:58 +00:00
static Layer *duplicate_layer(Sprite* sprite);
//////////////////////////////////////////////////////////////////////
// duplicate_layer
class DuplicateLayerCommand : public Command
{
public:
DuplicateLayerCommand();
Command* clone() const { return new DuplicateLayerCommand(*this); }
protected:
bool onEnabled(Context* context);
void onExecute(Context* context);
};
DuplicateLayerCommand::DuplicateLayerCommand()
2011-01-20 23:46:58 +00:00
: Command("DuplicateLayer",
"Duplicate Layer",
CmdRecordableFlag)
{
}
bool DuplicateLayerCommand::onEnabled(Context* context)
{
const CurrentSpriteReader sprite(context);
return sprite && sprite->getCurrentLayer();
}
void DuplicateLayerCommand::onExecute(Context* context)
2007-09-23 20:13:58 +00:00
{
CurrentSpriteWriter sprite(context);
if (duplicate_layer(sprite) != NULL)
update_screen_for_sprite(sprite);
}
static Layer* duplicate_layer(Sprite* sprite)
{
/* open undo */
if (sprite->getUndo()->isEnabled()) {
sprite->getUndo()->setLabel("Layer Duplication");
sprite->getUndo()->undo_open();
2008-03-27 16:58:14 +00:00
}
Layer* layer_copy = sprite->getCurrentLayer()->duplicate_for(sprite);
if (!layer_copy) {
if (sprite->getUndo()->isEnabled())
sprite->getUndo()->undo_close();
Console console;
console.printf("Not enough memory");
return NULL;
}
layer_copy->set_background(false);
layer_copy->set_moveable(true);
layer_copy->setName(layer_copy->getName() + " Copy");
/* add the new layer in the sprite */
if (sprite->getUndo()->isEnabled())
sprite->getUndo()->undo_add_layer(sprite->getCurrentLayer()->get_parent(), layer_copy);
sprite->getCurrentLayer()->get_parent()->add_layer(layer_copy);
if (sprite->getUndo()->isEnabled()) {
sprite->getUndo()->undo_move_layer(layer_copy);
sprite->getUndo()->undo_set_layer(sprite);
sprite->getUndo()->undo_close();
}
sprite->getCurrentLayer()->get_parent()->move_layer(layer_copy, sprite->getCurrentLayer());
sprite->setCurrentLayer(layer_copy);
return layer_copy;
2007-09-23 20:13:58 +00:00
}
//////////////////////////////////////////////////////////////////////
// CommandFactory
2011-01-20 23:46:58 +00:00
Command* CommandFactory::createDuplicateLayerCommand()
{
return new DuplicateLayerCommand;
}