Browse Source

Change src code style using ofStyler

master
Krisjanis Rijnieks 10 years ago
parent
commit
95b366ada5
  1. 180
      src/Application/Application.cpp
  2. 56
      src/Application/Application.h
  3. 14
      src/Application/ApplicationBaseState.cpp
  4. 30
      src/Application/ApplicationBaseState.h
  5. 29
      src/Application/PresentationState.cpp
  6. 26
      src/Application/PresentationState.h
  7. 87
      src/Application/ProjectionMappingState.cpp
  8. 28
      src/Application/ProjectionMappingState.h
  9. 29
      src/Application/SourceSelectionState.cpp
  10. 26
      src/Application/SourceSelectionState.h
  11. 29
      src/Application/TextureMappingState.cpp
  12. 26
      src/Application/TextureMappingState.h
  13. 114
      src/Commands/AddSurfaceCmd.cpp
  14. 41
      src/Commands/AddSurfaceCmd.h
  15. 66
      src/Commands/BaseCmd.h
  16. 50
      src/Commands/CmdManager.cpp
  17. 28
      src/Commands/CmdManager.h
  18. 44
      src/Commands/MvAllTexCoordsCmd.cpp
  19. 35
      src/Commands/MvAllTexCoordsCmd.h
  20. 49
      src/Commands/MvSurfaceCmd.cpp
  21. 38
      src/Commands/MvSurfaceCmd.h
  22. 51
      src/Commands/MvSurfaceVertCmd.cpp
  23. 42
      src/Commands/MvSurfaceVertCmd.h
  24. 42
      src/Commands/MvTexCoordCmd.cpp
  25. 35
      src/Commands/MvTexCoordCmd.h
  26. 50
      src/Commands/RmSurfaceCmd.cpp
  27. 33
      src/Commands/RmSurfaceCmd.h
  28. 57
      src/Commands/SelSurfaceCmd.cpp
  29. 42
      src/Commands/SelSurfaceCmd.h
  30. 72
      src/Commands/SetApplicationStateCmd.cpp
  31. 58
      src/Commands/SetApplicationStateCmd.h
  32. 106
      src/Commands/SetSourceCmd.cpp
  33. 51
      src/Commands/SetSourceCmd.h
  34. 69
      src/MediaServer/DirectoryWatcher.cpp
  35. 234
      src/MediaServer/DirectoryWatcher.h
  36. 734
      src/MediaServer/MediaServer.cpp
  37. 159
      src/MediaServer/MediaServer.h
  38. 114
      src/Sources/BaseSource.cpp
  39. 70
      src/Sources/BaseSource.h
  40. 222
      src/Sources/FboSource.cpp
  41. 70
      src/Sources/FboSource.h
  42. 73
      src/Sources/ImageSource.cpp
  43. 30
      src/Sources/ImageSource.h
  44. 88
      src/Sources/SourceType.h
  45. 129
      src/Sources/VideoSource.cpp
  46. 67
      src/Sources/VideoSource.h
  47. 188
      src/Surfaces/BaseSurface.cpp
  48. 74
      src/Surfaces/BaseSurface.h
  49. 492
      src/Surfaces/QuadSurface.cpp
  50. 63
      src/Surfaces/QuadSurface.h
  51. 775
      src/Surfaces/SurfaceManager.cpp
  52. 82
      src/Surfaces/SurfaceManager.h
  53. 627
      src/Surfaces/SurfaceManagerGui.cpp
  54. 80
      src/Surfaces/SurfaceManagerGui.h
  55. 16
      src/Surfaces/SurfaceType.h
  56. 204
      src/Surfaces/TriangleSurface.cpp
  57. 42
      src/Surfaces/TriangleSurface.h
  58. 107
      src/UserInterface/BaseJoint.cpp
  59. 87
      src/UserInterface/BaseJoint.h
  60. 86
      src/UserInterface/CircleJoint.cpp
  61. 23
      src/UserInterface/CircleJoint.h
  62. 10
      src/UserInterface/EditorType.h
  63. 10
      src/UserInterface/GuiMode.h
  64. 553
      src/UserInterface/ProjectionEditor.cpp
  65. 94
      src/UserInterface/ProjectionEditor.h
  66. 394
      src/UserInterface/RadioList.cpp
  67. 94
      src/UserInterface/RadioList.h
  68. 735
      src/UserInterface/SourcesEditor.cpp
  69. 152
      src/UserInterface/SourcesEditor.h
  70. 486
      src/UserInterface/TextureEditor.cpp
  71. 72
      src/UserInterface/TextureEditor.h
  72. 192
      src/ofxPiMapper.cpp
  73. 66
      src/ofxPiMapper.h

180
src/Application/Application.cpp

@ -2,94 +2,94 @@
#include "PresentationState.h" #include "PresentationState.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
Application::Application(ofxPiMapper * opm) { Application::Application(ofxPiMapper * opm){
_ofxPiMapper = opm; _ofxPiMapper = opm;
setState(PresentationState::instance()); setState(PresentationState::instance());
ofAddListener(ofEvents().keyPressed, this, &Application::onKeyPressed); ofAddListener(ofEvents().keyPressed, this, &Application::onKeyPressed);
} }
Application::~Application() { Application::~Application(){
_ofxPiMapper = 0; _ofxPiMapper = 0;
setState(0); setState(0);
ofRemoveListener(ofEvents().keyPressed, this, &Application::onKeyPressed); ofRemoveListener(ofEvents().keyPressed, this, &Application::onKeyPressed);
} }
ApplicationBaseState * Application::getState() { ApplicationBaseState * Application::getState(){
return _state; return _state;
} }
ofxPiMapper * Application::getOfxPiMapper() { ofxPiMapper * Application::getOfxPiMapper(){
return _ofxPiMapper; return _ofxPiMapper;
} }
void Application::draw(){ void Application::draw(){
_state->draw(this); _state->draw(this);
} }
// Here we handle application state changes only // Here we handle application state changes only
void Application::onKeyPressed(ofKeyEventArgs & args) { void Application::onKeyPressed(ofKeyEventArgs & args){
// For now we set the state of the new system and also the old // For now we set the state of the new system and also the old
// before it is completely ported to the state system. // before it is completely ported to the state system.
switch (args.key) { switch(args.key){
case '1': case '1':
_ofxPiMapper->getCmdManager().exec( _ofxPiMapper->getCmdManager().exec(
new ofx::piMapper::SetApplicationStateCmd( new ofx::piMapper::SetApplicationStateCmd(
this, PresentationState::instance(), this, PresentationState::instance(),
&_ofxPiMapper->getGui(), GuiMode::NONE)); &_ofxPiMapper->getGui(), GuiMode::NONE));
break; break;
case '2': case '2':
_ofxPiMapper->getCmdManager().exec( _ofxPiMapper->getCmdManager().exec(
new ofx::piMapper::SetApplicationStateCmd( new ofx::piMapper::SetApplicationStateCmd(
this, TextureMappingState::instance(), this, TextureMappingState::instance(),
&_ofxPiMapper->getGui(), GuiMode::TEXTURE_MAPPING)); &_ofxPiMapper->getGui(), GuiMode::TEXTURE_MAPPING));
break; break;
case '3': case '3':
_ofxPiMapper->getCmdManager().exec( _ofxPiMapper->getCmdManager().exec(
new ofx::piMapper::SetApplicationStateCmd( new ofx::piMapper::SetApplicationStateCmd(
this, ProjectionMappingState::instance(), this, ProjectionMappingState::instance(),
&_ofxPiMapper->getGui(), GuiMode::PROJECTION_MAPPING)); &_ofxPiMapper->getGui(), GuiMode::PROJECTION_MAPPING));
break; break;
case '4': case '4':
_ofxPiMapper->getCmdManager().exec( _ofxPiMapper->getCmdManager().exec(
new ofx::piMapper::SetApplicationStateCmd( new ofx::piMapper::SetApplicationStateCmd(
this, SourceSelectionState::instance(), this, SourceSelectionState::instance(),
&_ofxPiMapper->getGui(), GuiMode::SOURCE_SELECTION)); &_ofxPiMapper->getGui(), GuiMode::SOURCE_SELECTION));
break; break;
case 'f': case 'f':
ofToggleFullscreen(); ofToggleFullscreen();
break; break;
case 'i': case 'i':
_ofxPiMapper->toggleInfo(); _ofxPiMapper->toggleInfo();
break; break;
case 's': case 's':
_ofxPiMapper->getSurfaceManager().saveXmlSettings( _ofxPiMapper->getSurfaceManager().saveXmlSettings(
PIMAPPER_USER_SURFACES_XML_FILE); PIMAPPER_USER_SURFACES_XML_FILE);
break; break;
case 'z': case 'z':
_ofxPiMapper->getCmdManager().undo(); _ofxPiMapper->getCmdManager().undo();
break; break;
default: default:
// All the other keypresses are handled by the application state onKeyPressed // All the other keypresses are handled by the application state onKeyPressed
_state->onKeyPressed(this, args); _state->onKeyPressed(this, args);
break; break;
} }
} }
void Application::setState(ApplicationBaseState * st){ void Application::setState(ApplicationBaseState * st){
_state = st; _state = st;
} }
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

56
src/Application/Application.h

@ -19,31 +19,33 @@
class ofxPiMapper; class ofxPiMapper;
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class ApplicationBaseState; class ApplicationBaseState;
class Application { class Application {
public:
Application(ofxPiMapper * opm); public:
~Application(); Application(ofxPiMapper * opm);
~Application();
ApplicationBaseState * getState();
ofxPiMapper * getOfxPiMapper(); // Temporary method. ApplicationBaseState * getState();
ofxPiMapper * getOfxPiMapper(); // Temporary method.
void draw();
void onKeyPressed(ofKeyEventArgs & args); void draw();
void onKeyPressed(ofKeyEventArgs & args);
protected:
void setState(ApplicationBaseState * st); protected:
void setState(ApplicationBaseState * st);
private:
friend class ApplicationBaseState; private:
friend class SetApplicationStateCmd; friend class ApplicationBaseState;
friend class SetApplicationStateCmd;
ApplicationBaseState * _state;
ofxPiMapper * _ofxPiMapper; ApplicationBaseState * _state;
}; ofxPiMapper * _ofxPiMapper;
} // namespace piMapper };
} // namespace piMapper
} // namespace ofx } // namespace ofx

14
src/Application/ApplicationBaseState.cpp

@ -2,11 +2,11 @@
#include "PresentationState.h" #include "PresentationState.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
void ApplicationBaseState::setState(Application * app, ApplicationBaseState * st) { void ApplicationBaseState::setState(Application * app, ApplicationBaseState * st){
app->setState(st); app->setState(st);
} }
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

30
src/Application/ApplicationBaseState.h

@ -4,18 +4,20 @@
#include "ofLog.h" #include "ofLog.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class Application; class Application;
class ApplicationBaseState { class ApplicationBaseState {
public:
virtual void draw(Application * app){}; public:
virtual void setState(Application * app, ApplicationBaseState * st); virtual void draw(Application * app){}
virtual void setState(Application * app, ApplicationBaseState * st);
// Event handler virtual methods
virtual void onKeyPressed(Application * app, ofKeyEventArgs & args){}; // Event handler virtual methods
}; virtual void onKeyPressed(Application * app, ofKeyEventArgs & args){}
} // namespace piMapper };
} // namespace piMapper
} // namespace ofx } // namespace ofx

29
src/Application/PresentationState.cpp

@ -1,17 +1,18 @@
#include "PresentationState.h" #include "PresentationState.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
PresentationState * PresentationState::_instance = 0; PresentationState * PresentationState::_instance = 0;
PresentationState * PresentationState::instance() { PresentationState * PresentationState::instance(){
if (_instance == 0) { if(_instance == 0){
_instance = new ofx::piMapper::PresentationState(); _instance = new ofx::piMapper::PresentationState();
} }
return _instance; return _instance;
} }
void PresentationState::draw(Application * app) {} void PresentationState::draw(Application * app){}
}
} } // namespace piMapper
} // namespace ofx

26
src/Application/PresentationState.h

@ -6,16 +6,18 @@
#include "ofGraphics.h" #include "ofGraphics.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class PresentationState : public ApplicationBaseState { class PresentationState : public ApplicationBaseState {
public:
static PresentationState * instance(); public:
void draw(Application * app); static PresentationState * instance();
void draw(Application * app);
private:
static PresentationState * _instance; private:
}; static PresentationState * _instance;
} // namespace piMapper };
} // namespace piMapper
} // namespace ofx } // namespace ofx

87
src/Application/ProjectionMappingState.cpp

@ -1,46 +1,47 @@
#include "ProjectionMappingState.h" #include "ProjectionMappingState.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
ProjectionMappingState * ProjectionMappingState::_instance = 0; ProjectionMappingState * ProjectionMappingState::_instance = 0;
ProjectionMappingState * ProjectionMappingState::instance() { ProjectionMappingState * ProjectionMappingState::instance(){
if (_instance == 0) { if(_instance == 0){
_instance = new ofx::piMapper::ProjectionMappingState(); _instance = new ofx::piMapper::ProjectionMappingState();
} }
return _instance; return _instance;
} }
void ProjectionMappingState::draw(Application * app) {} void ProjectionMappingState::draw(Application * app){}
void ProjectionMappingState::onKeyPressed(Application * app, ofKeyEventArgs & args) { void ProjectionMappingState::onKeyPressed(Application * app, ofKeyEventArgs & args){
switch (args.key) { switch(args.key){
case 't': case 't':
app->getOfxPiMapper()->getCmdManager().exec( app->getOfxPiMapper()->getCmdManager().exec(
new AddSurfaceCmd( new AddSurfaceCmd(
app->getOfxPiMapper(), app->getOfxPiMapper(),
SurfaceType::TRIANGLE_SURFACE) SurfaceType::TRIANGLE_SURFACE)
); );
break; break;
case 'q': case 'q':
app->getOfxPiMapper()->getCmdManager().exec( app->getOfxPiMapper()->getCmdManager().exec(
new AddSurfaceCmd( new AddSurfaceCmd(
app->getOfxPiMapper(), app->getOfxPiMapper(),
SurfaceType::QUAD_SURFACE) SurfaceType::QUAD_SURFACE)
); );
break; break;
case OF_KEY_BACKSPACE: case OF_KEY_BACKSPACE:
app->getOfxPiMapper()->getCmdManager().exec( app->getOfxPiMapper()->getCmdManager().exec(
new RmSurfaceCmd(app->getOfxPiMapper())); new RmSurfaceCmd(app->getOfxPiMapper()));
break; break;
default: default:
break; break;
} }
} }
}
} } // namespace piMapper
} // namespace ofx

28
src/Application/ProjectionMappingState.h

@ -8,17 +8,19 @@
#include "SurfaceType.h" #include "SurfaceType.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class ProjectionMappingState : public ApplicationBaseState { class ProjectionMappingState : public ApplicationBaseState {
public:
static ProjectionMappingState * instance(); public:
void draw(Application * app); static ProjectionMappingState * instance();
void onKeyPressed(Application * app, ofKeyEventArgs & args); void draw(Application * app);
void onKeyPressed(Application * app, ofKeyEventArgs & args);
private:
static ProjectionMappingState * _instance; private:
}; static ProjectionMappingState * _instance;
} // namespace piMapper };
} // namespace piMapper
} // namespace ofx } // namespace ofx

29
src/Application/SourceSelectionState.cpp

@ -1,17 +1,18 @@
#include "SourceSelectionState.h" #include "SourceSelectionState.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
SourceSelectionState * SourceSelectionState::_instance = 0; SourceSelectionState * SourceSelectionState::_instance = 0;
SourceSelectionState * SourceSelectionState::instance() { SourceSelectionState * SourceSelectionState::instance(){
if (_instance == 0) { if(_instance == 0){
_instance = new ofx::piMapper::SourceSelectionState(); _instance = new ofx::piMapper::SourceSelectionState();
} }
return _instance; return _instance;
} }
void SourceSelectionState::draw(Application * app) {} void SourceSelectionState::draw(Application * app){}
}
} } // namespace piMapper
} // namespace ofx

26
src/Application/SourceSelectionState.h

@ -6,16 +6,18 @@
#include "ofGraphics.h" #include "ofGraphics.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class SourceSelectionState : public ApplicationBaseState { class SourceSelectionState : public ApplicationBaseState {
public:
static SourceSelectionState * instance(); public:
void draw(Application * app); static SourceSelectionState * instance();
void draw(Application * app);
private:
static SourceSelectionState * _instance; private:
}; static SourceSelectionState * _instance;
} // namespace piMapper };
} // namespace piMapper
} // namespace ofx } // namespace ofx

29
src/Application/TextureMappingState.cpp

@ -1,17 +1,18 @@
#include "TextureMappingState.h" #include "TextureMappingState.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
TextureMappingState * TextureMappingState::_instance = 0; TextureMappingState * TextureMappingState::_instance = 0;
TextureMappingState * TextureMappingState::instance() { TextureMappingState * TextureMappingState::instance(){
if (_instance == 0) { if(_instance == 0){
_instance = new ofx::piMapper::TextureMappingState(); _instance = new ofx::piMapper::TextureMappingState();
} }
return _instance; return _instance;
} }
void TextureMappingState::draw(Application * app) {} void TextureMappingState::draw(Application * app){}
}
} } // namespace piMapper
} // namespace ofx

26
src/Application/TextureMappingState.h

@ -6,16 +6,18 @@
#include "ofGraphics.h" #include "ofGraphics.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class TextureMappingState : public ApplicationBaseState { class TextureMappingState : public ApplicationBaseState {
public:
static TextureMappingState * instance(); public:
void draw(Application * app); static TextureMappingState * instance();
void draw(Application * app);
private:
static TextureMappingState * _instance; private:
}; static TextureMappingState * _instance;
} // namespace piMapper };
} // namespace piMapper
} // namespace ofx } // namespace ofx

114
src/Commands/AddSurfaceCmd.cpp

@ -1,61 +1,61 @@
#include "AddSurfaceCmd.h" #include "AddSurfaceCmd.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
AddSurfaceCmd::AddSurfaceCmd(ofxPiMapper * app, int surfaceType){ AddSurfaceCmd::AddSurfaceCmd(ofxPiMapper * app, int surfaceType){
_app = app; _app = app;
_surfaceType = surfaceType; _surfaceType = surfaceType;
} }
void AddSurfaceCmd::exec(){ void AddSurfaceCmd::exec(){
if (_surfaceType == SurfaceType::TRIANGLE_SURFACE) { if(_surfaceType == SurfaceType::TRIANGLE_SURFACE){
addTriangleSurface(); addTriangleSurface();
} else if (_surfaceType == SurfaceType::QUAD_SURFACE) { }else if(_surfaceType == SurfaceType::QUAD_SURFACE){
addQuadSurface(); addQuadSurface();
} }
} }
void AddSurfaceCmd::undo(){ void AddSurfaceCmd::undo(){
ofLogNotice("AddSurfaceCmd", "undo"); ofLogNotice("AddSurfaceCmd", "undo");
_app->getSurfaceManager().removeSurface(); _app->getSurfaceManager().removeSurface();
} }
void AddSurfaceCmd::addTriangleSurface() { void AddSurfaceCmd::addTriangleSurface(){
int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE; int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE;
vector<ofVec2f> vertices; vector <ofVec2f> vertices;
float margin = 50.0f; float margin = 50.0f;
vertices.push_back(ofVec2f((float)ofGetWidth() / 2.0f, margin)); vertices.push_back(ofVec2f((float)ofGetWidth() / 2.0f, margin));
vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin)); vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin));
vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin)); vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin));
vector<ofVec2f> texCoords; vector <ofVec2f> texCoords;
texCoords.push_back(ofVec2f(0.5f, 0.0f)); texCoords.push_back(ofVec2f(0.5f, 0.0f));
texCoords.push_back(ofVec2f(1.0f, 1.0f)); texCoords.push_back(ofVec2f(1.0f, 1.0f));
texCoords.push_back(ofVec2f(0.0f, 1.0f)); texCoords.push_back(ofVec2f(0.0f, 1.0f));
_app->getSurfaceManager().addSurface(surfaceType, vertices, texCoords); _app->getSurfaceManager().addSurface(surfaceType, vertices, texCoords);
} }
void AddSurfaceCmd::addQuadSurface() { void AddSurfaceCmd::addQuadSurface(){
int surfaceType = ofx::piMapper::SurfaceType::QUAD_SURFACE; int surfaceType = ofx::piMapper::SurfaceType::QUAD_SURFACE;
vector<ofVec2f> vertices; vector <ofVec2f> vertices;
float margin = 50.0f; float margin = 50.0f;
vertices.push_back(ofVec2f(margin, margin)); vertices.push_back(ofVec2f(margin, margin));
vertices.push_back(ofVec2f((float)ofGetWidth() - margin, margin)); vertices.push_back(ofVec2f((float)ofGetWidth() - margin, margin));
vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin)); vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin));
vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin)); vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin));
vector<ofVec2f> texCoords; vector <ofVec2f> texCoords;
texCoords.push_back(ofVec2f(ofVec2f(0.0f, 0.0f))); texCoords.push_back(ofVec2f(ofVec2f(0.0f, 0.0f)));
texCoords.push_back(ofVec2f(ofVec2f(1.0f, 0.0f))); texCoords.push_back(ofVec2f(ofVec2f(1.0f, 0.0f)));
texCoords.push_back(ofVec2f(ofVec2f(1.0f, 1.0f))); texCoords.push_back(ofVec2f(ofVec2f(1.0f, 1.0f)));
texCoords.push_back(ofVec2f(ofVec2f(0.0f, 1.0f))); texCoords.push_back(ofVec2f(ofVec2f(0.0f, 1.0f)));
_app->getSurfaceManager().addSurface(surfaceType, vertices, texCoords); _app->getSurfaceManager().addSurface(surfaceType, vertices, texCoords);
} }
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

41
src/Commands/AddSurfaceCmd.h

@ -7,25 +7,26 @@
class ofxPiMapper; class ofxPiMapper;
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class AddSurfaceCmd : public BaseUndoCmd{ class AddSurfaceCmd : public BaseUndoCmd {
public: public:
AddSurfaceCmd(ofxPiMapper * app, int surfaceType); AddSurfaceCmd(ofxPiMapper * app, int surfaceType);
void exec(); void exec();
void undo(); void undo();
private: private:
ofxPiMapper * _app; ofxPiMapper * _app;
int _surfaceType; int _surfaceType;
// TODO: Should use some kind of factory class here // TODO: Should use some kind of factory class here
void addTriangleSurface(); void addTriangleSurface();
void addQuadSurface(); void addQuadSurface();
};
};
} // namespace piMapper
} // namespace piMapper
} // namespace ofx } // namespace ofx

66
src/Commands/BaseCmd.h

@ -9,33 +9,41 @@
#pragma once #pragma once
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
// Base class for all commands // Base class for all commands
class BaseCmd{ class BaseCmd {
public:
virtual ~BaseCmd(){}; public:
virtual void exec() = 0; virtual ~BaseCmd(){}
virtual void exec() = 0;
// By default a command is not undo
virtual bool isUndoable(){return false;} // By default a command is not undo
virtual bool isUndoable(){
protected: return false;
// In order to avoid using this class directly, }
// we make the constructor protected.
BaseCmd(){}; protected:
}; // In order to avoid using this class directly,
// we make the constructor protected.
// Base class for all undoable commands BaseCmd(){}
class BaseUndoCmd : public BaseCmd{
public: };
virtual void undo() = 0;
virtual bool isUndoable(){return true;} // Base class for all undoable commands
class BaseUndoCmd : public BaseCmd {
protected:
BaseUndoCmd(){}; public:
}; virtual void undo() = 0;
virtual bool isUndoable(){
} // namespace piMapper return true;
}
protected:
BaseUndoCmd(){}
};
} // namespace piMapper
} // namespace ofx } // namespace ofx

50
src/Commands/CmdManager.cpp

@ -1,28 +1,28 @@
#include "CmdManager.h" #include "CmdManager.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
void CmdManager::exec(BaseCmd * cmd){ void CmdManager::exec(BaseCmd * cmd){
cmd->exec(); cmd->exec();
if (cmd->isUndoable()){ if(cmd->isUndoable()){
cmdStack.push_back(static_cast<BaseUndoCmd *>(cmd)); cmdStack.push_back(static_cast <BaseUndoCmd *>(cmd));
} }
} }
void CmdManager::undo(){ void CmdManager::undo(){
ofLogNotice("CmdManager", "undo"); ofLogNotice("CmdManager", "undo");
if (cmdStack.size() > 0){ if(cmdStack.size() > 0){
BaseUndoCmd * cmd = cmdStack.back(); BaseUndoCmd * cmd = cmdStack.back();
cmd->undo(); cmd->undo();
// Delete last command now, change this when implementing redo. // Delete last command now, change this when implementing redo.
delete cmdStack.back(); delete cmdStack.back();
cmdStack.pop_back(); cmdStack.pop_back();
} else { }else{
ofLogNotice("CmdManager", "Nothing to undo"); ofLogNotice("CmdManager", "Nothing to undo");
} }
} }
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

28
src/Commands/CmdManager.h

@ -4,17 +4,19 @@
#include "BaseCmd.h" #include "BaseCmd.h"
#include "ofLog.h" #include "ofLog.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class CmdManager{ class CmdManager {
public:
void exec(BaseCmd * cmd); public:
void undo(); void exec(BaseCmd * cmd);
void undo();
private:
std::vector<BaseUndoCmd *> cmdStack; private:
}; std::vector <BaseUndoCmd *> cmdStack;
} // namespace piMapper };
} // namespace piMapper
} // namespace ofx } // namespace ofx

44
src/Commands/MvAllTexCoordsCmd.cpp

@ -1,27 +1,27 @@
#include "MvAllTexCoordsCmd.h" #include "MvAllTexCoordsCmd.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
MvAllTexCoordsCmd::MvAllTexCoordsCmd(BaseSurface * surface, TextureEditor * texEditor){
_surface = surface;
_texEditor = texEditor;
}
void MvAllTexCoordsCmd::exec(){ MvAllTexCoordsCmd::MvAllTexCoordsCmd(BaseSurface * surface, TextureEditor * texEditor){
ofLogNotice("MvAllTexCoordsCmd", "exec"); _surface = surface;
_texCoords = _surface->getTexCoords(); _texEditor = texEditor;
} }
void MvAllTexCoordsCmd::undo(){ void MvAllTexCoordsCmd::exec(){
ofLogNotice("MvAllTexCoordsCmd", "undo"); ofLogNotice("MvAllTexCoordsCmd", "exec");
ofVec2f dist = _texCoords[0] - _surface->getTexCoords()[0]; _texCoords = _surface->getTexCoords();
dist.x = _surface->getSource()->getTexture()->getWidth() * dist.x; }
dist.y = _surface->getSource()->getTexture()->getHeight() * dist.y;
_texEditor->moveTexCoords(dist); void MvAllTexCoordsCmd::undo(){
_surface = 0; ofLogNotice("MvAllTexCoordsCmd", "undo");
} ofVec2f dist = _texCoords[0] - _surface->getTexCoords()[0];
dist.x = _surface->getSource()->getTexture()->getWidth() * dist.x;
} // namespace piMapper dist.y = _surface->getSource()->getTexture()->getHeight() * dist.y;
_texEditor->moveTexCoords(dist);
_surface = 0;
}
} // namespace piMapper
} // namespace ofx } // namespace ofx

35
src/Commands/MvAllTexCoordsCmd.h

@ -8,22 +8,23 @@
#include "BaseSurface.h" #include "BaseSurface.h"
#include "TextureEditor.h" #include "TextureEditor.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class MvAllTexCoordsCmd : public BaseUndoCmd{ class MvAllTexCoordsCmd : public BaseUndoCmd {
public: public:
MvAllTexCoordsCmd(BaseSurface * surface, TextureEditor * texEditor); MvAllTexCoordsCmd(BaseSurface * surface, TextureEditor * texEditor);
void exec(); void exec();
void undo(); void undo();
private: private:
vector<ofVec2f> _texCoords; vector <ofVec2f> _texCoords;
BaseSurface * _surface; BaseSurface * _surface;
TextureEditor * _texEditor; TextureEditor * _texEditor;
};
};
} // namespace piMapper
} // namespace piMapper
} // namespace ofx } // namespace ofx

49
src/Commands/MvSurfaceCmd.cpp

@ -1,30 +1,29 @@
#include "MvSurfaceCmd.h" #include "MvSurfaceCmd.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
MvSurfaceCmd::MvSurfaceCmd(
BaseSurface * surface,
ProjectionEditor * projectionEditor){
_surface = surface;
_projectionEditor = projectionEditor;
}
void MvSurfaceCmd::exec(){ MvSurfaceCmd::MvSurfaceCmd(BaseSurface * surface,
ofLogNotice("MvSurfaceCmd", "exec"); ProjectionEditor * projectionEditor){
_previousVertices = _surface->getVertices();
_surface->setMoved(false); _surface = surface;
} _projectionEditor = projectionEditor;
}
void MvSurfaceCmd::undo(){
ofLogNotice("MvSurfaceCmd", "undo"); void MvSurfaceCmd::exec(){
_surface->moveBy(_previousVertices[0] - _surface->getVertices()[0]); ofLogNotice("MvSurfaceCmd", "exec");
_projectionEditor->updateJoints(); _previousVertices = _surface->getVertices();
_previousVertices.clear(); _surface->setMoved(false);
_surface = 0; }
}
void MvSurfaceCmd::undo(){
} // namespace piMapper ofLogNotice("MvSurfaceCmd", "undo");
_surface->moveBy(_previousVertices[0] - _surface->getVertices()[0]);
_projectionEditor->updateJoints();
_previousVertices.clear();
_surface = 0;
}
} // namespace piMapper
} // namespace ofx } // namespace ofx

38
src/Commands/MvSurfaceCmd.h

@ -8,24 +8,24 @@
#include "BaseSurface.h" #include "BaseSurface.h"
#include "ProjectionEditor.h" #include "ProjectionEditor.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class MvSurfaceCmd : public BaseUndoCmd{ class MvSurfaceCmd : public BaseUndoCmd {
public: public:
MvSurfaceCmd( MvSurfaceCmd(BaseSurface * surface,
BaseSurface * surface, ProjectionEditor * projectionEditor);
ProjectionEditor * projectionEditor); void exec();
void exec(); void undo();
void undo();
private:
private: BaseSurface * _surface;
BaseSurface * _surface; ProjectionEditor * _projectionEditor;
ProjectionEditor * _projectionEditor; vector <ofVec3f> _previousVertices;
vector<ofVec3f> _previousVertices;
}; };
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

51
src/Commands/MvSurfaceVertCmd.cpp

@ -1,31 +1,30 @@
#include "MvSurfaceVertCmd.h" #include "MvSurfaceVertCmd.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
MvSurfaceVertCmd::MvSurfaceVertCmd(
int vertIndex,
BaseSurface * surface,
ProjectionEditor * projectionEditor){
_vertIndex = vertIndex;
_surface = surface;
_projectionEditor = projectionEditor;
}
void MvSurfaceVertCmd::exec(){ MvSurfaceVertCmd::MvSurfaceVertCmd(int vertIndex,
ofLogNotice("MvSurfaceVertCommand", "exec"); BaseSurface * surface,
_prevVertPos = _surface->getVertices()[_vertIndex]; ProjectionEditor * projectionEditor){
}
_vertIndex = vertIndex;
void MvSurfaceVertCmd::undo(){ _surface = surface;
ofLogNotice("MvSurfaceVertCommand", "undo"); _projectionEditor = projectionEditor;
_surface->setVertex(_vertIndex, _prevVertPos); }
_projectionEditor->updateJoints();
_projectionEditor = 0; void MvSurfaceVertCmd::exec(){
_surface = 0; ofLogNotice("MvSurfaceVertCommand", "exec");
} _prevVertPos = _surface->getVertices()[_vertIndex];
}
} // namespace piMapper
void MvSurfaceVertCmd::undo(){
ofLogNotice("MvSurfaceVertCommand", "undo");
_surface->setVertex(_vertIndex, _prevVertPos);
_projectionEditor->updateJoints();
_projectionEditor = 0;
_surface = 0;
}
} // namespace piMapper
} // namespace ofx } // namespace ofx

42
src/Commands/MvSurfaceVertCmd.h

@ -9,26 +9,26 @@
#include "ProjectionEditor.h" #include "ProjectionEditor.h"
#include "BaseJoint.h" #include "BaseJoint.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class MvSurfaceVertCmd : public BaseUndoCmd{ class MvSurfaceVertCmd : public BaseUndoCmd {
public: public:
MvSurfaceVertCmd( MvSurfaceVertCmd(int vertIndex,
int vertIndex, BaseSurface * surface,
BaseSurface * surface, ProjectionEditor * projectionEditor);
ProjectionEditor * projectionEditor); void exec();
void exec(); void undo();
void undo();
private:
private: int _vertIndex;
int _vertIndex; ofVec2f _prevVertPos;
ofVec2f _prevVertPos; BaseSurface * _surface;
BaseSurface * _surface; ProjectionEditor * _projectionEditor;
ProjectionEditor * _projectionEditor;
}; };
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

42
src/Commands/MvTexCoordCmd.cpp

@ -1,26 +1,26 @@
#include "MvTexCoordCmd.h" #include "MvTexCoordCmd.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
MvTexCoordCmd::MvTexCoordCmd(int jointIndex, TextureEditor * texEditor){
_jointIndex = jointIndex;
_texEditor = texEditor;
}
void MvTexCoordCmd::exec(){ MvTexCoordCmd::MvTexCoordCmd(int jointIndex, TextureEditor * texEditor){
ofLogNotice("MvTexCoordCmd", "exec"); _jointIndex = jointIndex;
_jointPosition = _texEditor->getJoints()[_jointIndex]->position; _texEditor = texEditor;
} }
void MvTexCoordCmd::undo(){ void MvTexCoordCmd::exec(){
ofLogNotice("MvTexCoordCmd", "undo"); ofLogNotice("MvTexCoordCmd", "exec");
_texEditor->unselectAllJoints(); _jointPosition = _texEditor->getJoints()[_jointIndex]->position;
_texEditor->getJoints()[_jointIndex]->select(); }
_texEditor->getJoints()[_jointIndex]->position = _jointPosition;
_texEditor = 0; void MvTexCoordCmd::undo(){
} ofLogNotice("MvTexCoordCmd", "undo");
_texEditor->unselectAllJoints();
} // namespace piMapper _texEditor->getJoints()[_jointIndex]->select();
_texEditor->getJoints()[_jointIndex]->position = _jointPosition;
_texEditor = 0;
}
} // namespace piMapper
} // namespace ofx } // namespace ofx

35
src/Commands/MvTexCoordCmd.h

@ -8,22 +8,23 @@
#include "CircleJoint.h" #include "CircleJoint.h"
#include "TextureEditor.h" #include "TextureEditor.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class MvTexCoordCmd : public BaseUndoCmd{ class MvTexCoordCmd : public BaseUndoCmd {
public: public:
MvTexCoordCmd(int jointIndex, TextureEditor * texEditor); MvTexCoordCmd(int jointIndex, TextureEditor * texEditor);
void exec(); void exec();
void undo(); void undo();
private: private:
ofVec2f _jointPosition; ofVec2f _jointPosition;
int _jointIndex; int _jointIndex;
TextureEditor * _texEditor; TextureEditor * _texEditor;
};
};
} // namespace piMapper
} // namespace piMapper
} // namespace ofx } // namespace ofx

50
src/Commands/RmSurfaceCmd.cpp

