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/cmd/add_frame.h"
12
13#include "app/cmd/add_cel.h"
14#include "app/doc.h"
15#include "app/doc_event.h"
16#include "doc/cel.h"
17#include "doc/layer.h"
18#include "doc/primitives.h"
19#include "doc/sprite.h"
20
21namespace app {
22namespace cmd {
23
24using namespace doc;
25
26AddFrame::AddFrame(Sprite* sprite, frame_t newFrame)
27 : WithSprite(sprite)
28 , m_newFrame(newFrame)
29 , m_addCel(nullptr)
30{
31}
32
33void AddFrame::onExecute()
34{
35 Sprite* sprite = this->sprite();
36 auto doc = static_cast<Doc*>(sprite->document());
37
38 sprite->addFrame(m_newFrame);
39 sprite->incrementVersion();
40
41 if (m_addCel) {
42 m_addCel->redo();
43 }
44 else {
45 LayerImage* bglayer = sprite->backgroundLayer();
46 if (bglayer) {
47 ImageRef bgimage(Image::create(sprite->pixelFormat(), sprite->width(), sprite->height()));
48 clear_image(bgimage.get(), doc->bgColor(bglayer));
49 Cel* cel = new Cel(m_newFrame, bgimage);
50 m_addCel.reset(new cmd::AddCel(bglayer, cel));
51 m_addCel->execute(context());
52 }
53 }
54
55 // Notify observers about the new frame.
56 DocEvent ev(doc);
57 ev.sprite(sprite);
58 ev.frame(m_newFrame);
59 doc->notify_observers<DocEvent&>(&DocObserver::onAddFrame, ev);
60}
61
62void AddFrame::onUndo()
63{
64 Sprite* sprite = this->sprite();
65 auto doc = static_cast<Doc*>(sprite->document());
66
67 if (m_addCel)
68 m_addCel->undo();
69
70 sprite->removeFrame(m_newFrame);
71 sprite->incrementVersion();
72
73 // Notify observers about the new frame.
74 DocEvent ev(doc);
75 ev.sprite(sprite);
76 ev.frame(m_newFrame);
77 doc->notify_observers<DocEvent&>(&DocObserver::onRemoveFrame, ev);
78}
79
80} // namespace cmd
81} // namespace app
82