2013-10-31 16:59:18 +00:00
|
|
|
#include "Layers.h"
|
|
|
|
|
2015-08-13 17:49:16 +00:00
|
|
|
#include "PackStream.h"
|
2015-08-12 15:02:27 +00:00
|
|
|
#include "Logs.h"
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
Layers::Layers(DefinitionNode* parent, const std::string &name, LayerConstructor layer_constructor):
|
2015-08-16 21:01:56 +00:00
|
|
|
DefinitionNode(parent, name, "layers" + name), layer_constructor(layer_constructor)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
|
|
|
max_layer_count = 100;
|
|
|
|
null_layer = layer_constructor(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Layers::~Layers()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
delete null_layer;
|
|
|
|
}
|
|
|
|
|
2015-08-13 17:49:16 +00:00
|
|
|
void Layers::save(PackStream *stream) const
|
|
|
|
{
|
|
|
|
int layer_count = (int)layers.size();
|
|
|
|
stream->write(&layer_count);
|
|
|
|
|
2015-08-13 21:46:50 +00:00
|
|
|
for (int i = 0; i < layer_count; i++)
|
|
|
|
{
|
|
|
|
stream->write(layers[i]->getName());
|
|
|
|
layers[i]->save(stream);
|
|
|
|
}
|
2015-08-13 17:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Layers::load(PackStream *stream)
|
|
|
|
{
|
|
|
|
int layer_count;
|
|
|
|
stream->read(&layer_count);
|
|
|
|
|
|
|
|
if (layer_count > max_layer_count)
|
|
|
|
{
|
|
|
|
layer_count = max_layer_count;
|
|
|
|
}
|
|
|
|
clear();
|
|
|
|
for (int i = 0; i < layer_count; i++)
|
|
|
|
{
|
2015-08-13 21:46:50 +00:00
|
|
|
int position = addLayer();
|
|
|
|
if (position >= 0)
|
|
|
|
{
|
|
|
|
layers[position]->setName(stream->readString());
|
|
|
|
layers[position]->load(stream);
|
|
|
|
}
|
2015-08-13 17:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
void Layers::copy(DefinitionNode* destination_) const
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2015-08-12 20:21:10 +00:00
|
|
|
DefinitionNode::copy(destination_);
|
2013-10-31 23:09:51 +00:00
|
|
|
|
2013-11-17 21:47:27 +00:00
|
|
|
Layers* destination = (Layers*)destination_;
|
2013-10-31 16:59:18 +00:00
|
|
|
|
|
|
|
destination->clear();
|
|
|
|
|
|
|
|
destination->max_layer_count = max_layer_count;
|
|
|
|
|
|
|
|
null_layer->copy(destination->null_layer);
|
|
|
|
|
2013-12-10 21:32:58 +00:00
|
|
|
for (auto layer: layers)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
|
|
|
int position = destination->addLayer();
|
2015-08-12 20:21:10 +00:00
|
|
|
DefinitionNode* new_layer = destination->getLayer(position);
|
2013-12-10 21:32:58 +00:00
|
|
|
layer->copy(new_layer);
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-16 18:12:42 +00:00
|
|
|
Layers* Layers::newCopy() const
|
|
|
|
{
|
2015-08-12 15:02:27 +00:00
|
|
|
Layers* result = new Layers(NULL, getName(), layer_constructor);
|
2013-11-16 18:12:42 +00:00
|
|
|
copy(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-31 16:59:18 +00:00
|
|
|
void Layers::setMaxLayerCount(int max_layer_count)
|
|
|
|
{
|
|
|
|
this->max_layer_count = max_layer_count;
|
|
|
|
// TODO Delete overlimit layers ?
|
|
|
|
}
|
|
|
|
|
2013-11-15 22:26:44 +00:00
|
|
|
int Layers::count() const
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
return layers.size();
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
DefinitionNode* Layers::getLayer(int position) const
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
if (position >= 0 and position < (int)layers.size())
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
|
|
|
return layers[position];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-12 22:33:16 +00:00
|
|
|
Logs::warning() << "Asked for a undefined layer " << position << " on a total of " << (int)layers.size() << std::endl;
|
2013-10-31 16:59:18 +00:00
|
|
|
return null_layer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
int Layers::findLayer(DefinitionNode* layer) const
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
int i = 0;
|
|
|
|
for (auto it:layers)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
if (it == layer)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i++;
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
2015-08-12 22:33:16 +00:00
|
|
|
Logs::warning() << "Layer " << layer << " (" << layer->getName() << " not found, on a total of " << (int)layers.size() << std::endl;
|
2013-12-10 21:32:58 +00:00
|
|
|
return -1;
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
int Layers::addLayer(DefinitionNode* layer)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
if ((int)layers.size() < max_layer_count)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
layers.push_back(layer);
|
2013-10-31 16:59:18 +00:00
|
|
|
addChild(layer);
|
|
|
|
return layers.size() - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-12 22:33:16 +00:00
|
|
|
Logs::warning() << "Add layer ignored because limit of " << max_layer_count << " reached" << std::endl;
|
2015-08-12 17:29:28 +00:00
|
|
|
delete layer;
|
2013-10-31 16:59:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Layers::addLayer()
|
|
|
|
{
|
|
|
|
return addLayer(layer_constructor(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layers::removeLayer(int position)
|
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
if (position >= 0 and position < (int)layers.size())
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
2015-08-12 20:21:10 +00:00
|
|
|
DefinitionNode* removed = layers[position];
|
2013-10-31 16:59:18 +00:00
|
|
|
removeChild(removed);
|
2013-12-10 21:32:58 +00:00
|
|
|
layers.erase(layers.begin() + position);
|
2013-10-31 16:59:18 +00:00
|
|
|
delete removed;
|
|
|
|
}
|
2013-12-15 14:33:54 +00:00
|
|
|
else
|
|
|
|
{
|
2015-08-12 22:33:16 +00:00
|
|
|
Logs::warning() << "Removing unknown layer " << position << " on " << (int)layers.size() << " from '" << getName() << "'" << std::endl;
|
2013-12-15 14:33:54 +00:00
|
|
|
}
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
void Layers::removeLayer(DefinitionNode* layer)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
|
|
|
removeLayer(findLayer(layer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layers::moveLayer(int old_position, int new_position)
|
|
|
|
{
|
2013-12-10 21:32:58 +00:00
|
|
|
if (old_position >= 0 and old_position < (int)layers.size() and new_position >= 0 and new_position < (int)layers.size())
|
2013-10-31 23:09:51 +00:00
|
|
|
{
|
2015-08-12 20:21:10 +00:00
|
|
|
DefinitionNode* layer = layers[old_position];
|
2013-12-10 21:32:58 +00:00
|
|
|
layers.erase(layers.begin() + old_position);
|
|
|
|
layers.insert(layers.begin() + new_position, layer);
|
2013-10-31 23:09:51 +00:00
|
|
|
}
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 20:21:10 +00:00
|
|
|
void Layers::moveLayer(DefinitionNode* layer, int new_position)
|
2013-10-31 16:59:18 +00:00
|
|
|
{
|
|
|
|
moveLayer(findLayer(layer), new_position);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layers::clear()
|
|
|
|
{
|
|
|
|
while (layers.size() > 0)
|
|
|
|
{
|
|
|
|
removeLayer(0);
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 21:46:50 +00:00
|
|
|
|
|
|
|
DefinitionNode *Layers::findChildByName(const std::string name)
|
|
|
|
{
|
|
|
|
DefinitionNode *result = DefinitionNode::findChildByName(name);
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int position = addLayer();
|
|
|
|
if (position >= 0)
|
|
|
|
{
|
|
|
|
result = getLayer(position);
|
|
|
|
result->setName(name);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|