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/app.h"
12#include "app/cmd/copy_cel.h"
13#include "app/commands/command.h"
14#include "app/context_access.h"
15#include "app/i18n/strings.h"
16#include "app/modules/gui.h"
17#include "app/tx.h"
18#include "app/ui/status_bar.h"
19#include "doc/cel.h"
20#include "doc/layer.h"
21#include "doc/sprite.h"
22
23namespace app {
24
25class LinkCelsCommand : public Command {
26public:
27 LinkCelsCommand();
28
29protected:
30 bool onEnabled(Context* context) override;
31 void onExecute(Context* context) override;
32};
33
34LinkCelsCommand::LinkCelsCommand()
35 : Command(CommandId::LinkCels(), CmdRecordableFlag)
36{
37}
38
39bool LinkCelsCommand::onEnabled(Context* context)
40{
41 if (context->checkFlags(ContextFlags::ActiveDocumentIsWritable)) {
42 auto site = context->activeSite();
43 return (site.inTimeline() &&
44 site.selectedFrames().size() > 1);
45 }
46 else
47 return false;
48}
49
50void LinkCelsCommand::onExecute(Context* context)
51{
52 ContextWriter writer(context);
53 Doc* document(writer.document());
54 bool nonEditableLayers = false;
55 {
56 auto site = context->activeSite();
57 if (!site.inTimeline())
58 return;
59
60 Tx tx(writer.context(), friendlyName());
61
62 for (Layer* layer : site.selectedLayers()) {
63 if (!layer->isImage())
64 continue;
65
66 if (!layer->isEditableHierarchy()) {
67 nonEditableLayers = true;
68 continue;
69 }
70
71 LayerImage* layerImage = static_cast<LayerImage*>(layer);
72
73 for (auto it=site.selectedFrames().begin(), end=site.selectedFrames().end();
74 it != end; ++it) {
75 frame_t frame = *it;
76 Cel* cel = layerImage->cel(frame);
77 if (cel) {
78 for (++it; it != end; ++it) {
79 tx(
80 new cmd::CopyCel(
81 layerImage, cel->frame(),
82 layerImage, *it,
83 true)); // true = force links
84 }
85 break;
86 }
87 }
88 }
89
90 tx.commit();
91 }
92
93 if (nonEditableLayers)
94 StatusBar::instance()->showTip(1000,
95 Strings::statusbar_tips_locked_layers());
96
97 update_screen_for_document(document);
98}
99
100Command* CommandFactory::createLinkCelsCommand()
101{
102 return new LinkCelsCommand;
103}
104
105} // namespace app
106