From 37d5cd9946816e91d4db1854d7af9bf99afb9060 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Mon, 12 May 2014 13:02:57 +0200 Subject: [PATCH] Add different surface management logic --- example/src/ofApp.cpp | 25 ++--- src/ofxBaseSurface.h | 4 + src/ofxSurfaceManager.cpp | 179 +++-------------------------------- src/ofxSurfaceManager.h | 37 +++----- src/ofxSurfaceManagerGui.cpp | 18 ++++ src/ofxSurfaceManagerGui.h | 2 + src/ofxTriangleSurface.cpp | 5 + src/ofxTriangleSurface.h | 4 + 8 files changed, 73 insertions(+), 201 deletions(-) diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp index 94d13a0..fec3e8a 100644 --- a/example/src/ofApp.cpp +++ b/example/src/ofApp.cpp @@ -5,11 +5,12 @@ void ofApp::setup() image.loadImage("TestPatternInvert.jpg"); bShowInfo = false; - surfaceManager.addSurface(); - surfaceManager.addSurface( &image.getTextureReference() ); - surfaceManager.addSurface(ofVec2f(10,10), ofVec2f(300, 20), ofVec2f(200, 300), - ofVec2f(0.1f, 0.1f), ofVec2f(0.5f, 0.2f), ofVec2f(0.4f, 0.9f), - &image.getTextureReference()); + surfaceManager.addSurface( ofxSurfaceType::TRIANGLE_SURFACE ); + + surfaceManager.addSurface( ofxSurfaceType::TRIANGLE_SURFACE ); + surfaceManager.getSurface(1)->setVertex(0, ofVec2f(10, 10)); + surfaceManager.getSurface(1)->setVertex(1, ofVec2f(400, 20)); + surfaceManager.getSurface(1)->setVertex(2, ofVec2f(300, 400)); } void ofApp::update() @@ -26,7 +27,7 @@ void ofApp::update() t.y = ofRandomuf(); //triangleSurface.setTexCoord(0, t); - surfaceManager.update(); + //surfaceManager.update(); } void ofApp::draw() @@ -52,9 +53,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': surfaceManager.setGuiMode(ofxSurfaceGui::NONE); break; + //case '2': surfaceManager.setGuiMode(ofxSurfaceGui::TEXTURE_MAPPING); break; + //case '3': surfaceManager.setGuiMode(ofxSurfaceGui::PROJECTION_MAPPING); break; case 'i': bShowInfo = !bShowInfo; break; default: break; } @@ -63,17 +64,17 @@ void ofApp::keyPressed(int key) void ofApp::mousePressed(int x, int y, int button) { //cout << "Mouse pressed." << endl; - surfaceManager.mousePressed(x, y, button); + //surfaceManager.mousePressed(x, y, button); } void ofApp::mouseReleased(int x, int y, int button) { //cout << "Mouse released." << endl; - surfaceManager.mouseReleased(x, y, button); + //surfaceManager.mouseReleased(x, y, button); } void ofApp::mouseDragged(int x, int y, int button) { // - surfaceManager.mouseDragged(x, y, button); + //surfaceManager.mouseDragged(x, y, button); } \ No newline at end of file diff --git a/src/ofxBaseSurface.h b/src/ofxBaseSurface.h index 3d6124f..07e4bd2 100644 --- a/src/ofxBaseSurface.h +++ b/src/ofxBaseSurface.h @@ -2,6 +2,9 @@ #define H_OFX_BASE_SURFACE #include "ofMain.h" +#include + +using namespace std; class ofxBaseSurface { @@ -11,6 +14,7 @@ public: virtual void draw(){}; virtual void setVertex(int index, ofVec2f p){}; virtual void setTexCoord(int index, ofVec2f t){}; + virtual int getType(){}; // Draws a texture using ofMesh void drawTexture(ofVec2f position); diff --git a/src/ofxSurfaceManager.cpp b/src/ofxSurfaceManager.cpp index 7c42413..f5e7edf 100644 --- a/src/ofxSurfaceManager.cpp +++ b/src/ofxSurfaceManager.cpp @@ -8,188 +8,39 @@ ofxSurfaceManager::ofxSurfaceManager() ofxSurfaceManager::~ofxSurfaceManager() { // delete all extra allocations from the heap - while ( triangleSurfaces.size() ) { - delete triangleSurfaces.back(); - triangleSurfaces.pop_back(); - } - - while ( surfaceGuis.size() ) { - delete surfaceGuis.back(); - surfaceGuis.pop_back(); - } -} - -void ofxSurfaceManager::setup() -{ - -} - -void ofxSurfaceManager::update() -{ - for ( int i=0; iupdate(); + while ( surfaces.size() ) { + delete surfaces.back(); + surfaces.pop_back(); } } void ofxSurfaceManager::draw() { - // Check GUI mode - we want to see the texture that we are editing - // together with the actual surface being projection mapped. - for ( int i=surfaceGuis.size()-1; i>=0; i-- ) { - - bool bDrawTexture = false; - if ( surfaceGuis[i]->isSelected() && surfaceGuis[i]->getMode() == ofxSurfaceGui::TEXTURE_MAPPING ) { - bDrawTexture = true; - } - - if ( bDrawTexture ) { - // Draw texture of the surface in the background - //triangleSurface.getTexture()->draw(ofPoint(0,0)); - triangleSurfaces[i]->drawTexture(ofVec2f(0, 0)); - - // Make the triangle surface transparent but still visible - // while we map the texture coordinates. - ofPushStyle(); - ofSetColor(255, 255, 255, 200); - } - - triangleSurfaces[i]->draw(); - - if ( bDrawTexture ) { - ofPopStyle(); - } - - surfaceGuis[i]->draw(); + for ( int i=0; idraw(); } } -void ofxSurfaceManager::mousePressed(int x, int y, int button) +void ofxSurfaceManager::addSurface(int surfaceType) { - bool bSurfaceSelected = false; - for ( int i=0; igetMode() == ofxSurfaceGui::PROJECTION_MAPPING ) { - if ( surfaceGuis[i]->hitTest(x, y) ) { - unselectAllSurfaces(); - selectSurface(i); - bSurfaceSelected = true; - break; // we need to select just one surface - } - } else { - bSurfaceSelected = true; // Hackish - we will need a lot of refractoring here later - } - } - - // execute mouse pressed events for surface GUIs - for ( int i=0; imousePressed(x, y, button); + if ( surfaceType == ofxSurfaceType::TRIANGLE_SURFACE ) { + surfaces.push_back( new ofxTriangleSurface() ); + } else { + throw std::runtime_error("Attempt to add non-existing surface type."); } - - if (!bSurfaceSelected) { - unselectAllSurfaces(); - } -} - -void ofxSurfaceManager::mouseReleased(int x, int y, int button) -{ - for ( int i=0; imouseReleased(x, y, button); - } -} - -void ofxSurfaceManager::mouseDragged(int x, int y, int button) -{ - for ( int i=0; imouseDragged(x, y, button); - } -} - -void ofxSurfaceManager::addSurface() -{ - addTriangleSurface(); -} - -void ofxSurfaceManager::addSurface(ofTexture* texturePtr) -{ - addTriangleSurface(texturePtr); -} - -void ofxSurfaceManager::addSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, ofVec2f t1, ofVec2f t2, ofVec2f t3) -{ - addTriangleSurface(v1, v2, v3, t1, t2, t3); -} - -void ofxSurfaceManager::addSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, - ofVec2f t1, ofVec2f t2, ofVec2f t3, - ofTexture *texturePtr) -{ - addTriangleSurface(v1, v2, v3, t1, t2, t3, texturePtr); } -void ofxSurfaceManager::removeSurface(int index) +ofxBaseSurface* ofxSurfaceManager::getSurface(int index) { - if ( index >= surfaceGuis.size() ) { + if ( index >= surfaces.size() ) { throw std::runtime_error("Surface index out of bounds."); - return; + return NULL; } - delete surfaceGuis[index]; - surfaceGuis.erase( surfaceGuis.begin()+index ); - delete triangleSurfaces[index]; - triangleSurfaces.erase( triangleSurfaces.begin()+index ); -} - -void ofxSurfaceManager::setGuiMode(ofxSurfaceGui::editMode editMode) -{ - for ( int i=0; isetMode(editMode); - } -} - -void ofxSurfaceManager::selectSurface(int index) -{ - if ( index >= surfaceGuis.size() ){ - throw std::runtime_error("Surface index out of bounds."); - return; - } - surfaceGuis[index]->select(); -} - -void ofxSurfaceManager::unselectAllSurfaces() -{ - for ( int i=0; iunselect(); - } + return surfaces[index]; } int ofxSurfaceManager::size() { - return surfaceGuis.size(); -} - -void ofxSurfaceManager::addTriangleSurface() -{ - triangleSurfaces.push_back( new ofxTriangleSurface() ); - surfaceGuis.push_back( new ofxSurfaceGui() ); - surfaceGuis.back()->setup( *triangleSurfaces.back() ); -} - -void ofxSurfaceManager::addTriangleSurface(ofTexture* texturePtr) -{ - addTriangleSurface(); - triangleSurfaces.back()->setTexture(texturePtr); + return surfaces.size(); } - -void ofxSurfaceManager::addTriangleSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, ofVec2f t1, ofVec2f t2, ofVec2f t3) -{ - addTriangleSurface(); - triangleSurfaces.back()->setup(v1, v2, v3, t1, t2, t3, triangleSurfaces.back()->getTexture()); - surfaceGuis.back()->updateJoints(); -} - -void ofxSurfaceManager::addTriangleSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr) -{ - addTriangleSurface(); - triangleSurfaces.back()->setup(v1, v2, v3, t1, t2, t3, texturePtr); - surfaceGuis.back()->updateJoints(); -} \ No newline at end of file diff --git a/src/ofxSurfaceManager.h b/src/ofxSurfaceManager.h index 1ebdbde..b114da2 100644 --- a/src/ofxSurfaceManager.h +++ b/src/ofxSurfaceManager.h @@ -1,9 +1,16 @@ #ifndef H_OFX_SURFACE_MANAGER #define H_OFX_SURFACE_MANAGER -#include "ofMain.h" +/* + Used ofxStateMachine by Neil Mendoza as example for this part of the addon. + https://github.com/neilmendoza/ofxStateMachine + */ + +#include "ofxBaseSurface.h" #include "ofxTriangleSurface.h" -#include "ofxSurfaceGui.h" +#include "ofxSurfaceType.h" + +using namespace std; class ofxSurfaceManager { @@ -11,33 +18,13 @@ public: ofxSurfaceManager(); ~ofxSurfaceManager(); - void setup(); - void update(); void draw(); - - void mousePressed(int x, int y, int button); - void mouseReleased(int x, int y, int button); - void mouseDragged(int x, int y, int button); - - void addSurface(); - void addSurface(ofTexture* texturePtr); - void addSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, ofVec2f t1, ofVec2f t2, ofVec2f t3); - void addSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr); - - void removeSurface(int index); - void setGuiMode(ofxSurfaceGui::editMode); - void selectSurface(int index); - void unselectAllSurfaces(); + void addSurface(int surfaceType); + ofxBaseSurface* getSurface(int index); int size(); private: - deque triangleSurfaces; - deque surfaceGuis; - - void addTriangleSurface(); - void addTriangleSurface(ofTexture* texturePtr); - void addTriangleSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, ofVec2f t1, ofVec2f t2, ofVec2f t3); - void addTriangleSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr); + vector surfaces; }; #endif \ No newline at end of file diff --git a/src/ofxSurfaceManagerGui.cpp b/src/ofxSurfaceManagerGui.cpp index e9a39c8..05b4406 100644 --- a/src/ofxSurfaceManagerGui.cpp +++ b/src/ofxSurfaceManagerGui.cpp @@ -14,4 +14,22 @@ ofxSurfaceManagerGui::ofxSurfaceManagerGui(ofxSurfaceManager* newSurfaceManager) ofxSurfaceManagerGui::~ofxSurfaceManagerGui() { surfaceManager = NULL; +} + +void ofxSurfaceManagerGui::draw() +{ + if ( surfaceManager == NULL ) return; + + if ( guiMode == ofxGuiMode::NONE ) { + // Do nothing + } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { + // Draw texture mapping GUI + } else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { + // Draw projection mapping GUI + } +} + +void ofxSurfaceManagerGui::setSurfaceManager(ofxSurfaceManager* newSurfaceManager) +{ + surfaceManager = newSurfaceManager; } \ No newline at end of file diff --git a/src/ofxSurfaceManagerGui.h b/src/ofxSurfaceManagerGui.h index 14cf333..0dd8b51 100644 --- a/src/ofxSurfaceManagerGui.h +++ b/src/ofxSurfaceManagerGui.h @@ -14,6 +14,8 @@ public: ofxSurfaceManagerGui(ofxSurfaceManager* newSurfaceManager); ~ofxSurfaceManagerGui(); + void draw(); + void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); private: diff --git a/src/ofxTriangleSurface.cpp b/src/ofxTriangleSurface.cpp index b5a7ef3..6da0564 100644 --- a/src/ofxTriangleSurface.cpp +++ b/src/ofxTriangleSurface.cpp @@ -72,6 +72,11 @@ void ofxTriangleSurface::setTexCoord(int index, ofVec2f t) mesh.setTexCoord(index, t); } +int ofxTriangleSurface::getType() +{ + return ofxSurfaceType::TRIANGLE_SURFACE; +} + ofVec2f ofxTriangleSurface::getVertex(int index) { if ( index > 2 ) { diff --git a/src/ofxTriangleSurface.h b/src/ofxTriangleSurface.h index 5c0198a..b3697c1 100644 --- a/src/ofxTriangleSurface.h +++ b/src/ofxTriangleSurface.h @@ -3,6 +3,7 @@ #include "ofMain.h" #include "ofxBaseSurface.h" +#include "ofxSurfaceType.h" class ofxTriangleSurface : public ofxBaseSurface { @@ -15,6 +16,9 @@ public: void draw(); void setVertex( int index, ofVec2f p ); void setTexCoord( int index, ofVec2f t ); + + int getType(); + ofVec2f getVertex(int index); ofVec2f getTexCoord(int index); };