2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2017-05-16 03:25:09 +08:00
|
|
|
// Copyright (C) 2001-2017 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
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/app.h"
|
2016-06-21 11:02:13 +08:00
|
|
|
#include "app/cmd/move_layer.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
2016-10-13 01:41:32 +08:00
|
|
|
#include "app/commands/commands.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/params.h"
|
|
|
|
#include "app/context_access.h"
|
2018-07-07 14:07:16 +08:00
|
|
|
#include "app/doc_api.h"
|
2012-06-16 10:37:59 +08:00
|
|
|
#include "app/find_widget.h"
|
|
|
|
#include "app/load_widget.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
2015-03-05 08:35:11 +08:00
|
|
|
#include "app/transaction.h"
|
2013-12-15 23:58:14 +08:00
|
|
|
#include "app/ui/main_window.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/status_bar.h"
|
2016-11-03 03:10:42 +08:00
|
|
|
#include "app/ui_context.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/layer.h"
|
2016-10-13 01:41:32 +08:00
|
|
|
#include "doc/primitives.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/sprite.h"
|
2017-05-20 02:49:31 +08:00
|
|
|
#include "render/ordered_dither.h"
|
2016-11-02 06:02:11 +08:00
|
|
|
#include "render/quantization.h"
|
2016-10-13 01:41:32 +08:00
|
|
|
#include "render/render.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "ui/ui.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2016-06-09 02:23:51 +08:00
|
|
|
#include "new_layer.xml.h"
|
|
|
|
|
2012-07-10 04:36:45 +08:00
|
|
|
#include <cstdio>
|
2015-03-05 08:35:11 +08:00
|
|
|
#include <cstring>
|
2012-07-10 04:36:45 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2012-06-18 09:02:54 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
using namespace ui;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class NewLayerCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
2016-10-05 06:55:30 +08:00
|
|
|
enum class Type { Layer, Group, ReferenceLayer };
|
|
|
|
enum class Place { AfterActiveLayer, Top };
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
NewLayerCommand();
|
2014-08-15 10:07:47 +08:00
|
|
|
Command* clone() const override { return new NewLayerCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
2015-03-12 02:40:22 +08:00
|
|
|
void onLoadParams(const Params& params) override;
|
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
private:
|
2016-06-09 02:23:51 +08:00
|
|
|
std::string getUniqueLayerName(const Sprite* sprite) const;
|
|
|
|
int getMaxLayerNum(const Layer* layer) const;
|
|
|
|
const char* layerPrefix() const;
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
std::string m_name;
|
2016-10-05 06:55:30 +08:00
|
|
|
Type m_type;
|
|
|
|
Place m_place;
|
|
|
|
bool m_ask;
|
2016-10-13 01:41:32 +08:00
|
|
|
bool m_fromFile;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NewLayerCommand::NewLayerCommand()
|
2017-12-02 02:10:21 +08:00
|
|
|
: Command(CommandId::NewLayer(), CmdRecordableFlag)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
m_name = "";
|
2016-10-05 06:55:30 +08:00
|
|
|
m_type = Type::Layer;
|
|
|
|
m_place = Place::AfterActiveLayer;
|
|
|
|
m_ask = false;
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
void NewLayerCommand::onLoadParams(const Params& params)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2015-03-12 02:40:22 +08:00
|
|
|
m_name = params.get("name");
|
2016-10-05 06:55:30 +08:00
|
|
|
m_type = Type::Layer;
|
|
|
|
if (params.get("group") == "true")
|
|
|
|
m_type = Type::Group;
|
|
|
|
else if (params.get("reference") == "true")
|
|
|
|
m_type = Type::ReferenceLayer;
|
|
|
|
|
|
|
|
m_ask = (params.get("ask") == "true");
|
2016-10-13 01:41:32 +08:00
|
|
|
m_fromFile = (params.get("from-file") == "true");
|
2016-10-05 06:55:30 +08:00
|
|
|
m_place = Place::AfterActiveLayer;
|
|
|
|
if (params.get("top") == "true")
|
|
|
|
m_place = Place::Top;
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NewLayerCommand::onEnabled(Context* context)
|
|
|
|
{
|
|
|
|
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
|
|
|
ContextFlags::HasActiveSprite);
|
|
|
|
}
|
|
|
|
|
2016-10-13 01:41:32 +08:00
|
|
|
namespace {
|
|
|
|
class Scoped { // TODO move this to base library
|
|
|
|
public:
|
|
|
|
Scoped(const std::function<void()>& func) : m_func(func) { }
|
|
|
|
~Scoped() { m_func(); }
|
|
|
|
private:
|
|
|
|
std::function<void()> m_func;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
void NewLayerCommand::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());
|
2012-01-06 06:45:03 +08:00
|
|
|
std::string name;
|
|
|
|
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* pasteDoc = nullptr;
|
2016-10-13 01:41:32 +08:00
|
|
|
Scoped destroyPasteDoc(
|
|
|
|
[&pasteDoc, context]{
|
|
|
|
if (pasteDoc) {
|
|
|
|
context->documents().remove(pasteDoc);
|
|
|
|
delete pasteDoc;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
// Default name (m_name is a name specified in params)
|
|
|
|
if (!m_name.empty())
|
|
|
|
name = m_name;
|
|
|
|
else
|
2016-06-09 02:23:51 +08:00
|
|
|
name = getUniqueLayerName(sprite);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2016-10-13 01:41:32 +08:00
|
|
|
// Select a file to copy its content
|
|
|
|
if (m_fromFile) {
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* oldActiveDocument = context->activeDocument();
|
2017-12-02 02:10:21 +08:00
|
|
|
Command* openFile = Commands::instance()->byId(CommandId::OpenFile());
|
2016-10-13 01:41:32 +08:00
|
|
|
Params params;
|
|
|
|
params.set("filename", "");
|
|
|
|
context->executeCommand(openFile, params);
|
|
|
|
|
|
|
|
// The user have selected another document.
|
2016-11-03 03:10:42 +08:00
|
|
|
if (oldActiveDocument != context->activeDocument()) {
|
2016-10-13 01:41:32 +08:00
|
|
|
pasteDoc = context->activeDocument();
|
2016-11-03 03:10:42 +08:00
|
|
|
static_cast<UIContext*>(context)
|
|
|
|
->setActiveDocument(oldActiveDocument);
|
|
|
|
}
|
2017-09-06 02:26:21 +08:00
|
|
|
// If the user didn't selected a new document, it means that the
|
|
|
|
// file selector dialog was canceled.
|
|
|
|
else
|
|
|
|
return;
|
2016-10-13 01:41:32 +08:00
|
|
|
}
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
// If params specify to ask the user about the name...
|
|
|
|
if (m_ask) {
|
|
|
|
// We open the window to ask the name
|
2016-06-09 02:23:51 +08:00
|
|
|
app::gen::NewLayer window;
|
|
|
|
window.name()->setText(name.c_str());
|
|
|
|
window.name()->setMinSize(gfx::Size(128, 0));
|
|
|
|
window.openWindowInForeground();
|
|
|
|
if (window.closer() != window.ok())
|
2012-01-06 06:45:03 +08:00
|
|
|
return;
|
|
|
|
|
2016-06-09 02:23:51 +08:00
|
|
|
name = window.name()->text();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2016-06-09 02:55:44 +08:00
|
|
|
LayerGroup* parent = sprite->root();
|
2015-05-04 22:45:08 +08:00
|
|
|
Layer* activeLayer = writer.layer();
|
2016-06-21 11:02:13 +08:00
|
|
|
SelectedLayers selLayers = writer.site()->selectedLayers();
|
2016-06-09 02:55:44 +08:00
|
|
|
if (activeLayer) {
|
|
|
|
if (activeLayer->isGroup() &&
|
|
|
|
activeLayer->isExpanded() &&
|
2016-10-05 06:55:30 +08:00
|
|
|
m_type != Type::Group) {
|
2016-06-09 02:55:44 +08:00
|
|
|
parent = static_cast<LayerGroup*>(activeLayer);
|
|
|
|
activeLayer = nullptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parent = activeLayer->parent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 23:30:23 +08:00
|
|
|
Layer* layer = nullptr;
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2016-06-09 02:23:51 +08:00
|
|
|
Transaction transaction(
|
|
|
|
writer.context(),
|
|
|
|
std::string("New ") + layerPrefix());
|
2018-07-07 14:07:16 +08:00
|
|
|
DocApi api = document->getApi(transaction);
|
2016-10-05 06:55:30 +08:00
|
|
|
bool afterBackground = false;
|
|
|
|
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Layer:
|
|
|
|
layer = api.newLayer(parent, name);
|
|
|
|
break;
|
|
|
|
case Type::Group:
|
|
|
|
layer = api.newGroup(parent, name);
|
|
|
|
break;
|
|
|
|
case Type::ReferenceLayer:
|
|
|
|
layer = api.newLayer(parent, name);
|
2016-10-05 07:06:24 +08:00
|
|
|
if (layer)
|
|
|
|
layer->setReference(true);
|
2016-10-05 06:55:30 +08:00
|
|
|
afterBackground = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-06-09 02:23:51 +08:00
|
|
|
|
2016-10-05 06:55:30 +08:00
|
|
|
ASSERT(layer);
|
|
|
|
if (!layer)
|
|
|
|
return;
|
2015-05-04 22:45:08 +08:00
|
|
|
|
2016-06-21 11:02:13 +08:00
|
|
|
ASSERT(layer->parent());
|
|
|
|
|
2016-10-05 06:55:30 +08:00
|
|
|
// Put new layer as an overlay of the background or in the first
|
|
|
|
// layer in case the sprite is transparent.
|
|
|
|
if (afterBackground) {
|
|
|
|
Layer* first = sprite->root()->firstLayer();
|
|
|
|
if (first) {
|
|
|
|
if (first->isBackground())
|
|
|
|
api.restackLayerAfter(layer, sprite->root(), first);
|
|
|
|
else
|
|
|
|
api.restackLayerBefore(layer, sprite->root(), first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Move the layer above the active one.
|
|
|
|
else if (activeLayer && m_place == Place::AfterActiveLayer) {
|
2016-08-27 05:18:25 +08:00
|
|
|
api.restackLayerAfter(layer,
|
|
|
|
activeLayer->parent(),
|
|
|
|
activeLayer);
|
2016-10-05 06:55:30 +08:00
|
|
|
}
|
2015-05-04 22:45:08 +08:00
|
|
|
|
2016-10-05 06:55:30 +08:00
|
|
|
// Put all selected layers inside the group
|
|
|
|
if (m_type == Type::Group && writer.site()->inTimeline()) {
|
|
|
|
LayerGroup* commonParent = nullptr;
|
|
|
|
layer_t sameParents = 0;
|
|
|
|
for (Layer* l : selLayers) {
|
|
|
|
if (!commonParent ||
|
|
|
|
commonParent == l->parent()) {
|
|
|
|
commonParent = l->parent();
|
|
|
|
++sameParents;
|
2016-06-21 11:02:13 +08:00
|
|
|
}
|
2016-10-05 06:55:30 +08:00
|
|
|
}
|
2016-06-21 11:02:13 +08:00
|
|
|
|
2016-10-05 06:55:30 +08:00
|
|
|
if (sameParents == selLayers.size()) {
|
|
|
|
for (Layer* newChild : selLayers.toLayerList()) {
|
|
|
|
transaction.execute(
|
|
|
|
new cmd::MoveLayer(newChild, layer,
|
|
|
|
static_cast<LayerGroup*>(layer)->lastLayer()));
|
2016-06-21 11:02:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 01:41:32 +08:00
|
|
|
// Paste sprite content
|
|
|
|
if (pasteDoc && layer->isImage()) {
|
|
|
|
Sprite* pasteSpr = pasteDoc->sprite();
|
|
|
|
render::Render render;
|
|
|
|
render.setBgType(render::BgType::NONE);
|
|
|
|
|
|
|
|
// Add more frames at the end
|
|
|
|
if (writer.frame()+pasteSpr->lastFrame() > sprite->lastFrame())
|
|
|
|
api.addEmptyFramesTo(sprite, writer.frame()+pasteSpr->lastFrame());
|
|
|
|
|
|
|
|
// Paste the given sprite as flatten
|
|
|
|
for (frame_t fr=0; fr<=pasteSpr->lastFrame(); ++fr) {
|
2016-11-02 06:02:11 +08:00
|
|
|
ImageRef pasteImage(
|
|
|
|
Image::create(
|
|
|
|
pasteSpr->pixelFormat(),
|
|
|
|
pasteSpr->width(),
|
|
|
|
pasteSpr->height()));
|
2016-10-13 01:41:32 +08:00
|
|
|
clear_image(pasteImage.get(),
|
|
|
|
pasteSpr->transparentColor());
|
|
|
|
render.renderSprite(pasteImage.get(), pasteSpr, fr);
|
|
|
|
|
|
|
|
frame_t dstFrame = writer.frame()+fr;
|
2016-11-02 06:02:11 +08:00
|
|
|
|
|
|
|
if (sprite->pixelFormat() != pasteSpr->pixelFormat() ||
|
|
|
|
sprite->pixelFormat() == IMAGE_INDEXED) {
|
|
|
|
ImageRef pasteImageConv(
|
|
|
|
render::convert_pixel_format(
|
|
|
|
pasteImage.get(),
|
|
|
|
nullptr,
|
|
|
|
sprite->pixelFormat(),
|
2017-05-16 03:25:09 +08:00
|
|
|
render::DitheringAlgorithm::None,
|
2017-05-20 02:49:31 +08:00
|
|
|
render::DitheringMatrix(),
|
2016-11-02 06:02:11 +08:00
|
|
|
sprite->rgbMap(dstFrame),
|
|
|
|
pasteSpr->palette(fr),
|
|
|
|
(pasteSpr->backgroundLayer() ? true: false),
|
|
|
|
sprite->transparentColor()));
|
|
|
|
if (pasteImageConv)
|
|
|
|
pasteImage = pasteImageConv;
|
|
|
|
}
|
|
|
|
|
2016-10-13 01:41:32 +08:00
|
|
|
Cel* cel = layer->cel(dstFrame);
|
|
|
|
if (cel) {
|
|
|
|
api.replaceImage(sprite, cel->imageRef(), pasteImage);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cel = api.addCel(static_cast<LayerImage*>(layer),
|
|
|
|
dstFrame, pasteImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cel) {
|
|
|
|
if (layer->isReference()) {
|
|
|
|
gfx::RectF bounds(0, 0, pasteSpr->width(), pasteSpr->height());
|
|
|
|
double scale = MIN(double(sprite->width()) / bounds.w,
|
|
|
|
double(sprite->height()) / bounds.h);
|
|
|
|
bounds.w *= scale;
|
|
|
|
bounds.h *= scale;
|
|
|
|
bounds.x = sprite->width()/2 - bounds.w/2;
|
|
|
|
bounds.y = sprite->height()/2 - bounds.h/2;
|
|
|
|
cel->setBoundsF(bounds);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cel->setPosition(sprite->width()/2 - pasteSpr->width()/2,
|
|
|
|
sprite->height()/2 - pasteSpr->height()/2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-05 06:55:30 +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);
|
|
|
|
|
2012-07-10 00:20:58 +08:00
|
|
|
StatusBar::instance()->invalidate();
|
2016-06-09 02:23:51 +08:00
|
|
|
StatusBar::instance()->showTip(
|
|
|
|
1000, "%s '%s' created",
|
|
|
|
layerPrefix(),
|
|
|
|
name.c_str());
|
2013-12-15 23:58:14 +08:00
|
|
|
|
2016-04-23 00:19:06 +08:00
|
|
|
App::instance()->mainWindow()->popTimeline();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2016-06-09 02:23:51 +08:00
|
|
|
std::string NewLayerCommand::getUniqueLayerName(const Sprite* sprite) const
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
char buf[1024];
|
2016-06-09 02:23:51 +08:00
|
|
|
std::sprintf(buf, "%s %d",
|
|
|
|
layerPrefix(),
|
|
|
|
getMaxLayerNum(sprite->root())+1);
|
2012-01-06 06:45:03 +08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-06-09 02:23:51 +08:00
|
|
|
int NewLayerCommand::getMaxLayerNum(const Layer* layer) const
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2016-06-09 02:23:51 +08:00
|
|
|
std::string prefix = layerPrefix();
|
|
|
|
prefix += " ";
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2016-06-09 02:23:51 +08:00
|
|
|
int max = 0;
|
|
|
|
if (std::strncmp(layer->name().c_str(), prefix.c_str(), prefix.size()) == 0)
|
|
|
|
max = std::strtol(layer->name().c_str()+prefix.size(), NULL, 10);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2016-06-08 06:38:56 +08:00
|
|
|
if (layer->isGroup()) {
|
2016-06-09 03:46:15 +08:00
|
|
|
for (const Layer* child : static_cast<const LayerGroup*>(layer)->layers()) {
|
|
|
|
int tmp = getMaxLayerNum(child);
|
2012-01-06 06:45:03 +08:00
|
|
|
max = MAX(tmp, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2016-06-09 02:23:51 +08:00
|
|
|
const char* NewLayerCommand::layerPrefix() const
|
|
|
|
{
|
2016-10-05 06:55:30 +08:00
|
|
|
switch (m_type) {
|
|
|
|
case Type::Layer: return "Layer";
|
|
|
|
case Type::Group: return "Group";
|
|
|
|
case Type::ReferenceLayer: return "Reference Layer";
|
|
|
|
}
|
|
|
|
return "Unknown";
|
2016-06-09 02:23:51 +08:00
|
|
|
}
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
Command* CommandFactory::createNewLayerCommand()
|
|
|
|
{
|
|
|
|
return new NewLayerCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|