Browse Source

Improve `SettingsLoader`

Simplify the `load()` method by moving the quad and triangle surface creation to private methods
master
Krisjanis Rijnieks 10 years ago
parent
commit
920b3d2a0b
  1. 155
      src/Application/SettingsLoader.cpp
  2. 3
      src/Application/SettingsLoader.h

155
src/Application/SettingsLoader.cpp

@ -54,12 +54,90 @@ bool SettingsLoader::load(SurfaceStack & surfaces, MediaServer & mediaServer, st
source = mediaServer.loadMedia(sourcePath, typeEnum);
}
}
xmlSettings->popTag(); // source
xmlSettings->pushTag("vertices");
vector <ofVec2f> vertices;
int vertexCount = xmlSettings->getNumTags("vertex");
if(vertexCount == 3){
BaseSurface * triangleSurface = getTriangleSurface(xmlSettings);
if(sourceName != "none" && source != 0){
triangleSurface->setSource(source);
}
surfaces.push_back(triangleSurface);
}else if(vertexCount == 4){
BaseSurface * quadSurface = getQuadSurface(xmlSettings);
if(sourceName != "none" && source != 0){
quadSurface->setSource(source);
}
surfaces.push_back(quadSurface);
}
xmlSettings->popTag(); // surface
}
xmlSettings->popTag(); // surfaces
return true;
}
bool SettingsLoader::save(SurfaceStack & surfaces, string fileName){
ofxXmlSettings * xmlSettings = new ofxXmlSettings();
// Save surfaces
xmlSettings->addTag("surfaces");
xmlSettings->pushTag("surfaces");
for(int i = 0; i < surfaces.size(); i++){
xmlSettings->addTag("surface");
xmlSettings->pushTag("surface", i);
BaseSurface * surface = surfaces[i];
xmlSettings->addTag("vertices");
xmlSettings->pushTag("vertices");
vector <ofVec3f> * vertices = &surface->getVertices();
for(int j = 0; j < vertices->size(); j++){
xmlSettings->addTag("vertex");
xmlSettings->pushTag("vertex", j);
ofVec3f * vertex = &(*vertices)[j];
xmlSettings->addValue("x", vertex->x);
xmlSettings->addValue("y", vertex->y);
// we don't need z as it will be 0 anyways
xmlSettings->popTag(); // vertex
}
xmlSettings->popTag(); // vertices
xmlSettings->addTag("texCoords");
xmlSettings->pushTag("texCoords");
vector <ofVec2f> * texCoords = &surface->getTexCoords();
for(int j = 0; j < texCoords->size(); j++){
xmlSettings->addTag("texCoord");
xmlSettings->pushTag("texCoord", j);
ofVec2f * texCoord = &(*texCoords)[j];
xmlSettings->addValue("x", texCoord->x);
xmlSettings->addValue("y", texCoord->y);
xmlSettings->popTag(); // texCoord
}
xmlSettings->popTag(); // texCoords
xmlSettings->addTag("source");
xmlSettings->pushTag("source");
string sourceTypeName = SourceType::GetSourceTypeName(surface->getSource()->getType());
xmlSettings->addValue("source-type", sourceTypeName);
xmlSettings->addValue("source-name", surface->getSource()->getName());
xmlSettings->popTag(); // source
xmlSettings->popTag(); // surface
}
xmlSettings->popTag(); // surfaces
xmlSettings->save(fileName);
}
BaseSurface * SettingsLoader::getTriangleSurface(ofxXmlSettings * xmlSettings){
vector <ofVec2f> vertices;
xmlSettings->pushTag("vertex", 0);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 0.0f)));
@ -105,13 +183,12 @@ bool SettingsLoader::load(SurfaceStack & surfaces, MediaServer & mediaServer, st
triangleSurface->setVertices(vertices);
triangleSurface->setTexCoords(texCoords);
if(sourceName != "none" && source != 0){
triangleSurface->setSource(source);
return triangleSurface;
}
surfaces.push_back(triangleSurface);
BaseSurface * SettingsLoader::getQuadSurface(ofxXmlSettings * xmlSettings){
vector <ofVec2f> vertices;
}else if(vertexCount == 4){
xmlSettings->pushTag("vertex", 0);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 0.0f)));
@ -167,73 +244,7 @@ bool SettingsLoader::load(SurfaceStack & surfaces, MediaServer & mediaServer, st
quadSurface->setVertices(vertices);
quadSurface->setTexCoords(texCoords);
if(sourceName != "none" && source != 0){
quadSurface->setSource(source);
}
surfaces.push_back(quadSurface);
}
xmlSettings->popTag(); // surface
}
xmlSettings->popTag(); // surfaces
return true;
}
bool SettingsLoader::save(SurfaceStack & surfaces, string fileName){
ofxXmlSettings * xmlSettings = new ofxXmlSettings();
// Save surfaces
xmlSettings->addTag("surfaces");
xmlSettings->pushTag("surfaces");
for(int i = 0; i < surfaces.size(); i++){
xmlSettings->addTag("surface");
xmlSettings->pushTag("surface", i);
BaseSurface * surface = surfaces[i];
xmlSettings->addTag("vertices");
xmlSettings->pushTag("vertices");
vector <ofVec3f> * vertices = &surface->getVertices();
for(int j = 0; j < vertices->size(); j++){
xmlSettings->addTag("vertex");
xmlSettings->pushTag("vertex", j);
ofVec3f * vertex = &(*vertices)[j];
xmlSettings->addValue("x", vertex->x);
xmlSettings->addValue("y", vertex->y);
// we don't need z as it will be 0 anyways
xmlSettings->popTag(); // vertex
}
xmlSettings->popTag(); // vertices
xmlSettings->addTag("texCoords");
xmlSettings->pushTag("texCoords");
vector <ofVec2f> * texCoords = &surface->getTexCoords();
for(int j = 0; j < texCoords->size(); j++){
xmlSettings->addTag("texCoord");
xmlSettings->pushTag("texCoord", j);
ofVec2f * texCoord = &(*texCoords)[j];
xmlSettings->addValue("x", texCoord->x);
xmlSettings->addValue("y", texCoord->y);
xmlSettings->popTag(); // texCoord
}
xmlSettings->popTag(); // texCoords
xmlSettings->addTag("source");
xmlSettings->pushTag("source");
string sourceTypeName = SourceType::GetSourceTypeName(surface->getSource()->getType());
xmlSettings->addValue("source-type", sourceTypeName);
xmlSettings->addValue("source-name", surface->getSource()->getName());
xmlSettings->popTag(); // source
xmlSettings->popTag(); // surface
}
xmlSettings->popTag(); // surfaces
xmlSettings->save(fileName);
return quadSurface;
}
} // namespace piMapper

3
src/Application/SettingsLoader.h

@ -19,6 +19,9 @@ class SettingsLoader {
private:
static SettingsLoader * _instance;
BaseSurface * getTriangleSurface(ofxXmlSettings * xmlSettings);
BaseSurface * getQuadSurface(ofxXmlSettings * xmlSettings);
};
}

Loading…
Cancel
Save