Browse Source

Change src code style using ofStyler

master
Krisjanis Rijnieks 10 years ago
parent
commit
95b366ada5
  1. 180
      src/Application/Application.cpp
  2. 40
      src/Application/Application.h
  3. 10
      src/Application/ApplicationBaseState.cpp
  4. 22
      src/Application/ApplicationBaseState.h
  5. 23
      src/Application/PresentationState.cpp
  6. 20
      src/Application/PresentationState.h
  7. 85
      src/Application/ProjectionMappingState.cpp
  8. 22
      src/Application/ProjectionMappingState.h
  9. 23
      src/Application/SourceSelectionState.cpp
  10. 20
      src/Application/SourceSelectionState.h
  11. 23
      src/Application/TextureMappingState.cpp
  12. 20
      src/Application/TextureMappingState.h
  13. 114
      src/Commands/AddSurfaceCmd.cpp
  14. 31
      src/Commands/AddSurfaceCmd.h
  15. 66
      src/Commands/BaseCmd.h
  16. 42
      src/Commands/CmdManager.cpp
  17. 22
      src/Commands/CmdManager.h
  18. 38
      src/Commands/MvAllTexCoordsCmd.cpp
  19. 27
      src/Commands/MvAllTexCoordsCmd.h
  20. 41
      src/Commands/MvSurfaceCmd.cpp
  21. 30
      src/Commands/MvSurfaceCmd.h
  22. 43
      src/Commands/MvSurfaceVertCmd.cpp
  23. 42
      src/Commands/MvSurfaceVertCmd.h
  24. 36
      src/Commands/MvTexCoordCmd.cpp
  25. 27
      src/Commands/MvTexCoordCmd.h
  26. 44
      src/Commands/RmSurfaceCmd.cpp
  27. 25
      src/Commands/RmSurfaceCmd.h
  28. 49
      src/Commands/SelSurfaceCmd.cpp
  29. 42
      src/Commands/SelSurfaceCmd.h
  30. 56
      src/Commands/SetApplicationStateCmd.cpp
  31. 44
      src/Commands/SetApplicationStateCmd.h
  32. 106
      src/Commands/SetSourceCmd.cpp
  33. 41
      src/Commands/SetSourceCmd.h
  34. 61
      src/MediaServer/DirectoryWatcher.cpp
  35. 232
      src/MediaServer/DirectoryWatcher.h
  36. 688
      src/MediaServer/MediaServer.cpp
  37. 159
      src/MediaServer/MediaServer.h
  38. 112
      src/Sources/BaseSource.cpp
  39. 70
      src/Sources/BaseSource.h
  40. 222
      src/Sources/FboSource.cpp
  41. 70
      src/Sources/FboSource.h
  42. 63
      src/Sources/ImageSource.cpp
  43. 30
      src/Sources/ImageSource.h
  44. 88
      src/Sources/SourceType.h
  45. 117
      src/Sources/VideoSource.cpp
  46. 55
      src/Sources/VideoSource.h
  47. 166
      src/Surfaces/BaseSurface.cpp
  48. 74
      src/Surfaces/BaseSurface.h
  49. 470
      src/Surfaces/QuadSurface.cpp
  50. 63
      src/Surfaces/QuadSurface.h
  51. 773
      src/Surfaces/SurfaceManager.cpp
  52. 82
      src/Surfaces/SurfaceManager.h
  53. 625
      src/Surfaces/SurfaceManagerGui.cpp
  54. 80
      src/Surfaces/SurfaceManagerGui.h
  55. 16
      src/Surfaces/SurfaceType.h
  56. 202
      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. 551
      src/UserInterface/ProjectionEditor.cpp
  65. 94
      src/UserInterface/ProjectionEditor.h
  66. 394
      src/UserInterface/RadioList.cpp
  67. 94
      src/UserInterface/RadioList.h
  68. 731
      src/UserInterface/SourcesEditor.cpp
  69. 150
      src/UserInterface/SourcesEditor.h
  70. 432
      src/UserInterface/TextureEditor.cpp
  71. 72
      src/UserInterface/TextureEditor.h
  72. 178
      src/ofxPiMapper.cpp
  73. 64
      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

