Reputation: 4419
I'm working on some GUI elements, some of them are overlapped and can both capture mouse events. Is there are way to make the mouse event captured by the "topmost" element only?
Bla a,b;
void setup(){
size(300,300);
a=new Bla("a",this);
b=new Bla("b",this);
}
void draw(){
background(0xffffffff);
a.draw(10,10);
b.draw(25,25);
}
public class Bla{
String name;
float x,y;
PApplet p;
Bla(String name, PApplet p){
this.name=name;
this.p=p;
p.registerMethod("mouseEvent",this);
}
public void mouseEvent(MouseEvent e){
float mx=e.getX();
float my=e.getY();
if(mx>x && mx<x+50 && my>y && my<y+50){
if(e.getAction()==MouseEvent.RELEASE){
print(name);
}
}
}
public void draw(float x, float y){
this.x=x;
this.y=y;
p.fill(0xffffffff);
p.stroke(0xff000000);
p.rect(x,y,50,50);
}
}
Upvotes: 2
Views: 190
Reputation: 42174
You might want to set up your own MouseListener. You can use Processing's mouseClicked() method. Then loop through your instances of Bla and check whether each is under the mouse at the time. Only call the triggered method of the ones you want to trigger.
Upvotes: 3