| 1 | // Aseprite | 
|---|---|
| 2 | // Copyright (C) 2001-2017 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/app.h" | 
| 12 | #include "app/commands/command.h" | 
| 13 | #include "app/context_access.h" | 
| 14 | #include "app/modules/gui.h" | 
| 15 | #include "app/ui/timeline/timeline.h" | 
| 16 | #include "doc/image.h" | 
| 17 | #include "doc/layer.h" | 
| 18 | |
| 19 | namespace app { | 
| 20 | |
| 21 | using namespace ui; | 
| 22 | |
| 23 | class LayerVisibilityCommand : public Command { | 
| 24 | public: | 
| 25 | LayerVisibilityCommand(); | 
| 26 | |
| 27 | protected: | 
| 28 | bool onEnabled(Context* context) override; | 
| 29 | bool onChecked(Context* context) override; | 
| 30 | void onExecute(Context* context) override; | 
| 31 | }; | 
| 32 | |
| 33 | LayerVisibilityCommand::LayerVisibilityCommand() | 
| 34 | : Command(CommandId::LayerVisibility(), CmdRecordableFlag) | 
| 35 | { | 
| 36 | } | 
| 37 | |
| 38 | bool LayerVisibilityCommand::onEnabled(Context* context) | 
| 39 | { | 
| 40 | return context->checkFlags(ContextFlags::ActiveDocumentIsWritable | | 
| 41 | ContextFlags::HasActiveLayer); | 
| 42 | } | 
| 43 | |
| 44 | bool LayerVisibilityCommand::onChecked(Context* context) | 
| 45 | { | 
| 46 | const ContextReader reader(context); | 
| 47 | if (!reader.document() || | 
| 48 | !reader.layer()) | 
| 49 | return false; | 
| 50 | |
| 51 | SelectedLayers selLayers; | 
| 52 | auto range = App::instance()->timeline()->range(); | 
| 53 | if (range.enabled()) { | 
| 54 | selLayers = range.selectedLayers(); | 
| 55 | } | 
| 56 | else { | 
| 57 | selLayers.insert(const_cast<Layer*>(reader.layer())); | 
| 58 | } | 
| 59 | bool visible = false; | 
| 60 | for (auto layer : selLayers) { | 
| 61 | if (layer && layer->isVisible()) | 
| 62 | visible = true; | 
| 63 | } | 
| 64 | return visible; | 
| 65 | } | 
| 66 | |
| 67 | void LayerVisibilityCommand::onExecute(Context* context) | 
| 68 | { | 
| 69 | ContextWriter writer(context); | 
| 70 | SelectedLayers selLayers; | 
| 71 | auto range = App::instance()->timeline()->range(); | 
| 72 | if (range.enabled()) { | 
| 73 | selLayers = range.selectedLayers(); | 
| 74 | } | 
| 75 | else { | 
| 76 | selLayers.insert(writer.layer()); | 
| 77 | } | 
| 78 | bool anyVisible = false; | 
| 79 | for (auto layer : selLayers) { | 
| 80 | if (layer->isVisible()) | 
| 81 | anyVisible = true; | 
| 82 | } | 
| 83 | for (auto layer : selLayers) { | 
| 84 | layer->setVisible(!anyVisible); | 
| 85 | } | 
| 86 | |
| 87 | update_screen_for_document(writer.document()); | 
| 88 | } | 
| 89 | |
| 90 | Command* CommandFactory::createLayerVisibilityCommand() | 
| 91 | { | 
| 92 | return new LayerVisibilityCommand; | 
| 93 | } | 
| 94 | |
| 95 | } // namespace app | 
| 96 | 
