Browse Source

Make the joint draggable

master
Krisjanis Rijnieks 11 years ago
parent
commit
4195a8ccfe
  1. 4
      example/src/ofApp.h
  2. 60
      src/ofxBaseJoint.cpp
  3. 29
      src/ofxBaseJoint.h
  4. 34
      src/ofxCircleJoint.cpp
  5. 13
      src/ofxCircleJoint.h

4
example/src/ofApp.h

@ -4,6 +4,8 @@
#include "ofMain.h" #include "ofMain.h"
#include "ofxPiMapper.h" #include "ofxPiMapper.h"
#include "ofxCircleJoint.h"
class ofApp : public ofBaseApp class ofApp : public ofBaseApp
{ {
public: public:
@ -18,6 +20,8 @@ public:
ofxTriangleSurface triangleSurface; ofxTriangleSurface triangleSurface;
ofImage image; ofImage image;
ofxCircleJoint joint;
}; };
#endif #endif

60
src/ofxBaseJoint.cpp

@ -2,14 +2,74 @@
ofxBaseJoint::ofxBaseJoint() ofxBaseJoint::ofxBaseJoint()
{ {
setDefaultColors();
setDefaultProperties();
registerAppEvents();
registerMouseEvents(); registerMouseEvents();
} }
ofxBaseJoint::~ofxBaseJoint() ofxBaseJoint::~ofxBaseJoint()
{ {
unregisterAppEvents();
unregisterMouseEvents(); unregisterMouseEvents();
} }
void ofxBaseJoint::mousePressed(ofMouseEventArgs &args)
{
if ( hitTest(ofVec2f(args.x, args.y)) ) {
clickDistance = position - ofVec2f(args.x, args.y);
startDrag();
}
}
void ofxBaseJoint::mouseReleased(ofMouseEventArgs &args)
{
stopDrag();
}
void ofxBaseJoint::mouseDragged(ofMouseEventArgs &args)
{
if ( !dragging ) return;
position = ofVec2f(args.x, args.y) + clickDistance;
}
void ofxBaseJoint::startDrag()
{
dragging = true;
}
void ofxBaseJoint::stopDrag()
{
dragging = false;
}
void ofxBaseJoint::setDefaultColors()
{
fillColor = ofColor(0,255,255);
strokeColor = ofColor(255,255,255);
}
void ofxBaseJoint::setDefaultProperties()
{
enabled = true;
visible = true;
position = ofVec2f(20.0f, 20.0f);
clickDistance = ofVec2f(0.0f, 0.0f);
dragging = false;
}
void ofxBaseJoint::registerAppEvents()
{
ofAddListener(ofEvents().update, this, &ofxBaseJoint::update);
ofAddListener(ofEvents().draw, this, &ofxBaseJoint::draw);
}
void ofxBaseJoint::unregisterAppEvents()
{
ofRemoveListener(ofEvents().update, this, &ofxBaseJoint::update);
ofRemoveListener(ofEvents().draw, this, &ofxBaseJoint::draw);
}
void ofxBaseJoint::registerMouseEvents() void ofxBaseJoint::registerMouseEvents()
{ {
ofAddListener(ofEvents().mousePressed, this, &ofxBaseJoint::mousePressed); ofAddListener(ofEvents().mousePressed, this, &ofxBaseJoint::mousePressed);

29
src/ofxBaseJoint.h

@ -8,12 +8,33 @@ public:
ofxBaseJoint(); ofxBaseJoint();
~ofxBaseJoint(); ~ofxBaseJoint();
bool enabled;
bool visible;
void mousePressed(ofMouseEventArgs& args);
void mouseReleased(ofMouseEventArgs& args);
void mouseDragged(ofMouseEventArgs& args);
void startDrag();
void stopDrag();
virtual void update(ofEventArgs& args){};
virtual void draw(ofEventArgs& args){};
virtual bool hitTest(ofVec2f position){};
protected:
ofColor fillColor;
ofColor strokeColor;
ofVec2f position;
ofVec2f clickDistance;
bool dragging;
private:
void setDefaultColors();
void setDefaultProperties();
void registerAppEvents();
void unregisterAppEvents();
void registerMouseEvents(); void registerMouseEvents();
void unregisterMouseEvents(); void unregisterMouseEvents();
virtual void mousePressed(ofMouseEventArgs& args){};
virtual void mouseReleased(ofMouseEventArgs& args){};
virtual void mouseDragged(ofMouseEventArgs& args){};
}; };
#endif #endif

34
src/ofxCircleJoint.cpp

@ -1,16 +1,38 @@
#include "ofxCircleJoint.h" #include "ofxCircleJoint.h"
void ofxCircleJoint::mousePressed(ofMouseEventArgs &args) ofxCircleJoint::ofxCircleJoint()
{ {
cout << "mouse pressed" << endl; setDefaultProperties();
} }
void ofxCircleJoint::mouseReleased(ofMouseEventArgs &args) void ofxCircleJoint::update(ofEventArgs& args)
{ {
cout << "mouse released" << endl; if (!enabled) return;
} }
void ofxCircleJoint::mouseDragged(ofMouseEventArgs &args) void ofxCircleJoint::draw(ofEventArgs& args)
{ {
cout << "mouse dragged" << endl; if (!visible) return;
if (!enabled) return;
ofPushStyle();
ofFill();
ofSetColor(fillColor);
ofCircle(position.x, position.y, radius);
ofNoFill();
ofSetColor(strokeColor);
ofCircle(position.x, position.y, radius);
ofPopStyle();
}
void ofxCircleJoint::setDefaultProperties()
{
radius = 10.0f;
}
bool ofxCircleJoint::hitTest(ofVec2f pos)
{
float distance = position.distance(pos);
if ( distance < radius ) return true;
else return false;
} }

13
src/ofxCircleJoint.h

@ -7,9 +7,16 @@
class ofxCircleJoint : public ofxBaseJoint class ofxCircleJoint : public ofxBaseJoint
{ {
public: public:
void mousePressed(ofMouseEventArgs& args); ofxCircleJoint();
void mouseReleased(ofMouseEventArgs& args);
void mouseDragged(ofMouseEventArgs& args); void update(ofEventArgs& args);
void draw(ofEventArgs& args);
bool hitTest(ofVec2f position);
private:
float radius;
void setDefaultProperties();
}; };
#endif #endif
Loading…
Cancel
Save