40
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);
~Application();
ApplicationBaseState * getState(); public:
ofxPiMapper * getOfxPiMapper(); // Temporary method. Application(ofxPiMapper * opm);
~Application();
void draw(); ApplicationBaseState * getState();
void onKeyPressed(ofKeyEventArgs & args); ofxPiMapper * getOfxPiMapper(); // Temporary method.
protected: void draw();
void setState(ApplicationBaseState * st); void onKeyPressed(ofKeyEventArgs & args);
private: protected:
friend class ApplicationBaseState; void setState(ApplicationBaseState * st);
friend class SetApplicationStateCmd;
ApplicationBaseState * _state; private:
ofxPiMapper * _ofxPiMapper; friend class ApplicationBaseState;
}; friend class SetApplicationStateCmd;
} // namespace piMapper ApplicationBaseState * _state;
ofxPiMapper * _ofxPiMapper;
};
} // namespace piMapper
} // namespace ofx } // namespace ofx

10
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

22
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){};
virtual void setState(Application * app, ApplicationBaseState * st);
// Event handler virtual methods public:
virtual void onKeyPressed(Application * app, ofKeyEventArgs & args){}; virtual void draw(Application * app){}
}; virtual void setState(Application * app, ApplicationBaseState * st);
} // namespace piMapper // Event handler virtual methods
virtual void onKeyPressed(Application * app, ofKeyEventArgs & args){}
};
} // namespace piMapper
} // namespace ofx } // namespace ofx

23
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

20
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();
void draw(Application * app);
private: public:
static PresentationState * _instance; static PresentationState * instance();
}; void draw(Application * app);
} // namespace piMapper private:
static PresentationState * _instance;
};
} // namespace piMapper
} // namespace ofx } // namespace ofx

85
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::onKeyPressed(Application * app, ofKeyEventArgs & args) {
switch (args.key) {
case 't':
app->getOfxPiMapper()->getCmdManager().exec(
new AddSurfaceCmd(
app->getOfxPiMapper(),
SurfaceType::TRIANGLE_SURFACE)
);
break;
case 'q':
app->getOfxPiMapper()->getCmdManager().exec(
new AddSurfaceCmd(
app->getOfxPiMapper(),
SurfaceType::QUAD_SURFACE)
);
break;
case OF_KEY_BACKSPACE:
app->getOfxPiMapper()->getCmdManager().exec(
new RmSurfaceCmd(app->getOfxPiMapper()));
break;
default:
break;
}
}
}
} }
void ProjectionMappingState::draw(Application * app){}
void ProjectionMappingState::onKeyPressed(Application * app, ofKeyEventArgs & args){
switch(args.key){
case 't':
app->getOfxPiMapper()->getCmdManager().exec(
new AddSurfaceCmd(
app->getOfxPiMapper(),
SurfaceType::TRIANGLE_SURFACE)
);
break;
case 'q':
app->getOfxPiMapper()->getCmdManager().exec(
new AddSurfaceCmd(
app->getOfxPiMapper(),
SurfaceType::QUAD_SURFACE)
);
break;
case OF_KEY_BACKSPACE:
app->getOfxPiMapper()->getCmdManager().exec(
new RmSurfaceCmd(app->getOfxPiMapper()));
break;
default:
break;
}
}
} // namespace piMapper
} // namespace ofx

22
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();
void draw(Application * app);
void onKeyPressed(Application * app, ofKeyEventArgs & args);
private: public:
static ProjectionMappingState * _instance; static ProjectionMappingState * instance();
}; void draw(Application * app);
void onKeyPressed(Application * app, ofKeyEventArgs & args);
} // namespace piMapper private:
static ProjectionMappingState * _instance;
};
} // namespace piMapper
} // namespace ofx } // namespace ofx

