2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2019-11-06 00:31:53 +08:00
|
|
|
// Copyright (C) 2019-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"
|
2024-09-21 04:25:09 +08:00
|
|
|
#include "app/cmd/flatten_layers.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"
|
2024-09-21 04:25:09 +08:00
|
|
|
#include "app/doc_range.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
2024-09-21 04:25:09 +08:00
|
|
|
#include "app/pref/preferences.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/layer.h"
|
|
|
|
#include "doc/sprite.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
|
|
|
};
|
|
|
|
|
2025-07-24 07:00:12 +08:00
|
|
|
MergeDownLayerCommand::MergeDownLayerCommand() : Command(CommandId::MergeDownLayer())
|
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();
|
2019-11-06 23:32:47 +08:00
|
|
|
if (!src_layer || !src_layer->isImage() || src_layer->isTilemap()) // TODO Add support to merge
|
|
|
|
// tilemaps (and groups!)
|
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();
|
2019-11-06 23:32:47 +08:00
|
|
|
if (!dst_layer || !dst_layer->isImage() || dst_layer->isTilemap()) // TODO Add support to merge
|
|
|
|
// tilemaps
|
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());
|
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
|
|
|
|
2023-12-13 08:34:30 +08:00
|
|
|
Tx tx(writer, friendlyName(), ModifyDocument);
|
2019-11-06 23:32:47 +08:00
|
|
|
|
2024-09-21 04:25:09 +08:00
|
|
|
DocRange range;
|
|
|
|
range.selectLayer(writer.layer());
|
|
|
|
range.selectLayer(dst_layer);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2024-09-21 04:25:09 +08:00
|
|
|
const bool newBlend = Preferences::instance().experimental.newBlend();
|
|
|
|
cmd::FlattenLayers::Options options;
|
|
|
|
options.newBlendMethod = newBlend;
|
|
|
|
options.inplace = true;
|
|
|
|
options.mergeDown = true;
|
|
|
|
options.dynamicCanvas = true;
|
|
|
|
|
|
|
|
tx(new cmd::FlattenLayers(sprite, range.selectedLayers(), options));
|
2018-08-21 03:00:59 +08:00
|
|
|
tx.commit();
|
2019-04-17 11:16:52 +08:00
|
|
|
|
2024-09-03 10:12:13 +08:00
|
|
|
update_screen_for_document(document);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createMergeDownLayerCommand()
|
|
|
|
{
|
|
|
|
return new MergeDownLayerCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|