paysages3d/src/definition/Layers.cpp

166 lines
4.4 KiB
C++
Raw Normal View History

2013-10-31 16:59:18 +00:00
#include "Layers.h"
#include "PackStream.h"
#include "Logs.h"
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);
}
Layers::~Layers() {
2013-10-31 16:59:18 +00:00
clear();
delete null_layer;
}
void Layers::save(PackStream *stream) const {
int layer_count = (int)layers.size();
stream->write(&layer_count);
for (int i = 0; i < layer_count; i++) {
stream->write(layers[i]->getName());
layers[i]->save(stream);
}
}
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++) {
int position = addLayer();
if (position >= 0) {
layers[position]->setName(stream->readString());
layers[position]->load(stream);
}
}
}
void Layers::copy(DefinitionNode *destination_) const {
DefinitionNode::copy(destination_);
2013-10-31 23:09:51 +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);
for (auto layer : layers) {
2013-10-31 16:59:18 +00:00
int position = destination->addLayer();
DefinitionNode *new_layer = destination->getLayer(position);
layer->copy(new_layer);
2013-10-31 16:59:18 +00:00
}
}
Layers *Layers::newCopy() const {
Layers *result = new Layers(NULL, getName(), layer_constructor);
copy(result);
return result;
}
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 ?
}
int Layers::count() const {
return layers.size();
2013-10-31 16:59:18 +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];
} 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;
}
}
int Layers::findLayer(DefinitionNode *layer) const {
int i = 0;
for (auto it : layers) {
if (it == layer) {
return i;
}
i++;
2013-10-31 16:59:18 +00:00
}
Logs::warning() << "Layer " << layer << " (" << layer->getName() << " not found, on a total of "
<< (int)layers.size() << std::endl;
return -1;
2013-10-31 16:59:18 +00:00
}
int Layers::addLayer(DefinitionNode *layer) {
if ((int)layers.size() < max_layer_count) {
layers.push_back(layer);
2013-10-31 16:59:18 +00:00
addChild(layer);
return layers.size() - 1;
} else {
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() {
2013-10-31 16:59:18 +00:00
return addLayer(layer_constructor(this));
}
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);
layers.erase(layers.begin() + position);
2013-10-31 16:59:18 +00:00
delete removed;
} else {
Logs::warning() << "Removing unknown layer " << position << " on " << (int)layers.size() << " from '"
<< getName() << "'" << std::endl;
}
2013-10-31 16:59:18 +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) {
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];
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
}
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) {
2013-10-31 16:59:18 +00:00
removeLayer(0);
}
}
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;
}
}
}