diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp index 4ca3766..017e866 100644 --- a/example/src/ofApp.cpp +++ b/example/src/ofApp.cpp @@ -13,7 +13,6 @@ void ofApp::setup() surfaceManager.getSurface(1)->setVertex(2, ofVec2f(300, 400)); gui.setSurfaceManager( &surfaceManager ); - gui.selectSurface(1); } void ofApp::update() diff --git a/src/ofxBaseSurface.h b/src/ofxBaseSurface.h index e038473..3b07046 100644 --- a/src/ofxBaseSurface.h +++ b/src/ofxBaseSurface.h @@ -17,6 +17,7 @@ public: virtual int getType(){}; virtual bool hitTest(ofVec2f p){}; virtual ofPolyline getHitArea(){}; + virtual vector& getVertices(){}; // Draws a texture using ofMesh void drawTexture(ofVec2f position); diff --git a/src/ofxProjectionEditor.cpp b/src/ofxProjectionEditor.cpp index 1b6b861..cc7f377 100644 --- a/src/ofxProjectionEditor.cpp +++ b/src/ofxProjectionEditor.cpp @@ -2,15 +2,69 @@ ofxProjectionEditor::ofxProjectionEditor() { - + surfaceManager = NULL; } ofxProjectionEditor::~ofxProjectionEditor() { - + clearJoints(); + surfaceManager = NULL; } void ofxProjectionEditor::draw() { + if ( surfaceManager == NULL ) return; + cout << "ofxProjectionEditor::draw()" << endl; + if ( surfaceManager->getSelectedSurface() == NULL ) return; + if ( joints.size() <= 0 ) createJoints(); + drawJoints(); +} + +void ofxProjectionEditor::setSurfaceManager(ofxSurfaceManager *newSurfaceManager) +{ + surfaceManager = newSurfaceManager; +} + +void ofxProjectionEditor::clearJoints() +{ + while ( joints.size() ) { + delete joints.back(); + joints.pop_back(); + } +} + +void ofxProjectionEditor::createJoints() +{ + if ( surfaceManager == NULL ) return; + clearJoints(); + + if ( surfaceManager->getSelectedSurface() == NULL ) { + ofLog(OF_LOG_WARNING, "Trying to create joints while no surface selected."); + return; + } + + vector& vertices = surfaceManager->getSelectedSurface()->getVertices(); + for ( int i=0; iposition = ofVec2f(vertices[i].x, vertices[i].y); + } +} + +bool ofxProjectionEditor::hitTestJoints(ofVec2f pos) +{ + for ( int i=0; ihitTest(pos) ){ + return true; + } + } + return false; +} + +void ofxProjectionEditor::drawJoints() +{ + cout << "draw joints" << endl; + for ( int i=0; idraw(); + } } \ No newline at end of file diff --git a/src/ofxProjectionEditor.h b/src/ofxProjectionEditor.h index 24f29ed..a52edb6 100644 --- a/src/ofxProjectionEditor.h +++ b/src/ofxProjectionEditor.h @@ -1,6 +1,9 @@ #ifndef H_OFX_PROJECTION_EDITOR #define H_OFX_PROJECTION_EDITOR +#include "ofxSurfaceManager.h" +#include "ofxCircleJoint.h" + class ofxProjectionEditor { public: @@ -8,6 +11,16 @@ public: ~ofxProjectionEditor(); void draw(); + void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); + void clearJoints(); + void createJoints(); + bool hitTestJoints(ofVec2f pos); + +private: + ofxSurfaceManager* surfaceManager; + vector joints; + + void drawJoints(); }; #endif \ No newline at end of file diff --git a/src/ofxSurfaceManager.cpp b/src/ofxSurfaceManager.cpp index f5e7edf..beb087b 100644 --- a/src/ofxSurfaceManager.cpp +++ b/src/ofxSurfaceManager.cpp @@ -30,6 +30,25 @@ void ofxSurfaceManager::addSurface(int surfaceType) } } +ofxBaseSurface* ofxSurfaceManager::selectSurface(int index) +{ + if ( index >= surfaces.size() ) { + throw std::runtime_error("Surface index out of bounds."); + } + + selectedSurface = surfaces[index]; +} + +ofxBaseSurface* ofxSurfaceManager::getSelectedSurface() +{ + return selectedSurface; +} + +void ofxSurfaceManager::deselectSurface() +{ + selectedSurface = NULL; +} + ofxBaseSurface* ofxSurfaceManager::getSurface(int index) { if ( index >= surfaces.size() ) { @@ -44,3 +63,4 @@ int ofxSurfaceManager::size() { return surfaces.size(); } + diff --git a/src/ofxSurfaceManager.h b/src/ofxSurfaceManager.h index b114da2..e78f32d 100644 --- a/src/ofxSurfaceManager.h +++ b/src/ofxSurfaceManager.h @@ -22,9 +22,13 @@ public: void addSurface(int surfaceType); ofxBaseSurface* getSurface(int index); int size(); + ofxBaseSurface* selectSurface(int index); + ofxBaseSurface* getSelectedSurface(); + void deselectSurface(); private: vector surfaces; + ofxBaseSurface* selectedSurface; }; #endif \ No newline at end of file diff --git a/src/ofxSurfaceManagerGui.cpp b/src/ofxSurfaceManagerGui.cpp index 67005c9..5dab6d4 100644 --- a/src/ofxSurfaceManagerGui.cpp +++ b/src/ofxSurfaceManagerGui.cpp @@ -4,7 +4,6 @@ ofxSurfaceManagerGui::ofxSurfaceManagerGui() { surfaceManager = NULL; guiMode = ofxGuiMode::NONE; - selectedSurface = NULL; registerMouseEvents(); } @@ -33,8 +32,8 @@ void ofxSurfaceManagerGui::draw() } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { // draw the texture of the selected surface - if ( selectedSurface != NULL ) { - selectedSurface->drawTexture( ofVec2f(0,0) ); + if ( surfaceManager->getSelectedSurface() != NULL ) { + surfaceManager->getSelectedSurface()->drawTexture( ofVec2f(0,0) ); } // draw surfaces with opacity @@ -71,18 +70,34 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args) } else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { // attempt to select surface, loop from end to beginning + bool bSurfaceSelected = false; for ( int i=surfaceManager->size()-1; i>=0; i-- ) { if ( surfaceManager->getSurface(i)->hitTest( ofVec2f(args.x, args.y) ) ) { - selectSurface(i); + projectionEditor.clearJoints(); + surfaceManager->selectSurface(i); + projectionEditor.createJoints(); + bSurfaceSelected = true; break; } } + + // Check if hitting one of the joints as that also couts as hit when surface is selected + if ( projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)) ) { + bSurfaceSelected = true; + } + + if ( !bSurfaceSelected ) { + // unselect if no surface selected + projectionEditor.clearJoints(); + surfaceManager->deselectSurface(); + } } } void ofxSurfaceManagerGui::setSurfaceManager(ofxSurfaceManager* newSurfaceManager) { surfaceManager = newSurfaceManager; + projectionEditor.setSurfaceManager( surfaceManager ); } void ofxSurfaceManagerGui::setMode(int newGuiMode) @@ -96,25 +111,11 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode) guiMode = newGuiMode; } -ofxBaseSurface* ofxSurfaceManagerGui::selectSurface(int index) -{ - if ( index >= surfaceManager->size() ) { - throw std::runtime_error("Surface index out of bounds."); - } - - selectedSurface = surfaceManager->getSurface(index); -} - -void ofxSurfaceManagerGui::deselectSurface() -{ - selectedSurface = NULL; -} - void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight() { - if ( selectedSurface == NULL ) return; + if ( surfaceManager->getSelectedSurface() == NULL ) return; - ofPolyline line = selectedSurface->getHitArea(); + ofPolyline line = surfaceManager->getSelectedSurface()->getHitArea(); ofPushStyle(); ofSetLineWidth(1); diff --git a/src/ofxSurfaceManagerGui.h b/src/ofxSurfaceManagerGui.h index 9aa679d..062056a 100644 --- a/src/ofxSurfaceManagerGui.h +++ b/src/ofxSurfaceManagerGui.h @@ -20,8 +20,6 @@ public: void mousePressed(ofMouseEventArgs& args); void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); void setMode(int newGuiMode); - ofxBaseSurface* selectSurface(int index); - void deselectSurface(); void drawSelectedSurfaceHighlight(); private: @@ -29,7 +27,6 @@ private: ofxTextureEditor textureEditor; ofxProjectionEditor projectionEditor; int guiMode; - ofxBaseSurface* selectedSurface; }; #endif \ No newline at end of file diff --git a/src/ofxTriangleSurface.cpp b/src/ofxTriangleSurface.cpp index 68bd870..631d822 100644 --- a/src/ofxTriangleSurface.cpp +++ b/src/ofxTriangleSurface.cpp @@ -118,4 +118,10 @@ ofPolyline ofxTriangleSurface::getHitArea() line.close(); return line; +} + +vector& ofxTriangleSurface::getVertices() +{ + // return only joint vertices + return mesh.getVertices(); } \ No newline at end of file diff --git a/src/ofxTriangleSurface.h b/src/ofxTriangleSurface.h index 8837d23..f54a890 100644 --- a/src/ofxTriangleSurface.h +++ b/src/ofxTriangleSurface.h @@ -22,6 +22,7 @@ public: ofVec2f getVertex(int index); ofVec2f getTexCoord(int index); ofPolyline getHitArea(); + vector& getVertices(); }; #endif \ No newline at end of file