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
|
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
#include "app/cmd/set_mask.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/commands/commands.h"
|
|
|
|
#include "app/context_access.h"
|
|
|
|
#include "app/modules/gui.h"
|
2015-01-19 09:05:33 +08:00
|
|
|
#include "app/transaction.h"
|
2012-01-09 09:34:36 +08:00
|
|
|
#include "base/unique_ptr.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/image.h"
|
|
|
|
#include "doc/mask.h"
|
|
|
|
#include "doc/primitives.h"
|
|
|
|
#include "doc/sprite.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class InvertMaskCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
|
|
|
InvertMaskCommand();
|
2014-08-15 10:07:47 +08:00
|
|
|
Command* clone() const override { return new InvertMaskCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool onEnabled(Context* context);
|
|
|
|
void onExecute(Context* context);
|
|
|
|
};
|
|
|
|
|
|
|
|
InvertMaskCommand::InvertMaskCommand()
|
|
|
|
: Command("InvertMask",
|
|
|
|
"Invert Mask",
|
|
|
|
CmdRecordableFlag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InvertMaskCommand::onEnabled(Context* context)
|
|
|
|
{
|
|
|
|
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
|
|
|
|
ContextFlags::HasActiveSprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InvertMaskCommand::onExecute(Context* context)
|
|
|
|
{
|
|
|
|
bool hasMask = false;
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
const ContextReader reader(context);
|
|
|
|
if (reader.document()->isMaskVisible())
|
2012-01-06 06:45:03 +08:00
|
|
|
hasMask = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// without mask?...
|
|
|
|
if (!hasMask) {
|
|
|
|
// so we select all
|
|
|
|
Command* mask_all_cmd =
|
|
|
|
CommandsModule::instance()->getCommandByName(CommandId::MaskAll);
|
|
|
|
context->executeCommand(mask_all_cmd);
|
|
|
|
}
|
|
|
|
// invert the current mask
|
|
|
|
else {
|
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
|
|
|
|
2014-12-14 06:10:54 +08:00
|
|
|
// Select all the sprite area
|
2013-08-06 08:20:19 +08:00
|
|
|
base::UniquePtr<Mask> mask(new Mask());
|
2014-12-14 06:10:54 +08:00
|
|
|
mask->replace(sprite->bounds());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2014-12-14 06:10:54 +08:00
|
|
|
// Remove in the new mask the current sprite marked region
|
2014-07-30 12:28:15 +08:00
|
|
|
const gfx::Rect& maskBounds = document->mask()->bounds();
|
2014-10-21 09:21:31 +08:00
|
|
|
doc::fill_rect(mask->bitmap(),
|
2014-12-14 06:10:54 +08:00
|
|
|
maskBounds.x, maskBounds.y,
|
|
|
|
maskBounds.x + maskBounds.w-1,
|
|
|
|
maskBounds.y + maskBounds.h-1, 0);
|
2012-01-09 09:34:36 +08:00
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
Mask* curMask = document->mask();
|
|
|
|
if (curMask->bitmap()) {
|
|
|
|
// Copy the inverted region in the new mask (we just modify the
|
|
|
|
// document's mask temporaly here)
|
|
|
|
curMask->freeze();
|
|
|
|
curMask->invert();
|
2014-10-21 09:21:31 +08:00
|
|
|
doc::copy_image(mask->bitmap(),
|
2015-01-19 09:05:33 +08:00
|
|
|
curMask->bitmap(),
|
|
|
|
curMask->bounds().x,
|
|
|
|
curMask->bounds().y);
|
|
|
|
curMask->invert();
|
|
|
|
curMask->unfreeze();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2012-07-08 12:25:26 +08:00
|
|
|
// We need only need the area inside the sprite
|
2014-11-24 11:09:22 +08:00
|
|
|
mask->intersect(sprite->bounds());
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2012-01-09 09:34:36 +08:00
|
|
|
// Set the new mask
|
2015-01-19 09:05:33 +08:00
|
|
|
Transaction transaction(writer.context(), "Mask Invert", DoesntModifyDocument);
|
|
|
|
transaction.execute(new cmd::SetMask(document, mask));
|
|
|
|
transaction.commit();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
document->generateMaskBoundaries();
|
|
|
|
update_screen_for_document(document);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createInvertMaskCommand()
|
|
|
|
{
|
|
|
|
return new InvertMaskCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|