Browse Source

Remove of internal event system from joints as events are not called if object is stored in a vector

master
Krisjanis Rijnieks 11 years ago
parent
commit
bca5d0eadc
  1. 10
      example/src/ofApp.cpp
  2. 43
      src/ofxBaseJoint.cpp
  3. 14
      src/ofxBaseJoint.h
  4. 4
      src/ofxCircleJoint.cpp
  5. 4
      src/ofxCircleJoint.h
  6. 39
      src/ofxSurfaceGui.cpp
  7. 8
      src/ofxSurfaceGui.h

10
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);
}

43
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);
}

14
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

4
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;

4
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:

39
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.size(); i++ ) {
joints[i].update();
}
}
void ofxSurfaceGui::draw()
{
for ( int i=0; i<joints.size(); i++ ) {
joints[i].draw();
}
}
void ofxSurfaceGui::mousePressed(int x, int y, int button)
{
for ( int i=0; i<joints.size(); i++ ) {
joints[i].mousePressed(x, y, button);
}
}
void ofxSurfaceGui::mouseReleased(int x, int y, int button)
{
for ( int i=0; i<joints.size(); i++ ) {
joints[i].mouseReleased(x, y, button);
}
}
void ofxSurfaceGui::mouseDragged(int x, int y, int button)
{
for ( int i=0; i<joints.size(); i++ ) {
joints[i].mouseDragged(x, y, button);
}
}
void ofxSurfaceGui::addJoint()
{
jointCounter++;
joints.push_back(ofxCircleJoint());
}
void ofxSurfaceGui::addNumJoints(int num)

8
src/ofxSurfaceGui.h

@ -12,11 +12,15 @@ public:
~ofxSurfaceGui();
void setup(ofxTriangleSurface& surfaceForGui);
void update();
void draw();
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseDragged(int x, int y, int button);
private:
ofxTriangleSurface* surface;
ofxCircleJoint joints[100];
int jointCounter;
vector<ofxCircleJoint> joints;
void addJoint();
void addNumJoints(int num);

Loading…
Cancel
Save