@ -1,30 +1,30 @@
#include "RmSurfaceCmd.h" #include "RmSurfaceCmd.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
RmSurfaceCmd::RmSurfaceCmd(ofxPiMapper * app){
_app = app;
_surface = 0;
}
void RmSurfaceCmd::exec(){ RmSurfaceCmd::RmSurfaceCmd(ofxPiMapper * app){
// Store the surface, this implies that the surfaceManager's _app = app;
// removeSelectedSurface does not destroy the surface. _surface = 0;
_surface = _app->surfaceManager.getSelectedSurface(); }
_app->surfaceManager.removeSelectedSurface();
} void RmSurfaceCmd::exec(){
// Store the surface, this implies that the surfaceManager's
void RmSurfaceCmd::undo(){ // removeSelectedSurface does not destroy the surface.
ofLogNotice("RmSurfaceCmd", "undo"); _surface = _app->surfaceManager.getSelectedSurface();
if (_surface == 0) { _app->surfaceManager.removeSelectedSurface();
ofLogError("RmSurfaceCmd", "No surface stored"); }
}
_app->surfaceManager.addSurface(_surface); void RmSurfaceCmd::undo(){
_app->surfaceManager.selectSurface(_surface); ofLogNotice("RmSurfaceCmd", "undo");
_surface = 0; if(_surface == 0){
} ofLogError("RmSurfaceCmd", "No surface stored");
}
} // namespace piMapper _app->surfaceManager.addSurface(_surface);
_app->surfaceManager.selectSurface(_surface);
_surface = 0;
}
} // namespace piMapper
} // namespace ofx } // namespace ofx

33
src/Commands/RmSurfaceCmd.h

@ -10,21 +10,22 @@
class ofxPiMapper; class ofxPiMapper;
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class RmSurfaceCmd : public BaseUndoCmd{ class RmSurfaceCmd : public BaseUndoCmd {
public: public:
RmSurfaceCmd(ofxPiMapper * app); RmSurfaceCmd(ofxPiMapper * app);
void exec(); void exec();
void undo(); void undo();
private: private:
ofxPiMapper * _app; ofxPiMapper * _app;
BaseSurface * _surface; BaseSurface * _surface;
};
};
} // namespace piMapper
} // namespace piMapper
} // namespace ofx } // namespace ofx

57
src/Commands/SelSurfaceCmd.cpp

@ -1,34 +1,33 @@
#include "SelSurfaceCmd.h" #include "SelSurfaceCmd.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
SelSurfaceCmd::SelSurfaceCmd(
SurfaceManager * surfaceManager,
BaseSurface * surfaceToSelect,
ProjectionEditor * projectionEditor){
_surfaceManager = surfaceManager;
_surfaceToSelect = surfaceToSelect;
_projectionEditor = projectionEditor;
}
void SelSurfaceCmd::exec(){ SelSurfaceCmd::SelSurfaceCmd(SurfaceManager * surfaceManager,
_prevSelectedSurface = _surfaceManager->getSelectedSurface(); BaseSurface * surfaceToSelect,
_projectionEditor->clearJoints(); ProjectionEditor * projectionEditor){
_surfaceManager->selectSurface(_surfaceToSelect);
_projectionEditor->createJoints(); _surfaceManager = surfaceManager;
} _surfaceToSelect = surfaceToSelect;
_projectionEditor = projectionEditor;
void SelSurfaceCmd::undo(){ }
ofLogNotice("SelSurfaceCmd", "undo");
_projectionEditor->clearJoints(); void SelSurfaceCmd::exec(){
_surfaceManager->selectSurface(_prevSelectedSurface); _prevSelectedSurface = _surfaceManager->getSelectedSurface();
_projectionEditor->createJoints(); _projectionEditor->clearJoints();
_surfaceToSelect = 0; _surfaceManager->selectSurface(_surfaceToSelect);
_prevSelectedSurface = 0; _projectionEditor->createJoints();
} }
} // namespace piMapper void SelSurfaceCmd::undo(){
ofLogNotice("SelSurfaceCmd", "undo");
_projectionEditor->clearJoints();
_surfaceManager->selectSurface(_prevSelectedSurface);
_projectionEditor->createJoints();
_surfaceToSelect = 0;
_prevSelectedSurface = 0;
}
} // namespace piMapper
} // namespace ofx } // namespace ofx

42
src/Commands/SelSurfaceCmd.h

@ -9,26 +9,26 @@
#include "SurfaceManager.h" #include "SurfaceManager.h"
#include "ProjectionEditor.h" #include "ProjectionEditor.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class SelSurfaceCmd : public BaseUndoCmd{ class SelSurfaceCmd : public BaseUndoCmd {
public: public:
SelSurfaceCmd( SelSurfaceCmd(SurfaceManager * surfaceManager,
SurfaceManager * surfaceManager, BaseSurface * surfaceToSelect,
BaseSurface * surfaceToSelect, ProjectionEditor * projectionEditor);
ProjectionEditor * projectionEditor); void exec();
void exec(); void undo();
void undo();
private:
private: BaseSurface * _surfaceToSelect;
BaseSurface * _surfaceToSelect; SurfaceManager * _surfaceManager;
SurfaceManager * _surfaceManager; BaseSurface * _prevSelectedSurface;
BaseSurface * _prevSelectedSurface; ProjectionEditor * _projectionEditor;
ProjectionEditor * _projectionEditor;
}; };
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

72
src/Commands/SetApplicationStateCmd.cpp

@ -1,42 +1,40 @@
#include "SetApplicationStateCmd.h" #include "SetApplicationStateCmd.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
SetApplicationStateCmd::SetApplicationStateCmd( SetApplicationStateCmd::SetApplicationStateCmd(Application * app,
Application * app, ApplicationBaseState * st,
ApplicationBaseState * st, SurfaceManagerGui * gui,
int mode){
SurfaceManagerGui * gui,
int mode) { _application = app;
_prevApplicationState = 0;
_application = app; _applicationState = st;
_prevApplicationState = 0;
_applicationState = st; // TODO: To be removed
_gui = gui;
// TODO: To be removed _prevGuiMode = -1;
_gui = gui; _mode = mode;
_prevGuiMode = -1; }
_mode = mode;
} void SetApplicationStateCmd::exec(){
_prevApplicationState = _application->getState();
void SetApplicationStateCmd::exec() { _application->setState(_applicationState);
_prevApplicationState = _application->getState();
_application->setState(_applicationState); // TODO: To be removed.
_prevGuiMode = _gui->getMode();
// TODO: To be removed. _gui->setMode(_mode);
_prevGuiMode = _gui->getMode(); }
_gui->setMode(_mode);
} void SetApplicationStateCmd::undo(){
ofLogNotice("SetApplicationStateCmd", "undo");
void SetApplicationStateCmd::undo() { _application->setState(_prevApplicationState);
ofLogNotice("SetApplicationStateCmd", "undo");
_application->setState(_prevApplicationState); // TODO: To be removed.
_gui->setMode(_prevGuiMode);
// TODO: To be removed. }
_gui->setMode(_prevGuiMode);
} } // namespace piMapper
} // namespace piMapper
} // namespace ofx } // namespace ofx

58
src/Commands/SetApplicationStateCmd.h

@ -4,34 +4,34 @@
#include "Application.h" #include "Application.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class Application; class Application;
class ApplicationBaseState; class ApplicationBaseState;
class SetApplicationStateCmd : public BaseUndoCmd { class SetApplicationStateCmd : public BaseUndoCmd {
public: public:
SetApplicationStateCmd( SetApplicationStateCmd(Application * app,
Application * app, ApplicationBaseState * st,
ApplicationBaseState * st, SurfaceManagerGui * gui,
SurfaceManagerGui * gui, int mode);
int mode);
void exec();
void exec(); void undo();
void undo();
private:
private: Application * _application;
Application * _application; ApplicationBaseState * _prevApplicationState;
ApplicationBaseState * _prevApplicationState; ApplicationBaseState * _applicationState;
ApplicationBaseState * _applicationState;
// TODO: Remove these after porting to app state system is done
// TODO: Remove these after porting to app state system is done SurfaceManagerGui * _gui;
SurfaceManagerGui * _gui; int _prevGuiMode;
int _prevGuiMode; int _mode;
int _mode;
}; };
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

106
src/Commands/SetSourceCmd.cpp

@ -1,57 +1,57 @@
#include "SetSourceCmd.h" #include "SetSourceCmd.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
SetSourceCmd::SetSourceCmd(int sourceType, SetSourceCmd::SetSourceCmd(int sourceType,
string sourceId, string sourceId,
BaseSurface * surface, BaseSurface * surface,
SourcesEditor * sourcesEditor){ SourcesEditor * sourcesEditor){
_sourceType = sourceType; _sourceType = sourceType;
_sourceId = sourceId; _sourceId = sourceId;
_surface = surface; _surface = surface;
_sourcesEditor = sourcesEditor; _sourcesEditor = sourcesEditor;
} }
void SetSourceCmd::exec(){ void SetSourceCmd::exec(){
ofLogNotice("SetSourceCmd", "exec"); ofLogNotice("SetSourceCmd", "exec");
_oldSourceType = _surface->getSource()->getType(); _oldSourceType = _surface->getSource()->getType();
if (_surface->getSource()->isLoadable()) { if(_surface->getSource()->isLoadable()){
_oldSourceId = _surface->getSource()->getPath(); _oldSourceId = _surface->getSource()->getPath();
} else { }else{
_oldSourceId = _surface->getSource()->getName(); _oldSourceId = _surface->getSource()->getName();
} }
if (_sourceType == SourceType::SOURCE_TYPE_IMAGE) { if(_sourceType == SourceType::SOURCE_TYPE_IMAGE){
_sourcesEditor->setImageSource(_sourceId); _sourcesEditor->setImageSource(_sourceId);
} else if (_sourceType == SourceType::SOURCE_TYPE_VIDEO) { }else if(_sourceType == SourceType::SOURCE_TYPE_VIDEO){
_sourcesEditor->setVideoSource(_sourceId); _sourcesEditor->setVideoSource(_sourceId);
} else if (_sourceType == SourceType::SOURCE_TYPE_FBO) { }else if(_sourceType == SourceType::SOURCE_TYPE_FBO){
_sourcesEditor->setFboSource(_sourceId); _sourcesEditor->setFboSource(_sourceId);
} else if (_sourceType == SourceType::SOURCE_TYPE_NONE) { }else if(_sourceType == SourceType::SOURCE_TYPE_NONE){
_sourcesEditor->clearSource(); _sourcesEditor->clearSource();
} }
} }
void SetSourceCmd::undo(){ void SetSourceCmd::undo(){
ofLogNotice("SetSourceCmd", "undo"); ofLogNotice("SetSourceCmd", "undo");
if (_oldSourceType == SourceType::SOURCE_TYPE_IMAGE) { if(_oldSourceType == SourceType::SOURCE_TYPE_IMAGE){
_sourcesEditor->setImageSource(_oldSourceId); _sourcesEditor->setImageSource(_oldSourceId);
} else if (_oldSourceType == SourceType::SOURCE_TYPE_VIDEO) { }else if(_oldSourceType == SourceType::SOURCE_TYPE_VIDEO){
_sourcesEditor->setVideoSource(_oldSourceId); _sourcesEditor->setVideoSource(_oldSourceId);
} else if (_oldSourceType == SourceType::SOURCE_TYPE_FBO) { }else if(_oldSourceType == SourceType::SOURCE_TYPE_FBO){
_sourcesEditor->setFboSource(_oldSourceId); _sourcesEditor->setFboSource(_oldSourceId);
} else if (_oldSourceType == SourceType::SOURCE_TYPE_NONE) { }else if(_oldSourceType == SourceType::SOURCE_TYPE_NONE){
_sourcesEditor->clearSource(); _sourcesEditor->clearSource();
} }
_surface = 0; _surface = 0;
_sourcesEditor = 0; _sourcesEditor = 0;
} }
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

51
src/Commands/SetSourceCmd.h

@ -8,33 +8,32 @@
#include "BaseSurface.h" #include "BaseSurface.h"
#include "SourcesEditor.h" #include "SourcesEditor.h"
namespace ofx {
namespace piMapper {
class SourcesEditor;
namespace ofx{ class SetSourceCmd : public BaseUndoCmd {
namespace piMapper{
public:
class SourcesEditor; SetSourceCmd(int sourceType,
string sourceId,
class SetSourceCmd : public BaseUndoCmd{ BaseSurface * surface,
SourcesEditor * sourcesEditor);
public: void exec();
SetSourceCmd(int sourceType, void undo();
string sourceId,
BaseSurface * surface, private:
SourcesEditor * sourcesEditor); int _sourceType;
void exec(); string _sourceId;
void undo(); BaseSurface * _surface;
SourcesEditor * _sourcesEditor;
private:
int _sourceType; int _oldSourceType;
string _sourceId; string _oldSourceId;
BaseSurface * _surface;
SourcesEditor * _sourcesEditor; };
int _oldSourceType; } // namespace piMapper
string _oldSourceId;
};
} // namespace piMapper
} // namespace ofx } // namespace ofx

69
src/MediaServer/DirectoryWatcher.cpp

@ -9,38 +9,39 @@
#include "DirectoryWatcher.h" #include "DirectoryWatcher.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
DirectoryWatcher::DirectoryWatcher(std::string path, int watcherMediaType) {
mediaType = watcherMediaType; DirectoryWatcher::DirectoryWatcher(std::string path, int watcherMediaType){
// Decide what filter we need depending on media type mediaType = watcherMediaType;
if (mediaType == SourceType::SOURCE_TYPE_VIDEO) { // Decide what filter we need depending on media type
filter = new VideoPathFilter(); if(mediaType == SourceType::SOURCE_TYPE_VIDEO){
} else if (mediaType == SourceType::SOURCE_TYPE_IMAGE) { filter = new VideoPathFilter();
filter = new ImagePathFilter(); }else if(mediaType == SourceType::SOURCE_TYPE_IMAGE){
} else { filter = new ImagePathFilter();
ofLogFatalError("DirectoryWatcher::DirectoryWatcher", "Unkonwn media type"); }else{
std::exit(EXIT_FAILURE); ofLogFatalError("DirectoryWatcher::DirectoryWatcher", "Unkonwn media type");
} std::exit(EXIT_FAILURE);
dirWatcher.registerAllEvents(this); }
// For some reason the filters are not working, dirWatcher.registerAllEvents(this);
// we leave just the path here and do the filter logic in the listeners // For some reason the filters are not working,
dirWatcher.addPath(path); // we leave just the path here and do the filter logic in the listeners
// Initial directory listing. Fill the file paths vector. dirWatcher.addPath(path);
IO::DirectoryUtils::list(path, filePaths, true, filter); // Initial directory listing. Fill the file paths vector.
} IO::DirectoryUtils::list(path, filePaths, true, filter);
}
DirectoryWatcher::~DirectoryWatcher() {
delete filter; DirectoryWatcher::~DirectoryWatcher(){
filter = NULL; delete filter;
} filter = NULL;
}
std::vector<std::string>& DirectoryWatcher::getFilePaths() {
return filePaths; std::vector <std::string> & DirectoryWatcher::getFilePaths(){
} return filePaths;
}
int DirectoryWatcher::getMediaType() {
return mediaType; int DirectoryWatcher::getMediaType(){
} return mediaType;
}
} // namespace piMapper
} // namespace piMapper
} // namespace ofx } // namespace ofx

234
src/MediaServer/DirectoryWatcher.h

@ -15,130 +15,126 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class BasePathFilter : public ofx::IO::AbstractPathFilter { class BasePathFilter : public ofx::IO::AbstractPathFilter {
public: public:
BasePathFilter() {}; BasePathFilter(){}
virtual ~BasePathFilter() {}; virtual ~BasePathFilter(){}
virtual bool accept(const Poco::Path& path) const {}; virtual bool accept(const Poco::Path & path) const {}
}; };
class VideoPathFilter : public BasePathFilter { class VideoPathFilter : public BasePathFilter {
public: public:
VideoPathFilter() {}; VideoPathFilter(){}
virtual ~VideoPathFilter() {}; virtual ~VideoPathFilter(){}
bool accept(const Poco::Path& path) const { bool accept(const Poco::Path & path) const {
return !Poco::File(path).isHidden() && return !Poco::File(path).isHidden() &&
(ofIsStringInString(path.toString(), ".mp4") || (ofIsStringInString(path.toString(), ".mp4") ||
ofIsStringInString(path.toString(), ".h264")|| ofIsStringInString(path.toString(), ".h264") ||
ofIsStringInString(path.toString(), ".mov") || ofIsStringInString(path.toString(), ".mov") ||
ofIsStringInString(path.toString(), ".avi") || ofIsStringInString(path.toString(), ".avi") ||
ofIsStringInString(path.toString(), ".mpeg")); ofIsStringInString(path.toString(), ".mpeg"));
} }
}; };
class ImagePathFilter : public BasePathFilter { class ImagePathFilter : public BasePathFilter {
public: public:
ImagePathFilter() {}; ImagePathFilter(){}
virtual ~ImagePathFilter() {}; virtual ~ImagePathFilter(){}
bool accept(const Poco::Path& path) const { bool accept(const Poco::Path & path) const {
return !Poco::File(path).isHidden() && return !Poco::File(path).isHidden() &&
(ofIsStringInString(path.toString(), ".png") || (ofIsStringInString(path.toString(), ".png") ||
ofIsStringInString(path.toString(), ".jpg") || ofIsStringInString(path.toString(), ".jpg") ||
ofIsStringInString(path.toString(), ".jpeg")); ofIsStringInString(path.toString(), ".jpeg"));
} }
}; };
class DirectoryWatcher { class DirectoryWatcher {
public: public:
DirectoryWatcher(std::string path, int watcherMediaType); DirectoryWatcher(std::string path, int watcherMediaType);
~DirectoryWatcher(); ~DirectoryWatcher();
// TODO make useful stuff with onDirectoryWatcher* // TODO make useful stuff with onDirectoryWatcher*
void onDirectoryWatcherItemAdded( void onDirectoryWatcherItemAdded(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){
const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) { string path = evt.item.path();
string path = evt.item.path(); Poco::Path pocoPath = Poco::Path(path);
Poco::Path pocoPath = Poco::Path(path); if(!filter->accept(pocoPath)){
if (!filter->accept(pocoPath)) { return;
return; }
} filePaths.push_back(path);
filePaths.push_back(path); ofNotifyEvent(onItemAdded, path, this);
ofNotifyEvent(onItemAdded, path, this); }
}
void onDirectoryWatcherItemRemoved(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){
void onDirectoryWatcherItemRemoved( string path = evt.item.path();
const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) { Poco::Path pocoPath = Poco::Path(path);
string path = evt.item.path(); if(!filter->accept(pocoPath)){
Poco::Path pocoPath = Poco::Path(path); return;
if (!filter->accept(pocoPath)) { }
return; // Remove path from vector
} for(int i = 0; i < filePaths.size(); i++){
// Remove path from vector if(path == filePaths[i]){
for (int i = 0; i < filePaths.size(); i++) { filePaths.erase(filePaths.begin() + i);
if (path == filePaths[i]) { break;
filePaths.erase(filePaths.begin() + i); }
break; }
} ofNotifyEvent(onItemRemoved, path, this);
} }
ofNotifyEvent(onItemRemoved, path, this);
} void onDirectoryWatcherItemModified(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){
string path = evt.item.path();
void onDirectoryWatcherItemModified( Poco::Path pocoPath = Poco::Path(path);
const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) { if(!filter->accept(pocoPath)){
string path = evt.item.path(); return;
Poco::Path pocoPath = Poco::Path(path); }
if (!filter->accept(pocoPath)) { ofNotifyEvent(onItemModified, path, this);
return; }
}
ofNotifyEvent(onItemModified, path, this); void onDirectoryWatcherItemMovedFrom(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){
} string path = evt.item.path();
Poco::Path pocoPath = Poco::Path(path);
void onDirectoryWatcherItemMovedFrom( if(!filter->accept(pocoPath)){
const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) { return;
string path = evt.item.path(); }
Poco::Path pocoPath = Poco::Path(path); ofLogNotice("ofApp::onDirectoryWatcherItemMovedFrom")
if (!filter->accept(pocoPath)) { << "Moved From: " << path;
return; ofNotifyEvent(onItemMovedFrom, path, this);
} }
ofLogNotice("ofApp::onDirectoryWatcherItemMovedFrom")
<< "Moved From: " << path; void onDirectoryWatcherItemMovedTo(const ofx::IO::DirectoryWatcherManager::DirectoryEvent & evt){
ofNotifyEvent(onItemMovedFrom, path, this); string path = evt.item.path();
} Poco::Path pocoPath = Poco::Path(path);
if(!filter->accept(pocoPath)){
void onDirectoryWatcherItemMovedTo( return;
const ofx::IO::DirectoryWatcherManager::DirectoryEvent& evt) { }
string path = evt.item.path(); ofLogNotice("ofApp::onDirectoryWatcherItemMovedTo")
Poco::Path pocoPath = Poco::Path(path); << "Moved To: " << path;
if (!filter->accept(pocoPath)) { ofNotifyEvent(onItemMovedTo, path, this);
return; }
}
ofLogNotice("ofApp::onDirectoryWatcherItemMovedTo") void onDirectoryWatcherError(const Poco::Exception & exc){
<< "Moved To: " << path; ofLogError("ofApp::onDirectoryWatcherError")
ofNotifyEvent(onItemMovedTo, path, this); << "Error: " << exc.displayText();
} }
void onDirectoryWatcherError(const Poco::Exception& exc) { // Getters
ofLogError("ofApp::onDirectoryWatcherError") std::vector <std::string> & getFilePaths();
<< "Error: " << exc.displayText(); int getMediaType();
}
// Custom events
// Getters ofEvent <string> onItemAdded;
std::vector<std::string>& getFilePaths(); ofEvent <string> onItemRemoved;
int getMediaType(); ofEvent <string> onItemModified;
ofEvent <string> onItemMovedFrom;
// Custom events ofEvent <string> onItemMovedTo;
ofEvent<string> onItemAdded;
ofEvent<string> onItemRemoved; private:
ofEvent<string> onItemModified; ofx::IO::DirectoryWatcherManager dirWatcher;
ofEvent<string> onItemMovedFrom; BasePathFilter * filter;
ofEvent<string> onItemMovedTo; std::vector <std::string> filePaths;
int mediaType;
private:
ofx::IO::DirectoryWatcherManager dirWatcher;
BasePathFilter* filter;
std::vector<std::string> filePaths;
int mediaType;
}; };
}
} } // namespace piMapper
} // namespace ofx

734
src/MediaServer/MediaServer.cpp

