From 4195a8ccfe377e7295853270a9981c4a0df982a9 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Fri, 9 May 2014 23:31:26 +0200 Subject: [PATCH] Make the joint draggable --- example/src/ofApp.h | 4 +++ src/ofxBaseJoint.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++ src/ofxBaseJoint.h | 29 +++++++++++++++++--- src/ofxCircleJoint.cpp | 34 +++++++++++++++++++----- src/ofxCircleJoint.h | 13 ++++++--- 5 files changed, 127 insertions(+), 13 deletions(-) diff --git a/example/src/ofApp.h b/example/src/ofApp.h index 1edd863..5789e6a 100644 --- a/example/src/ofApp.h +++ b/example/src/ofApp.h @@ -4,6 +4,8 @@ #include "ofMain.h" #include "ofxPiMapper.h" +#include "ofxCircleJoint.h" + class ofApp : public ofBaseApp { public: @@ -18,6 +20,8 @@ public: ofxTriangleSurface triangleSurface; ofImage image; + + ofxCircleJoint joint; }; #endif \ No newline at end of file diff --git a/src/ofxBaseJoint.cpp b/src/ofxBaseJoint.cpp index 11195ab..80c8e9d 100644 --- a/src/ofxBaseJoint.cpp +++ b/src/ofxBaseJoint.cpp @@ -2,14 +2,74 @@ ofxBaseJoint::ofxBaseJoint() { + setDefaultColors(); + setDefaultProperties(); + registerAppEvents(); registerMouseEvents(); } ofxBaseJoint::~ofxBaseJoint() { + unregisterAppEvents(); 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() { ofAddListener(ofEvents().mousePressed, this, &ofxBaseJoint::mousePressed); diff --git a/src/ofxBaseJoint.h b/src/ofxBaseJoint.h index 34549c6..2bc7c09 100644 --- a/src/ofxBaseJoint.h +++ b/src/ofxBaseJoint.h @@ -8,12 +8,33 @@ public: 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 unregisterMouseEvents(); - - virtual void mousePressed(ofMouseEventArgs& args){}; - virtual void mouseReleased(ofMouseEventArgs& args){}; - virtual void mouseDragged(ofMouseEventArgs& args){}; }; #endif diff --git a/src/ofxCircleJoint.cpp b/src/ofxCircleJoint.cpp index 1e2a91c..2721894 100644 --- a/src/ofxCircleJoint.cpp +++ b/src/ofxCircleJoint.cpp @@ -1,16 +1,38 @@ #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; } \ No newline at end of file diff --git a/src/ofxCircleJoint.h b/src/ofxCircleJoint.h index 60aca84..f225bae 100644 --- a/src/ofxCircleJoint.h +++ b/src/ofxCircleJoint.h @@ -7,9 +7,16 @@ class ofxCircleJoint : public ofxBaseJoint { public: - void mousePressed(ofMouseEventArgs& args); - void mouseReleased(ofMouseEventArgs& args); - void mouseDragged(ofMouseEventArgs& args); + ofxCircleJoint(); + + void update(ofEventArgs& args); + void draw(ofEventArgs& args); + bool hitTest(ofVec2f position); + +private: + float radius; + + void setDefaultProperties(); }; #endif \ No newline at end of file