Add Remove Frame Tag command

This commit is contained in:
David Capello 2015-02-18 11:44:54 -03:00
parent 9e67942235
commit 9f8505f490
10 changed files with 242 additions and 25 deletions

View File

@ -521,9 +521,11 @@
<item command="NewFrame" text="New &amp;Empty Frame"> <item command="NewFrame" text="New &amp;Empty Frame">
<param name="content" value="empty" /> <param name="content" value="empty" />
</item> </item>
<item command="NewFrameTag" text="New &amp;Tag" />
<item command="RemoveFrame" text="&amp;Remove Frame" /> <item command="RemoveFrame" text="&amp;Remove Frame" />
<separator /> <separator />
<item command="NewFrameTag" text="New &amp;Tag" />
<item command="RemoveFrameTag" text="Remove Ta&amp;g" />
<separator />
<menu text="&amp;Jump to"> <menu text="&amp;Jump to">
<item command="GotoFirstFrame" text="&amp;First Frame" /> <item command="GotoFirstFrame" text="&amp;First Frame" />
<item command="GotoPreviousFrame" text="&amp;Previous Frame" /> <item command="GotoPreviousFrame" text="&amp;Previous Frame" />

View File

@ -78,6 +78,7 @@ add_library(app-lib
cmd/move_layer.cpp cmd/move_layer.cpp
cmd/remove_cel.cpp cmd/remove_cel.cpp
cmd/remove_frame.cpp cmd/remove_frame.cpp
cmd/remove_frame_tag.cpp
cmd/remove_layer.cpp cmd/remove_layer.cpp
cmd/remove_palette.cpp cmd/remove_palette.cpp
cmd/replace_image.cpp cmd/replace_image.cpp
@ -99,6 +100,7 @@ add_library(app-lib
cmd/unlink_cel.cpp cmd/unlink_cel.cpp
cmd/with_cel.cpp cmd/with_cel.cpp
cmd/with_document.cpp cmd/with_document.cpp
cmd/with_frame_tag.cpp
cmd/with_image.cpp cmd/with_image.cpp
cmd/with_layer.cpp cmd/with_layer.cpp
cmd/with_palette.cpp cmd/with_palette.cpp
@ -171,6 +173,7 @@ add_library(app-lib
commands/cmd_play_animation.cpp commands/cmd_play_animation.cpp
commands/cmd_refresh.cpp commands/cmd_refresh.cpp
commands/cmd_remove_frame.cpp commands/cmd_remove_frame.cpp
commands/cmd_remove_frame_tag.cpp
commands/cmd_remove_layer.cpp commands/cmd_remove_layer.cpp
commands/cmd_repeat_last_export.cpp commands/cmd_repeat_last_export.cpp
commands/cmd_reselect_mask.cpp commands/cmd_reselect_mask.cpp

View File

@ -22,43 +22,41 @@ using namespace doc;
AddFrameTag::AddFrameTag(Sprite* sprite, FrameTag* frameTag) AddFrameTag::AddFrameTag(Sprite* sprite, FrameTag* frameTag)
: WithSprite(sprite) : WithSprite(sprite)
, m_frameTag(frameTag) , WithFrameTag(frameTag)
, m_frameTagId(0)
{ {
} }
void AddFrameTag::onExecute() void AddFrameTag::onExecute()
{ {
Sprite* sprite = this->sprite(); Sprite* sprite = this->sprite();
FrameTag* frameTag = this->frameTag();
if (m_frameTag) { sprite->frameTags().add(frameTag);
m_frameTagId = m_frameTag->id();
}
else {
m_frameTag = read_frame_tag(m_stream);
m_stream.str(std::string());
m_stream.clear();
}
sprite->frameTags().add(m_frameTag);
m_frameTag = nullptr;
} }
void AddFrameTag::onUndo() void AddFrameTag::onUndo()
{ {
Sprite* sprite = this->sprite(); Sprite* sprite = this->sprite();
FrameTag* frameTag = get<FrameTag>(m_frameTagId); FrameTag* frameTag = this->frameTag();
write_frame_tag(m_stream, frameTag); write_frame_tag(m_stream, frameTag);
sprite->frameTags().remove(frameTag); sprite->frameTags().remove(frameTag);
delete frameTag; delete frameTag;
} }
void AddFrameTag::onRedo()
{
Sprite* sprite = this->sprite();
FrameTag* frameTag = read_frame_tag(m_stream);
sprite->frameTags().add(frameTag);
m_stream.str(std::string());
m_stream.clear();
}
size_t AddFrameTag::onMemSize() const size_t AddFrameTag::onMemSize() const
{ {
return sizeof(*this) return sizeof(*this)
+ (m_frameTag ? m_frameTag->getMemSize(): 0)
+ (size_t)const_cast<std::stringstream*>(&m_stream)->tellp(); + (size_t)const_cast<std::stringstream*>(&m_stream)->tellp();
} }

View File

@ -10,32 +10,28 @@
#pragma once #pragma once
#include "app/cmd.h" #include "app/cmd.h"
#include "app/cmd/with_frame_tag.h"
#include "app/cmd/with_sprite.h" #include "app/cmd/with_sprite.h"
#include <sstream> #include <sstream>
namespace doc {
class FrameTag;
class Sprite;
}
namespace app { namespace app {
namespace cmd { namespace cmd {
using namespace doc; using namespace doc;
class AddFrameTag : public Cmd class AddFrameTag : public Cmd
, public WithSprite { , public WithSprite
, public WithFrameTag {
public: public:
AddFrameTag(Sprite* sprite, FrameTag* tag); AddFrameTag(Sprite* sprite, FrameTag* tag);
protected: protected:
void onExecute() override; void onExecute() override;
void onUndo() override; void onUndo() override;
void onRedo() override;
size_t onMemSize() const override; size_t onMemSize() const override;
private: private:
FrameTag* m_frameTag;
ObjectId m_frameTagId;
std::stringstream m_stream; std::stringstream m_stream;
}; };

View File

@ -0,0 +1,40 @@
// 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.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/cmd/remove_frame_tag.h"
namespace app {
namespace cmd {
using namespace doc;
RemoveFrameTag::RemoveFrameTag(Sprite* sprite, FrameTag* tag)
: AddFrameTag(sprite, tag)
{
}
void RemoveFrameTag::onExecute()
{
AddFrameTag::onUndo();
}
void RemoveFrameTag::onUndo()
{
AddFrameTag::onRedo();
}
void RemoveFrameTag::onRedo()
{
AddFrameTag::onUndo();
}
} // namespace cmd
} // namespace app

View File

@ -0,0 +1,31 @@
// 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.
#ifndef APP_CMD_REMOVE_FRAME_TAG_H_INCLUDED
#define APP_CMD_REMOVE_FRAME_TAG_H_INCLUDED
#pragma once
#include "app/cmd/add_frame_tag.h"
namespace app {
namespace cmd {
using namespace doc;
class RemoveFrameTag : public AddFrameTag {
public:
RemoveFrameTag(Sprite* sprite, FrameTag* tag);
protected:
void onExecute() override;
void onUndo() override;
void onRedo() override;
};
} // namespace cmd
} // namespace app
#endif

View File

@ -0,0 +1,32 @@
// 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.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/cmd/with_frame_tag.h"
#include "doc/frame_tag.h"
namespace app {
namespace cmd {
using namespace doc;
WithFrameTag::WithFrameTag(FrameTag* frameTag)
: m_frameTagId(frameTag->id())
{
}
FrameTag* WithFrameTag::frameTag()
{
return get<FrameTag>(m_frameTagId);
}
} // namespace cmd
} // namespace app

View File

@ -0,0 +1,34 @@
// 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.
#ifndef APP_CMD_WITH_FRAME_TAG_H_INCLUDED
#define APP_CMD_WITH_FRAME_TAG_H_INCLUDED
#pragma once
#include "doc/object_id.h"
namespace doc {
class FrameTag;
}
namespace app {
namespace cmd {
using namespace doc;
class WithFrameTag {
public:
WithFrameTag(FrameTag* tag);
FrameTag* frameTag();
private:
ObjectId m_frameTagId;
};
} // namespace cmd
} // namespace app
#endif

View File

@ -0,0 +1,80 @@
// 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.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/app.h"
#include "app/cmd/remove_frame_tag.h"
#include "app/commands/command.h"
#include "app/context.h"
#include "app/context_access.h"
#include "app/transaction.h"
#include "app/ui/main_window.h"
#include "app/ui/timeline.h"
#include "doc/frame_tag.h"
namespace app {
class RemoveFrameTagCommand : public Command {
public:
RemoveFrameTagCommand();
Command* clone() const override { return new RemoveFrameTagCommand(*this); }
protected:
bool onEnabled(Context* context);
void onExecute(Context* context);
};
RemoveFrameTagCommand::RemoveFrameTagCommand()
: Command("RemoveFrameTag",
"Remove Frame Tag",
CmdRecordableFlag)
{
}
bool RemoveFrameTagCommand::onEnabled(Context* context)
{
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
ContextFlags::HasActiveSprite);
}
void RemoveFrameTagCommand::onExecute(Context* context)
{
ContextWriter writer(context);
Document* document(writer.document());
Sprite* sprite(writer.sprite());
frame_t frame = writer.frame();
FrameTag* best = nullptr;
for (FrameTag* tag : sprite->frameTags()) {
if (frame >= tag->fromFrame() &&
frame <= tag->toFrame()) {
if (!best ||
(tag->toFrame() - tag->fromFrame()) < (best->toFrame() - best->fromFrame())) {
best = tag;
}
}
}
if (!best)
return;
Transaction transaction(writer.context(), "Remove Frame Tag");
transaction.execute(new cmd::RemoveFrameTag(sprite, best));
transaction.commit();
App::instance()->getMainWindow()->getTimeline()->invalidate();
}
Command* CommandFactory::createRemoveFrameTagCommand()
{
return new RemoveFrameTagCommand;
}
} // namespace app

View File

@ -80,6 +80,7 @@ FOR_EACH_COMMAND(PlayAnimation)
FOR_EACH_COMMAND(Redo) FOR_EACH_COMMAND(Redo)
FOR_EACH_COMMAND(Refresh) FOR_EACH_COMMAND(Refresh)
FOR_EACH_COMMAND(RemoveFrame) FOR_EACH_COMMAND(RemoveFrame)
FOR_EACH_COMMAND(RemoveFrameTag)
FOR_EACH_COMMAND(RemoveLayer) FOR_EACH_COMMAND(RemoveLayer)
FOR_EACH_COMMAND(RepeatLastExport) FOR_EACH_COMMAND(RepeatLastExport)
FOR_EACH_COMMAND(ReplaceColor) FOR_EACH_COMMAND(ReplaceColor)