@ -11,372 +11,378 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
MediaServer::MediaServer(): MediaServer::MediaServer() :
videoWatcher(ofToDataPath(DEFAULT_VIDEOS_DIR, true), SourceType::SOURCE_TYPE_VIDEO), videoWatcher(ofToDataPath(DEFAULT_VIDEOS_DIR, true), SourceType::SOURCE_TYPE_VIDEO),
imageWatcher(ofToDataPath(DEFAULT_IMAGES_DIR, true), SourceType::SOURCE_TYPE_IMAGE) { imageWatcher(ofToDataPath(DEFAULT_IMAGES_DIR, true), SourceType::SOURCE_TYPE_IMAGE){
addWatcherListeners(); addWatcherListeners();
} }
MediaServer::~MediaServer() { MediaServer::~MediaServer(){
removeWatcherListeners(); removeWatcherListeners();
}; }
int MediaServer::getNumImages() { return imageWatcher.getFilePaths().size(); } int MediaServer::getNumImages(){
int MediaServer::getNumVideos() { return videoWatcher.getFilePaths().size(); } return imageWatcher.getFilePaths().size();
int MediaServer::getNumFboSources() { return fboSources.size(); } }
int MediaServer::getNumVideos(){
return videoWatcher.getFilePaths().size();
}
int MediaServer::getNumFboSources(){
return fboSources.size();
}
std::vector <std::string> & MediaServer::getImagePaths(){
return imageWatcher.getFilePaths();
}
std::vector <std::string> MediaServer::getImageNames(){
std::vector <std::string> imageNames;
for(int i = 0; i < getNumImages(); i++){
// Split image path
std::vector <std::string> pathParts = ofSplitString(getImagePaths()[i], "/");
// And get only the last piece
std::string name = pathParts[pathParts.size() - 1];
imageNames.push_back(name);
}
return imageNames;
}
std::vector <std::string> MediaServer::getFboSourceNames(){
std::vector <std::string> fboSourceNames;
for(int i = 0; i < fboSources.size(); i++){
fboSourceNames.push_back(fboSources[i]->getName());
}
return fboSourceNames;
}
std::vector <std::string> & MediaServer::getVideoPaths(){
return videoWatcher.getFilePaths();
}
std::vector <std::string> MediaServer::getVideoNames(){
std::vector <std::string> videoNames;
for(int i = 0; i < getNumVideos(); i++){
// Split video path
std::vector <std::string> pathParts = ofSplitString(getVideoPaths()[i], "/");
// And get only the last piece
std::string name = pathParts[pathParts.size() - 1];
videoNames.push_back(name);
}
return videoNames;
}
BaseSource * MediaServer::loadMedia(string & path, int mediaType){
// Chose load method depending on type
if(mediaType == SourceType::SOURCE_TYPE_IMAGE){
return loadImage(path);
}else if(mediaType == SourceType::SOURCE_TYPE_VIDEO){
return loadVideo(path);
}else if(mediaType == SourceType::SOURCE_TYPE_FBO){
return loadFboSource(path);
}else{
std::stringstream ss;
ss << "Can not load media of unknown type: " << mediaType;
ofLogFatalError("MediaServer") << ss.str();
std::exit(EXIT_FAILURE);
}
return NULL;
}
BaseSource * MediaServer::loadImage(string & path){
ImageSource * imageSource = NULL;
// Check if this image is already loaded
bool isImageLoaded = false;
if(loadedSources.count(path)){
imageSource = static_cast <ImageSource *>(loadedSources[path]);
isImageLoaded = true;
}
// If image is loaded
if(isImageLoaded){
// Increase reference count of this source
//referenceCount[path]++;
imageSource->referenceCount++;
std::stringstream refss;
refss << "Current reference count for " << path << " = " << imageSource->referenceCount;
ofLogNotice("MediaServer") << refss.str();
// Notify objects registered to onImageLoaded event
std::stringstream ss;
ss << "Image " << path << " already loaded";
ofLogNotice("MediaServer") << ss.str();
ofNotifyEvent(onImageLoaded, path, this);
return imageSource;
}
// Else load fresh
imageSource = new ImageSource();
imageSource->loadImage(path);
loadedSources[path] = imageSource;
// Set reference count of this image path to 1
//referenceCount[path] = 1;
std::stringstream refss;
refss << "Initialized reference count of " << path << " to " << imageSource->referenceCount;
ofLogNotice("MediaServer") << refss.str();
// Notify objects registered to onImageLoaded event
ofNotifyEvent(onImageLoaded, path, this);
return imageSource;
}
void MediaServer::unloadImage(string & path){
ImageSource * source = static_cast <ImageSource *>(getSourceByPath(path));
ofLogNotice("MediaServer") << "Unload image, current reference count: " << source->referenceCount;
source->referenceCount--;
// Unload only if reference count is less or equal to 0
ofLogNotice("MediaServer") << "New reference count: " << source->referenceCount;
if(source->referenceCount > 0){
ofLogNotice("MediaServer") << "Not unloading image as it is being referenced elsewhere";
return;
}
// Reference count 0 or less, unload image
std::stringstream ss;
ss << "Removing image " << path;
ofLogNotice("MediaServer") << ss.str();
// Destroy image source
if(loadedSources.count(path)){
ofLogNotice("MediaServer") << "Source count BEFORE image removal: " << loadedSources.size() << endl;
loadedSources[path]->clear();
std::map <std::string, BaseSource *>::iterator it = loadedSources.find(path);
delete it->second;
loadedSources.erase(it);
ofLogNotice("MediaServer") << "Source count AFTER image removal: " << loadedSources.size() << endl;
ofNotifyEvent(onImageUnloaded, path, this);
return;
}
// Something wrong here, we should be out of the routine by now
std::stringstream failss;
failss << "Failed to remove image source: " << path;
ofLogFatalError("MediaServer") << failss.str();
std::exit(EXIT_FAILURE);
}
BaseSource * MediaServer::loadVideo(string & path){
VideoSource * videoSource = NULL;
// Check if this video is already loaded
bool isVideoLoaded = false;
if(loadedSources.count(path)){
videoSource = static_cast <VideoSource *>(loadedSources[path]);
isVideoLoaded = true;
}
// If is loaded
if(isVideoLoaded){
// Increase reference count of this source
videoSource->referenceCount++;
std::stringstream refss;
refss << "Current reference count for " << path << " = " << videoSource->referenceCount;
ofLogNotice("MediaServer") << refss.str();
// Notify objects registered to onImageLoaded event
std::stringstream ss;
ss << "Video " << path << " already loaded";
ofLogNotice("MediaServer") << ss.str();
ofNotifyEvent(onVideoLoaded, path, this);
return videoSource;
}
// Else load fresh
videoSource = new VideoSource();
videoSource->loadVideo(path);
loadedSources[path] = videoSource;
// Set reference count of this image path to 1
//referenceCount[path] = 1;
std::stringstream refss;
refss << "Initialized reference count of " << path << " to " << videoSource->referenceCount;
ofLogNotice("MediaServer") << refss.str();
ofNotifyEvent(onVideoLoaded, path, this);
return videoSource;
}
void MediaServer::unloadVideo(string & path){
VideoSource * videoSource = static_cast <VideoSource *>(getSourceByPath(path));
// Decrease reference count of the video
//referenceCount[path]--;
videoSource->referenceCount--;
// Unload only if reference count is less or equal to 0
if(videoSource->referenceCount > 0){
ofLogNotice("MediaServer") << "Not unloading video as it is being referenced elsewhere";
return;
}
// Reference count 0 or less, let's unload the video
ofLogNotice("MediaServer") << "Removing video " << path;
// Distroy video source
if(loadedSources.count(path)){
ofLogNotice("MediaServer") << "Source count before video removal: " << loadedSources.size() << endl;
videoSource->clear();
std::map <std::string, BaseSource *>::iterator it = loadedSources.find(path);
delete it->second;
loadedSources.erase(it);
ofLogNotice("MediaServer") << "Source count after video removal: " << loadedSources.size() << endl;
ofNotifyEvent(onVideoUnloaded, path, this);
return;
}
// Something wrong here, we should be out of the routine by now
std::stringstream failss;
failss << "Failed to remove video source: " << path;
ofLogFatalError("MediaServer") << failss.str();
std::exit(EXIT_FAILURE);
}
void MediaServer::unloadMedia(string & path){
if(loadedSources.count(path)){
BaseSource * mediaSource = getSourceByPath(path);
if(mediaSource->getType() == SourceType::SOURCE_TYPE_IMAGE){
unloadImage(path);
}else if(mediaSource->getType() == SourceType::SOURCE_TYPE_VIDEO){
unloadVideo(path);
}else if(mediaSource->getType() == SourceType::SOURCE_TYPE_FBO){
unloadFboSource(path);
}else{
// Oh my god, what to do!? Relax and exit.
ofLogFatalError("MediaServer") << "Attempt to unload media of unknown type";
std::exit(EXIT_FAILURE);
}
}else{
ofLogNotice("MediaServer") << "Nothing to unload";
}
}
// Clear all loaded media
void MediaServer::clear(){
typedef std::map <std::string, BaseSource *>::iterator it_type;
for(it_type i = loadedSources.begin(); i != loadedSources.end(); i++){
// Do not delete FBO source pointers as they are (and should be) initialized elsewhere
if(i->second->getType() != SourceType::SOURCE_TYPE_FBO){
delete i->second;
}
}
loadedSources.clear();
}
// TODO: getLoadedSourceByPath
BaseSource * MediaServer::getSourceByPath(std::string & mediaPath){
if(loadedSources.count(mediaPath)){
return loadedSources[mediaPath];
}
// Source not found, exit with error
std::stringstream ss;
ss << "Could not find source by path: " << mediaPath;
ofLogFatalError("MediaServer") << ss.str();
std::exit(EXIT_FAILURE);
}
std::string MediaServer::getDefaultImageDir(){
return DEFAULT_IMAGES_DIR;
}
std::string MediaServer::getDefaultVideoDir(){
return DEFAULT_VIDEOS_DIR;
}
std::string MediaServer::getDefaultMediaDir(int sourceType){
if(sourceType == SourceType::SOURCE_TYPE_IMAGE){
return getDefaultImageDir();
}else if(sourceType == SourceType::SOURCE_TYPE_VIDEO){
return getDefaultVideoDir();
}else{
std::stringstream ss;
ss << "Could not get default media dir. Unknown source type: " << sourceType;
ofLogFatalError("MediaServer") << ss.str();
std::exit(EXIT_FAILURE);
}
}
void MediaServer::addFboSource(ofx::piMapper::FboSource & fboSource){
ofLogNotice("MediaServer") << "Attempting to add FBO source with name " << fboSource.getName();
// FBO source has to be with unique name
for(int i = 0; i < fboSources.size(); i++){
if(fboSources[i]->getName() == fboSource.getName()){
ofLogWarning("MediaServer") << "Attempt to add FBO source with duplicate name";
ofExit(EXIT_FAILURE); // Here we definitely need to fail to avoid confusion
}
}
ofLogNotice("MediaServer") << "Source new, adding";
fboSources.push_back(&fboSource);
} // addFboSource
BaseSource * MediaServer::loadFboSource(std::string & fboSourceName){
ofLogNotice("MediaServer") << "Attempting to load FBO source with name " << fboSourceName;
// Search for FBO source name in our storage
FboSource * source = NULL;
for(int i = 0; i < fboSources.size(); i++){
if(fboSources[i]->getName() == fboSourceName){
source = fboSources[i];
break;
}
}
// Panic if not in storage
if(source == NULL){
ofLogError("MediaServer") << "Attempt to load non existing FBO source: " << fboSourceName;
ofExit(EXIT_FAILURE);
}
// Check if it is loaded/activated
if(loadedSources.count(fboSourceName)){
// Is loaded, increase reference count and return existing
loadedSources[fboSourceName]->referenceCount++;
ofLogNotice("MediaServer") << "Current " << fboSourceName << "reference count: " << loadedSources[fboSourceName]->referenceCount;
return loadedSources[fboSourceName];
}
// else
// Not loaded, add to loaded sources and activate
// source var should be set by now
source->addAppListeners();
source->referenceCount = 1;
ofLogNotice("MediaServer") << "Current " << fboSourceName << " reference count: " << source->referenceCount;
loadedSources[fboSourceName] = source;
return loadedSources[fboSourceName];
} // loadFboSource
void MediaServer::unloadFboSource(std::string & fboSourceName){
ofLogNotice("MediaServer") << "Attempt to unload FBO source " << fboSourceName;
// Check if loaded at all
if(!loadedSources.count(fboSourceName)){
ofLogWarning("MediaServer") << "FBO source not loaded";
return;
}
// TODO: remove static cast, make the sources handle reference counting,
// enabling and disabling by themselves
FboSource * source = static_cast <FboSource *>(loadedSources[fboSourceName]);
// else decrease reference count
source->referenceCount--;
ofLogNotice("MediaServer") << "Current " << fboSourceName << "reference count: " << loadedSources[fboSourceName]->referenceCount;
// If no references left, disable
if(source->referenceCount <= 0){
ofLogNotice("MediaServer") << fboSourceName << " reference count <= 0, removing from loaded sources";
source->referenceCount = 0;
source->removeAppListeners();
std::map <std::string, BaseSource *>::iterator it = loadedSources.find(fboSourceName);
loadedSources.erase(it);
ofLogNotice("MediaServer") << "Source count after FBO source removal: " << loadedSources.size() << endl;
ofNotifyEvent(onFboSourceUnloaded, fboSourceName, this);
}
} // unloadFboSource
void MediaServer::handleImageAdded(string & path){
ofNotifyEvent(onImageAdded, path, this);
}
void MediaServer::handleImageRemoved(string & path){
ofNotifyEvent(onImageRemoved, path, this);
}
void MediaServer::handleVideoAdded(string & path){
ofNotifyEvent(onVideoAdded, path, this);
}
void MediaServer::handleVideoRemoved(string & path){
ofNotifyEvent(onVideoRemoved, path, this);
}
void MediaServer::addWatcherListeners(){
ofAddListener(imageWatcher.onItemAdded, this, &MediaServer::handleImageAdded);
ofAddListener(imageWatcher.onItemRemoved, this, &MediaServer::handleImageRemoved);
ofAddListener(videoWatcher.onItemAdded, this, &MediaServer::handleVideoAdded);
ofAddListener(videoWatcher.onItemRemoved, this, &MediaServer::handleVideoRemoved);
}
void MediaServer::removeWatcherListeners(){
ofRemoveListener(imageWatcher.onItemAdded, this, &MediaServer::handleImageAdded);
ofRemoveListener(imageWatcher.onItemRemoved, this, &MediaServer::handleImageRemoved);
ofRemoveListener(videoWatcher.onItemAdded, this, &MediaServer::handleVideoAdded);
ofRemoveListener(videoWatcher.onItemRemoved, this, &MediaServer::handleVideoRemoved);
}
std::vector<std::string>& MediaServer::getImagePaths() {
return imageWatcher.getFilePaths();
}
std::vector<std::string> MediaServer::getImageNames() {
std::vector<std::string> imageNames;
for (int i = 0; i < getNumImages(); i++) {
// Split image path
std::vector<std::string> pathParts = ofSplitString(getImagePaths()[i], "/");
// And get only the last piece
std::string name = pathParts[pathParts.size()-1];
imageNames.push_back(name);
}
return imageNames;
}
std::vector<std::string> MediaServer::getFboSourceNames() {
std::vector<std::string> fboSourceNames;
for (int i = 0; i < fboSources.size(); i++) {
fboSourceNames.push_back(fboSources[i]->getName());
}
return fboSourceNames;
}
std::vector<std::string>& MediaServer::getVideoPaths() {
return videoWatcher.getFilePaths();
}
std::vector<std::string> MediaServer::getVideoNames() {
std::vector<std::string> videoNames;
for (int i = 0; i < getNumVideos(); i++) {
// Split video path
std::vector<std::string> pathParts = ofSplitString(getVideoPaths()[i], "/");
// And get only the last piece
std::string name = pathParts[pathParts.size()-1];
videoNames.push_back(name);
}
return videoNames;
}
BaseSource* MediaServer::loadMedia(string &path, int mediaType) {
// Chose load method depending on type
if (mediaType == SourceType::SOURCE_TYPE_IMAGE) {
return loadImage(path);
} else if (mediaType == SourceType::SOURCE_TYPE_VIDEO) {
return loadVideo(path);
} else if (mediaType == SourceType::SOURCE_TYPE_FBO) {
return loadFboSource(path);
} else {
std::stringstream ss;
ss << "Can not load media of unknown type: " << mediaType;
ofLogFatalError("MediaServer") << ss.str();
std::exit(EXIT_FAILURE);
}
return NULL;
}
BaseSource* MediaServer::loadImage(string& path) {
ImageSource* imageSource = NULL;
// Check if this image is already loaded
bool isImageLoaded = false;
if (loadedSources.count(path)) {
imageSource = static_cast<ImageSource*>(loadedSources[path]);
isImageLoaded = true;
}
// If image is loaded
if (isImageLoaded) {
// Increase reference count of this source
//referenceCount[path]++;
imageSource->referenceCount++;
std::stringstream refss;
refss << "Current reference count for " << path << " = " << imageSource->referenceCount;
ofLogNotice("MediaServer") << refss.str();
// Notify objects registered to onImageLoaded event
std::stringstream ss;
ss << "Image " << path << " already loaded";
ofLogNotice("MediaServer") << ss.str();
ofNotifyEvent(onImageLoaded, path, this);
return imageSource;
}
// Else load fresh
imageSource = new ImageSource();
imageSource->loadImage(path);
loadedSources[path] = imageSource;
// Set reference count of this image path to 1
//referenceCount[path] = 1;
std::stringstream refss;
refss << "Initialized reference count of " << path << " to " << imageSource->referenceCount;
ofLogNotice("MediaServer") << refss.str();
// Notify objects registered to onImageLoaded event
ofNotifyEvent(onImageLoaded, path, this);
return imageSource;
}
void MediaServer::unloadImage(string& path) {
ImageSource* source = static_cast<ImageSource*>(getSourceByPath(path));
ofLogNotice("MediaServer") << "Unload image, current reference count: " << source->referenceCount;
source->referenceCount--;
// Unload only if reference count is less or equal to 0
ofLogNotice("MediaServer") << "New reference count: " << source->referenceCount;
if (source->referenceCount > 0) {
ofLogNotice("MediaServer") << "Not unloading image as it is being referenced elsewhere";
return;
}
// Reference count 0 or less, unload image
std::stringstream ss;
ss << "Removing image " << path;
ofLogNotice("MediaServer") << ss.str();
// Destroy image source
if (loadedSources.count(path)) {
ofLogNotice("MediaServer") << "Source count BEFORE image removal: " << loadedSources.size() << endl;
loadedSources[path]->clear();
std::map<std::string, BaseSource*>::iterator it = loadedSources.find(path);
delete it->second;
loadedSources.erase(it);
ofLogNotice("MediaServer") << "Source count AFTER image removal: " << loadedSources.size() << endl;
ofNotifyEvent(onImageUnloaded, path, this);
return;
}
// Something wrong here, we should be out of the routine by now
std::stringstream failss;
failss << "Failed to remove image source: " << path;
ofLogFatalError("MediaServer") << failss.str();
std::exit(EXIT_FAILURE);
}
BaseSource* MediaServer::loadVideo(string& path) {
VideoSource* videoSource = NULL;
// Check if this video is already loaded
bool isVideoLoaded = false;
if (loadedSources.count(path)) {
videoSource = static_cast<VideoSource*>(loadedSources[path]);
isVideoLoaded = true;
}
// If is loaded
if (isVideoLoaded) {
// Increase reference count of this source
videoSource->referenceCount++;
std::stringstream refss;
refss << "Current reference count for " << path << " = " << videoSource->referenceCount;
ofLogNotice("MediaServer") << refss.str();
// Notify objects registered to onImageLoaded event
std::stringstream ss;
ss << "Video " << path << " already loaded";
ofLogNotice("MediaServer") << ss.str();
ofNotifyEvent(onVideoLoaded, path, this);
return videoSource;
}
// Else load fresh
videoSource = new VideoSource();
videoSource->loadVideo(path);
loadedSources[path] = videoSource;
// Set reference count of this image path to 1
//referenceCount[path] = 1;
std::stringstream refss;
refss << "Initialized reference count of " << path << " to " << videoSource->referenceCount;
ofLogNotice("MediaServer") << refss.str();
ofNotifyEvent(onVideoLoaded, path, this);
return videoSource;
}
void MediaServer::unloadVideo(string& path) {
VideoSource* videoSource = static_cast<VideoSource*>(getSourceByPath(path));
// Decrease reference count of the video
//referenceCount[path]--;
videoSource->referenceCount--;
// Unload only if reference count is less or equal to 0
if (videoSource->referenceCount > 0) {
ofLogNotice("MediaServer") << "Not unloading video as it is being referenced elsewhere";
return;
}
// Reference count 0 or less, let's unload the video
ofLogNotice("MediaServer") << "Removing video " << path;
// Distroy video source
if (loadedSources.count(path)) {
ofLogNotice("MediaServer") << "Source count before video removal: " << loadedSources.size() << endl;
videoSource->clear();
std::map<std::string, BaseSource*>::iterator it = loadedSources.find(path);
delete it->second;
loadedSources.erase(it);
ofLogNotice("MediaServer") << "Source count after video removal: " << loadedSources.size() << endl;
ofNotifyEvent(onVideoUnloaded, path, this);
return;
}
// Something wrong here, we should be out of the routine by now
std::stringstream failss;
failss << "Failed to remove video source: " << path;
ofLogFatalError("MediaServer") << failss.str();
std::exit(EXIT_FAILURE);
}
void MediaServer::unloadMedia(string &path) {
if (loadedSources.count(path)) {
BaseSource* mediaSource = getSourceByPath(path);
if (mediaSource->getType() == SourceType::SOURCE_TYPE_IMAGE) {
unloadImage(path);
} else if (mediaSource->getType() == SourceType::SOURCE_TYPE_VIDEO) {
unloadVideo(path);
} else if (mediaSource->getType() == SourceType::SOURCE_TYPE_FBO) {
unloadFboSource(path);
} else {
// Oh my god, what to do!? Relax and exit.
ofLogFatalError("MediaServer") << "Attempt to unload media of unknown type";
std::exit(EXIT_FAILURE);
}
} else {
ofLogNotice("MediaServer") << "Nothing to unload";
}
}
// Clear all loaded media
void MediaServer::clear() {
typedef std::map<std::string, BaseSource*>::iterator it_type;
for (it_type i = loadedSources.begin(); i != loadedSources.end(); i++) {
// Do not delete FBO source pointers as they are (and should be) initialized elsewhere
if (i->second->getType() != SourceType::SOURCE_TYPE_FBO) {
delete i->second;
}
}
loadedSources.clear();
}
// TODO: getLoadedSourceByPath
BaseSource* MediaServer::getSourceByPath(std::string& mediaPath) {
if (loadedSources.count(mediaPath)) {
return loadedSources[mediaPath];
}
// Source not found, exit with error
std::stringstream ss;
ss << "Could not find source by path: " << mediaPath;
ofLogFatalError("MediaServer") << ss.str();
std::exit(EXIT_FAILURE);
}
std::string MediaServer::getDefaultImageDir() {
return DEFAULT_IMAGES_DIR;
}
std::string MediaServer::getDefaultVideoDir() {
return DEFAULT_VIDEOS_DIR;
}
std::string MediaServer::getDefaultMediaDir(int sourceType) {
if (sourceType == SourceType::SOURCE_TYPE_IMAGE) {
return getDefaultImageDir();
} else if (sourceType == SourceType::SOURCE_TYPE_VIDEO) {
return getDefaultVideoDir();
} else {
std::stringstream ss;
ss << "Could not get default media dir. Unknown source type: " << sourceType;
ofLogFatalError("MediaServer") << ss.str();
std::exit(EXIT_FAILURE);
}
}
void MediaServer::addFboSource(ofx::piMapper::FboSource &fboSource) {
ofLogNotice("MediaServer") << "Attempting to add FBO source with name " << fboSource.getName();
// FBO source has to be with unique name
for (int i = 0; i < fboSources.size(); i++) {
if (fboSources[i]->getName() == fboSource.getName()) {
ofLogWarning("MediaServer") << "Attempt to add FBO source with duplicate name";
ofExit(EXIT_FAILURE); // Here we definitely need to fail to avoid confusion
}
}
ofLogNotice("MediaServer") << "Source new, adding";
fboSources.push_back(&fboSource);
} // addFboSource
BaseSource* MediaServer::loadFboSource(std::string &fboSourceName) {
ofLogNotice("MediaServer") << "Attempting to load FBO source with name " << fboSourceName;
// Search for FBO source name in our storage
FboSource* source = NULL;
for (int i = 0; i < fboSources.size(); i++) {
if (fboSources[i]->getName() == fboSourceName) {
source = fboSources[i];
break;
}
}
// Panic if not in storage
if (source == NULL) {
ofLogError("MediaServer") << "Attempt to load non existing FBO source: " << fboSourceName;
ofExit(EXIT_FAILURE);
}
// Check if it is loaded/activated
if (loadedSources.count(fboSourceName)) {
// Is loaded, increase reference count and return existing
loadedSources[fboSourceName]->referenceCount++;
ofLogNotice("MediaServer") << "Current " << fboSourceName << "reference count: " << loadedSources[fboSourceName]->referenceCount;
return loadedSources[fboSourceName];
}
// else
// Not loaded, add to loaded sources and activate
// source var should be set by now
source->addAppListeners();
source->referenceCount = 1;
ofLogNotice("MediaServer") << "Current " << fboSourceName << " reference count: " << source->referenceCount;
loadedSources[fboSourceName] = source;
return loadedSources[fboSourceName];
} // loadFboSource
void MediaServer::unloadFboSource(std::string &fboSourceName) {
ofLogNotice("MediaServer") << "Attempt to unload FBO source " << fboSourceName;
// Check if loaded at all
if (!loadedSources.count(fboSourceName)) {
ofLogWarning("MediaServer") << "FBO source not loaded";
return;
}
// TODO: remove static cast, make the sources handle reference counting,
// enabling and disabling by themselves
FboSource* source = static_cast<FboSource*>(loadedSources[fboSourceName]);
// else decrease reference count
source->referenceCount--;
ofLogNotice("MediaServer") << "Current " << fboSourceName << "reference count: " << loadedSources[fboSourceName]->referenceCount;
// If no references left, disable
if (source->referenceCount <= 0) {
ofLogNotice("MediaServer") << fboSourceName << " reference count <= 0, removing from loaded sources";
source->referenceCount = 0;
source->removeAppListeners();
std::map<std::string, BaseSource*>::iterator it = loadedSources.find(fboSourceName);
loadedSources.erase(it);
ofLogNotice("MediaServer") << "Source count after FBO source removal: " << loadedSources.size() << endl;
ofNotifyEvent(onFboSourceUnloaded, fboSourceName, this);
}
} // unloadFboSource
void MediaServer::handleImageAdded(string& path) {
ofNotifyEvent(onImageAdded, path, this);
}
void MediaServer::handleImageRemoved(string& path) {
ofNotifyEvent(onImageRemoved, path, this);
}
void MediaServer::handleVideoAdded(string& path) {
ofNotifyEvent(onVideoAdded, path, this);
}
void MediaServer::handleVideoRemoved(string& path) {
ofNotifyEvent(onVideoRemoved, path, this);
}
void MediaServer::addWatcherListeners() {
ofAddListener(imageWatcher.onItemAdded, this, &MediaServer::handleImageAdded);
ofAddListener(imageWatcher.onItemRemoved, this, &MediaServer::handleImageRemoved);
ofAddListener(videoWatcher.onItemAdded, this, &MediaServer::handleVideoAdded);
ofAddListener(videoWatcher.onItemRemoved, this, &MediaServer::handleVideoRemoved);
}
void MediaServer::removeWatcherListeners() {
ofRemoveListener(imageWatcher.onItemAdded, this, &MediaServer::handleImageAdded);
ofRemoveListener(imageWatcher.onItemRemoved, this, &MediaServer::handleImageRemoved);
ofRemoveListener(videoWatcher.onItemAdded, this, &MediaServer::handleVideoAdded);
ofRemoveListener(videoWatcher.onItemRemoved, this, &MediaServer::handleVideoRemoved);
}
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

159
src/MediaServer/MediaServer.h

@ -30,85 +30,86 @@ namespace ofx {
namespace piMapper { namespace piMapper {
class MediaServer { class MediaServer {
public: public:
MediaServer(); MediaServer();
virtual ~MediaServer(); virtual ~MediaServer();
int getNumVideos(); int getNumVideos();
int getNumImages(); int getNumImages();
int getNumFboSources(); // new int getNumFboSources(); // new
std::vector<std::string>& getVideoPaths(); std::vector <std::string> & getVideoPaths();
std::vector<std::string> getVideoNames(); std::vector <std::string> getVideoNames();
std::vector<std::string>& getImagePaths(); std::vector <std::string> & getImagePaths();
std::vector<std::string> getImageNames(); std::vector <std::string> getImageNames();
std::vector<std::string> getFboSourceNames(); // new std::vector <std::string> getFboSourceNames(); // new
BaseSource* loadMedia(string& path, int mediaType); BaseSource * loadMedia(string & path, int mediaType);
BaseSource* loadImage(string& path); BaseSource * loadImage(string & path);
void unloadImage(string& path); void unloadImage(string & path);
BaseSource* loadVideo(string& path); BaseSource * loadVideo(string & path);
void unloadVideo(string& path); void unloadVideo(string & path);
void unloadMedia(string& path); void unloadMedia(string & path);
void clear(); // Force all loaded source unload void clear(); // Force all loaded source unload
BaseSource* getSourceByPath(std::string& mediaPath); BaseSource * getSourceByPath(std::string & mediaPath);
std::string getDefaultImageDir(); std::string getDefaultImageDir();
std::string getDefaultVideoDir(); std::string getDefaultVideoDir();
std::string getDefaultMediaDir(int sourceType); std::string getDefaultMediaDir(int sourceType);
// Do things with FBO sources // Do things with FBO sources
void addFboSource(FboSource& fboSource); // could be called also as register FBO source void addFboSource(FboSource & fboSource); // could be called also as register FBO source
BaseSource* loadFboSource(std::string& fboSourceName); BaseSource * loadFboSource(std::string & fboSourceName);
void unloadFboSource(std::string& fboSourceName); void unloadFboSource(std::string & fboSourceName);
// Custom events, add/remove // Custom events, add/remove
ofEvent<std::string> onImageAdded; ofEvent <std::string> onImageAdded;
ofEvent<std::string> onImageRemoved; ofEvent <std::string> onImageRemoved;
ofEvent<std::string> onVideoAdded; ofEvent <std::string> onVideoAdded;
ofEvent<std::string> onVideoRemoved; ofEvent <std::string> onVideoRemoved;
ofEvent<std::string> onFboSourceAdded; ofEvent <std::string> onFboSourceAdded;
ofEvent<std::string> onFboSourceRemoved; ofEvent <std::string> onFboSourceRemoved;
// load/unload // load/unload
ofEvent<std::string> onImageLoaded; ofEvent <std::string> onImageLoaded;
ofEvent<std::string> onImageUnloaded; ofEvent <std::string> onImageUnloaded;
ofEvent<std::string> onVideoLoaded; ofEvent <std::string> onVideoLoaded;
ofEvent<std::string> onVideoUnloaded; ofEvent <std::string> onVideoUnloaded;
ofEvent<std::string> onFboSourceLoaded; ofEvent <std::string> onFboSourceLoaded;
ofEvent<std::string> onFboSourceUnloaded; ofEvent <std::string> onFboSourceUnloaded;
private: private:
// Directory Watchers // Directory Watchers
ofx::piMapper::DirectoryWatcher videoWatcher; ofx::piMapper::DirectoryWatcher videoWatcher;
ofx::piMapper::DirectoryWatcher imageWatcher; ofx::piMapper::DirectoryWatcher imageWatcher;
std::map<std::string, BaseSource*> loadedSources; std::map <std::string, BaseSource *> loadedSources;
// imageWatcher event listeners // imageWatcher event listeners
void handleImageAdded(string& path); void handleImageAdded(string & path);
void handleImageRemoved(string& path); void handleImageRemoved(string & path);
// TODO rest of listeners // TODO rest of listeners
/* /*
void onImageModified(); void onImageModified();
void onImageMovedFrom(); void onImageMovedFrom();
void onImageMovedTo(); void onImageMovedTo();
*/ */
// videoWatcher event listeners // videoWatcher event listeners
void handleVideoAdded(string& path); void handleVideoAdded(string & path);
void handleVideoRemoved(string& path); void handleVideoRemoved(string & path);
// TODO rest of listeners // TODO rest of listeners
/* /*
void onVideoModified(); void onVideoModified();
void onVideoMovedFrom(); void onVideoMovedFrom();
void onVideoMovedTo(); void onVideoMovedTo();
*/ */
// Add/remove event listeners. // Add/remove event listeners.
// Add event listeners to image and video watcher events. // Add event listeners to image and video watcher events.
void addWatcherListeners(); void addWatcherListeners();
// Remove event listeners to image and video watcher events // Remove event listeners to image and video watcher events
void removeWatcherListeners(); void removeWatcherListeners();
// FBO source storage before they go to loadedSources // FBO source storage before they go to loadedSources
std::vector<FboSource*> fboSources; // FBO source storage std::vector <FboSource *> fboSources; // FBO source storage
}; };
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

114
src/Sources/BaseSource.cpp

@ -1,59 +1,61 @@
#include "BaseSource.h" #include "BaseSource.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
BaseSource::BaseSource() {
//cout << "BaseSource" << endl; BaseSource::BaseSource(){
init(); //cout << "BaseSource" << endl;
} init();
}
BaseSource::BaseSource(ofTexture* newTexture) {
init(); BaseSource::BaseSource(ofTexture * newTexture){
texture = newTexture; init();
} texture = newTexture;
}
BaseSource::~BaseSource() {}
BaseSource::~BaseSource(){}
ofTexture* BaseSource::getTexture() {
return texture; ofTexture * BaseSource::getTexture(){
} return texture;
}
std::string& BaseSource::getName() {
return name; std::string & BaseSource::getName(){
} return name;
}
bool BaseSource::isLoadable() {
return loadable; bool BaseSource::isLoadable(){
} return loadable;
}
bool BaseSource::isLoaded() {
return loaded; bool BaseSource::isLoaded(){
} return loaded;
}
int BaseSource::getType() {
return type; int BaseSource::getType(){
} return type;
}
std::string& BaseSource::getPath() {
return path; std::string & BaseSource::getPath(){
} return path;
}
void BaseSource::init() {
texture = NULL; void BaseSource::init(){
name = ""; texture = NULL;
path = ""; name = "";
loadable = false; path = "";
loaded = false; loadable = false;
type = SourceType::SOURCE_TYPE_NONE; loaded = false;
referenceCount = 1; // We have one instance on init type = SourceType::SOURCE_TYPE_NONE;
} referenceCount = 1; // We have one instance on init
}
void BaseSource::setNameFromPath(std::string& fullPath) {
std::vector<string> pathParts; void BaseSource::setNameFromPath(std::string & fullPath){
//cout << "fullPath: " << fullPath << endl; std::vector <string> pathParts;
pathParts = ofSplitString(fullPath, "/"); // Maybe on win "/" is "\", have to test //cout << "fullPath: " << fullPath << endl;
//cout << "lastPathPart: " << pathParts[pathParts.size() - 1] << endl; pathParts = ofSplitString(fullPath, "/"); // Maybe on win "/" is "\", have to test
name = pathParts[pathParts.size() - 1]; //cout << "lastPathPart: " << pathParts[pathParts.size() - 1] << endl;
} name = pathParts[pathParts.size() - 1];
} }
}
} // namespace piMapper
} // namespace ofx

70
src/Sources/BaseSource.h

@ -4,36 +4,40 @@
#include "SourceType.h" #include "SourceType.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
// Use this for adding generative sources to your surfaces
class BaseSource { // Use this for adding generative sources to your surfaces
public: class BaseSource {
BaseSource();
BaseSource(ofTexture* newTexture); // Only one clean way of passing the texture public:
~BaseSource(); BaseSource();
ofTexture* getTexture(); BaseSource(ofTexture * newTexture); // Only one clean way of passing the texture
std::string& getName(); ~BaseSource();
bool isLoadable(); // Maybe the loading features shoud go to a derrived class ofTexture * getTexture();
bool isLoaded(); // as BaseSourceLoadable std::string & getName();
int getType(); bool isLoadable(); // Maybe the loading features shoud go to a derrived class
std::string& getPath(); bool isLoaded(); // as BaseSourceLoadable
virtual void clear() {}; int getType();
std::string & getPath();
// TODO: add virtual increaseReferenceCount and decreaseReferenceCount methods virtual void clear(){}
// and make the variable protected
int referenceCount; // TODO: add virtual increaseReferenceCount and decreaseReferenceCount methods
// and make the variable protected
private: int referenceCount;
void init();
private:
protected: void init();
void setNameFromPath(std::string& fullPath);
ofTexture* texture; protected:
std::string name; void setNameFromPath(std::string & fullPath);
std::string path; // This is set only if loadable is true ofTexture * texture;
bool loadable; // If the source can be loaded from disk like image and video std::string name;
bool loaded; // Is the source loaded? std::string path; // This is set only if loadable is true
int type; bool loadable; // If the source can be loaded from disk like image and video
}; bool loaded; // Is the source loaded?
} int type;
}
};
} // namespace piMapper
} // namespace ofx

222
src/Sources/FboSource.cpp

@ -1,115 +1,115 @@
#include "FboSource.h" #include "FboSource.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
FboSource::FboSource() : fbo(NULL) { FboSource::FboSource() : fbo(NULL){
name = PIMAPPER_FBO_SOURCE_DEF_NAME; name = PIMAPPER_FBO_SOURCE_DEF_NAME;
loadable = false; loadable = false;
loaded = false; loaded = false;
type = SourceType::SOURCE_TYPE_FBO; type = SourceType::SOURCE_TYPE_FBO;
ofAddListener(ofEvents().setup, this, ofAddListener(ofEvents().setup, this,
&FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP); &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP);
} }
FboSource::~FboSource() { FboSource::~FboSource(){
removeAppListeners(); removeAppListeners();
clear(); clear();
} }
void FboSource::addAppListeners() { void FboSource::addAppListeners(){
ofLogNotice("FboSource") << "Adding app listeners"; ofLogNotice("FboSource") << "Adding app listeners";
ofAddListener(ofEvents().update, this, ofAddListener(ofEvents().update, this,
&FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP); &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP);
ofAddListener(ofEvents().draw, this, ofAddListener(ofEvents().draw, this,
&FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP); &FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP);
ofAddListener(ofEvents().exit, this, ofAddListener(ofEvents().exit, this,
&FboSource::onAppExit, OF_EVENT_ORDER_AFTER_APP); &FboSource::onAppExit, OF_EVENT_ORDER_AFTER_APP);
} }
void FboSource::removeAppListeners() { void FboSource::removeAppListeners(){
ofLogNotice("FboSource") << "Removing app listeners"; ofLogNotice("FboSource") << "Removing app listeners";
ofRemoveListener(ofEvents().update, this, ofRemoveListener(ofEvents().update, this,
&FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP); &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP);
ofRemoveListener(ofEvents().draw, this, ofRemoveListener(ofEvents().draw, this,
&FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP); &FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP);
ofRemoveListener(ofEvents().exit, this, ofRemoveListener(ofEvents().exit, this,
&FboSource::onAppExit, OF_EVENT_ORDER_AFTER_APP); &FboSource::onAppExit, OF_EVENT_ORDER_AFTER_APP);
} }
void FboSource::onAppSetup(ofEventArgs &args) { void FboSource::onAppSetup(ofEventArgs & args){
ofRemoveListener(ofEvents().setup, this, ofRemoveListener(ofEvents().setup, this,
&FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP); &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP);
setup(); setup();
// Check if FBO was allocated in user defined setup // Check if FBO was allocated in user defined setup
// If not, show warning and alocate to avoid panic // If not, show warning and alocate to avoid panic
if (!fbo->isAllocated()) { if(!fbo->isAllocated()){
ofLogWarning("FboSource::onAppSetup") << ofLogWarning("FboSource::onAppSetup")
"FBO not allocated, allocating with default values"; << "FBO not allocated, allocating with default values";
allocate(PIMAPPER_FBO_SOURCE_DEF_WIDTH, allocate(PIMAPPER_FBO_SOURCE_DEF_WIDTH,
PIMAPPER_FBO_SOURCE_DEF_HEIGHT); PIMAPPER_FBO_SOURCE_DEF_HEIGHT);
} }
} }
void FboSource::onAppUpdate(ofEventArgs &args) { void FboSource::onAppUpdate(ofEventArgs & args){
if (fbo == NULL || !fbo->isAllocated()) { if(fbo == NULL || !fbo->isAllocated()){
ofLogWarning("FboSource") << "FBO not allocated"; ofLogWarning("FboSource") << "FBO not allocated";
return; return;
} }
update(); update();
} }
void FboSource::onAppDraw(ofEventArgs &args) { void FboSource::onAppDraw(ofEventArgs & args){
if (fbo == NULL || !fbo->isAllocated()) { if(fbo == NULL || !fbo->isAllocated()){
ofLogWarning("FboSource") << "FBO not allocated"; ofLogWarning("FboSource") << "FBO not allocated";
return; return;
} }
fbo->begin(); fbo->begin();
draw(); draw();
fbo->end(); fbo->end();
} }
void FboSource::onAppExit(ofEventArgs &args) { void FboSource::onAppExit(ofEventArgs & args){
exit(); exit();
} }
void FboSource::allocate(int width, int height) { void FboSource::allocate(int width, int height){
clear(); clear();
fbo = new ofFbo(); fbo = new ofFbo();
fbo->allocate(width, height); fbo->allocate(width, height);
// Clear FBO // Clear FBO
fbo->begin(); fbo->begin();
ofClear(0); ofClear(0);
fbo->end(); fbo->end();
texture = &(fbo->getTextureReference()); texture = &(fbo->getTextureReference());
} }
void FboSource::clear() { void FboSource::clear(){
texture = NULL; texture = NULL;
if (fbo != NULL) { if(fbo != NULL){
delete fbo; delete fbo;
fbo = NULL; fbo = NULL;
} }
} }
int FboSource::getWidth() { int FboSource::getWidth(){
if (fbo->isAllocated()) { if(fbo->isAllocated()){
return fbo->getWidth(); return fbo->getWidth();
} else { }else{
return 0; return 0;
} }
} }
int FboSource::getHeight() { int FboSource::getHeight(){
if (fbo->isAllocated()) { if(fbo->isAllocated()){
return fbo->getHeight(); return fbo->getHeight();
} else { }else{
return 0; return 0;
} }
} }
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

70
src/Sources/FboSource.h

@ -8,38 +8,40 @@
#define PIMAPPER_FBO_SOURCE_DEF_HEIGHT 500 #define PIMAPPER_FBO_SOURCE_DEF_HEIGHT 500
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class FboSource : public BaseSource { class FboSource : public BaseSource {
public:
FboSource(); public:
~FboSource(); FboSource();
~FboSource();
// Override these in your custom FBO source
virtual void setup() {}; // Override these in your custom FBO source
virtual void update() {}; virtual void setup(){}
virtual void draw() {}; virtual void update(){}
virtual void exit() {}; virtual void draw(){}
virtual void exit(){}
// The only method from BaseSource to be overriden
void clear(); // The only method from BaseSource to be overriden
void clear();
// App listeners
void addAppListeners(); // App listeners
void removeAppListeners(); void addAppListeners();
void onAppSetup(ofEventArgs & args); void removeAppListeners();
void onAppUpdate(ofEventArgs & args); void onAppSetup(ofEventArgs & args);
void onAppDraw(ofEventArgs & args); void onAppUpdate(ofEventArgs & args);
void onAppExit(ofEventArgs & args); void onAppDraw(ofEventArgs & args);
void onAppExit(ofEventArgs & args);
protected:
ofFbo * fbo; protected:
void allocate(int width, int height); ofFbo * fbo;
void allocate(int width, int height);
// Some handy getters
int getWidth(); // Some handy getters
int getHeight(); int getWidth();
}; int getHeight();
} // namespace piMapper };
} // namespace piMapper
} // namespace ofx } // namespace ofx

73
src/Sources/ImageSource.cpp

@ -1,39 +1,40 @@
#include "ImageSource.h" #include "ImageSource.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
ImageSource::ImageSource() {
//cout << "ImageSource" << endl; ImageSource::ImageSource(){
loadable = true; //cout << "ImageSource" << endl;
loaded = false; loadable = true;
type = SourceType::SOURCE_TYPE_IMAGE; loaded = false;
} type = SourceType::SOURCE_TYPE_IMAGE;
}
ImageSource::~ImageSource() {}
ImageSource::~ImageSource(){}
void ImageSource::loadImage(std::string& filePath) {
path = filePath; void ImageSource::loadImage(std::string & filePath){
//cout << "loading image: " << filePath << endl; path = filePath;
setNameFromPath(filePath); //cout << "loading image: " << filePath << endl;
//cout << "path: " << path << endl; setNameFromPath(filePath);
image = new ofImage(); //cout << "path: " << path << endl;
if (!image->loadImage(filePath)) { image = new ofImage();
ofLogWarning("ImageSource") << "Could not load image"; if(!image->loadImage(filePath)){
//std::exit(EXIT_FAILURE); ofLogWarning("ImageSource") << "Could not load image";
} //std::exit(EXIT_FAILURE);
texture = &image->getTextureReference(); }
loaded = true; texture = &image->getTextureReference();
} loaded = true;
}
void ImageSource::clear() {
texture = NULL; void ImageSource::clear(){
image->clear(); texture = NULL;
delete image; image->clear();
image = NULL; delete image;
//path = ""; image = NULL;
//name = ""; //path = "";
loaded = false; //name = "";
} loaded = false;
}
}
} } // namespace piMapper
} // namespace ofx

30
src/Sources/ImageSource.h

@ -3,16 +3,20 @@
#include "BaseSource.h" #include "BaseSource.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class ImageSource : public BaseSource {
public: class ImageSource : public BaseSource {
ImageSource();
~ImageSource(); public:
std::string& getPath(); ImageSource();
void loadImage(std::string& filePath); ~ImageSource();
void clear(); std::string & getPath();
private: void loadImage(std::string & filePath);
ofImage* image; void clear();
}; private:
} ofImage * image;
}
};
} // namespace piMapper
} // namespace ofx

88
src/Sources/SourceType.h

@ -8,44 +8,50 @@
#define SOURCE_TYPE_NAME_FBO "fbo" #define SOURCE_TYPE_NAME_FBO "fbo"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class SourceType {
public: class SourceType {
enum { SOURCE_TYPE_NONE, SOURCE_TYPE_IMAGE, SOURCE_TYPE_VIDEO, SOURCE_TYPE_FBO };
public:
static std::string GetSourceTypeName(int sourceTypeEnum) { enum {
if (sourceTypeEnum == SOURCE_TYPE_IMAGE) { SOURCE_TYPE_NONE, SOURCE_TYPE_IMAGE, SOURCE_TYPE_VIDEO, SOURCE_TYPE_FBO
return SOURCE_TYPE_NAME_IMAGE; };
} else if (sourceTypeEnum == SOURCE_TYPE_VIDEO) {
return SOURCE_TYPE_NAME_VIDEO; static std::string GetSourceTypeName(int sourceTypeEnum){
} else if (sourceTypeEnum == SOURCE_TYPE_NONE) { if(sourceTypeEnum == SOURCE_TYPE_IMAGE){
return SOURCE_TYPE_NAME_NONE; return SOURCE_TYPE_NAME_IMAGE;
} else if (sourceTypeEnum == SOURCE_TYPE_FBO) { }else if(sourceTypeEnum == SOURCE_TYPE_VIDEO){
return SOURCE_TYPE_NAME_FBO; return SOURCE_TYPE_NAME_VIDEO;
} else { }else if(sourceTypeEnum == SOURCE_TYPE_NONE){
std::stringstream ss; return SOURCE_TYPE_NAME_NONE;
ss << "Invalid source type: " << sourceTypeEnum; }else if(sourceTypeEnum == SOURCE_TYPE_FBO){
ofLogFatalError("SourceType") << ss.str(); return SOURCE_TYPE_NAME_FBO;
std::exit(EXIT_FAILURE); }else{
} std::stringstream ss;
}; ss << "Invalid source type: " << sourceTypeEnum;
ofLogFatalError("SourceType") << ss.str();
static int GetSourceTypeEnum(std::string sourceTypeName) { std::exit(EXIT_FAILURE);
if (sourceTypeName == SOURCE_TYPE_NAME_IMAGE) { }
return SOURCE_TYPE_IMAGE; }
} else if (sourceTypeName == SOURCE_TYPE_NAME_VIDEO) {
return SOURCE_TYPE_VIDEO; static int GetSourceTypeEnum(std::string sourceTypeName){
} else if (sourceTypeName == SOURCE_TYPE_NAME_NONE) { if(sourceTypeName == SOURCE_TYPE_NAME_IMAGE){
return SOURCE_TYPE_NONE; return SOURCE_TYPE_IMAGE;
} else if (sourceTypeName == SOURCE_TYPE_NAME_FBO) { }else if(sourceTypeName == SOURCE_TYPE_NAME_VIDEO){
return SOURCE_TYPE_FBO; return SOURCE_TYPE_VIDEO;
} else { }else if(sourceTypeName == SOURCE_TYPE_NAME_NONE){
std::stringstream ss; return SOURCE_TYPE_NONE;
ss << "Invalid source type name: " << sourceTypeName; }else if(sourceTypeName == SOURCE_TYPE_NAME_FBO){
ofLogFatalError("SourceType") << ss.str(); return SOURCE_TYPE_FBO;
std::exit(EXIT_FAILURE); }else{
} std::stringstream ss;
} ss << "Invalid source type name: " << sourceTypeName;
}; ofLogFatalError("SourceType") << ss.str();
} std::exit(EXIT_FAILURE);
} }
}
};
} // namespace piMapper
} // namespace ofx

