Browse Source

Add snapping to projection surface editor

master
Krisjanis Rijnieks 11 years ago
parent
commit
cbac99cbdf
  1. 12
      example/bin/data/surfaces.xml
  2. 5
      src/ofxBaseJoint.cpp
  3. 1
      src/ofxBaseJoint.h
  4. 37
      src/ofxProjectionEditor.cpp
  5. 2
      src/ofxProjectionEditor.h

12
example/bin/data/surfaces.xml

@ -36,16 +36,16 @@
<surface>
<vertices>
<vertex>
<x>160.000000000</x>
<y>28.000000000</y>
<x>24.396121979</x>
<y>259.170288086</y>
</vertex>
<vertex>
<x>260.000000000</x>
<y>209.000000000</y>
<x>250.000000000</x>
<y>34.000000000</y>
</vertex>
<vertex>
<x>42.000000000</x>
<y>206.000000000</y>
<x>214.630920410</x>
<y>282.926849365</y>
</vertex>
</vertices>
<texCoords>

5
src/ofxBaseJoint.cpp

@ -64,6 +64,11 @@ void ofxBaseJoint::unselect()
selected = false;
}
void ofxBaseJoint::setClickDistance(ofVec2f newClickDistance)
{
clickDistance = newClickDistance;
}
bool ofxBaseJoint::isDragged()
{
return bDrag;

1
src/ofxBaseJoint.h

@ -23,6 +23,7 @@ public:
void stopDrag();
void select();
void unselect();
void setClickDistance(ofVec2f newClickDistance);
bool isDragged();
bool isSelected();

37
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<ofVec3f*> allVertices;
for ( int i=0; i<surfaceManager->size(); i++ ) {
ofxBaseSurface* surface = surfaceManager->getSurface(i);
if ( surface == surfaceManager->getSelectedSurface() ) {
continue; // Don't add vertices of selected surface
}
for ( int j=0; j<surface->getVertices().size(); j++ ) {
allVertices.push_back(&surface->getVertices()[j]);
}
}
// Snap currently dragged joint to nearest vertex
for ( int i=0; i<joints.size(); i++ ) {
if ( joints[i]->isDragged() ) {
// Snap it!
for ( int j=0; j<allVertices.size(); j++ ) {
float distance = mousePosition.distance(*allVertices[j]);
//cout << "distance: " << distance << endl;
if ( distance < fSnapDistance ) {
joints[i]->position = *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.size(); i++ ) {

2
src/ofxProjectionEditor.h

@ -35,12 +35,14 @@ public:
void stopDragJoints();
void updateVertices();
void moveSelection(ofVec2f by);
void setSnapDistance(float newSnapDistance);
ofxCircleJoint* hitTestJoints(ofVec2f pos);
private:
ofxSurfaceManager* surfaceManager;
vector<ofxCircleJoint*> joints;
bool bShiftKeyDown;
float fSnapDistance;
void drawJoints();
};

Loading…
Cancel
Save