23
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

20
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();
void draw(Application * app);
private: public:
static SourceSelectionState * _instance; static SourceSelectionState * instance();
}; void draw(Application * app);
} // namespace piMapper private:
static SourceSelectionState * _instance;
};
} // namespace piMapper
} // namespace ofx } // namespace ofx

23
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

20
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();
void draw(Application * app);
private: public:
static TextureMappingState * _instance; static TextureMappingState * instance();
}; void draw(Application * app);
} // namespace piMapper private:
static TextureMappingState * _instance;
};
} // 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

31
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

42
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

22
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);
void undo();
private: public:
std::vector<BaseUndoCmd *> cmdStack; void exec(BaseCmd * cmd);
}; void undo();
} // namespace piMapper private:
std::vector <BaseUndoCmd *> cmdStack;
};
} // namespace piMapper
} // namespace ofx } // namespace ofx

38
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){ MvAllTexCoordsCmd::MvAllTexCoordsCmd(BaseSurface * surface, TextureEditor * texEditor){
_surface = surface; _surface = surface;
_texEditor = texEditor; _texEditor = texEditor;
} }
void MvAllTexCoordsCmd::exec(){ void MvAllTexCoordsCmd::exec(){
ofLogNotice("MvAllTexCoordsCmd", "exec"); ofLogNotice("MvAllTexCoordsCmd", "exec");
_texCoords = _surface->getTexCoords(); _texCoords = _surface->getTexCoords();
} }
void MvAllTexCoordsCmd::undo(){ void MvAllTexCoordsCmd::undo(){
ofLogNotice("MvAllTexCoordsCmd", "undo"); ofLogNotice("MvAllTexCoordsCmd", "undo");
ofVec2f dist = _texCoords[0] - _surface->getTexCoords()[0]; ofVec2f dist = _texCoords[0] - _surface->getTexCoords()[0];
dist.x = _surface->getSource()->getTexture()->getWidth() * dist.x; dist.x = _surface->getSource()->getTexture()->getWidth() * dist.x;
dist.y = _surface->getSource()->getTexture()->getHeight() * dist.y; dist.y = _surface->getSource()->getTexture()->getHeight() * dist.y;
_texEditor->moveTexCoords(dist); _texEditor->moveTexCoords(dist);
_surface = 0; _surface = 0;
} }
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

27
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

41
src/Commands/MvSurfaceCmd.cpp

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

30
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

43
src/Commands/MvSurfaceVertCmd.cpp

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

36
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){ MvTexCoordCmd::MvTexCoordCmd(int jointIndex, TextureEditor * texEditor){
_jointIndex = jointIndex; _jointIndex = jointIndex;
_texEditor = texEditor; _texEditor = texEditor;
} }
void MvTexCoordCmd::exec(){ void MvTexCoordCmd::exec(){
ofLogNotice("MvTexCoordCmd", "exec"); ofLogNotice("MvTexCoordCmd", "exec");
_jointPosition = _texEditor->getJoints()[_jointIndex]->position; _jointPosition = _texEditor->getJoints()[_jointIndex]->position;
} }
void MvTexCoordCmd::undo(){ void MvTexCoordCmd::undo(){
ofLogNotice("MvTexCoordCmd", "undo"); ofLogNotice("MvTexCoordCmd", "undo");
_texEditor->unselectAllJoints(); _texEditor->unselectAllJoints();
_texEditor->getJoints()[_jointIndex]->select(); _texEditor->getJoints()[_jointIndex]->select();
_texEditor->getJoints()[_jointIndex]->position = _jointPosition; _texEditor->getJoints()[_jointIndex]->position = _jointPosition;
_texEditor = 0; _texEditor = 0;
} }
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

27
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

