From ea5c83dcebcd40f787136b34d742517b0e01aab6 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Sun, 11 May 2014 16:45:14 +0200 Subject: [PATCH] Add surface manager --- README.md | 3 +- example/src/main.cpp | 2 +- example/src/ofApp.cpp | 46 +++--------- example/src/ofApp.h | 6 +- src/ofxPiMapper.h | 2 +- src/ofxSurfaceGui.cpp | 74 ++++++++++++++++---- src/ofxSurfaceGui.h | 6 ++ src/ofxSurfaceManager.cpp | 142 ++++++++++++++++++++++++++++++++++++++ src/ofxSurfaceManager.h | 42 +++++++++++ 9 files changed, 265 insertions(+), 58 deletions(-) create mode 100644 src/ofxSurfaceManager.cpp create mode 100644 src/ofxSurfaceManager.h diff --git a/README.md b/README.md index 8bd1407..7f4bb23 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,8 @@ Version history ### Version 0.1.2: TODO: - Possibility to add multiple surfaces with a kind of layer management utility. - - Ability to select and drag whole surfaces + + Ability to select and drag whole surfaces + - Select / deselect surfaces - Example with video source. Maybe one example with different surfaces and sources. - How to switch between sources? diff --git a/example/src/main.cpp b/example/src/main.cpp index f7292b9..6da1c65 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -3,6 +3,6 @@ int main() { - ofSetupOpenGL(1024, 768, OF_WINDOW); + ofSetupOpenGL(600, 500, OF_WINDOW); ofRunApp(new ofApp()); } \ No newline at end of file diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp index 3f16b66..558e1fe 100644 --- a/example/src/ofApp.cpp +++ b/example/src/ofApp.cpp @@ -3,14 +3,9 @@ void ofApp::setup() { image.loadImage("TestPatternInvert.jpg"); + bShowInfo = false; - triangleSurface.setup(ofVec2f(30,40), ofVec2f(500,500), ofVec2f(0,600), - ofVec2f(0,0), ofVec2f(1,1), ofVec2f(0,1), - &image.getTextureReference()); - - gui.setup(triangleSurface); - - bShowInfo = true; + surfaceManager.addSurface(); } void ofApp::update() @@ -27,33 +22,12 @@ void ofApp::update() t.y = ofRandomuf(); //triangleSurface.setTexCoord(0, t); - gui.update(); + surfaceManager.update(); } void ofApp::draw() { - // Check GUI mode - we want to see the texture that we are editing - // together with the actual surface being projection mapped. - - ofxSurfaceGui::editMode mode = gui.getMode(); - if ( mode == ofxSurfaceGui::TEXTURE_MAPPING ) { - // Draw texture of the surface in the background - //triangleSurface.getTexture()->draw(ofPoint(0,0)); - triangleSurface.drawTexture(ofVec2f(0, 0)); - - // Make the triangle surface transparent but still visible - // while we map the texture coordinates. - ofPushStyle(); - ofSetColor(255, 255, 255, 200); - } - - triangleSurface.draw(); - - if ( mode == ofxSurfaceGui::TEXTURE_MAPPING ) { - ofPopStyle(); - } - - gui.draw(); + surfaceManager.draw(); if ( bShowInfo ) { // Draw instructions @@ -74,9 +48,9 @@ void ofApp::keyPressed(int key) cout << "Key pressed: " << static_cast(key) << endl; switch (key) { - case '1': gui.setMode(ofxSurfaceGui::NONE); break; - case '2': gui.setMode(ofxSurfaceGui::TEXTURE_MAPPING); break; - case '3': gui.setMode(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; } @@ -85,17 +59,17 @@ void ofApp::keyPressed(int key) void ofApp::mousePressed(int x, int y, int button) { //cout << "Mouse pressed." << endl; - gui.mousePressed(x, y, button); + surfaceManager.mousePressed(x, y, button); } void ofApp::mouseReleased(int x, int y, int button) { //cout << "Mouse released." << endl; - gui.mouseReleased(x, y, button); + surfaceManager.mouseReleased(x, y, button); } void ofApp::mouseDragged(int x, int y, int button) { // - gui.mouseDragged(x, y, button); + surfaceManager.mouseDragged(x, y, button); } \ No newline at end of file diff --git a/example/src/ofApp.h b/example/src/ofApp.h index 06cc87c..324aecb 100644 --- a/example/src/ofApp.h +++ b/example/src/ofApp.h @@ -4,8 +4,6 @@ #include "ofMain.h" #include "ofxPiMapper.h" -#include "ofxSurfaceGui.h" - class ofApp : public ofBaseApp { public: @@ -18,10 +16,8 @@ public: void mouseReleased(int x, int y, int button); void mouseDragged(int x, int y, int button); - ofxTriangleSurface triangleSurface; ofImage image; - - ofxSurfaceGui gui; + ofxSurfaceManager surfaceManager; bool bShowInfo; }; diff --git a/src/ofxPiMapper.h b/src/ofxPiMapper.h index e824ae0..2027f89 100644 --- a/src/ofxPiMapper.h +++ b/src/ofxPiMapper.h @@ -1,6 +1,6 @@ #ifndef H_OFX_PI_MAPPER #define H_OFX_PI_MAPPER -#include "ofxTriangleSurface.h" +#include "ofxSurfaceManager.h" #endif \ No newline at end of file diff --git a/src/ofxSurfaceGui.cpp b/src/ofxSurfaceGui.cpp index 27e5cf0..8fd9cbb 100644 --- a/src/ofxSurfaceGui.cpp +++ b/src/ofxSurfaceGui.cpp @@ -8,6 +8,7 @@ ofxSurfaceGui::ofxSurfaceGui() bTextureMappingJointSelected = false; bTextureDragging = false; bProjectionDragging = false; + bSelected = false; } ofxSurfaceGui::~ofxSurfaceGui() @@ -43,20 +44,8 @@ void ofxSurfaceGui::draw() if (surface == NULL) return; if (mode == NONE) return; - if (mode == PROJECTION_MAPPING) { - ofPolyline line; - for ( int i=0; i 2 ) { + return true; + } else { + return false; + } } \ No newline at end of file diff --git a/src/ofxSurfaceGui.h b/src/ofxSurfaceGui.h index 904644f..1f7b130 100644 --- a/src/ofxSurfaceGui.h +++ b/src/ofxSurfaceGui.h @@ -24,6 +24,10 @@ public: void mouseReleased(int x, int y, int button); void mouseDragged(int x, int y, int button); void setMode(editMode newMode); + void select(); + void unselect(); + bool hitTest(float x, float y); + bool isSelected(); editMode getMode(); @@ -41,9 +45,11 @@ private: bool bProjectionMappingJointSelected; bool bTextureDragging; bool bProjectionDragging; + bool bSelected; bool isProjectionMappingJointSelected(); bool isTextureMappingJointSelected(); + bool projectionAreaExists(); void addProjectionMappingJoint(); void addNumProjectionMappingJoints(int num); diff --git a/src/ofxSurfaceManager.cpp b/src/ofxSurfaceManager.cpp new file mode 100644 index 0000000..c11da30 --- /dev/null +++ b/src/ofxSurfaceManager.cpp @@ -0,0 +1,142 @@ +#include "ofxSurfaceManager.h" + +ofxSurfaceManager::ofxSurfaceManager() +{ + +} + +ofxSurfaceManager::~ofxSurfaceManager() +{ + +} + +void ofxSurfaceManager::setup() +{ + +} + +void ofxSurfaceManager::update() +{ + for ( int i=0; iupdate(); + } +} + +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=0; iisSelected() && 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(); + } +} + +void ofxSurfaceManager::mousePressed(int x, int y, int button) +{ + bool bSurfaceSelected = false; + for ( int i=0; ihitTest(x, y) ) { + selectSurface(i); + bSurfaceSelected = true; + } + surfaceGuis[i]->mousePressed(x, y, button); + } + + 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::removeSurface(int index) +{ + if ( index >= surfaceGuis.size() ) { + throw std::runtime_error("Surface index out of bounds."); + return; + } + + surfaceGuis.erase( surfaceGuis.begin()+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(); + } +} + +int ofxSurfaceManager::size() +{ + return surfaceGuis.size(); +} + +void ofxSurfaceManager::addTriangleSurface() +{ + triangleSurfaces.push_back( &aTriangleSurfaces[triangleSurfaces.size()] ); + surfaceGuis.push_back( &aSurfaceGuis[surfaceGuis.size()] ); + surfaceGuis.back()->setup( *triangleSurfaces.back() ); +} + +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); +} \ No newline at end of file diff --git a/src/ofxSurfaceManager.h b/src/ofxSurfaceManager.h new file mode 100644 index 0000000..b11add9 --- /dev/null +++ b/src/ofxSurfaceManager.h @@ -0,0 +1,42 @@ +#ifndef H_OFX_SURFACE_MANAGER +#define H_OFX_SURFACE_MANAGER + +#include "ofMain.h" +#include "ofxTriangleSurface.h" +#include "ofxSurfaceGui.h" + +#define MAX_SURFACE_COUNT 25 + +class ofxSurfaceManager +{ +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 removeSurface(int index); + void setGuiMode(ofxSurfaceGui::editMode); + void selectSurface(int index); + void unselectAllSurfaces(); + int size(); + // TODO: add simple surface + +private: + ofxTriangleSurface aTriangleSurfaces[MAX_SURFACE_COUNT]; + ofxSurfaceGui aSurfaceGuis[MAX_SURFACE_COUNT]; + deque triangleSurfaces; + deque surfaceGuis; + + void addTriangleSurface(); + void addTriangleSurface(ofVec2f v1, ofVec2f v2, ofVec2f v3, ofVec2f t1, ofVec2f t2, ofVec2f t3, ofTexture* texturePtr); +}; + +#endif \ No newline at end of file