Browse Source

Move XML settings save and load to `SettingsLoader` singleton

master
Krisjanis Rijnieks 10 years ago
parent
commit
6bb151b369
  1. 12
      example/example.xcodeproj/project.pbxproj
  2. 17
      src/Application/Settings.cpp
  3. 17
      src/Application/Settings.h
  4. 240
      src/Application/SettingsLoader.cpp
  5. 25
      src/Application/SettingsLoader.h
  6. 216
      src/Surfaces/SurfaceManager.cpp
  7. 1
      src/Surfaces/SurfaceManager.h

12
example/example.xcodeproj/project.pbxproj

@ -83,7 +83,7 @@
39C1248819F1EB75005DF557 /* SurfaceManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39C1248319F1EB75005DF557 /* SurfaceManager.cpp */; };
39C1248919F1EB75005DF557 /* SurfaceManagerGui.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39C1248519F1EB75005DF557 /* SurfaceManagerGui.cpp */; };
39C787BD1AC2111B00691393 /* CmdManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39C787BC1AC2111B00691393 /* CmdManager.cpp */; };
39FA2B801C457606003FAB28 /* Settings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39FA2B7E1C457606003FAB28 /* Settings.cpp */; };
39FA2B801C457606003FAB28 /* SettingsLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 39FA2B7E1C457606003FAB28 /* SettingsLoader.cpp */; };
E4328149138ABC9F0047C5CB /* openFrameworksDebug.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E4328148138ABC890047C5CB /* openFrameworksDebug.a */; };
E45BE97B0E8CC7DD009D7055 /* AGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E45BE9710E8CC7DD009D7055 /* AGL.framework */; };
E45BE97C0E8CC7DD009D7055 /* ApplicationServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E45BE9720E8CC7DD009D7055 /* ApplicationServices.framework */; };
@ -305,8 +305,8 @@
39C1248719F1EB75005DF557 /* SurfaceType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SurfaceType.h; sourceTree = "<group>"; };
39C787BB1AC20D2400691393 /* CmdManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CmdManager.h; path = Commands/CmdManager.h; sourceTree = "<group>"; };
39C787BC1AC2111B00691393 /* CmdManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CmdManager.cpp; path = Commands/CmdManager.cpp; sourceTree = "<group>"; };
39FA2B7E1C457606003FAB28 /* Settings.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Settings.cpp; sourceTree = "<group>"; };
39FA2B7F1C457606003FAB28 /* Settings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Settings.h; sourceTree = "<group>"; };
39FA2B7E1C457606003FAB28 /* SettingsLoader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SettingsLoader.cpp; sourceTree = "<group>"; };
39FA2B7F1C457606003FAB28 /* SettingsLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SettingsLoader.h; sourceTree = "<group>"; };
39FDD9EA1AC007BF00262205 /* BaseCmd.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BaseCmd.h; path = Commands/BaseCmd.h; sourceTree = "<group>"; };
BBAB23BE13894E4700AA2426 /* GLUT.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GLUT.framework; path = ../../../libs/glut/lib/osx/GLUT.framework; sourceTree = "<group>"; };
E4328143138ABC890047C5CB /* openFrameworksLib.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = openFrameworksLib.xcodeproj; path = ../../../libs/openFrameworksCompiled/project/osx/openFrameworksLib.xcodeproj; sourceTree = SOURCE_ROOT; };
@ -428,8 +428,8 @@
children = (
3968416A1BC91F6F009F0BAE /* Application.h */,
396841691BC91F6F009F0BAE /* Application.cpp */,
39FA2B7F1C457606003FAB28 /* Settings.h */,
39FA2B7E1C457606003FAB28 /* Settings.cpp */,
39FA2B7F1C457606003FAB28 /* SettingsLoader.h */,
39FA2B7E1C457606003FAB28 /* SettingsLoader.cpp */,
3968416C1BC91F6F009F0BAE /* ApplicationBaseState.h */,
3968416B1BC91F6F009F0BAE /* ApplicationBaseState.cpp */,
3968416E1BC91F6F009F0BAE /* PresentationState.h */,
@ -969,7 +969,7 @@
39C1248819F1EB75005DF557 /* SurfaceManager.cpp in Sources */,
396841771BC91F6F009F0BAE /* PresentationState.cpp in Sources */,
39A9AAF21B054FC300AA83BC /* MvSurfaceVertCmd.cpp in Sources */,
39FA2B801C457606003FAB28 /* Settings.cpp in Sources */,
39FA2B801C457606003FAB28 /* SettingsLoader.cpp in Sources */,
39C1244319EE9589005DF557 /* LinkFilter.cpp in Sources */,
39C1245919F086A9005DF557 /* BaseSource.cpp in Sources */,
39C1244019EE9589005DF557 /* FileExtensionFilter.cpp in Sources */,

17
src/Application/Settings.cpp