44
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){ RmSurfaceCmd::RmSurfaceCmd(ofxPiMapper * app){
_app = app; _app = app;
_surface = 0; _surface = 0;
} }
void RmSurfaceCmd::exec(){ void RmSurfaceCmd::exec(){
// Store the surface, this implies that the surfaceManager's // Store the surface, this implies that the surfaceManager's
// removeSelectedSurface does not destroy the surface. // removeSelectedSurface does not destroy the surface.
_surface = _app->surfaceManager.getSelectedSurface(); _surface = _app->surfaceManager.getSelectedSurface();
_app->surfaceManager.removeSelectedSurface(); _app->surfaceManager.removeSelectedSurface();
} }
void RmSurfaceCmd::undo(){ void RmSurfaceCmd::undo(){
ofLogNotice("RmSurfaceCmd", "undo"); ofLogNotice("RmSurfaceCmd", "undo");
if (_surface == 0) { if(_surface == 0){
ofLogError("RmSurfaceCmd", "No surface stored"); ofLogError("RmSurfaceCmd", "No surface stored");
} }
_app->surfaceManager.addSurface(_surface); _app->surfaceManager.addSurface(_surface);
_app->surfaceManager.selectSurface(_surface); _app->surfaceManager.selectSurface(_surface);
_surface = 0; _surface = 0;
} }
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx

25
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

49
src/Commands/SelSurfaceCmd.cpp

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

56
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, _application = app;
int mode) { _prevApplicationState = 0;
_applicationState = st;
_application = app; // TODO: To be removed
_prevApplicationState = 0; _gui = gui;
_applicationState = st; _prevGuiMode = -1;
_mode = mode;
}
// TODO: To be removed void SetApplicationStateCmd::exec(){
_gui = gui; _prevApplicationState = _application->getState();
_prevGuiMode = -1; _application->setState(_applicationState);
_mode = mode;
}
void SetApplicationStateCmd::exec() { // TODO: To be removed.
_prevApplicationState = _application->getState(); _prevGuiMode = _gui->getMode();
_application->setState(_applicationState); _gui->setMode(_mode);
}
// TODO: To be removed. void SetApplicationStateCmd::undo(){
_prevGuiMode = _gui->getMode(); ofLogNotice("SetApplicationStateCmd", "undo");
_gui->setMode(_mode); _application->setState(_prevApplicationState);
}
void SetApplicationStateCmd::undo() { // TODO: To be removed.
ofLogNotice("SetApplicationStateCmd", "undo"); _gui->setMode(_prevGuiMode);
_application->setState(_prevApplicationState); }
// TODO: To be removed. } // namespace piMapper
_gui->setMode(_prevGuiMode);
}
} // namespace piMapper
} // namespace ofx } // namespace ofx

44
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

41
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{
class SourcesEditor; public:
SetSourceCmd(int sourceType,
string sourceId,
BaseSurface * surface,
SourcesEditor * sourcesEditor);
void exec();
void undo();
class SetSourceCmd : public BaseUndoCmd{ private:
int _sourceType;
string _sourceId;
BaseSurface * _surface;
SourcesEditor * _sourcesEditor;
public: int _oldSourceType;
SetSourceCmd(int sourceType, string _oldSourceId;
string sourceId,
BaseSurface * surface,
SourcesEditor * sourcesEditor);
void exec();
void undo();
private: };
int _sourceType;
string _sourceId;
BaseSurface * _surface;
SourcesEditor * _sourcesEditor;
int _oldSourceType; } // namespace piMapper
string _oldSourceId;
};
} // namespace piMapper
} // namespace ofx } // namespace ofx

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

232
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

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

