2014-10-21 09:21:31 +08:00
|
|
|
// Aseprite Document Library
|
2021-04-09 03:15:02 +08:00
|
|
|
// Copyright (C) 2019-2021 Igara Studio S.A.
|
2020-03-17 11:25:24 +08:00
|
|
|
// Copyright (C) 2001-2018 David Capello
|
2014-10-21 09:21:31 +08:00
|
|
|
//
|
|
|
|
// This file is released under the terms of the MIT license.
|
|
|
|
// Read LICENSE.txt for more information.
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2007-09-19 07:57:02 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/layer.h"
|
2011-03-25 00:10:48 +08:00
|
|
|
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/cel.h"
|
2020-08-28 07:32:22 +08:00
|
|
|
#include "doc/grid.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/image.h"
|
2024-09-11 05:02:22 +08:00
|
|
|
#include "doc/layer_tilemap.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/primitives.h"
|
|
|
|
#include "doc/sprite.h"
|
2024-09-11 05:02:22 +08:00
|
|
|
#include "doc/tilesets.h"
|
2011-03-24 22:50:00 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
2015-03-05 23:40:47 +08:00
|
|
|
#include <cstring>
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2014-10-21 09:21:31 +08:00
|
|
|
namespace doc {
|
2008-10-01 09:27:51 +08:00
|
|
|
|
2013-11-10 06:59:05 +08:00
|
|
|
Layer::Layer(ObjectType type, Sprite* sprite)
|
2015-12-11 05:34:25 +08:00
|
|
|
: WithUserData(type)
|
2014-11-17 10:03:30 +08:00
|
|
|
, m_sprite(sprite)
|
|
|
|
, m_parent(NULL)
|
|
|
|
, m_flags(LayerFlags(int(LayerFlags::Visible) | int(LayerFlags::Editable)))
|
2024-06-04 06:00:04 +08:00
|
|
|
, m_blendmode(BlendMode::NORMAL)
|
|
|
|
, m_opacity(255)
|
2008-10-01 09:27:51 +08:00
|
|
|
{
|
2019-03-30 02:57:10 +08:00
|
|
|
ASSERT(type == ObjectType::LayerImage || type == ObjectType::LayerGroup ||
|
|
|
|
type == ObjectType::LayerTilemap);
|
2008-10-01 09:27:51 +08:00
|
|
|
|
2010-09-19 11:03:32 +08:00
|
|
|
setName("Layer");
|
2008-10-01 09:27:51 +08:00
|
|
|
}
|
|
|
|
|
2009-11-17 21:12:26 +08:00
|
|
|
Layer::~Layer()
|
|
|
|
{
|
|
|
|
}
|
2008-10-01 09:27:51 +08:00
|
|
|
|
2010-10-04 02:51:03 +08:00
|
|
|
int Layer::getMemSize() const
|
|
|
|
{
|
|
|
|
return sizeof(Layer);
|
|
|
|
}
|
|
|
|
|
2013-01-11 23:43:25 +08:00
|
|
|
Layer* Layer::getPrevious() const
|
2009-11-17 21:12:26 +08:00
|
|
|
{
|
2016-06-09 03:46:15 +08:00
|
|
|
if (m_parent) {
|
|
|
|
auto it = std::find(m_parent->layers().begin(), m_parent->layers().end(), this);
|
2009-11-17 21:12:26 +08:00
|
|
|
|
2016-06-09 03:46:15 +08:00
|
|
|
if (it != m_parent->layers().end() && it != m_parent->layers().begin()) {
|
2009-11-17 21:12:26 +08:00
|
|
|
it--;
|
|
|
|
return *it;
|
2008-10-01 09:27:51 +08:00
|
|
|
}
|
2009-11-17 21:12:26 +08:00
|
|
|
}
|
2016-06-09 03:46:15 +08:00
|
|
|
return nullptr;
|
2009-11-17 21:12:26 +08:00
|
|
|
}
|
2008-10-01 09:27:51 +08:00
|
|
|
|
2013-01-11 23:43:25 +08:00
|
|
|
Layer* Layer::getNext() const
|
2009-11-17 21:12:26 +08:00
|
|
|
{
|
2016-06-09 03:46:15 +08:00
|
|
|
if (m_parent) {
|
|
|
|
auto it = std::find(m_parent->layers().begin(), m_parent->layers().end(), this);
|
2009-11-17 21:12:26 +08:00
|
|
|
|
2016-06-09 03:46:15 +08:00
|
|
|
if (it != m_parent->layers().end()) {
|
2009-11-17 21:12:26 +08:00
|
|
|
it++;
|
2016-06-09 03:46:15 +08:00
|
|
|
if (it != m_parent->layers().end())
|
2012-01-06 06:45:03 +08:00
|
|
|
return *it;
|
2008-10-01 09:27:51 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-09 03:46:15 +08:00
|
|
|
return nullptr;
|
2008-10-01 09:27:51 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 20:11:54 +08:00
|
|
|
Layer* Layer::getPreviousBrowsable() const
|
2016-06-15 02:00:11 +08:00
|
|
|
{
|
|
|
|
// Go to children
|
2016-06-21 08:15:35 +08:00
|
|
|
if (isBrowsable())
|
2016-06-15 02:00:11 +08:00
|
|
|
return static_cast<const LayerGroup*>(this)->lastLayer();
|
|
|
|
|
|
|
|
// Go to previous layer
|
|
|
|
if (Layer* prev = getPrevious())
|
|
|
|
return prev;
|
|
|
|
|
|
|
|
// Go to previous layer in the parent
|
2016-06-21 08:15:35 +08:00
|
|
|
LayerGroup* parent = this->parent();
|
|
|
|
while (parent != sprite()->root() && !parent->getPrevious()) {
|
|
|
|
parent = parent->parent();
|
|
|
|
}
|
|
|
|
return parent->getPrevious();
|
2016-06-15 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 20:11:54 +08:00
|
|
|
Layer* Layer::getNextBrowsable() const
|
2016-06-15 02:00:11 +08:00
|
|
|
{
|
|
|
|
// Go to next layer
|
|
|
|
if (Layer* next = getNext()) {
|
|
|
|
// Go to children
|
2016-06-21 08:15:35 +08:00
|
|
|
while (next->isBrowsable()) {
|
2016-06-15 02:00:11 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-12-08 20:11:54 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-16 02:27:38 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:24:55 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-02-26 01:10:50 +08:00
|
|
|
bool Layer::hasAncestor(const Layer* ancestor) const
|
|
|
|
{
|
|
|
|
Layer* it = parent();
|
|
|
|
while (it) {
|
|
|
|
if (it == ancestor)
|
|
|
|
return true;
|
|
|
|
it = it->parent();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-28 07:32:22 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-28 22:06:11 +08:00
|
|
|
Cel* Layer::cel(frame_t frame) const
|
|
|
|
{
|
2016-06-09 03:46:15 +08:00
|
|
|
return nullptr;
|
2014-12-28 22:06:11 +08:00
|
|
|
}
|
|
|
|
|
2008-10-01 09:27:51 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
2009-11-17 21:12:26 +08:00
|
|
|
// LayerImage class
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2019-03-30 02:57:10 +08:00
|
|
|
LayerImage::LayerImage(ObjectType type, Sprite* sprite) : Layer(type, sprite)
|
2009-11-17 21:12:26 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-30 02:57:10 +08:00
|
|
|
LayerImage::LayerImage(Sprite* sprite) : LayerImage(ObjectType::LayerImage, sprite)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-11-17 21:12:26 +08:00
|
|
|
LayerImage::~LayerImage()
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2011-03-25 00:15:09 +08:00
|
|
|
destroyAllCels();
|
2009-11-17 21:12:26 +08:00
|
|
|
}
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2010-10-04 02:51:03 +08:00
|
|
|
int LayerImage::getMemSize() const
|
|
|
|
{
|
|
|
|
int size = sizeof(LayerImage);
|
|
|
|
CelConstIterator it = getCelBegin();
|
|
|
|
CelConstIterator end = getCelEnd();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
const Cel* cel = *it;
|
|
|
|
|
2024-10-16 22:31:08 +08:00
|
|
|
if (cel->link()) // Skip link
|
|
|
|
continue;
|
|
|
|
|
|
|
|
size += cel->getMemSize();
|
2010-10-04 02:51:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2011-03-25 00:15:09 +08:00
|
|
|
void LayerImage::destroyAllCels()
|
2009-11-17 21:12:26 +08:00
|
|
|
{
|
2010-09-19 11:15:44 +08:00
|
|
|
CelIterator it = getCelBegin();
|
|
|
|
CelIterator end = getCelEnd();
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2009-11-17 21:12:26 +08:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
Cel* cel = *it;
|
2011-03-28 11:24:27 +08:00
|
|
|
delete cel;
|
2009-11-17 21:12:26 +08:00
|
|
|
}
|
2010-03-26 22:44:27 +08:00
|
|
|
m_cels.clear();
|
2009-11-17 21:12:26 +08:00
|
|
|
}
|
2008-03-01 03:29:49 +08:00
|
|
|
|
2014-12-28 22:06:11 +08:00
|
|
|
Cel* LayerImage::cel(frame_t frame) const
|
|
|
|
{
|
2015-09-15 19:18:52 +08:00
|
|
|
CelConstIterator it = findCelIterator(frame);
|
|
|
|
if (it != getCelEnd())
|
|
|
|
return *it;
|
|
|
|
else
|
|
|
|
return nullptr;
|
2014-12-28 22:06:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-02 22:28:03 +08:00
|
|
|
void LayerImage::getCels(CelList& cels) const
|
2009-11-17 21:12:26 +08:00
|
|
|
{
|
2014-05-02 22:28:03 +08:00
|
|
|
CelConstIterator it = getCelBegin();
|
|
|
|
CelConstIterator end = getCelEnd();
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2009-11-17 21:12:26 +08:00
|
|
|
for (; it != end; ++it)
|
|
|
|
cels.push_back(*it);
|
|
|
|
}
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2014-08-12 18:57:40 +08:00
|
|
|
Cel* LayerImage::getLastCel() const
|
|
|
|
{
|
|
|
|
if (!m_cels.empty())
|
|
|
|
return m_cels.back();
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-15 19:18:52 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-09 22:40:43 +08:00
|
|
|
void LayerImage::addCel(Cel* cel)
|
2009-11-17 21:12:26 +08:00
|
|
|
{
|
2015-07-27 23:40:02 +08:00
|
|
|
ASSERT(cel);
|
2015-02-09 22:40:43 +08:00
|
|
|
ASSERT(cel->data() && "The cel doesn't contain CelData");
|
2015-07-27 23:40:02 +08:00
|
|
|
ASSERT(cel->image());
|
|
|
|
ASSERT(sprite());
|
2019-03-30 02:57:10 +08:00
|
|
|
ASSERT(cel->image()->pixelFormat() == sprite()->pixelFormat() ||
|
|
|
|
cel->image()->pixelFormat() == IMAGE_TILEMAP);
|
2015-02-09 22:40:43 +08:00
|
|
|
|
2015-09-15 19:18:52 +08:00
|
|
|
CelIterator it = findFirstCelIteratorAfter(cel->frame());
|
2009-11-17 21:12:26 +08:00
|
|
|
m_cels.insert(it, cel);
|
2014-07-30 12:28:15 +08:00
|
|
|
|
|
|
|
cel->setParentLayer(this);
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2008-03-23 02:43:56 +08:00
|
|
|
/**
|
2009-11-17 21:12:26 +08:00
|
|
|
* Removes the cel from the layer.
|
2008-03-23 02:43:56 +08:00
|
|
|
*
|
2009-11-17 21:12:26 +08:00
|
|
|
* It doesn't destroy the cel, you have to delete it after calling
|
|
|
|
* this routine.
|
2008-03-23 02:43:56 +08:00
|
|
|
*/
|
2015-01-04 21:58:14 +08:00
|
|
|
void LayerImage::removeCel(Cel* cel)
|
2008-03-23 02:43:56 +08:00
|
|
|
{
|
2015-09-15 19:18:52 +08:00
|
|
|
ASSERT(cel);
|
|
|
|
CelIterator it = findCelIterator(cel->frame());
|
2010-08-04 10:33:44 +08:00
|
|
|
ASSERT(it != m_cels.end());
|
2008-03-23 02:43:56 +08:00
|
|
|
|
2009-11-17 21:12:26 +08:00
|
|
|
m_cels.erase(it);
|
2015-01-04 21:58:14 +08:00
|
|
|
|
|
|
|
cel->setParentLayer(NULL);
|
2008-03-23 02:43:56 +08:00
|
|
|
}
|
|
|
|
|
2014-12-29 07:39:11 +08:00
|
|
|
void LayerImage::moveCel(Cel* cel, frame_t frame)
|
2014-04-10 08:56:06 +08:00
|
|
|
{
|
|
|
|
removeCel(cel);
|
|
|
|
cel->setFrame(frame);
|
2018-07-05 00:38:54 +08:00
|
|
|
cel->incrementVersion(); // TODO this should be in app::cmd module
|
2014-04-10 08:56:06 +08:00
|
|
|
addCel(cel);
|
|
|
|
}
|
|
|
|
|
2008-03-27 22:29:33 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2010-09-19 11:17:21 +08:00
|
|
|
void LayerImage::configureAsBackground()
|
2008-03-27 22:29:33 +08:00
|
|
|
{
|
2014-07-30 12:28:15 +08:00
|
|
|
ASSERT(sprite() != NULL);
|
|
|
|
ASSERT(sprite()->backgroundLayer() == NULL);
|
2008-03-27 22:29:33 +08:00
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
switchFlags(LayerFlags::BackgroundLayerFlags, true);
|
2010-09-19 11:03:32 +08:00
|
|
|
setName("Background");
|
2008-03-27 22:29:33 +08:00
|
|
|
|
2016-06-08 06:38:56 +08:00
|
|
|
sprite()->root()->stackLayer(this, NULL);
|
2008-03-27 22:29:33 +08:00
|
|
|
}
|
|
|
|
|
2015-01-19 09:05:33 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-17 21:12:26 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
2016-06-08 06:38:56 +08:00
|
|
|
// LayerGroup class
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2016-06-08 06:38:56 +08:00
|
|
|
LayerGroup::LayerGroup(Sprite* sprite) : Layer(ObjectType::LayerGroup, sprite)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2020-11-28 06:26:18 +08:00
|
|
|
setName("Group");
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 06:38:56 +08:00
|
|
|
LayerGroup::~LayerGroup()
|
2008-03-27 22:29:33 +08:00
|
|
|
{
|
2010-09-19 11:15:44 +08:00
|
|
|
destroyAllLayers();
|
2008-01-24 00:16:43 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 06:38:56 +08:00
|
|
|
void LayerGroup::destroyAllLayers()
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2016-06-09 03:46:15 +08:00
|
|
|
for (Layer* layer : m_layers)
|
2009-11-17 21:12:26 +08:00
|
|
|
delete layer;
|
2010-03-26 22:44:27 +08:00
|
|
|
m_layers.clear();
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 06:38:56 +08:00
|
|
|
int LayerGroup::getMemSize() const
|
2010-10-04 02:51:03 +08:00
|
|
|
{
|
2016-06-08 06:38:56 +08:00
|
|
|
int size = sizeof(LayerGroup);
|
2010-10-04 02:51:03 +08:00
|
|
|
|
2016-06-09 03:46:15 +08:00
|
|
|
for (const Layer* layer : m_layers) {
|
2010-10-04 02:51:03 +08:00
|
|
|
size += layer->getMemSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-12-08 20:11:54 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-21 23:02:31 +08:00
|
|
|
void LayerGroup::allLayers(LayerList& list) const
|
|
|
|
{
|
|
|
|
for (Layer* child : m_layers) {
|
|
|
|
if (child->isGroup())
|
|
|
|
static_cast<LayerGroup*>(child)->allLayers(list);
|
|
|
|
|
|
|
|
list.push_back(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 01:53:51 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-29 03:56:35 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-21 23:02:31 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 08:19:25 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 23:02:31 +08:00
|
|
|
void LayerGroup::allBrowsableLayers(LayerList& list) const
|
|
|
|
{
|
|
|
|
for (Layer* child : m_layers) {
|
|
|
|
if (child->isBrowsable())
|
|
|
|
static_cast<LayerGroup*>(child)->allBrowsableLayers(list);
|
|
|
|
|
|
|
|
list.push_back(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-07 21:37:39 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 09:58:06 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-08 06:38:56 +08:00
|
|
|
void LayerGroup::getCels(CelList& cels) const
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2016-06-09 03:46:15 +08:00
|
|
|
for (const Layer* layer : m_layers)
|
|
|
|
layer->getCels(cels);
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 06:38:56 +08:00
|
|
|
void LayerGroup::addLayer(Layer* layer)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2009-11-17 21:12:26 +08:00
|
|
|
m_layers.push_back(layer);
|
2013-01-11 23:43:25 +08:00
|
|
|
layer->setParent(this);
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 06:38:56 +08:00
|
|
|
void LayerGroup::removeLayer(Layer* layer)
|
2009-06-01 00:02:32 +08:00
|
|
|
{
|
2016-06-09 03:46:15 +08:00
|
|
|
auto it = std::find(m_layers.begin(), m_layers.end(), layer);
|
2010-08-04 10:33:44 +08:00
|
|
|
ASSERT(it != m_layers.end());
|
2009-11-17 21:12:26 +08:00
|
|
|
m_layers.erase(it);
|
2009-06-01 00:02:32 +08:00
|
|
|
|
2016-06-21 08:15:35 +08:00
|
|
|
layer->setParent(nullptr);
|
2009-06-01 00:02:32 +08:00
|
|
|
}
|
|
|
|
|
2016-06-21 11:02:13 +08:00
|
|
|
void LayerGroup::insertLayer(Layer* layer, Layer* after)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2016-08-25 23:31:00 +08:00
|
|
|
auto after_it = m_layers.begin();
|
2009-11-17 21:12:26 +08:00
|
|
|
if (after) {
|
2016-08-25 23:31:00 +08:00
|
|
|
after_it = std::find(m_layers.begin(), m_layers.end(), after);
|
|
|
|
if (after_it != m_layers.end())
|
|
|
|
++after_it;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
2016-08-25 23:31:00 +08:00
|
|
|
m_layers.insert(after_it, layer);
|
2016-06-21 11:02:13 +08:00
|
|
|
|
|
|
|
layer->setParent(this);
|
|
|
|
}
|
|
|
|
|
2024-09-17 03:00:02 +08:00
|
|
|
void LayerGroup::insertLayerBefore(Layer* layer, Layer* before)
|
|
|
|
{
|
|
|
|
auto before_it = m_layers.end();
|
|
|
|
if (before) {
|
|
|
|
before_it = std::find(m_layers.begin(), m_layers.end(), before);
|
|
|
|
}
|
|
|
|
m_layers.insert(before_it, layer);
|
|
|
|
|
|
|
|
layer->setParent(this);
|
|
|
|
}
|
|
|
|
|
2016-06-21 11:02:13 +08:00
|
|
|
void LayerGroup::stackLayer(Layer* layer, Layer* after)
|
|
|
|
{
|
|
|
|
ASSERT(layer != after);
|
|
|
|
if (layer == after)
|
|
|
|
return;
|
|
|
|
|
|
|
|
removeLayer(layer);
|
|
|
|
insertLayer(layer, after);
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 06:38:56 +08:00
|
|
|
void LayerGroup::displaceFrames(frame_t fromThis, frame_t delta)
|
2015-01-19 09:05:33 +08:00
|
|
|
{
|
|
|
|
for (Layer* layer : m_layers)
|
|
|
|
layer->displaceFrames(fromThis, delta);
|
|
|
|
}
|
|
|
|
|
2014-10-21 09:21:31 +08:00
|
|
|
} // namespace doc
|