1// Aseprite
2// Copyright (C) 2019-2020 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/background_from_layer.h"
13#include "app/commands/command.h"
14#include "app/context_access.h"
15#include "app/modules/gui.h"
16#include "app/tx.h"
17#include "doc/layer.h"
18#include "doc/sprite.h"
19
20namespace app {
21
22class BackgroundFromLayerCommand : public Command {
23public:
24 BackgroundFromLayerCommand();
25
26protected:
27 bool onEnabled(Context* context) override;
28 void onExecute(Context* context) override;
29};
30
31BackgroundFromLayerCommand::BackgroundFromLayerCommand()
32 : Command(CommandId::BackgroundFromLayer(), CmdRecordableFlag)
33{
34}
35
36bool BackgroundFromLayerCommand::onEnabled(Context* context)
37{
38 return
39 context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
40 ContextFlags::ActiveLayerIsVisible |
41 ContextFlags::ActiveLayerIsEditable |
42 ContextFlags::ActiveLayerIsImage) &&
43 // Doesn't have a background layer
44 !context->checkFlags(ContextFlags::HasBackgroundLayer) &&
45 // Isn't a reference layer
46 !context->checkFlags(ContextFlags::ActiveLayerIsReference) &&
47 // Isn't a tilemap layer
48 // TODO support background tilemaps
49 !context->checkFlags(ContextFlags::ActiveLayerIsTilemap);
50}
51
52void BackgroundFromLayerCommand::onExecute(Context* context)
53{
54 ContextWriter writer(context);
55 Doc* document(writer.document());
56
57 {
58 Tx tx(writer.context(), friendlyName());
59 tx(new cmd::BackgroundFromLayer(static_cast<LayerImage*>(writer.layer())));
60 tx.commit();
61 }
62
63#ifdef ENABLE_UI
64 if (context->isUIAvailable())
65 update_screen_for_document(document);
66#endif
67}
68
69Command* CommandFactory::createBackgroundFromLayerCommand()
70{
71 return new BackgroundFromLayerCommand;
72}
73
74} // namespace app
75