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/cmd_set_palette.h" |
12 | #include "app/commands/command.h" |
13 | #include "app/commands/commands.h" |
14 | #include "app/context.h" |
15 | #include "app/file/palette_file.h" |
16 | #include "app/file_selector.h" |
17 | #include "app/i18n/strings.h" |
18 | #include "app/modules/palettes.h" |
19 | #include "base/fs.h" |
20 | #include "doc/palette.h" |
21 | #include "fmt/format.h" |
22 | #include "ui/alert.h" |
23 | |
24 | namespace app { |
25 | |
26 | using namespace ui; |
27 | |
28 | class LoadPaletteCommand : public Command { |
29 | public: |
30 | LoadPaletteCommand(); |
31 | |
32 | protected: |
33 | void onLoadParams(const Params& params) override; |
34 | void onExecute(Context* context) override; |
35 | |
36 | private: |
37 | std::string m_preset; |
38 | std::string m_filename; |
39 | }; |
40 | |
41 | LoadPaletteCommand::LoadPaletteCommand() |
42 | : Command(CommandId::LoadPalette(), CmdRecordableFlag) |
43 | { |
44 | } |
45 | |
46 | void LoadPaletteCommand::onLoadParams(const Params& params) |
47 | { |
48 | m_preset = params.get("preset"); |
49 | m_filename = params.get("filename"); |
50 | } |
51 | |
52 | void LoadPaletteCommand::onExecute(Context* context) |
53 | { |
54 | std::string filename; |
55 | |
56 | if (!m_preset.empty()) { |
57 | filename = get_preset_palette_filename(m_preset, ".ase"); |
58 | if (!base::is_file(filename)) |
59 | filename = get_preset_palette_filename(m_preset, ".gpl"); |
60 | } |
61 | else if (!m_filename.empty()) { |
62 | filename = m_filename; |
63 | } |
64 | #ifdef ENABLE_UI |
65 | else { |
66 | base::paths exts = get_readable_palette_extensions(); |
67 | base::paths filenames; |
68 | if (app::show_file_selector( |
69 | Strings::load_palette_title(), "", exts, |
70 | FileSelectorType::Open, filenames)) { |
71 | filename = filenames.front(); |
72 | } |
73 | } |
74 | #endif // ENABLE_UI |
75 | |
76 | // Do nothing |
77 | if (filename.empty()) |
78 | return; |
79 | |
80 | std::unique_ptr<doc::Palette> palette(load_palette(filename.c_str())); |
81 | if (!palette) { |
82 | if (context->isUIAvailable()) |
83 | ui::Alert::show(fmt::format(Strings::alerts_error_loading_file(), filename)); |
84 | return; |
85 | } |
86 | |
87 | SetPaletteCommand* cmd = static_cast<SetPaletteCommand*>( |
88 | Commands::instance()->byId(CommandId::SetPalette())); |
89 | cmd->setPalette(palette.get()); |
90 | context->executeCommand(cmd); |
91 | } |
92 | |
93 | Command* CommandFactory::createLoadPaletteCommand() |
94 | { |
95 | return new LoadPaletteCommand; |
96 | } |
97 | |
98 | } // namespace app |
99 |