From 4c93ea241fa0ff301b41af312a51ae944c527d50 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Mon, 12 May 2014 17:08:26 +0200 Subject: [PATCH] Add projection surface joint movement --- src/ofxBaseJoint.cpp | 33 ++- src/ofxBaseJoint.h | 9 +- src/ofxProjectionEditor.cpp | 56 ++++- src/ofxProjectionEditor.h | 11 +- src/ofxSurfaceGui.cpp | 384 ----------------------------------- src/ofxSurfaceGui.h | 71 ------- src/ofxSurfaceManagerGui.cpp | 32 +-- 7 files changed, 110 insertions(+), 486 deletions(-) delete mode 100644 src/ofxSurfaceGui.cpp delete mode 100644 src/ofxSurfaceGui.h diff --git a/src/ofxBaseJoint.cpp b/src/ofxBaseJoint.cpp index 0c87068..0360a72 100644 --- a/src/ofxBaseJoint.cpp +++ b/src/ofxBaseJoint.cpp @@ -4,18 +4,31 @@ ofxBaseJoint::ofxBaseJoint() { setDefaultColors(); setDefaultProperties(); + registerMouseEvents(); } ofxBaseJoint::~ofxBaseJoint() { + unregisterMouseEvents(); +} +void ofxBaseJoint::registerMouseEvents() +{ + ofAddListener(ofEvents().mousePressed, this, &ofxBaseJoint::mousePressed); + ofAddListener(ofEvents().mouseDragged, this, &ofxBaseJoint::mouseDragged); +} + +void ofxBaseJoint::unregisterMouseEvents() +{ + ofRemoveListener(ofEvents().mousePressed, this, &ofxBaseJoint::mousePressed); + ofRemoveListener(ofEvents().mouseDragged, this, &ofxBaseJoint::mouseDragged); } -void ofxBaseJoint::mousePressed(int x, int y, int button) +void ofxBaseJoint::mousePressed(ofMouseEventArgs& args) { - if ( hitTest(ofVec2f(x, y)) ) { + if ( hitTest(ofVec2f(args.x, args.y)) ) { //selected = true; - clickDistance = position - ofVec2f(x, y); + clickDistance = position - ofVec2f(args.x, args.y); //startDrag(); } } @@ -25,25 +38,25 @@ void ofxBaseJoint::mouseReleased(int x, int y, int button) stopDrag(); } -void ofxBaseJoint::mouseDragged(int x, int y, int button) +void ofxBaseJoint::mouseDragged(ofMouseEventArgs& args) { - if ( !dragging ) return; - position = ofVec2f(x, y) + clickDistance; + if ( !bDrag ) return; + position = ofVec2f(args.x, args.y) + clickDistance; } void ofxBaseJoint::startDrag() { - dragging = true; + bDrag = true; } void ofxBaseJoint::stopDrag() { - dragging = false; + bDrag = false; } bool ofxBaseJoint::isDragged() { - return dragging; + return bDrag; } void ofxBaseJoint::setDefaultColors() @@ -60,7 +73,7 @@ void ofxBaseJoint::setDefaultProperties() visible = true; position = ofVec2f(20.0f, 20.0f); clickDistance = ofVec2f(0.0f, 0.0f); - dragging = false; + bDrag = false; selected = false; strokeWidth = 1.5f; } \ No newline at end of file diff --git a/src/ofxBaseJoint.h b/src/ofxBaseJoint.h index 7fc8495..a75e235 100644 --- a/src/ofxBaseJoint.h +++ b/src/ofxBaseJoint.h @@ -8,14 +8,17 @@ public: ofxBaseJoint(); ~ofxBaseJoint(); + void registerMouseEvents(); + void unregisterMouseEvents(); + ofVec2f position; bool enabled; bool visible; bool selected; - void mousePressed(int x, int y, int button); + void mousePressed(ofMouseEventArgs& args); void mouseReleased(int x, int y, int button); - void mouseDragged(int x, int y, int button); + void mouseDragged(ofMouseEventArgs& args); void startDrag(); void stopDrag(); bool isDragged(); @@ -31,7 +34,7 @@ protected: ofColor strokeColorSelected; float strokeWidth; ofVec2f clickDistance; // How far from the center of the joint the user has clicked? - bool dragging; + bool bDrag; private: void setDefaultColors(); diff --git a/src/ofxProjectionEditor.cpp b/src/ofxProjectionEditor.cpp index b1c7bc1..23d76f0 100644 --- a/src/ofxProjectionEditor.cpp +++ b/src/ofxProjectionEditor.cpp @@ -3,23 +3,63 @@ ofxProjectionEditor::ofxProjectionEditor() { surfaceManager = NULL; + registerAppEvents(); + registerMouseEvents(); } ofxProjectionEditor::~ofxProjectionEditor() { clearJoints(); surfaceManager = NULL; + unregisterAppEvents(); + unregisterMouseEvents(); +} + +void ofxProjectionEditor::registerAppEvents() +{ + ofAddListener(ofEvents().update, this, &ofxProjectionEditor::update); +} + +void ofxProjectionEditor::unregisterAppEvents() +{ + ofRemoveListener(ofEvents().update, this, &ofxProjectionEditor::update); +} + +void ofxProjectionEditor::registerMouseEvents() +{ + ofAddListener(ofEvents().mouseDragged, this, &ofxProjectionEditor::mouseDragged); +} + +void ofxProjectionEditor::unregisterMouseEvents() +{ + ofRemoveListener(ofEvents().mouseDragged, this, &ofxProjectionEditor::mouseDragged); +} + +void ofxProjectionEditor::update(ofEventArgs &args) +{ + // update surface if one of the joints is being dragged + for ( int i=0; iisDragged() ) { + // update vertex to new location + surfaceManager->getSelectedSurface()->setVertex(i, joints[i]->position); + break; + } + } } void ofxProjectionEditor::draw() { if ( surfaceManager == NULL ) return; - cout << "ofxProjectionEditor::draw()" << endl; if ( surfaceManager->getSelectedSurface() == NULL ) return; if ( joints.size() <= 0 ) createJoints(); drawJoints(); } +void ofxProjectionEditor::mouseDragged(ofMouseEventArgs &args) +{ + // +} + void ofxProjectionEditor::setSurfaceManager(ofxSurfaceManager *newSurfaceManager) { surfaceManager = newSurfaceManager; @@ -70,19 +110,25 @@ void ofxProjectionEditor::moveSelectedSurface(ofVec2f by) updateJoints(); } -bool ofxProjectionEditor::hitTestJoints(ofVec2f pos) +void ofxProjectionEditor::stopDragJoints() +{ + for (int i=0; istopDrag(); + } +} + +ofxCircleJoint* ofxProjectionEditor::hitTestJoints(ofVec2f pos) { for ( int i=0; ihitTest(pos) ){ - return true; + return joints[i]; } } - return false; + return NULL; } void ofxProjectionEditor::drawJoints() { - cout << "draw joints" << endl; for ( int i=0; idraw(); } diff --git a/src/ofxProjectionEditor.h b/src/ofxProjectionEditor.h index 8da6693..ae72597 100644 --- a/src/ofxProjectionEditor.h +++ b/src/ofxProjectionEditor.h @@ -10,13 +10,22 @@ public: ofxProjectionEditor(); ~ofxProjectionEditor(); + void registerAppEvents(); + void unregisterAppEvents(); + void registerMouseEvents(); + void unregisterMouseEvents(); + + void update(ofEventArgs& args); void draw(); + void mouseDragged(ofMouseEventArgs& args); void setSurfaceManager(ofxSurfaceManager* newSurfaceManager); void clearJoints(); void createJoints(); void updateJoints(); void moveSelectedSurface(ofVec2f by); - bool hitTestJoints(ofVec2f pos); + void stopDragJoints(); + void updateVertices(); + ofxCircleJoint* hitTestJoints(ofVec2f pos); private: ofxSurfaceManager* surfaceManager; diff --git a/src/ofxSurfaceGui.cpp b/src/ofxSurfaceGui.cpp deleted file mode 100644 index f44a7af..0000000 --- a/src/ofxSurfaceGui.cpp +++ /dev/null @@ -1,384 +0,0 @@ -#include "ofxSurfaceGui.h" - -ofxSurfaceGui::ofxSurfaceGui() -{ - surface = NULL; - mode = NONE; - bProjectionMappingJointSelected = false; - bTextureMappingJointSelected = false; - bTextureDragging = false; - bProjectionDragging = false; - bSelected = false; -} - -ofxSurfaceGui::~ofxSurfaceGui() -{ - -} - -void ofxSurfaceGui::setup(ofxTriangleSurface& surfaceForGui) -{ - surface = &surfaceForGui; - addNumProjectionMappingJoints(3); - addNumTextureMappingJoints(3); -} - -void ofxSurfaceGui::update() -{ - if (surface == NULL) return; - if (mode == NONE) return; - - if (mode == PROJECTION_MAPPING) { - for ( int i=0; isetVertex(i, projectionMappingJoints[i].position); - } - updateProjectionHitarea(); - clickPosition = curPos; - } else { - for ( int i=0; isetVertex(i, projectionMappingJoints[i].position); - updateProjectionHitarea(); - } - } // for - } // if ( bProjectionDragging - } else if (mode == TEXTURE_MAPPING) { - ofVec2f textureSize = ofVec2f( surface->getTexture()->getWidth(), surface->getTexture()->getHeight() ); - if ( bTextureDragging ) { - ofVec2f curPos = ofVec2f(x, y); - ofVec2f dist = curPos - clickPosition; - for ( int i=0; isetTexCoord(i, textureMappingJoints[i].position/textureSize); - } - updateTextureHitarea(); - clickPosition = curPos; - } else { - for ( int i=0; isetTexCoord(i, textureMappingJoints[i].position/textureSize); - updateTextureHitarea(); - } - } // for - } // if ( bTextureDragging - } // if (mode -} - -void ofxSurfaceGui::setMode(ofxSurfaceGui::editMode newMode) -{ - if (mode != NONE && - mode != PROJECTION_MAPPING && - mode != TEXTURE_MAPPING) { - throw std::runtime_error("Trying to set invalid mode."); - }; - - mode = newMode; -} - -void ofxSurfaceGui::select() -{ - bSelected = true; -} - -void ofxSurfaceGui::unselect() -{ - bSelected = false; -} - -void ofxSurfaceGui::updateTextureHitarea() -{ - textureHitarea.clear(); - for ( int i=0; igetVertex(i); - } - updateProjectionHitarea(); - - ofVec2f textureSize = ofVec2f(surface->getTexture()->getWidth(), surface->getTexture()->getHeight()); - for ( int i=0; igetTexCoord(i) * textureSize; - } - updateTextureHitarea(); -} - -bool ofxSurfaceGui::hitTest(float x, float y) -{ - if (mode == PROJECTION_MAPPING){ - return hitTestProjectionArea(x, y); - } else if (mode == TEXTURE_MAPPING) { - return hitTestTextureArea(x, y); - } -} - -bool ofxSurfaceGui::hitTestTextureArea(float x, float y) -{ - if ( textureAreaExists() ) { - if ( textureHitarea.inside(x, y) ) { - return true; - } else { - // are we hitting texture mapping joints? - for ( int i=0; igetVertex(projectionMappingJoints.size()-1); - updateProjectionHitarea(); -} - -void ofxSurfaceGui::addNumProjectionMappingJoints(int num) -{ - for ( int i=0; igetTexture()->getWidth(), surface->getTexture()->getHeight()); - textureMappingJoints.back().position = surface->getTexCoord(textureMappingJoints.size()-1) * textureSize; - updateTextureHitarea(); -} - -void ofxSurfaceGui::addNumTextureMappingJoints(int num) -{ - for ( int i=0; i 2 ) { - return true; - } else { - return false; - } -} - -bool ofxSurfaceGui::textureAreaExists() -{ - if ( textureHitarea.size() > 2 ) { - return true; - } else { - return false; - } -} \ No newline at end of file diff --git a/src/ofxSurfaceGui.h b/src/ofxSurfaceGui.h deleted file mode 100644 index 3964dff..0000000 --- a/src/ofxSurfaceGui.h +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef H_OFX_SURFACE_GUI -#define H_OFX_SURFACE_GUI - -#include "ofMain.h" -#include "ofxTriangleSurface.h" -#include "ofxCircleJoint.h" - -class ofxSurfaceGui -{ -public: - ofxSurfaceGui(); - ~ofxSurfaceGui(); - - enum editMode { - NONE, - TEXTURE_MAPPING, - PROJECTION_MAPPING - }; - - void setup(ofxTriangleSurface& surfaceForGui); - 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 setMode(editMode newMode); - void select(); - void unselect(); - void updateTextureHitarea(); - void updateProjectionHitarea(); - void updateHitarea(); // update both - void updateJoints(); - - bool hitTest(float x, float y); - bool hitTestTextureArea(float x, float y); - bool hitTestProjectionArea(float x, float y); - bool isSelected(); - - editMode getMode(); - -private: - editMode mode; - ofxTriangleSurface* surface; - ofPolyline textureHitarea; - ofPolyline projectionHitarea; - ofVec2f clickPosition; - - vector projectionMappingJoints; - vector textureMappingJoints; - - bool bTextureMappingJointSelected; - bool bProjectionMappingJointSelected; - bool bTextureDragging; - bool bProjectionDragging; - bool bSelected; - - bool isProjectionMappingJointSelected(); - bool isTextureMappingJointSelected(); - bool projectionAreaExists(); - bool textureAreaExists(); - - void addProjectionMappingJoint(); - void addNumProjectionMappingJoints(int num); - void addTextureMappingJoint(); - void addNumTextureMappingJoints(int num); - void dragTextureArea(); - void dragProjectionArea(); - void stopDrag(); -}; - -#endif \ No newline at end of file diff --git a/src/ofxSurfaceManagerGui.cpp b/src/ofxSurfaceManagerGui.cpp index 39b4b8f..a1e7b96 100644 --- a/src/ofxSurfaceManagerGui.cpp +++ b/src/ofxSurfaceManagerGui.cpp @@ -74,22 +74,29 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args) } else if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) { return; } else if ( guiMode == ofxGuiMode::PROJECTION_MAPPING ) { - // attempt to select surface, loop from end to beginning + bool bSurfaceSelected = false; - for ( int i=surfaceManager->size()-1; i>=0; i-- ) { - if ( surfaceManager->getSurface(i)->hitTest( ofVec2f(args.x, args.y) ) ) { - projectionEditor.clearJoints(); - surfaceManager->selectSurface(i); - projectionEditor.createJoints(); - bSurfaceSelected = true; - break; + + ofxCircleJoint* hitJoint = projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)); + if ( hitJoint != NULL ) { + hitJoint->startDrag(); + bSurfaceSelected = true; + } + + // attempt to select surface, loop from end to beginning + if ( !bSurfaceSelected ){ + for ( int i=surfaceManager->size()-1; i>=0; i-- ) { + if ( surfaceManager->getSurface(i)->hitTest( ofVec2f(args.x, args.y) ) ) { + projectionEditor.clearJoints(); + surfaceManager->selectSurface(i); + projectionEditor.createJoints(); + bSurfaceSelected = true; + break; + } } } - // Check if hitting one of the joints as that also counts as hit when surface is selected - if ( projectionEditor.hitTestJoints(ofVec2f(args.x, args.y)) ) { - bSurfaceSelected = true; - } else if ( bSurfaceSelected ) { + if ( bSurfaceSelected && hitJoint == NULL ) { // if not hitting the joints, start drag only if we have a selected surface clickPosition = ofVec2f(args.x, args.y); startDrag(); @@ -106,6 +113,7 @@ void ofxSurfaceManagerGui::mousePressed(ofMouseEventArgs &args) void ofxSurfaceManagerGui::mouseReleased(ofMouseEventArgs &args) { stopDrag(); + projectionEditor.stopDragJoints(); } void ofxSurfaceManagerGui::mouseDragged(ofMouseEventArgs &args)