DefinitionNode.copy now copies children automatically

This commit is contained in:
Michaël Lemaire 2015-08-24 01:23:05 +02:00
parent c199bef7f2
commit 9b2c60fe16
6 changed files with 31 additions and 27 deletions

View file

@ -71,8 +71,6 @@ void AtmosphereDefinition::copy(DefinitionNode* _destination) const
AtmosphereDefinition* destination = (AtmosphereDefinition*)_destination;
daytime->copy(destination->daytime);
destination->model = model;
destination->sun_color = sun_color;
destination->sun_radius = sun_radius;

View file

@ -241,7 +241,18 @@ void DefinitionNode::copy(DefinitionNode* destination) const
if (destination->getTypeName() == getTypeName())
{
destination->setName(name);
// TODO Copy children ?
for (auto &child: children)
{
DefinitionNode *dest_child = destination->findChildByName(child->name);
if (dest_child)
{
child->copy(dest_child);
}
else
{
Logs::warning() << "Can't copy to child " << child->name << " of " << destination->getTypeName() << std::endl;
}
}
}
else
{

View file

@ -18,26 +18,12 @@ static const int DATA_VERSION = 1;
Scenery::Scenery():
DefinitionNode(NULL, "scenery", "scenery")
{
addChild(atmosphere = new AtmosphereDefinition(this));
addChild(camera = new CameraDefinition);
addChild(clouds = new CloudsDefinition(this));
addChild(terrain = new TerrainDefinition(this));
addChild(textures = new TexturesDefinition(this));
addChild(water = new WaterDefinition(this));
}
void Scenery::copy(DefinitionNode *destination_) const
{
Scenery* destination = (Scenery*)destination_;
atmosphere->copy(destination->atmosphere);
camera->copy(destination->camera);
clouds->copy(destination->clouds);
terrain->copy(destination->terrain);
textures->copy(destination->textures);
water->copy(destination->water);
destination->validate();
atmosphere = new AtmosphereDefinition(this);
camera = new CameraDefinition(this);
clouds = new CloudsDefinition(this);
terrain = new TerrainDefinition(this);
textures = new TexturesDefinition(this);
water = new WaterDefinition(this);
}
void Scenery::validate()

View file

@ -29,7 +29,6 @@ public:
Scenery();
virtual void validate() override;
virtual void copy(DefinitionNode *destination) const override;
FileOperationResult saveGlobal(const std::string &filepath) const;
FileOperationResult loadGlobal(const std::string &filepath);

View file

@ -52,8 +52,6 @@ void TerrainDefinition::copy(DefinitionNode* _destination) const
height_map->copy(destination->height_map);
water_height->copy(destination->water_height);
_height_noise->copy(destination->_height_noise);
destination->validate();

View file

@ -65,7 +65,7 @@ TEST(DefinitionNode, attachDetach)
delete root;
}
TEST(DefinitionNode, saveLoad)
TEST(DefinitionNode, saveLoadCopy)
{
PackStream *stream;
int check_in = 42, check_out = 0;
@ -105,6 +105,18 @@ TEST(DefinitionNode, saveLoad)
EXPECT_DOUBLE_EQ(0.0, after4->getValue());
EXPECT_EQ(42, check_out);
// Test copy, that should work like "save then load"
after12->setValue(1.1);
after13->setValue(1.1);
after3->setValue(1.1);
after4->setValue(1.1);
before->copy(after);
EXPECT_DOUBLE_EQ(-4.3, after12->getValue());
EXPECT_DOUBLE_EQ(1.1, after13->getValue());
EXPECT_DOUBLE_EQ(6.7, after3->getValue());
EXPECT_DOUBLE_EQ(1.1, after4->getValue());
delete before;
delete after;
}