How to detect mouse clicks on translated/scaled coordinates

Hi,
I am trying to register mouse events on objects and using a simple test such as

  
void someObject::mousePressed(ofMouseEventArgs & args){  
        ofVec2f mousePos = ofVec2f(args.x, args.y);  
		if ( boundary.inside(mousePos) )  
			ofNotifyEvent(pressedInside,  *this);   
}  

with boundary being the rectangle of interest: for example, if this is an image, the boundary is the boundary of the image. This, unfortunately, fails if the coordinates get translated/scaled, because the mouse position is still reported in the untranslated units.
What would be the best way to handle the situation? The ugly solution is to have the object be aware of any change in the matrices by manually notifying it of those changes. I would imagine there could be a more elegant solution.
By the way, basically the same question was asked before with no replies here:
http://forum.openframeworks.cc/t/how-to-detect-if-mouse-is-within-bounds-of-translated-rectangle/9641/0
and here:
http://forum.openframeworks.cc/t/best-practice-for-touchmouse-pressed-on-translated-object/11187/0
Thanks a lot in advance!

i did this a long time ago.

i found this code snippet. i hope it’s enough to get you going

  
  
void ofButton::rotate(float _angle){  
	pushPop_angle =_angle;  
}  
void ofButton::translate(float _x, float _y){  
	pushPop_x =_x;  
	pushPop_y =_y;  
}  
  
bool ofButton::overRect(int x, int y, int width, int height)   
{  
  
	int tx = mouseX - pushPop_x;  
	int ty = mouseY - pushPop_y;  
	ofxVec2f coords(tx,ty);  
	coords.rotate(-pushPop_angle);  
	return ((coords.x > x) && (coords.x < x + width) && (coords.y > y) && (coords.y < y + height));  
}  
  

stephanschulz , thanks a lot.
So, looking at your code, I understand that it is basically keeping track of rotations and translations inside your object (the “manual notify” option I mentioned in my original post)? That is, every time you, say, execute ofTranslate(…) you manually make sure the objects would be aware of it by updating some member variables of these objects, correct?

Hi,
Or the ofxMSAInteractiveObject addon can help.

Thanks! I guess this is because it extends ofRectangle, which should be aware of all the coordinate transforms, correct?

Hm, even this seems harder than I thought. I can keep a stack of coordinate transformations inside my object, but I can only update it - the stack - when I push/pop matrices and do transforms, which happens in draw(). When I finish draw(), I need to pop those transforms.
But my mouse events are obviously asynchronous so when a mouse click occurs the object already has no idea about what transforms occurred during the draw() stage. Not sure how to get around this elegantly.

Tried it, but unless I am doing something stupid this did not seem to work…

Well, for now the only solution I found is to keep track of coordinate transformations stack and manually let all the transformed object know about these transformations.
To resolve the issue I raised in the quote above, I don’t pop the stack with ofPopMatrix (in draw()), instead I initialize the stack each time anew in testApp.cpp’s draw() - that is, the first instance where a coordinate transformation occurs.
This is very awkward but I cannot see a better solution for now…