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_layer.h" |
12 | |
13 | #include "app/doc.h" |
14 | #include "app/doc_event.h" |
15 | #include "doc/layer.h" |
16 | #include "doc/layer_io.h" |
17 | #include "doc/subobjects_io.h" |
18 | |
19 | namespace app { |
20 | namespace cmd { |
21 | |
22 | using namespace doc; |
23 | |
24 | AddLayer::AddLayer(Layer* group, Layer* newLayer, Layer* afterThis) |
25 | : m_group(group) |
26 | , m_newLayer(newLayer) |
27 | , m_afterThis(afterThis) |
28 | , m_size(0) |
29 | { |
30 | } |
31 | |
32 | void AddLayer::onExecute() |
33 | { |
34 | Layer* group = m_group.layer(); |
35 | Layer* newLayer = m_newLayer.layer(); |
36 | Layer* afterThis = m_afterThis.layer(); |
37 | |
38 | addLayer(group, newLayer, afterThis); |
39 | } |
40 | |
41 | void AddLayer::onUndo() |
42 | { |
43 | Layer* group = m_group.layer(); |
44 | Layer* layer = m_newLayer.layer(); |
45 | |
46 | write_layer(m_stream, layer); |
47 | m_size = size_t(m_stream.tellp()); |
48 | |
49 | removeLayer(group, layer); |
50 | } |
51 | |
52 | void AddLayer::onRedo() |
53 | { |
54 | Layer* group = m_group.layer(); |
55 | SubObjectsFromSprite io(group->sprite()); |
56 | Layer* newLayer = read_layer(m_stream, &io); |
57 | Layer* afterThis = m_afterThis.layer(); |
58 | |
59 | addLayer(group, newLayer, afterThis); |
60 | |
61 | m_stream.str(std::string()); |
62 | m_stream.clear(); |
63 | m_size = 0; |
64 | } |
65 | |
66 | void AddLayer::addLayer(Layer* group, Layer* newLayer, Layer* afterThis) |
67 | { |
68 | static_cast<LayerGroup*>(group)->insertLayer(newLayer, afterThis); |
69 | group->incrementVersion(); |
70 | group->sprite()->incrementVersion(); |
71 | |
72 | Doc* doc = static_cast<Doc*>(group->sprite()->document()); |
73 | DocEvent ev(doc); |
74 | ev.sprite(group->sprite()); |
75 | ev.layer(newLayer); |
76 | doc->notify_observers<DocEvent&>(&DocObserver::onAddLayer, ev); |
77 | } |
78 | |
79 | void AddLayer::removeLayer(Layer* group, Layer* layer) |
80 | { |
81 | Doc* doc = static_cast<Doc*>(group->sprite()->document()); |
82 | DocEvent ev(doc); |
83 | ev.sprite(layer->sprite()); |
84 | ev.layer(layer); |
85 | doc->notify_observers<DocEvent&>(&DocObserver::onBeforeRemoveLayer, ev); |
86 | |
87 | static_cast<LayerGroup*>(group)->removeLayer(layer); |
88 | group->incrementVersion(); |
89 | group->sprite()->incrementVersion(); |
90 | |
91 | doc->notify_observers<DocEvent&>(&DocObserver::onAfterRemoveLayer, ev); |
92 | |
93 | delete layer; |
94 | } |
95 | |
96 | } // namespace cmd |
97 | } // namespace app |
98 | |