2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2018-07-07 21:07:21 +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"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "app/color.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
2014-08-25 05:01:52 +08:00
|
|
|
#include "app/commands/params.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/console.h"
|
|
|
|
#include "app/context_access.h"
|
2018-07-07 14:07:16 +08:00
|
|
|
#include "app/doc_api.h"
|
2017-12-01 10:41:45 +08:00
|
|
|
#include "app/i18n/strings.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
2015-03-18 05:19:41 +08:00
|
|
|
#include "app/transaction.h"
|
2018-07-15 10:24:49 +08:00
|
|
|
#include "app/ui/doc_view.h"
|
2015-03-18 05:19:41 +08:00
|
|
|
#include "app/ui/editor/editor.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"
|
2017-03-27 00:33:12 +08:00
|
|
|
#include "app/ui/timeline/timeline.h"
|
2015-03-18 05:19:41 +08:00
|
|
|
#include "app/ui_context.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/cel.h"
|
|
|
|
#include "doc/image.h"
|
|
|
|
#include "doc/layer.h"
|
|
|
|
#include "doc/sprite.h"
|
2017-12-01 10:41:45 +08:00
|
|
|
#include "fmt/format.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "ui/ui.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2015-02-12 23:16:25 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class NewFrameCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
2015-03-18 05:19:41 +08:00
|
|
|
enum class Content {
|
|
|
|
DUPLICATE_FRAME,
|
|
|
|
NEW_EMPTY_FRAME,
|
2015-08-20 02:59:30 +08:00
|
|
|
DUPLICATE_CELS,
|
|
|
|
DUPLICATE_CELS_BLOCK,
|
2015-03-18 05:19:41 +08:00
|
|
|
};
|
2014-08-25 05:01:52 +08:00
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
NewFrameCommand();
|
2014-08-15 10:07:47 +08:00
|
|
|
Command* clone() const override { return new NewFrameCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
2015-03-12 02:40:22 +08:00
|
|
|
void onLoadParams(const Params& params) override;
|
2014-11-16 02:47:21 +08:00
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
|
|
|
std::string onGetFriendlyName() const override;
|
2014-08-25 05:01:52 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Content m_content;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NewFrameCommand::NewFrameCommand()
|
2017-12-02 02:10:21 +08:00
|
|
|
: Command(CommandId::NewFrame(), CmdRecordableFlag)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
void NewFrameCommand::onLoadParams(const Params& params)
|
2014-08-25 05:01:52 +08:00
|
|
|
{
|
2015-03-18 05:19:41 +08:00
|
|
|
m_content = Content::DUPLICATE_FRAME;
|
2014-08-25 05:01:52 +08:00
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
std::string content = params.get("content");
|
2015-03-18 05:19:41 +08:00
|
|
|
if (content == "current" ||
|
|
|
|
content == "frame")
|
|
|
|
m_content = Content::DUPLICATE_FRAME;
|
|
|
|
else if (content == "empty")
|
|
|
|
m_content = Content::NEW_EMPTY_FRAME;
|
|
|
|
else if (content == "cel")
|
2015-08-20 02:59:30 +08:00
|
|
|
m_content = Content::DUPLICATE_CELS;
|
|
|
|
else if (content == "celblock")
|
|
|
|
m_content = Content::DUPLICATE_CELS_BLOCK;
|
2014-08-25 05:01:52 +08:00
|
|
|
}
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
bool NewFrameCommand::onEnabled(Context* context)
|
|
|
|
{
|
|
|
|
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
2014-04-29 12:00:15 +08:00
|
|
|
ContextFlags::HasActiveSprite);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NewFrameCommand::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
|
|
|
{
|
2015-08-20 03:44:59 +08:00
|
|
|
Transaction transaction(writer.context(), friendlyName());
|
2018-07-07 14:07:16 +08:00
|
|
|
DocApi api = document->getApi(transaction);
|
2015-01-19 09:05:33 +08:00
|
|
|
|
2014-08-25 05:01:52 +08:00
|
|
|
switch (m_content) {
|
2015-08-20 02:59:30 +08:00
|
|
|
|
2015-03-18 05:19:41 +08:00
|
|
|
case Content::DUPLICATE_FRAME:
|
2015-01-19 09:05:33 +08:00
|
|
|
api.addFrame(sprite, writer.frame()+1);
|
2014-08-25 05:01:52 +08:00
|
|
|
break;
|
2015-08-20 02:59:30 +08:00
|
|
|
|
2015-03-18 05:19:41 +08:00
|
|
|
case Content::NEW_EMPTY_FRAME:
|
2015-01-19 09:05:33 +08:00
|
|
|
api.addEmptyFrame(sprite, writer.frame()+1);
|
2014-08-25 05:01:52 +08:00
|
|
|
break;
|
2015-08-20 02:59:30 +08:00
|
|
|
|
|
|
|
case Content::DUPLICATE_CELS:
|
|
|
|
case Content::DUPLICATE_CELS_BLOCK: {
|
2016-06-15 01:28:00 +08:00
|
|
|
const Site* site = writer.site();
|
|
|
|
if (site->inTimeline() &&
|
|
|
|
!site->selectedLayers().empty() &&
|
|
|
|
!site->selectedFrames().empty()) {
|
2015-08-20 02:59:30 +08:00
|
|
|
std::map<CelData*, Cel*> relatedCels;
|
|
|
|
|
2016-06-15 01:28:00 +08:00
|
|
|
auto timeline = App::instance()->timeline();
|
|
|
|
timeline->prepareToMoveRange();
|
2018-07-07 21:07:21 +08:00
|
|
|
DocRange range = timeline->range();
|
2016-06-15 01:28:00 +08:00
|
|
|
|
|
|
|
SelectedLayers selLayers;
|
|
|
|
if (site->inFrames())
|
2016-08-13 04:59:33 +08:00
|
|
|
selLayers.selectAllLayers(writer.sprite()->root());
|
2018-06-22 01:37:52 +08:00
|
|
|
else {
|
2016-06-15 01:28:00 +08:00
|
|
|
selLayers = site->selectedLayers();
|
2018-06-22 01:37:52 +08:00
|
|
|
selLayers.expandCollapsedGroups();
|
|
|
|
}
|
2018-07-07 08:06:03 +08:00
|
|
|
|
2016-06-15 01:28:00 +08:00
|
|
|
frame_t frameRange =
|
|
|
|
(site->selectedFrames().lastFrame() -
|
|
|
|
site->selectedFrames().firstFrame() + 1);
|
|
|
|
|
|
|
|
for (Layer* layer : selLayers) {
|
|
|
|
if (layer->isImage()) {
|
|
|
|
for (frame_t srcFrame : site->selectedFrames().reversed()) {
|
|
|
|
frame_t dstFrame = srcFrame+frameRange;
|
2015-08-20 02:59:30 +08:00
|
|
|
bool continuous;
|
|
|
|
CelData* srcCelData = nullptr;
|
|
|
|
|
|
|
|
if (m_content == Content::DUPLICATE_CELS_BLOCK) {
|
|
|
|
continuous = false;
|
|
|
|
|
2016-06-15 01:28:00 +08:00
|
|
|
Cel* srcCel = static_cast<LayerImage*>(layer)->cel(srcFrame);
|
2015-08-20 02:59:30 +08:00
|
|
|
if (srcCel) {
|
|
|
|
srcCelData = srcCel->data();
|
|
|
|
|
|
|
|
auto it = relatedCels.find(srcCelData);
|
|
|
|
if (it != relatedCels.end()) {
|
|
|
|
srcFrame = it->second->frame();
|
|
|
|
continuous = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-06-15 01:28:00 +08:00
|
|
|
continuous = layer->isContinuous();
|
2015-08-20 02:59:30 +08:00
|
|
|
|
2015-08-05 20:40:11 +08:00
|
|
|
api.copyCel(
|
2016-06-15 01:28:00 +08:00
|
|
|
static_cast<LayerImage*>(layer), srcFrame,
|
|
|
|
static_cast<LayerImage*>(layer), dstFrame, continuous);
|
2015-08-20 02:59:30 +08:00
|
|
|
|
|
|
|
if (srcCelData && !relatedCels[srcCelData])
|
2016-06-15 01:28:00 +08:00
|
|
|
relatedCels[srcCelData] = layer->cel(dstFrame);
|
2015-08-05 20:40:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 01:28:00 +08:00
|
|
|
range.displace(0, frameRange);
|
2015-08-05 20:40:11 +08:00
|
|
|
timeline->moveRange(range);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
api.copyCel(
|
|
|
|
static_cast<LayerImage*>(writer.layer()), writer.frame(),
|
|
|
|
static_cast<LayerImage*>(writer.layer()), writer.frame()+1);
|
|
|
|
|
2018-07-07 08:06:03 +08:00
|
|
|
// TODO should we use DocObserver?
|
2015-08-05 20:40:11 +08:00
|
|
|
if (UIContext::instance() == context) {
|
2018-07-15 10:24:49 +08:00
|
|
|
if (DocView* view = UIContext::instance()->activeView())
|
2016-02-13 12:33:43 +08:00
|
|
|
view->editor()->setFrame(writer.frame()+1);
|
2015-08-05 20:40:11 +08:00
|
|
|
}
|
2015-03-18 05:19:41 +08:00
|
|
|
}
|
|
|
|
break;
|
2015-08-05 20:40:11 +08:00
|
|
|
}
|
2014-08-25 05:01:52 +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);
|
2013-03-12 07:29:45 +08:00
|
|
|
|
2012-07-10 00:20:58 +08:00
|
|
|
StatusBar::instance()
|
2012-01-06 06:45:03 +08:00
|
|
|
->showTip(1000, "New frame %d/%d",
|
2015-04-21 03:27:09 +08:00
|
|
|
(int)context->activeSite().frame()+1,
|
2014-07-30 12:28:15 +08:00
|
|
|
(int)sprite->totalFrames());
|
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
|
|
|
}
|
|
|
|
|
2014-11-16 02:47:21 +08:00
|
|
|
std::string NewFrameCommand::onGetFriendlyName() const
|
|
|
|
{
|
2017-12-01 10:41:45 +08:00
|
|
|
std::string text;
|
2014-11-16 02:47:21 +08:00
|
|
|
|
|
|
|
switch (m_content) {
|
2015-03-18 05:19:41 +08:00
|
|
|
case Content::DUPLICATE_FRAME:
|
2017-12-01 10:41:45 +08:00
|
|
|
text = Strings::commands_NewFrame();
|
2015-03-18 05:19:41 +08:00
|
|
|
break;
|
|
|
|
case Content::NEW_EMPTY_FRAME:
|
2017-12-01 10:41:45 +08:00
|
|
|
text = Strings::commands_NewFrame_NewEmptyFrame();
|
2014-11-16 02:47:21 +08:00
|
|
|
break;
|
2015-08-20 02:59:30 +08:00
|
|
|
case Content::DUPLICATE_CELS:
|
2017-12-01 10:41:45 +08:00
|
|
|
text = Strings::commands_NewFrame_DuplicateCels();
|
2015-08-20 02:59:30 +08:00
|
|
|
break;
|
|
|
|
case Content::DUPLICATE_CELS_BLOCK:
|
2017-12-01 10:41:45 +08:00
|
|
|
text = Strings::commands_NewFrame_DuplicateCelsBlock();
|
2014-11-16 02:47:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
Command* CommandFactory::createNewFrameCommand()
|
|
|
|
{
|
|
|
|
return new NewFrameCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|