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/move_layer.h"
12
13#include "app/doc.h"
14#include "app/doc_event.h"
15#include "doc/layer.h"
16#include "doc/sprite.h"
17
18namespace app {
19namespace cmd {
20
21using namespace doc;
22
23MoveLayer::MoveLayer(Layer* layer,
24 Layer* newParent,
25 Layer* afterThis)
26 : m_layer(layer)
27 , m_oldParent(layer->parent())
28 , m_oldAfterThis(layer->getPrevious())
29 , m_newParent(newParent)
30 , m_newAfterThis(afterThis == layer ? afterThis->getPrevious(): afterThis)
31{
32}
33
34void MoveLayer::onExecute()
35{
36 Layer* layer = m_layer.layer();
37 Layer* afterThis = m_newAfterThis.layer();
38 LayerGroup* oldParent = static_cast<LayerGroup*>(m_oldParent.layer());
39 LayerGroup* newParent = static_cast<LayerGroup*>(m_newParent.layer());
40 ASSERT(layer);
41 ASSERT(oldParent);
42 ASSERT(newParent);
43
44#if _DEBUG // Check that we are not inserting a layer inside itself
45 {
46 Layer* p = newParent;
47 while (p) {
48 ASSERT(p != layer);
49 p = p->parent();
50 }
51 }
52#endif
53
54 oldParent->removeLayer(layer);
55 newParent->insertLayer(layer, afterThis);
56
57 if (oldParent != newParent)
58 oldParent->incrementVersion();
59 newParent->incrementVersion();
60 layer->sprite()->incrementVersion();
61}
62
63void MoveLayer::onUndo()
64{
65 Layer* layer = m_layer.layer();
66 Layer* afterThis = m_oldAfterThis.layer();
67 LayerGroup* oldParent = static_cast<LayerGroup*>(m_oldParent.layer());
68 LayerGroup* newParent = static_cast<LayerGroup*>(m_newParent.layer());
69 ASSERT(layer);
70 ASSERT(oldParent);
71 ASSERT(newParent);
72
73#if _DEBUG // Check that we are not inserting a layer inside itself
74 {
75 Layer* p = newParent;
76 while (p) {
77 ASSERT(p != layer);
78 p = p->parent();
79 }
80 }
81#endif
82
83 newParent->removeLayer(layer);
84 oldParent->insertLayer(layer, afterThis);
85
86 if (oldParent != newParent)
87 oldParent->incrementVersion();
88 newParent->incrementVersion();
89 layer->sprite()->incrementVersion();
90}
91
92void MoveLayer::onFireNotifications()
93{
94 Layer* layer = m_layer.layer();
95 Doc* doc = static_cast<Doc*>(layer->sprite()->document());
96 DocEvent ev(doc);
97 ev.sprite(layer->sprite());
98 ev.layer(layer);
99 doc->notify_observers<DocEvent&>(&DocObserver::onLayerRestacked, ev);
100}
101
102} // namespace cmd
103} // namespace app
104