2013-08-09 08:01:20 +08:00
|
|
|
/* Aseprite
|
2013-01-27 23:13:13 +08:00
|
|
|
* Copyright (C) 2001-2013 David Capello
|
2012-01-06 06:45:03 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/commands/commands.h"
|
|
|
|
#include "app/context_access.h"
|
|
|
|
#include "app/modules/gui.h"
|
|
|
|
#include "app/undo_transaction.h"
|
|
|
|
#include "app/undoers/set_mask.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());
|
|
|
|
UndoTransaction undo(writer.context(), "Mask Invert", undo::DoesntModifyDocument);
|
2012-07-08 12:25:26 +08:00
|
|
|
if (undo.isEnabled())
|
|
|
|
undo.pushUndoer(new undoers::SetMask(undo.getObjects(), document));
|
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
|
|
|
|
|
|
|
// Invert the current mask in the sprite
|
2014-07-30 12:28:15 +08:00
|
|
|
document->mask()->invert();
|
|
|
|
if (document->mask()->bitmap()) {
|
2012-01-09 09:34:36 +08:00
|
|
|
// Copy the inverted region in the new mask
|
2014-10-21 09:21:31 +08:00
|
|
|
doc::copy_image(mask->bitmap(),
|
2014-12-28 22:06:11 +08:00
|
|
|
document->mask()->bitmap(),
|
|
|
|
document->mask()->bounds().x,
|
|
|
|
document->mask()->bounds().y);
|
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
|
2012-01-06 06:45:03 +08:00
|
|
|
document->setMask(mask);
|
2012-07-08 12:25:26 +08:00
|
|
|
undo.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
|