@ -1,17 +0,0 @@
#include "Settings.h"
namespace ofx {
namespace piMapper {
Settings::Settings(){}
void Settings::load(){
}
void Settings::save(){
}
} // namespace piMapper
} // namespace ofx

17
src/Application/Settings.h

@ -1,17 +0,0 @@
#pragma once
#include "ofMain.h"
namespace ofx {
namespace piMapper {
class Settings {
public:
Settings();
void load();
void save();
};
}
}

240
src/Application/SettingsLoader.cpp

@ -0,0 +1,240 @@
#include "SettingsLoader.h"
namespace ofx {
namespace piMapper {
SettingsLoader * SettingsLoader::_instance = 0;
SettingsLoader * SettingsLoader::instance(){
if(_instance == 0){
_instance = new ofx::piMapper::SettingsLoader();
}
return _instance;
}
bool SettingsLoader::load(SurfaceStack & surfaces, MediaServer & mediaServer, string fileName){
ofxXmlSettings * xmlSettings = new ofxXmlSettings();
if(!xmlSettings->loadFile(fileName)){
ofLogWarning("SurfaceManager") << "Could not load XML settings";
return false;
}
if(!xmlSettings->tagExists("surfaces")){
ofLogWarning("SurfaceManager") << "XML settings is empty or has wrong markup";
return false;
}
xmlSettings->pushTag("surfaces");
int numSurfaces = xmlSettings->getNumTags("surface");
for(int i = 0; i < numSurfaces; i++){
xmlSettings->pushTag("surface", i);
// attempt to load surface source
xmlSettings->pushTag("source");
string sourceType = xmlSettings->getValue("source-type", "");
string sourceName = xmlSettings->getValue("source-name", "");
BaseSource * source = 0;
if(sourceName != "" && sourceName != "none" && sourceType != ""){
// Load source depending on type
int typeEnum = SourceType::GetSourceTypeEnum(sourceType);
if(typeEnum == SourceType::SOURCE_TYPE_FBO){
// Load FBO source using sourceName
source = mediaServer.loadMedia(sourceName, typeEnum);
}else{
// Construct full path
string dir = mediaServer.getDefaultMediaDir(typeEnum);
stringstream pathss;
pathss << ofToDataPath(dir, true) << sourceName;
string sourcePath = pathss.str();
// Load media by using full path
source = mediaServer.loadMedia(sourcePath, typeEnum);
}
}
xmlSettings->popTag(); // source
xmlSettings->pushTag("vertices");
vector <ofVec2f> vertices;
int vertexCount = xmlSettings->getNumTags("vertex");
if(vertexCount == 3){
xmlSettings->pushTag("vertex", 0);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
xmlSettings->pushTag("vertex", 1);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
xmlSettings->pushTag("vertex", 2);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 100.0f)));
xmlSettings->popTag();
xmlSettings->popTag(); // vertices
xmlSettings->pushTag("texCoords");
vector <ofVec2f> texCoords;
xmlSettings->pushTag("texCoord", 0);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
xmlSettings->pushTag("texCoord", 1);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
xmlSettings->pushTag("texCoord", 2);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 1.0f)));
xmlSettings->popTag();
xmlSettings->popTag(); // texCoords
// Create and add a triangle surface
BaseSurface * triangleSurface =
SurfaceFactory::instance()->createSurface(
SurfaceType::TRIANGLE_SURFACE);
triangleSurface->setVertices(vertices);
triangleSurface->setTexCoords(texCoords);
if(sourceName != "none" && source != 0){
triangleSurface->setSource(source);
}
surfaces.push_back(triangleSurface);
}else if(vertexCount == 4){
xmlSettings->pushTag("vertex", 0);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
xmlSettings->pushTag("vertex", 1);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
xmlSettings->pushTag("vertex", 2);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f),
xmlSettings->getValue("y", 100.0f)));
xmlSettings->popTag();
xmlSettings->pushTag("vertex", 3);
vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 100.0f)));
xmlSettings->popTag();
xmlSettings->popTag(); // vertices
xmlSettings->pushTag("texCoords");
vector <ofVec2f> texCoords;
xmlSettings->pushTag("texCoord", 0);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
xmlSettings->pushTag("texCoord", 1);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f),
xmlSettings->getValue("y", 0.0f)));
xmlSettings->popTag();
xmlSettings->pushTag("texCoord", 2);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f),
xmlSettings->getValue("y", 1.0f)));
xmlSettings->popTag();
xmlSettings->pushTag("texCoord", 3);
texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f),
xmlSettings->getValue("y", 1.0f)));
xmlSettings->popTag();
xmlSettings->popTag(); // texCoords
// Create and add quad surface
BaseSurface * quadSurface =
SurfaceFactory::instance()->createSurface(
SurfaceType::QUAD_SURFACE);
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);
}
} // namespace piMapper
} // namespace ofx

25
src/Application/SettingsLoader.h

