Add Unlink Cel command

This commit is contained in:
David Capello 2015-01-20 09:39:12 -03:00
parent 052a736a49
commit d2ea95716f
5 changed files with 217 additions and 0 deletions

View File

@ -645,6 +645,7 @@
<item command="CelProperties" text="&amp;Properties..." />
<separator />
<item command="ClearCel" text="&amp;Clear" />
<item command="UnlinkCel" text="&amp;Unlink" />
</menu>
<menu id="cel_movement_popup">

View File

@ -0,0 +1,64 @@
/* 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 as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/cmd/unlink_cel.h"
#include "doc/cel.h"
#include "doc/image.h"
#include "doc/sprite.h"
namespace app {
namespace cmd {
using namespace doc;
UnlinkCel::UnlinkCel(Cel* cel)
: WithCel(cel)
, m_oldLinkedImageId(cel->image()->id())
, m_newLinkedImageId(0)
{
ASSERT(cel->links());
}
void UnlinkCel::onExecute()
{
Cel* cel = this->cel();
ImageRef oldImage = cel->sprite()->getImage(m_oldLinkedImageId);
ImageRef copy(Image::createCopy(oldImage));
if (m_newLinkedImageId)
copy->setId(m_newLinkedImageId);
else
m_newLinkedImageId = copy->id();
cel->setImage(copy);
}
void UnlinkCel::onUndo()
{
Cel* cel = this->cel();
ImageRef oldImage = cel->sprite()->getImage(m_oldLinkedImageId);
cel->setImage(oldImage);
}
} // namespace cmd
} // namespace app

47
src/app/cmd/unlink_cel.h Normal file
View File

@ -0,0 +1,47 @@
/* 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 as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef APP_CMD_UNLINK_CEL_H_INCLUDED
#define APP_CMD_UNLINK_CEL_H_INCLUDED
#pragma once
#include "app/cmd.h"
#include "app/cmd/with_cel.h"
namespace app {
namespace cmd {
using namespace doc;
class UnlinkCel : public Cmd
, public WithCel {
public:
UnlinkCel(Cel* cel);
protected:
void onExecute() override;
void onUndo() override;
private:
ObjectId m_oldLinkedImageId;
ObjectId m_newLinkedImageId;
};
} // namespace cmd
} // namespace app
#endif

View File

@ -0,0 +1,104 @@
/* 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 as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/app.h"
#include "app/cmd/unlink_cel.h"
#include "app/commands/command.h"
#include "app/context_access.h"
#include "app/modules/gui.h"
#include "app/transaction.h"
#include "app/ui/main_window.h"
#include "app/ui/timeline.h"
#include "doc/cel.h"
#include "doc/layer.h"
#include "doc/sprite.h"
namespace app {
class UnlinkCelCommand : public Command {
public:
UnlinkCelCommand();
Command* clone() const override { return new UnlinkCelCommand(*this); }
protected:
bool onEnabled(Context* context);
void onExecute(Context* context);
};
UnlinkCelCommand::UnlinkCelCommand()
: Command("UnlinkCel",
"Unlink Cel",
CmdRecordableFlag)
{
}
bool UnlinkCelCommand::onEnabled(Context* context)
{
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable);
}
void UnlinkCelCommand::onExecute(Context* context)
{
ContextWriter writer(context);
Document* document(writer.document());
{
Transaction transaction(writer.context(), "Unlink Cel");
// TODO the range of selected frames should be in the DocumentLocation.
Timeline::Range range = App::instance()->getMainWindow()->getTimeline()->range();
if (range.enabled()) {
Sprite* sprite = writer.sprite();
for (LayerIndex layerIdx = range.layerBegin(); layerIdx <= range.layerEnd(); ++layerIdx) {
Layer* layer = sprite->indexToLayer(layerIdx);
if (!layer->isImage())
continue;
LayerImage* layerImage = static_cast<LayerImage*>(layer);
for (frame_t frame = range.frameEnd(),
begin = range.frameBegin()-1;
frame != begin;
--frame) {
Cel* cel = layerImage->cel(frame);
if (cel && cel->links())
transaction.execute(new cmd::UnlinkCel(cel));
}
}
}
else {
Cel* cel = writer.cel();
if (cel && cel->links())
transaction.execute(new cmd::UnlinkCel(writer.cel()));
}
transaction.commit();
}
update_screen_for_document(document);
}
Command* CommandFactory::createUnlinkCelCommand()
{
return new UnlinkCelCommand;
}
} // namespace app

View File

@ -114,4 +114,5 @@ FOR_EACH_COMMAND(SpriteSize)
FOR_EACH_COMMAND(SwitchColors)
FOR_EACH_COMMAND(Timeline)
FOR_EACH_COMMAND(Undo)
FOR_EACH_COMMAND(UnlinkCel)
FOR_EACH_COMMAND(Zoom)