We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Getting name of instance clicked
Page Index Toggle Pages: 1
Getting name of instance clicked (Read 683 times)
Getting name of instance clicked
Sep 7th, 2009, 8:43am
 
Greetings all!

I'm working on a project to control a video projector. I just started learning Processing last night and I'm a little stuck.  I used the code I found in Learning >> Topoics >> GUI >> Image Button. I have three instances of the button class.

How can I figure out WHICH button object is being pressed?

Thanks very much for your help!
Re: Getting name of instance clicked
Reply #1 - Sep 7th, 2009, 9:56am
 
The mouseReleased callback must call a similar function in the button class for each instance.
The class will check if the mouseX/mouseY is within its surface (check against position and dimensions). If not, the event is ignored, otherwise, it is handled.
Re: Getting name of instance clicked
Reply #2 - Sep 7th, 2009, 12:23pm
 
Thanks for the reply but I'm new enough that didn't make much sense.

An example perhaps?
Re: Getting name of instance clicked
Reply #3 - Sep 7th, 2009, 1:53pm
 
clickableObject[] clickables = new clickableObject[10]; // for example
boolean hitFound;

class clickable{
 float x1,y1,x2,y2;
 clickable(float _x1, float _y1, float _x2, float _y2){
   x1 = _x1;  y1 = _y1;  x2 = _x2;  y2 = _y2:
 }
 void mouseCheck(){
    if ((mouseX>=x1) && (mouseX<=x2) &&
       (mouseY>=y1) && (mouseY<=y2)){
     // handle your event, turn the button blue, whatever
     hitFound = true; // so the click won't go "through" to anything else
   }
 }
}

void mousePressed(){
 hitFound=false;
 for (int i=0; i< clickables.length; i++){
  if (!hitFound) clickables[i].mouseCheck();
 }
}

--Ben
Page Index Toggle Pages: 1