129
src/Sources/VideoSource.cpp

@ -1,70 +1,71 @@
#include "VideoSource.h" #include "VideoSource.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
bool VideoSource::enableAudio = false; bool VideoSource::enableAudio = false;
VideoSource::VideoSource() { VideoSource::VideoSource(){
loadable = true; loadable = true;
loaded = false; loaded = false;
type = SourceType::SOURCE_TYPE_VIDEO; type = SourceType::SOURCE_TYPE_VIDEO;
#ifdef TARGET_RASPBERRY_PI #ifdef TARGET_RASPBERRY_PI
omxPlayer = NULL; omxPlayer = NULL;
#else #else
videoPlayer = NULL; videoPlayer = NULL;
#endif #endif
} }
VideoSource::~VideoSource() {} VideoSource::~VideoSource(){}
void VideoSource::loadVideo(std::string & filePath) { void VideoSource::loadVideo(std::string & filePath){
path = filePath; path = filePath;
setNameFromPath(filePath); setNameFromPath(filePath);
#ifdef TARGET_RASPBERRY_PI #ifdef TARGET_RASPBERRY_PI
ofxOMXPlayerSettings settings; ofxOMXPlayerSettings settings;
settings.videoPath = filePath; settings.videoPath = filePath;
settings.useHDMIForAudio = true; settings.useHDMIForAudio = true;
settings.enableTexture = true; settings.enableTexture = true;
settings.enableLooping = true; settings.enableLooping = true;
settings.enableAudio = VideoSource::enableAudio; settings.enableAudio = VideoSource::enableAudio;
omxPlayer = new ofxOMXPlayer(); omxPlayer = new ofxOMXPlayer();
omxPlayer->setup(settings); omxPlayer->setup(settings);
texture = &(omxPlayer->getTextureReference()); texture = &(omxPlayer->getTextureReference());
#else #else
videoPlayer = new ofVideoPlayer(); videoPlayer = new ofVideoPlayer();
videoPlayer->load(filePath); videoPlayer->load(filePath);
videoPlayer->setLoopState(OF_LOOP_NORMAL); videoPlayer->setLoopState(OF_LOOP_NORMAL);
videoPlayer->setVolume(VideoSource::enableAudio ? 1.0f : 0.0f); videoPlayer->setVolume(VideoSource::enableAudio ? 1.0f : 0.0f);
videoPlayer->play(); videoPlayer->play();
texture = &(videoPlayer->getTexture()); texture = &(videoPlayer->getTexture());
ofAddListener(ofEvents().update, this, &VideoSource::update); ofAddListener(ofEvents().update, this, &VideoSource::update);
#endif #endif
loaded = true; loaded = true;
} }
void VideoSource::clear() { void VideoSource::clear(){
texture = NULL; texture = NULL;
#ifdef TARGET_RASPBERRY_PI #ifdef TARGET_RASPBERRY_PI
omxPlayer->close(); omxPlayer->close();
delete omxPlayer; delete omxPlayer;
omxPlayer = NULL; omxPlayer = NULL;
#else #else
ofRemoveListener(ofEvents().update, this, &VideoSource::update); ofRemoveListener(ofEvents().update, this, &VideoSource::update);
videoPlayer->stop(); videoPlayer->stop();
videoPlayer->close(); videoPlayer->close();
delete videoPlayer; delete videoPlayer;
videoPlayer = NULL; videoPlayer = NULL;
#endif #endif
loaded = false; loaded = false;
} }
#ifndef TARGET_RASPBERRY_PI #ifndef TARGET_RASPBERRY_PI
void VideoSource::update(ofEventArgs & args) { void VideoSource::update(ofEventArgs & args){
if (videoPlayer != NULL) { if(videoPlayer != NULL){
videoPlayer->update(); videoPlayer->update();
} }
} }
#endif #endif
} // namespace piMapper
} // namespace piMapper
} // namespace ofx } // namespace ofx

67
src/Sources/VideoSource.h

@ -4,39 +4,42 @@
#include "BaseSource.h" #include "BaseSource.h"
#ifdef TARGET_RASPBERRY_PI #ifdef TARGET_RASPBERRY_PI
#include "ofxOMXPlayer.h" #include "ofxOMXPlayer.h"
#endif #endif
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class VideoSource : public BaseSource {
public:
// TODO: Create enableAudio() and disableAudio() methods
// for live audio enabling and disabling.
static bool enableAudio;
VideoSource();
~VideoSource();
std::string & getPath();
void loadVideo(std::string & path);
void clear();
#ifndef TARGET_RASPBERRY_PI
void update(ofEventArgs & args);
#endif
private:
#ifdef TARGET_RASPBERRY_PI class VideoSource : public BaseSource {
ofxOMXPlayer * omxPlayer; // Naming different for less confusion
#else public:
// Go with ofVideoPlayer or
// TODO: High Performance Video player on newer Macs // TODO: Create enableAudio() and disableAudio() methods
ofVideoPlayer * videoPlayer; // for live audio enabling and disabling.
#endif static bool enableAudio;
}; VideoSource();
} ~VideoSource();
}
std::string & getPath();
void loadVideo(std::string & path);
void clear();
#ifndef TARGET_RASPBERRY_PI
void update(ofEventArgs & args);
#endif
private:
#ifdef TARGET_RASPBERRY_PI
ofxOMXPlayer * omxPlayer; // Naming different for less confusion
#else
// Go with ofVideoPlayer or
// TODO: High Performance Video player on newer Macs
ofVideoPlayer * videoPlayer;
#endif
};
} // namespace piMapper
} // namespace ofx

188
src/Surfaces/BaseSurface.cpp

@ -1,94 +1,100 @@
#include "BaseSurface.h" #include "BaseSurface.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
BaseSurface::BaseSurface() { BaseSurface::BaseSurface(){
_moved = false; _moved = false;
ofEnableNormalizedTexCoords(); ofEnableNormalizedTexCoords();
createDefaultTexture(); createDefaultTexture();
} }
BaseSurface::~BaseSurface() { BaseSurface::~BaseSurface(){
delete defaultSource; delete defaultSource;
defaultSource = NULL; defaultSource = NULL;
defaultTexture.clear(); defaultTexture.clear();
} }
void BaseSurface::createDefaultTexture() { void BaseSurface::createDefaultTexture(){
ofPixels pixels; ofPixels pixels;
pixels.allocate(500, 500, 1); pixels.allocate(500, 500, 1);
for (int i = 0; i < pixels.size(); i++) { for(int i = 0; i < pixels.size(); i++){
pixels[i] = 255; pixels[i] = 255;
} }
int squareSize = 10; // size of each test pattern square int squareSize = 10; // size of each test pattern square
bool sy = false; bool sy = false;
for (int y = 0; y < pixels.getWidth(); y += squareSize) { for(int y = 0; y < pixels.getWidth(); y += squareSize){
bool sx = false; bool sx = false;
for (int x = 0; x < pixels.getHeight(); x += squareSize) { for(int x = 0; x < pixels.getHeight(); x += squareSize){
for (int yi = 0; yi < squareSize; yi++) { for(int yi = 0; yi < squareSize; yi++){
for (int xi = 0; xi < squareSize; xi++) { for(int xi = 0; xi < squareSize; xi++){
if (sx && sy) if(sx && sy){
pixels[(y + yi) * pixels.getWidth() + x + xi] = 255; pixels[(y + yi) * pixels.getWidth() + x + xi] = 255;
else if (sx && !sy) }else if(sx && !sy){
pixels[(y + yi) * pixels.getWidth() + x + xi] = 0; pixels[(y + yi) * pixels.getWidth() + x + xi] = 0;
else if (!sx && sy) }else if(!sx && sy){
pixels[(y + yi) * pixels.getWidth() + x + xi] = 0; pixels[(y + yi) * pixels.getWidth() + x + xi] = 0;
else }else{
pixels[(y + yi) * pixels.getWidth() + x + xi] = 255; pixels[(y + yi) * pixels.getWidth() + x + xi] = 255;
} }
} }
sx = !sx; }
} sx = !sx;
sy = !sy; }
} sy = !sy;
}
// load pixels into texture
defaultTexture.loadData(pixels); // load pixels into texture
// Create new source to be the default defaultTexture.loadData(pixels);
defaultSource = new BaseSource(&defaultTexture); // Create new source to be the default
source = defaultSource; defaultSource = new BaseSource(&defaultTexture);
} source = defaultSource;
}
void BaseSurface::drawTexture(ofVec2f position) {
if (source->getTexture() == NULL) { void BaseSurface::drawTexture(ofVec2f position){
ofLogWarning("BaseSurface") << "Source texture empty. Not drawing."; if(source->getTexture() == NULL){
return; ofLogWarning("BaseSurface") << "Source texture empty. Not drawing.";
} return;
}
ofMesh texMesh;
texMesh.addVertex(position); ofMesh texMesh;
texMesh.addVertex(position + ofVec2f(source->getTexture()->getWidth(), 0.0f)); texMesh.addVertex(position);
texMesh.addVertex(position + texMesh.addVertex(position + ofVec2f(source->getTexture()->getWidth(), 0.0f));
ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight())); texMesh.addVertex(position
texMesh.addVertex(position + ofVec2f(0.0f, source->getTexture()->getHeight())); + ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight()));
texMesh.addTriangle(0, 2, 3); texMesh.addVertex(position + ofVec2f(0.0f, source->getTexture()->getHeight()));
texMesh.addTriangle(0, 1, 2); texMesh.addTriangle(0, 2, 3);
texMesh.addTexCoord(ofVec2f(0.0f, 0.0f)); texMesh.addTriangle(0, 1, 2);
texMesh.addTexCoord(ofVec2f(1.0f, 0.0f)); texMesh.addTexCoord(ofVec2f(0.0f, 0.0f));
texMesh.addTexCoord(ofVec2f(1.0f, 1.0f)); texMesh.addTexCoord(ofVec2f(1.0f, 0.0f));
texMesh.addTexCoord(ofVec2f(0.0f, 1.0f)); texMesh.addTexCoord(ofVec2f(1.0f, 1.0f));
source->getTexture()->bind(); texMesh.addTexCoord(ofVec2f(0.0f, 1.0f));
texMesh.draw(); source->getTexture()->bind();
source->getTexture()->unbind(); texMesh.draw();
} source->getTexture()->unbind();
}
//void BaseSurface::setTexture(ofTexture* texturePtr) { texture = texturePtr; }
void BaseSurface::setSource(BaseSource* newSource) { //void BaseSurface::setTexture(ofTexture* texturePtr) { texture = texturePtr; }
source = newSource; void BaseSurface::setSource(BaseSource * newSource){
} source = newSource;
}
//ofTexture* BaseSurface::getTexture() { return texture; }
BaseSource* BaseSurface::getSource() { //ofTexture* BaseSurface::getTexture() { return texture; }
return source; BaseSource * BaseSurface::getSource(){
} return source;
}
//ofTexture* BaseSurface::getDefaultTexture() { return &defaultTexture; }
BaseSource* BaseSurface::getDefaultSource() { //ofTexture* BaseSurface::getDefaultTexture() { return &defaultTexture; }
return defaultSource; BaseSource * BaseSurface::getDefaultSource(){
} return defaultSource;
}
void BaseSurface::setMoved(bool moved){ _moved = moved; }
bool BaseSurface::getMoved(){ return _moved; } void BaseSurface::setMoved(bool moved){
} _moved = moved;
} }
bool BaseSurface::getMoved(){
return _moved;
}
} // namespace piMapper
} // namespace ofx

74
src/Surfaces/BaseSurface.h

@ -7,38 +7,42 @@
using namespace std; using namespace std;
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class BaseSurface {
public: class BaseSurface {
BaseSurface();
~BaseSurface(); public:
virtual void setup() {}; BaseSurface();
virtual void draw() {}; ~BaseSurface();
virtual void setVertex(int index, ofVec2f p) {}; virtual void setup(){}
virtual void setTexCoord(int index, ofVec2f t) {}; virtual void draw(){}
virtual void moveBy(ofVec2f v) {}; virtual void setVertex(int index, ofVec2f p){}
virtual int getType() {}; virtual void setTexCoord(int index, ofVec2f t){}
virtual bool hitTest(ofVec2f p) {}; virtual void moveBy(ofVec2f v){}
virtual ofPolyline getHitArea() {}; virtual int getType(){}
virtual ofPolyline getTextureHitArea() {}; virtual bool hitTest(ofVec2f p){}
virtual vector<ofVec3f>& getVertices() {}; virtual ofPolyline getHitArea(){}
virtual vector<ofVec2f>& getTexCoords() {}; virtual ofPolyline getTextureHitArea(){}
virtual vector <ofVec3f> & getVertices(){}
void drawTexture(ofVec2f position); virtual vector <ofVec2f> & getTexCoords(){}
void setSource(BaseSource* newSource);
BaseSource* getSource(); void drawTexture(ofVec2f position);
BaseSource* getDefaultSource(); void setSource(BaseSource * newSource);
BaseSource * getSource();
void setMoved(bool moved); BaseSource * getDefaultSource();
bool getMoved();
void setMoved(bool moved);
protected: bool getMoved();
ofMesh mesh;
ofTexture defaultTexture; protected:
BaseSource* source; ofMesh mesh;
BaseSource* defaultSource; ofTexture defaultTexture;
void createDefaultTexture(); BaseSource * source;
bool _moved; BaseSource * defaultSource;
}; void createDefaultTexture();
} bool _moved;
}
};
} // namespace piMapper
} // namespace ofx

492
src/Surfaces/QuadSurface.cpp

@ -2,253 +2,261 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
QuadSurface::QuadSurface() {
setup(); QuadSurface::QuadSurface(){
setup();
} }
QuadSurface::~QuadSurface() { cout << "QuadSurface destructor." << endl; } QuadSurface::~QuadSurface(){
cout << "QuadSurface destructor." << endl;
}
void QuadSurface::setup() { void QuadSurface::setup(){
// Create 4 points for the 2 triangles // Create 4 points for the 2 triangles
ofVec2f p1 = ofVec2f(0, 0); ofVec2f p1 = ofVec2f(0, 0);
ofVec2f p2 = ofVec2f(0, ofGetHeight()); ofVec2f p2 = ofVec2f(0, ofGetHeight());
ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight()); ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight());
ofVec2f p4 = ofVec2f(ofGetWidth(), 0); ofVec2f p4 = ofVec2f(ofGetWidth(), 0);
// Create 4 point for the texture coordinates // Create 4 point for the texture coordinates
ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 0.0f)); ofVec2f t1 = ofVec2f(ofVec2f(0.0f, 0.0f));
ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 0.0f)); ofVec2f t2 = ofVec2f(ofVec2f(1.0f, 0.0f));
ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 1.0f)); ofVec2f t3 = ofVec2f(ofVec2f(1.0f, 1.0f));
ofVec2f t4 = ofVec2f(ofVec2f(0.0f, 1.0f)); ofVec2f t4 = ofVec2f(ofVec2f(0.0f, 1.0f));
setup(p1, p2, p3, p4, t1, t2, t3, t4, source); setup(p1, p2, p3, p4, t1, t2, t3, t4, source);
} }
void QuadSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, void QuadSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4,
ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofVec2f t4,
BaseSource* newSource) { BaseSource * newSource){
// Assign texture // Assign texture
source = newSource; source = newSource;
// Clear mesh // Clear mesh
mesh.clear(); mesh.clear();
// Create a surface with the points // Create a surface with the points
mesh.addVertex(p1); mesh.addVertex(p1);
mesh.addVertex(p2); mesh.addVertex(p2);
mesh.addVertex(p3); mesh.addVertex(p3);
mesh.addVertex(p4); mesh.addVertex(p4);
// Add 2 triangles // Add 2 triangles
mesh.addTriangle(0, 2, 3); mesh.addTriangle(0, 2, 3);
mesh.addTriangle(0, 1, 2); mesh.addTriangle(0, 1, 2);
// Add texture coordinates // Add texture coordinates
mesh.addTexCoord(t1); mesh.addTexCoord(t1);
mesh.addTexCoord(t2); mesh.addTexCoord(t2);
mesh.addTexCoord(t3); mesh.addTexCoord(t3);
mesh.addTexCoord(t4); mesh.addTexCoord(t4);
// Pure GL setup // Pure GL setup
// indices // indices
quadIndices[0] = 0; quadIndices[0] = 0;
quadIndices[1] = 1; quadIndices[1] = 1;
quadIndices[2] = 2; quadIndices[2] = 2;
quadIndices[3] = 0; quadIndices[3] = 0;
quadIndices[4] = 2; quadIndices[4] = 2;
quadIndices[5] = 3; quadIndices[5] = 3;
// tex coords (those are alway 0) // tex coords (those are alway 0)
quadTexCoordinates[2] = 0; quadTexCoordinates[2] = 0;
quadTexCoordinates[6] = 0; quadTexCoordinates[6] = 0;
quadTexCoordinates[10] = 0; quadTexCoordinates[10] = 0;
quadTexCoordinates[14] = 0; quadTexCoordinates[14] = 0;
calculate4dTextureCoords(); calculate4dTextureCoords();
} }
void QuadSurface::draw() { void QuadSurface::draw(){
if (source->getTexture() == NULL) { if(source->getTexture() == NULL){
ofLogWarning("QuadSurface") << "Source texture empty. Not drawing."; ofLogWarning("QuadSurface") << "Source texture empty. Not drawing.";
return; return;
} }
/*if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){ /*if(mesh.haveVertsChanged() || mesh.haveTexCoordsChanged()){
calculate4dTextureCoords(); calculate4dTextureCoords();
}*/ }*/
/* /*
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(4, GL_FLOAT, 0, quadTexCoordinates); glTexCoordPointer(4, GL_FLOAT, 0, quadTexCoordinates);
glVertexPointer(3, GL_FLOAT, 0, quadVertices); glVertexPointer(3, GL_FLOAT, 0, quadVertices);
source->getTexture()->bind(); source->getTexture()->bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, quadIndices); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, quadIndices);
source->getTexture()->unbind(); source->getTexture()->unbind();
*/ */
source->getTexture()->bind(); source->getTexture()->bind();
mesh.draw(); mesh.draw();
source->getTexture()->unbind(); source->getTexture()->unbind();
} }
void QuadSurface::setVertex(int index, ofVec2f p) { void QuadSurface::setVertex(int index, ofVec2f p){
if (index > 3) { if(index > 3){
ofLog() << "Vertex with this index does not exist: " << index << endl; ofLog() << "Vertex with this index does not exist: " << index << endl;
return; return;
} }
mesh.setVertex(index, p); mesh.setVertex(index, p);
calculate4dTextureCoords(); calculate4dTextureCoords();
} }
void QuadSurface::setTexCoord(int index, ofVec2f t) { void QuadSurface::setTexCoord(int index, ofVec2f t){
if (index > 3) { if(index > 3){
ofLog() << "Texture coordinate with this index does not exist: " << index ofLog() << "Texture coordinate with this index does not exist: " << index
<< endl; << endl;
return; return;
} }
mesh.setTexCoord(index, t); mesh.setTexCoord(index, t);
calculate4dTextureCoords(); calculate4dTextureCoords();
} }
void QuadSurface::moveBy(ofVec2f v) { void QuadSurface::moveBy(ofVec2f v){
vector<ofVec3f>& vertices = getVertices(); vector <ofVec3f> & vertices = getVertices();
for (int i = 0; i < vertices.size(); i++) { for(int i = 0; i < vertices.size(); i++){
vertices[i] += v; vertices[i] += v;
} }
calculate4dTextureCoords(); calculate4dTextureCoords();
setMoved(true); setMoved(true);
} }
int QuadSurface::getType() { return SurfaceType::QUAD_SURFACE; } int QuadSurface::getType(){
return SurfaceType::QUAD_SURFACE;
bool QuadSurface::hitTest(ofVec2f p) { }
// Construct ofPolyline from vertices
ofPolyline line = getHitArea(); bool QuadSurface::hitTest(ofVec2f p){
// Construct ofPolyline from vertices
if (line.inside(p.x, p.y)) { ofPolyline line = getHitArea();
return true;
} else { if(line.inside(p.x, p.y)){
return false; return true;
} }else{
} return false;
}
ofVec2f QuadSurface::getVertex(int index) { }
if (index > 3) {
ofLog() << "Vertex with this index does not exist: " << index << endl; ofVec2f QuadSurface::getVertex(int index){
throw std::runtime_error("Vertex index out of bounds."); if(index > 3){
} ofLog() << "Vertex with this index does not exist: " << index << endl;
throw std::runtime_error("Vertex index out of bounds.");
ofVec3f vert = mesh.getVertex(index); }
return ofVec2f(vert.x, vert.y);
} ofVec3f vert = mesh.getVertex(index);
return ofVec2f(vert.x, vert.y);
ofVec2f QuadSurface::getTexCoord(int index) { }
if (index > 3) {
throw std::runtime_error("Texture coordinate index out of bounds."); ofVec2f QuadSurface::getTexCoord(int index){
} if(index > 3){
throw std::runtime_error("Texture coordinate index out of bounds.");
return mesh.getTexCoord(index); }
}
return mesh.getTexCoord(index);
ofPolyline QuadSurface::getHitArea() { }
ofPolyline line;
line.addVertex(ofPoint(mesh.getVertex(0).x, mesh.getVertex(0).y)); ofPolyline QuadSurface::getHitArea(){
line.addVertex(ofPoint(mesh.getVertex(1).x, mesh.getVertex(1).y)); ofPolyline line;
line.addVertex(ofPoint(mesh.getVertex(2).x, mesh.getVertex(2).y)); line.addVertex(ofPoint(mesh.getVertex(0).x, mesh.getVertex(0).y));
line.addVertex(ofPoint(mesh.getVertex(3).x, mesh.getVertex(3).y)); line.addVertex(ofPoint(mesh.getVertex(1).x, mesh.getVertex(1).y));
line.close(); line.addVertex(ofPoint(mesh.getVertex(2).x, mesh.getVertex(2).y));
line.addVertex(ofPoint(mesh.getVertex(3).x, mesh.getVertex(3).y));
return line; line.close();
}
return line;
ofPolyline QuadSurface::getTextureHitArea() { }
ofPolyline line;
vector<ofVec2f>& texCoords = mesh.getTexCoords(); ofPolyline QuadSurface::getTextureHitArea(){
ofVec2f textureSize = ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight()); ofPolyline line;
for (int i = 0; i < texCoords.size(); i++) { vector <ofVec2f> & texCoords = mesh.getTexCoords();
line.addVertex(ofPoint(texCoords[i] * textureSize)); ofVec2f textureSize = ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight());
} for(int i = 0; i < texCoords.size(); i++){
line.close(); line.addVertex(ofPoint(texCoords[i] * textureSize));
}
return line; line.close();
}
return line;
vector<ofVec3f>& QuadSurface::getVertices() { }
// return only joint vertices
return mesh.getVertices(); vector <ofVec3f> & QuadSurface::getVertices(){
} // return only joint vertices
return mesh.getVertices();
vector<ofVec2f>& QuadSurface::getTexCoords() { return mesh.getTexCoords(); } }
void QuadSurface::calculate4dTextureCoords() { vector <ofVec2f> & QuadSurface::getTexCoords(){
// Perspective Warping with OpenGL Fixed Pipeline and q coordinates return mesh.getTexCoords();
// see: }
// http://www.reedbeta.com/blog/2012/05/26/quadrilateral-interpolation-part-1/
// for information on the technique void QuadSurface::calculate4dTextureCoords(){
// Pue OpenGL is used because the ofMesh sadly doesn't support ofVec4f as // Perspective Warping with OpenGL Fixed Pipeline and q coordinates
// texture coordinates. // see:
// calculate intersection point // http://www.reedbeta.com/blog/2012/05/26/quadrilateral-interpolation-part-1/
ofVec3f p0 = mesh.getVertex(0); // for information on the technique
ofVec3f p1 = mesh.getVertex(1); // Pue OpenGL is used because the ofMesh sadly doesn't support ofVec4f as
ofVec3f p2 = mesh.getVertex(2); // texture coordinates.
ofVec3f p3 = mesh.getVertex(3); // calculate intersection point
ofVec3f p0 = mesh.getVertex(0);
ofVec3f t0 = mesh.getTexCoord(0); ofVec3f p1 = mesh.getVertex(1);
ofVec3f t1 = mesh.getTexCoord(1); ofVec3f p2 = mesh.getVertex(2);
ofVec3f t2 = mesh.getTexCoord(2); ofVec3f p3 = mesh.getVertex(3);
ofVec3f t3 = mesh.getTexCoord(3);
ofVec3f t0 = mesh.getTexCoord(0);
ofPoint interSect; ofVec3f t1 = mesh.getTexCoord(1);
ofLineSegmentIntersection(ofPoint(p0.x, p0.y), ofPoint(p2.x, p2.y), ofVec3f t2 = mesh.getTexCoord(2);
ofPoint(p1.x, p1.y), ofPoint(p3.x, p3.y), ofVec3f t3 = mesh.getTexCoord(3);
interSect);
ofVec3f interSecVec = ofVec3f(interSect.x, interSect.y, 0); ofPoint interSect;
ofLineSegmentIntersection(ofPoint(p0.x, p0.y), ofPoint(p2.x, p2.y),
// calculate distances to intersection point ofPoint(p1.x, p1.y), ofPoint(p3.x, p3.y),
float d0 = interSecVec.distance(p0); interSect);
float d1 = interSecVec.distance(p1); ofVec3f interSecVec = ofVec3f(interSect.x, interSect.y, 0);
float d2 = interSecVec.distance(p2);
float d3 = interSecVec.distance(p3); // calculate distances to intersection point
float d0 = interSecVec.distance(p0);
// vertices float d1 = interSecVec.distance(p1);
// top left corner float d2 = interSecVec.distance(p2);
quadVertices[0] = p0.x; float d3 = interSecVec.distance(p3);
quadVertices[1] = p0.y;
quadVertices[2] = 0; // vertices
// top right corner // top left corner
quadVertices[3] = p1.x; quadVertices[0] = p0.x;
quadVertices[4] = p1.y; quadVertices[1] = p0.y;
quadVertices[5] = 0; quadVertices[2] = 0;
// bottom right corner // top right corner
quadVertices[6] = p2.x; quadVertices[3] = p1.x;
quadVertices[7] = p2.y; quadVertices[4] = p1.y;
quadVertices[8] = 0; quadVertices[5] = 0;
// bottom left corner // bottom right corner
quadVertices[9] = p3.x; quadVertices[6] = p2.x;
quadVertices[10] = p3.y; quadVertices[7] = p2.y;
quadVertices[11] = 0; quadVertices[8] = 0;
// bottom left corner
float q0 = (d0 + d2) / (d2); quadVertices[9] = p3.x;
float q1 = (d1 + d3) / (d3); quadVertices[10] = p3.y;
float q2 = (d2 + d0) / (d0); quadVertices[11] = 0;
float q3 = (d3 + d1) / (d1);
float q0 = (d0 + d2) / (d2);
quadTexCoordinates[0] = t0.x; float q1 = (d1 + d3) / (d3);
quadTexCoordinates[1] = t0.y; float q2 = (d2 + d0) / (d0);
quadTexCoordinates[3] = q0; float q3 = (d3 + d1) / (d1);
quadTexCoordinates[4] = t1.x * q1; quadTexCoordinates[0] = t0.x;
quadTexCoordinates[5] = t1.y; quadTexCoordinates[1] = t0.y;
quadTexCoordinates[7] = q1; quadTexCoordinates[3] = q0;
quadTexCoordinates[8] = t2.x * q2; quadTexCoordinates[4] = t1.x * q1;
quadTexCoordinates[9] = t2.y * q2; quadTexCoordinates[5] = t1.y;
quadTexCoordinates[11] = q2; quadTexCoordinates[7] = q1;
quadTexCoordinates[12] = t3.x; quadTexCoordinates[8] = t2.x * q2;
quadTexCoordinates[13] = t3.y * q3; quadTexCoordinates[9] = t2.y * q2;
quadTexCoordinates[15] = q3; quadTexCoordinates[11] = q2;
}
} quadTexCoordinates[12] = t3.x;
} quadTexCoordinates[13] = t3.y * q3;
quadTexCoordinates[15] = q3;
}
} // namespace piMapper
} // namespace ofx

63
src/Surfaces/QuadSurface.h

@ -6,35 +6,38 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class QuadSurface : public BaseSurface { class QuadSurface : public BaseSurface {
public: public:
QuadSurface(); QuadSurface();
~QuadSurface(); ~QuadSurface();
void setup(); void setup();
void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, ofVec2f t1, void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f p4, ofVec2f t1,
ofVec2f t2, ofVec2f t3, ofVec2f t4, BaseSource* newSource); ofVec2f t2, ofVec2f t3, ofVec2f t4, BaseSource * newSource);
void draw(); void draw();
void setVertex(int index, ofVec2f p); void setVertex(int index, ofVec2f p);
void setTexCoord(int index, ofVec2f t); void setTexCoord(int index, ofVec2f t);
void moveBy(ofVec2f v); void moveBy(ofVec2f v);
int getType(); int getType();
bool hitTest(ofVec2f p); bool hitTest(ofVec2f p);
ofVec2f getVertex(int index); ofVec2f getVertex(int index);
ofVec2f getTexCoord(int index); ofVec2f getTexCoord(int index);
ofPolyline getHitArea(); ofPolyline getHitArea();
ofPolyline getTextureHitArea(); ofPolyline getTextureHitArea();
vector<ofVec3f>& getVertices(); vector <ofVec3f> & getVertices();
vector<ofVec2f>& getTexCoords(); vector <ofVec2f> & getTexCoords();
private: private:
void calculate4dTextureCoords(); void calculate4dTextureCoords();
GLfloat quadVertices[12]; GLfloat quadVertices[12];
GLubyte quadIndices[6]; GLubyte quadIndices[6];
GLfloat quadTexCoordinates[16]; GLfloat quadTexCoordinates[16];
}; };
}
} } // namespace piMapper
} // namespace ofx

775
src/Surfaces/SurfaceManager.cpp

