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/app.h"
13#include "app/commands/command.h"
14#include "app/context_access.h"
15#include "app/doc_api.h"
16#include "app/modules/gui.h"
17#include "app/tx.h"
18#include "doc/sprite.h"
19#include "ui/ui.h"
20
21namespace app {
22
23class RemoveFrameCommand : public Command {
24public:
25 RemoveFrameCommand();
26
27protected:
28 bool onEnabled(Context* context) override;
29 void onExecute(Context* context) override;
30};
31
32RemoveFrameCommand::RemoveFrameCommand()
33 : Command(CommandId::RemoveFrame(), CmdRecordableFlag)
34{
35}
36
37bool RemoveFrameCommand::onEnabled(Context* context)
38{
39 if (!context->checkFlags(ContextFlags::ActiveDocumentIsWritable |
40 ContextFlags::HasActiveSprite))
41 return false;
42
43 const ContextReader reader(context);
44 const Sprite* sprite(reader.sprite());
45 return
46 sprite &&
47 sprite->totalFrames() > 1;
48}
49
50void RemoveFrameCommand::onExecute(Context* context)
51{
52 ContextWriter writer(context);
53 Doc* document(writer.document());
54 Sprite* sprite(writer.sprite());
55 {
56 Tx tx(writer.context(), "Remove Frame");
57 DocApi api = document->getApi(tx);
58 const Site* site = writer.site();
59 if (site->inTimeline() &&
60 !site->selectedFrames().empty()) {
61 for (frame_t frame : site->selectedFrames().reversed()) {
62 api.removeFrame(sprite, frame);
63 }
64 }
65 else {
66 api.removeFrame(sprite, writer.frame());
67 }
68
69 tx.commit();
70 }
71 update_screen_for_document(document);
72}
73
74Command* CommandFactory::createRemoveFrameCommand()
75{
76 return new RemoveFrameCommand;
77}
78
79} // namespace app
80