2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2019-02-16 04:14:44 +08:00
|
|
|
// Copyright (C) 2019 Igara Studio S.A.
|
2018-08-09 04:27:26 +08:00
|
|
|
// Copyright (C) 2001-2018 David Capello
|
2015-02-12 23:16:25 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
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"
|
2018-08-21 03:00:59 +08:00
|
|
|
#include "app/tx.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();
|
|
|
|
|
|
|
|
protected:
|
2015-10-01 03:34:43 +08:00
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
2025-08-06 02:59:31 +08:00
|
|
|
InvertMaskCommand::InvertMaskCommand() : Command(CommandId::InvertMask())
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-12-02 02:10:21 +08:00
|
|
|
Command* mask_all_cmd = Commands::instance()->byId(CommandId::MaskAll());
|
2012-01-06 06:45:03 +08:00
|
|
|
context->executeCommand(mask_all_cmd);
|
|
|
|
}
|
|
|
|
// invert the current mask
|
|
|
|
else {
|
2013-03-12 07:29:45 +08:00
|
|
|
ContextWriter writer(context);
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* document(writer.document());
|
2013-03-12 07:29:45 +08:00
|
|
|
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
|
2018-08-09 04:27:26 +08:00
|
|
|
std::unique_ptr<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(), curMask->bitmap(), curMask->bounds().x, curMask->bounds().y);
|
2015-01-19 09:05:33 +08:00
|
|
|
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
|
2023-12-13 08:34:30 +08:00
|
|
|
Tx tx(writer, "Mask Invert", DoesntModifyDocument);
|
2018-08-21 03:00:59 +08:00
|
|
|
tx(new cmd::SetMask(document, mask.get()));
|
|
|
|
tx.commit();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
update_screen_for_document(document);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createInvertMaskCommand()
|
|
|
|
{
|
|
|
|
return new InvertMaskCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|