@ -2,423 +2,428 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
SurfaceManager::SurfaceManager() :
mediaServer(NULL),
selectedSurface(NULL) {}
SurfaceManager::~SurfaceManager() { clear(); } SurfaceManager::SurfaceManager() :
mediaServer(NULL),
selectedSurface(NULL){}
void SurfaceManager::draw() { SurfaceManager::~SurfaceManager(){
for (int i = 0; i < surfaces.size(); i++) { clear();
ofSetColor(255, 255, 255, 255);
surfaces[i]->draw();
}
} }
void SurfaceManager::addSurface(int surfaceType) { void SurfaceManager::draw(){
if (surfaceType == SurfaceType::TRIANGLE_SURFACE) { for(int i = 0; i < surfaces.size(); i++){
surfaces.push_back(new TriangleSurface()); ofSetColor(255, 255, 255, 255);
} else if (surfaceType == SurfaceType::QUAD_SURFACE) { surfaces[i]->draw();
surfaces.push_back(new QuadSurface()); }
} else {
ofLogFatalError("SurfaceManager") << "Attempt to add non-existing surface type";
std::exit(EXIT_FAILURE);
}
} }
void SurfaceManager::addSurface(int surfaceType, BaseSource* newSource) { void SurfaceManager::addSurface(int surfaceType){
if (surfaceType == SurfaceType::TRIANGLE_SURFACE) { if(surfaceType == SurfaceType::TRIANGLE_SURFACE){
surfaces.push_back(new TriangleSurface()); surfaces.push_back(new TriangleSurface());
surfaces.back()->setSource(newSource); }else if(surfaceType == SurfaceType::QUAD_SURFACE){
} else if (surfaceType == SurfaceType::QUAD_SURFACE) { surfaces.push_back(new QuadSurface());
surfaces.push_back(new QuadSurface()); }else{
surfaces.back()->setSource(newSource); ofLogFatalError("SurfaceManager") << "Attempt to add non-existing surface type";
} else { std::exit(EXIT_FAILURE);
ofLogFatalError("SurfaceManager") << "Attempt to add non-existing surface type"; }
std::exit(EXIT_FAILURE);
}
} }
void SurfaceManager::addSurface(int surfaceType, vector<ofVec2f> vertices, void SurfaceManager::addSurface(int surfaceType, BaseSource * newSource){
vector<ofVec2f> texCoords) { if(surfaceType == SurfaceType::TRIANGLE_SURFACE){
if (surfaceType == SurfaceType::TRIANGLE_SURFACE) { surfaces.push_back(new TriangleSurface());
if (vertices.size() < 3) { surfaces.back()->setSource(newSource);
throw std::runtime_error( }else if(surfaceType == SurfaceType::QUAD_SURFACE){
"There must be 3 vertices for a triangle surface."); surfaces.push_back(new QuadSurface());
} else if (texCoords.size() < 3) { surfaces.back()->setSource(newSource);
throw std::runtime_error( }else{
"There must be 3 texture coordinates for a triangle surface."); ofLogFatalError("SurfaceManager") << "Attempt to add non-existing surface type";
} std::exit(EXIT_FAILURE);
}
surfaces.push_back(new TriangleSurface());
for (int i = 0; i < 3; i++) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
} else if (surfaceType == SurfaceType::QUAD_SURFACE) {
if (vertices.size() < 4) {
throw std::runtime_error("There must be 4 vertices for a quad surface.");
} else if (texCoords.size() < 4) {
throw std::runtime_error(
"There must be 4 texture coordinates for a quad surface.");
}
surfaces.push_back(new QuadSurface());
for (int i = 0; i < 4; i++) {
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
} else {
ofLogFatalError("SurfaceManager") << "Attempt to add non-existing surface type";
std::exit(EXIT_FAILURE);
}
} }
void SurfaceManager::addSurface(int surfaceType, BaseSource* newSource, void SurfaceManager::addSurface(int surfaceType, vector <ofVec2f> vertices,
vector<ofVec2f> vertices, vector <ofVec2f> texCoords){
vector<ofVec2f> texCoords) { if(surfaceType == SurfaceType::TRIANGLE_SURFACE){
if (surfaceType == SurfaceType::TRIANGLE_SURFACE) { if(vertices.size() < 3){
if (vertices.size() < 3) { throw std::runtime_error(
throw std::runtime_error( "There must be 3 vertices for a triangle surface.");
"There must be 3 vertices for a triangle surface."); }else if(texCoords.size() < 3){
} else if (texCoords.size() < 3) { throw std::runtime_error(
throw std::runtime_error( "There must be 3 texture coordinates for a triangle surface.");
"Thre must be 3 texture coordinates for a triangle surface."); }
}
surfaces.push_back(new TriangleSurface());
surfaces.push_back(new TriangleSurface());
surfaces.back()->setSource(newSource); for(int i = 0; i < 3; i++){
surfaces.back()->setVertex(i, vertices[i]);
for (int i = 0; i < 3; i++) { surfaces.back()->setTexCoord(i, texCoords[i]);
surfaces.back()->setVertex(i, vertices[i]); }
surfaces.back()->setTexCoord(i, texCoords[i]);
} }else if(surfaceType == SurfaceType::QUAD_SURFACE){
if(vertices.size() < 4){
} else if (surfaceType == SurfaceType::QUAD_SURFACE) { throw std::runtime_error("There must be 4 vertices for a quad surface.");
if (vertices.size() < 4) { }else if(texCoords.size() < 4){
throw std::runtime_error("There must be 4 vertices for a quad surface."); throw std::runtime_error(
} else if (texCoords.size() < 4) { "There must be 4 texture coordinates for a quad surface.");
throw std::runtime_error( }
"Thre must be 4 texture coordinates for a quad surface.");
} surfaces.push_back(new QuadSurface());
surfaces.push_back(new QuadSurface()); for(int i = 0; i < 4; i++){
surfaces.back()->setSource(newSource); surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
for (int i = 0; i < 4; i++) { }
surfaces.back()->setVertex(i, vertices[i]); }else{
surfaces.back()->setTexCoord(i, texCoords[i]); ofLogFatalError("SurfaceManager") << "Attempt to add non-existing surface type";
} std::exit(EXIT_FAILURE);
} else { }
ofLogFatalError("SurfaceManager") << "Attempt to add non-existing surface type"; }
std::exit(EXIT_FAILURE);
} void SurfaceManager::addSurface(int surfaceType, BaseSource * newSource,
vector <ofVec2f> vertices,
vector <ofVec2f> texCoords){
if(surfaceType == SurfaceType::TRIANGLE_SURFACE){
if(vertices.size() < 3){
throw std::runtime_error(
"There must be 3 vertices for a triangle surface.");
}else if(texCoords.size() < 3){
throw std::runtime_error(
"Thre must be 3 texture coordinates for a triangle surface.");
}
surfaces.push_back(new TriangleSurface());
surfaces.back()->setSource(newSource);
for(int i = 0; i < 3; i++){
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
}else if(surfaceType == SurfaceType::QUAD_SURFACE){
if(vertices.size() < 4){
throw std::runtime_error("There must be 4 vertices for a quad surface.");
}else if(texCoords.size() < 4){
throw std::runtime_error(
"Thre must be 4 texture coordinates for a quad surface.");
}
surfaces.push_back(new QuadSurface());
surfaces.back()->setSource(newSource);
for(int i = 0; i < 4; i++){
surfaces.back()->setVertex(i, vertices[i]);
surfaces.back()->setTexCoord(i, texCoords[i]);
}
}else{
ofLogFatalError("SurfaceManager") << "Attempt to add non-existing surface type";
std::exit(EXIT_FAILURE);
}
} }
// Add existing surface // Add existing surface
void SurfaceManager::addSurface(BaseSurface * surface){ void SurfaceManager::addSurface(BaseSurface * surface){
surfaces.push_back(surface); surfaces.push_back(surface);
} }
void SurfaceManager::removeSelectedSurface(){ void SurfaceManager::removeSelectedSurface(){
if (selectedSurface == NULL){ if(selectedSurface == NULL){
return; return;
} }
for (int i = 0; i < surfaces.size(); i++){ for(int i = 0; i < surfaces.size(); i++){
if (surfaces[i] == selectedSurface){ if(surfaces[i] == selectedSurface){
// Do not delete pointer as we are storing the // Do not delete pointer as we are storing the
// surface in the RemoveSurfaceCommand. // surface in the RemoveSurfaceCommand.
//delete surfaces[i]; //delete surfaces[i];
surfaces.erase(surfaces.begin() + i); surfaces.erase(surfaces.begin() + i);
selectedSurface = NULL; selectedSurface = NULL;
break; break;
} }
} }
} }
void SurfaceManager::removeSurface() { void SurfaceManager::removeSurface(){
if (!surfaces.size()) { if(!surfaces.size()){
return; return;
} }
delete surfaces.back(); delete surfaces.back();
surfaces.pop_back(); surfaces.pop_back();
} }
void SurfaceManager::clear() { void SurfaceManager::clear(){
// delete all extra allocations from the heap // delete all extra allocations from the heap
while (surfaces.size()) { while(surfaces.size()){
delete surfaces.back(); delete surfaces.back();
surfaces.pop_back(); surfaces.pop_back();
} }
} }
void SurfaceManager::saveXmlSettings(string fileName) { void SurfaceManager::saveXmlSettings(string fileName){
// Exit if mediaServer not set // Exit if mediaServer not set
if (mediaServer == NULL) { if(mediaServer == NULL){
ofLogFatalError("SurfaceManager") << "Media server not set"; ofLogFatalError("SurfaceManager") << "Media server not set";
std::exit(EXIT_FAILURE); std::exit(EXIT_FAILURE);
} }
// We need a fresh copy of the xml settings object // We need a fresh copy of the xml settings object
xmlSettings.clear(); xmlSettings.clear();
// Save surfaces // Save surfaces
xmlSettings.addTag("surfaces"); xmlSettings.addTag("surfaces");
xmlSettings.pushTag("surfaces"); xmlSettings.pushTag("surfaces");
for (int i = 0; i < surfaces.size(); i++) { for(int i = 0; i < surfaces.size(); i++){
xmlSettings.addTag("surface"); xmlSettings.addTag("surface");
xmlSettings.pushTag("surface", i); xmlSettings.pushTag("surface", i);
BaseSurface* surface = surfaces[i]; BaseSurface * surface = surfaces[i];
xmlSettings.addTag("vertices"); xmlSettings.addTag("vertices");
xmlSettings.pushTag("vertices"); xmlSettings.pushTag("vertices");
vector<ofVec3f>* vertices = &surface->getVertices(); vector <ofVec3f> * vertices = &surface->getVertices();
for (int j = 0; j < vertices->size(); j++) { for(int j = 0; j < vertices->size(); j++){
xmlSettings.addTag("vertex"); xmlSettings.addTag("vertex");
xmlSettings.pushTag("vertex", j); xmlSettings.pushTag("vertex", j);
ofVec3f* vertex = &(*vertices)[j]; ofVec3f * vertex = &(*vertices)[j];
xmlSettings.addValue("x", vertex->x); xmlSettings.addValue("x", vertex->x);
xmlSettings.addValue("y", vertex->y); xmlSettings.addValue("y", vertex->y);
// we don't need z as it will be 0 anyways // we don't need z as it will be 0 anyways
xmlSettings.popTag(); // vertex xmlSettings.popTag(); // vertex
} }
xmlSettings.popTag(); // vertices xmlSettings.popTag(); // vertices
xmlSettings.addTag("texCoords"); xmlSettings.addTag("texCoords");
xmlSettings.pushTag("texCoords"); xmlSettings.pushTag("texCoords");
vector<ofVec2f>* texCoords = &surface->getTexCoords(); vector <ofVec2f> * texCoords = &surface->getTexCoords();
for (int j = 0; j < texCoords->size(); j++) { for(int j = 0; j < texCoords->size(); j++){
xmlSettings.addTag("texCoord"); xmlSettings.addTag("texCoord");
xmlSettings.pushTag("texCoord", j); xmlSettings.pushTag("texCoord", j);
ofVec2f* texCoord = &(*texCoords)[j]; ofVec2f * texCoord = &(*texCoords)[j];
xmlSettings.addValue("x", texCoord->x); xmlSettings.addValue("x", texCoord->x);
xmlSettings.addValue("y", texCoord->y); xmlSettings.addValue("y", texCoord->y);
xmlSettings.popTag(); // texCoord xmlSettings.popTag(); // texCoord
} }
xmlSettings.popTag(); // texCoords xmlSettings.popTag(); // texCoords
xmlSettings.addTag("source"); xmlSettings.addTag("source");
xmlSettings.pushTag("source"); xmlSettings.pushTag("source");
string sourceTypeName = SourceType::GetSourceTypeName(surface->getSource()->getType()); string sourceTypeName = SourceType::GetSourceTypeName(surface->getSource()->getType());
cout << "sourceTypeName: " << sourceTypeName << endl; cout << "sourceTypeName: " << sourceTypeName << endl;
xmlSettings.addValue("source-type", sourceTypeName); xmlSettings.addValue("source-type", sourceTypeName);
xmlSettings.addValue("source-name", surface->getSource()->getName()); xmlSettings.addValue("source-name", surface->getSource()->getName());
xmlSettings.popTag(); // source xmlSettings.popTag(); // source
xmlSettings.popTag(); // surface xmlSettings.popTag(); // surface
} }
xmlSettings.popTag(); // surfaces xmlSettings.popTag(); // surfaces
xmlSettings.save(fileName); xmlSettings.save(fileName);
} }
void SurfaceManager::loadXmlSettings(string fileName) { void SurfaceManager::loadXmlSettings(string fileName){
// Exit if there is no media server // Exit if there is no media server
if (mediaServer == NULL) { if(mediaServer == NULL){
ofLogFatalError("SurfaceManager") << "Media server not set"; ofLogFatalError("SurfaceManager") << "Media server not set";
std::exit(EXIT_FAILURE); std::exit(EXIT_FAILURE);
} }
if (!xmlSettings.loadFile(fileName)) { if(!xmlSettings.loadFile(fileName)){
ofLogWarning("SurfaceManager") << "Could not load XML settings"; ofLogWarning("SurfaceManager") << "Could not load XML settings";
return; return;
} }
if (!xmlSettings.tagExists("surfaces")) { if(!xmlSettings.tagExists("surfaces")){
ofLogWarning("SurfaceManager") << "XML settings is empty or has wrong markup"; ofLogWarning("SurfaceManager") << "XML settings is empty or has wrong markup";
return; return;
} }
xmlSettings.pushTag("surfaces"); xmlSettings.pushTag("surfaces");
int numSurfaces = xmlSettings.getNumTags("surface"); int numSurfaces = xmlSettings.getNumTags("surface");
for (int i = 0; i < numSurfaces; i++) { for(int i = 0; i < numSurfaces; i++){
xmlSettings.pushTag("surface", i); xmlSettings.pushTag("surface", i);
// attempt to load surface source // attempt to load surface source
xmlSettings.pushTag("source"); xmlSettings.pushTag("source");
string sourceType = xmlSettings.getValue("source-type", ""); string sourceType = xmlSettings.getValue("source-type", "");
string sourceName = xmlSettings.getValue("source-name", ""); string sourceName = xmlSettings.getValue("source-name", "");
BaseSource* source = NULL; BaseSource * source = NULL;
if (sourceName != "" && sourceName != "none" && sourceType != "") { if(sourceName != "" && sourceName != "none" && sourceType != ""){
// Load source depending on type // Load source depending on type
int typeEnum = SourceType::GetSourceTypeEnum(sourceType); int typeEnum = SourceType::GetSourceTypeEnum(sourceType);
if (typeEnum == SourceType::SOURCE_TYPE_FBO) { if(typeEnum == SourceType::SOURCE_TYPE_FBO){
// Load FBO source using sourceName // Load FBO source using sourceName
source = mediaServer->loadMedia(sourceName, typeEnum); source = mediaServer->loadMedia(sourceName, typeEnum);
} else { }else{
// Construct full path // Construct full path
string dir = mediaServer->getDefaultMediaDir(typeEnum); string dir = mediaServer->getDefaultMediaDir(typeEnum);
std::stringstream pathss; std::stringstream pathss;
pathss << ofToDataPath(dir, true) << sourceName; pathss << ofToDataPath(dir, true) << sourceName;
string sourcePath = pathss.str(); string sourcePath = pathss.str();
// Load media by using full path // Load media by using full path
source = mediaServer->loadMedia(sourcePath, typeEnum); source = mediaServer->loadMedia(sourcePath, typeEnum);
} }
} }
xmlSettings.popTag(); // source xmlSettings.popTag(); // source
xmlSettings.pushTag("vertices"); xmlSettings.pushTag("vertices");
vector<ofVec2f> vertices; vector <ofVec2f> vertices;
int vertexCount = xmlSettings.getNumTags("vertex"); int vertexCount = xmlSettings.getNumTags("vertex");
// it's a triangle ? // it's a triangle ?
if (vertexCount == 3) { if(vertexCount == 3){
//ofLog(OF_LOG_NOTICE, "create Triangle"); //ofLog(OF_LOG_NOTICE, "create Triangle");
xmlSettings.pushTag("vertex", 0); xmlSettings.pushTag("vertex", 0);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 0.0f))); xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag(); xmlSettings.popTag();
xmlSettings.pushTag("vertex", 1); xmlSettings.pushTag("vertex", 1);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f), vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
xmlSettings.getValue("y", 0.0f))); xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag(); xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2); xmlSettings.pushTag("vertex", 2);
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 100.0f))); xmlSettings.getValue("y", 100.0f)));
xmlSettings.popTag(); xmlSettings.popTag();
xmlSettings.popTag(); // vertices xmlSettings.popTag(); // vertices
xmlSettings.pushTag("texCoords"); xmlSettings.pushTag("texCoords");
vector<ofVec2f> texCoords; vector <ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0); xmlSettings.pushTag("texCoord", 0);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 0.0f))); xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag(); xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 1); xmlSettings.pushTag("texCoord", 1);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f), texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
xmlSettings.getValue("y", 0.0f))); xmlSettings.getValue("y", 0.0f)));
xmlSettings.popTag(); xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2); xmlSettings.pushTag("texCoord", 2);
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
xmlSettings.getValue("y", 1.0f))); xmlSettings.getValue("y", 1.0f)));
xmlSettings.popTag(); xmlSettings.popTag();
xmlSettings.popTag(); // texCoords xmlSettings.popTag(); // texCoords
// now we have variables sourceName and sourceTexture // now we have variables sourceName and sourceTexture
// by checking those we can use one or another addSurface method // by checking those we can use one or another addSurface method
if (sourceName != "none" && source != NULL) { if(sourceName != "none" && source != NULL){
addSurface(SurfaceType::TRIANGLE_SURFACE, source, vertices, addSurface(SurfaceType::TRIANGLE_SURFACE, source, vertices,
texCoords); texCoords);
} else { }else{
addSurface(SurfaceType::TRIANGLE_SURFACE, vertices, texCoords); addSurface(SurfaceType::TRIANGLE_SURFACE, vertices, texCoords);
} }
} }
// it's a quad ? // it's a quad ?
else if (vertexCount == 4) else if(vertexCount == 4){
// if (surface-type == QUAD_SURFACE) // if (surface-type == QUAD_SURFACE)
{ xmlSettings.pushTag("vertex", 0);
xmlSettings.pushTag("vertex", 0); vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f)));
xmlSettings.getValue("y", 0.0f))); xmlSettings.popTag();
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 1);
xmlSettings.pushTag("vertex", 1); vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 0.0f)));
xmlSettings.getValue("y", 0.0f))); xmlSettings.popTag();
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 2);
xmlSettings.pushTag("vertex", 2); vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f),
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 100.0f), xmlSettings.getValue("y", 100.0f)));
xmlSettings.getValue("y", 100.0f))); xmlSettings.popTag();
xmlSettings.popTag();
xmlSettings.pushTag("vertex", 3);
xmlSettings.pushTag("vertex", 3); vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
vertices.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 100.0f)));
xmlSettings.getValue("y", 100.0f))); xmlSettings.popTag();
xmlSettings.popTag();
xmlSettings.popTag(); // vertices
xmlSettings.popTag(); // vertices
xmlSettings.pushTag("texCoords");
xmlSettings.pushTag("texCoords");
vector <ofVec2f> texCoords;
vector<ofVec2f> texCoords;
xmlSettings.pushTag("texCoord", 0);
xmlSettings.pushTag("texCoord", 0); texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 0.0f)));
xmlSettings.getValue("y", 0.0f))); xmlSettings.popTag();
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 1);
xmlSettings.pushTag("texCoord", 1); texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 0.0f)));
xmlSettings.getValue("y", 0.0f))); xmlSettings.popTag();
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 2);
xmlSettings.pushTag("texCoord", 2); texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f),
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 1.0f), xmlSettings.getValue("y", 1.0f)));
xmlSettings.getValue("y", 1.0f))); xmlSettings.popTag();
xmlSettings.popTag();
xmlSettings.pushTag("texCoord", 3);
xmlSettings.pushTag("texCoord", 3); texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f),
texCoords.push_back(ofVec2f(xmlSettings.getValue("x", 0.0f), xmlSettings.getValue("y", 1.0f)));
xmlSettings.getValue("y", 1.0f))); xmlSettings.popTag();
xmlSettings.popTag();
xmlSettings.popTag(); // texCoords
xmlSettings.popTag(); // texCoords
// now we have variables sourceName and sourceTexture
// now we have variables sourceName and sourceTexture // by checking those we can use one or another addSurface method
// by checking those we can use one or another addSurface method if(sourceName != "none" && source != NULL){
if (sourceName != "none" && source != NULL) { addSurface(SurfaceType::QUAD_SURFACE, source, vertices,
addSurface(SurfaceType::QUAD_SURFACE, source, vertices, texCoords);
texCoords); }else{
} else { addSurface(SurfaceType::QUAD_SURFACE, vertices, texCoords);
addSurface(SurfaceType::QUAD_SURFACE, vertices, texCoords); }
} }
}
xmlSettings.popTag(); // surface
xmlSettings.popTag(); // surface }
}
xmlSettings.popTag(); // surfaces
xmlSettings.popTag(); // surfaces
} }
void SurfaceManager::setMediaServer(MediaServer* newMediaServer) { void SurfaceManager::setMediaServer(MediaServer * newMediaServer){
mediaServer = newMediaServer; mediaServer = newMediaServer;
} }
BaseSurface * SurfaceManager::selectSurface(int index) { BaseSurface * SurfaceManager::selectSurface(int index){
if (index >= surfaces.size()) { if(index >= surfaces.size()){
throw std::runtime_error("Surface index out of bounds."); throw std::runtime_error("Surface index out of bounds.");
} }
selectedSurface = surfaces[index]; selectedSurface = surfaces[index];
// notify that a new surface has been selected // notify that a new surface has been selected
ofSendMessage("surfaceSelected"); ofSendMessage("surfaceSelected");
return selectedSurface; return selectedSurface;
}
BaseSurface * SurfaceManager::selectSurface(BaseSurface * surface){
for (int i = 0; i < surfaces.size(); i++) {
if (surfaces[i] == surface){
selectedSurface = surface;
ofSendMessage("surfaceSelected");
return selectedSurface;
}
}
deselectSurface();
return 0;
}
BaseSurface* SurfaceManager::getSelectedSurface() {
return selectedSurface;
} }
void SurfaceManager::deselectSurface() { BaseSurface * SurfaceManager::selectSurface(BaseSurface * surface){
selectedSurface = NULL; for(int i = 0; i < surfaces.size(); i++){
if(surfaces[i] == surface){
selectedSurface = surface;
ofSendMessage("surfaceSelected");
return selectedSurface;
}
}
deselectSurface();
return 0;
} }
BaseSurface* SurfaceManager::getSurface(int index) { BaseSurface * SurfaceManager::getSelectedSurface(){
if (index >= surfaces.size()) { return selectedSurface;
throw std::runtime_error("Surface index out of bounds."); }
return NULL;
}
return surfaces[index]; void SurfaceManager::deselectSurface(){
selectedSurface = NULL;
} }
int SurfaceManager::size() { return surfaces.size(); } BaseSurface * SurfaceManager::getSurface(int index){
if(index >= surfaces.size()){
throw std::runtime_error("Surface index out of bounds.");
return NULL;
}
return surfaces[index];
} }
int SurfaceManager::size(){
return surfaces.size();
} }
} // namespace piMapper
} // namespace ofx

82
src/Surfaces/SurfaceManager.h

@ -15,44 +15,48 @@ using namespace std;
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class SurfaceManager { class SurfaceManager {
public:
SurfaceManager(); public:
~SurfaceManager(); SurfaceManager();
~SurfaceManager();
void draw();
void draw();
// TODO: These should be renamed to createSurface
void addSurface(int surfaceType); // TODO: These should be renamed to createSurface
void addSurface(int surfaceType, BaseSource* newSource); void addSurface(int surfaceType);
void addSurface(int surfaceType, vector<ofVec2f> vertices, void addSurface(int surfaceType, BaseSource * newSource);
vector<ofVec2f> texCoords); void addSurface(int surfaceType, vector <ofVec2f> vertices,
void addSurface(int surfaceType, BaseSource* newSource, vector <ofVec2f> texCoords);
vector<ofVec2f> vertices, vector<ofVec2f> texCoords); void addSurface(int surfaceType, BaseSource * newSource,
vector <ofVec2f> vertices, vector <ofVec2f> texCoords);
// Except this, as it adds existing surface
void addSurface(BaseSurface * surface); // Except this, as it adds existing surface
void addSurface(BaseSurface * surface);
void removeSelectedSurface();
void removeSurface(); void removeSelectedSurface();
void removeSurface();
void clear();
void saveXmlSettings(string fileName); void clear();
void loadXmlSettings(string fileName); void saveXmlSettings(string fileName);
void setMediaServer(MediaServer* newMediaServer); void loadXmlSettings(string fileName);
void setMediaServer(MediaServer * newMediaServer);
BaseSurface* getSurface(int index);
int size(); BaseSurface * getSurface(int index);
BaseSurface* selectSurface(int index); int size();
BaseSurface* selectSurface(BaseSurface * surface); BaseSurface * selectSurface(int index);
BaseSurface* getSelectedSurface(); BaseSurface * selectSurface(BaseSurface * surface);
void deselectSurface(); BaseSurface * getSelectedSurface();
void deselectSurface();
private:
std::vector<BaseSurface*> surfaces; private:
BaseSurface* selectedSurface; std::vector <BaseSurface *> surfaces;
ofxXmlSettings xmlSettings; BaseSurface * selectedSurface;
MediaServer* mediaServer; ofxXmlSettings xmlSettings;
MediaServer * mediaServer;
}; };
}
} } // namespace piMapper
} // namespace ofx

627
src/Surfaces/SurfaceManagerGui.cpp

@ -1,311 +1,322 @@
#include "SurfaceManagerGui.h" #include "SurfaceManagerGui.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
SurfaceManagerGui::SurfaceManagerGui() {
surfaceManager = NULL; SurfaceManagerGui::SurfaceManagerGui(){
guiMode = GuiMode::NONE; surfaceManager = NULL;
bDrag = false; guiMode = GuiMode::NONE;
registerMouseEvents(); bDrag = false;
ofHideCursor(); registerMouseEvents();
_cmdManager = 0; ofHideCursor();
} _cmdManager = 0;
}
SurfaceManagerGui::~SurfaceManagerGui() {
unregisterMouseEvents(); SurfaceManagerGui::~SurfaceManagerGui(){
surfaceManager = NULL; unregisterMouseEvents();
_cmdManager = 0; surfaceManager = NULL;
} _cmdManager = 0;
}
void SurfaceManagerGui::registerMouseEvents() {
ofAddListener(ofEvents().mousePressed, this, void SurfaceManagerGui::registerMouseEvents(){
&SurfaceManagerGui::mousePressed); ofAddListener(ofEvents().mousePressed, this,
ofAddListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mousePressed);
&SurfaceManagerGui::mouseReleased); ofAddListener(ofEvents().mouseReleased, this,
ofAddListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseReleased);
&SurfaceManagerGui::mouseDragged); ofAddListener(ofEvents().mouseDragged, this,
} &SurfaceManagerGui::mouseDragged);
}
void SurfaceManagerGui::unregisterMouseEvents() {
ofRemoveListener(ofEvents().mousePressed, this, void SurfaceManagerGui::unregisterMouseEvents(){
&SurfaceManagerGui::mousePressed); ofRemoveListener(ofEvents().mousePressed, this,
ofRemoveListener(ofEvents().mouseReleased, this, &SurfaceManagerGui::mousePressed);
&SurfaceManagerGui::mouseReleased); ofRemoveListener(ofEvents().mouseReleased, this,
ofRemoveListener(ofEvents().mouseDragged, this, &SurfaceManagerGui::mouseReleased);
&SurfaceManagerGui::mouseDragged); ofRemoveListener(ofEvents().mouseDragged, this,
} &SurfaceManagerGui::mouseDragged);
}
void SurfaceManagerGui::draw() {
if (surfaceManager == NULL) return; void SurfaceManagerGui::draw(){
if(surfaceManager == NULL){
if (guiMode == GuiMode::NONE) { return;
surfaceManager->draw(); }
} else if (guiMode == GuiMode::TEXTURE_MAPPING) {
// draw the texture of the selected surface if(guiMode == GuiMode::NONE){
if (surfaceManager->getSelectedSurface() != NULL) { surfaceManager->draw();
// Reset default color to white first }else if(guiMode == GuiMode::TEXTURE_MAPPING){
ofSetColor(255, 255, 255, 255); // draw the texture of the selected surface
surfaceManager->getSelectedSurface()->drawTexture(ofVec2f(0, 0)); if(surfaceManager->getSelectedSurface() != NULL){
} // Reset default color to white first
ofSetColor(255, 255, 255, 255);
// draw surfaces with opacity surfaceManager->getSelectedSurface()->drawTexture(ofVec2f(0, 0));
ofPushStyle(); }
ofSetColor(255, 255, 255, 200);
surfaceManager->draw(); // draw surfaces with opacity
ofPopStyle(); ofPushStyle();
ofSetColor(255, 255, 255, 200);
// highlight selected surface surfaceManager->draw();
drawSelectedSurfaceHighlight(); ofPopStyle();
// hilight selected surface texture // highlight selected surface
drawSelectedSurfaceTextureHighlight(); drawSelectedSurfaceHighlight();
// draw texture editing GUI on top // hilight selected surface texture
textureEditor.draw(); drawSelectedSurfaceTextureHighlight();
} else if (guiMode == GuiMode::PROJECTION_MAPPING) { // draw texture editing GUI on top
// draw projection surfaces first textureEditor.draw();
surfaceManager->draw();
}else if(guiMode == GuiMode::PROJECTION_MAPPING){
// highlight selected surface // draw projection surfaces first
drawSelectedSurfaceHighlight(); surfaceManager->draw();
// draw projection mapping editing gui // highlight selected surface
projectionEditor.draw(); drawSelectedSurfaceHighlight();
} else if (guiMode == GuiMode::SOURCE_SELECTION) { // draw projection mapping editing gui
// draw projection surfaces first projectionEditor.draw();
surfaceManager->draw();
}else if(guiMode == GuiMode::SOURCE_SELECTION){
// highlight selected surface // draw projection surfaces first
drawSelectedSurfaceHighlight(); surfaceManager->draw();
sourcesEditor.draw(); // highlight selected surface
} drawSelectedSurfaceHighlight();
}
sourcesEditor.draw();
void SurfaceManagerGui::mousePressed(ofMouseEventArgs& args) { }
if (guiMode == GuiMode::NONE) { }
return;
} else if (guiMode == GuiMode::TEXTURE_MAPPING) { void SurfaceManagerGui::mousePressed(ofMouseEventArgs & args){
bool bSurfaceSelected = false; if(guiMode == GuiMode::NONE){
CircleJoint* hitJoint = return;
textureEditor.hitTestJoints(ofVec2f(args.x, args.y)); }else if(guiMode == GuiMode::TEXTURE_MAPPING){
if (hitJoint != NULL) { bool bSurfaceSelected = false;
textureEditor.unselectAllJoints(); CircleJoint * hitJoint =
textureEditor.hitTestJoints(ofVec2f(args.x, args.y));
hitJoint->select(); if(hitJoint != NULL){
hitJoint->startDrag(); textureEditor.unselectAllJoints();
bSurfaceSelected = true;
hitJoint->select();
int jointIndex = -1; hitJoint->startDrag();
for (int i = 0; i < textureEditor.getJoints().size(); i++) { bSurfaceSelected = true;
if (textureEditor.getJoints()[i] == hitJoint) {
jointIndex = i; int jointIndex = -1;
break; for(int i = 0; i < textureEditor.getJoints().size(); i++){
} if(textureEditor.getJoints()[i] == hitJoint){
} jointIndex = i;
break;
if (jointIndex != -1) { }
_cmdManager->exec(new MvTexCoordCmd(jointIndex, &textureEditor)); }
}
if(jointIndex != -1){
} else { _cmdManager->exec(new MvTexCoordCmd(jointIndex, &textureEditor));
textureEditor.unselectAllJoints(); }
}
}else{
if (surfaceManager->getSelectedSurface() != NULL && !bSurfaceSelected) { textureEditor.unselectAllJoints();
// hittest texture area to see if we are hitting the texture surface }
if (surfaceManager->getSelectedSurface()->getTextureHitArea().inside(
args.x, args.y)) { if(surfaceManager->getSelectedSurface() != NULL && !bSurfaceSelected){
// hittest texture area to see if we are hitting the texture surface
// TODO: move these to a separate routine if(surfaceManager->getSelectedSurface()->getTextureHitArea().inside(
clickPosition = ofVec2f(args.x, args.y); args.x, args.y)){
startDrag();
// TODO: move these to a separate routine
_cmdManager->exec(new MvAllTexCoordsCmd( clickPosition = ofVec2f(args.x, args.y);
surfaceManager->getSelectedSurface(), startDrag();
&textureEditor));
_cmdManager->exec(new MvAllTexCoordsCmd(
} surfaceManager->getSelectedSurface(),
} &textureEditor));
} else if (guiMode == GuiMode::PROJECTION_MAPPING) { }
bool bSurfaceSelected = false; }
CircleJoint* hitJoint = }else if(guiMode == GuiMode::PROJECTION_MAPPING){
projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)); bool bSurfaceSelected = false;
if (hitJoint != NULL) {
projectionEditor.unselectAllJoints(); CircleJoint * hitJoint =
hitJoint->select(); projectionEditor.hitTestJoints(ofVec2f(args.x, args.y));
hitJoint->startDrag(); if(hitJoint != NULL){
int jointVertIndex = 0; projectionEditor.unselectAllJoints();
for (int i = 0; i < projectionEditor.getJoints()->size(); i++) { hitJoint->select();
if ((*projectionEditor.getJoints())[i] == hitJoint) { hitJoint->startDrag();
jointVertIndex = i; int jointVertIndex = 0;
break; for(int i = 0; i < projectionEditor.getJoints()->size(); i++){
} if((*projectionEditor.getJoints())[i] == hitJoint){
} jointVertIndex = i;
_cmdManager->exec(new MvSurfaceVertCmd( break;
jointVertIndex, }
surfaceManager->getSelectedSurface(), }
&projectionEditor)); _cmdManager->exec(new MvSurfaceVertCmd(
bSurfaceSelected = true; jointVertIndex,
} surfaceManager->getSelectedSurface(),
&projectionEditor));
// attempt to select surface, loop from end to beginning bSurfaceSelected = true;
if (!bSurfaceSelected) { }
for (int i = surfaceManager->size() - 1; i >= 0; i--) {
if (surfaceManager->getSurface(i)->hitTest(ofVec2f(args.x, args.y))) { // attempt to select surface, loop from end to beginning
if(!bSurfaceSelected){
// Do not repeat this command if attempting to select an for(int i = surfaceManager->size() - 1; i >= 0; i--){
// already selected surface. if(surfaceManager->getSurface(i)->hitTest(ofVec2f(args.x, args.y))){
if (surfaceManager->getSelectedSurface() != surfaceManager->getSurface(i)){
_cmdManager->exec(new SelSurfaceCmd( // Do not repeat this command if attempting to select an
surfaceManager, // already selected surface.
surfaceManager->getSurface(i), if(surfaceManager->getSelectedSurface() != surfaceManager->getSurface(i)){
&projectionEditor)); _cmdManager->exec(new SelSurfaceCmd(
} surfaceManager,
surfaceManager->getSurface(i),
bSurfaceSelected = true; &projectionEditor));
break; }
}
} bSurfaceSelected = true;
} break;
}
if (bSurfaceSelected && hitJoint == NULL) { }
}
// if not hitting the joints, start drag only if
// we have a selected surface if(bSurfaceSelected && hitJoint == NULL){
clickPosition = ofVec2f(args.x, args.y);
startDrag(); // if not hitting the joints, start drag only if
// we have a selected surface
_cmdManager->exec( clickPosition = ofVec2f(args.x, args.y);
new MvSurfaceCmd( startDrag();
surfaceManager->getSelectedSurface(),
&projectionEditor)); _cmdManager->exec(
} new MvSurfaceCmd(
surfaceManager->getSelectedSurface(),
if (!bSurfaceSelected) { &projectionEditor));
// unselect if no surface selected }
projectionEditor.clearJoints();
surfaceManager->deselectSurface(); if(!bSurfaceSelected){
} // unselect if no surface selected
} else if (guiMode == GuiMode::SOURCE_SELECTION) { projectionEditor.clearJoints();
} surfaceManager->deselectSurface();
} }
}else if(guiMode == GuiMode::SOURCE_SELECTION){}
void SurfaceManagerGui::mouseReleased(ofMouseEventArgs& args) { }
stopDrag();
projectionEditor.stopDragJoints(); void SurfaceManagerGui::mouseReleased(ofMouseEventArgs & args){
textureEditor.stopDragJoints(); stopDrag();
projectionEditor.stopDragJoints();
// Check if surface has moved textureEditor.stopDragJoints();
if (surfaceManager->getSelectedSurface()){
if (!surfaceManager->getSelectedSurface()->getMoved()) { // Check if surface has moved
_cmdManager->undo(); if(surfaceManager->getSelectedSurface()){
} if(!surfaceManager->getSelectedSurface()->getMoved()){
} _cmdManager->undo();
}
} }
void SurfaceManagerGui::mouseDragged(ofMouseEventArgs& args) { }
if (bDrag) {
ofVec2f mousePosition = ofVec2f(args.x, args.y); void SurfaceManagerGui::mouseDragged(ofMouseEventArgs & args){
ofVec2f distance = mousePosition - clickPosition; if(bDrag){
ofVec2f mousePosition = ofVec2f(args.x, args.y);
if (guiMode == GuiMode::PROJECTION_MAPPING) { ofVec2f distance = mousePosition - clickPosition;
// add this distance to all vertices in surface
projectionEditor.moveSelectedSurface(distance); if(guiMode == GuiMode::PROJECTION_MAPPING){
} else if (guiMode == GuiMode::TEXTURE_MAPPING) { // add this distance to all vertices in surface
textureEditor.moveTexCoords(distance); projectionEditor.moveSelectedSurface(distance);
} }else if(guiMode == GuiMode::TEXTURE_MAPPING){
clickPosition = mousePosition; textureEditor.moveTexCoords(distance);
} }
} clickPosition = mousePosition;
}
void SurfaceManagerGui::setSurfaceManager(SurfaceManager* newSurfaceManager) { }
surfaceManager = newSurfaceManager;
projectionEditor.setSurfaceManager(surfaceManager); void SurfaceManagerGui::setSurfaceManager(SurfaceManager * newSurfaceManager){
sourcesEditor.setSurfaceManager(surfaceManager); surfaceManager = newSurfaceManager;
} projectionEditor.setSurfaceManager(surfaceManager);
sourcesEditor.setSurfaceManager(surfaceManager);
// Set external media server so we can access it from wherever we need }
void SurfaceManagerGui::setMediaServer(MediaServer* newMediaServer) {
mediaServer = newMediaServer; // Set external media server so we can access it from wherever we need
// Set the media server of the sources editor here void SurfaceManagerGui::setMediaServer(MediaServer * newMediaServer){
sourcesEditor.setMediaServer(mediaServer); mediaServer = newMediaServer;
} // Set the media server of the sources editor here
sourcesEditor.setMediaServer(mediaServer);
void SurfaceManagerGui::setCmdManager(CmdManager * cmdManager){ }
_cmdManager = cmdManager;
sourcesEditor.setCmdManager(_cmdManager); void SurfaceManagerGui::setCmdManager(CmdManager * cmdManager){
} _cmdManager = cmdManager;
sourcesEditor.setCmdManager(_cmdManager);
void SurfaceManagerGui::setMode(int newGuiMode) { }
if (newGuiMode != GuiMode::NONE && newGuiMode != GuiMode::TEXTURE_MAPPING &&
newGuiMode != GuiMode::PROJECTION_MAPPING && void SurfaceManagerGui::setMode(int newGuiMode){
newGuiMode != GuiMode::SOURCE_SELECTION) { if(newGuiMode != GuiMode::NONE && newGuiMode != GuiMode::TEXTURE_MAPPING &&
throw std::runtime_error("Trying to set invalid mode."); newGuiMode != GuiMode::PROJECTION_MAPPING &&
} newGuiMode != GuiMode::SOURCE_SELECTION){
throw std::runtime_error("Trying to set invalid mode.");
if (newGuiMode == GuiMode::NONE) { }
ofHideCursor();
} else { if(newGuiMode == GuiMode::NONE){
ofShowCursor(); ofHideCursor();
} }else{
ofShowCursor();
guiMode = newGuiMode; }
if (guiMode == GuiMode::SOURCE_SELECTION) { guiMode = newGuiMode;
sourcesEditor.enable();
//string sourceName = surfaceManager->getSelectedSurfaceSourceName(); if(guiMode == GuiMode::SOURCE_SELECTION){
//sourcesEditor.selectImageSourceRadioButton(sourceName); sourcesEditor.enable();
} else { //string sourceName = surfaceManager->getSelectedSurfaceSourceName();
sourcesEditor.disable(); //sourcesEditor.selectImageSourceRadioButton(sourceName);
} }else{
sourcesEditor.disable();
if (guiMode == GuiMode::TEXTURE_MAPPING) { }
textureEditor.enable();
// refresh texture editor surface reference if(guiMode == GuiMode::TEXTURE_MAPPING){
textureEditor.setSurface(surfaceManager->getSelectedSurface()); textureEditor.enable();
} else { // refresh texture editor surface reference
textureEditor.disable(); textureEditor.setSurface(surfaceManager->getSelectedSurface());
} }else{
textureEditor.disable();
if (guiMode == GuiMode::PROJECTION_MAPPING) { }
projectionEditor.enable();
} else { if(guiMode == GuiMode::PROJECTION_MAPPING){
projectionEditor.disable(); projectionEditor.enable();
} }else{
} projectionEditor.disable();
}
int SurfaceManagerGui::getMode(){ }
return guiMode;
} int SurfaceManagerGui::getMode(){
return guiMode;
void SurfaceManagerGui::drawSelectedSurfaceHighlight() { }
if (surfaceManager->getSelectedSurface() == NULL) return;
ofPolyline line = surfaceManager->getSelectedSurface()->getHitArea(); void SurfaceManagerGui::drawSelectedSurfaceHighlight(){
ofPushStyle(); if(surfaceManager->getSelectedSurface() == NULL){
ofSetLineWidth(1); return;
ofSetColor(255, 255, 255, 255); }
line.draw(); ofPolyline line = surfaceManager->getSelectedSurface()->getHitArea();
ofPopStyle(); ofPushStyle();
} ofSetLineWidth(1);
ofSetColor(255, 255, 255, 255);
void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight() { line.draw();
if (surfaceManager->getSelectedSurface() == NULL) return; ofPopStyle();
ofPolyline line = surfaceManager->getSelectedSurface()->getTextureHitArea(); }
ofPushStyle();
ofSetLineWidth(1); void SurfaceManagerGui::drawSelectedSurfaceTextureHighlight(){
ofSetColor(255, 255, 0, 255); if(surfaceManager->getSelectedSurface() == NULL){
line.draw(); return;
ofPopStyle(); }
} ofPolyline line = surfaceManager->getSelectedSurface()->getTextureHitArea();
ofPushStyle();
void SurfaceManagerGui::startDrag() { bDrag = true; } ofSetLineWidth(1);
ofSetColor(255, 255, 0, 255);
void SurfaceManagerGui::stopDrag() { bDrag = false; } line.draw();
} ofPopStyle();
} }
void SurfaceManagerGui::startDrag(){
bDrag = true;
}
void SurfaceManagerGui::stopDrag(){
bDrag = false;
}
} // namespace piMapper
} // namespace ofx

