forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer.cpp
607 lines (508 loc) · 12.7 KB
/
layer.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// Aseprite Document Library
// Copyright (C) 2019-2021 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "doc/layer.h"
#include "doc/cel.h"
#include "doc/grid.h"
#include "doc/image.h"
#include "doc/primitives.h"
#include "doc/sprite.h"
#include <algorithm>
#include <cstring>
namespace doc {
Layer::Layer(ObjectType type, Sprite* sprite)
: WithUserData(type)
, m_sprite(sprite)
, m_parent(NULL)
, m_flags(LayerFlags(
int(LayerFlags::Visible) |
int(LayerFlags::Editable)))
{
ASSERT(type == ObjectType::LayerImage ||
type == ObjectType::LayerGroup ||
type == ObjectType::LayerTilemap);
setName("Layer");
}
Layer::~Layer()
{
}
int Layer::getMemSize() const
{
return sizeof(Layer);
}
Layer* Layer::getPrevious() const
{
if (m_parent) {
auto it =
std::find(m_parent->layers().begin(),
m_parent->layers().end(), this);
if (it != m_parent->layers().end() &&
it != m_parent->layers().begin()) {
it--;
return *it;
}
}
return nullptr;
}
Layer* Layer::getNext() const
{
if (m_parent) {
auto it =
std::find(m_parent->layers().begin(),
m_parent->layers().end(), this);
if (it != m_parent->layers().end()) {
it++;
if (it != m_parent->layers().end())
return *it;
}
}
return nullptr;
}
Layer* Layer::getPreviousBrowsable() const
{
// Go to children
if (isBrowsable())
return static_cast<const LayerGroup*>(this)->lastLayer();
// Go to previous layer
if (Layer* prev = getPrevious())
return prev;
// Go to previous layer in the parent
LayerGroup* parent = this->parent();
while (parent != sprite()->root() &&
!parent->getPrevious()) {
parent = parent->parent();
}
return parent->getPrevious();
}
Layer* Layer::getNextBrowsable() const
{
// Go to next layer
if (Layer* next = getNext()) {
// Go to children
while (next->isBrowsable()) {
Layer* firstChild = static_cast<const LayerGroup*>(next)->firstLayer();
if (!firstChild)
break;
next = firstChild;
}
return next;
}
// Go to parent
if (m_sprite && parent() != m_sprite->root())
return m_parent;
return nullptr;
}
Layer* Layer::getPreviousInWholeHierarchy() const
{
// Go to children
if (isGroup() && static_cast<const LayerGroup*>(this)->layersCount() > 0)
return static_cast<const LayerGroup*>(this)->lastLayer();
// Go to previous layer
if (Layer* prev = getPrevious())
return prev;
// Go to previous layer in the parent
LayerGroup* parent = this->parent();
while (parent != sprite()->root() &&
!parent->getPrevious()) {
parent = parent->parent();
}
return parent->getPrevious();
}
Layer* Layer::getNextInWholeHierarchy() const
{
// Go to next layer
if (Layer* next = getNext()) {
// Go to children
while (next->isGroup() && static_cast<const LayerGroup*>(next)->layersCount() > 0) {
Layer* firstChild = static_cast<const LayerGroup*>(next)->firstLayer();
if (!firstChild)
break;
next = firstChild;
}
return next;
}
// Go to parent
if (m_sprite && parent() != m_sprite->root())
return m_parent;
return nullptr;
}
bool Layer::isVisibleHierarchy() const
{
const Layer* layer = this;
while (layer) {
if (!layer->isVisible())
return false;
layer = layer->parent();
}
return true;
}
bool Layer::isEditableHierarchy() const
{
const Layer* layer = this;
while (layer) {
if (!layer->isEditable())
return false;
layer = layer->parent();
}
return true;
}
// It's like isVisibleHierarchy + isEditableHierarchy. Returns true if
// the whole layer hierarchy is unlocked and visible, so the user can
// edit its pixels without unexpected side-effects (e.g. editing
// hidden layers).
bool Layer::canEditPixels() const
{
const Layer* layer = this;
while (layer) {
if (!layer->isVisible() ||
!layer->isEditable() ||
layer->isReference()) { // Cannot edit pixels from reference layers
return false;
}
layer = layer->parent();
}
return true;
}
bool Layer::hasAncestor(const Layer* ancestor) const
{
Layer* it = parent();
while (it) {
if (it == ancestor)
return true;
it = it->parent();
}
return false;
}
Grid Layer::grid() const
{
gfx::Rect rc = (m_sprite ? m_sprite->gridBounds():
doc::Sprite::DefaultGridBounds());
doc::Grid grid = Grid(rc.size());
grid.origin(gfx::Point(rc.x % rc.w, rc.y % rc.h));
return grid;
}
Cel* Layer::cel(frame_t frame) const
{
return nullptr;
}
//////////////////////////////////////////////////////////////////////
// LayerImage class
LayerImage::LayerImage(ObjectType type, Sprite* sprite)
: Layer(type, sprite)
, m_blendmode(BlendMode::NORMAL)
, m_opacity(255)
{
}
LayerImage::LayerImage(Sprite* sprite)
: LayerImage(ObjectType::LayerImage, sprite)
{
}
LayerImage::~LayerImage()
{
destroyAllCels();
}
int LayerImage::getMemSize() const
{
int size = sizeof(LayerImage);
CelConstIterator it = getCelBegin();
CelConstIterator end = getCelEnd();
for (; it != end; ++it) {
const Cel* cel = *it;
size += cel->getMemSize();
const Image* image = cel->image();
size += image->getMemSize();
}
return size;
}
void LayerImage::destroyAllCels()
{
CelIterator it = getCelBegin();
CelIterator end = getCelEnd();
for (; it != end; ++it) {
Cel* cel = *it;
delete cel;
}
m_cels.clear();
}
Cel* LayerImage::cel(frame_t frame) const
{
CelConstIterator it = findCelIterator(frame);
if (it != getCelEnd())
return *it;
else
return nullptr;
}
void LayerImage::getCels(CelList& cels) const
{
CelConstIterator it = getCelBegin();
CelConstIterator end = getCelEnd();
for (; it != end; ++it)
cels.push_back(*it);
}
Cel* LayerImage::getLastCel() const
{
if (!m_cels.empty())
return m_cels.back();
else
return NULL;
}
CelConstIterator LayerImage::findCelIterator(frame_t frame) const
{
CelIterator it = const_cast<LayerImage*>(this)->findCelIterator(frame);
return CelConstIterator(it);
}
CelIterator LayerImage::findCelIterator(frame_t frame)
{
auto first = getCelBegin();
auto end = getCelEnd();
// Here we use a binary search to find the first cel equal to "frame" (or after frame)
first = std::lower_bound(
first, end, nullptr,
[frame](Cel* cel, Cel*) -> bool {
return cel->frame() < frame;
});
// We return the iterator only if it's an exact match
if (first != end && (*first)->frame() == frame)
return first;
else
return end;
}
CelIterator LayerImage::findFirstCelIteratorAfter(frame_t firstAfterFrame)
{
auto first = getCelBegin();
auto end = getCelEnd();
// Here we use a binary search to find the first cel after the given frame
first = std::lower_bound(
first, end, nullptr,
[firstAfterFrame](Cel* cel, Cel*) -> bool {
return cel->frame() <= firstAfterFrame;
});
return first;
}
void LayerImage::addCel(Cel* cel)
{
ASSERT(cel);
ASSERT(cel->data() && "The cel doesn't contain CelData");
ASSERT(cel->image());
ASSERT(sprite());
ASSERT(cel->image()->pixelFormat() == sprite()->pixelFormat() ||
cel->image()->pixelFormat() == IMAGE_TILEMAP);
CelIterator it = findFirstCelIteratorAfter(cel->frame());
m_cels.insert(it, cel);
cel->setParentLayer(this);
}
/**
* Removes the cel from the layer.
*
* It doesn't destroy the cel, you have to delete it after calling
* this routine.
*/
void LayerImage::removeCel(Cel* cel)
{
ASSERT(cel);
CelIterator it = findCelIterator(cel->frame());
ASSERT(it != m_cels.end());
m_cels.erase(it);
cel->setParentLayer(NULL);
}
void LayerImage::moveCel(Cel* cel, frame_t frame)
{
removeCel(cel);
cel->setFrame(frame);
cel->incrementVersion(); // TODO this should be in app::cmd module
addCel(cel);
}
/**
* Configures some properties of the specified layer to make it as the
* "Background" of the sprite.
*
* You can't use this routine if the sprite already has a background
* layer.
*/
void LayerImage::configureAsBackground()
{
ASSERT(sprite() != NULL);
ASSERT(sprite()->backgroundLayer() == NULL);
switchFlags(LayerFlags::BackgroundLayerFlags, true);
setName("Background");
sprite()->root()->stackLayer(this, NULL);
}
void LayerImage::displaceFrames(frame_t fromThis, frame_t delta)
{
Sprite* sprite = this->sprite();
if (delta > 0) {
for (frame_t c=sprite->lastFrame(); c>=fromThis; --c) {
if (Cel* cel = this->cel(c))
moveCel(cel, c+delta);
}
}
else {
for (frame_t c=fromThis; c<=sprite->lastFrame(); ++c) {
if (Cel* cel = this->cel(c))
moveCel(cel, c+delta);
}
}
}
//////////////////////////////////////////////////////////////////////
// LayerGroup class
LayerGroup::LayerGroup(Sprite* sprite)
: Layer(ObjectType::LayerGroup, sprite)
{
setName("Group");
}
LayerGroup::~LayerGroup()
{
destroyAllLayers();
}
void LayerGroup::destroyAllLayers()
{
for (Layer* layer : m_layers)
delete layer;
m_layers.clear();
}
int LayerGroup::getMemSize() const
{
int size = sizeof(LayerGroup);
for (const Layer* layer : m_layers) {
size += layer->getMemSize();
}
return size;
}
Layer* LayerGroup::firstLayerInWholeHierarchy() const
{
Layer* layer = firstLayer();
if (layer) {
while (layer->isGroup() &&
static_cast<LayerGroup*>(layer)->layersCount() > 0) {
layer = static_cast<LayerGroup*>(layer)->firstLayer();
}
}
return layer;
}
void LayerGroup::allLayers(LayerList& list) const
{
for (Layer* child : m_layers) {
if (child->isGroup())
static_cast<LayerGroup*>(child)->allLayers(list);
list.push_back(child);
}
}
layer_t LayerGroup::allLayersCount() const
{
layer_t count = 0;
for (Layer* child : m_layers) {
if (child->isGroup())
count += static_cast<LayerGroup*>(child)->allLayersCount();
++count;
}
return count;
}
bool LayerGroup::hasVisibleReferenceLayers() const
{
for (Layer* child : m_layers) {
if ((child->isReference() && child->isVisible())
|| (child->isGroup()
&& static_cast<LayerGroup*>(child)->hasVisibleReferenceLayers()))
return true;
}
return false;
}
void LayerGroup::allVisibleLayers(LayerList& list) const
{
for (Layer* child : m_layers) {
if (!child->isVisible())
continue;
if (child->isGroup())
static_cast<LayerGroup*>(child)->allVisibleLayers(list);
list.push_back(child);
}
}
void LayerGroup::allVisibleReferenceLayers(LayerList& list) const
{
for (Layer* child : m_layers) {
if (!child->isVisible())
continue;
if (child->isGroup())
static_cast<LayerGroup*>(child)->allVisibleReferenceLayers(list);
if (!child->isReference())
continue;
list.push_back(child);
}
}
void LayerGroup::allBrowsableLayers(LayerList& list) const
{
for (Layer* child : m_layers) {
if (child->isBrowsable())
static_cast<LayerGroup*>(child)->allBrowsableLayers(list);
list.push_back(child);
}
}
void LayerGroup::allTilemaps(LayerList& list) const
{
for (Layer* child : m_layers) {
if (child->isGroup())
static_cast<LayerGroup*>(child)->allTilemaps(list);
if (child->isTilemap())
list.push_back(child);
}
}
std::string LayerGroup::visibleLayerHierarchyAsString(const std::string& indent) const
{
std::string str;
for (Layer* child : m_layers) {
if (!child->isVisible())
continue;
str += indent + child->name() + (child->isGroup() ? "/" : "") + "\n";
if (child->isGroup())
str += static_cast<LayerGroup*>(child)->visibleLayerHierarchyAsString(indent+" ");
}
return str;
}
void LayerGroup::getCels(CelList& cels) const
{
for (const Layer* layer : m_layers)
layer->getCels(cels);
}
void LayerGroup::addLayer(Layer* layer)
{
m_layers.push_back(layer);
layer->setParent(this);
}
void LayerGroup::removeLayer(Layer* layer)
{
auto it = std::find(m_layers.begin(), m_layers.end(), layer);
ASSERT(it != m_layers.end());
m_layers.erase(it);
layer->setParent(nullptr);
}
void LayerGroup::insertLayer(Layer* layer, Layer* after)
{
auto after_it = m_layers.begin();
if (after) {
after_it = std::find(m_layers.begin(), m_layers.end(), after);
if (after_it != m_layers.end())
++after_it;
}
m_layers.insert(after_it, layer);
layer->setParent(this);
}
void LayerGroup::stackLayer(Layer* layer, Layer* after)
{
ASSERT(layer != after);
if (layer == after)
return;
removeLayer(layer);
insertLayer(layer, after);
}
void LayerGroup::displaceFrames(frame_t fromThis, frame_t delta)
{
for (Layer* layer : m_layers)
layer->displaceFrames(fromThis, delta);
}
} // namespace doc