| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2001-2018 David Capello |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "app/commands/command.h" |
| 12 | #include "app/context_access.h" |
| 13 | #include "app/file_selector.h" |
| 14 | #include "app/i18n/strings.h" |
| 15 | #include "app/util/msk_file.h" |
| 16 | #include "base/fs.h" |
| 17 | #include "doc/mask.h" |
| 18 | #include "doc/sprite.h" |
| 19 | #include "fmt/format.h" |
| 20 | #include "ui/alert.h" |
| 21 | |
| 22 | namespace app { |
| 23 | |
| 24 | class SaveMaskCommand : public Command { |
| 25 | public: |
| 26 | SaveMaskCommand(); |
| 27 | |
| 28 | protected: |
| 29 | bool onEnabled(Context* context) override; |
| 30 | void onExecute(Context* context) override; |
| 31 | }; |
| 32 | |
| 33 | SaveMaskCommand::SaveMaskCommand() |
| 34 | : Command(CommandId::SaveMask(), CmdUIOnlyFlag) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | bool SaveMaskCommand::onEnabled(Context* context) |
| 39 | { |
| 40 | return context->checkFlags(ContextFlags::ActiveDocumentIsReadable); |
| 41 | } |
| 42 | |
| 43 | void SaveMaskCommand::onExecute(Context* context) |
| 44 | { |
| 45 | const ContextReader reader(context); |
| 46 | const Doc* document(reader.document()); |
| 47 | |
| 48 | base::paths exts = { "msk"}; |
| 49 | base::paths selFilename; |
| 50 | if (!app::show_file_selector( |
| 51 | Strings::save_selection_title(), "default.msk", exts, |
| 52 | FileSelectorType::Save, selFilename)) |
| 53 | return; |
| 54 | |
| 55 | std::string filename = selFilename.front(); |
| 56 | |
| 57 | if (save_msk_file(document->mask(), filename.c_str()) != 0) |
| 58 | ui::Alert::show(fmt::format(Strings::alerts_error_saving_file(), filename)); |
| 59 | } |
| 60 | |
| 61 | Command* CommandFactory::createSaveMaskCommand() |
| 62 | { |
| 63 | return new SaveMaskCommand; |
| 64 | } |
| 65 | |
| 66 | } // namespace app |
| 67 |