80
src/Surfaces/SurfaceManagerGui.h

@ -18,41 +18,45 @@
#include "MvTexCoordCmd.h" #include "MvTexCoordCmd.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class SurfaceManagerGui {
public: class SurfaceManagerGui {
SurfaceManagerGui();
~SurfaceManagerGui(); public:
SurfaceManagerGui();
void registerMouseEvents(); ~SurfaceManagerGui();
void unregisterMouseEvents();
void registerMouseEvents();
void draw(); void unregisterMouseEvents();
void mousePressed(ofMouseEventArgs& args);
void mouseReleased(ofMouseEventArgs& args); void draw();
void mouseDragged(ofMouseEventArgs& args); void mousePressed(ofMouseEventArgs & args);
void mouseReleased(ofMouseEventArgs & args);
void setSurfaceManager(SurfaceManager* newSurfaceManager); void mouseDragged(ofMouseEventArgs & args);
void setMediaServer(MediaServer* newMediaServer);
void setCmdManager(CmdManager * cmdManager); void setSurfaceManager(SurfaceManager * newSurfaceManager);
void setMediaServer(MediaServer * newMediaServer);
void setMode(int newGuiMode); void setCmdManager(CmdManager * cmdManager);
int getMode();
void drawSelectedSurfaceHighlight(); void setMode(int newGuiMode);
void drawSelectedSurfaceTextureHighlight(); int getMode();
void startDrag(); void drawSelectedSurfaceHighlight();
void stopDrag(); void drawSelectedSurfaceTextureHighlight();
void startDrag();
private: void stopDrag();
SurfaceManager* surfaceManager;
MediaServer* mediaServer; private:
TextureEditor textureEditor; SurfaceManager * surfaceManager;
ProjectionEditor projectionEditor; MediaServer * mediaServer;
SourcesEditor sourcesEditor; TextureEditor textureEditor;
int guiMode; ProjectionEditor projectionEditor;
bool bDrag; SourcesEditor sourcesEditor;
ofVec2f clickPosition; int guiMode;
CmdManager * _cmdManager; bool bDrag;
}; ofVec2f clickPosition;
} CmdManager * _cmdManager;
}
};
} // namespace piMapper
} // namespace ofx

16
src/Surfaces/SurfaceType.h

@ -1,9 +1,13 @@
#pragma once #pragma once
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
struct SurfaceType {
enum { TRIANGLE_SURFACE, QUAD_SURFACE }; struct SurfaceType {
}; enum {
} TRIANGLE_SURFACE, QUAD_SURFACE
} };
};
} // namespace piMapper
} // namespace ofx

204
src/Surfaces/TriangleSurface.cpp

@ -2,141 +2,147 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
TriangleSurface::TriangleSurface() {
setup(); TriangleSurface::TriangleSurface(){
setup();
} }
TriangleSurface::~TriangleSurface() {} TriangleSurface::~TriangleSurface(){}
void TriangleSurface::setup() { void TriangleSurface::setup(){
// Create 3 points for the triangle // Create 3 points for the triangle
ofVec2f p1 = ofVec2f(ofGetWidth() / 2.0f, 0); ofVec2f p1 = ofVec2f(ofGetWidth() / 2.0f, 0);
ofVec2f p2 = ofVec2f(ofVec2f(0, ofGetHeight())); ofVec2f p2 = ofVec2f(ofVec2f(0, ofGetHeight()));
ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight()); ofVec2f p3 = ofVec2f(ofGetWidth(), ofGetHeight());
// Create 3 point for the texture coordinates // Create 3 point for the texture coordinates
ofVec2f t1 = ofVec2f(0.5f, 0); ofVec2f t1 = ofVec2f(0.5f, 0);
ofVec2f t2 = ofVec2f(0, 1.0f); ofVec2f t2 = ofVec2f(0, 1.0f);
ofVec2f t3 = ofVec2f(1, 1.0f); ofVec2f t3 = ofVec2f(1, 1.0f);
setup(p1, p2, p3, t1, t2, t3, source); setup(p1, p2, p3, t1, t2, t3, source);
} }
void TriangleSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, void TriangleSurface::setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1,
ofVec2f t2, ofVec2f t3, BaseSource* newSource) { ofVec2f t2, ofVec2f t3, BaseSource * newSource){
// Assign texture // Assign texture
source = newSource; source = newSource;
// Clear mesh // Clear mesh
mesh.clear(); mesh.clear();
// Create a surface with the points // Create a surface with the points
mesh.addVertex(p1); mesh.addVertex(p1);
mesh.addVertex(p2); mesh.addVertex(p2);
mesh.addVertex(p3); mesh.addVertex(p3);
// Add texture coordinates // Add texture coordinates
mesh.addTexCoord(t1); mesh.addTexCoord(t1);
mesh.addTexCoord(t2); mesh.addTexCoord(t2);
mesh.addTexCoord(t3); mesh.addTexCoord(t3);
} }
void TriangleSurface::draw() { void TriangleSurface::draw(){
if (source->getTexture() == NULL) { if(source->getTexture() == NULL){
ofLogWarning("TriangleSurface") << "Source texture is empty. Not drawing."; ofLogWarning("TriangleSurface") << "Source texture is empty. Not drawing.";
return; return;
} }
source->getTexture()->bind(); source->getTexture()->bind();
mesh.draw(); mesh.draw();
source->getTexture()->unbind(); source->getTexture()->unbind();
} }
void TriangleSurface::setVertex(int index, ofVec2f p) { void TriangleSurface::setVertex(int index, ofVec2f p){
if (index > 2) { if(index > 2){
ofLog() << "Vertex with this index does not exist: " << index << endl; ofLog() << "Vertex with this index does not exist: " << index << endl;
return; return;
} }
mesh.setVertex(index, p); mesh.setVertex(index, p);
} }
void TriangleSurface::setTexCoord(int index, ofVec2f t) { void TriangleSurface::setTexCoord(int index, ofVec2f t){
if (index > 2) { if(index > 2){
ofLog() << "Texture coordinate with this index does not exist: " << index ofLog() << "Texture coordinate with this index does not exist: " << index
<< endl; << endl;
return; return;
} }
mesh.setTexCoord(index, t); mesh.setTexCoord(index, t);
} }
void TriangleSurface::moveBy(ofVec2f v) { void TriangleSurface::moveBy(ofVec2f v){
vector<ofVec3f>& vertices = getVertices(); vector <ofVec3f> & vertices = getVertices();
for (int i = 0; i < vertices.size(); i++) { for(int i = 0; i < vertices.size(); i++){
vertices[i] += v; vertices[i] += v;
} }
setMoved(true); setMoved(true);
} }
int TriangleSurface::getType() { return SurfaceType::TRIANGLE_SURFACE; } int TriangleSurface::getType(){
return SurfaceType::TRIANGLE_SURFACE;
}
bool TriangleSurface::hitTest(ofVec2f p) { bool TriangleSurface::hitTest(ofVec2f p){
// Construct ofPolyline from vertices // Construct ofPolyline from vertices
ofPolyline line = getHitArea(); ofPolyline line = getHitArea();
if (line.inside(p.x, p.y)) { if(line.inside(p.x, p.y)){
return true; return true;
} else { }else{
return false; return false;
} }
} }
ofVec2f TriangleSurface::getVertex(int index) { ofVec2f TriangleSurface::getVertex(int index){
if (index > 2) { if(index > 2){
ofLog() << "Vertex with this index does not exist: " << index << endl; ofLog() << "Vertex with this index does not exist: " << index << endl;
throw std::runtime_error("Vertex index out of bounds."); throw std::runtime_error("Vertex index out of bounds.");
} }
ofVec3f vert = mesh.getVertex(index); ofVec3f vert = mesh.getVertex(index);
return ofVec2f(vert.x, vert.y); return ofVec2f(vert.x, vert.y);
} }
ofVec2f TriangleSurface::getTexCoord(int index) { ofVec2f TriangleSurface::getTexCoord(int index){
if (index > 2) { if(index > 2){
throw std::runtime_error("Texture coordinate index out of bounds."); throw std::runtime_error("Texture coordinate index out of bounds.");
} }
return mesh.getTexCoord(index); return mesh.getTexCoord(index);
} }
ofPolyline TriangleSurface::getHitArea() { ofPolyline TriangleSurface::getHitArea(){
ofPolyline line; ofPolyline line;
line.addVertex(ofPoint(mesh.getVertex(0).x, mesh.getVertex(0).y)); line.addVertex(ofPoint(mesh.getVertex(0).x, mesh.getVertex(0).y));
line.addVertex(ofPoint(mesh.getVertex(1).x, mesh.getVertex(1).y)); line.addVertex(ofPoint(mesh.getVertex(1).x, mesh.getVertex(1).y));
line.addVertex(ofPoint(mesh.getVertex(2).x, mesh.getVertex(2).y)); line.addVertex(ofPoint(mesh.getVertex(2).x, mesh.getVertex(2).y));
line.close(); line.close();
return line; return line;
} }
ofPolyline TriangleSurface::getTextureHitArea() { ofPolyline TriangleSurface::getTextureHitArea(){
ofPolyline line; ofPolyline line;
vector<ofVec2f>& texCoords = mesh.getTexCoords(); vector <ofVec2f> & texCoords = mesh.getTexCoords();
ofVec2f textureSize = ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight()); ofVec2f textureSize = ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight());
for (int i = 0; i < texCoords.size(); i++) { for(int i = 0; i < texCoords.size(); i++){
line.addVertex(ofPoint(texCoords[i] * textureSize)); line.addVertex(ofPoint(texCoords[i] * textureSize));
} }
line.close(); line.close();
return line; return line;
} }
vector<ofVec3f>& TriangleSurface::getVertices() { vector <ofVec3f> & TriangleSurface::getVertices(){
// return only joint vertices // return only joint vertices
return mesh.getVertices(); return mesh.getVertices();
} }
vector<ofVec2f>& TriangleSurface::getTexCoords() { return mesh.getTexCoords(); } vector <ofVec2f> & TriangleSurface::getTexCoords(){
return mesh.getTexCoords();
} }
}
} // namespace piMapper
} // namespace ofx

42
src/Surfaces/TriangleSurface.h

@ -6,27 +6,29 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class TriangleSurface : public BaseSurface { class TriangleSurface : public BaseSurface {
public: public:
TriangleSurface(); TriangleSurface();
~TriangleSurface(); ~TriangleSurface();
void setup(); void setup();
void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2, void setup(ofVec2f p1, ofVec2f p2, ofVec2f p3, ofVec2f t1, ofVec2f t2,
ofVec2f t3, BaseSource* newSource); ofVec2f t3, BaseSource * newSource);
void draw(); void draw();
void setVertex(int index, ofVec2f p); void setVertex(int index, ofVec2f p);
void setTexCoord(int index, ofVec2f t); void setTexCoord(int index, ofVec2f t);
void moveBy(ofVec2f v); void moveBy(ofVec2f v);
int getType(); int getType();
bool hitTest(ofVec2f p); bool hitTest(ofVec2f p);
ofVec2f getVertex(int index); ofVec2f getVertex(int index);
ofVec2f getTexCoord(int index); ofVec2f getTexCoord(int index);
ofPolyline getHitArea(); ofPolyline getHitArea();
ofPolyline getTextureHitArea(); ofPolyline getTextureHitArea();
vector<ofVec3f>& getVertices(); vector <ofVec3f> & getVertices();
vector<ofVec2f>& getTexCoords(); vector <ofVec2f> & getTexCoords();
}; };
}
} } // namespace piMapper
} // namespace ofx

107
src/UserInterface/BaseJoint.cpp

@ -3,70 +3,89 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
BaseJoint::BaseJoint() { BaseJoint::BaseJoint(){
setDefaultColors(); setDefaultColors();
setDefaultProperties(); setDefaultProperties();
registerMouseEvents(); registerMouseEvents();
} }
BaseJoint::~BaseJoint() { unregisterMouseEvents(); } BaseJoint::~BaseJoint(){
unregisterMouseEvents();
void BaseJoint::registerMouseEvents() {
ofAddListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed);
ofAddListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged);
} }
void BaseJoint::unregisterMouseEvents() { void BaseJoint::registerMouseEvents(){
ofRemoveListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed); ofAddListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed);
ofRemoveListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged); ofAddListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged);
} }
void BaseJoint::mousePressed(ofMouseEventArgs& args) { void BaseJoint::unregisterMouseEvents(){
if (hitTest(ofVec2f(args.x, args.y))) { ofRemoveListener(ofEvents().mousePressed, this, &BaseJoint::mousePressed);
// selected = true; ofRemoveListener(ofEvents().mouseDragged, this, &BaseJoint::mouseDragged);
clickDistance = position - ofVec2f(args.x, args.y);
// startDrag();
}
} }
void BaseJoint::mouseReleased(int x, int y, int button) { stopDrag(); } void BaseJoint::mousePressed(ofMouseEventArgs & args){
if(hitTest(ofVec2f(args.x, args.y))){
// selected = true;
clickDistance = position - ofVec2f(args.x, args.y);
// startDrag();
}
}
void BaseJoint::mouseDragged(ofMouseEventArgs& args) { void BaseJoint::mouseReleased(int x, int y, int button){
if (!bDrag) return; stopDrag();
position = ofVec2f(args.x, args.y) + clickDistance;
} }
void BaseJoint::startDrag() { bDrag = true; } void BaseJoint::mouseDragged(ofMouseEventArgs & args){
if(!bDrag){
return;
}
position = ofVec2f(args.x, args.y) + clickDistance;
}
void BaseJoint::stopDrag() { bDrag = false; } void BaseJoint::startDrag(){
bDrag = true;
}
void BaseJoint::select() { selected = true; } void BaseJoint::stopDrag(){
bDrag = false;
}
void BaseJoint::unselect() { selected = false; } void BaseJoint::select(){
selected = true;
}
void BaseJoint::setClickDistance(ofVec2f newClickDistance) { void BaseJoint::unselect(){
clickDistance = newClickDistance; selected = false;
} }
bool BaseJoint::isDragged() { return bDrag; } void BaseJoint::setClickDistance(ofVec2f newClickDistance){
clickDistance = newClickDistance;
}
bool BaseJoint::isSelected() { return selected; } bool BaseJoint::isDragged(){
return bDrag;
}
void BaseJoint::setDefaultColors() { bool BaseJoint::isSelected(){
fillColor = ofColor(0, 255, 255, 0); return selected;
strokeColor = ofColor(255, 255, 255);
fillColorSelected = ofColor(255, 255, 0, 0);
strokeColorSelected = ofColor(255, 0, 0);
} }
void BaseJoint::setDefaultProperties() { void BaseJoint::setDefaultColors(){
enabled = true; fillColor = ofColor(0, 255, 255, 0);
visible = true; strokeColor = ofColor(255, 255, 255);
position = ofVec2f(20.0f, 20.0f); fillColorSelected = ofColor(255, 255, 0, 0);
clickDistance = ofVec2f(0.0f, 0.0f); strokeColorSelected = ofColor(255, 0, 0);
bDrag = false;
selected = false;
strokeWidth = 1.5f;
} }
void BaseJoint::setDefaultProperties(){
enabled = true;
visible = true;
position = ofVec2f(20.0f, 20.0f);
clickDistance = ofVec2f(0.0f, 0.0f);
bDrag = false;
selected = false;
strokeWidth = 1.5f;
} }
}
} // namespace piMapper
} // namespace ofx

87
src/UserInterface/BaseJoint.h

@ -6,46 +6,49 @@ namespace ofx {
namespace piMapper { namespace piMapper {
class BaseJoint { class BaseJoint {
public:
BaseJoint(); public:
~BaseJoint(); BaseJoint();
~BaseJoint();
void registerMouseEvents();
void unregisterMouseEvents(); void registerMouseEvents();
void unregisterMouseEvents();
ofVec2f position;
bool enabled; ofVec2f position;
bool visible; bool enabled;
bool selected; bool visible;
bool selected;
void mousePressed(ofMouseEventArgs& args);
void mouseReleased(int x, int y, int button); void mousePressed(ofMouseEventArgs & args);
void mouseDragged(ofMouseEventArgs& args); void mouseReleased(int x, int y, int button);
void startDrag(); void mouseDragged(ofMouseEventArgs & args);
void stopDrag(); void startDrag();
void select(); void stopDrag();
void unselect(); void select();
void setClickDistance(ofVec2f newClickDistance); void unselect();
bool isDragged(); void setClickDistance(ofVec2f newClickDistance);
bool isSelected(); bool isDragged();
bool isSelected();
virtual void update() {};
virtual void draw() {}; virtual void update(){}
virtual bool hitTest(ofVec2f position) {}; virtual void draw(){}
virtual bool hitTest(ofVec2f position){}
protected:
ofColor fillColor; protected:
ofColor strokeColor; ofColor fillColor;
ofColor fillColorSelected; ofColor strokeColor;
ofColor strokeColorSelected; ofColor fillColorSelected;
float strokeWidth; ofColor strokeColorSelected;
ofVec2f clickDistance; // How far from the center of the joint the user has float strokeWidth;
// clicked? ofVec2f clickDistance; // How far from the center of the joint the user has
bool bDrag; // clicked?
bool bDrag;
private:
void setDefaultColors(); private:
void setDefaultProperties(); void setDefaultColors();
void setDefaultProperties();
}; };
}
} } // namespace piMapper
} // namespace ofx

86
src/UserInterface/CircleJoint.cpp

@ -3,47 +3,59 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
CircleJoint::CircleJoint() { setDefaultProperties(); } CircleJoint::CircleJoint(){
setDefaultProperties();
void CircleJoint::update() {
if (!enabled) return;
} }
void CircleJoint::draw() { void CircleJoint::update(){
if (!visible) return; if(!enabled){
if (!enabled) return; return;
}
ofPushStyle();
ofFill();
if (selected) {
ofSetColor(fillColorSelected);
} else {
ofSetColor(fillColor);
}
ofCircle(position.x, position.y, radius);
ofNoFill();
if (selected) {
ofSetColor(strokeColorSelected);
} else {
ofSetColor(strokeColor);
}
ofSetLineWidth(strokeWidth);
ofCircle(position.x, position.y, radius);
ofPopStyle();
} }
void CircleJoint::setDefaultProperties() { radius = 10.0f; } void CircleJoint::draw(){
if(!visible){
return;
}
if(!enabled){
return;
}
ofPushStyle();
ofFill();
if(selected){
ofSetColor(fillColorSelected);
}else{
ofSetColor(fillColor);
}
ofCircle(position.x, position.y, radius);
ofNoFill();
if(selected){
ofSetColor(strokeColorSelected);
}else{
ofSetColor(strokeColor);
}
ofSetLineWidth(strokeWidth);
ofCircle(position.x, position.y, radius);
ofPopStyle();
}
bool CircleJoint::hitTest(ofVec2f pos) { void CircleJoint::setDefaultProperties(){
float distance = position.distance(pos); radius = 10.0f;
if (distance < radius)
return true;
else
return false;
} }
bool CircleJoint::hitTest(ofVec2f pos){
float distance = position.distance(pos);
if(distance < radius){
return true;
}else{
return false;
}
} }
}
} // namespace piMapper
} // namespace ofx

23
src/UserInterface/CircleJoint.h

@ -6,17 +6,20 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class CircleJoint : public BaseJoint { class CircleJoint : public BaseJoint {
public:
CircleJoint();
void update(); public:
void draw(); CircleJoint();
bool hitTest(ofVec2f position);
private: void update();
float radius; void draw();
bool hitTest(ofVec2f position);
private:
float radius;
void setDefaultProperties();
void setDefaultProperties();
}; };
}
} } // namespace piMapper
} // namespace ofx

10
src/UserInterface/EditorType.h

@ -2,8 +2,12 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
struct EditorType { struct EditorType {
enum { TEXTURE, PROJECTION }; enum {
TEXTURE, PROJECTION
};
}; };
}
} } // namespace piMapper
} // namespace ofx

10
src/UserInterface/GuiMode.h

@ -2,8 +2,12 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
struct GuiMode { struct GuiMode {
enum { NONE, TEXTURE_MAPPING, PROJECTION_MAPPING, SOURCE_SELECTION }; enum {
NONE, TEXTURE_MAPPING, PROJECTION_MAPPING, SOURCE_SELECTION
};
}; };
}
} } // namespace piMapper
} // namespace ofx

553
src/UserInterface/ProjectionEditor.cpp

@ -1,270 +1,289 @@
#include "ProjectionEditor.h" #include "ProjectionEditor.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
ProjectionEditor::ProjectionEditor() {
surfaceManager = NULL; ProjectionEditor::ProjectionEditor(){
bShiftKeyDown = false; surfaceManager = NULL;
fSnapDistance = 10.0f; bShiftKeyDown = false;
enable(); fSnapDistance = 10.0f;
} enable();
}
ProjectionEditor::~ProjectionEditor() {
clearJoints(); ProjectionEditor::~ProjectionEditor(){
surfaceManager = NULL; clearJoints();
disable(); surfaceManager = NULL;
} disable();
}
void ProjectionEditor::registerAppEvents() {
ofAddListener(ofEvents().update, this, &ProjectionEditor::update); void ProjectionEditor::registerAppEvents(){
ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage); ofAddListener(ofEvents().update, this, &ProjectionEditor::update);
} ofAddListener(ofEvents().messageEvent, this, &ProjectionEditor::gotMessage);
}
void ProjectionEditor::unregisterAppEvents() {
ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update); void ProjectionEditor::unregisterAppEvents(){
ofRemoveListener(ofEvents().messageEvent, this, ofRemoveListener(ofEvents().update, this, &ProjectionEditor::update);
&ProjectionEditor::gotMessage); ofRemoveListener(ofEvents().messageEvent, this,
} &ProjectionEditor::gotMessage);
}
void ProjectionEditor::registerMouseEvents() {
ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged); void ProjectionEditor::registerMouseEvents(){
} ofAddListener(ofEvents().mouseDragged, this, &ProjectionEditor::mouseDragged);
}
void ProjectionEditor::unregisterMouseEvents() {
ofRemoveListener(ofEvents().mouseDragged, this, void ProjectionEditor::unregisterMouseEvents(){
&ProjectionEditor::mouseDragged); ofRemoveListener(ofEvents().mouseDragged, this,
} &ProjectionEditor::mouseDragged);
}
void ProjectionEditor::registerKeyEvents() {
ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); void ProjectionEditor::registerKeyEvents(){
ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased); ofAddListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed);
} ofAddListener(ofEvents().keyReleased, this, &ProjectionEditor::keyReleased);
}
void ProjectionEditor::unregisterKeyEvents() {
ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed); void ProjectionEditor::unregisterKeyEvents(){
ofRemoveListener(ofEvents().keyReleased, this, ofRemoveListener(ofEvents().keyPressed, this, &ProjectionEditor::keyPressed);
&ProjectionEditor::keyReleased); ofRemoveListener(ofEvents().keyReleased, this,
} &ProjectionEditor::keyReleased);
}
void ProjectionEditor::enable() {
registerAppEvents(); void ProjectionEditor::enable(){
registerMouseEvents(); registerAppEvents();
registerKeyEvents(); registerMouseEvents();
} registerKeyEvents();
}
void ProjectionEditor::disable() {
unregisterAppEvents(); void ProjectionEditor::disable(){
unregisterMouseEvents(); unregisterAppEvents();
unregisterKeyEvents(); unregisterMouseEvents();
} unregisterKeyEvents();
}
void ProjectionEditor::update(ofEventArgs& args) {
// update surface if one of the joints is being dragged void ProjectionEditor::update(ofEventArgs & args){
for (int i = 0; i < joints.size(); i++) { // update surface if one of the joints is being dragged
if (joints[i]->isDragged() || joints[i]->isSelected()) { for(int i = 0; i < joints.size(); i++){
if (surfaceManager->getSelectedSurface() != NULL) { if(joints[i]->isDragged() || joints[i]->isSelected()){
// update vertex to new location if(surfaceManager->getSelectedSurface() != NULL){
surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position); // update vertex to new location
} else { surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position);
// clear joints if there is no surface selected }else{
// as the remove selected surface in the surface manager // clear joints if there is no surface selected
// is not supposed to access joints here // as the remove selected surface in the surface manager
joints.clear(); // is not supposed to access joints here
} joints.clear();
break; }
} break;
} }
} }
}
void ProjectionEditor::draw() {
if (surfaceManager == NULL) return; void ProjectionEditor::draw(){
if (surfaceManager->getSelectedSurface() == NULL) return; if(surfaceManager == NULL){
if (joints.size() <= 0) createJoints(); return;
drawJoints(); }
} if(surfaceManager->getSelectedSurface() == NULL){
return;
void ProjectionEditor::mouseDragged(ofMouseEventArgs& args) { }
ofVec2f mousePosition = ofVec2f(args.x, args.y); if(joints.size() <= 0){
createJoints();
// Collect all vertices of the projection surfaces }
vector<ofVec3f*> allVertices; drawJoints();
for (int i = 0; i < surfaceManager->size(); i++) { }
BaseSurface* surface = surfaceManager->getSurface(i);
if (surface == surfaceManager->getSelectedSurface()) { void ProjectionEditor::mouseDragged(ofMouseEventArgs & args){
continue; // Don't add vertices of selected surface ofVec2f mousePosition = ofVec2f(args.x, args.y);
}
for (int j = 0; j < surface->getVertices().size(); j++) { // Collect all vertices of the projection surfaces
allVertices.push_back(&surface->getVertices()[j]); vector <ofVec3f *> allVertices;
} for(int i = 0; i < surfaceManager->size(); i++){
} BaseSurface * surface = surfaceManager->getSurface(i);
if(surface == surfaceManager->getSelectedSurface()){
// Snap currently dragged joint to nearest vertex continue; // Don't add vertices of selected surface
for (int i = 0; i < joints.size(); i++) { }
if (joints[i]->isDragged()) { for(int j = 0; j < surface->getVertices().size(); j++){
// Snap it! allVertices.push_back(&surface->getVertices()[j]);
for (int j = 0; j < allVertices.size(); j++) { }
float distance = mousePosition.distance(*allVertices[j]); }
// cout << "distance: " << distance << endl;
if (distance < fSnapDistance) { // Snap currently dragged joint to nearest vertex
joints[i]->position = *allVertices[j]; for(int i = 0; i < joints.size(); i++){
ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y); if(joints[i]->isDragged()){
joints[i]->setClickDistance(clickDistance); // Snap it!
break; for(int j = 0; j < allVertices.size(); j++){
} float distance = mousePosition.distance(*allVertices[j]);
} // cout << "distance: " << distance << endl;
} if(distance < fSnapDistance){
} joints[i]->position = *allVertices[j];
} ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y);
joints[i]->setClickDistance(clickDistance);
void ProjectionEditor::keyPressed(ofKeyEventArgs& args) { break;
int key = args.key; }
float moveStep; }
}
if (bShiftKeyDown) }
moveStep = 10.0f; }
else
moveStep = 0.5f; void ProjectionEditor::keyPressed(ofKeyEventArgs & args){
int key = args.key;
switch (key) { float moveStep;
case OF_KEY_LEFT:
moveSelection(ofVec2f(-moveStep, 0.0f)); if(bShiftKeyDown){
break; moveStep = 10.0f;
case OF_KEY_RIGHT: }else{
moveSelection(ofVec2f(moveStep, 0.0f)); moveStep = 0.5f;
break; }
case OF_KEY_UP:
moveSelection(ofVec2f(0.0f, -moveStep)); switch(key){
break; case OF_KEY_LEFT:
case OF_KEY_DOWN: moveSelection(ofVec2f(-moveStep, 0.0f));
moveSelection(ofVec2f(0.0f, moveStep)); break;
break;
case OF_KEY_SHIFT: case OF_KEY_RIGHT:
bShiftKeyDown = true; moveSelection(ofVec2f(moveStep, 0.0f));
break; break;
}
} case OF_KEY_UP:
moveSelection(ofVec2f(0.0f, -moveStep));
void ProjectionEditor::keyReleased(ofKeyEventArgs& args) { break;
int key = args.key;
switch (key) { case OF_KEY_DOWN:
case OF_KEY_SHIFT: moveSelection(ofVec2f(0.0f, moveStep));
bShiftKeyDown = false; break;
break;
} case OF_KEY_SHIFT:
} bShiftKeyDown = true;
break;
void ProjectionEditor::gotMessage(ofMessage& msg) { }
if (msg.message == "surfaceSelected") { }
// refresh gui
clearJoints(); void ProjectionEditor::keyReleased(ofKeyEventArgs & args){
createJoints(); int key = args.key;
} switch(key){
} case OF_KEY_SHIFT:
bShiftKeyDown = false;
void ProjectionEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) { break;
surfaceManager = newSurfaceManager; }
} }
void ProjectionEditor::clearJoints() { void ProjectionEditor::gotMessage(ofMessage & msg){
while (joints.size()) { if(msg.message == "surfaceSelected"){
delete joints.back(); // refresh gui
joints.pop_back(); clearJoints();
} createJoints();
} }
}
void ProjectionEditor::createJoints() {
if (surfaceManager == NULL) return; void ProjectionEditor::setSurfaceManager(SurfaceManager * newSurfaceManager){
clearJoints(); surfaceManager = newSurfaceManager;
}
if (surfaceManager->getSelectedSurface() == NULL) {
ofLog(OF_LOG_WARNING, "Trying to create joints while no surface selected."); void ProjectionEditor::clearJoints(){
return; while(joints.size()){
} delete joints.back();
joints.pop_back();
vector<ofVec3f>& vertices = }
surfaceManager->getSelectedSurface()->getVertices(); }
for (int i = 0; i < vertices.size(); i++) { void ProjectionEditor::createJoints(){
joints.push_back(new CircleJoint()); if(surfaceManager == NULL){
joints.back()->position = ofVec2f(vertices[i].x, vertices[i].y); return;
} }
} clearJoints();
void ProjectionEditor::updateJoints() { if(surfaceManager->getSelectedSurface() == NULL){
if (surfaceManager->getSelectedSurface()) { ofLog(OF_LOG_WARNING, "Trying to create joints while no surface selected.");
vector<ofVec3f>& vertices = return;
surfaceManager->getSelectedSurface()->getVertices(); }
for (int i = 0; i < vertices.size(); i++) {
joints[i]->position = ofVec2f(vertices[i].x, vertices[i].y); vector <ofVec3f> & vertices =
} surfaceManager->getSelectedSurface()->getVertices();
}
for(int i = 0; i < vertices.size(); i++){
} joints.push_back(new CircleJoint());
joints.back()->position = ofVec2f(vertices[i].x, vertices[i].y);
void ProjectionEditor::unselectAllJoints() { }
for (int i = 0; i < joints.size(); i++) { }
joints[i]->unselect();
} void ProjectionEditor::updateJoints(){
} if(surfaceManager->getSelectedSurface()){
vector <ofVec3f> & vertices =
void ProjectionEditor::moveSelectedSurface(ofVec2f by) { surfaceManager->getSelectedSurface()->getVertices();
if (surfaceManager == NULL) return; for(int i = 0; i < vertices.size(); i++){
if (surfaceManager->getSelectedSurface() == NULL) return; joints[i]->position = ofVec2f(vertices[i].x, vertices[i].y);
surfaceManager->getSelectedSurface()->moveBy(by); }
/*vector<ofVec3f>& vertices = }
surfaceManager->getSelectedSurface()->getVertices();
for (int i=0; i<vertices.size(); i++) { }
vertices[i] += by;
}*/ void ProjectionEditor::unselectAllJoints(){
updateJoints(); for(int i = 0; i < joints.size(); i++){
} joints[i]->unselect();
}
void ProjectionEditor::stopDragJoints() { }
for (int i = 0; i < joints.size(); i++) {
joints[i]->stopDrag(); void ProjectionEditor::moveSelectedSurface(ofVec2f by){
} if(surfaceManager == NULL){
} return;
}
void ProjectionEditor::moveSelection(ofVec2f by) { if(surfaceManager->getSelectedSurface() == NULL){
// check if joints selected return;
bool bJointSelected = false; }
BaseJoint* selectedJoint; surfaceManager->getSelectedSurface()->moveBy(by);
for (int i = 0; i < joints.size(); i++) { /*vector<ofVec3f>& vertices =
if (joints[i]->isSelected()) { surfaceManager->getSelectedSurface()->getVertices();
bJointSelected = true; for (int i=0; i<vertices.size(); i++) {
selectedJoint = joints[i]; vertices[i] += by;
break; }*/
} updateJoints();
} }
if (bJointSelected) { void ProjectionEditor::stopDragJoints(){
selectedJoint->position += by; for(int i = 0; i < joints.size(); i++){
} else { joints[i]->stopDrag();
moveSelectedSurface(by); }
} }
}
void ProjectionEditor::moveSelection(ofVec2f by){
void ProjectionEditor::setSnapDistance(float newSnapDistance) { // check if joints selected
fSnapDistance = newSnapDistance; bool bJointSelected = false;
} BaseJoint * selectedJoint;
for(int i = 0; i < joints.size(); i++){
CircleJoint* ProjectionEditor::hitTestJoints(ofVec2f pos) { if(joints[i]->isSelected()){
for (int i = 0; i < joints.size(); i++) { bJointSelected = true;
if (joints[i]->hitTest(pos)) { selectedJoint = joints[i];
return joints[i]; break;
} }
} }
return NULL;
} if(bJointSelected){
selectedJoint->position += by;
vector<CircleJoint *> * ProjectionEditor::getJoints(){ }else{
return &joints; moveSelectedSurface(by);
} }
}
void ProjectionEditor::drawJoints() {
for (int i = 0; i < joints.size(); i++) { void ProjectionEditor::setSnapDistance(float newSnapDistance){
joints[i]->draw(); fSnapDistance = newSnapDistance;
} }
}
} CircleJoint * ProjectionEditor::hitTestJoints(ofVec2f pos){
} for(int i = 0; i < joints.size(); i++){
if(joints[i]->hitTest(pos)){
return joints[i];
}
}
return NULL;
}
vector <CircleJoint *> * ProjectionEditor::getJoints(){
return &joints;
}
void ProjectionEditor::drawJoints(){
for(int i = 0; i < joints.size(); i++){
joints[i]->draw();
}
}
} // namespace piMapper
} // namespace ofx

