2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2024-06-21 07:14:29 +08:00
|
|
|
// Copyright (C) 2019-2024 Igara Studio S.A.
|
2018-02-21 21:39:30 +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
|
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"
|
2017-10-18 05:00:45 +08:00
|
|
|
#include "app/i18n/strings.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/modules/gui.h"
|
2018-08-21 03:00:59 +08:00
|
|
|
#include "app/tx.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;
|
2025-08-06 02:59:31 +08:00
|
|
|
bool m_ui = true;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
LoadMaskCommand();
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2025-08-06 02:59:31 +08:00
|
|
|
LoadMaskCommand::LoadMaskCommand() : Command(CommandId::LoadMask())
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
void LoadMaskCommand::onLoadParams(const Params& params)
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2025-08-06 02:59:31 +08:00
|
|
|
if (params.has_param("ui"))
|
|
|
|
m_ui = params.get_as<bool>("ui");
|
|
|
|
else
|
|
|
|
m_ui = true;
|
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
|
|
|
|
2025-08-06 02:59:31 +08:00
|
|
|
if (context->isUIAvailable() && m_ui) {
|
2018-02-21 21:39:30 +08:00
|
|
|
base::paths exts = { "msk" };
|
|
|
|
base::paths selectedFilename;
|
2022-01-07 17:02:38 +08:00
|
|
|
if (!app::show_file_selector(Strings::load_selection_title(),
|
|
|
|
m_filename,
|
|
|
|
exts,
|
2017-04-08 11:06:25 +08:00
|
|
|
FileSelectorType::Open,
|
|
|
|
selectedFilename))
|
2012-01-06 06:45:03 +08:00
|
|
|
return;
|
|
|
|
|
2017-04-08 11:06:25 +08:00
|
|
|
m_filename = selectedFilename.front();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2018-08-09 04:27:26 +08:00
|
|
|
std::unique_ptr<Mask> mask(load_msk_file(m_filename.c_str()));
|
2017-10-18 05:00:45 +08:00
|
|
|
if (!mask) {
|
2025-08-06 02:59:31 +08:00
|
|
|
if (context->isUIAvailable())
|
|
|
|
ui::Alert::show(Strings::alerts_error_loading_file(m_filename));
|
2017-10-18 05:00:45 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
{
|
2013-03-12 07:29:45 +08:00
|
|
|
ContextWriter writer(reader);
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* document = writer.document();
|
2022-01-07 17:02:38 +08:00
|
|
|
Tx tx(writer, Strings::load_selection_title(), 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
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
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
|