Browse Source

Create presets through SettingsLoader

master
Krisjanis Rijnieks 9 years ago
parent
commit
7febe46958
  1. 1
      src/Application/Application.cpp
  2. 19
      src/Application/SettingsLoader.cpp
  3. 5
      src/Application/SettingsLoader.h
  4. 229
      src/Surfaces/SurfaceManager.cpp
  5. 7
      src/Surfaces/SurfaceManager.h

1
src/Application/Application.cpp

@ -10,6 +10,7 @@ Application::Application(){
_surfaceManager.setMediaServer(&_mediaServer);
setState(PresentationMode::instance());
ofAddListener(ofEvents().keyPressed, this, &Application::onKeyPressed);
ofAddListener(ofEvents().keyReleased, this, &Application::onKeyReleased);
ofAddListener(ofEvents().mousePressed, this, &Application::onMousePressed);

19
src/Application/SettingsLoader.cpp

@ -16,7 +16,10 @@ SettingsLoader::SettingsLoader(){
_lastLoadedFilename = "surfaces.xml";
}
bool SettingsLoader::load(SurfaceStack & surfaces, MediaServer & mediaServer, string fileName){
bool SettingsLoader::load(
SurfaceManager & surfaceManager,
MediaServer & mediaServer,
string fileName){
ofxXmlSettings * xmlSettings = new ofxXmlSettings();
string sourceType = "";
@ -42,6 +45,13 @@ bool SettingsLoader::load(SurfaceStack & surfaces, MediaServer & mediaServer, st
// Count <surfaces> tags.
unsigned int numPresets = xmlSettings->getNumTags("surfaces");
cout << "numPresets: " << numPresets << endl;
// Clear previous presets and surfaces first.
// TODO...
// Add new presets.
// surfaceManager.createPreset...
SurfaceStack * surfaces = surfaceManager.createPreset();
xmlSettings->pushTag("surfaces");
@ -109,19 +119,19 @@ bool SettingsLoader::load(SurfaceStack & surfaces, MediaServer & mediaServer, st
if(sourceName != "none" && source != 0){
triangleSurface->setSource(source);
}
surfaces.push_back(triangleSurface);
surfaces->push_back(triangleSurface);
}else if(type == SurfaceType::QUAD_SURFACE){
BaseSurface * quadSurface = getQuadSurface(xmlSettings);
if(sourceName != "none" && source != 0){
quadSurface->setSource(source);
}
surfaces.push_back(quadSurface);
surfaces->push_back(quadSurface);
}else if(type == SurfaceType::GRID_WARP_SURFACE){
BaseSurface * gridWarpSurface = getGridWarpSurface(xmlSettings);
if(sourceName != "none" && source != 0){
gridWarpSurface->setSource(source);
}
surfaces.push_back(gridWarpSurface);
surfaces->push_back(gridWarpSurface);
}
xmlSettings->popTag(); // surface
@ -136,6 +146,7 @@ bool SettingsLoader::load(SurfaceStack & surfaces, MediaServer & mediaServer, st
return true;
}
// TODO: Save all presets, not just the active one.
bool SettingsLoader::save(SurfaceStack & surfaces, string fileName){
ofxXmlSettings * xmlSettings = new ofxXmlSettings();

5
src/Application/SettingsLoader.h

@ -3,6 +3,7 @@
#include "ofMain.h"
#include "ofxXmlSettings.h"
#include "SurfaceStack.h"
#include "SurfaceManager.h"
#include "MediaServer.h"
#include "SurfaceFactory.h"
#include "SurfaceType.h"
@ -10,11 +11,13 @@
namespace ofx {
namespace piMapper {
class SurfaceManager;
class SettingsLoader {
public:
static SettingsLoader * instance();
bool load(SurfaceStack & surfaces, MediaServer & mediaServer, string fileName);
bool load(SurfaceManager & surfaceManager, MediaServer & mediaServer, string fileName);
bool save(SurfaceStack & surfaces, string fileName);
string getLastLoadedFilename(){ return _lastLoadedFilename; };

229
src/Surfaces/SurfaceManager.cpp

@ -7,39 +7,46 @@ SurfaceManager::SurfaceManager(){
mediaServer = 0;
selectedSurface = 0;
// Create one SurfaceStack instance in the beginning as interphase towards full
// preset functionality.
//SurfaceStack * preset = new SurfaceStack();
//_presets.push_back(preset);
//_activePresetIndex = 0;
SurfaceStack * preset = new SurfaceStack();
_presets.push_back(preset);
ofAddListener(
_presets[0]->vertexChangedEvent,
this,
&SurfaceManager::onVertexChanged);
ofAddListener(
_presets[0]->verticesChangedEvent,
this,
&SurfaceManager::onVerticesChanged);
_activePresetIndex = -1;
_selectedVertexIndex = -1;
}
void SurfaceManager::draw(){
_presets[0]->draw();
if(_activePresetIndex < 0){
return;
}
_presets[_activePresetIndex]->draw();
}
void SurfaceManager::addSurface(BaseSurface * surface){
_presets[0]->push_back(surface);
if(_activePresetIndex < 0){
ofLogWarning("SurfaceManager::addSurface", "Can not add surface. No active preset.");
return;
}
_presets[_activePresetIndex]->push_back(surface);
}
void SurfaceManager::removeSelectedSurface(){
if(_activePresetIndex < 0){
ofLogWarning(
"SurfaceManager::removeSelectedSurface",
"Can not remove surface. No Active preset");
return;
}
if(selectedSurface == 0){
return;
}
for(int i = 0; i < _presets[0]->size(); i++){
if(_presets[0]->at(i) == selectedSurface){
_presets[0]->erase(i);
for(int i = 0; i < _presets[_activePresetIndex]->size(); i++){
if(_presets[_activePresetIndex]->at(i) == selectedSurface){
_presets[_activePresetIndex]->erase(i);
selectedSurface = 0;
_selectedVertexIndex = -1;
break;
@ -48,46 +55,80 @@ void SurfaceManager::removeSelectedSurface(){
}
void SurfaceManager::removeSurface(){
if(_presets[0]->size() <= 0){
if(_activePresetIndex < 0){
ofLogWarning("SurfaceManager::removeSurface", "Can not remove surface. No active preset.");
return;
}
if(_presets[_activePresetIndex]->size() <= 0){
return;
}
BaseSurface * s = _presets[0]->back();
_presets[0]->pop_back();
BaseSurface * s = _presets[_activePresetIndex]->back();
_presets[_activePresetIndex]->pop_back();
delete s;
}
void SurfaceManager::deleteSurface(ofx::piMapper::BaseSurface * surface){
for(int i = 0; i < _presets[0]->size(); ++i){
if(_presets[0]->at(i) == surface){
_presets[0]->erase(i);
if(_activePresetIndex < 0){
ofLogWarning("SurfaceManager::deleteSurface", "Can not delete surface. No active preset.");
return;
}
for(int i = 0; i < _presets[_activePresetIndex]->size(); ++i){
if(_presets[_activePresetIndex]->at(i) == surface){
_presets[_activePresetIndex]->erase(i);
break;
}
}
}
/*
* Question: Should it clear the active preset or all presets?
* Maybe creating additional methods like clearPresets() and clearActivePreset() would help.
*/
void SurfaceManager::clear(){
while(_presets[0]->size()){
delete _presets[0]->back();
_presets[0]->pop_back();
if(_activePresetIndex < 0){
ofLogWarning("SurfaceManager::clear", "Can not clear. No active preset.");
return;
}
while(_presets[_activePresetIndex]->size()){
delete _presets[_activePresetIndex]->back();
_presets[_activePresetIndex]->pop_back();
}
}
// TODO: Do serious adjustment here. We need to save all presets. Not just the active one.
void SurfaceManager::saveXmlSettings(string fileName){
if(_presets.size() <= 0){
ofLogWarning(
"SurfaceManager::saveXmlSettings",
"Can not save XML settings. No presets.");
return;
}
if(mediaServer == 0){
ofLogFatalError("SurfaceManager") << "Media server not set";
exit(EXIT_FAILURE);
}
SettingsLoader::instance()->save(*_presets[0], fileName);
SettingsLoader::instance()->save(*_presets[_activePresetIndex], fileName);
}
// TODO: We need to load all presets. Not just the active one.
bool SurfaceManager::loadXmlSettings(string fileName){
// TODO: clear old presets beforehand?
if(mediaServer == 0){
ofLogFatalError("SurfaceManager") << "Media server not set";
exit(EXIT_FAILURE);
}
return SettingsLoader::instance()->load(*_presets[0], *mediaServer, fileName);
bool success = SettingsLoader::instance()->load(*this, *mediaServer, fileName);
return success;
//return SettingsLoader::instance()->load(*_presets[_activePresetIndex], *mediaServer, fileName);
}
void SurfaceManager::setMediaServer(MediaServer * newMediaServer){
@ -95,21 +136,28 @@ void SurfaceManager::setMediaServer(MediaServer * newMediaServer){
}
BaseSurface * SurfaceManager::selectSurface(int index){
if(index >= _presets[0]->size()){
if(_activePresetIndex < 0){
ofLogWarning("SurfaceManager::selectSurface", "Can not select surface. No active preset.");
return;
}
if(index >= _presets[_activePresetIndex]->size()){
throw runtime_error("Surface index out of bounds.");
}
selectedSurface = _presets[0]->at(index);
selectedSurface = _presets[_activePresetIndex]->at(index);
_selectedVertexIndex = -1;
ofSendMessage("surfaceSelected");
return selectedSurface;
}
BaseSurface * SurfaceManager::selectSurface(BaseSurface * surface){
cout << "SurfaceManager::selectSurface()" << endl;
for(int i = 0; i < _presets[0]->size(); i++){
if(_presets[0]->at(i) == surface){
if(_activePresetIndex < 0){
ofLogWarning("SurfaceManager::selectSurface", "Can not select surface. No active preset.");
}
for(int i = 0; i < _presets[_activePresetIndex]->size(); i++){
if(_presets[_activePresetIndex]->at(i) == surface){
selectedSurface = surface;
_selectedVertexIndex = -1;
ofSendMessage("surfaceSelected");
@ -123,6 +171,13 @@ BaseSurface * SurfaceManager::selectSurface(BaseSurface * surface){
}
BaseSurface * SurfaceManager::selectNextSurface(){
if(_activePresetIndex < 0){
ofLogWarning(
"SurfaceManager::selectNextSurface",
"Can not select next surface. No active preset.");
return;
}
int next;
_selectedVertexIndex = -1;
@ -133,15 +188,15 @@ BaseSurface * SurfaceManager::selectNextSurface(){
return selectedSurface;
}
for(int i = 0; i < _presets[0]->size(); ++i){
if(_presets[0]->at(i) == selectedSurface){
if(i < _presets[0]->size() - 1){
for(int i = 0; i < _presets[_activePresetIndex]->size(); ++i){
if(_presets[_activePresetIndex]->at(i) == selectedSurface){
if(i < _presets[_activePresetIndex]->size() - 1){
next = i + 1;
}else{
next = 0;
}
selectedSurface = _presets[0]->at(next);
selectedSurface = _presets[_activePresetIndex]->at(next);
ofNotifyEvent(surfaceSelectedEvent, next, this);
return selectedSurface;
}
@ -151,25 +206,32 @@ BaseSurface * SurfaceManager::selectNextSurface(){
}
BaseSurface * SurfaceManager::selectPrevSurface(){
if(_activePresetIndex < 0){
ofLogWarning(
"SurfaceManager::selectPrevSurface",
"Can not select prev surface. No active preset.");
return;
}
int prev;
_selectedVertexIndex = -1;
if(selectedSurface == 0){
prev = _presets[0]->size() - 1;
prev = _presets[_activePresetIndex]->size() - 1;
selectedSurface = selectSurface(prev);
ofNotifyEvent(surfaceSelectedEvent, prev, this);
return selectedSurface;
}
for(int i = 0; i < _presets[0]->size(); ++i){
if(_presets[0]->at(i) == selectedSurface){
for(int i = 0; i < _presets[_activePresetIndex]->size(); ++i){
if(_presets[_activePresetIndex]->at(i) == selectedSurface){
if(i > 0){
prev = i - 1;
}else{
prev = _presets[0]->size() - 1;
prev = _presets[_activePresetIndex]->size() - 1;
}
selectedSurface = _presets[0]->at(prev);
selectedSurface = _presets[_activePresetIndex]->at(prev);
ofNotifyEvent(surfaceSelectedEvent, prev, this);
return selectedSurface;
}
@ -254,8 +316,15 @@ void SurfaceManager::moveSelectionBy(ofVec2f v){
}
void SurfaceManager::moveAllSurfacesBy(ofVec2f v){
for(int i = 0; i < _presets[0]->size(); ++i){
_presets[0]->at(i)->moveBy(v);
if(_activePresetIndex < 0){
ofLogWarning(
"SurfaceManager::moveAllSurfacesBy",
"Can not move surfaces. No active preset.");
return;
}
for(int i = 0; i < _presets[_activePresetIndex]->size(); ++i){
_presets[_activePresetIndex]->at(i)->moveBy(v);
}
}
@ -265,22 +334,39 @@ void SurfaceManager::deselectSurface(){
}
BaseSurface * SurfaceManager::getSurface(int index){
if(index >= _presets[0]->size()){
if(_activePresetIndex < 0){
throw runtime_error("No active preset.");
return 0;
}
if(index >= _presets[_activePresetIndex]->size()){
throw runtime_error("Surface index out of bounds.");
return 0;
}
return _presets[0]->at(index);
return _presets[_activePresetIndex]->at(index);
}
/* TODO: Solve fundamental question
* What size are we talking about here?
* Is it the number of presets or surfaces?
*/
int SurfaceManager::size(){
return _presets[0]->size();
if(_activePresetIndex < 0){
return 0;
}
return _presets[_activePresetIndex]->size();
}
int SurfaceManager::getSelectedVertexIndex(){
return _selectedVertexIndex;
}
int SurfaceManager::getActivePresetIndex(){
return 0;
}
void SurfaceManager::onVertexChanged(int & i){
ofNotifyEvent(vertexChangedEvent, i, this);
}
@ -290,7 +376,50 @@ void SurfaceManager::onVerticesChanged(vector<ofVec3f> & vertices){
}
SurfaceStack * SurfaceManager::getActivePreset(){
return _presets[0];
if(_activePresetIndex < 0){
throw runtime_error(
"SurfaceManager::getActivePreset: Can not getActivePreset. No active preset.");
}
return _presets[_activePresetIndex];
}
SurfaceStack * SurfaceManager::createPreset(){
SurfaceStack * preset = new SurfaceStack();
_presets.push_back(preset);
// If we did not have any presets before, set the new as active one.
if(_presets.size() == 1){
_activePresetIndex = 0;
}
// Remember to remove these listeners when adding a removePreset method.
ofAddListener(
preset->vertexChangedEvent,
this,
&SurfaceManager::onVertexChanged);
ofAddListener(
preset->verticesChangedEvent,
this,
&SurfaceManager::onVerticesChanged);
return preset;
// TODO: Create command for this. And what not.
}
void SurfaceManager::setNextPreset(){
if(_presets.size() <= 1){
return;
}
if(_activePresetIndex == _presets.size() - 1){
_activePresetIndex = 0;
}else{
_activePresetIndex += 1;
}
// TODO: Create command for this.
}
} // namespace piMapper

7
src/Surfaces/SurfaceManager.h

@ -60,6 +60,7 @@ class SurfaceManager {
int size();
int getSelectedVertexIndex();
int getActivePresetIndex();
ofEvent <int> vertexChangedEvent;
ofEvent <vector<ofVec3f>> verticesChangedEvent;
@ -71,6 +72,9 @@ class SurfaceManager {
void onVerticesChanged(vector<ofVec3f> & vertices);
SurfaceStack * getActivePreset();
SurfaceStack * createPreset();
void setNextPreset();
private:
BaseSurface * selectedSurface;
@ -78,8 +82,11 @@ class SurfaceManager {
MediaServer * mediaServer;
int _selectedVertexIndex;
int _activePresetIndex;
vector <SurfaceStack *> _presets;
};
} // namespace piMapper

Loading…
Cancel
Save