diff --git a/example/bin/data/surfaces.xml b/example/bin/data/surfaces.xml index 388c4c5..4bcc56a 100644 --- a/example/bin/data/surfaces.xml +++ b/example/bin/data/surfaces.xml @@ -36,16 +36,16 @@ - 160.000000000 - 28.000000000 + 24.396121979 + 259.170288086 - 260.000000000 - 209.000000000 + 250.000000000 + 34.000000000 - 42.000000000 - 206.000000000 + 214.630920410 + 282.926849365 diff --git a/src/ofxBaseJoint.cpp b/src/ofxBaseJoint.cpp index bedede5..22d147d 100644 --- a/src/ofxBaseJoint.cpp +++ b/src/ofxBaseJoint.cpp @@ -64,6 +64,11 @@ void ofxBaseJoint::unselect() selected = false; } +void ofxBaseJoint::setClickDistance(ofVec2f newClickDistance) +{ + clickDistance = newClickDistance; +} + bool ofxBaseJoint::isDragged() { return bDrag; diff --git a/src/ofxBaseJoint.h b/src/ofxBaseJoint.h index 25b0f55..d3decb7 100644 --- a/src/ofxBaseJoint.h +++ b/src/ofxBaseJoint.h @@ -23,6 +23,7 @@ public: void stopDrag(); void select(); void unselect(); + void setClickDistance(ofVec2f newClickDistance); bool isDragged(); bool isSelected(); diff --git a/src/ofxProjectionEditor.cpp b/src/ofxProjectionEditor.cpp index 946a168..eb9c050 100644 --- a/src/ofxProjectionEditor.cpp +++ b/src/ofxProjectionEditor.cpp @@ -4,6 +4,7 @@ ofxProjectionEditor::ofxProjectionEditor() { surfaceManager = NULL; bShiftKeyDown = false; + fSnapDistance = 10.0f; enable(); } @@ -84,7 +85,36 @@ void ofxProjectionEditor::draw() void ofxProjectionEditor::mouseDragged(ofMouseEventArgs &args) { - // + ofVec2f mousePosition = ofVec2f(args.x, args.y); + + // Collect all vertices of the projection surfaces + vector allVertices; + for ( int i=0; isize(); i++ ) { + ofxBaseSurface* surface = surfaceManager->getSurface(i); + if ( surface == surfaceManager->getSelectedSurface() ) { + continue; // Don't add vertices of selected surface + } + for ( int j=0; jgetVertices().size(); j++ ) { + allVertices.push_back(&surface->getVertices()[j]); + } + } + + // Snap currently dragged joint to nearest vertex + for ( int i=0; iisDragged() ) { + // Snap it! + for ( int j=0; jposition = *allVertices[j]; + ofVec2f clickDistance = joints[i]->position - ofVec2f(args.x, args.y); + joints[i]->setClickDistance(clickDistance); + break; + } + } + } + } } void ofxProjectionEditor::keyPressed(ofKeyEventArgs &args) @@ -205,6 +235,11 @@ void ofxProjectionEditor::moveSelection(ofVec2f by) } } +void ofxProjectionEditor::setSnapDistance(float newSnapDistance) +{ + fSnapDistance = newSnapDistance; +} + ofxCircleJoint* ofxProjectionEditor::hitTestJoints(ofVec2f pos) { for ( int i=0; i joints; bool bShiftKeyDown; + float fSnapDistance; void drawJoints(); };