1// Aseprite
2// Copyright (C) 2019 Igara Studio S.A.
3// Copyright (C) 2017 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/add_slice.h"
13
14#include "app/doc.h"
15#include "app/doc_event.h"
16#include "doc/slice.h"
17#include "doc/slice_io.h"
18#include "doc/sprite.h"
19
20namespace app {
21namespace cmd {
22
23using namespace doc;
24
25AddSlice::AddSlice(Sprite* sprite, Slice* slice)
26 : WithSprite(sprite)
27 , WithSlice(slice)
28 , m_size(0)
29{
30}
31
32void AddSlice::onExecute()
33{
34 Sprite* sprite = this->sprite();
35 Slice* slice = this->slice();
36
37 addSlice(sprite, slice);
38}
39
40void AddSlice::onUndo()
41{
42 Sprite* sprite = this->sprite();
43 Slice* slice = this->slice();
44 write_slice(m_stream, slice);
45 m_size = size_t(m_stream.tellp());
46
47 removeSlice(sprite, slice);
48}
49
50void AddSlice::onRedo()
51{
52 Sprite* sprite = this->sprite();
53 Slice* slice = read_slice(m_stream);
54
55 addSlice(sprite, slice);
56
57 m_stream.str(std::string());
58 m_stream.clear();
59 m_size = 0;
60}
61
62void AddSlice::addSlice(Sprite* sprite, Slice* slice)
63{
64 sprite->slices().add(slice);
65 sprite->incrementVersion();
66
67 Doc* doc = static_cast<Doc*>(sprite->document());
68 DocEvent ev(doc);
69 ev.sprite(sprite);
70 ev.slice(slice);
71 doc->notify_observers<DocEvent&>(&DocObserver::onAddSlice, ev);
72}
73
74void AddSlice::removeSlice(Sprite* sprite, Slice* slice)
75{
76 Doc* doc = static_cast<Doc*>(sprite->document());
77 DocEvent ev(doc);
78 ev.sprite(sprite);
79 ev.slice(slice);
80 doc->notify_observers<DocEvent&>(&DocObserver::onRemoveSlice, ev);
81
82 sprite->slices().remove(slice);
83 sprite->incrementVersion();
84 delete slice;
85}
86
87} // namespace cmd
88} // namespace app
89