diff --git a/src/ofxBaseSurface.h b/src/ofxBaseSurface.h index 3b07046..744e926 100644 --- a/src/ofxBaseSurface.h +++ b/src/ofxBaseSurface.h @@ -17,7 +17,9 @@ public: virtual int getType(){}; virtual bool hitTest(ofVec2f p){}; virtual ofPolyline getHitArea(){}; + virtual ofPolyline getTextureHitArea(){}; virtual vector& getVertices(){}; + virtual vector& getTexCoords(){}; // Draws a texture using ofMesh void drawTexture(ofVec2f position); diff --git a/src/ofxSurfaceManager.h b/src/ofxSurfaceManager.h index e78f32d..cf43b1f 100644 --- a/src/ofxSurfaceManager.h +++ b/src/ofxSurfaceManager.h @@ -1,11 +1,6 @@ #ifndef H_OFX_SURFACE_MANAGER #define H_OFX_SURFACE_MANAGER -/* - 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 "ofxSurfaceType.h" diff --git a/src/ofxSurfaceManagerGui.cpp b/src/ofxSurfaceManagerGui.cpp index a1e7b96..f1819fd 100644 --- a/src/ofxSurfaceManagerGui.cpp +++ b/src/ofxSurfaceManagerGui.cpp @@ -47,9 +47,12 @@ void ofxSurfaceManagerGui::draw() surfaceManager->draw(); ofPopStyle(); - // hilight selected surface + // highlight selected surface drawSelectedSurfaceHighlight(); + // hilight selected surface texture + drawSelectedSurfaceTextureHighlight(); + // draw texture editing GUI on top textureEditor.draw(); @@ -72,7 +75,15 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args) if ( guiMode == ofxGuiMode::NONE ) { return; } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { - return; + + if ( surfaceManager->getSelectedSurface() != NULL ) { + // hittest texture area to see if we are hitting the texture surface + if ( surfaceManager->getSelectedSurface()->getTextureHitArea().inside(args.x, args.y) ) { + clickPosition = ofVec2f(args.x, args.y); + startDrag(); + } + } + } else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { bool bSurfaceSelected = false; @@ -121,8 +132,13 @@ void ofxSurfaceManagerGui::mouseDragged(ofMouseEventArgs &args) if (bDrag) { ofVec2f mousePosition = ofVec2f(args.x, args.y); ofVec2f distance = mousePosition - clickPosition; - // add this distance to all vertices in surface - projectionEditor.moveSelectedSurface(distance); + + if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { + // add this distance to all vertices in surface + projectionEditor.moveSelectedSurface(distance); + } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { + textureEditor.moveTexCoords(distance); + } clickPosition = mousePosition; } } @@ -142,6 +158,9 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode) } guiMode = newGuiMode; + + // refresh texture editor surface reference + textureEditor.setSurface(surfaceManager->getSelectedSurface()); } void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight() @@ -157,6 +176,20 @@ void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight() ofPopStyle(); } +void ofxSurfaceManagerGui::drawSelectedSurfaceTextureHighlight() +{ + if ( surfaceManager->getSelectedSurface() == NULL ) return; + + ofPolyline line = surfaceManager->getSelectedSurface()->getTextureHitArea(); + + ofPushStyle(); + ofSetLineWidth(1); + ofSetColor(255, 255, 0, 255); + line.draw(); + ofPopStyle(); + +} + void ofxSurfaceManagerGui::startDrag() { bDrag = true; diff --git a/src/ofxSurfaceManagerGui.h b/src/ofxSurfaceManagerGui.h index d19cd1c..3dc7e4a 100644 --- a/src/ofxSurfaceManagerGui.h +++ b/src/ofxSurfaceManagerGui.h @@ -23,6 +23,7 @@ public: void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); void setMode(int newGuiMode); void drawSelectedSurfaceHighlight(); + void drawSelectedSurfaceTextureHighlight(); void startDrag(); void stopDrag(); diff --git a/src/ofxTextureEditor.cpp b/src/ofxTextureEditor.cpp index 17ffb9d..5bce467 100644 --- a/src/ofxTextureEditor.cpp +++ b/src/ofxTextureEditor.cpp @@ -5,11 +5,6 @@ ofxTextureEditor::ofxTextureEditor() clear(); } -ofxTextureEditor::ofxTextureEditor(ofxBaseSurface* newSurface) -{ - setSurface(newSurface); -} - ofxTextureEditor::~ofxTextureEditor() { clear(); @@ -17,17 +12,58 @@ ofxTextureEditor::~ofxTextureEditor() void ofxTextureEditor::draw() { - if ( surface != NULL ) { - surface->drawTexture(ofVec2f(0.0f,0.0f)); + if (surface == NULL) return; + + drawJoints(); +} + +void ofxTextureEditor::drawJoints() +{ + for ( int i=0; idraw(); } } void ofxTextureEditor::setSurface(ofxBaseSurface* newSurface) { surface = newSurface; + createJoints(); } void ofxTextureEditor::clear() { surface = NULL; + clearJoints(); +} + +void ofxTextureEditor::createJoints() +{ + if ( surface == NULL ) return; + clearJoints(); + vector& texCoords = surface->getTexCoords(); + ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), surface->getTexture()->getHeight()); + + for ( int i=0; iposition = texCoords[i] * textureSize; + } +} + +void ofxTextureEditor::clearJoints() +{ + while ( joints.size() ) { + delete joints.back(); + joints.pop_back(); + } +} + +void ofxTextureEditor::moveTexCoords(ofVec2f by) +{ + if ( surface == NULL ) return; + vector& texCoords = surface->getTexCoords(); + ofVec2f textureSize = ofVec2f( surface->getTexture()->getWidth(), surface->getTexture()->getHeight() ); + for (int i=0; iposition += by; + texCoords[i] = joints[i]->position / textureSize; + } } \ No newline at end of file diff --git a/src/ofxTextureEditor.h b/src/ofxTextureEditor.h index 4239fb8..fcfac1c 100644 --- a/src/ofxTextureEditor.h +++ b/src/ofxTextureEditor.h @@ -2,20 +2,25 @@ #define H_OFX_TEXTURE_EDITOR #include "ofxBaseSurface.h" +#include "ofxCircleJoint.h" class ofxTextureEditor { public: ofxTextureEditor(); - ofxTextureEditor(ofxBaseSurface* newSurface); ~ofxTextureEditor(); void draw(); + void drawJoints(); void setSurface(ofxBaseSurface* newSurface); void clear(); + void createJoints(); + void clearJoints(); + void moveTexCoords(ofVec2f by); private: ofxBaseSurface* surface; + vector joints; }; diff --git a/src/ofxTriangleSurface.cpp b/src/ofxTriangleSurface.cpp index 631d822..2eacf94 100644 --- a/src/ofxTriangleSurface.cpp +++ b/src/ofxTriangleSurface.cpp @@ -120,8 +120,27 @@ ofPolyline ofxTriangleSurface::getHitArea() return line; } +ofPolyline ofxTriangleSurface::getTextureHitArea() +{ + ofPolyline line; + vector& texCoords = mesh.getTexCoords(); + ofVec2f textureSize = ofVec2f(texture->getWidth(), texture->getHeight()); + for ( int i=0; i& ofxTriangleSurface::getVertices() { // return only joint vertices return mesh.getVertices(); +} + +vector& ofxTriangleSurface::getTexCoords() +{ + + return mesh.getTexCoords(); } \ No newline at end of file diff --git a/src/ofxTriangleSurface.h b/src/ofxTriangleSurface.h index f54a890..bed128c 100644 --- a/src/ofxTriangleSurface.h +++ b/src/ofxTriangleSurface.h @@ -22,7 +22,9 @@ public: ofVec2f getVertex(int index); ofVec2f getTexCoord(int index); ofPolyline getHitArea(); + ofPolyline getTextureHitArea(); vector& getVertices(); + vector& getTexCoords(); }; #endif \ No newline at end of file