94
src/UserInterface/ProjectionEditor.h

@ -4,48 +4,52 @@
#include "CircleJoint.h" #include "CircleJoint.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class ProjectionEditor {
public: class ProjectionEditor {
ProjectionEditor();
~ProjectionEditor(); public:
ProjectionEditor();
void registerAppEvents(); ~ProjectionEditor();
void unregisterAppEvents();
void registerMouseEvents(); void registerAppEvents();
void unregisterMouseEvents(); void unregisterAppEvents();
void registerKeyEvents(); void registerMouseEvents();
void unregisterKeyEvents(); void unregisterMouseEvents();
void registerKeyEvents();
void enable(); void unregisterKeyEvents();
void disable();
void enable();
void update(ofEventArgs& args); void disable();
void draw();
void mouseDragged(ofMouseEventArgs& args); void update(ofEventArgs & args);
void keyPressed(ofKeyEventArgs& args); void draw();
void keyReleased(ofKeyEventArgs& args); void mouseDragged(ofMouseEventArgs & args);
void gotMessage(ofMessage& msg); void keyPressed(ofKeyEventArgs & args);
void setSurfaceManager(SurfaceManager* newSurfaceManager); void keyReleased(ofKeyEventArgs & args);
void clearJoints(); void gotMessage(ofMessage & msg);
void createJoints(); void setSurfaceManager(SurfaceManager * newSurfaceManager);
void updateJoints(); void clearJoints();
void unselectAllJoints(); void createJoints();
void moveSelectedSurface(ofVec2f by); void updateJoints();
void stopDragJoints(); void unselectAllJoints();
void updateVertices(); void moveSelectedSurface(ofVec2f by);
void moveSelection(ofVec2f by); void stopDragJoints();
void setSnapDistance(float newSnapDistance); void updateVertices();
CircleJoint* hitTestJoints(ofVec2f pos); void moveSelection(ofVec2f by);
vector<CircleJoint *> * getJoints(); void setSnapDistance(float newSnapDistance);
CircleJoint * hitTestJoints(ofVec2f pos);
private: vector <CircleJoint *> * getJoints();
SurfaceManager* surfaceManager;
vector<CircleJoint*> joints; private:
bool bShiftKeyDown; SurfaceManager * surfaceManager;
float fSnapDistance; vector <CircleJoint *> joints;
bool bShiftKeyDown;
void drawJoints(); float fSnapDistance;
};
} void drawJoints();
}
};
} // namespace piMapper
} // namespace ofx

394
src/UserInterface/RadioList.cpp

@ -1,199 +1,201 @@
#include "RadioList.h" #include "RadioList.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
RadioList::RadioList(){
storedTitle = ""; RadioList::RadioList(){
storedSelectedItem = 0; storedTitle = "";
} storedSelectedItem = 0;
}
RadioList::RadioList(vector<string> & labels, vector<string> & values){
RadioList(); RadioList::RadioList(vector <string> & labels, vector <string> & values){
setup(labels, values); RadioList();
} setup(labels, values);
}
RadioList::RadioList(string title, vector<string> & labels, vector<string> & values){
RadioList(); RadioList::RadioList(string title, vector <string> & labels, vector <string> & values){
setup(title, labels, values); RadioList();
} setup(title, labels, values);
}
RadioList::~RadioList(){
clear(); RadioList::~RadioList(){
} clear();
}
void RadioList::setup(vector<string> & labels, vector<string> & values){
void RadioList::setup(vector <string> & labels, vector <string> & values){
// Copy incomming labels for later use
storedLabels = labels; // Copy incomming labels for later use
storedValues = values; storedLabels = labels;
storedValues = values;
// Create toggles with labels from the labels arg
int i; // Create toggles with labels from the labels arg
for(i = 0; i < labels.size(); i++){ int i;
ofxToggle * toggle = new ofxToggle(); for(i = 0; i < labels.size(); i++){
toggle->setup(false); ofxToggle * toggle = new ofxToggle();
toggle->setName(labels[i]); toggle->setup(false);
toggle->addListener(this, &RadioList::onToggleClicked); toggle->setName(labels[i]);
guiGroup.add(toggle); toggle->addListener(this, &RadioList::onToggleClicked);
#if OF_VERSION_MAJOR == 0 && (OF_VERSION_MINOR >= 8 && OF_VERSION_PATCH >= 2) || (OF_VERSION_MINOR >= 9 && OF_VERSION_PATCH >= 0) guiGroup.add(toggle);
toggle->registerMouseEvents(); #if OF_VERSION_MAJOR == 0 && (OF_VERSION_MINOR >= 8 && OF_VERSION_PATCH >= 2) || (OF_VERSION_MINOR >= 9 && OF_VERSION_PATCH >= 0)
#endif toggle->registerMouseEvents();
} #endif
} }
}
void RadioList::setup(string title, vector<string> & labels, vector<string> & values){
void RadioList::setup(string title, vector <string> & labels, vector <string> & values){
// Store title for later use
storedTitle = title; // Store title for later use
guiGroup.setName(title); storedTitle = title;
setup(labels, values); guiGroup.setName(title);
} setup(labels, values);
}
void RadioList::draw(){
guiGroup.draw(); void RadioList::draw(){
} guiGroup.draw();
}
void RadioList::setTitle(string title){
storedTitle = title; void RadioList::setTitle(string title){
guiGroup.setName(title); storedTitle = title;
} guiGroup.setName(title);
}
void RadioList::setPosition(ofPoint p){
guiGroup.setPosition(p); void RadioList::setPosition(ofPoint p){
} guiGroup.setPosition(p);
}
void RadioList::setPosition(float x, float y){
guiGroup.setPosition(x, y); void RadioList::setPosition(float x, float y){
} guiGroup.setPosition(x, y);
}
void RadioList::selectItem(int index){
if(index >= guiGroup.getNumControls()){ void RadioList::selectItem(int index){
return; if(index >= guiGroup.getNumControls()){
} return;
}
unselectAll();
unselectAll();
ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(index));
toggle->removeListener(this, &RadioList::onToggleClicked); ofxToggle * toggle = static_cast <ofxToggle *>(guiGroup.getControl(index));
*toggle = true; // Select the specific radio button toggle->removeListener(this, &RadioList::onToggleClicked);
toggle->addListener(this, &RadioList::onToggleClicked); *toggle = true; // Select the specific radio button
//string name = toggle->getName(); toggle->addListener(this, &RadioList::onToggleClicked);
// Throw event with value that is image path instead of name //string name = toggle->getName();
string value = storedValues[index]; // Throw event with value that is image path instead of name
ofNotifyEvent(onRadioSelected, value, this); string value = storedValues[index];
storedSelectedItem = index; ofNotifyEvent(onRadioSelected, value, this);
} storedSelectedItem = index;
}
bool RadioList::selectItemByValue(std::string itemValue){
if(itemValue == ""){ bool RadioList::selectItemByValue(std::string itemValue){
ofLogNotice("RadioList") << "Item value empty"; if(itemValue == ""){
return false; ofLogNotice("RadioList") << "Item value empty";
} return false;
unselectAll(); }
int itemIndex = -1; unselectAll();
for(int i = 0; i < storedValues.size(); i++){ int itemIndex = -1;
if(itemValue == storedValues[i]){ for(int i = 0; i < storedValues.size(); i++){
itemIndex = i; if(itemValue == storedValues[i]){
break; itemIndex = i;
} break;
} }
if(itemIndex >= 0){ }
ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(itemIndex)); if(itemIndex >= 0){
toggle->removeListener(this, &RadioList::onToggleClicked); ofxToggle * toggle = static_cast <ofxToggle *>(guiGroup.getControl(itemIndex));
*toggle = true; // Select the specific radio button toggle->removeListener(this, &RadioList::onToggleClicked);
toggle->addListener(this, &RadioList::onToggleClicked); *toggle = true; // Select the specific radio button
return true; toggle->addListener(this, &RadioList::onToggleClicked);
} return true;
ofLogNotice("RadioList") << "Item with value " << itemValue << " not found"; }
return false; ofLogNotice("RadioList") << "Item with value " << itemValue << " not found";
} return false;
}
void RadioList::enable(){
if(guiGroup.getNumControls() > 0){ void RadioList::enable(){
clear(); if(guiGroup.getNumControls() > 0){
} clear();
}
// Rebuild everyting
setup(storedTitle, storedLabels, storedValues); // Rebuild everyting
setup(storedTitle, storedLabels, storedValues);
// Select the stored selected item without throwing an event
ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(storedSelectedItem)); // Select the stored selected item without throwing an event
toggle->removeListener(this, &RadioList::onToggleClicked); ofxToggle * toggle = static_cast <ofxToggle *>(guiGroup.getControl(storedSelectedItem));
*toggle = true; toggle->removeListener(this, &RadioList::onToggleClicked);
toggle->addListener(this, &RadioList::onToggleClicked); *toggle = true;
toggle->addListener(this, &RadioList::onToggleClicked);
cout << "num items after enable: " << guiGroup.getNumControls() << endl;
} cout << "num items after enable: " << guiGroup.getNumControls() << endl;
}
void RadioList::disable(){
// Just remove everything void RadioList::disable(){
clear(); // Just remove everything
} clear();
}
void RadioList::clear(){
int i; void RadioList::clear(){
for(i = 0; i < guiGroup.getNumControls(); i++){ int i;
ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(i)); for(i = 0; i < guiGroup.getNumControls(); i++){
toggle->removeListener(this, &RadioList::onToggleClicked); ofxToggle * toggle = static_cast <ofxToggle *>(guiGroup.getControl(i));
delete toggle; toggle->removeListener(this, &RadioList::onToggleClicked);
} delete toggle;
guiGroup.clear(); }
} guiGroup.clear();
}
void RadioList::unselectAll(){
int i; void RadioList::unselectAll(){
for(i = 0; i < guiGroup.getNumControls(); i++){ int i;
ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(i)); for(i = 0; i < guiGroup.getNumControls(); i++){
ofParameter<bool> * paramPtr = static_cast<ofParameter<bool> *>(&toggle->getParameter()); ofxToggle * toggle = static_cast <ofxToggle *>(guiGroup.getControl(i));
toggle->removeListener(this, &RadioList::onToggleClicked); ofParameter <bool> * paramPtr = static_cast <ofParameter <bool> *>(&toggle->getParameter());
*toggle = false; toggle->removeListener(this, &RadioList::onToggleClicked);
toggle->addListener(this, &RadioList::onToggleClicked); *toggle = false;
} toggle->addListener(this, &RadioList::onToggleClicked);
} }
}
ofPoint RadioList::getPosition(){
return guiGroup.getPosition(); ofPoint RadioList::getPosition(){
} return guiGroup.getPosition();
}
float RadioList::getWidth(){
return guiGroup.getWidth(); float RadioList::getWidth(){
} return guiGroup.getWidth();
}
float RadioList::getHeight(){
return guiGroup.getHeight(); float RadioList::getHeight(){
} return guiGroup.getHeight();
}
string RadioList::getTitle(){
return guiGroup.getName(); string RadioList::getTitle(){
} return guiGroup.getName();
}
string RadioList::getItemName(int index){
if(index >= guiGroup.getNumControls()){ string RadioList::getItemName(int index){
return ""; if(index >= guiGroup.getNumControls()){
} return "";
}
ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(index));
return toggle->getName(); ofxToggle * toggle = static_cast <ofxToggle *>(guiGroup.getControl(index));
} return toggle->getName();
}
int RadioList::size(){
return storedValues.size(); int RadioList::size(){
} return storedValues.size();
}
void RadioList::onToggleClicked(bool & toggleValue){
unselectAll(); void RadioList::onToggleClicked(bool & toggleValue){
unselectAll();
// Search for the actual toggle triggering the event
int i; // Search for the actual toggle triggering the event
for(i = 0; i < guiGroup.getNumControls(); i++){ int i;
ofxToggle * toggle = static_cast<ofxToggle *>(guiGroup.getControl(i)); for(i = 0; i < guiGroup.getNumControls(); i++){
ofParameter<bool> * paramPtr = static_cast<ofParameter<bool> *>(&toggle->getParameter()); ofxToggle * toggle = static_cast <ofxToggle *>(guiGroup.getControl(i));
ofParameter <bool> * paramPtr = static_cast <ofParameter <bool> *>(&toggle->getParameter());
if(&(paramPtr->get()) == &toggleValue){
selectItem(i); if(&(paramPtr->get()) == &toggleValue){
break; selectItem(i);
} break;
} }
} }
} // namespace piMapper }
} // namespace piMapper
} // namespace ofx } // namespace ofx

94
src/UserInterface/RadioList.h

@ -5,50 +5,52 @@
#include "ofxToggle.h" #include "ofxToggle.h"
#include "ofxLabel.h" #include "ofxLabel.h"
namespace ofx{ namespace ofx {
namespace piMapper{ namespace piMapper {
class RadioList{
public: class RadioList {
RadioList(); public:
RadioList(vector<string> & labels, vector<string> & values); RadioList();
RadioList(string title, vector<string> & labels, vector<string> & values); RadioList(vector <string> & labels, vector <string> & values);
~RadioList(); RadioList(string title, vector <string> & labels, vector <string> & values);
~RadioList();
void setup(vector<string> & labels, vector<string> & values);
void setup(string title, vector<string> & labels, vector<string> & values); void setup(vector <string> & labels, vector <string> & values);
void draw(); void setup(string title, vector <string> & labels, vector <string> & values);
void setTitle(string title); void draw();
void setPosition(ofPoint p); void setTitle(string title);
void setPosition(float x, float y); void setPosition(ofPoint p);
void selectItem(int index); void setPosition(float x, float y);
bool selectItemByValue(std::string itemValue); void selectItem(int index);
void enable(); bool selectItemByValue(std::string itemValue);
void disable(); void enable();
void clear(); void disable();
void unselectAll(); void clear();
ofPoint getPosition(); void unselectAll();
float getWidth(); ofPoint getPosition();
float getHeight(); float getWidth();
string getTitle(); float getHeight();
string getItemName(int index); string getTitle();
int size(); string getItemName(int index);
int size();
// This event notifies about a toggle being selected and passes it's name to
// the listeners. // This event notifies about a toggle being selected and passes it's name to
// Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr, // the listeners.
// &listenerClass::listenerMethod) // Use ofAddListener(RadioListInstance.radioSelectedEvent, listenerClassPtr,
// to listen to this. Listner method void listenerMethod(string & radioName) // &listenerClass::listenerMethod)
ofEvent<string> onRadioSelected; // to listen to this. Listner method void listenerMethod(string & radioName)
ofEvent <string> onRadioSelected;
private:
vector<string> storedLabels; private:
vector<string> storedValues; vector <string> storedLabels;
string storedTitle; vector <string> storedValues;
ofxGuiGroup guiGroup; string storedTitle;
int storedSelectedItem; ofxGuiGroup guiGroup;
int storedSelectedItem;
void onToggleClicked(bool & toggleValue);
void onToggleClicked(bool & toggleValue);
};
} // namespace piMapper };
} // namespace piMapper
} // namespace ofx } // namespace ofx

735
src/UserInterface/SourcesEditor.cpp

@ -2,371 +2,372 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
SourcesEditor::SourcesEditor() {
init(); SourcesEditor::SourcesEditor(){
// Create new MediaServer instance, init();
// we will need to clear this in the deconstr // Create new MediaServer instance,
mediaServer = new MediaServer(); // we will need to clear this in the deconstr
isMediaServerExternal = false; mediaServer = new MediaServer();
addMediaServerListeners(); isMediaServerExternal = false;
} addMediaServerListeners();
}
void SourcesEditor::init() {
mediaServer = NULL; // Pointers to NULL pointer so we can check later void SourcesEditor::init(){
isMediaServerExternal = false; mediaServer = NULL; // Pointers to NULL pointer so we can check later
registerAppEvents(); isMediaServerExternal = false;
} registerAppEvents();
}
SourcesEditor::SourcesEditor(MediaServer* externalMediaServer) {
init(); SourcesEditor::SourcesEditor(MediaServer * externalMediaServer){
// Assign external MediaServer instance pointer init();
mediaServer = externalMediaServer; // Assign external MediaServer instance pointer
isMediaServerExternal = true; mediaServer = externalMediaServer;
addMediaServerListeners(); isMediaServerExternal = true;
} addMediaServerListeners();
}
SourcesEditor::~SourcesEditor() {
unregisterAppEvents(); SourcesEditor::~SourcesEditor(){
delete imageSelector; unregisterAppEvents();
delete videoSelector; delete imageSelector;
delete fboSelector; delete videoSelector;
removeMediaServerListeners(); delete fboSelector;
clearMediaServer(); removeMediaServerListeners();
} clearMediaServer();
}
void SourcesEditor::registerAppEvents() {
ofAddListener(ofEvents().setup, this, &SourcesEditor::setup); void SourcesEditor::registerAppEvents(){
} ofAddListener(ofEvents().setup, this, &SourcesEditor::setup);
}
void SourcesEditor::unregisterAppEvents() {
ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup); void SourcesEditor::unregisterAppEvents(){
} ofRemoveListener(ofEvents().setup, this, &SourcesEditor::setup);
}
void SourcesEditor::setup(ofEventArgs& args) {
imageSelector = new RadioList(); void SourcesEditor::setup(ofEventArgs & args){
videoSelector = new RadioList(); imageSelector = new RadioList();
fboSelector = new RadioList(); videoSelector = new RadioList();
fboSelector = new RadioList();
// Get media count
int numImages = mediaServer->getNumImages(); // Get media count
int numVideos = mediaServer->getNumVideos(); int numImages = mediaServer->getNumImages();
int numFbos = mediaServer->getNumFboSources(); int numVideos = mediaServer->getNumVideos();
int numFbos = mediaServer->getNumFboSources();
// Depending on media count, decide what to load and initialize
if (numImages) { // Depending on media count, decide what to load and initialize
// Get image names from media server if(numImages){
vector<string> imageNames = mediaServer->getImageNames(); // Get image names from media server
imageSelector->setup("Images", imageNames, mediaServer->getImagePaths()); vector <string> imageNames = mediaServer->getImageNames();
ofAddListener(imageSelector->onRadioSelected, this, &SourcesEditor::handleImageSelected); imageSelector->setup("Images", imageNames, mediaServer->getImagePaths());
} ofAddListener(imageSelector->onRadioSelected, this, &SourcesEditor::handleImageSelected);
if (numVideos) { }
vector<string> videoNames = mediaServer->getVideoNames(); if(numVideos){
videoSelector->setup("Videos", videoNames, mediaServer->getVideoPaths()); vector <string> videoNames = mediaServer->getVideoNames();
ofAddListener(videoSelector->onRadioSelected, this, &SourcesEditor::handleVideoSelected); videoSelector->setup("Videos", videoNames, mediaServer->getVideoPaths());
} ofAddListener(videoSelector->onRadioSelected, this, &SourcesEditor::handleVideoSelected);
if (numFbos) { }
std::vector<std::string> fboNames = mediaServer->getFboSourceNames(); if(numFbos){
fboSelector->setup("FBOs", fboNames, fboNames); std::vector <std::string> fboNames = mediaServer->getFboSourceNames();
ofAddListener(fboSelector->onRadioSelected, this, &SourcesEditor::handleFboSelected); fboSelector->setup("FBOs", fboNames, fboNames);
} ofAddListener(fboSelector->onRadioSelected, this, &SourcesEditor::handleFboSelected);
}
// Align menus
int menuPosX = 20; // Align menus
int distX = 230; int menuPosX = 20;
if (numImages) { int distX = 230;
imageSelector->setPosition(menuPosX, 20); if(numImages){
menuPosX += distX; imageSelector->setPosition(menuPosX, 20);
} menuPosX += distX;
if (numVideos) { }
videoSelector->setPosition(menuPosX, 20); if(numVideos){
menuPosX += distX; videoSelector->setPosition(menuPosX, 20);
} menuPosX += distX;
if (numFbos) { }
fboSelector->setPosition(menuPosX, 20); if(numFbos){
} fboSelector->setPosition(menuPosX, 20);
}
}
}
void SourcesEditor::draw() {
// Don't draw if there is no source selected void SourcesEditor::draw(){
if (surfaceManager->getSelectedSurface() == NULL) { // Don't draw if there is no source selected
//ofLogNotice("SourcesEditor") << "No surface selected"; if(surfaceManager->getSelectedSurface() == NULL){
return; //ofLogNotice("SourcesEditor") << "No surface selected";
} return;
if (imageSelector->size()) { }
imageSelector->draw(); if(imageSelector->size()){
} imageSelector->draw();
if (videoSelector->size()) { }
videoSelector->draw(); if(videoSelector->size()){
} videoSelector->draw();
if (fboSelector->size()) { }
fboSelector->draw(); if(fboSelector->size()){
} fboSelector->draw();
}
}
}
void SourcesEditor::disable() {
if (imageSelector->size()) { void SourcesEditor::disable(){
imageSelector->disable(); if(imageSelector->size()){
} imageSelector->disable();
if (videoSelector->size()) { }
videoSelector->disable(); if(videoSelector->size()){
} videoSelector->disable();
if (fboSelector->size()) { }
fboSelector->disable(); if(fboSelector->size()){
} fboSelector->disable();
} }
}
void SourcesEditor::enable() {
// Don't enable if there is no surface selected void SourcesEditor::enable(){
if (surfaceManager->getSelectedSurface() == NULL) { // Don't enable if there is no surface selected
ofLogNotice("SourcesEditor") << "No surface selected. Not enabling and not showing source list."; if(surfaceManager->getSelectedSurface() == NULL){
return; ofLogNotice("SourcesEditor") << "No surface selected. Not enabling and not showing source list.";
} return;
if (imageSelector->size()) { }
imageSelector->enable(); if(imageSelector->size()){
} imageSelector->enable();
if (videoSelector->size()) { }
videoSelector->enable(); if(videoSelector->size()){
} videoSelector->enable();
if (fboSelector->size()) { }
fboSelector->enable(); if(fboSelector->size()){
} fboSelector->enable();
BaseSource* source = surfaceManager->getSelectedSurface()->getSource(); }
BaseSource * source = surfaceManager->getSelectedSurface()->getSource();
// TODO: getPath should be replaced with something like getId() as now we
// use paths for loadable sources and names for FBOs // TODO: getPath should be replaced with something like getId() as now we
if (source->getType() == SourceType::SOURCE_TYPE_FBO) { // use paths for loadable sources and names for FBOs
selectSourceRadioButton(source->getName()); if(source->getType() == SourceType::SOURCE_TYPE_FBO){
} else { selectSourceRadioButton(source->getName());
selectSourceRadioButton(source->getPath()); }else{
} selectSourceRadioButton(source->getPath());
} }
}
void SourcesEditor::setSurfaceManager(SurfaceManager* newSurfaceManager) {
surfaceManager = newSurfaceManager; void SourcesEditor::setSurfaceManager(SurfaceManager * newSurfaceManager){
} surfaceManager = newSurfaceManager;
}
void SourcesEditor::setCmdManager(CmdManager * cmdManager){
_cmdManager = cmdManager; void SourcesEditor::setCmdManager(CmdManager * cmdManager){
} _cmdManager = cmdManager;
}
void SourcesEditor::setMediaServer(MediaServer* newMediaServer) {
// If the new media server is not valid void SourcesEditor::setMediaServer(MediaServer * newMediaServer){
if (newMediaServer == NULL) { // If the new media server is not valid
// Log an error and return from the routine if(newMediaServer == NULL){
ofLogFatalError("SourcesEditor") << "New media server is NULL"; // Log an error and return from the routine
std::exit(EXIT_FAILURE); ofLogFatalError("SourcesEditor") << "New media server is NULL";
} std::exit(EXIT_FAILURE);
// Attempt to clear existing media server and assign new one }
clearMediaServer(); // Attempt to clear existing media server and assign new one
//cout << "old ms addr: " << mediaServer << endl; clearMediaServer();
//cout << "new ms addr: " << newMediaServer << endl; //cout << "old ms addr: " << mediaServer << endl;
mediaServer = newMediaServer; //cout << "new ms addr: " << newMediaServer << endl;
isMediaServerExternal = true; mediaServer = newMediaServer;
} isMediaServerExternal = true;
}
void SourcesEditor::selectSourceRadioButton(std::string& sourcePath) {
if (sourcePath == "") { void SourcesEditor::selectSourceRadioButton(std::string & sourcePath){
ofLogNotice("SourcesEditor") << "Path is empty"; if(sourcePath == ""){
if (imageSelector->size()) { ofLogNotice("SourcesEditor") << "Path is empty";
imageSelector->unselectAll(); if(imageSelector->size()){
} imageSelector->unselectAll();
if (videoSelector->size()) { }
videoSelector->unselectAll(); if(videoSelector->size()){
} videoSelector->unselectAll();
if (fboSelector->size()) { }
fboSelector->unselectAll(); if(fboSelector->size()){
} fboSelector->unselectAll();
return; }
} else { return;
// Check image selector first }else{
bool imageRadioSelected = false; // Check image selector first
bool videoRadioSelected = false; bool imageRadioSelected = false;
bool fboRadioSelected = false; bool videoRadioSelected = false;
if (imageSelector->size()) { bool fboRadioSelected = false;
imageRadioSelected = imageSelector->selectItemByValue(sourcePath); if(imageSelector->size()){
} imageRadioSelected = imageSelector->selectItemByValue(sourcePath);
if (videoSelector->size()) { }
videoRadioSelected = videoSelector->selectItemByValue(sourcePath); if(videoSelector->size()){
} videoRadioSelected = videoSelector->selectItemByValue(sourcePath);
if (fboSelector->size()) { }
fboRadioSelected = fboSelector->selectItemByValue(sourcePath); if(fboSelector->size()){
} fboRadioSelected = fboSelector->selectItemByValue(sourcePath);
if (imageRadioSelected || videoRadioSelected || fboRadioSelected) { }
return; if(imageRadioSelected || videoRadioSelected || fboRadioSelected){
} return;
// Log warning if we are still here }
ofLogWarning("SourcesEditor") << "Could not find option in any of the source lists"; // Log warning if we are still here
} ofLogWarning("SourcesEditor") << "Could not find option in any of the source lists";
} }
}
void SourcesEditor::addMediaServerListeners() {
// Check if the media server is valid void SourcesEditor::addMediaServerListeners(){
if (mediaServer == NULL) { // Check if the media server is valid
ofLogError("SourcesEditor::addMediaServerListeners", "Media server not set"); if(mediaServer == NULL){
return; ofLogError("SourcesEditor::addMediaServerListeners", "Media server not set");
} return;
// Add listeners to custom events of the media server }
ofAddListener(mediaServer->onImageAdded, this, &SourcesEditor::handleImageAdded); // Add listeners to custom events of the media server
ofAddListener(mediaServer->onImageRemoved, this, &SourcesEditor::handleImageRemoved); ofAddListener(mediaServer->onImageAdded, this, &SourcesEditor::handleImageAdded);
ofAddListener(mediaServer->onVideoAdded, this, &SourcesEditor::handleVideoAdded); ofAddListener(mediaServer->onImageRemoved, this, &SourcesEditor::handleImageRemoved);
ofAddListener(mediaServer->onVideoRemoved, this, &SourcesEditor::handleVideoRemoved); ofAddListener(mediaServer->onVideoAdded, this, &SourcesEditor::handleVideoAdded);
ofAddListener(mediaServer->onImageLoaded, this, &SourcesEditor::handleImageLoaded); ofAddListener(mediaServer->onVideoRemoved, this, &SourcesEditor::handleVideoRemoved);
ofAddListener(mediaServer->onImageUnloaded, this, &SourcesEditor::handleImageUnloaded); ofAddListener(mediaServer->onImageLoaded, this, &SourcesEditor::handleImageLoaded);
ofAddListener(mediaServer->onImageUnloaded, this, &SourcesEditor::handleImageUnloaded);
ofAddListener(mediaServer->onFboSourceAdded, this, &SourcesEditor::handleFboSourceAdded);
ofAddListener(mediaServer->onFboSourceRemoved, this, &SourcesEditor::handleFboSourceRemoved); ofAddListener(mediaServer->onFboSourceAdded, this, &SourcesEditor::handleFboSourceAdded);
ofAddListener(mediaServer->onFboSourceLoaded, this, &SourcesEditor::handleFboSourceLoaded); ofAddListener(mediaServer->onFboSourceRemoved, this, &SourcesEditor::handleFboSourceRemoved);
ofAddListener(mediaServer->onFboSourceUnloaded, this, &SourcesEditor::handleFboSourceUnloaded); ofAddListener(mediaServer->onFboSourceLoaded, this, &SourcesEditor::handleFboSourceLoaded);
ofAddListener(mediaServer->onFboSourceUnloaded, this, &SourcesEditor::handleFboSourceUnloaded);
}
}
void SourcesEditor::removeMediaServerListeners() {
// Check if the media server is valid void SourcesEditor::removeMediaServerListeners(){
if (mediaServer == NULL) { // Check if the media server is valid
ofLogError("SourcesEditor::addMediaServerListeners", "Media server not set"); if(mediaServer == NULL){
return; ofLogError("SourcesEditor::addMediaServerListeners", "Media server not set");
} return;
// Remove listeners to custom events of the media server }
ofRemoveListener(mediaServer->onImageAdded, this, &SourcesEditor::handleImageAdded); // Remove listeners to custom events of the media server
ofRemoveListener(mediaServer->onImageRemoved, this, &SourcesEditor::handleImageRemoved); ofRemoveListener(mediaServer->onImageAdded, this, &SourcesEditor::handleImageAdded);
ofRemoveListener(mediaServer->onVideoAdded, this, &SourcesEditor::handleVideoAdded); ofRemoveListener(mediaServer->onImageRemoved, this, &SourcesEditor::handleImageRemoved);
ofRemoveListener(mediaServer->onVideoRemoved, this, &SourcesEditor::handleVideoRemoved); ofRemoveListener(mediaServer->onVideoAdded, this, &SourcesEditor::handleVideoAdded);
ofRemoveListener(mediaServer->onImageLoaded, this, &SourcesEditor::handleImageLoaded); ofRemoveListener(mediaServer->onVideoRemoved, this, &SourcesEditor::handleVideoRemoved);
ofRemoveListener(mediaServer->onImageUnloaded, this, &SourcesEditor::handleImageUnloaded); ofRemoveListener(mediaServer->onImageLoaded, this, &SourcesEditor::handleImageLoaded);
ofRemoveListener(mediaServer->onFboSourceAdded, this, &SourcesEditor::handleFboSourceAdded); ofRemoveListener(mediaServer->onImageUnloaded, this, &SourcesEditor::handleImageUnloaded);
ofRemoveListener(mediaServer->onFboSourceRemoved, this, &SourcesEditor::handleFboSourceRemoved); ofRemoveListener(mediaServer->onFboSourceAdded, this, &SourcesEditor::handleFboSourceAdded);
ofRemoveListener(mediaServer->onFboSourceLoaded, this, &SourcesEditor::handleFboSourceLoaded); ofRemoveListener(mediaServer->onFboSourceRemoved, this, &SourcesEditor::handleFboSourceRemoved);
ofRemoveListener(mediaServer->onFboSourceUnloaded, this, &SourcesEditor::handleFboSourceUnloaded); ofRemoveListener(mediaServer->onFboSourceLoaded, this, &SourcesEditor::handleFboSourceLoaded);
} ofRemoveListener(mediaServer->onFboSourceUnloaded, this, &SourcesEditor::handleFboSourceUnloaded);
}
void SourcesEditor::handleImageSelected(string & imagePath){
_cmdManager->exec(new SetSourceCmd(SourceType::SOURCE_TYPE_IMAGE, void SourcesEditor::handleImageSelected(string & imagePath){
imagePath, _cmdManager->exec(new SetSourceCmd(SourceType::SOURCE_TYPE_IMAGE,
surfaceManager->getSelectedSurface(), imagePath,
(SourcesEditor *)this)); surfaceManager->getSelectedSurface(),
} (SourcesEditor *)this));
}
void SourcesEditor::setImageSource(string & imagePath){
// Unselect selected items void SourcesEditor::setImageSource(string & imagePath){
videoSelector->unselectAll(); // Unselect selected items
fboSelector->unselectAll(); videoSelector->unselectAll();
fboSelector->unselectAll();
BaseSurface* surface = surfaceManager->getSelectedSurface();
if (surface == NULL) { BaseSurface * surface = surfaceManager->getSelectedSurface();
ofLogWarning("SourcesEditor") << "No surface selected"; if(surface == NULL){
return; ofLogWarning("SourcesEditor") << "No surface selected";
} return;
}
// Unload old media
BaseSource* source = surface->getSource(); // Unload old media
if (source->isLoadable()) { BaseSource * source = surface->getSource();
mediaServer->unloadMedia(source->getPath()); if(source->isLoadable()){
} else { mediaServer->unloadMedia(source->getPath());
mediaServer->unloadMedia(source->getName()); }else{
} mediaServer->unloadMedia(source->getName());
}
// Load new image
surface->setSource(mediaServer->loadImage(imagePath)); // Load new image
} surface->setSource(mediaServer->loadImage(imagePath));
}
void SourcesEditor::handleVideoSelected(string & videoPath){
_cmdManager->exec(new SetSourceCmd(SourceType::SOURCE_TYPE_VIDEO, void SourcesEditor::handleVideoSelected(string & videoPath){
videoPath, _cmdManager->exec(new SetSourceCmd(SourceType::SOURCE_TYPE_VIDEO,
surfaceManager->getSelectedSurface(), videoPath,
(SourcesEditor *)this)); surfaceManager->getSelectedSurface(),
} (SourcesEditor *)this));
}
void SourcesEditor::setVideoSource(string & videoPath){
// Unselect any selected items void SourcesEditor::setVideoSource(string & videoPath){
fboSelector->unselectAll(); // Unselect any selected items
imageSelector->unselectAll(); fboSelector->unselectAll();
imageSelector->unselectAll();
BaseSurface* surface = surfaceManager->getSelectedSurface();
if (surface == NULL) { BaseSurface * surface = surfaceManager->getSelectedSurface();
ofLogWarning("SourcesEditor") << "No surface selected"; if(surface == NULL){
return; ofLogWarning("SourcesEditor") << "No surface selected";
} return;
}
// Unload old media
BaseSource* source = surface->getSource(); // Unload old media
if (source->isLoadable()) { BaseSource * source = surface->getSource();
mediaServer->unloadMedia(source->getPath()); if(source->isLoadable()){
} else { mediaServer->unloadMedia(source->getPath());
mediaServer->unloadMedia(source->getName()); }else{
} mediaServer->unloadMedia(source->getName());
}
// Load new video
surface->setSource(mediaServer->loadVideo(videoPath)); // Load new video
} surface->setSource(mediaServer->loadVideo(videoPath));
}
void SourcesEditor::handleFboSelected(string & fboName){
_cmdManager->exec(new SetSourceCmd(SourceType::SOURCE_TYPE_FBO, void SourcesEditor::handleFboSelected(string & fboName){
fboName, _cmdManager->exec(new SetSourceCmd(SourceType::SOURCE_TYPE_FBO,
surfaceManager->getSelectedSurface(), fboName,
(SourcesEditor *)this)); surfaceManager->getSelectedSurface(),
} (SourcesEditor *)this));
}
void SourcesEditor::setFboSource(string & fboName) {
videoSelector->unselectAll(); void SourcesEditor::setFboSource(string & fboName){
imageSelector->unselectAll(); videoSelector->unselectAll();
imageSelector->unselectAll();
// Get selected surface
BaseSurface* surface = surfaceManager->getSelectedSurface(); // Get selected surface
if (surface == NULL) { BaseSurface * surface = surfaceManager->getSelectedSurface();
ofLogWarning("SourcesEditor") << "No surface selected"; if(surface == NULL){
return; ofLogWarning("SourcesEditor") << "No surface selected";
} return;
}
// Unload old media
BaseSource* source = surface->getSource(); // Unload old media
if (source->isLoadable()) { BaseSource * source = surface->getSource();
mediaServer->unloadMedia(source->getPath()); if(source->isLoadable()){
} else { mediaServer->unloadMedia(source->getPath());
mediaServer->unloadMedia(source->getName()); }else{
} mediaServer->unloadMedia(source->getName());
}
// Load new FBO
surface->setSource(mediaServer->loadFboSource(fboName)); // Load new FBO
} surface->setSource(mediaServer->loadFboSource(fboName));
void SourcesEditor::clearSource(){
BaseSurface* surface = surfaceManager->getSelectedSurface();
// Unload old media
BaseSource* source = surface->getSource();
if (source->isLoadable()) {
mediaServer->unloadMedia(source->getPath());
} else {
mediaServer->unloadMedia(source->getName());
}
// Reset default source
surface->setSource(surface->getDefaultSource());
}
void SourcesEditor::clearMediaServer() {
// If mediaServer is local, clear it
if (!isMediaServerExternal) {
// Clear all loaded sources
mediaServer->clear();
// Destroy the pointer and set it to NULL pointer
delete mediaServer;
mediaServer = NULL;
}
}
void SourcesEditor::handleImageAdded(std::string& path) {}
void SourcesEditor::handleImageRemoved(std::string& path) {}
void SourcesEditor::handleVideoAdded(std::string& path) {}
void SourcesEditor::handleVideoRemoved(std::string& path) {}
void SourcesEditor::handleImageLoaded(std::string& path) {}
void SourcesEditor::handleImageUnloaded(std::string& path) {}
void SourcesEditor::handleFboSourceAdded(std::string& name) {}
void SourcesEditor::handleFboSourceRemoved(std::string& name) {}
void SourcesEditor::handleFboSourceLoaded(std::string& name) {}
void SourcesEditor::handleFboSourceUnloaded(std::string& name) {}
} }
}
void SourcesEditor::clearSource(){
BaseSurface * surface = surfaceManager->getSelectedSurface();
// Unload old media
BaseSource * source = surface->getSource();
if(source->isLoadable()){
mediaServer->unloadMedia(source->getPath());
}else{
mediaServer->unloadMedia(source->getName());
}
// Reset default source
surface->setSource(surface->getDefaultSource());
}
void SourcesEditor::clearMediaServer(){
// If mediaServer is local, clear it
if(!isMediaServerExternal){
// Clear all loaded sources
mediaServer->clear();
// Destroy the pointer and set it to NULL pointer
delete mediaServer;
mediaServer = NULL;
}
}
void SourcesEditor::handleImageAdded(std::string & path){}
void SourcesEditor::handleImageRemoved(std::string & path){}
void SourcesEditor::handleVideoAdded(std::string & path){}
void SourcesEditor::handleVideoRemoved(std::string & path){}
void SourcesEditor::handleImageLoaded(std::string & path){}
void SourcesEditor::handleImageUnloaded(std::string & path){}
void SourcesEditor::handleFboSourceAdded(std::string & name){}
void SourcesEditor::handleFboSourceRemoved(std::string & name){}
void SourcesEditor::handleFboSourceLoaded(std::string & name){}
void SourcesEditor::handleFboSourceUnloaded(std::string & name){}
} // namespace piMapper
} // namespace ofx

