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
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/commands/params.h"
|
|
|
|
#include "app/context_access.h"
|
2012-06-16 10:37:59 +08:00
|
|
|
#include "app/file_selector.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
|
|
|
#include "app/undo_transaction.h"
|
|
|
|
#include "app/undoers/set_mask.h"
|
|
|
|
#include "app/util/msk_file.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "raster/mask.h"
|
|
|
|
#include "raster/sprite.h"
|
2012-06-18 09:49:58 +08:00
|
|
|
#include "ui/alert.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
|
|
|
|
|
|
|
class LoadMaskCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
std::string m_filename;
|
|
|
|
|
|
|
|
public:
|
|
|
|
LoadMaskCommand();
|
2014-08-15 10:07:47 +08:00
|
|
|
Command* clone() const override { return new LoadMaskCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void onLoadParams(Params* params);
|
|
|
|
|
|
|
|
bool onEnabled(Context* context);
|
|
|
|
void onExecute(Context* context);
|
|
|
|
};
|
|
|
|
|
|
|
|
LoadMaskCommand::LoadMaskCommand()
|
|
|
|
: Command("LoadMask",
|
|
|
|
"LoadMask",
|
|
|
|
CmdRecordableFlag)
|
|
|
|
{
|
|
|
|
m_filename = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoadMaskCommand::onLoadParams(Params* params)
|
|
|
|
{
|
|
|
|
m_filename = params->get("filename");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LoadMaskCommand::onEnabled(Context* context)
|
|
|
|
{
|
|
|
|
return context->checkFlags(ContextFlags::ActiveDocumentIsWritable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoadMaskCommand::onExecute(Context* context)
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
const ContextReader reader(context);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2014-04-21 06:53:27 +08:00
|
|
|
std::string filename = m_filename;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
if (context->isUiAvailable()) {
|
2012-06-16 10:37:59 +08:00
|
|
|
filename = app::show_file_selector("Load .msk File", filename, "msk");
|
2012-01-06 06:45:03 +08:00
|
|
|
if (filename.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_filename = filename;
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
base::UniquePtr<Mask> mask(load_msk_file(m_filename.c_str()));
|
2012-01-06 06:45:03 +08:00
|
|
|
if (!mask)
|
|
|
|
throw base::Exception("Error loading .msk file: %s",
|
|
|
|
static_cast<const char*>(m_filename.c_str()));
|
|
|
|
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
ContextWriter writer(reader);
|
|
|
|
Document* document = writer.document();
|
|
|
|
UndoTransaction undo(writer.context(), "Mask Load", undo::DoesntModifyDocument);
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Add the mask change into the undo history.
|
2012-07-08 12:25:26 +08:00
|
|
|
if (undo.isEnabled())
|
2013-03-12 07:29:45 +08:00
|
|
|
undo.pushUndoer(new undoers::SetMask(undo.getObjects(), document));
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
document->setMask(mask);
|
2012-07-08 12:25:26 +08:00
|
|
|
|
|
|
|
undo.commit();
|
2013-03-12 07:29:45 +08:00
|
|
|
document->generateMaskBoundaries();
|
|
|
|
update_screen_for_document(document);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createLoadMaskCommand()
|
|
|
|
{
|
|
|
|
return new LoadMaskCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|