Part of a sketch uses a button and a mouse event where the mouse event should only occur within the border of a rectangle. Unfortunately, my code forces the user to click the button twice before taking any action. This is annoying and makes a terrible mess if there are multiple buttons. How come and how can I prevent this.
// libraries
import controlP5.*;
// miscellaneous
int start_time; // Add a holder for the startup time in milliseconds
// font
PFont font;
// make controllers
ControlP5 MyController;
Button but_Quit;
Textlabel tlb_ButtonPressed;
Textlabel tlb_MousePressed;
void settings() {
fullScreen();
}
void setup() {
// draw settings
imageMode(CENTER);
// initialise controls
MyController = new ControlP5(this);
font = createFont("ProcessingSansPro-Regular-48", 25);
MyController.setFont(font);
// make button Quit
but_Quit = MyController.addButton("but_Quit")
.setPosition(width/2 - 60, height/2 - 500)
.setSize(120,120)
;
// make textlabel ButtonPressed
tlb_ButtonPressed = MyController.addLabel("buttonPressed")
.setText("Button was pressed")
.setPosition(width/2 - 100, height/2 - 300)
.setColor(color(#FF0318));
tlb_ButtonPressed.hide();
// make textlabel MousePressed
tlb_MousePressed = MyController.addLabel("mousePressed")
.setText("Mouse was pressed")
.setPosition(width/2 - 60, height/2 + 300)
.setColor(color(#000000));
tlb_MousePressed.hide();
}
void but_Quit() {
if (millis()-start_time<1000) {
return;
}
println("ButtonEvent");
tlb_MousePressed.hide();
tlb_ButtonPressed.setText("Button was pressed at x:" + mouseX +" ,y:" + mouseY);
tlb_ButtonPressed.show();
}
void mouseEvent() {
if ((mouseX >= (width/2 - 250)) && (mouseY >= (height/2 - 250))
&& (mouseX <= (width/2 + 250)) && (mouseY <= (height/2 + 250))) {
// mouseEvent only allowed inside the rectangle area
println("MouseEvent");
tlb_ButtonPressed.hide();
tlb_MousePressed.setText("Mouse was pressed at x:" + mouseX +" ,y:" + mouseY);
tlb_MousePressed.show();
}
}
void draw() {
background(255);
rect(width/2 - 250,height/2 -250,500,500);
mouseEvent();
}