1// Aseprite
2// Copyright (C) 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/layer_from_background.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 LayerFromBackgroundCommand : public Command {
23public:
24 LayerFromBackgroundCommand();
25
26protected:
27 bool onEnabled(Context* context) override;
28 void onExecute(Context* context) override;
29};
30
31LayerFromBackgroundCommand::LayerFromBackgroundCommand()
32 : Command(CommandId::LayerFromBackground(), CmdRecordableFlag)
33{
34}
35
36bool LayerFromBackgroundCommand::onEnabled(Context* context)
37{
38 return context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
39 ContextFlags::HasActiveSprite |
40 ContextFlags::HasActiveLayer |
41 ContextFlags::ActiveLayerIsVisible |
42 ContextFlags::ActiveLayerIsEditable |
43 ContextFlags::ActiveLayerIsImage |
44 ContextFlags::ActiveLayerIsBackground) &&
45 // Isn't a reference layer
46 !context->checkFlags(ContextFlags::ActiveLayerIsReference);
47}
48
49void LayerFromBackgroundCommand::onExecute(Context* context)
50{
51 ContextWriter writer(context);
52 Doc* document(writer.document());
53 {
54 Tx tx(writer.context(), friendlyName());
55 tx(new cmd::LayerFromBackground(writer.layer()));
56 tx.commit();
57 }
58#ifdef ENABLE_UI
59 if (context->isUIAvailable())
60 update_screen_for_document(document);
61#endif
62}
63
64Command* CommandFactory::createLayerFromBackgroundCommand()
65{
66 return new LayerFromBackgroundCommand;
67}
68
69} // namespace app
70