112
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;
}
bool BaseSource::isLoadable() {
return loadable;
}
bool BaseSource::isLoaded() {
return loaded;
}
int BaseSource::getType() {
return type;
}
std::string& BaseSource::getPath() {
return path;
}
void BaseSource::init() {
texture = NULL;
name = "";
path = "";
loadable = false;
loaded = false;
type = SourceType::SOURCE_TYPE_NONE;
referenceCount = 1; // We have one instance on init
}
void BaseSource::setNameFromPath(std::string& fullPath) {
std::vector<string> pathParts;
//cout << "fullPath: " << fullPath << endl;
pathParts = ofSplitString(fullPath, "/"); // Maybe on win "/" is "\", have to test
//cout << "lastPathPart: " << pathParts[pathParts.size() - 1] << endl;
name = pathParts[pathParts.size() - 1];
}
}
} }
std::string & BaseSource::getName(){
return name;
}
bool BaseSource::isLoadable(){
return loadable;
}
bool BaseSource::isLoaded(){
return loaded;
}
int BaseSource::getType(){
return type;
}
std::string & BaseSource::getPath(){
return path;
}
void BaseSource::init(){
texture = NULL;
name = "";
path = "";
loadable = false;
loaded = false;
type = SourceType::SOURCE_TYPE_NONE;
referenceCount = 1; // We have one instance on init
}
void BaseSource::setNameFromPath(std::string & fullPath){
std::vector <string> pathParts;
//cout << "fullPath: " << fullPath << endl;
pathParts = ofSplitString(fullPath, "/"); // Maybe on win "/" is "\", have to test
//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

63
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;
loadable = true;
loaded = false;
type = SourceType::SOURCE_TYPE_IMAGE;
}
ImageSource::~ImageSource() {} ImageSource::ImageSource(){
//cout << "ImageSource" << endl;
loadable = true;
loaded = false;
type = SourceType::SOURCE_TYPE_IMAGE;
}
void ImageSource::loadImage(std::string& filePath) { ImageSource::~ImageSource(){}
path = filePath;
//cout << "loading image: " << filePath << endl;
setNameFromPath(filePath);
//cout << "path: " << path << endl;
image = new ofImage();
if (!image->loadImage(filePath)) {
ofLogWarning("ImageSource") << "Could not load image";
//std::exit(EXIT_FAILURE);
}
texture = &image->getTextureReference();
loaded = true;
}
void ImageSource::clear() { void ImageSource::loadImage(std::string & filePath){
texture = NULL; path = filePath;
image->clear(); //cout << "loading image: " << filePath << endl;
delete image; setNameFromPath(filePath);
image = NULL; //cout << "path: " << path << endl;
//path = ""; image = new ofImage();
//name = ""; if(!image->loadImage(filePath)){
loaded = false; ofLogWarning("ImageSource") << "Could not load image";
} //std::exit(EXIT_FAILURE);
}
texture = &image->getTextureReference();
loaded = true;
}
} void ImageSource::clear(){
texture = NULL;
image->clear();
delete image;
image = NULL;
//path = "";
//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

117
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

55
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 class VideoSource : public BaseSource {
// for live audio enabling and disabling.
static bool enableAudio;
VideoSource(); public:
~VideoSource();
std::string & getPath(); // TODO: Create enableAudio() and disableAudio() methods
void loadVideo(std::string & path); // for live audio enabling and disabling.
void clear(); static bool enableAudio;
#ifndef TARGET_RASPBERRY_PI VideoSource();
void update(ofEventArgs & args); ~VideoSource();
#endif
private: std::string & getPath();
void loadVideo(std::string & path);
void clear();
#ifdef TARGET_RASPBERRY_PI #ifndef TARGET_RASPBERRY_PI
ofxOMXPlayer * omxPlayer; // Naming different for less confusion void update(ofEventArgs & args);
#else #endif
// Go with ofVideoPlayer or
// TODO: High Performance Video player on newer Macs private:
ofVideoPlayer * videoPlayer;
#endif #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
}

166
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 // load pixels into texture
defaultTexture.loadData(pixels); defaultTexture.loadData(pixels);
// Create new source to be the default // Create new source to be the default
defaultSource = new BaseSource(&defaultTexture); defaultSource = new BaseSource(&defaultTexture);
source = defaultSource; source = defaultSource;
} }
void BaseSurface::drawTexture(ofVec2f position) { void BaseSurface::drawTexture(ofVec2f position){
if (source->getTexture() == NULL) { if(source->getTexture() == NULL){
ofLogWarning("BaseSurface") << "Source texture empty. Not drawing."; ofLogWarning("BaseSurface") << "Source texture empty. Not drawing.";
return; return;
} }
ofMesh texMesh; ofMesh texMesh;
texMesh.addVertex(position); texMesh.addVertex(position);
texMesh.addVertex(position + ofVec2f(source->getTexture()->getWidth(), 0.0f)); texMesh.addVertex(position + ofVec2f(source->getTexture()->getWidth(), 0.0f));
texMesh.addVertex(position + texMesh.addVertex(position
ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight())); + ofVec2f(source->getTexture()->getWidth(), source->getTexture()->getHeight()));
texMesh.addVertex(position + ofVec2f(0.0f, source->getTexture()->getHeight())); texMesh.addVertex(position + ofVec2f(0.0f, source->getTexture()->getHeight()));
texMesh.addTriangle(0, 2, 3); texMesh.addTriangle(0, 2, 3);
texMesh.addTriangle(0, 1, 2); texMesh.addTriangle(0, 1, 2);
texMesh.addTexCoord(ofVec2f(0.0f, 0.0f)); texMesh.addTexCoord(ofVec2f(0.0f, 0.0f));
texMesh.addTexCoord(ofVec2f(1.0f, 0.0f)); texMesh.addTexCoord(ofVec2f(1.0f, 0.0f));
texMesh.addTexCoord(ofVec2f(1.0f, 1.0f)); texMesh.addTexCoord(ofVec2f(1.0f, 1.0f));
texMesh.addTexCoord(ofVec2f(0.0f, 1.0f)); texMesh.addTexCoord(ofVec2f(0.0f, 1.0f));
source->getTexture()->bind(); source->getTexture()->bind();
texMesh.draw(); texMesh.draw();
source->getTexture()->unbind(); source->getTexture()->unbind();
} }
//void BaseSurface::setTexture(ofTexture* texturePtr) { texture = texturePtr; } //void BaseSurface::setTexture(ofTexture* texturePtr) { texture = texturePtr; }
void BaseSurface::setSource(BaseSource* newSource) { void BaseSurface::setSource(BaseSource * newSource){
source = newSource; source = newSource;
} }
//ofTexture* BaseSurface::getTexture() { return texture; } //ofTexture* BaseSurface::getTexture() { return texture; }
BaseSource* BaseSurface::getSource() { BaseSource * BaseSurface::getSource(){
return source; return source;
} }
//ofTexture* BaseSurface::getDefaultTexture() { return &defaultTexture; } //ofTexture* BaseSurface::getDefaultTexture() { return &defaultTexture; }
BaseSource* BaseSurface::getDefaultSource() { BaseSource * BaseSurface::getDefaultSource(){
return defaultSource; return defaultSource;
} }
void BaseSurface::setMoved(bool moved){ _moved = moved; } void BaseSurface::setMoved(bool moved){
bool BaseSurface::getMoved(){ return _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

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

773
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) {
if (index >= surfaces.size()) {
throw std::runtime_error("Surface index out of bounds.");
}
selectedSurface = surfaces[index];
// notify that a new surface has been selected
ofSendMessage("surfaceSelected");
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(int index){
selectedSurface = NULL; if(index >= surfaces.size()){
throw std::runtime_error("Surface index out of bounds.");
}
selectedSurface = surfaces[index];
// notify that a new surface has been selected
ofSendMessage("surfaceSelected");
return selectedSurface;
} }
BaseSurface* SurfaceManager::getSurface(int index) { BaseSurface * SurfaceManager::selectSurface(BaseSurface * surface){
if (index >= surfaces.size()) { for(int i = 0; i < surfaces.size(); i++){
throw std::runtime_error("Surface index out of bounds."); if(surfaces[i] == surface){
return NULL; selectedSurface = surface;
} ofSendMessage("surfaceSelected");
return selectedSurface;
}
}
deselectSurface();
return 0;
}
return surfaces[index]; BaseSurface * SurfaceManager::getSelectedSurface(){
return selectedSurface;
} }
int SurfaceManager::size() { return surfaces.size(); } void SurfaceManager::deselectSurface(){
selectedSurface = NULL;
} }
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

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

