forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactive_site_handler.cpp
148 lines (124 loc) · 3.38 KB
/
active_site_handler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/active_site_handler.h"
#include "app/doc.h"
#include "app/doc_event.h"
#include "app/site.h"
#include "doc/layer.h"
namespace app {
ActiveSiteHandler::ActiveSiteHandler()
{
}
ActiveSiteHandler::~ActiveSiteHandler()
{
}
void ActiveSiteHandler::addDoc(Doc* doc)
{
Data data;
if (doc::Layer* layer = doc->sprite()->root()->firstLayer())
data.layer = layer->id();
else
data.layer = doc::NullId;
data.frame = 0;
m_data.insert(std::make_pair(doc, data));
doc->add_observer(this);
}
void ActiveSiteHandler::removeDoc(Doc* doc)
{
auto it = m_data.find(doc);
if (it == m_data.end())
return;
doc->remove_observer(this);
m_data.erase(it);
}
ActiveSiteHandler::Data& ActiveSiteHandler::getData(Doc* doc)
{
auto it = m_data.find(doc);
if (it == m_data.end()) {
addDoc(doc);
it = m_data.find(doc);
ASSERT(it != m_data.end());
}
return it->second;
}
void ActiveSiteHandler::getActiveSiteForDoc(Doc* doc, Site* site)
{
Data& data = getData(doc);
site->document(doc);
site->sprite(doc->sprite());
site->layer(doc::get<doc::Layer>(data.layer));
site->frame(data.frame);
site->selectedColors(data.selectedColors);
}
void ActiveSiteHandler::setActiveLayerInDoc(Doc* doc, doc::Layer* layer)
{
Data& data = getData(doc);
data.layer = (layer ? layer->id(): 0);
}
void ActiveSiteHandler::setActiveFrameInDoc(Doc* doc, doc::frame_t frame)
{
Data& data = getData(doc);
data.frame = frame;
}
void ActiveSiteHandler::setSelectedColorsInDoc(Doc* doc, const doc::PalettePicks& picks)
{
Data& data = getData(doc);
data.selectedColors = picks;
}
void ActiveSiteHandler::onAddLayer(DocEvent& ev)
{
Data& data = getData(ev.document());
data.layer = ev.layer()->id();
}
void ActiveSiteHandler::onAddFrame(DocEvent& ev)
{
Data& data = getData(ev.document());
data.frame = ev.frame();
}
// TODO similar to Timeline::onBeforeRemoveLayer()
void ActiveSiteHandler::onBeforeRemoveLayer(DocEvent& ev)
{
Data& data = getData(ev.document());
Layer* layer = ev.layer();
// If the layer that was removed is the selected one
ASSERT(layer);
if (layer && data.layer == layer->id()) {
LayerGroup* parent = layer->parent();
Layer* layer_select = nullptr;
// Select previous layer, or next layer, or the parent (if it is
// not the main layer of sprite set).
if (layer->getPrevious()) {
layer_select = layer->getPrevious();
}
else if (layer->getNext())
layer_select = layer->getNext();
else if (parent != layer->sprite()->root())
layer_select = parent;
data.layer = (layer_select ? layer_select->id(): 0);
}
}
// TODO similar to Timeline::onRemoveFrame()
void ActiveSiteHandler::onRemoveFrame(DocEvent& ev)
{
Data& data = getData(ev.document());
// Adjust current frame of the data that are in a frame more
// advanced that the removed one.
if (data.frame > ev.frame()) {
--data.frame;
}
// If the data was in the previous "last frame" (current value of
// totalFrames()), we've to adjust it to the new last frame
// (lastFrame())
else if (data.frame >= ev.sprite()->totalFrames()) {
data.frame = ev.sprite()->lastFrame();
}
if (data.frame < ev.frame())
--data.frame;
}
} // namespace app