2017-03-06 22:27:43 +00:00
|
|
|
// Aseprite
|
|
|
|
// Copyright (C) 2017 David Capello
|
|
|
|
//
|
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/app.h"
|
|
|
|
#include "app/cmd/remove_slice.h"
|
|
|
|
#include "app/cmd/set_slice_key.h"
|
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/context_access.h"
|
|
|
|
#include "app/modules/gui.h"
|
|
|
|
#include "app/ui/status_bar.h"
|
|
|
|
#include "app/transaction.h"
|
|
|
|
#include "base/convert_to.h"
|
|
|
|
#include "doc/slice.h"
|
|
|
|
#include "doc/sprite.h"
|
|
|
|
#include "ui/alert.h"
|
|
|
|
#include "ui/widget.h"
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
|
|
class RemoveSliceCommand : public Command {
|
|
|
|
public:
|
|
|
|
RemoveSliceCommand();
|
|
|
|
Command* clone() const override { return new RemoveSliceCommand(*this); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onLoadParams(const Params& params) override;
|
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_sliceName;
|
|
|
|
ObjectId m_sliceId;
|
|
|
|
};
|
|
|
|
|
|
|
|
RemoveSliceCommand::RemoveSliceCommand()
|
2017-12-01 18:10:21 +00:00
|
|
|
: Command(CommandId::RemoveSlice(), CmdRecordableFlag)
|
2017-03-06 22:27:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveSliceCommand::onLoadParams(const Params& params)
|
|
|
|
{
|
|
|
|
m_sliceName = params.get("name");
|
|
|
|
|
|
|
|
std::string id = params.get("id");
|
|
|
|
if (!id.empty())
|
|
|
|
m_sliceId = ObjectId(base::convert_to<doc::ObjectId>(id));
|
|
|
|
else
|
|
|
|
m_sliceId = NullId;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoveSliceCommand::onEnabled(Context* context)
|
|
|
|
{
|
|
|
|
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
|
|
|
ContextFlags::HasActiveSprite |
|
|
|
|
ContextFlags::HasActiveLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveSliceCommand::onExecute(Context* context)
|
|
|
|
{
|
|
|
|
const ContextReader reader(context);
|
|
|
|
const Sprite* sprite = reader.sprite();
|
|
|
|
frame_t frame = reader.frame();
|
|
|
|
const Slice* foundSlice = nullptr;
|
|
|
|
|
|
|
|
if (!m_sliceName.empty())
|
|
|
|
foundSlice = sprite->slices().getByName(m_sliceName);
|
|
|
|
else if (m_sliceId != NullId)
|
|
|
|
foundSlice = sprite->slices().getById(m_sliceId);
|
|
|
|
|
|
|
|
if (!foundSlice)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string sliceName = foundSlice->name();
|
|
|
|
|
|
|
|
{
|
|
|
|
ContextWriter writer(reader, 500);
|
2018-07-07 14:54:44 +00:00
|
|
|
Doc* document(writer.document());
|
2017-03-06 22:27:43 +00:00
|
|
|
Sprite* sprite(writer.sprite());
|
|
|
|
Transaction transaction(writer.context(), "Remove Slice");
|
|
|
|
Slice* slice = const_cast<Slice*>(foundSlice);
|
2017-12-01 02:41:45 +00:00
|
|
|
|
2017-03-06 22:27:43 +00:00
|
|
|
if (slice->size() > 1) {
|
|
|
|
transaction.execute(new cmd::SetSliceKey(slice, frame, SliceKey()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
transaction.execute(new cmd::RemoveSlice(sprite, slice));
|
|
|
|
}
|
|
|
|
transaction.commit();
|
|
|
|
document->notifyGeneralUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusBar::instance()->invalidate();
|
|
|
|
if (!sliceName.empty())
|
|
|
|
StatusBar::instance()->showTip(1000, "Slice '%s' removed", sliceName.c_str());
|
|
|
|
else
|
|
|
|
StatusBar::instance()->showTip(1000, "Slice removed");
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createRemoveSliceCommand()
|
|
|
|
{
|
|
|
|
return new RemoveSliceCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace app
|