2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
|
|
|
// Copyright (C) 2001-2015 David Capello
|
|
|
|
//
|
|
|
|
// 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"
|
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"
|
|
|
|
#include "app/document_api.h"
|
|
|
|
#include "app/modules/gui.h"
|
2015-03-18 05:19:41 +08:00
|
|
|
#include "app/transaction.h"
|
|
|
|
#include "app/ui/document_view.h"
|
|
|
|
#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"
|
2015-08-05 20:40:11 +08:00
|
|
|
#include "app/ui/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"
|
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()
|
|
|
|
: Command("NewFrame",
|
|
|
|
"New Frame",
|
|
|
|
CmdRecordableFlag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
Document* document(writer.document());
|
|
|
|
Sprite* sprite(writer.sprite());
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2015-01-19 09:05:33 +08:00
|
|
|
Transaction transaction(writer.context(), "New Frame");
|
|
|
|
DocumentApi api = document->getApi(transaction);
|
|
|
|
|
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: {
|
2015-08-05 20:40:11 +08:00
|
|
|
// TODO the range of selected frames should be in doc::Site.
|
|
|
|
Timeline* timeline = App::instance()->getMainWindow()->getTimeline();
|
|
|
|
Timeline::Range range = timeline->range();
|
|
|
|
if (range.enabled()) {
|
2015-08-20 02:59:30 +08:00
|
|
|
std::map<CelData*, Cel*> relatedCels;
|
2015-08-05 20:40:11 +08:00
|
|
|
timeline->prepareToMoveRange();
|
|
|
|
|
2015-08-20 02:59:30 +08:00
|
|
|
LayerIndex layerBegin = range.layerBegin();
|
|
|
|
LayerIndex layerEnd = range.layerEnd();
|
|
|
|
|
|
|
|
if (range.type() == DocumentRange::kFrames) {
|
|
|
|
layerBegin = writer.sprite()->firstLayer();
|
|
|
|
layerEnd = writer.sprite()->lastLayer();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (LayerIndex layer = layerBegin; layer <= layerEnd; ++layer) {
|
2015-08-05 20:40:11 +08:00
|
|
|
Layer* layerPtr = writer.sprite()->indexToLayer(layer);
|
|
|
|
if (layerPtr->isImage()) {
|
|
|
|
for (frame_t frame = range.frameEnd(); frame >= range.frameBegin(); --frame) {
|
2015-08-20 02:59:30 +08:00
|
|
|
frame_t srcFrame = frame;
|
|
|
|
frame_t dstFrame = frame+range.frames();
|
|
|
|
bool continuous;
|
|
|
|
CelData* srcCelData = nullptr;
|
|
|
|
|
|
|
|
if (m_content == Content::DUPLICATE_CELS_BLOCK) {
|
|
|
|
continuous = false;
|
|
|
|
|
|
|
|
Cel* srcCel = static_cast<LayerImage*>(layerPtr)->cel(srcFrame);
|
|
|
|
if (srcCel) {
|
|
|
|
srcCelData = srcCel->data();
|
|
|
|
|
|
|
|
auto it = relatedCels.find(srcCelData);
|
|
|
|
if (it != relatedCels.end()) {
|
|
|
|
srcFrame = it->second->frame();
|
|
|
|
continuous = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continuous = layerPtr->isContinuous();
|
|
|
|
|
2015-08-05 20:40:11 +08:00
|
|
|
api.copyCel(
|
2015-08-20 02:59:30 +08:00
|
|
|
static_cast<LayerImage*>(layerPtr), srcFrame,
|
|
|
|
static_cast<LayerImage*>(layerPtr), dstFrame, continuous);
|
|
|
|
|
|
|
|
if (srcCelData && !relatedCels[srcCelData])
|
|
|
|
relatedCels[srcCelData] = layerPtr->cel(dstFrame);
|
2015-08-05 20:40:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
range.displace(0, range.frames());
|
|
|
|
timeline->moveRange(range);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
api.copyCel(
|
|
|
|
static_cast<LayerImage*>(writer.layer()), writer.frame(),
|
|
|
|
static_cast<LayerImage*>(writer.layer()), writer.frame()+1);
|
|
|
|
|
|
|
|
// TODO should we use DocumentObserver?
|
|
|
|
if (UIContext::instance() == context) {
|
|
|
|
if (DocumentView* view = UIContext::instance()->activeView())
|
|
|
|
view->getEditor()->setFrame(writer.frame()+1);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
App::instance()->getMainWindow()->popTimeline();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2014-11-16 02:47:21 +08:00
|
|
|
std::string NewFrameCommand::onGetFriendlyName() const
|
|
|
|
{
|
|
|
|
std::string text = "New Frame";
|
|
|
|
|
|
|
|
switch (m_content) {
|
2015-03-18 05:19:41 +08:00
|
|
|
case Content::DUPLICATE_FRAME:
|
|
|
|
text = "New Frame";
|
|
|
|
break;
|
|
|
|
case Content::NEW_EMPTY_FRAME:
|
|
|
|
text = "New Empty Frame";
|
2014-11-16 02:47:21 +08:00
|
|
|
break;
|
2015-08-20 02:59:30 +08:00
|
|
|
case Content::DUPLICATE_CELS:
|
|
|
|
text = "Duplicate Linked Cels";
|
|
|
|
break;
|
|
|
|
case Content::DUPLICATE_CELS_BLOCK:
|
|
|
|
text = "Duplicate Cels";
|
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
|