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.
|
2015-01-19 09:05:33 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/cmd/flatten_layers.h"
|
|
|
|
|
|
|
|
#include "app/cmd/add_layer.h"
|
2015-09-22 01:19:39 +08:00
|
|
|
#include "app/cmd/set_layer_name.h"
|
2015-01-19 09:05:33 +08:00
|
|
|
#include "app/cmd/configure_background.h"
|
|
|
|
#include "app/cmd/copy_rect.h"
|
|
|
|
#include "app/cmd/remove_layer.h"
|
|
|
|
#include "app/cmd/remove_layer.h"
|
2015-07-27 19:20:15 +08:00
|
|
|
#include "app/cmd/set_layer_flags.h"
|
2015-03-20 04:37:22 +08:00
|
|
|
#include "app/cmd/unlink_cel.h"
|
2015-01-19 09:05:33 +08:00
|
|
|
#include "app/document.h"
|
|
|
|
#include "doc/cel.h"
|
|
|
|
#include "doc/layer.h"
|
|
|
|
#include "doc/primitives.h"
|
|
|
|
#include "doc/sprite.h"
|
|
|
|
#include "render/render.h"
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
namespace cmd {
|
|
|
|
|
|
|
|
FlattenLayers::FlattenLayers(Sprite* sprite)
|
|
|
|
: WithSprite(sprite)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlattenLayers::onExecute()
|
|
|
|
{
|
|
|
|
Sprite* sprite = this->sprite();
|
|
|
|
app::Document* doc = static_cast<app::Document*>(sprite->document());
|
|
|
|
|
|
|
|
// Create a temporary image.
|
|
|
|
ImageRef image(Image::create(sprite->pixelFormat(),
|
|
|
|
sprite->width(),
|
|
|
|
sprite->height()));
|
|
|
|
|
2015-09-22 01:19:39 +08:00
|
|
|
LayerImage* flatLayer; // The layer onto which everything will be flattened.
|
|
|
|
color_t bgcolor; // The background color to use for flatLayer.
|
2015-09-22 04:47:42 +08:00
|
|
|
|
2015-09-22 01:19:39 +08:00
|
|
|
flatLayer = sprite->backgroundLayer();
|
2015-09-22 04:47:42 +08:00
|
|
|
if (flatLayer && flatLayer->isVisible()) {
|
2015-09-22 01:19:39 +08:00
|
|
|
// There exists a visible background layer, so we will flatten onto that.
|
|
|
|
bgcolor = doc->bgColor(flatLayer);
|
2015-01-19 09:05:33 +08:00
|
|
|
}
|
2015-09-22 04:47:42 +08:00
|
|
|
else {
|
2015-09-22 01:19:39 +08:00
|
|
|
// Create a new transparent layer to flatten everything onto.
|
|
|
|
flatLayer = new LayerImage(sprite);
|
|
|
|
ASSERT(flatLayer->isVisible());
|
|
|
|
executeAndAdd(new cmd::AddLayer(sprite->folder(), flatLayer, nullptr));
|
|
|
|
executeAndAdd(new cmd::SetLayerName(flatLayer, "Flattened"));
|
2015-07-27 19:34:05 +08:00
|
|
|
bgcolor = sprite->transparentColor();
|
2015-09-22 01:19:39 +08:00
|
|
|
}
|
2015-07-27 19:34:05 +08:00
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
render::Render render;
|
|
|
|
render.setBgType(render::BgType::NONE);
|
|
|
|
|
|
|
|
// Copy all frames to the background.
|
|
|
|
for (frame_t frame(0); frame<sprite->totalFrames(); ++frame) {
|
|
|
|
// Clear the image and render this frame.
|
2015-04-03 07:42:43 +08:00
|
|
|
clear_image(image.get(), bgcolor);
|
|
|
|
render.renderSprite(image.get(), sprite, frame);
|
2015-01-19 09:05:33 +08:00
|
|
|
|
2015-02-09 22:40:43 +08:00
|
|
|
// TODO Keep cel links when possible
|
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
ImageRef cel_image;
|
2015-09-22 01:19:39 +08:00
|
|
|
Cel* cel = flatLayer->cel(frame);
|
2015-01-19 09:05:33 +08:00
|
|
|
if (cel) {
|
2015-03-20 04:37:22 +08:00
|
|
|
if (cel->links())
|
|
|
|
executeAndAdd(new cmd::UnlinkCel(cel));
|
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
cel_image = cel->imageRef();
|
|
|
|
ASSERT(cel_image);
|
|
|
|
|
2015-04-03 07:42:43 +08:00
|
|
|
executeAndAdd(new cmd::CopyRect(cel_image.get(), image.get(),
|
2015-01-19 09:05:33 +08:00
|
|
|
gfx::Clip(0, 0, image->bounds())));
|
|
|
|
}
|
|
|
|
else {
|
2015-04-03 07:42:43 +08:00
|
|
|
cel_image.reset(Image::createCopy(image.get()));
|
2015-01-19 09:05:33 +08:00
|
|
|
cel = new Cel(frame, cel_image);
|
2015-09-22 01:19:39 +08:00
|
|
|
flatLayer->addCel(cel);
|
2015-01-19 09:05:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete old layers.
|
|
|
|
LayerList layers = sprite->folder()->getLayersList();
|
|
|
|
for (Layer* layer : layers)
|
2015-09-22 01:19:39 +08:00
|
|
|
if (layer != flatLayer)
|
2015-01-19 09:05:33 +08:00
|
|
|
executeAndAdd(new cmd::RemoveLayer(layer));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cmd
|
|
|
|
} // namespace app
|