| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2019 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/cmd/flatten_layers.h" |
| 13 | #include "app/commands/command.h" |
| 14 | #include "app/context_access.h" |
| 15 | #include "app/doc_range.h" |
| 16 | #include "app/i18n/strings.h" |
| 17 | #include "app/modules/gui.h" |
| 18 | #include "app/tx.h" |
| 19 | #include "app/ui/color_bar.h" |
| 20 | #include "app/ui/timeline/timeline.h" |
| 21 | #include "doc/layer.h" |
| 22 | #include "doc/sprite.h" |
| 23 | |
| 24 | namespace app { |
| 25 | |
| 26 | class FlattenLayersCommand : public Command { |
| 27 | public: |
| 28 | FlattenLayersCommand(); |
| 29 | |
| 30 | protected: |
| 31 | void onLoadParams(const Params& params) override; |
| 32 | bool onEnabled(Context* context) override; |
| 33 | void onExecute(Context* context) override; |
| 34 | std::string onGetFriendlyName() const override; |
| 35 | |
| 36 | bool m_visibleOnly; |
| 37 | }; |
| 38 | |
| 39 | FlattenLayersCommand::FlattenLayersCommand() |
| 40 | : Command(CommandId::FlattenLayers(), CmdUIOnlyFlag) |
| 41 | { |
| 42 | m_visibleOnly = false; |
| 43 | } |
| 44 | |
| 45 | void FlattenLayersCommand::onLoadParams(const Params& params) |
| 46 | { |
| 47 | m_visibleOnly = params.get_as<bool>("visibleOnly"); |
| 48 | } |
| 49 | |
| 50 | bool FlattenLayersCommand::onEnabled(Context* context) |
| 51 | { |
| 52 | return context->checkFlags(ContextFlags::ActiveDocumentIsWritable); |
| 53 | } |
| 54 | |
| 55 | void FlattenLayersCommand::onExecute(Context* context) |
| 56 | { |
| 57 | ContextWriter writer(context); |
| 58 | Sprite* sprite = writer.sprite(); |
| 59 | { |
| 60 | Tx tx(writer.context(), "Flatten Layers"); |
| 61 | |
| 62 | // TODO the range of selected layers should be in app::Site. |
| 63 | DocRange range; |
| 64 | |
| 65 | if (m_visibleOnly) { |
| 66 | for (auto layer : sprite->root()->layers()) |
| 67 | if (layer->isVisible()) |
| 68 | range.selectLayer(layer); |
| 69 | } |
| 70 | else { |
| 71 | #ifdef ENABLE_UI |
| 72 | if (context->isUIAvailable()) |
| 73 | range = App::instance()->timeline()->range(); |
| 74 | #endif |
| 75 | |
| 76 | // If the range is not selected or we have only one image layer |
| 77 | // selected, we'll flatten all layers. |
| 78 | if (!range.enabled() || |
| 79 | (range.selectedLayers().size() == 1 && |
| 80 | (*range.selectedLayers().begin())->isImage())) { |
| 81 | for (auto layer : sprite->root()->layers()) |
| 82 | range.selectLayer(layer); |
| 83 | } |
| 84 | } |
| 85 | const bool newBlend = Preferences::instance().experimental.newBlend(); |
| 86 | tx(new cmd::FlattenLayers(sprite, |
| 87 | range.selectedLayers(), |
| 88 | newBlend)); |
| 89 | tx.commit(); |
| 90 | } |
| 91 | |
| 92 | #ifdef ENABLE_UI |
| 93 | update_screen_for_document(writer.document()); |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | std::string FlattenLayersCommand::onGetFriendlyName() const |
| 98 | { |
| 99 | if (m_visibleOnly) |
| 100 | return Strings::commands_FlattenLayers_Visible(); |
| 101 | else |
| 102 | return Strings::commands_FlattenLayers(); |
| 103 | } |
| 104 | |
| 105 | Command* CommandFactory::createFlattenLayersCommand() |
| 106 | { |
| 107 | return new FlattenLayersCommand; |
| 108 | } |
| 109 | |
| 110 | } // namespace app |
| 111 |