1// Aseprite
2// Copyright (C) 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
19namespace app {
20
21using namespace ui;
22
23class LayerLockCommand : public Command {
24public:
25 LayerLockCommand();
26
27protected:
28 bool onEnabled(Context* context) override;
29 bool onChecked(Context* context) override;
30 void onExecute(Context* context) override;
31};
32
33LayerLockCommand::LayerLockCommand()
34 : Command(CommandId::LayerLock(), CmdRecordableFlag)
35{
36}
37
38bool LayerLockCommand::onEnabled(Context* context)
39{
40 return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
41 ContextFlags::HasActiveLayer);
42}
43
44bool LayerLockCommand::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 lock = false;
60 for (auto layer : selLayers) {
61 if (layer && !layer->isEditable())
62 lock = true;
63 }
64 return lock;
65}
66
67void LayerLockCommand::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 anyLock = false;
79 for (auto layer : selLayers) {
80 if (!layer->isEditable())
81 anyLock = true;
82 }
83 for (auto layer : selLayers) {
84 layer->setEditable(anyLock);
85 }
86
87 update_screen_for_document(writer.document());
88}
89
90Command* CommandFactory::createLayerLockCommand()
91{
92 return new LayerLockCommand;
93}
94
95} // namespace app
96