152
src/UserInterface/SourcesEditor.h

@ -10,80 +10,82 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class SourcesEditor { class SourcesEditor {
public: public:
// Default contructor that initializes media server locally, // Default contructor that initializes media server locally,
// thus requiring to delete the media server from memory on deconstr // thus requiring to delete the media server from memory on deconstr
SourcesEditor(); SourcesEditor();
// Alternative constructor that allows to assign external media server // Alternative constructor that allows to assign external media server
SourcesEditor(MediaServer* externalMediaServer); SourcesEditor(MediaServer * externalMediaServer);
~SourcesEditor(); ~SourcesEditor();
void registerAppEvents(); void registerAppEvents();
void unregisterAppEvents(); void unregisterAppEvents();
void setup(ofEventArgs& args); void setup(ofEventArgs & args);
void draw(); void draw();
void loadImage(string name, string path); void loadImage(string name, string path);
void disable(); void disable();
void enable(); void enable();
void setSurfaceManager(SurfaceManager* newSurfaceManager); void setSurfaceManager(SurfaceManager * newSurfaceManager);
void setCmdManager(CmdManager * cmdManager); void setCmdManager(CmdManager * cmdManager);
// Sets external MediaServer // Sets external MediaServer
void setMediaServer(MediaServer* newMediaServer); void setMediaServer(MediaServer * newMediaServer);
//void selectImageSourceRadioButton(string name); //void selectImageSourceRadioButton(string name);
void selectSourceRadioButton(std::string& sourcePath); void selectSourceRadioButton(std::string & sourcePath);
int getLoadedTexCount(); int getLoadedTexCount();
ofTexture* getTexture(int index); ofTexture * getTexture(int index);
void setImageSource(string & imagePath); void setImageSource(string & imagePath);
void setVideoSource(string & videoPath); void setVideoSource(string & videoPath);
void setFboSource(string & fboName); void setFboSource(string & fboName);
void clearSource(); void clearSource();
private: private:
MediaServer* mediaServer; MediaServer * mediaServer;
SurfaceManager* surfaceManager; SurfaceManager * surfaceManager;
RadioList* imageSelector; RadioList * imageSelector;
RadioList* videoSelector; RadioList * videoSelector;
RadioList* fboSelector; RadioList * fboSelector;
CmdManager * _cmdManager; CmdManager * _cmdManager;
// Is the media server pointer local or from somewhere else? // Is the media server pointer local or from somewhere else?
// We use this to determine if we are allowed to clear media server locally. // We use this to determine if we are allowed to clear media server locally.
bool isMediaServerExternal; bool isMediaServerExternal;
// Init handles variable initialization in all constructors // Init handles variable initialization in all constructors
void init(); void init();
// Methods for adding and removing listeners to the media server // Methods for adding and removing listeners to the media server
void addMediaServerListeners(); void addMediaServerListeners();
void removeMediaServerListeners(); void removeMediaServerListeners();
// Handles GUI event, whenever someone has clicked on a radio button // Handles GUI event, whenever someone has clicked on a radio button
void handleImageSelected(string & imagePath); void handleImageSelected(string & imagePath);
void handleVideoSelected(string & videoPath); void handleVideoSelected(string & videoPath);
void handleFboSelected(string & fboName); void handleFboSelected(string & fboName);
// Careful clearing of the media server, // Careful clearing of the media server,
// clears only if the media server has been initialized locally // clears only if the media server has been initialized locally
void clearMediaServer(); void clearMediaServer();
// MediaServer event handlers // MediaServer event handlers
void handleImageAdded(std::string& path); void handleImageAdded(std::string & path);
void handleImageRemoved(std::string& path); void handleImageRemoved(std::string & path);
void handleVideoAdded(std::string& path); void handleVideoAdded(std::string & path);
void handleVideoRemoved(std::string& path); void handleVideoRemoved(std::string & path);
void handleImageLoaded(std::string& path); void handleImageLoaded(std::string & path);
void handleImageUnloaded(std::string& path); void handleImageUnloaded(std::string & path);
void handleFboSourceAdded(std::string& name); void handleFboSourceAdded(std::string & name);
void handleFboSourceRemoved(std::string& name); void handleFboSourceRemoved(std::string & name);
void handleFboSourceLoaded(std::string& name); void handleFboSourceLoaded(std::string & name);
void handleFboSourceUnloaded(std::string& name); void handleFboSourceUnloaded(std::string & name);
}; };
}
} } // namespace piMapper
} // namespace ofx

486
src/UserInterface/TextureEditor.cpp

@ -2,240 +2,256 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
TextureEditor::TextureEditor() {
clear();
enable();
}
TextureEditor::~TextureEditor() {
clear();
disable();
}
void TextureEditor::registerAppEvents() {
ofAddListener(ofEvents().update, this, &TextureEditor::update);
}
void TextureEditor::unregisterAppEvents() {
ofRemoveListener(ofEvents().update, this, &TextureEditor::update);
}
void TextureEditor::registerKeyEvents() {
ofAddListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed);
ofAddListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased);
}
void TextureEditor::unregisterKeyEvents() {
ofRemoveListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed);
ofRemoveListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased);
}
void TextureEditor::enable() {
registerAppEvents();
registerKeyEvents();
bShiftKeyDown = false;
}
void TextureEditor::disable() { TextureEditor::TextureEditor(){
unregisterAppEvents(); clear();
unregisterKeyEvents(); enable();
} }
void TextureEditor::update(ofEventArgs& args) { TextureEditor::~TextureEditor(){
if (surface == NULL) return; clear();
disable();
// update surface if one of the joints is being dragged }
ofVec2f textureSize = ofVec2f(surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight()); void TextureEditor::registerAppEvents(){
ofAddListener(ofEvents().update, this, &TextureEditor::update);
// Get selected joint index }
int selectedJointIndex = 0;
bool bJointSelected = false; void TextureEditor::unregisterAppEvents(){
for (int i = 0; i < joints.size(); i++) { ofRemoveListener(ofEvents().update, this, &TextureEditor::update);
if (joints[i]->isDragged() || joints[i]->isSelected()) { }
selectedJointIndex = i;
bJointSelected = true; void TextureEditor::registerKeyEvents(){
break; ofAddListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed);
} ofAddListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased);
} // for }
// Constrain quad texture selection void TextureEditor::unregisterKeyEvents(){
if (joints.size() == 4) { ofRemoveListener(ofEvents().keyPressed, this, &TextureEditor::keyPressed);
if (bJointSelected) { ofRemoveListener(ofEvents().keyReleased, this, &TextureEditor::keyReleased);
constrainJointsToQuad(selectedJointIndex); }
for (int i = 0; i < joints.size(); i++) { void TextureEditor::enable(){
surface->setTexCoord(i, joints[i]->position / textureSize); registerAppEvents();
} registerKeyEvents();
} // if bShiftKeyDown = false;
} else { }
if (bJointSelected) {
surface->setTexCoord(selectedJointIndex, joints[selectedJointIndex]->position / textureSize); void TextureEditor::disable(){
} unregisterAppEvents();
} // else unregisterKeyEvents();
} }
void TextureEditor::keyPressed(ofKeyEventArgs& args) { void TextureEditor::update(ofEventArgs & args){
int key = args.key; if(surface == NULL){
float moveStep; return;
}
// update surface if one of the joints is being dragged
ofVec2f textureSize = ofVec2f(surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight());
// Get selected joint index
int selectedJointIndex = 0;
bool bJointSelected = false;
for(int i = 0; i < joints.size(); i++){
if(joints[i]->isDragged() || joints[i]->isSelected()){
selectedJointIndex = i;
bJointSelected = true;
break;
}
} // for
// Constrain quad texture selection
if(joints.size() == 4){
if(bJointSelected){
constrainJointsToQuad(selectedJointIndex);
for(int i = 0; i < joints.size(); i++){
surface->setTexCoord(i, joints[i]->position / textureSize);
}
} // if
}else{
if(bJointSelected){
surface->setTexCoord(selectedJointIndex, joints[selectedJointIndex]->position / textureSize);
}
} // else
}
void TextureEditor::keyPressed(ofKeyEventArgs & args){
int key = args.key;
float moveStep;
if(bShiftKeyDown){
moveStep = 10.0f;
}else{
moveStep = 0.5f;
}
switch(key){
case OF_KEY_LEFT:
moveSelection(ofVec2f(-moveStep, 0.0f));
break;
case OF_KEY_RIGHT:
moveSelection(ofVec2f(moveStep, 0.0f));
break;
case OF_KEY_UP:
moveSelection(ofVec2f(0.0f, -moveStep));
break;
case OF_KEY_DOWN:
moveSelection(ofVec2f(0.0f, moveStep));
break;
case OF_KEY_SHIFT:
bShiftKeyDown = true;
break;
}
}
void TextureEditor::keyReleased(ofKeyEventArgs & args){
int key = args.key;
switch(key){
case OF_KEY_SHIFT:
bShiftKeyDown = false;
break;
}
}
void TextureEditor::draw(){
if(surface == NULL){
return;
}
// Reset default color to white
ofSetColor(255, 255, 255, 255);
drawJoints();
}
void TextureEditor::drawJoints(){
for(int i = 0; i < joints.size(); i++){
joints[i]->draw();
}
}
void TextureEditor::setSurface(BaseSurface * newSurface){
surface = newSurface;
createJoints();
}
void TextureEditor::clear(){
surface = NULL;
clearJoints();
}
void TextureEditor::createJoints(){
if(surface == NULL){
return;
}
clearJoints();
vector <ofVec2f> & texCoords = surface->getTexCoords();
ofVec2f textureSize = ofVec2f(surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight());
for(int i = 0; i < texCoords.size(); i++){
joints.push_back(new CircleJoint());
joints.back()->position = texCoords[i] * textureSize;
}
}
void TextureEditor::clearJoints(){
while(joints.size()){
delete joints.back();
joints.pop_back();
}
}
void TextureEditor::unselectAllJoints(){
for(int i = 0; i < joints.size(); i++){
joints[i]->unselect();
}
}
void TextureEditor::moveTexCoords(ofVec2f by){
if(surface == NULL){
return;
}
vector <ofVec2f> & texCoords = surface->getTexCoords();
ofVec2f textureSize = ofVec2f(surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight());
for(int i = 0; i < texCoords.size(); i++){
joints[i]->position += by;
surface->setTexCoord(i, joints[i]->position / textureSize);
}
}
void TextureEditor::stopDragJoints(){
for(int i = 0; i < joints.size(); i++){
joints[i]->stopDrag();
}
}
void TextureEditor::moveSelection(ofVec2f by){
// check if joints selected
bool bJointSelected = false;
BaseJoint * selectedJoint;
for(int i = 0; i < joints.size(); i++){
if(joints[i]->isSelected()){
bJointSelected = true;
selectedJoint = joints[i];
break;
}
}
if(bJointSelected){
selectedJoint->position += by;
}else{
moveTexCoords(by);
}
}
void TextureEditor::constrainJointsToQuad(int selectedJointIndex){
switch(selectedJointIndex){
case 0:
joints[1]->position = ofVec2f(joints[1]->position.x, joints[0]->position.y);
joints[2]->position = ofVec2f(joints[1]->position.x, joints[3]->position.y);
joints[3]->position = ofVec2f(joints[0]->position.x, joints[3]->position.y);
break;
case 1:
joints[0]->position = ofVec2f(joints[0]->position.x, joints[1]->position.y);
joints[2]->position = ofVec2f(joints[1]->position.x, joints[2]->position.y);
joints[3]->position = ofVec2f(joints[0]->position.x, joints[2]->position.y);
break;
case 2:
joints[1]->position = ofVec2f(joints[2]->position.x, joints[1]->position.y);
joints[3]->position = ofVec2f(joints[3]->position.x, joints[2]->position.y);
joints[0]->position = ofVec2f(joints[3]->position.x, joints[1]->position.y);
break;
case 3:
joints[0]->position = ofVec2f(joints[3]->position.x, joints[0]->position.y);
joints[2]->position = ofVec2f(joints[2]->position.x, joints[3]->position.y);
joints[1]->position = ofVec2f(joints[2]->position.x, joints[0]->position.y);
break;
} // switch
}
CircleJoint * TextureEditor::hitTestJoints(ofVec2f pos){
for(int i = 0; i < joints.size(); i++){
if(joints[i]->hitTest(pos)){
return joints[i];
}
}
return NULL;
}
vector <CircleJoint *> & TextureEditor::getJoints(){
return joints;
}
if (bShiftKeyDown) } // namespace piMapper
moveStep = 10.0f; } // namespace ofx
else
moveStep = 0.5f;
switch (key) {
case OF_KEY_LEFT:
moveSelection(ofVec2f(-moveStep, 0.0f));
break;
case OF_KEY_RIGHT:
moveSelection(ofVec2f(moveStep, 0.0f));
break;
case OF_KEY_UP:
moveSelection(ofVec2f(0.0f, -moveStep));
break;
case OF_KEY_DOWN:
moveSelection(ofVec2f(0.0f, moveStep));
break;
case OF_KEY_SHIFT:
bShiftKeyDown = true;
break;
}
}
void TextureEditor::keyReleased(ofKeyEventArgs& args) {
int key = args.key;
switch (key) {
case OF_KEY_SHIFT:
bShiftKeyDown = false;
break;
}
}
void TextureEditor::draw() {
if (surface == NULL) return;
// Reset default color to white
ofSetColor(255, 255, 255, 255);
drawJoints();
}
void TextureEditor::drawJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->draw();
}
}
void TextureEditor::setSurface(BaseSurface* newSurface) {
surface = newSurface;
createJoints();
}
void TextureEditor::clear() {
surface = NULL;
clearJoints();
}
void TextureEditor::createJoints() {
if (surface == NULL) return;
clearJoints();
vector<ofVec2f>& texCoords = surface->getTexCoords();
ofVec2f textureSize = ofVec2f(surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight());
for (int i = 0; i < texCoords.size(); i++) {
joints.push_back(new CircleJoint());
joints.back()->position = texCoords[i] * textureSize;
}
}
void TextureEditor::clearJoints() {
while (joints.size()) {
delete joints.back();
joints.pop_back();
}
}
void TextureEditor::unselectAllJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->unselect();
}
}
void TextureEditor::moveTexCoords(ofVec2f by) {
if (surface == NULL) return;
vector<ofVec2f>& texCoords = surface->getTexCoords();
ofVec2f textureSize = ofVec2f(surface->getSource()->getTexture()->getWidth(),
surface->getSource()->getTexture()->getHeight());
for (int i = 0; i < texCoords.size(); i++) {
joints[i]->position += by;
surface->setTexCoord(i, joints[i]->position / textureSize);
}
}
void TextureEditor::stopDragJoints() {
for (int i = 0; i < joints.size(); i++) {
joints[i]->stopDrag();
}
}
void TextureEditor::moveSelection(ofVec2f by) {
// check if joints selected
bool bJointSelected = false;
BaseJoint* selectedJoint;
for (int i = 0; i < joints.size(); i++) {
if (joints[i]->isSelected()) {
bJointSelected = true;
selectedJoint = joints[i];
break;
}
}
if (bJointSelected) {
selectedJoint->position += by;
} else {
moveTexCoords(by);
}
}
void TextureEditor::constrainJointsToQuad(int selectedJointIndex)
{
switch (selectedJointIndex) {
case 0:
joints[1]->position = ofVec2f(joints[1]->position.x, joints[0]->position.y);
joints[2]->position = ofVec2f(joints[1]->position.x, joints[3]->position.y);
joints[3]->position = ofVec2f(joints[0]->position.x, joints[3]->position.y);
break;
case 1:
joints[0]->position = ofVec2f(joints[0]->position.x, joints[1]->position.y);
joints[2]->position = ofVec2f(joints[1]->position.x, joints[2]->position.y);
joints[3]->position = ofVec2f(joints[0]->position.x, joints[2]->position.y);
break;
case 2:
joints[1]->position = ofVec2f(joints[2]->position.x, joints[1]->position.y);
joints[3]->position = ofVec2f(joints[3]->position.x, joints[2]->position.y);
joints[0]->position = ofVec2f(joints[3]->position.x, joints[1]->position.y);
break;
case 3:
joints[0]->position = ofVec2f(joints[3]->position.x, joints[0]->position.y);
joints[2]->position = ofVec2f(joints[2]->position.x, joints[3]->position.y);
joints[1]->position = ofVec2f(joints[2]->position.x, joints[0]->position.y);
break;
} // switch
}
CircleJoint* TextureEditor::hitTestJoints(ofVec2f pos) {
for (int i = 0; i < joints.size(); i++) {
if (joints[i]->hitTest(pos)) {
return joints[i];
}
}
return NULL;
}
vector<CircleJoint*> & TextureEditor::getJoints(){
return joints;
}
}
}

72
src/UserInterface/TextureEditor.h

@ -7,39 +7,43 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class TextureEditor { class TextureEditor {
public:
TextureEditor(); public:
~TextureEditor(); TextureEditor();
~TextureEditor();
void registerAppEvents();
void unregisterAppEvents(); void registerAppEvents();
void registerKeyEvents(); void unregisterAppEvents();
void unregisterKeyEvents(); void registerKeyEvents();
void enable(); void unregisterKeyEvents();
void disable(); void enable();
void disable();
void update(ofEventArgs& args);
void keyPressed(ofKeyEventArgs& args); void update(ofEventArgs & args);
void keyReleased(ofKeyEventArgs& args); void keyPressed(ofKeyEventArgs & args);
void draw(); void keyReleased(ofKeyEventArgs & args);
void drawJoints(); void draw();
void setSurface(BaseSurface* newSurface); void drawJoints();
void clear(); void setSurface(BaseSurface * newSurface);
void createJoints(); void clear();
void clearJoints(); void createJoints();
void unselectAllJoints(); void clearJoints();
void moveTexCoords(ofVec2f by); void unselectAllJoints();
void stopDragJoints(); void moveTexCoords(ofVec2f by);
void moveSelection(ofVec2f by); void stopDragJoints();
void constrainJointsToQuad(int selectedJointIndex); void moveSelection(ofVec2f by);
CircleJoint* hitTestJoints(ofVec2f pos); void constrainJointsToQuad(int selectedJointIndex);
vector<CircleJoint*> & getJoints(); CircleJoint * hitTestJoints(ofVec2f pos);
vector <CircleJoint *> & getJoints();
private:
BaseSurface* surface; private:
vector<CircleJoint*> joints; BaseSurface * surface;
bool bShiftKeyDown; vector <CircleJoint *> joints;
bool bShiftKeyDown;
}; };
}
} } // namespace piMapper
} // namespace ofx

192
src/ofxPiMapper.cpp

@ -1,117 +1,117 @@
#include "ofxPiMapper.h" #include "ofxPiMapper.h"
ofxPiMapper::ofxPiMapper() { ofxPiMapper::ofxPiMapper(){
bShowInfo = false; bShowInfo = false;
isSetUp = false; isSetUp = false;
} }
void ofxPiMapper::setup() { void ofxPiMapper::setup(){
ofLogNotice("ofxPiMapper") << "Setting up..."; ofLogNotice("ofxPiMapper") << "Setting up...";
surfaceManager.setMediaServer(&mediaServer); surfaceManager.setMediaServer(&mediaServer);
gui.setMediaServer(&mediaServer); gui.setMediaServer(&mediaServer);
gui.setCmdManager(&cmdManager); gui.setCmdManager(&cmdManager);
if (ofFile::doesFileExist(PIMAPPER_USER_SURFACES_XML_FILE)){ if(ofFile::doesFileExist(PIMAPPER_USER_SURFACES_XML_FILE)){
ofLogNotice("ofxPiMapper") << "Loading user surfaces from " << PIMAPPER_USER_SURFACES_XML_FILE; ofLogNotice("ofxPiMapper") << "Loading user surfaces from " << PIMAPPER_USER_SURFACES_XML_FILE;
surfaceManager.loadXmlSettings(PIMAPPER_USER_SURFACES_XML_FILE); surfaceManager.loadXmlSettings(PIMAPPER_USER_SURFACES_XML_FILE);
} else { }else{
ofLogNotice("ofxPiMapper") << "Loading default surfaces from " << PIMAPPER_DEF_SURFACES_XML_FILE; ofLogNotice("ofxPiMapper") << "Loading default surfaces from " << PIMAPPER_DEF_SURFACES_XML_FILE;
surfaceManager.loadXmlSettings(PIMAPPER_DEF_SURFACES_XML_FILE); surfaceManager.loadXmlSettings(PIMAPPER_DEF_SURFACES_XML_FILE);
} }
gui.setSurfaceManager(&surfaceManager); gui.setSurfaceManager(&surfaceManager);
isSetUp = true; isSetUp = true;
ofLogNotice("ofxPiMapper") << "Done setting up"; ofLogNotice("ofxPiMapper") << "Done setting up";
_application = new ofx::piMapper::Application(this); _application = new ofx::piMapper::Application(this);
} }
void ofxPiMapper::draw() { void ofxPiMapper::draw(){
if (!isSetUp) { if(!isSetUp){
return; return;
} }
gui.draw(); gui.draw();
if (bShowInfo){ if(bShowInfo){
stringstream ss; stringstream ss;
ss << "There are 4 modes:\n\n"; ss << "There are 4 modes:\n\n";
ss << " 1. Presentation mode\n"; ss << " 1. Presentation mode\n";
ss << " 2. Texture mapping mode\n"; ss << " 2. Texture mapping mode\n";
ss << " 3. Projection mapping mode\n"; ss << " 3. Projection mapping mode\n";
ss << " 4. Source selection mode\n\n"; ss << " 4. Source selection mode\n\n";
ss << "You can switch between the modes by using <1>, <2>, <3> and <4> " ss << "You can switch between the modes by using <1>, <2>, <3> and <4> "
"keys on the keyboard.\n\n"; "keys on the keyboard.\n\n";
ss << "Press <t> to add new triangle surface\n"; ss << "Press <t> to add new triangle surface\n";
ss << "Press <q> to add new quad surface\n"; ss << "Press <q> to add new quad surface\n";
ss << "Press <s> to save the composition\n"; ss << "Press <s> to save the composition\n";
ss << "Press <f> to toggle fullscreen\n"; ss << "Press <f> to toggle fullscreen\n";
ss << "Press <i> to hide this message"; ss << "Press <i> to hide this message";
ofDrawBitmapStringHighlight(ss.str(), 10, 20, ofDrawBitmapStringHighlight(ss.str(), 10, 20,
ofColor(0, 0, 0, 100), ofColor(0, 0, 0, 100),
ofColor(255, 255, 255, 200)); ofColor(255, 255, 255, 200));
} }
_application->draw(); _application->draw();
} // draw } // draw
void ofxPiMapper::registerFboSource(ofx::piMapper::FboSource & fboSource) { void ofxPiMapper::registerFboSource(ofx::piMapper::FboSource & fboSource){
mediaServer.addFboSource(fboSource); mediaServer.addFboSource(fboSource);
} }
void ofxPiMapper::addTriangleSurface() { void ofxPiMapper::addTriangleSurface(){
int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE; int surfaceType = ofx::piMapper::SurfaceType::TRIANGLE_SURFACE;
vector<ofVec2f> vertices; vector <ofVec2f> vertices;
float margin = 50.0f; float margin = 50.0f;
vertices.push_back(ofVec2f((float)ofGetWidth() / 2.0f, margin)); vertices.push_back(ofVec2f((float)ofGetWidth() / 2.0f, margin));
vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin)); vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin));
vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin)); vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin));
vector<ofVec2f> texCoords; vector <ofVec2f> texCoords;
texCoords.push_back(ofVec2f(0.5f, 0.0f)); texCoords.push_back(ofVec2f(0.5f, 0.0f));
texCoords.push_back(ofVec2f(1.0f, 1.0f)); texCoords.push_back(ofVec2f(1.0f, 1.0f));
texCoords.push_back(ofVec2f(0.0f, 1.0f)); texCoords.push_back(ofVec2f(0.0f, 1.0f));
surfaceManager.addSurface(surfaceType, vertices, texCoords); surfaceManager.addSurface(surfaceType, vertices, texCoords);
// Select this surface right away // Select this surface right away
surfaceManager.selectSurface(surfaceManager.size() - 1); surfaceManager.selectSurface(surfaceManager.size() - 1);
} // addTriangleSurface } // addTriangleSurface
void ofxPiMapper::addQuadSurface() { void ofxPiMapper::addQuadSurface(){
int surfaceType = ofx::piMapper::SurfaceType::QUAD_SURFACE; int surfaceType = ofx::piMapper::SurfaceType::QUAD_SURFACE;
vector<ofVec2f> vertices; vector <ofVec2f> vertices;
float margin = 50.0f; float margin = 50.0f;
vertices.push_back(ofVec2f(margin, margin)); vertices.push_back(ofVec2f(margin, margin));
vertices.push_back(ofVec2f((float)ofGetWidth() - margin, margin)); vertices.push_back(ofVec2f((float)ofGetWidth() - margin, margin));
vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin)); vertices.push_back(ofVec2f((float)ofGetWidth() - margin, (float)ofGetHeight() - margin));
vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin)); vertices.push_back(ofVec2f(margin, (float)ofGetHeight() - margin));
vector<ofVec2f> texCoords; vector <ofVec2f> texCoords;
texCoords.push_back(ofVec2f(ofVec2f(0.0f, 0.0f))); texCoords.push_back(ofVec2f(ofVec2f(0.0f, 0.0f)));
texCoords.push_back(ofVec2f(ofVec2f(1.0f, 0.0f))); texCoords.push_back(ofVec2f(ofVec2f(1.0f, 0.0f)));
texCoords.push_back(ofVec2f(ofVec2f(1.0f, 1.0f))); texCoords.push_back(ofVec2f(ofVec2f(1.0f, 1.0f)));
texCoords.push_back(ofVec2f(ofVec2f(0.0f, 1.0f))); texCoords.push_back(ofVec2f(ofVec2f(0.0f, 1.0f)));
surfaceManager.addSurface(surfaceType, vertices, texCoords); surfaceManager.addSurface(surfaceType, vertices, texCoords);
// select this surface right away // select this surface right away
surfaceManager.selectSurface(surfaceManager.size() - 1); surfaceManager.selectSurface(surfaceManager.size() - 1);
} // addQuadSurface } // addQuadSurface
ofx::piMapper::CmdManager & ofxPiMapper::getCmdManager() { ofx::piMapper::CmdManager & ofxPiMapper::getCmdManager(){
return cmdManager; return cmdManager;
} }
ofx::piMapper::SurfaceManagerGui & ofxPiMapper::getGui() { ofx::piMapper::SurfaceManagerGui & ofxPiMapper::getGui(){
return gui; return gui;
} }
ofx::piMapper::MediaServer & ofxPiMapper::getMediaServer() { ofx::piMapper::MediaServer & ofxPiMapper::getMediaServer(){
return mediaServer; return mediaServer;
} }
ofx::piMapper::SurfaceManager & ofxPiMapper::getSurfaceManager() { ofx::piMapper::SurfaceManager & ofxPiMapper::getSurfaceManager(){
return surfaceManager; return surfaceManager;
} }

66
src/ofxPiMapper.h

@ -8,41 +8,49 @@
#include "BaseCmd.h" #include "BaseCmd.h"
#include "CmdManager.h" #include "CmdManager.h"
#include "RmSurfaceCmd.h" #include "RmSurfaceCmd.h"
#include "Application.h" #include "Application.h"
#define PIMAPPER_DEF_SURFACES_XML_FILE "defaultSurfaces.xml" #define PIMAPPER_DEF_SURFACES_XML_FILE "defaultSurfaces.xml"
#define PIMAPPER_USER_SURFACES_XML_FILE "surfaces.xml" #define PIMAPPER_USER_SURFACES_XML_FILE "surfaces.xml"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class Application; class Application;
} }
} }
class ofxPiMapper { class ofxPiMapper {
public:
ofxPiMapper(); public:
ofxPiMapper();
void setup();
void draw(); void setup();
void registerFboSource(ofx::piMapper::FboSource & fboSource); void draw();
void addTriangleSurface(); void registerFboSource(ofx::piMapper::FboSource & fboSource);
void addQuadSurface(); void addTriangleSurface();
void showInfo() { bShowInfo = true; }; void addQuadSurface();
void hideInfo() { bShowInfo = false; }; void showInfo(){
void toggleInfo() { bShowInfo = !bShowInfo; } bShowInfo = true;
}
ofx::piMapper::CmdManager & getCmdManager(); void hideInfo(){
ofx::piMapper::SurfaceManagerGui & getGui(); bShowInfo = false;
ofx::piMapper::MediaServer & getMediaServer(); }
ofx::piMapper::SurfaceManager & getSurfaceManager(); void toggleInfo(){
ofx::piMapper::CmdManager cmdManager; bShowInfo = !bShowInfo;
ofx::piMapper::SurfaceManager surfaceManager; }
private: ofx::piMapper::CmdManager & getCmdManager();
bool isSetUp; ofx::piMapper::SurfaceManagerGui & getGui();
bool bShowInfo; ofx::piMapper::MediaServer & getMediaServer();
ofx::piMapper::MediaServer mediaServer; ofx::piMapper::SurfaceManager & getSurfaceManager();
ofx::piMapper::SurfaceManagerGui gui; ofx::piMapper::CmdManager cmdManager;
ofx::piMapper::Application * _application; ofx::piMapper::SurfaceManager surfaceManager;
private:
bool isSetUp;
bool bShowInfo;
ofx::piMapper::MediaServer mediaServer;
ofx::piMapper::SurfaceManagerGui gui;
ofx::piMapper::Application * _application;
}; };
Loading…
Cancel
Save