2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2020-02-06 04:24:21 +08:00
|
|
|
// Copyright (C) 2020 Igara Studio S.A.
|
2018-08-09 04:27:26 +08:00
|
|
|
// Copyright (C) 2001-2018 David Capello
|
2015-02-12 23:16:25 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
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"
|
2018-07-07 22:54:44 +08:00
|
|
|
#include "app/doc.h"
|
2018-07-07 14:07:16 +08:00
|
|
|
#include "app/doc_api.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
2018-08-21 03:00:59 +08:00
|
|
|
#include "app/tx.h"
|
2015-07-23 20:19:08 +08:00
|
|
|
#include "doc/blend_internals.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();
|
|
|
|
|
|
|
|
protected:
|
2015-10-01 03:34:43 +08:00
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
MergeDownLayerCommand::MergeDownLayerCommand()
|
2017-12-02 02:10:21 +08:00
|
|
|
: Command(CommandId::MergeDownLayer(), CmdRecordableFlag)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MergeDownLayerCommand::onEnabled(Context* context)
|
|
|
|
{
|
2020-02-06 04:24:21 +08:00
|
|
|
if (!context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
|
|
|
ContextFlags::HasActiveSprite))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ContextReader reader(context);
|
|
|
|
const Sprite* sprite(reader.sprite());
|
2012-01-06 06:45:03 +08:00
|
|
|
if (!sprite)
|
|
|
|
return false;
|
|
|
|
|
2020-02-06 04:24:21 +08:00
|
|
|
const Layer* src_layer = reader.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;
|
|
|
|
|
2020-02-06 04:24:21 +08:00
|
|
|
const 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);
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* document(writer.document());
|
2013-03-12 07:29:45 +08:00
|
|
|
Sprite* sprite(writer.sprite());
|
2018-08-21 03:00:59 +08:00
|
|
|
Tx tx(writer.context(), "Merge Down Layer", ModifyDocument);
|
2015-07-23 20:19:08 +08:00
|
|
|
LayerImage* src_layer = static_cast<LayerImage*>(writer.layer());
|
2013-03-12 07:29:45 +08:00
|
|
|
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) {
|
2015-07-23 20:19:08 +08:00
|
|
|
int t;
|
|
|
|
int opacity;
|
|
|
|
opacity = MUL_UN8(src_cel->opacity(), src_layer->opacity(), t);
|
|
|
|
|
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());
|
2015-07-23 20:19:08 +08:00
|
|
|
dst_cel->setOpacity(opacity);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2018-08-21 03:00:59 +08:00
|
|
|
tx(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-07-27 23:24:54 +08:00
|
|
|
render::composite_image(
|
|
|
|
new_image.get(), src_image,
|
|
|
|
sprite->palette(src_cel->frame()),
|
2015-01-19 09:05:33 +08:00
|
|
|
src_cel->x()-bounds.x,
|
|
|
|
src_cel->y()-bounds.y,
|
2015-07-23 20:19:08 +08:00
|
|
|
opacity,
|
|
|
|
src_layer->blendMode());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2019-04-14 04:19:29 +08:00
|
|
|
// First unlink the dst_cel
|
2015-01-22 22:36:05 +08:00
|
|
|
if (dst_cel->links())
|
2018-08-21 03:00:59 +08:00
|
|
|
tx(new cmd::UnlinkCel(dst_cel));
|
2015-01-22 22:36:05 +08:00
|
|
|
|
2019-04-14 04:19:29 +08:00
|
|
|
// Then modify the dst_cel
|
|
|
|
tx(new cmd::SetCelPosition(dst_cel,
|
|
|
|
bounds.x, bounds.y));
|
|
|
|
|
2018-08-21 03:00:59 +08:00
|
|
|
tx(new cmd::ReplaceImage(sprite,
|
2015-01-19 09:05:33 +08:00
|
|
|
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);
|
2018-08-21 03:00:59 +08:00
|
|
|
document->getApi(tx).removeLayer(src_layer); // src_layer is deleted inside removeLayer()
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2018-08-21 03:00:59 +08:00
|
|
|
tx.commit();
|
2019-04-17 11:16:52 +08:00
|
|
|
|
|
|
|
#ifdef ENABLE_UI
|
|
|
|
if (context->isUIAvailable())
|
|
|
|
update_screen_for_document(document);
|
|
|
|
#endif
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createMergeDownLayerCommand()
|
|
|
|
{
|
|
|
|
return new MergeDownLayerCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|