2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2020-04-08 23:03:32 +08:00
|
|
|
// Copyright (C) 2019-2020 Igara Studio S.A.
|
2018-08-21 03:00:59 +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
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/app.h"
|
2019-03-30 02:57:10 +08:00
|
|
|
#include "app/cmd/add_tileset.h"
|
2019-07-03 02:28:05 +08:00
|
|
|
#include "app/cmd/clear_mask.h"
|
2016-06-21 11:02:13 +08:00
|
|
|
#include "app/cmd/move_layer.h"
|
2019-07-03 02:28:05 +08:00
|
|
|
#include "app/cmd/trim_cel.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"
|
2019-06-28 02:34:56 +08:00
|
|
|
#include "app/commands/new_params.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"
|
2018-08-25 02:03:48 +08:00
|
|
|
#include "app/i18n/strings.h"
|
2012-06-16 10:37:59 +08:00
|
|
|
#include "app/load_widget.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
2020-04-18 03:13:22 +08:00
|
|
|
#include "app/restore_visible_layers.h"
|
2018-08-21 03:00:59 +08:00
|
|
|
#include "app/tx.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"
|
2019-04-14 20:01:18 +08:00
|
|
|
#include "app/ui/tileset_selector.h"
|
2016-11-03 03:10:42 +08:00
|
|
|
#include "app/ui_context.h"
|
2019-06-28 02:34:56 +08:00
|
|
|
#include "app/util/clipboard.h"
|
2019-07-03 02:28:05 +08:00
|
|
|
#include "app/util/new_image_from_mask.h"
|
2020-04-18 03:13:22 +08:00
|
|
|
#include "app/util/range_utils.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/layer.h"
|
2019-03-30 02:57:10 +08:00
|
|
|
#include "doc/layer_tilemap.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"
|
2019-06-28 02:34:56 +08:00
|
|
|
#include "fmt/format.h"
|
2019-04-04 06:32:24 +08:00
|
|
|
#include "render/dithering.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"
|
|
|
|
|
2020-04-08 23:03:32 +08:00
|
|
|
#include <algorithm>
|
2015-03-05 08:35:11 +08:00
|
|
|
#include <cstring>
|
2020-04-08 23:03:32 +08:00
|
|
|
#include <string>
|
|
|
|
#include <cstdlib>
|
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
|
|
|
|
2019-06-28 02:34:56 +08:00
|
|
|
struct NewLayerParams : public NewParams {
|
|
|
|
Param<std::string> name { this, std::string(), "name" };
|
|
|
|
Param<bool> group { this, false, "group" };
|
|
|
|
Param<bool> reference { this, false, "reference" };
|
2019-03-30 02:57:10 +08:00
|
|
|
Param<bool> tilemap { this, false, "tilemap" };
|
2019-06-28 02:34:56 +08:00
|
|
|
Param<bool> ask { this, false, "ask" };
|
|
|
|
Param<bool> fromFile { this, false, { "fromFile", "from-file" } };
|
|
|
|
Param<bool> fromClipboard { this, false, "fromClipboard" };
|
2019-07-03 02:28:05 +08:00
|
|
|
Param<bool> viaCut { this, false, "viaCut" };
|
|
|
|
Param<bool> viaCopy { this, false, "viaCopy" };
|
2019-06-28 02:34:56 +08:00
|
|
|
Param<bool> top { this, false, "top" };
|
|
|
|
Param<bool> before { this, false, "before" };
|
|
|
|
};
|
|
|
|
|
|
|
|
class NewLayerCommand : public CommandWithNewParams<NewLayerParams> {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
2019-03-30 02:57:10 +08:00
|
|
|
enum class Type { Layer, Group, ReferenceLayer, TilemapLayer };
|
2018-08-25 02:03:48 +08:00
|
|
|
enum class Place { AfterActiveLayer, BeforeActiveLayer, Top };
|
2016-10-05 06:55:30 +08:00
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
NewLayerCommand();
|
|
|
|
|
|
|
|
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;
|
2018-08-25 02:03:48 +08:00
|
|
|
std::string onGetFriendlyName() const override;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
private:
|
2019-07-03 02:28:05 +08:00
|
|
|
void adjustRefCelBounds(Cel* cel, gfx::RectF bounds);
|
2016-06-09 02:23:51 +08:00
|
|
|
std::string getUniqueLayerName(const Sprite* sprite) const;
|
|
|
|
int getMaxLayerNum(const Layer* layer) const;
|
2019-06-28 02:34:56 +08:00
|
|
|
std::string layerPrefix() const;
|
2016-06-09 02:23:51 +08:00
|
|
|
|
2016-10-05 06:55:30 +08:00
|
|
|
Type m_type;
|
|
|
|
Place m_place;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NewLayerCommand::NewLayerCommand()
|
2019-06-28 02:34:56 +08:00
|
|
|
: CommandWithNewParams(CommandId::NewLayer(), CmdRecordableFlag)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-28 02:34:56 +08:00
|
|
|
void NewLayerCommand::onLoadParams(const Params& commandParams)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2019-06-28 02:34:56 +08:00
|
|
|
CommandWithNewParams<NewLayerParams>::onLoadParams(commandParams);
|
|
|
|
|
2016-10-05 06:55:30 +08:00
|
|
|
m_type = Type::Layer;
|
2019-06-28 02:34:56 +08:00
|
|
|
if (params().group())
|
2016-10-05 06:55:30 +08:00
|
|
|
m_type = Type::Group;
|
2019-06-28 02:34:56 +08:00
|
|
|
else if (params().reference())
|
2016-10-05 06:55:30 +08:00
|
|
|
m_type = Type::ReferenceLayer;
|
2019-03-30 02:57:10 +08:00
|
|
|
else if (params().tilemap())
|
|
|
|
m_type = Type::TilemapLayer;
|
|
|
|
else
|
|
|
|
m_type = Type::Layer;
|
2016-10-05 06:55:30 +08:00
|
|
|
|
|
|
|
m_place = Place::AfterActiveLayer;
|
2019-06-28 02:34:56 +08:00
|
|
|
if (params().top())
|
2016-10-05 06:55:30 +08:00
|
|
|
m_place = Place::Top;
|
2019-06-28 02:34:56 +08:00
|
|
|
else if (params().before())
|
2018-08-25 02:03:48 +08:00
|
|
|
m_place = Place::BeforeActiveLayer;
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2020-09-25 22:13:52 +08:00
|
|
|
bool NewLayerCommand::onEnabled(Context* ctx)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2020-09-25 22:13:52 +08:00
|
|
|
if (!ctx->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
|
|
|
ContextFlags::HasActiveSprite))
|
2019-07-03 02:28:05 +08:00
|
|
|
return false;
|
|
|
|
|
2019-06-29 05:53:23 +08:00
|
|
|
#ifdef ENABLE_UI
|
2019-07-03 02:28:05 +08:00
|
|
|
if (params().fromClipboard() &&
|
2020-09-25 22:13:52 +08:00
|
|
|
ctx->clipboard()->format() != ClipboardFormat::Image)
|
2019-07-03 02:28:05 +08:00
|
|
|
return false;
|
2019-06-29 05:53:23 +08:00
|
|
|
#endif
|
2019-07-03 02:28:05 +08:00
|
|
|
|
|
|
|
if ((params().viaCut() ||
|
|
|
|
params().viaCopy()) &&
|
2020-09-25 22:13:52 +08:00
|
|
|
!ctx->checkFlags(ContextFlags::HasVisibleMask))
|
2019-07-03 02:28:05 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2019-04-14 20:01:18 +08:00
|
|
|
ContextReader reader(context);
|
2019-07-03 02:28:05 +08:00
|
|
|
Site site = context->activeSite();
|
2019-04-14 20:01:18 +08:00
|
|
|
Doc* document(reader.document());
|
|
|
|
Sprite* sprite(reader.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) {
|
2019-06-26 02:53:31 +08:00
|
|
|
DocDestroyer destroyer(context, pasteDoc, 100);
|
|
|
|
destroyer.destroyDocument();
|
2016-10-13 01:41:32 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-28 02:34:56 +08:00
|
|
|
// Default name
|
|
|
|
if (params().name.isSet())
|
|
|
|
name = params().name();
|
2012-01-06 06:45:03 +08:00
|
|
|
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
|
2019-06-28 02:34:56 +08:00
|
|
|
if (params().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
|
|
|
}
|
|
|
|
|
2020-07-15 04:27:03 +08:00
|
|
|
// Information about the tileset to be used for new tilemaps
|
2019-04-14 20:01:18 +08:00
|
|
|
TilesetSelector::Info tilesetInfo;
|
|
|
|
tilesetInfo.newTileset = true;
|
|
|
|
tilesetInfo.grid = context->activeSite().grid();
|
2020-10-31 03:33:34 +08:00
|
|
|
tilesetInfo.firstVisibleIndex = 1;
|
2019-04-14 20:01:18 +08:00
|
|
|
|
2018-09-07 01:18:59 +08:00
|
|
|
#ifdef ENABLE_UI
|
2012-01-06 06:45:03 +08:00
|
|
|
// If params specify to ask the user about the name...
|
2019-07-10 23:45:53 +08:00
|
|
|
if (params().ask() && context->isUIAvailable()) {
|
2012-01-06 06:45:03 +08:00
|
|
|
// We open the window to ask the name
|
2016-06-09 02:23:51 +08:00
|
|
|
app::gen::NewLayer window;
|
2019-04-14 20:01:18 +08:00
|
|
|
TilesetSelector* tilesetSelector = nullptr;
|
2016-06-09 02:23:51 +08:00
|
|
|
window.name()->setText(name.c_str());
|
|
|
|
window.name()->setMinSize(gfx::Size(128, 0));
|
2019-04-14 20:01:18 +08:00
|
|
|
|
|
|
|
// Tileset selector for new tilemaps
|
|
|
|
const bool isTilemap = (m_type == Type::TilemapLayer);
|
|
|
|
window.tilesetLabel()->setVisible(isTilemap);
|
|
|
|
window.tilesetOptions()->setVisible(isTilemap);
|
|
|
|
if (isTilemap) {
|
|
|
|
tilesetSelector = new TilesetSelector(sprite, tilesetInfo);
|
|
|
|
window.tilesetOptions()->addChild(tilesetSelector);
|
|
|
|
}
|
|
|
|
|
2016-06-09 02:23:51 +08:00
|
|
|
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();
|
2019-04-14 20:01:18 +08:00
|
|
|
if (tilesetSelector)
|
|
|
|
tilesetInfo = tilesetSelector->getInfo();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
2018-09-07 01:18:59 +08:00
|
|
|
#endif
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2019-04-14 20:01:18 +08:00
|
|
|
ContextWriter writer(reader);
|
2016-06-09 02:55:44 +08:00
|
|
|
LayerGroup* parent = sprite->root();
|
2015-05-04 22:45:08 +08:00
|
|
|
Layer* activeLayer = writer.layer();
|
2019-07-03 02:28:05 +08:00
|
|
|
SelectedLayers selLayers = 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
|
|
|
{
|
2018-08-21 03:00:59 +08:00
|
|
|
Tx tx(
|
2016-06-09 02:23:51 +08:00
|
|
|
writer.context(),
|
2019-06-28 02:34:56 +08:00
|
|
|
fmt::format(Strings::commands_NewLayer(), layerPrefix()));
|
2018-08-21 03:00:59 +08:00
|
|
|
DocApi api = document->getApi(tx);
|
2016-10-05 06:55:30 +08:00
|
|
|
bool afterBackground = false;
|
|
|
|
|
|
|
|
switch (m_type) {
|
|
|
|
case Type::Layer:
|
|
|
|
layer = api.newLayer(parent, name);
|
2018-08-25 02:03:48 +08:00
|
|
|
if (m_place == Place::BeforeActiveLayer)
|
|
|
|
api.restackLayerBefore(layer, parent, activeLayer);
|
2016-10-05 06:55:30 +08:00
|
|
|
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;
|
2019-03-30 02:57:10 +08:00
|
|
|
case Type::TilemapLayer: {
|
2019-04-14 20:01:18 +08:00
|
|
|
tileset_index tsi;
|
|
|
|
if (tilesetInfo.newTileset) {
|
2020-10-31 03:33:34 +08:00
|
|
|
auto tileset = new Tileset(sprite, tilesetInfo.grid, 1);
|
|
|
|
tileset->setFirstVisibleIndex(tilesetInfo.firstVisibleIndex);
|
|
|
|
|
2019-04-14 20:01:18 +08:00
|
|
|
auto addTileset = new cmd::AddTileset(sprite, tileset);
|
|
|
|
tx(addTileset);
|
2019-03-30 02:57:10 +08:00
|
|
|
|
2019-04-14 20:01:18 +08:00
|
|
|
tsi = addTileset->tilesetIndex();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tsi = tilesetInfo.tsi;
|
|
|
|
}
|
2019-03-30 02:57:10 +08:00
|
|
|
|
|
|
|
layer = new LayerTilemap(sprite, tsi);
|
|
|
|
layer->setName(name);
|
|
|
|
api.addLayer(parent, layer, parent->lastLayer());
|
|
|
|
break;
|
|
|
|
}
|
2016-10-05 06:55:30 +08:00
|
|
|
}
|
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
|
2019-07-03 02:28:05 +08:00
|
|
|
if (m_type == Type::Group && site.inTimeline()) {
|
2016-10-05 06:55:30 +08:00
|
|
|
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()) {
|
2020-08-18 04:14:12 +08:00
|
|
|
for (Layer* newChild : selLayers.toBrowsableLayerList()) {
|
2018-08-21 03:00:59 +08:00
|
|
|
tx(
|
2016-10-05 06:55:30 +08:00
|
|
|
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;
|
2019-02-26 05:02:58 +08:00
|
|
|
render.setNewBlend(true);
|
2016-10-13 01:41:32 +08:00
|
|
|
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(),
|
2019-04-04 06:32:24 +08:00
|
|
|
render::Dithering(),
|
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()) {
|
2019-07-03 02:28:05 +08:00
|
|
|
adjustRefCelBounds(
|
|
|
|
cel, gfx::RectF(0, 0, pasteSpr->width(), pasteSpr->height()));
|
2016-10-13 01:41:32 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
cel->setPosition(sprite->width()/2 - pasteSpr->width()/2,
|
|
|
|
sprite->height()/2 - pasteSpr->height()/2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-29 05:53:23 +08:00
|
|
|
#ifdef ENABLE_UI
|
2019-06-28 02:34:56 +08:00
|
|
|
// Paste new layer from clipboard
|
|
|
|
else if (params().fromClipboard() && layer->isImage()) {
|
2020-09-25 22:13:52 +08:00
|
|
|
context->clipboard()->paste(context, false);
|
2019-07-03 02:28:05 +08:00
|
|
|
|
|
|
|
if (layer->isReference()) {
|
|
|
|
if (Cel* cel = layer->cel(site.frame())) {
|
|
|
|
adjustRefCelBounds(
|
|
|
|
cel, cel->boundsF());
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 02:34:56 +08:00
|
|
|
}
|
2019-06-29 05:53:23 +08:00
|
|
|
#endif // ENABLE_UI
|
2019-07-03 02:28:05 +08:00
|
|
|
// Paste new layer from selection
|
|
|
|
else if ((params().viaCut() || params().viaCopy())
|
|
|
|
&& document->isMaskVisible()) {
|
|
|
|
const doc::Mask* mask = document->mask();
|
|
|
|
ASSERT(mask);
|
2020-04-18 03:13:22 +08:00
|
|
|
|
|
|
|
RestoreVisibleLayers restore;
|
|
|
|
SelectedLayers layers;
|
|
|
|
SelectedFrames frames;
|
|
|
|
bool merged;
|
|
|
|
if (site.range().enabled()) {
|
|
|
|
merged = true;
|
|
|
|
layers = site.range().selectedLayers();
|
|
|
|
frames = site.range().selectedFrames();
|
|
|
|
restore.showSelectedLayers(site.sprite(), layers);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
merged = false;
|
|
|
|
layers.insert(site.layer());
|
|
|
|
frames.insert(site.frame());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (frame_t frame : frames) {
|
|
|
|
ImageRef newImage(new_image_from_mask(site, mask, true, merged));
|
|
|
|
if (!newImage)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Cel* newCel = api.addCel(static_cast<LayerImage*>(layer),
|
|
|
|
frame, newImage);
|
|
|
|
if (newCel) {
|
2019-07-03 02:28:05 +08:00
|
|
|
gfx::Point pos = mask->bounds().origin();
|
2020-04-18 03:13:22 +08:00
|
|
|
newCel->setPosition(pos.x, pos.y);
|
2019-07-03 02:28:05 +08:00
|
|
|
}
|
|
|
|
|
2020-04-18 03:13:22 +08:00
|
|
|
for (Layer* layer : layers) {
|
|
|
|
if (!layer->isImage() ||
|
|
|
|
!layer->isEditable()) // Locked layers will not be modified
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Cel* origCel = layer->cel(site.frame());
|
|
|
|
if (origCel &&
|
|
|
|
params().viaCut()) {
|
|
|
|
tx(new cmd::ClearMask(origCel));
|
|
|
|
|
|
|
|
if (layer->isTransparent()) {
|
|
|
|
// If the cel wasn't deleted by cmd::ClearMask, we trim it.
|
|
|
|
origCel = layer->cel(frame);
|
|
|
|
if (origCel)
|
|
|
|
tx(new cmd::TrimCel(origCel));
|
|
|
|
}
|
2019-07-03 02:28:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-05 06:55:30 +08:00
|
|
|
|
2018-08-21 03:00:59 +08:00
|
|
|
tx.commit();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2018-09-07 01:18:59 +08:00
|
|
|
#ifdef ENABLE_UI
|
2018-09-05 04:30:34 +08:00
|
|
|
if (context->isUIAvailable()) {
|
|
|
|
update_screen_for_document(document);
|
2013-12-15 23:58:14 +08:00
|
|
|
|
2018-09-05 04:30:34 +08:00
|
|
|
StatusBar::instance()->invalidate();
|
|
|
|
StatusBar::instance()->showTip(
|
2020-05-09 04:39:55 +08:00
|
|
|
1000, fmt::format("{} '{}' created",
|
|
|
|
layerPrefix(),
|
|
|
|
name));
|
2018-09-05 04:30:34 +08:00
|
|
|
|
|
|
|
App::instance()->mainWindow()->popTimeline();
|
|
|
|
}
|
2018-09-07 01:18:59 +08:00
|
|
|
#endif
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2018-08-25 02:03:48 +08:00
|
|
|
std::string NewLayerCommand::onGetFriendlyName() const
|
|
|
|
{
|
|
|
|
std::string text;
|
2019-06-28 02:34:56 +08:00
|
|
|
if (m_place == Place::BeforeActiveLayer)
|
|
|
|
text = fmt::format(Strings::commands_NewLayer_BeforeActiveLayer(), layerPrefix());
|
|
|
|
else
|
|
|
|
text = fmt::format(Strings::commands_NewLayer(), layerPrefix());
|
|
|
|
if (params().fromClipboard())
|
|
|
|
text = fmt::format(Strings::commands_NewLayer_FromClipboard(), text);
|
2019-07-03 02:28:05 +08:00
|
|
|
if (params().viaCopy())
|
|
|
|
text = fmt::format(Strings::commands_NewLayer_ViaCopy(), text);
|
|
|
|
if (params().viaCut())
|
|
|
|
text = fmt::format(Strings::commands_NewLayer_ViaCut(), text);
|
2019-04-14 20:01:18 +08:00
|
|
|
if (params().ask())
|
|
|
|
text = fmt::format(Strings::commands_NewLayer_WithDialog(), text);
|
2018-08-25 02:03:48 +08:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2019-07-03 02:28:05 +08:00
|
|
|
void NewLayerCommand::adjustRefCelBounds(Cel* cel, gfx::RectF bounds)
|
|
|
|
{
|
|
|
|
Sprite* sprite = cel->sprite();
|
2020-04-08 23:03:32 +08:00
|
|
|
double scale = std::min(double(sprite->width()) / bounds.w,
|
|
|
|
double(sprite->height()) / bounds.h);
|
2019-07-03 02:28:05 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-06-09 02:23:51 +08:00
|
|
|
std::string NewLayerCommand::getUniqueLayerName(const Sprite* sprite) const
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2019-06-28 02:34:56 +08:00
|
|
|
return fmt::format("{} {}",
|
|
|
|
layerPrefix(),
|
|
|
|
getMaxLayerNum(sprite->root())+1);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2020-04-08 23:03:32 +08:00
|
|
|
max = std::max(tmp, max);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2019-06-28 02:34:56 +08:00
|
|
|
std::string NewLayerCommand::layerPrefix() const
|
2016-06-09 02:23:51 +08:00
|
|
|
{
|
2016-10-05 06:55:30 +08:00
|
|
|
switch (m_type) {
|
2019-06-28 02:34:56 +08:00
|
|
|
case Type::Layer: return Strings::commands_NewLayer_Layer();
|
|
|
|
case Type::Group: return Strings::commands_NewLayer_Group();
|
|
|
|
case Type::ReferenceLayer: return Strings::commands_NewLayer_ReferenceLayer();
|
2019-03-30 02:57:10 +08:00
|
|
|
case Type::TilemapLayer: return Strings::commands_NewLayer_TilemapLayer();
|
2016-10-05 06:55:30 +08:00
|
|
|
}
|
|
|
|
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
|