202
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))){
void BaseJoint::mouseDragged(ofMouseEventArgs& args) { // selected = true;
if (!bDrag) return; clickDistance = position - ofVec2f(args.x, args.y);
position = ofVec2f(args.x, args.y) + clickDistance; // startDrag();
}
} }
void BaseJoint::startDrag() { bDrag = true; } void BaseJoint::mouseReleased(int x, int y, int button){
stopDrag();
}
void BaseJoint::stopDrag() { bDrag = false; } void BaseJoint::mouseDragged(ofMouseEventArgs & args){
if(!bDrag){
return;
}
position = ofVec2f(args.x, args.y) + clickDistance;
}
void BaseJoint::select() { selected = true; } void BaseJoint::startDrag(){
bDrag = true;
}
void BaseJoint::unselect() { selected = false; } void BaseJoint::stopDrag(){
bDrag = false;
}
void BaseJoint::setClickDistance(ofVec2f newClickDistance) { void BaseJoint::select(){
clickDistance = newClickDistance; selected = true;
} }
bool BaseJoint::isDragged() { return bDrag; } void BaseJoint::unselect(){
selected = false;
}
bool BaseJoint::isSelected() { return selected; } void BaseJoint::setClickDistance(ofVec2f newClickDistance){
clickDistance = newClickDistance;
}
void BaseJoint::setDefaultColors() { bool BaseJoint::isDragged(){
fillColor = ofColor(0, 255, 255, 0); return bDrag;
strokeColor = ofColor(255, 255, 255);
fillColorSelected = ofColor(255, 255, 0, 0);
strokeColorSelected = ofColor(255, 0, 0);
} }
void BaseJoint::setDefaultProperties() { bool BaseJoint::isSelected(){
enabled = true; return selected;
visible = true;
position = ofVec2f(20.0f, 20.0f);
clickDistance = ofVec2f(0.0f, 0.0f);
bDrag = false;
selected = false;
strokeWidth = 1.5f;
} }
void BaseJoint::setDefaultColors(){
fillColor = ofColor(0, 255, 255, 0);
strokeColor = ofColor(255, 255, 255);
fillColorSelected = ofColor(255, 255, 0, 0);
strokeColorSelected = ofColor(255, 0, 0);
} }
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){
bool CircleJoint::hitTest(ofVec2f pos) { return;
float distance = position.distance(pos); }
if (distance < radius) if(!enabled){
return true; return;
else }
return false;
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;
} }
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

551
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::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 {
moveSelectedSurface(by);
}
}
void ProjectionEditor::setSnapDistance(float newSnapDistance) {
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();
}
}
}
} }
void ProjectionEditor::moveSelectedSurface(ofVec2f by){
if(surfaceManager == NULL){
return;
}
if(surfaceManager->getSelectedSurface() == NULL){
return;
}
surfaceManager->getSelectedSurface()->moveBy(by);
/*vector<ofVec3f>& vertices =
surfaceManager->getSelectedSurface()->getVertices();
for (int i=0; i<vertices.size(); i++) {
vertices[i] += by;
}*/
updateJoints();
}
void ProjectionEditor::stopDragJoints(){
for(int i = 0; i < joints.size(); i++){
joints[i]->stopDrag();
}
}
void ProjectionEditor::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{
moveSelectedSurface(by);
}
}
void ProjectionEditor::setSnapDistance(float newSnapDistance){
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

731
src/UserInterface/SourcesEditor.cpp

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

150
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

432
src/UserInterface/TextureEditor.cpp

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

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

178
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;
} }

64
src/ofxPiMapper.h

@ -14,35 +14,43 @@
#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