Simple button help

Hello all

This seems pretty simple but I cannot wrap my head around creating a simple button. All I want to do is have an image and when I click on it I want to call a function

  
  
testApp.h  
  
#include "MyButton.h"  
  
class testApp : public ofBaseApp{  
  
	public:  
		void setup();  
		void draw();  
  
		void imageClicked();  
  
		MyButton button;  
};  
  

  
  
testApp.cpp  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup()  
{  
	button.load("theimage.jpg");  
}  
  
void testApp::draw()  
{  
	button.draw(0,0);  
}  
  
void testApp::imageClicked()  
{  
	printf("the image was clicked");  
}  
  

  
  
MyButton.h  
class MyButton  
{  
public:  
  
	ofImage theImg;  
  
	void setup(string src)  
	{  
		theImg.load(src);  
	}  
  
	void update();  
	{  
	}  
          
	void draw(int x, int y);      
	{  
		theImg.draw(x,y);  
	}  
  
};  
  

How would I call imageClicked() ?

Does anyone have a simple solution, MSAInteractiveObject/ofEvent?

Thanks

you know the x and y coordinates of you rectangular image. you know height and width.

so you check your mouse x and y and see if the mouse x is between imageX and imageX+imageWidth
and the same with mouseY.

hope that helps.
s.

so something like

  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
	if(x > button.x && x < button.x+button.width && y > button.y && y < button.y+button.height)  
	{  
		imageClicked();  
	}  
}  
  

What if I have 100 buttons. There has to be a better way to do this

There is. Check out the msaInteractiveObject https://github.com/Flightphase/ofxMSAInteractiveObject if you’re familiar with Actionscript or Javascript metaphors it’ll feel very familiar.

== edit

Just noticed you’ve already mentioned that. It’s quite nifty for what you want to do, highly recommended.