2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2016-04-23 00:19:06 +08:00
|
|
|
// Copyright (C) 2001-2016 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"
|
|
|
|
#include "app/commands/params.h"
|
|
|
|
#include "app/context_access.h"
|
|
|
|
#include "app/document_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"
|
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"
|
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;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NewLayerCommand::NewLayerCommand()
|
|
|
|
: Command("NewLayer",
|
|
|
|
"New Layer",
|
|
|
|
CmdRecordableFlag)
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewLayerCommand::onExecute(Context* context)
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
ContextWriter writer(context);
|
|
|
|
Document* document(writer.document());
|
|
|
|
Sprite* sprite(writer.sprite());
|
2012-01-06 06:45:03 +08:00
|
|
|
std::string name;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
Layer* layer;
|
|
|
|
{
|
2016-06-09 02:23:51 +08:00
|
|
|
Transaction transaction(
|
|
|
|
writer.context(),
|
|
|
|
std::string("New ") + layerPrefix());
|
2015-05-04 22:45:08 +08:00
|
|
|
DocumentApi 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-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
|