@ -0,0 +1,25 @@
#pragma once
#include "ofMain.h"
#include "ofxXmlSettings.h"
#include "SurfaceStack.h"
#include "MediaServer.h"
#include "SurfaceFactory.h"
#include "SurfaceType.h"
namespace ofx {
namespace piMapper {
class SettingsLoader {
public:
static SettingsLoader * instance();
bool load(SurfaceStack & surfaces, MediaServer & mediaServer, string fileName);
bool save(SurfaceStack & surfaces, string fileName);
private:
static SettingsLoader * _instance;
};
}
}

216
src/Surfaces/SurfaceManager.cpp

@ -55,227 +55,17 @@ void SurfaceManager::saveXmlSettings(string fileName){
ofLogFatalError("SurfaceManager") << "Media server not set";
exit(EXIT_FAILURE);
}
xmlSettings.clear();
// 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());
cout << "sourceTypeName: " << sourceTypeName << endl;
xmlSettings.addValue("source-type", sourceTypeName);
xmlSettings.addValue("source-name", surface->getSource()->getName());
xmlSettings.popTag(); // source
xmlSettings.popTag(); // surface
}
xmlSettings.popTag(); // surfaces
xmlSettings.save(fileName);
SettingsLoader::instance()->save(_surfaces, fileName);
}
bool SurfaceManager::loadXmlSettings(string fileName){
// Exit if there is no media server
if(mediaServer == 0){
ofLogFatalError("SurfaceManager") << "Media server not set";
exit(EXIT_FAILURE);
}
if(!xmlSettings.loadFile(fileName)){
ofLogWarning("SurfaceManager") << "Could not load XML settings";
return false;
}
if(!xmlSettings.tagExists("surfaces")){
ofLogWarning("SurfaceManager") << "XML settings is empty or has wrong markup";
return false;
}
xmlSettings.pushTag("surfaces");
int numSurfaces = xmlSettings.getNumTags("surface");
for(int i = 0; i < numSurfaces; i++){
xmlSettings.pushTag("surface", i);
// attempt to load surface source
xmlSettings.pushTag("source");
string sourceType = xmlSettings.getValue("source-type", "");
string sourceName = xmlSettings.getValue("source-name", "");
BaseSource * source = 0;
if(sourceName != "" && sourceName != "none" && sourceType != ""){
// Load source depending on type
int typeEnum = SourceType::GetSourceTypeEnum(sourceType);
if(typeEnum == SourceType::SOURCE_TYPE_FBO){
// Load FBO source using sourceName
source = mediaServer->loadMedia(sourceName, typeEnum);
}else{
// Construct full path
string dir = mediaServer->getDefaultMediaDir(typeEnum);
stringstream pathss;
pathss << ofToDataPath(dir, true) << sourceName;
string sourcePath = pathss.str();
// Load media by using full path
source = mediaServer->loadMedia(sourcePath, typeEnum);
}
}
xmlSettings.popTag(); // source
xmlSettings.pushTag("vertices");
vector <ofVec2f> vertices;
int vertexCount = xmlSettings.getNumTags("vertex");
if(vertexCount == 3){
xmlSettings.pushTag("vertex", 0);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 1);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 100.0f)));
xmlSettings.popTag();
xmlSettings.popTag(); // vertices
xmlSettings.pushTag("texCoords");
vector <ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 1);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 1.0f)));
xmlSettings.popTag();
xmlSettings.popTag(); // texCoords
// Create and add a triangle surface
BaseSurface * triangleSurface =
SurfaceFactory::instance()->createSurface(
SurfaceType::TRIANGLE_SURFACE);
triangleSurface->setVertices(vertices);
triangleSurface->setTexCoords(texCoords);
if(sourceName != "none" && source != 0){
triangleSurface->setSource(source);
}
addSurface(triangleSurface);
}else if(vertexCount == 4){
xmlSettings.pushTag("vertex", 0);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 1);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
xmlSettings.getValue("y", 100.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 3);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 100.0f)));
xmlSettings.popTag();
xmlSettings.popTag(); // vertices
xmlSettings.pushTag("texCoords");
vector <ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 1);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
xmlSettings.getValue("y", 1.0f)));
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 3);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 1.0f)));
xmlSettings.popTag();
xmlSettings.popTag(); // texCoords
// Create and add quad surface
BaseSurface * quadSurface =
SurfaceFactory::instance()->createSurface(
SurfaceType::QUAD_SURFACE);
quadSurface->setVertices(vertices);
quadSurface->setTexCoords(texCoords);
if(sourceName != "none" && source != 0){
quadSurface->setSource(source);
}
addSurface(quadSurface);
}
xmlSettings.popTag(); // surface
}
xmlSettings.popTag(); // surfaces
return true;
return SettingsLoader::instance()->load(_surfaces, *mediaServer, fileName);
}
void SurfaceManager::setMediaServer(MediaServer * newMediaServer){

1
src/Surfaces/SurfaceManager.h

@ -12,6 +12,7 @@
#include "ofEvents.h"
#include "ofxXmlSettings.h"
#include "SettingsLoader.h"
using namespace std;

Loading…
Cancel
Save