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
|
|
|
//
|
|
|
|
// 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.
|
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"
|
|
|
|
#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
|
|
|
|
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:
|
|
|
|
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:
|
|
|
|
bool m_ask;
|
2015-05-04 22:45:08 +08:00
|
|
|
bool m_top;
|
2012-01-06 06:45:03 +08:00
|
|
|
std::string m_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::string get_unique_layer_name(Sprite* sprite);
|
|
|
|
static int get_max_layer_num(Layer* layer);
|
|
|
|
|
|
|
|
NewLayerCommand::NewLayerCommand()
|
|
|
|
: Command("NewLayer",
|
|
|
|
"New Layer",
|
|
|
|
CmdRecordableFlag)
|
|
|
|
{
|
|
|
|
m_ask = false;
|
2015-05-04 22:45:08 +08:00
|
|
|
m_top = false;
|
2012-01-06 06:45:03 +08:00
|
|
|
m_name = "";
|
|
|
|
}
|
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
void NewLayerCommand::onLoadParams(const Params& params)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2015-05-04 22:45:08 +08:00
|
|
|
m_ask = (params.get("ask") == "true");
|
|
|
|
m_top = (params.get("top") == "true");
|
2015-03-12 02:40:22 +08:00
|
|
|
m_name = params.get("name");
|
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
|
|
|
|
name = get_unique_layer_name(sprite);
|
|
|
|
|
|
|
|
// If params specify to ask the user about the name...
|
|
|
|
if (m_ask) {
|
|
|
|
// We open the window to ask the name
|
2013-08-06 08:20:19 +08:00
|
|
|
base::UniquePtr<Window> window(app::load_widget<Window>("new_layer.xml", "new_layer"));
|
2012-06-16 10:37:59 +08:00
|
|
|
Widget* name_widget = app::find_widget<Widget>(window, "name");
|
2012-01-06 06:45:03 +08:00
|
|
|
name_widget->setText(name.c_str());
|
2014-04-22 09:15:29 +08:00
|
|
|
name_widget->setMinSize(gfx::Size(128, 0));
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2012-07-09 10:24:42 +08:00
|
|
|
window->openWindowInForeground();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2015-12-05 01:54:15 +08:00
|
|
|
if (window->closer() != window->findChild("ok"))
|
2012-01-06 06:45:03 +08:00
|
|
|
return;
|
|
|
|
|
Refactor several "getNoun()" getters to "noun()"
This is a work-in-progress to create a consistent API and finally
separate the whole Aseprite base/gfx/ui libs into a reusable C++ library.
Classes:
app::IFileItem, app::AppMenuItem, app::skin::SkinPart,
gfx::Rect, gfx::Border, she::FileDialog,
ui::IButtonIcon, ui::Graphics, ui::Overlay, ui::Widget,
ui::ScrollableViewDelegate, and UI events
2015-12-05 01:39:04 +08:00
|
|
|
name = window->findChild("name")->text();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2015-05-04 22:45:08 +08:00
|
|
|
Layer* activeLayer = writer.layer();
|
2012-01-06 06:45:03 +08:00
|
|
|
Layer* layer;
|
|
|
|
{
|
2015-01-19 09:05:33 +08:00
|
|
|
Transaction transaction(writer.context(), "New Layer");
|
2015-05-04 22:45:08 +08:00
|
|
|
DocumentApi api = document->getApi(transaction);
|
2015-08-22 01:55:34 +08:00
|
|
|
layer = api.newLayer(sprite, name);
|
2015-05-04 22:45:08 +08:00
|
|
|
|
|
|
|
// If "top" parameter is false, create the layer above the active
|
|
|
|
// one.
|
|
|
|
if (activeLayer && !m_top)
|
|
|
|
api.restackLayerAfter(layer, activeLayer);
|
|
|
|
|
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();
|
|
|
|
StatusBar::instance()->showTip(1000, "Layer `%s' created", 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
|
|
|
}
|
|
|
|
|
|
|
|
static std::string get_unique_layer_name(Sprite* sprite)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
2014-07-30 12:28:15 +08:00
|
|
|
std::sprintf(buf, "Layer %d", get_max_layer_num(sprite->folder())+1);
|
2012-01-06 06:45:03 +08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_max_layer_num(Layer* layer)
|
|
|
|
{
|
|
|
|
int max = 0;
|
|
|
|
|
2015-03-05 08:35:11 +08:00
|
|
|
if (std::strncmp(layer->name().c_str(), "Layer ", 6) == 0)
|
|
|
|
max = std::strtol(layer->name().c_str()+6, NULL, 10);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-01-11 23:43:25 +08:00
|
|
|
if (layer->isFolder()) {
|
|
|
|
LayerIterator it = static_cast<LayerFolder*>(layer)->getLayerBegin();
|
|
|
|
LayerIterator end = static_cast<LayerFolder*>(layer)->getLayerEnd();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
int tmp = get_max_layer_num(*it);
|
|
|
|
max = MAX(tmp, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createNewLayerCommand()
|
|
|
|
{
|
|
|
|
return new NewLayerCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|