1 | // Aseprite |
2 | // Copyright (C) 2021 Igara Studio S.A. |
3 | // Copyright (C) 2001-2018 David Capello |
4 | // |
5 | // This program is distributed under the terms of |
6 | // the End-User License Agreement for Aseprite. |
7 | |
8 | #ifdef HAVE_CONFIG_H |
9 | #include "config.h" |
10 | #endif |
11 | |
12 | #include "app/app.h" |
13 | #include "app/commands/cmd_set_palette.h" |
14 | #include "app/commands/commands.h" |
15 | #include "app/commands/params.h" |
16 | #include "app/context.h" |
17 | #include "app/doc.h" |
18 | #include "app/file/palette_file.h" |
19 | #include "app/file_selector.h" |
20 | #include "app/i18n/strings.h" |
21 | #include "app/modules/palettes.h" |
22 | #include "base/fs.h" |
23 | #include "doc/palette.h" |
24 | #include "fmt/format.h" |
25 | #include "ui/alert.h" |
26 | |
27 | namespace app { |
28 | |
29 | using namespace ui; |
30 | |
31 | class SavePaletteCommand : public Command { |
32 | public: |
33 | SavePaletteCommand(); |
34 | |
35 | protected: |
36 | void onLoadParams(const Params& params) override; |
37 | void onExecute(Context* context) override; |
38 | |
39 | private: |
40 | std::string m_preset; |
41 | bool m_saveAsPreset = false; |
42 | }; |
43 | |
44 | SavePaletteCommand::SavePaletteCommand() |
45 | : Command(CommandId::SavePalette(), CmdRecordableFlag) |
46 | { |
47 | } |
48 | |
49 | void SavePaletteCommand::onLoadParams(const Params& params) |
50 | { |
51 | m_preset = params.get("preset" ); |
52 | m_saveAsPreset = (params.get("saveAsPreset" ) == "true" ); |
53 | } |
54 | |
55 | void SavePaletteCommand::onExecute(Context* ctx) |
56 | { |
57 | const doc::Palette* palette = get_current_palette(); |
58 | std::string filename; |
59 | |
60 | if (!m_preset.empty()) { |
61 | filename = get_preset_palette_filename(m_preset, ".ase" ); |
62 | } |
63 | else { |
64 | base::paths exts = get_writable_palette_extensions(); |
65 | base::paths selFilename; |
66 | std::string initialPath = (m_saveAsPreset ? get_preset_palettes_dir(): "" ); |
67 | if (!app::show_file_selector(Strings::save_palette_title(), |
68 | initialPath, |
69 | exts, |
70 | FileSelectorType::Save, selFilename)) |
71 | return; |
72 | |
73 | filename = selFilename.front(); |
74 | |
75 | // Check that the file format supports saving indexed images/color |
76 | // palettes (e.g. if the user specified .jpg we should show that |
77 | // that file format is not supported to save color palettes) |
78 | if (!base::has_file_extension(filename, exts)) { |
79 | if (ctx->isUIAvailable()) { |
80 | ui::Alert::show( |
81 | fmt::format(Strings::alerts_file_format_doesnt_support_palette(), |
82 | base::get_file_extension(filename))); |
83 | } |
84 | return; |
85 | } |
86 | } |
87 | gfx::ColorSpaceRef colorSpace = nullptr; |
88 | auto activeDoc = ctx->activeDocument(); |
89 | if (activeDoc) |
90 | colorSpace = activeDoc->sprite()->colorSpace(); |
91 | |
92 | if (!save_palette(filename.c_str(), palette, 16, colorSpace)) // TODO 16 should be configurable |
93 | ui::Alert::show(fmt::format(Strings::alerts_error_saving_file(), filename)); |
94 | |
95 | if (m_preset == get_default_palette_preset_name()) { |
96 | set_default_palette(palette); |
97 | if (!activeDoc) |
98 | set_current_palette(palette, false); |
99 | } |
100 | if (m_saveAsPreset) { |
101 | App::instance()->PalettePresetsChange(); |
102 | } |
103 | } |
104 | |
105 | Command* CommandFactory::createSavePaletteCommand() |
106 | { |
107 | return new SavePaletteCommand; |
108 | } |
109 | |
110 | } // namespace app |
111 | |