2007-11-16 18:25:45 +00:00
|
|
|
/* ASE - Allegro Sprite Editor
|
2010-02-01 21:25:40 +00:00
|
|
|
* Copyright (C) 2001-2010 David Capello
|
2007-09-18 23:57:02 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
#include <algorithm>
|
2007-09-18 23:57:02 +00:00
|
|
|
#include <string.h>
|
2008-03-27 14:29:33 +00:00
|
|
|
#include <allegro/unicode.h>
|
2007-09-18 23:57:02 +00:00
|
|
|
|
|
|
|
#include "raster/blend.h"
|
2007-11-16 20:49:40 +00:00
|
|
|
#include "raster/cel.h"
|
2007-09-18 23:57:02 +00:00
|
|
|
#include "raster/image.h"
|
|
|
|
#include "raster/layer.h"
|
2007-12-05 01:30:50 +00:00
|
|
|
#include "raster/sprite.h"
|
2007-09-18 23:57:02 +00:00
|
|
|
#include "raster/stock.h"
|
2008-02-29 19:29:49 +00:00
|
|
|
#include "raster/undo.h"
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2008-09-30 21:01:54 +00:00
|
|
|
static bool has_cels(const Layer* layer, int frame);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2008-10-01 01:27:51 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
2009-11-17 13:12:26 +00:00
|
|
|
// Layer class
|
2008-10-01 01:27:51 +00:00
|
|
|
|
2010-09-18 20:49:13 -03:00
|
|
|
Layer::Layer(GfxObjType type, Sprite* sprite)
|
2008-10-01 01:27:51 +00:00
|
|
|
: GfxObj(type)
|
|
|
|
{
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT(type == GFXOBJ_LAYER_IMAGE || type == GFXOBJ_LAYER_FOLDER);
|
2008-10-01 01:27:51 +00:00
|
|
|
|
2010-09-19 00:03:32 -03:00
|
|
|
setName("Layer");
|
2008-10-01 01:27:51 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
m_sprite = sprite;
|
|
|
|
m_parent = NULL;
|
|
|
|
m_flags =
|
2008-10-01 01:27:51 +00:00
|
|
|
LAYER_IS_READABLE |
|
|
|
|
LAYER_IS_WRITABLE;
|
|
|
|
}
|
|
|
|
|
2010-03-30 21:43:18 -03:00
|
|
|
Layer::Layer(const Layer* src_layer, Sprite* dst_sprite)
|
2010-09-18 20:49:13 -03:00
|
|
|
: GfxObj(src_layer->getType())
|
2008-10-01 01:27:51 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
m_sprite = dst_sprite;
|
|
|
|
m_parent = NULL;
|
|
|
|
m_flags =
|
|
|
|
LAYER_IS_READABLE |
|
|
|
|
LAYER_IS_WRITABLE;
|
2008-10-01 01:27:51 +00:00
|
|
|
|
2010-09-19 00:03:32 -03:00
|
|
|
setName(src_layer->getName());
|
2009-11-17 13:12:26 +00:00
|
|
|
m_flags = src_layer->m_flags;
|
|
|
|
}
|
2008-10-01 01:27:51 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
Layer::~Layer()
|
|
|
|
{
|
|
|
|
}
|
2008-10-01 01:27:51 +00:00
|
|
|
|
2010-10-03 15:51:03 -03:00
|
|
|
int Layer::getMemSize() const
|
|
|
|
{
|
|
|
|
return sizeof(Layer);
|
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
/**
|
|
|
|
* Gets the previous layer of "layer" that are in the parent set.
|
|
|
|
*/
|
|
|
|
Layer* Layer::get_prev() const
|
|
|
|
{
|
|
|
|
if (m_parent != NULL) {
|
|
|
|
LayerConstIterator it =
|
|
|
|
std::find(m_parent->get_layer_begin(),
|
|
|
|
m_parent->get_layer_end(), this);
|
|
|
|
|
|
|
|
if (it != m_parent->get_layer_end() &&
|
|
|
|
it != m_parent->get_layer_begin()) {
|
|
|
|
it--;
|
|
|
|
return *it;
|
2008-10-01 01:27:51 +00:00
|
|
|
}
|
2009-11-17 13:12:26 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-10-01 01:27:51 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
Layer* Layer::get_next() const
|
|
|
|
{
|
|
|
|
if (m_parent != NULL) {
|
|
|
|
LayerConstIterator it =
|
|
|
|
std::find(m_parent->get_layer_begin(),
|
|
|
|
m_parent->get_layer_end(), this);
|
|
|
|
|
|
|
|
if (it != m_parent->get_layer_end()) {
|
|
|
|
it++;
|
|
|
|
if (it != m_parent->get_layer_end())
|
|
|
|
return *it;
|
2008-10-01 01:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-17 13:12:26 +00:00
|
|
|
return NULL;
|
2008-10-01 01:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2009-11-17 13:12:26 +00:00
|
|
|
// LayerImage class
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
LayerImage::LayerImage(Sprite* sprite)
|
|
|
|
: Layer(GFXOBJ_LAYER_IMAGE, sprite)
|
|
|
|
{
|
|
|
|
m_blend_mode = BLEND_MODE_NORMAL;
|
|
|
|
}
|
|
|
|
|
2010-03-30 21:43:18 -03:00
|
|
|
LayerImage::LayerImage(const LayerImage* src_layer, Sprite* dst_sprite)
|
2009-11-17 13:12:26 +00:00
|
|
|
: Layer(src_layer, dst_sprite)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
set_blend_mode(src_layer->get_blend_mode());
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
try {
|
|
|
|
// copy cels
|
2010-09-19 00:15:44 -03:00
|
|
|
CelConstIterator it = src_layer->getCelBegin();
|
|
|
|
CelConstIterator end = src_layer->getCelEnd();
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
for (; it != end; ++it) {
|
2010-03-30 21:43:18 -03:00
|
|
|
const Cel* cel = *it;
|
2009-11-17 13:12:26 +00:00
|
|
|
Cel* cel_copy = cel_new_copy(cel);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT((cel->image >= 0) &&
|
2010-09-30 22:38:26 -03:00
|
|
|
(cel->image < src_layer->getSprite()->getStock()->size()));
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-09-30 22:38:26 -03:00
|
|
|
Image* image = src_layer->getSprite()->getStock()->getImage(cel->image);
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT(image != NULL);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
Image* image_copy = image_new_copy(image);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-09-30 22:38:26 -03:00
|
|
|
cel_copy->image = dst_sprite->getStock()->addImage(image_copy);
|
2010-09-19 15:23:15 -03:00
|
|
|
if (dst_sprite->getUndo()->isEnabled())
|
2010-09-30 23:55:35 -03:00
|
|
|
dst_sprite->getUndo()->undo_add_image(dst_sprite->getStock(), cel_copy->image);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-09-19 00:26:33 -03:00
|
|
|
addCel(cel_copy);
|
2009-11-17 13:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
destroy_all_cels();
|
|
|
|
throw;
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
LayerImage::~LayerImage()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
destroy_all_cels();
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-10-03 15:51:03 -03:00
|
|
|
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 = getSprite()->getStock()->getImage(cel->image);
|
|
|
|
size += image->getMemSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
void LayerImage::destroy_all_cels()
|
|
|
|
{
|
2010-09-19 00:15:44 -03:00
|
|
|
CelIterator it = getCelBegin();
|
|
|
|
CelIterator end = getCelEnd();
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
Cel* cel = *it;
|
2010-09-30 22:38:26 -03:00
|
|
|
Image* image = getSprite()->getStock()->getImage(cel->image);
|
2008-02-29 19:29:49 +00:00
|
|
|
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT(image != NULL);
|
2008-02-29 19:29:49 +00:00
|
|
|
|
2010-09-30 22:38:26 -03:00
|
|
|
getSprite()->getStock()->removeImage(image);
|
2010-03-26 11:44:27 -03:00
|
|
|
image_free(image);
|
2009-11-17 13:12:26 +00:00
|
|
|
cel_free(cel);
|
|
|
|
}
|
2010-03-26 11:44:27 -03:00
|
|
|
m_cels.clear();
|
2009-11-17 13:12:26 +00:00
|
|
|
}
|
2008-02-29 19:29:49 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
void LayerImage::set_blend_mode(int blend_mode)
|
|
|
|
{
|
|
|
|
m_blend_mode = blend_mode;
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-09-19 00:15:44 -03:00
|
|
|
void LayerImage::getCels(CelList& cels)
|
2009-11-17 13:12:26 +00:00
|
|
|
{
|
2010-09-19 00:15:44 -03:00
|
|
|
CelIterator it = getCelBegin();
|
|
|
|
CelIterator end = getCelEnd();
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
for (; it != end; ++it)
|
|
|
|
cels.push_back(*it);
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-09-19 00:26:33 -03:00
|
|
|
void LayerImage::addCel(Cel *cel)
|
2009-11-17 13:12:26 +00:00
|
|
|
{
|
2010-09-19 00:15:44 -03:00
|
|
|
CelIterator it = getCelBegin();
|
|
|
|
CelIterator end = getCelEnd();
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
if ((*it)->frame > cel->frame)
|
|
|
|
break;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
m_cels.insert(it, cel);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2008-03-22 18:43:56 +00:00
|
|
|
/**
|
2009-11-17 13:12:26 +00:00
|
|
|
* Removes the cel from the layer.
|
2008-03-22 18:43:56 +00:00
|
|
|
*
|
2009-11-17 13:12:26 +00:00
|
|
|
* It doesn't destroy the cel, you have to delete it after calling
|
|
|
|
* this routine.
|
2008-03-22 18:43:56 +00:00
|
|
|
*/
|
2010-09-19 00:26:33 -03:00
|
|
|
void LayerImage::removeCel(Cel *cel)
|
2008-03-22 18:43:56 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
CelIterator it = std::find(m_cels.begin(), m_cels.end(), cel);
|
2008-03-22 18:43:56 +00:00
|
|
|
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT(it != m_cels.end());
|
2008-03-22 18:43:56 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
m_cels.erase(it);
|
2008-03-22 18:43:56 +00:00
|
|
|
}
|
|
|
|
|
2010-09-19 00:26:33 -03:00
|
|
|
const Cel* LayerImage::getCel(int frame) const
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-09-19 00:15:44 -03:00
|
|
|
CelConstIterator it = getCelBegin();
|
|
|
|
CelConstIterator end = getCelEnd();
|
2008-03-27 14:29:33 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
const Cel* cel = *it;
|
|
|
|
if (cel->frame == frame)
|
|
|
|
return cel;
|
|
|
|
}
|
2008-03-27 14:29:33 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-27 14:29:33 +00:00
|
|
|
|
2010-09-19 00:26:33 -03:00
|
|
|
Cel* LayerImage::getCel(int frame)
|
2009-11-17 13:12:26 +00:00
|
|
|
{
|
2010-09-19 00:15:44 -03:00
|
|
|
CelIterator it = getCelBegin();
|
|
|
|
CelIterator end = getCelEnd();
|
2008-03-27 14:29:33 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
Cel* cel = *it;
|
|
|
|
if (cel->frame == frame)
|
|
|
|
return cel;
|
2008-03-27 14:29:33 +00:00
|
|
|
}
|
2009-11-17 13:12:26 +00:00
|
|
|
|
|
|
|
return NULL;
|
2008-03-27 14:29:33 +00: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 00:17:21 -03:00
|
|
|
void LayerImage::configureAsBackground()
|
2008-03-27 14:29:33 +00:00
|
|
|
{
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT(getSprite() != NULL);
|
|
|
|
ASSERT(getSprite()->getBackgroundLayer() == NULL);
|
2008-03-27 14:29:33 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
*flags_addr() |= LAYER_IS_LOCKMOVE | LAYER_IS_BACKGROUND;
|
2010-09-19 00:03:32 -03:00
|
|
|
setName("Background");
|
2008-03-27 14:29:33 +00:00
|
|
|
|
2010-03-30 21:43:18 -03:00
|
|
|
getSprite()->getFolder()->move_layer(this, NULL);
|
2008-03-27 14:29:33 +00:00
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// LayerFolder class
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
LayerFolder::LayerFolder(Sprite* sprite)
|
|
|
|
: Layer(GFXOBJ_LAYER_FOLDER, sprite)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-09-19 00:03:32 -03:00
|
|
|
setName("Layer Set");
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-03-30 21:43:18 -03:00
|
|
|
LayerFolder::LayerFolder(const LayerFolder* src_layer, Sprite* dst_sprite)
|
2009-11-17 13:12:26 +00:00
|
|
|
: Layer(src_layer, dst_sprite)
|
2008-01-23 16:16:43 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
try {
|
2010-03-30 21:43:18 -03:00
|
|
|
LayerConstIterator it = src_layer->get_layer_begin();
|
|
|
|
LayerConstIterator end = src_layer->get_layer_end();
|
2008-01-23 16:16:43 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
// duplicate the child
|
|
|
|
Layer* child_copy = (*it)->duplicate_for(dst_sprite);
|
2008-03-27 14:29:33 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
// add the new child in the layer copy
|
|
|
|
add_layer(child_copy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...) {
|
2010-09-19 00:15:44 -03:00
|
|
|
destroyAllLayers();
|
2009-11-17 13:12:26 +00:00
|
|
|
throw;
|
|
|
|
}
|
2008-03-27 14:29:33 +00:00
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
LayerFolder::~LayerFolder()
|
2008-03-27 14:29:33 +00:00
|
|
|
{
|
2010-09-19 00:15:44 -03:00
|
|
|
destroyAllLayers();
|
2008-01-23 16:16:43 +00:00
|
|
|
}
|
|
|
|
|
2010-09-19 00:15:44 -03:00
|
|
|
void LayerFolder::destroyAllLayers()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
LayerIterator it = get_layer_begin();
|
|
|
|
LayerIterator end = get_layer_end();
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
Layer* layer = *it;
|
|
|
|
delete layer;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
2010-03-26 11:44:27 -03:00
|
|
|
m_layers.clear();
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-10-03 15:51:03 -03:00
|
|
|
int LayerFolder::getMemSize() const
|
|
|
|
{
|
|
|
|
int size = sizeof(LayerFolder);
|
|
|
|
LayerConstIterator it = get_layer_begin();
|
|
|
|
LayerConstIterator end = get_layer_end();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
const Layer* layer = *it;
|
|
|
|
size += layer->getMemSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2010-09-19 00:15:44 -03:00
|
|
|
void LayerFolder::getCels(CelList& cels)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
LayerIterator it = get_layer_begin();
|
|
|
|
LayerIterator end = get_layer_end();
|
|
|
|
|
|
|
|
for (; it != end; ++it)
|
2010-09-19 00:15:44 -03:00
|
|
|
(*it)->getCels(cels);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
void LayerFolder::add_layer(Layer* layer)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
m_layers.push_back(layer);
|
|
|
|
layer->set_parent(this);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
void LayerFolder::remove_layer(Layer* layer)
|
2009-05-31 16:02:32 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
LayerIterator it = std::find(m_layers.begin(), m_layers.end(), layer);
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT(it != m_layers.end());
|
2009-11-17 13:12:26 +00:00
|
|
|
m_layers.erase(it);
|
2009-05-31 16:02:32 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
layer->set_parent(NULL);
|
2009-05-31 16:02:32 +00:00
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
void LayerFolder::move_layer(Layer* layer, Layer* after)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
LayerIterator it = std::find(m_layers.begin(), m_layers.end(), layer);
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT(it != m_layers.end());
|
2009-11-17 13:12:26 +00:00
|
|
|
m_layers.erase(it);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
if (after) {
|
|
|
|
LayerIterator after_it = std::find(m_layers.begin(), m_layers.end(), after);
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT(after_it != m_layers.end());
|
2009-11-17 13:12:26 +00:00
|
|
|
after_it++;
|
|
|
|
m_layers.insert(after_it, layer);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
2009-11-17 13:12:26 +00:00
|
|
|
else
|
|
|
|
m_layers.push_front(layer);
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// if (after) {
|
|
|
|
// JLink before = jlist_find(m_layers, after)->next;
|
|
|
|
// jlist_insert_before(m_layers, before, layer);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// jlist_prepend(m_layers, layer);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-10-02 02:31:07 +00:00
|
|
|
/**
|
2009-11-17 13:12:26 +00:00
|
|
|
* Returns a new layer (flat_layer) with all "layer" rendered frame by
|
|
|
|
* frame from "frmin" to "frmax" (inclusive). "layer" can be a set of
|
|
|
|
* layers, so the routine flattens all children to an unique output
|
|
|
|
* layer.
|
2008-10-02 02:31:07 +00:00
|
|
|
*
|
2009-11-17 13:12:26 +00:00
|
|
|
* @param dst_sprite The sprite where to put the new flattened layer.
|
|
|
|
* @param src_layer Generally a set of layers to be flattened.
|
2008-10-02 02:31:07 +00:00
|
|
|
*/
|
2009-11-17 13:12:26 +00:00
|
|
|
Layer* layer_new_flatten_copy(Sprite* dst_sprite, const Layer* src_layer,
|
|
|
|
int x, int y, int w, int h, int frmin, int frmax)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
LayerImage* flat_layer = new LayerImage(dst_sprite);
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (int frame=frmin; frame<=frmax; frame++) {
|
|
|
|
/* does this frame have cels to render? */
|
|
|
|
if (has_cels(src_layer, frame)) {
|
|
|
|
/* create a new image */
|
2010-03-30 21:43:18 -03:00
|
|
|
Image* image = image_new(flat_layer->getSprite()->getImgType(), w, h);
|
2009-11-17 13:12:26 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
/* create the new cel for the output layer (add the image to
|
|
|
|
stock too) */
|
2010-09-30 22:38:26 -03:00
|
|
|
Cel* cel = cel_new(frame, flat_layer->getSprite()->getStock()->addImage(image));
|
2009-11-17 13:12:26 +00:00
|
|
|
cel_set_position(cel, x, y);
|
|
|
|
|
|
|
|
/* clear the image and render this frame */
|
|
|
|
image_clear(image, 0);
|
|
|
|
layer_render(src_layer, image, -x, -y, frame);
|
2010-09-19 00:26:33 -03:00
|
|
|
flat_layer->addCel(cel);
|
2009-11-17 13:12:26 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
delete image;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-17 13:12:26 +00:00
|
|
|
catch (...) {
|
|
|
|
delete flat_layer;
|
|
|
|
throw;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
2009-11-17 13:12:26 +00:00
|
|
|
|
|
|
|
return flat_layer;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 21:01:54 +00:00
|
|
|
void layer_render(const Layer* layer, Image* image, int x, int y, int frame)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
if (!layer->is_readable())
|
2007-09-18 23:57:02 +00:00
|
|
|
return;
|
|
|
|
|
2010-09-18 20:49:13 -03:00
|
|
|
switch (layer->getType()) {
|
2007-09-18 23:57:02 +00:00
|
|
|
|
|
|
|
case GFXOBJ_LAYER_IMAGE: {
|
2010-09-19 00:26:33 -03:00
|
|
|
const Cel* cel = static_cast<const LayerImage*>(layer)->getCel(frame);
|
2008-09-30 21:01:54 +00:00
|
|
|
Image* src_image;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2007-11-16 20:49:40 +00:00
|
|
|
if (cel) {
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT((cel->image >= 0) &&
|
2010-09-30 22:38:26 -03:00
|
|
|
(cel->image < layer->getSprite()->getStock()->size()));
|
2008-02-29 19:29:49 +00:00
|
|
|
|
2010-09-30 22:38:26 -03:00
|
|
|
src_image = layer->getSprite()->getStock()->getImage(cel->image);
|
2010-08-03 23:33:44 -03:00
|
|
|
ASSERT(src_image != NULL);
|
2008-02-29 19:29:49 +00:00
|
|
|
|
|
|
|
image_merge(image, src_image,
|
|
|
|
cel->x + x,
|
|
|
|
cel->y + y,
|
|
|
|
MID (0, cel->opacity, 255),
|
2009-11-17 13:12:26 +00:00
|
|
|
static_cast<const LayerImage*>(layer)->get_blend_mode());
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
case GFXOBJ_LAYER_FOLDER: {
|
|
|
|
LayerConstIterator it = static_cast<const LayerFolder*>(layer)->get_layer_begin();
|
|
|
|
LayerConstIterator end = static_cast<const LayerFolder*>(layer)->get_layer_end();
|
|
|
|
|
|
|
|
for (; it != end; ++it)
|
|
|
|
layer_render(*it, image, x, y, frame);
|
|
|
|
|
2007-09-18 23:57:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-23 16:16:43 +00:00
|
|
|
/**
|
2009-11-17 13:12:26 +00:00
|
|
|
* Returns true if the "layer" (or him childs) has cels to render in
|
2008-01-23 16:16:43 +00:00
|
|
|
* frame.
|
|
|
|
*/
|
2008-09-30 21:01:54 +00:00
|
|
|
static bool has_cels(const Layer* layer, int frame)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2009-11-17 13:12:26 +00:00
|
|
|
if (!layer->is_readable())
|
|
|
|
return false;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-09-18 20:49:13 -03:00
|
|
|
switch (layer->getType()) {
|
2007-09-18 23:57:02 +00:00
|
|
|
|
|
|
|
case GFXOBJ_LAYER_IMAGE:
|
2010-09-19 00:26:33 -03:00
|
|
|
return static_cast<const LayerImage*>(layer)->getCel(frame) ? true: false;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
case GFXOBJ_LAYER_FOLDER: {
|
|
|
|
LayerConstIterator it = static_cast<const LayerFolder*>(layer)->get_layer_begin();
|
|
|
|
LayerConstIterator end = static_cast<const LayerFolder*>(layer)->get_layer_end();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
if (has_cels(*it, frame))
|
|
|
|
return true;
|
2007-11-16 20:49:40 +00:00
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-11-17 13:12:26 +00:00
|
|
|
return false;
|
2008-03-22 18:43:56 +00:00
|
|
|
}
|