2013-08-09 08:01:20 +08:00
|
|
|
/* Aseprite
|
2013-01-27 23:13:13 +08:00
|
|
|
* Copyright (C) 2001-2013 David Capello
|
2012-01-06 06:45:03 +08: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
|
|
|
|
*/
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/app.h"
|
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/context_access.h"
|
|
|
|
#include "app/document.h"
|
|
|
|
#include "app/document_api.h"
|
|
|
|
#include "app/modules/gui.h"
|
|
|
|
#include "app/undo_transaction.h"
|
|
|
|
#include "app/undoers/add_cel.h"
|
|
|
|
#include "app/undoers/add_image.h"
|
|
|
|
#include "app/undoers/remove_layer.h"
|
|
|
|
#include "app/undoers/replace_image.h"
|
|
|
|
#include "app/undoers/set_cel_position.h"
|
2013-03-31 06:53:52 +08:00
|
|
|
#include "base/unique_ptr.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "raster/cel.h"
|
|
|
|
#include "raster/image.h"
|
|
|
|
#include "raster/layer.h"
|
2013-11-10 06:59:05 +08:00
|
|
|
#include "raster/primitives.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "raster/sprite.h"
|
|
|
|
#include "raster/stock.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "ui/ui.h"
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
|
|
class MergeDownLayerCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
|
|
|
MergeDownLayerCommand();
|
2014-03-30 04:08:40 +08:00
|
|
|
Command* clone() const OVERRIDE { return new MergeDownLayerCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool onEnabled(Context* context);
|
|
|
|
void onExecute(Context* context);
|
|
|
|
};
|
|
|
|
|
|
|
|
MergeDownLayerCommand::MergeDownLayerCommand()
|
|
|
|
: Command("MergeDownLayer",
|
|
|
|
"Merge Down Layer",
|
|
|
|
CmdRecordableFlag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MergeDownLayerCommand::onEnabled(Context* context)
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
ContextWriter writer(context);
|
|
|
|
Sprite* sprite(writer.sprite());
|
2012-01-06 06:45:03 +08:00
|
|
|
if (!sprite)
|
|
|
|
return false;
|
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
Layer* src_layer = writer.layer();
|
2013-01-11 23:43:25 +08:00
|
|
|
if (!src_layer || !src_layer->isImage())
|
2012-01-06 06:45:03 +08:00
|
|
|
return false;
|
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
Layer* dst_layer = src_layer->getPrevious();
|
2013-01-11 23:43:25 +08:00
|
|
|
if (!dst_layer || !dst_layer->isImage())
|
2012-01-06 06:45:03 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MergeDownLayerCommand::onExecute(Context* context)
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
ContextWriter writer(context);
|
|
|
|
Document* document(writer.document());
|
|
|
|
Sprite* sprite(writer.sprite());
|
|
|
|
UndoTransaction undo(writer.context(), "Merge Down Layer", undo::ModifyDocument);
|
|
|
|
Layer* src_layer = writer.layer();
|
|
|
|
Layer* dst_layer = src_layer->getPrevious();
|
2012-07-09 08:09:09 +08:00
|
|
|
int index;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2012-07-09 08:09:09 +08:00
|
|
|
for (FrameNumber frpos(0); frpos<sprite->getTotalFrames(); ++frpos) {
|
2012-01-06 06:45:03 +08:00
|
|
|
// Get frames
|
2014-06-10 09:07:24 +08:00
|
|
|
Cel* src_cel = static_cast<LayerImage*>(src_layer)->getCel(frpos);
|
|
|
|
Cel* dst_cel = static_cast<LayerImage*>(dst_layer)->getCel(frpos);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Get images
|
2014-06-10 09:07:24 +08:00
|
|
|
Image* src_image;
|
2012-01-06 06:45:03 +08:00
|
|
|
if (src_cel != NULL)
|
|
|
|
src_image = sprite->getStock()->getImage(src_cel->getImage());
|
|
|
|
else
|
|
|
|
src_image = NULL;
|
|
|
|
|
2014-06-10 09:07:24 +08:00
|
|
|
Image* dst_image;
|
2012-01-06 06:45:03 +08:00
|
|
|
if (dst_cel != NULL)
|
2014-06-10 09:07:24 +08:00
|
|
|
dst_image = sprite->getStock()->getImage(dst_cel->getImage());
|
|
|
|
else
|
|
|
|
dst_image = NULL;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// With source image?
|
|
|
|
if (src_image != NULL) {
|
|
|
|
// No destination image
|
|
|
|
if (dst_image == NULL) { // Only a transparent layer can have a null cel
|
|
|
|
// Copy this cel to the destination layer...
|
|
|
|
|
|
|
|
// Creating a copy of the image
|
2014-06-10 09:07:24 +08:00
|
|
|
dst_image = Image::createCopy(src_image);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Adding it in the stock of images
|
2014-06-10 09:07:24 +08:00
|
|
|
index = sprite->getStock()->addImage(dst_image);
|
2012-07-08 12:25:26 +08:00
|
|
|
if (undo.isEnabled())
|
|
|
|
undo.pushUndoer(new undoers::AddImage(
|
|
|
|
undo.getObjects(), sprite->getStock(), index));
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Creating a copy of the cell
|
|
|
|
dst_cel = new Cel(frpos, index);
|
|
|
|
dst_cel->setPosition(src_cel->getX(), src_cel->getY());
|
|
|
|
dst_cel->setOpacity(src_cel->getOpacity());
|
|
|
|
|
2012-07-08 12:25:26 +08:00
|
|
|
if (undo.isEnabled())
|
|
|
|
undo.pushUndoer(new undoers::AddCel(undo.getObjects(), dst_layer, dst_cel));
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
static_cast<LayerImage*>(dst_layer)->addCel(dst_cel);
|
|
|
|
}
|
2013-03-31 06:53:52 +08:00
|
|
|
// With destination
|
2012-01-06 06:45:03 +08:00
|
|
|
else {
|
2014-05-08 11:30:36 +08:00
|
|
|
int x1, y1, x2, y2;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-03-31 06:53:52 +08:00
|
|
|
// Merge down in the background layer
|
2013-01-11 23:43:25 +08:00
|
|
|
if (dst_layer->isBackground()) {
|
2012-01-06 06:45:03 +08:00
|
|
|
x1 = 0;
|
|
|
|
y1 = 0;
|
|
|
|
x2 = sprite->getWidth();
|
|
|
|
y2 = sprite->getHeight();
|
|
|
|
}
|
2013-03-31 06:53:52 +08:00
|
|
|
// Merge down in a transparent layer
|
2012-01-06 06:45:03 +08:00
|
|
|
else {
|
|
|
|
x1 = MIN(src_cel->getX(), dst_cel->getX());
|
|
|
|
y1 = MIN(src_cel->getY(), dst_cel->getY());
|
2013-11-10 06:59:05 +08:00
|
|
|
x2 = MAX(src_cel->getX()+src_image->getWidth()-1, dst_cel->getX()+dst_image->getWidth()-1);
|
|
|
|
y2 = MAX(src_cel->getY()+src_image->getHeight()-1, dst_cel->getY()+dst_image->getHeight()-1);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 11:30:36 +08:00
|
|
|
raster::color_t bgcolor = app_get_color_to_clear_layer(dst_layer);
|
|
|
|
|
2014-06-10 09:07:24 +08:00
|
|
|
Image* new_image = raster::crop_image(dst_image,
|
|
|
|
x1-dst_cel->getX(),
|
|
|
|
y1-dst_cel->getY(),
|
|
|
|
x2-x1+1, y2-y1+1, bgcolor);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-03-31 06:53:52 +08:00
|
|
|
// Merge src_image in new_image
|
2013-11-10 06:59:05 +08:00
|
|
|
raster::composite_image(new_image, src_image,
|
|
|
|
src_cel->getX()-x1,
|
|
|
|
src_cel->getY()-y1,
|
|
|
|
src_cel->getOpacity(),
|
|
|
|
static_cast<LayerImage*>(src_layer)->getBlendMode());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2012-07-08 12:25:26 +08:00
|
|
|
if (undo.isEnabled())
|
|
|
|
undo.pushUndoer(new undoers::SetCelPosition(undo.getObjects(), dst_cel));
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
dst_cel->setPosition(x1, y1);
|
|
|
|
|
2012-07-08 12:25:26 +08:00
|
|
|
if (undo.isEnabled())
|
|
|
|
undo.pushUndoer(new undoers::ReplaceImage(undo.getObjects(),
|
2012-01-06 06:45:03 +08:00
|
|
|
sprite->getStock(), dst_cel->getImage()));
|
|
|
|
|
|
|
|
sprite->getStock()->replaceImage(dst_cel->getImage(), new_image);
|
2014-06-10 09:07:24 +08:00
|
|
|
delete dst_image;
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
document->notifyLayerMergedDown(src_layer, dst_layer);
|
|
|
|
document->getApi().removeLayer(src_layer); // src_layer is deleted inside removeLayer()
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2012-07-08 12:25:26 +08:00
|
|
|
undo.commit();
|
2012-01-06 06:45:03 +08:00
|
|
|
update_screen_for_document(document);
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createMergeDownLayerCommand()
|
|
|
|
{
|
|
|
|
return new MergeDownLayerCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|