From bca5d0eadc9ee5510d02c74cf88990591f120f34 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Sat, 10 May 2014 01:08:50 +0200 Subject: [PATCH] Remove of internal event system from joints as events are not called if object is stored in a vector --- example/src/ofApp.cpp | 10 ++++++++-- src/ofxBaseJoint.cpp | 43 +++++++----------------------------------- src/ofxBaseJoint.h | 14 +++++--------- src/ofxCircleJoint.cpp | 4 ++-- src/ofxCircleJoint.h | 4 ++-- src/ofxSurfaceGui.cpp | 39 ++++++++++++++++++++++++++++++++++++-- src/ofxSurfaceGui.h | 8 ++++++-- 7 files changed, 67 insertions(+), 55 deletions(-) diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp index bf142e1..bbd4655 100644 --- a/example/src/ofApp.cpp +++ b/example/src/ofApp.cpp @@ -25,11 +25,14 @@ void ofApp::update() t.x = ofRandomuf(); t.y = ofRandomuf(); //triangleSurface.setTexCoord(0, t); + + gui.update(); } void ofApp::draw() { triangleSurface.draw(); + gui.draw(); } void ofApp::keyPressed(int key) @@ -39,15 +42,18 @@ void ofApp::keyPressed(int key) void ofApp::mousePressed(int x, int y, int button) { - cout << "Mouse pressed." << endl; + //cout << "Mouse pressed." << endl; + gui.mousePressed(x, y, button); } void ofApp::mouseReleased(int x, int y, int button) { - cout << "Mouse released." << endl; + //cout << "Mouse released." << endl; + gui.mouseReleased(x, y, button); } void ofApp::mouseDragged(int x, int y, int button) { // + gui.mouseDragged(x, y, button); } \ No newline at end of file diff --git a/src/ofxBaseJoint.cpp b/src/ofxBaseJoint.cpp index efba3a2..4afb8d0 100644 --- a/src/ofxBaseJoint.cpp +++ b/src/ofxBaseJoint.cpp @@ -4,33 +4,30 @@ ofxBaseJoint::ofxBaseJoint() { setDefaultColors(); setDefaultProperties(); - registerAppEvents(); - registerMouseEvents(); } ofxBaseJoint::~ofxBaseJoint() { - unregisterAppEvents(); - unregisterMouseEvents(); + } -void ofxBaseJoint::mousePressed(ofMouseEventArgs &args) +void ofxBaseJoint::mousePressed(int x, int y, int button) { - if ( hitTest(ofVec2f(args.x, args.y)) ) { - clickDistance = position - ofVec2f(args.x, args.y); + if ( hitTest(ofVec2f(x, y)) ) { + clickDistance = position - ofVec2f(x, y); startDrag(); } } -void ofxBaseJoint::mouseReleased(ofMouseEventArgs &args) +void ofxBaseJoint::mouseReleased(int x, int y, int button) { stopDrag(); } -void ofxBaseJoint::mouseDragged(ofMouseEventArgs &args) +void ofxBaseJoint::mouseDragged(int x, int y, int button) { if ( !dragging ) return; - position = ofVec2f(args.x, args.y) + clickDistance; + position = ofVec2f(x, y) + clickDistance; } void ofxBaseJoint::startDrag() @@ -58,30 +55,4 @@ void ofxBaseJoint::setDefaultProperties() dragging = false; selected = false; strokeWidth = 1.5f; -} - -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); - ofAddListener(ofEvents().mouseReleased, this, &ofxBaseJoint::mouseReleased); - ofAddListener(ofEvents().mouseDragged, this, &ofxBaseJoint::mouseDragged); -} - -void ofxBaseJoint::unregisterMouseEvents() -{ - ofRemoveListener(ofEvents().mousePressed, this, &ofxBaseJoint::mousePressed); - ofRemoveListener(ofEvents().mouseReleased, this, &ofxBaseJoint::mouseReleased); - ofRemoveListener(ofEvents().mouseDragged, this, &ofxBaseJoint::mouseDragged); } \ No newline at end of file diff --git a/src/ofxBaseJoint.h b/src/ofxBaseJoint.h index a4e5597..cdb48b0 100644 --- a/src/ofxBaseJoint.h +++ b/src/ofxBaseJoint.h @@ -13,14 +13,14 @@ public: bool visible; bool selected; - void mousePressed(ofMouseEventArgs& args); - void mouseReleased(ofMouseEventArgs& args); - void mouseDragged(ofMouseEventArgs& args); + 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 startDrag(); void stopDrag(); - virtual void update(ofEventArgs& args){}; - virtual void draw(ofEventArgs& args){}; + virtual void update(){}; + virtual void draw(){}; virtual bool hitTest(ofVec2f position){}; protected: @@ -33,10 +33,6 @@ protected: private: void setDefaultColors(); void setDefaultProperties(); - void registerAppEvents(); - void unregisterAppEvents(); - void registerMouseEvents(); - void unregisterMouseEvents(); }; #endif diff --git a/src/ofxCircleJoint.cpp b/src/ofxCircleJoint.cpp index ff0e249..82165af 100644 --- a/src/ofxCircleJoint.cpp +++ b/src/ofxCircleJoint.cpp @@ -5,12 +5,12 @@ ofxCircleJoint::ofxCircleJoint() setDefaultProperties(); } -void ofxCircleJoint::update(ofEventArgs& args) +void ofxCircleJoint::update() { if (!enabled) return; } -void ofxCircleJoint::draw(ofEventArgs& args) +void ofxCircleJoint::draw() { if (!visible) return; if (!enabled) return; diff --git a/src/ofxCircleJoint.h b/src/ofxCircleJoint.h index f225bae..f3016e9 100644 --- a/src/ofxCircleJoint.h +++ b/src/ofxCircleJoint.h @@ -9,8 +9,8 @@ class ofxCircleJoint : public ofxBaseJoint public: ofxCircleJoint(); - void update(ofEventArgs& args); - void draw(ofEventArgs& args); + void update(); + void draw(); bool hitTest(ofVec2f position); private: diff --git a/src/ofxSurfaceGui.cpp b/src/ofxSurfaceGui.cpp index 032181a..f860393 100644 --- a/src/ofxSurfaceGui.cpp +++ b/src/ofxSurfaceGui.cpp @@ -2,7 +2,7 @@ ofxSurfaceGui::ofxSurfaceGui() { - jointCounter = 0; + } ofxSurfaceGui::~ofxSurfaceGui() @@ -16,9 +16,44 @@ void ofxSurfaceGui::setup(ofxTriangleSurface& surfaceForGui) addNumJoints(3); } +void ofxSurfaceGui::update() +{ + for ( int i=0; i joints; void addJoint(); void addNumJoints(int num);