2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
|
|
|
// Copyright (C) 2001-2015 David Capello
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License version 2 as
|
|
|
|
// published by the Free Software Foundation.
|
2012-01-06 06:45:03 +08:00
|
|
|
|
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"
|
2015-01-19 09:05:33 +08:00
|
|
|
#include "app/cmd/add_cel.h"
|
|
|
|
#include "app/cmd/replace_image.h"
|
|
|
|
#include "app/cmd/set_cel_position.h"
|
2015-01-22 22:36:05 +08:00
|
|
|
#include "app/cmd/unlink_cel.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/context_access.h"
|
|
|
|
#include "app/document.h"
|
|
|
|
#include "app/document_api.h"
|
|
|
|
#include "app/modules/gui.h"
|
2015-01-19 09:05:33 +08:00
|
|
|
#include "app/transaction.h"
|
2013-03-31 06:53:52 +08:00
|
|
|
#include "base/unique_ptr.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/cel.h"
|
|
|
|
#include "doc/image.h"
|
|
|
|
#include "doc/layer.h"
|
|
|
|
#include "doc/primitives.h"
|
|
|
|
#include "doc/sprite.h"
|
2014-12-28 22:06:11 +08:00
|
|
|
#include "render/render.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-08-15 10:07:47 +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());
|
2015-01-19 09:05:33 +08:00
|
|
|
Transaction transaction(writer.context(), "Merge Down Layer", ModifyDocument);
|
2013-03-12 07:29:45 +08:00
|
|
|
Layer* src_layer = writer.layer();
|
|
|
|
Layer* dst_layer = src_layer->getPrevious();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2014-12-29 07:39:11 +08:00
|
|
|
for (frame_t frpos = 0; frpos<sprite->totalFrames(); ++frpos) {
|
2012-01-06 06:45:03 +08:00
|
|
|
// Get frames
|
2014-12-29 08:04:08 +08:00
|
|
|
Cel* src_cel = src_layer->cel(frpos);
|
|
|
|
Cel* dst_cel = dst_layer->cel(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)
|
2014-07-30 12:28:15 +08:00
|
|
|
src_image = src_cel->image();
|
2012-01-06 06:45:03 +08:00
|
|
|
else
|
|
|
|
src_image = NULL;
|
|
|
|
|
2015-01-04 21:58:14 +08:00
|
|
|
ImageRef dst_image;
|
|
|
|
if (dst_cel)
|
|
|
|
dst_image = dst_cel->imageRef();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// With source image?
|
2015-01-04 21:58:14 +08:00
|
|
|
if (src_image) {
|
2012-01-06 06:45:03 +08:00
|
|
|
// No destination image
|
2015-04-03 07:42:43 +08:00
|
|
|
if (!dst_image) { // Only a transparent layer can have a null cel
|
2012-01-06 06:45:03 +08:00
|
|
|
// Copy this cel to the destination layer...
|
|
|
|
|
|
|
|
// Creating a copy of the image
|
2015-01-04 21:58:14 +08:00
|
|
|
dst_image.reset(Image::createCopy(src_image));
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Creating a copy of the cell
|
2015-01-04 21:58:14 +08:00
|
|
|
dst_cel = new Cel(frpos, dst_image);
|
2014-07-30 12:28:15 +08:00
|
|
|
dst_cel->setPosition(src_cel->x(), src_cel->y());
|
|
|
|
dst_cel->setOpacity(src_cel->opacity());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
transaction.execute(new cmd::AddCel(dst_layer, dst_cel));
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
2013-03-31 06:53:52 +08:00
|
|
|
// With destination
|
2012-01-06 06:45:03 +08:00
|
|
|
else {
|
2015-01-19 09:05:33 +08:00
|
|
|
gfx::Rect bounds;
|
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()) {
|
2015-01-19 09:05:33 +08:00
|
|
|
bounds = sprite->bounds();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
2013-03-31 06:53:52 +08:00
|
|
|
// Merge down in a transparent layer
|
2012-01-06 06:45:03 +08:00
|
|
|
else {
|
2015-01-19 09:05:33 +08:00
|
|
|
bounds = src_cel->bounds().createUnion(dst_cel->bounds());
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 09:21:31 +08:00
|
|
|
doc::color_t bgcolor = app_get_color_to_clear_layer(dst_layer);
|
2014-05-08 11:30:36 +08:00
|
|
|
|
2015-04-03 07:42:43 +08:00
|
|
|
ImageRef new_image(doc::crop_image(
|
|
|
|
dst_image.get(),
|
2015-01-19 09:05:33 +08:00
|
|
|
bounds.x-dst_cel->x(),
|
|
|
|
bounds.y-dst_cel->y(),
|
|
|
|
bounds.w, bounds.h, bgcolor));
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-03-31 06:53:52 +08:00
|
|
|
// Merge src_image in new_image
|
2015-04-03 07:42:43 +08:00
|
|
|
render::composite_image(new_image.get(), src_image,
|
2015-01-19 09:05:33 +08:00
|
|
|
src_cel->x()-bounds.x,
|
|
|
|
src_cel->y()-bounds.y,
|
2014-12-28 22:06:11 +08:00
|
|
|
src_cel->opacity(),
|
|
|
|
static_cast<LayerImage*>(src_layer)->getBlendMode());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
transaction.execute(new cmd::SetCelPosition(dst_cel,
|
|
|
|
bounds.x, bounds.y));
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-01-22 22:36:05 +08:00
|
|
|
if (dst_cel->links())
|
|
|
|
transaction.execute(new cmd::UnlinkCel(dst_cel));
|
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
transaction.execute(new cmd::ReplaceImage(sprite,
|
|
|
|
dst_cel->imageRef(), new_image));
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
document->notifyLayerMergedDown(src_layer, dst_layer);
|
2015-01-19 09:05:33 +08:00
|
|
|
document->getApi(transaction).removeLayer(src_layer); // src_layer is deleted inside removeLayer()
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
transaction.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
|