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-11-09 21:30:46 +00:00
|
|
|
Layers::Layers(DefinitionNode *parent, const std::string &name, LayerConstructor layer_constructor)
|
|
|
|
: 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);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
Layers::~Layers() {
|
2013-10-31 16:59:18 +00:00
|
|
|
clear();
|
|
|
|
delete null_layer;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void Layers::save(PackStream *stream) const {
|
2015-08-13 17:49:16 +00:00
|
|
|
int layer_count = (int)layers.size();
|
|
|
|
stream->write(&layer_count);
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
for (int i = 0; i < layer_count; i++) {
|
2015-08-13 21:46:50 +00:00
|
|
|
stream->write(layers[i]->getName());
|
|
|
|
layers[i]->save(stream);
|
|
|
|
}
|
2015-08-13 17:49:16 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void Layers::load(PackStream *stream) {
|
2015-08-13 17:49:16 +00:00
|
|
|
int layer_count;
|
|
|
|
stream->read(&layer_count);
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
if (layer_count > max_layer_count) {
|
2015-08-13 17:49:16 +00:00
|
|
|
layer_count = max_layer_count;
|
|
|
|
}
|
|
|
|
clear();
|
2015-11-09 21:30:46 +00:00
|
|
|
for (int i = 0; i < layer_count; i++) {
|
2015-08-13 21:46:50 +00:00
|
|
|
int position = addLayer();
|
2015-11-09 21:30:46 +00:00
|
|
|
if (position >= 0) {
|
2015-08-13 21:46:50 +00:00
|
|
|
layers[position]->setName(stream->readString());
|
|
|
|
layers[position]->load(stream);
|
|
|
|
}
|
2015-08-13 17:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void Layers::copy(DefinitionNode *destination_) const {
|
2015-08-12 20:21:10 +00:00
|
|
|
DefinitionNode::copy(destination_);
|
2013-10-31 23:09:51 +00:00
|
|
|
|
2015-11-09 21:30:46 +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);
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
for (auto layer : layers) {
|
2013-10-31 16:59:18 +00:00
|
|
|
int position = destination->addLayer();
|
2015-11-09 21:30:46 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
Layers *Layers::newCopy() const {
|
|
|
|
Layers *result = new Layers(NULL, getName(), layer_constructor);
|
2013-11-16 18:12:42 +00:00
|
|
|
copy(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void Layers::setMaxLayerCount(int max_layer_count) {
|
2013-10-31 16:59:18 +00:00
|
|
|
this->max_layer_count = max_layer_count;
|
|
|
|
// TODO Delete overlimit layers ?
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
int Layers::count() const {
|
2013-12-10 21:32:58 +00:00
|
|
|
return layers.size();
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
DefinitionNode *Layers::getLayer(int position) const {
|
|
|
|
if (position >= 0 and position < (int)layers.size()) {
|
2013-10-31 16:59:18 +00:00
|
|
|
return layers[position];
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
|
|
|
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-11-09 21:30:46 +00:00
|
|
|
int Layers::findLayer(DefinitionNode *layer) const {
|
2013-12-10 21:32:58 +00:00
|
|
|
int i = 0;
|
2015-11-09 21:30:46 +00:00
|
|
|
for (auto it : layers) {
|
|
|
|
if (it == layer) {
|
2013-12-10 21:32:58 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
i++;
|
2013-10-31 16:59:18 +00:00
|
|
|
}
|
2015-11-09 21:30:46 +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-11-09 21:30:46 +00:00
|
|
|
int Layers::addLayer(DefinitionNode *layer) {
|
|
|
|
if ((int)layers.size() < max_layer_count) {
|
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;
|
2015-11-09 21:30:46 +00:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
int Layers::addLayer() {
|
2013-10-31 16:59:18 +00:00
|
|
|
return addLayer(layer_constructor(this));
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void Layers::removeLayer(int position) {
|
|
|
|
if (position >= 0 and position < (int)layers.size()) {
|
|
|
|
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;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
|
|
|
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-11-09 21:30:46 +00:00
|
|
|
void Layers::removeLayer(DefinitionNode *layer) {
|
2013-10-31 16:59:18 +00:00
|
|
|
removeLayer(findLayer(layer));
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void Layers::moveLayer(int old_position, int new_position) {
|
|
|
|
if (old_position >= 0 and old_position < (int)layers.size() and new_position >= 0 and
|
|
|
|
new_position < (int)layers.size()) {
|
|
|
|
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-11-09 21:30:46 +00:00
|
|
|
void Layers::moveLayer(DefinitionNode *layer, int new_position) {
|
2013-10-31 16:59:18 +00:00
|
|
|
moveLayer(findLayer(layer), new_position);
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void Layers::clear() {
|
|
|
|
while (layers.size() > 0) {
|
2013-10-31 16:59:18 +00:00
|
|
|
removeLayer(0);
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 21:46:50 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
DefinitionNode *Layers::findChildByName(const std::string name) {
|
2015-08-13 21:46:50 +00:00
|
|
|
DefinitionNode *result = DefinitionNode::findChildByName(name);
|
2015-11-09 21:30:46 +00:00
|
|
|
if (result) {
|
2015-08-13 21:46:50 +00:00
|
|
|
return result;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-08-13 21:46:50 +00:00
|
|
|
int position = addLayer();
|
2015-11-09 21:30:46 +00:00
|
|
|
if (position >= 0) {
|
2015-08-13 21:46:50 +00:00
|
|
|
result = getLayer(position);
|
|
|
|
result->setName(name);
|
|
|
|
return result;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-08-13 21:46:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|