From 7c002c73b63048995b9bad7749dbf2e0c2f86f20 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Mon, 12 May 2014 14:43:27 +0200 Subject: [PATCH] Add surface selection in projection mapping edit mode --- example/src/ofApp.cpp | 15 +++-- example/src/ofApp.h | 1 + src/ofxBaseSurface.h | 2 + src/ofxPiMapper.h | 1 + src/ofxProjectionEditor.cpp | 17 +++++- src/ofxProjectionEditor.h | 2 + src/ofxSurfaceManagerGui.cpp | 103 ++++++++++++++++++++++++++++++++--- src/ofxSurfaceManagerGui.h | 13 ++++- src/ofxTextureEditor.cpp | 34 +++++++++++- src/ofxTextureEditor.h | 11 ++++ src/ofxTriangleSurface.cpp | 23 ++++++++ src/ofxTriangleSurface.h | 3 +- 12 files changed, 208 insertions(+), 17 deletions(-) diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp index fec3e8a..4ca3766 100644 --- a/example/src/ofApp.cpp +++ b/example/src/ofApp.cpp @@ -11,6 +11,9 @@ void ofApp::setup() surfaceManager.getSurface(1)->setVertex(0, ofVec2f(10, 10)); surfaceManager.getSurface(1)->setVertex(1, ofVec2f(400, 20)); surfaceManager.getSurface(1)->setVertex(2, ofVec2f(300, 400)); + + gui.setSurfaceManager( &surfaceManager ); + gui.selectSurface(1); } void ofApp::update() @@ -20,11 +23,13 @@ void ofApp::update() ofVec2f p; p.x = ofRandomWidth(); p.y = ofRandomHeight(); + //surfaceManager.getSurface(1)->setVertex(0, p); //triangleSurface.setVertex(0, p); ofVec2f t; t.x = ofRandomuf(); t.y = ofRandomuf(); + //surfaceManager.getSurface(1)->setTexCoord(0, t); //triangleSurface.setTexCoord(0, t); //surfaceManager.update(); @@ -32,7 +37,9 @@ void ofApp::update() void ofApp::draw() { - surfaceManager.draw(); + //surfaceManager.draw(); + // if using gui - use ofxSurfaceManagerGui::draw() instead of surfaceManager::draw() + gui.draw(); if ( bShowInfo ) { // Draw instructions @@ -53,9 +60,9 @@ void ofApp::keyPressed(int key) cout << "Key pressed: " << static_cast(key) << endl; switch (key) { - //case '1': surfaceManager.setGuiMode(ofxSurfaceGui::NONE); break; - //case '2': surfaceManager.setGuiMode(ofxSurfaceGui::TEXTURE_MAPPING); break; - //case '3': surfaceManager.setGuiMode(ofxSurfaceGui::PROJECTION_MAPPING); break; + case '1': gui.setMode(ofxGuiMode::NONE); break; + case '2': gui.setMode(ofxGuiMode::TEXTURE_MAPPING); break; + case '3': gui.setMode(ofxGuiMode::PROJECTION_MAPPING); break; case 'i': bShowInfo = !bShowInfo; break; default: break; } diff --git a/example/src/ofApp.h b/example/src/ofApp.h index 324aecb..d79bd08 100644 --- a/example/src/ofApp.h +++ b/example/src/ofApp.h @@ -18,6 +18,7 @@ public: ofImage image; ofxSurfaceManager surfaceManager; + ofxSurfaceManagerGui gui; bool bShowInfo; }; diff --git a/src/ofxBaseSurface.h b/src/ofxBaseSurface.h index 07e4bd2..e038473 100644 --- a/src/ofxBaseSurface.h +++ b/src/ofxBaseSurface.h @@ -15,6 +15,8 @@ public: virtual void setVertex(int index, ofVec2f p){}; virtual void setTexCoord(int index, ofVec2f t){}; virtual int getType(){}; + virtual bool hitTest(ofVec2f p){}; + virtual ofPolyline getHitArea(){}; // Draws a texture using ofMesh void drawTexture(ofVec2f position); diff --git a/src/ofxPiMapper.h b/src/ofxPiMapper.h index 2027f89..4f65bd2 100644 --- a/src/ofxPiMapper.h +++ b/src/ofxPiMapper.h @@ -2,5 +2,6 @@ #define H_OFX_PI_MAPPER #include "ofxSurfaceManager.h" +#include "ofxSurfaceManagerGui.h" #endif \ No newline at end of file diff --git a/src/ofxProjectionEditor.cpp b/src/ofxProjectionEditor.cpp index 7f7946e..1b6b861 100644 --- a/src/ofxProjectionEditor.cpp +++ b/src/ofxProjectionEditor.cpp @@ -1 +1,16 @@ -#include "ofxProjectionEditor.h" \ No newline at end of file +#include "ofxProjectionEditor.h" + +ofxProjectionEditor::ofxProjectionEditor() +{ + +} + +ofxProjectionEditor::~ofxProjectionEditor() +{ + +} + +void ofxProjectionEditor::draw() +{ + +} \ No newline at end of file diff --git a/src/ofxProjectionEditor.h b/src/ofxProjectionEditor.h index 26ca272..24f29ed 100644 --- a/src/ofxProjectionEditor.h +++ b/src/ofxProjectionEditor.h @@ -6,6 +6,8 @@ class ofxProjectionEditor public: ofxProjectionEditor(); ~ofxProjectionEditor(); + + void draw(); }; #endif \ No newline at end of file diff --git a/src/ofxSurfaceManagerGui.cpp b/src/ofxSurfaceManagerGui.cpp index 05b4406..67005c9 100644 --- a/src/ofxSurfaceManagerGui.cpp +++ b/src/ofxSurfaceManagerGui.cpp @@ -4,16 +4,24 @@ ofxSurfaceManagerGui::ofxSurfaceManagerGui() { surfaceManager = NULL; guiMode = ofxGuiMode::NONE; + selectedSurface = NULL; + registerMouseEvents(); } -ofxSurfaceManagerGui::ofxSurfaceManagerGui(ofxSurfaceManager* newSurfaceManager) +ofxSurfaceManagerGui::~ofxSurfaceManagerGui() { - surfaceManager = newSurfaceManager; + unregisterMouseEvents(); + surfaceManager = NULL; } -ofxSurfaceManagerGui::~ofxSurfaceManagerGui() +void ofxSurfaceManagerGui::registerMouseEvents() { - surfaceManager = NULL; + ofAddListener(ofEvents().mousePressed, this, &ofxSurfaceManagerGui::mousePressed); +} + +void ofxSurfaceManagerGui::unregisterMouseEvents() +{ + ofRemoveListener(ofEvents().mousePressed, this, &ofxSurfaceManagerGui::mousePressed); } void ofxSurfaceManagerGui::draw() @@ -21,15 +29,96 @@ void ofxSurfaceManagerGui::draw() if ( surfaceManager == NULL ) return; if ( guiMode == ofxGuiMode::NONE ) { - // Do nothing + surfaceManager->draw(); } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { - // Draw texture mapping GUI + + // draw the texture of the selected surface + if ( selectedSurface != NULL ) { + selectedSurface->drawTexture( ofVec2f(0,0) ); + } + + // draw surfaces with opacity + ofPushStyle(); + ofSetColor(255, 255, 255, 200); + surfaceManager->draw(); + ofPopStyle(); + + // hilight selected surface + drawSelectedSurfaceHighlight(); + + // draw texture editing GUI on top + textureEditor.draw(); + } else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { - // Draw projection mapping GUI + + // draw projection surfaces first + surfaceManager->draw(); + + // highlight selected surface + drawSelectedSurfaceHighlight(); + + // draw projection mapping editing gui + projectionEditor.draw(); + + } +} + +void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args) +{ + if ( guiMode == ofxGuiMode::NONE ) { + return; + } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { + + } else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { + // attempt to select surface, loop from end to beginning + for ( int i=surfaceManager->size()-1; i>=0; i-- ) { + if ( surfaceManager->getSurface(i)->hitTest( ofVec2f(args.x, args.y) ) ) { + selectSurface(i); + break; + } + } } } void ofxSurfaceManagerGui::setSurfaceManager(ofxSurfaceManager* newSurfaceManager) { surfaceManager = newSurfaceManager; +} + +void ofxSurfaceManagerGui::setMode(int newGuiMode) +{ + if (newGuiMode != ofxGuiMode::NONE && + newGuiMode != ofxGuiMode::TEXTURE_MAPPING && + newGuiMode != ofxGuiMode::PROJECTION_MAPPING) { + throw std::runtime_error("Trying to set invalid mode."); + } + + 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; + + ofPolyline line = selectedSurface->getHitArea(); + + ofPushStyle(); + ofSetLineWidth(1); + ofSetColor(255, 255, 255, 255); + line.draw(); + ofPopStyle(); } \ No newline at end of file diff --git a/src/ofxSurfaceManagerGui.h b/src/ofxSurfaceManagerGui.h index 0dd8b51..9aa679d 100644 --- a/src/ofxSurfaceManagerGui.h +++ b/src/ofxSurfaceManagerGui.h @@ -1,7 +1,7 @@ #ifndef H_OFX_SURFACE_MANAGER_GUI #define H_OFX_SURFACE_MANAGER_GUI -#include "ofMain.h" +#include "ofEvents.h" #include "ofxSurfaceManager.h" #include "ofxTextureEditor.h" #include "ofxProjectionEditor.h" @@ -11,18 +11,25 @@ class ofxSurfaceManagerGui { public: ofxSurfaceManagerGui(); - ofxSurfaceManagerGui(ofxSurfaceManager* newSurfaceManager); ~ofxSurfaceManagerGui(); - void draw(); + void registerMouseEvents(); + void unregisterMouseEvents(); + void draw(); + void mousePressed(ofMouseEventArgs& args); void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); + void setMode(int newGuiMode); + ofxBaseSurface* selectSurface(int index); + void deselectSurface(); + void drawSelectedSurfaceHighlight(); private: ofxSurfaceManager* surfaceManager; ofxTextureEditor textureEditor; ofxProjectionEditor projectionEditor; int guiMode; + ofxBaseSurface* selectedSurface; }; #endif \ No newline at end of file diff --git a/src/ofxTextureEditor.cpp b/src/ofxTextureEditor.cpp index d324303..17ffb9d 100644 --- a/src/ofxTextureEditor.cpp +++ b/src/ofxTextureEditor.cpp @@ -1 +1,33 @@ -#include "ofxTextureEditor.h" \ No newline at end of file +#include "ofxTextureEditor.h" + +ofxTextureEditor::ofxTextureEditor() +{ + clear(); +} + +ofxTextureEditor::ofxTextureEditor(ofxBaseSurface* newSurface) +{ + setSurface(newSurface); +} + +ofxTextureEditor::~ofxTextureEditor() +{ + clear(); +} + +void ofxTextureEditor::draw() +{ + if ( surface != NULL ) { + surface->drawTexture(ofVec2f(0.0f,0.0f)); + } +} + +void ofxTextureEditor::setSurface(ofxBaseSurface* newSurface) +{ + surface = newSurface; +} + +void ofxTextureEditor::clear() +{ + surface = NULL; +} \ No newline at end of file diff --git a/src/ofxTextureEditor.h b/src/ofxTextureEditor.h index d631358..4239fb8 100644 --- a/src/ofxTextureEditor.h +++ b/src/ofxTextureEditor.h @@ -1,11 +1,22 @@ #ifndef H_OFX_TEXTURE_EDITOR #define H_OFX_TEXTURE_EDITOR +#include "ofxBaseSurface.h" + class ofxTextureEditor { public: ofxTextureEditor(); + ofxTextureEditor(ofxBaseSurface* newSurface); ~ofxTextureEditor(); + + void draw(); + void setSurface(ofxBaseSurface* newSurface); + void clear(); + +private: + ofxBaseSurface* surface; + }; #endif \ No newline at end of file diff --git a/src/ofxTriangleSurface.cpp b/src/ofxTriangleSurface.cpp index 6da0564..68bd870 100644 --- a/src/ofxTriangleSurface.cpp +++ b/src/ofxTriangleSurface.cpp @@ -77,6 +77,18 @@ int ofxTriangleSurface::getType() return ofxSurfaceType::TRIANGLE_SURFACE; } +bool ofxTriangleSurface::hitTest(ofVec2f p) +{ + // Construct ofPolyline from vertices + ofPolyline line = getHitArea(); + + if ( line.inside(p.x, p.y) ) { + return true; + } else { + return false; + } +} + ofVec2f ofxTriangleSurface::getVertex(int index) { if ( index > 2 ) { @@ -95,4 +107,15 @@ ofVec2f ofxTriangleSurface::getTexCoord(int index) } return mesh.getTexCoord(index); +} + +ofPolyline ofxTriangleSurface::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.close(); + + return line; } \ No newline at end of file diff --git a/src/ofxTriangleSurface.h b/src/ofxTriangleSurface.h index b3697c1..8837d23 100644 --- a/src/ofxTriangleSurface.h +++ b/src/ofxTriangleSurface.h @@ -18,9 +18,10 @@ public: void setTexCoord( int index, ofVec2f t ); int getType(); - + bool hitTest(ofVec2f p); ofVec2f getVertex(int index); ofVec2f getTexCoord(int index); + ofPolyline getHitArea(); }; #endif \ No newline at end of file