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
|
2012-01-06 06:45:03 +08:00
|
|
|
|
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/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"
|
2015-01-19 09:05:33 +08:00
|
|
|
#include "app/transaction.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/util/msk_file.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/mask.h"
|
|
|
|
#include "doc/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:
|
2015-03-12 02:40:22 +08:00
|
|
|
void onLoadParams(const Params& params) override;
|
|
|
|
bool onEnabled(Context* context) override;
|
|
|
|
void onExecute(Context* context) override;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
LoadMaskCommand::LoadMaskCommand()
|
|
|
|
: Command("LoadMask",
|
|
|
|
"LoadMask",
|
|
|
|
CmdRecordableFlag)
|
|
|
|
{
|
|
|
|
m_filename = "";
|
|
|
|
}
|
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
void LoadMaskCommand::onLoadParams(const Params& params)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2015-03-12 02:40:22 +08:00
|
|
|
m_filename = params.get("filename");
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-05-19 04:04:31 +08:00
|
|
|
if (context->isUIAvailable()) {
|
2015-03-03 03:07:35 +08:00
|
|
|
filename = app::show_file_selector(
|
|
|
|
"Load .msk File", filename, "msk",
|
|
|
|
FileSelectorType::Open);
|
|
|
|
|
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();
|
2015-01-19 09:05:33 +08:00
|
|
|
Transaction transaction(writer.context(), "Mask Load", DoesntModifyDocument);
|
|
|
|
transaction.execute(new cmd::SetMask(document, mask));
|
|
|
|
transaction.commit();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
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
|