From 90712b37db67948449be9b9b1ee5cfe0d6bd8232 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Mon, 12 May 2014 18:36:43 +0200 Subject: [PATCH] Add movable joints for texture edit mode --- src/ofxSurfaceManagerGui.cpp | 11 ++++++++- src/ofxTextureEditor.cpp | 44 ++++++++++++++++++++++++++++++++++++ src/ofxTextureEditor.h | 9 +++++++- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/ofxSurfaceManagerGui.cpp b/src/ofxSurfaceManagerGui.cpp index f1819fd..a2e2532 100644 --- a/src/ofxSurfaceManagerGui.cpp +++ b/src/ofxSurfaceManagerGui.cpp @@ -76,7 +76,15 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args) return; } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { - if ( surfaceManager->getSelectedSurface() != NULL ) { + bool bSurfaceSelected = false; + + ofxCircleJoint* hitJoint = textureEditor.hitTestJoints(ofVec2f(args.x, args.y)); + if ( hitJoint != NULL ) { + hitJoint->startDrag(); + bSurfaceSelected = true; + } + + if ( surfaceManager->getSelectedSurface() != NULL && !bSurfaceSelected ) { // 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); @@ -125,6 +133,7 @@ void ofxSurfaceManagerGui::mouseReleased(ofMouseEventArgs &args) { stopDrag(); projectionEditor.stopDragJoints(); + textureEditor.stopDragJoints(); } void ofxSurfaceManagerGui::mouseDragged(ofMouseEventArgs &args) diff --git a/src/ofxTextureEditor.cpp b/src/ofxTextureEditor.cpp index 5bce467..33265c8 100644 --- a/src/ofxTextureEditor.cpp +++ b/src/ofxTextureEditor.cpp @@ -3,11 +3,38 @@ ofxTextureEditor::ofxTextureEditor() { clear(); + registerAppEvents(); } ofxTextureEditor::~ofxTextureEditor() { clear(); + unregisterAppEvents(); +} + +void ofxTextureEditor::registerAppEvents() +{ + ofAddListener(ofEvents().update, this, &ofxTextureEditor::update); +} + +void ofxTextureEditor::unregisterAppEvents() +{ + ofRemoveListener(ofEvents().update, this, &ofxTextureEditor::update); +} + +void ofxTextureEditor::update(ofEventArgs &args) +{ + if ( surface == NULL ) return; + + // update surface if one of the joints is being dragged + ofVec2f textureSize = ofVec2f( surface->getTexture()->getWidth(), surface->getTexture()->getHeight() ); + for ( int i=0; iisDragged() ) { + // update vertex to new location + surface->setTexCoord(i, joints[i]->position / textureSize); + break; + } + } } void ofxTextureEditor::draw() @@ -66,4 +93,21 @@ void ofxTextureEditor::moveTexCoords(ofVec2f by) joints[i]->position += by; texCoords[i] = joints[i]->position / textureSize; } +} + +void ofxTextureEditor::stopDragJoints() +{ + for (int i=0; istopDrag(); + } +} + +ofxCircleJoint* ofxTextureEditor::hitTestJoints(ofVec2f pos) +{ + for ( int i=0; ihitTest(pos) ){ + return joints[i]; + } + } + return NULL; } \ No newline at end of file diff --git a/src/ofxTextureEditor.h b/src/ofxTextureEditor.h index fcfac1c..cbfdafe 100644 --- a/src/ofxTextureEditor.h +++ b/src/ofxTextureEditor.h @@ -1,6 +1,7 @@ #ifndef H_OFX_TEXTURE_EDITOR #define H_OFX_TEXTURE_EDITOR +#include "ofEvents.h" #include "ofxBaseSurface.h" #include "ofxCircleJoint.h" @@ -10,6 +11,10 @@ public: ofxTextureEditor(); ~ofxTextureEditor(); + void registerAppEvents(); + void unregisterAppEvents(); + + void update(ofEventArgs& args); void draw(); void drawJoints(); void setSurface(ofxBaseSurface* newSurface); @@ -17,10 +22,12 @@ public: void createJoints(); void clearJoints(); void moveTexCoords(ofVec2f by); + void stopDragJoints(); + ofxCircleJoint* hitTestJoints(ofVec2f pos); private: ofxBaseSurface* surface; - vector joints; + vector joints; };