OK. Here's my version:
Code:Menu menu;
boolean button = false;
// To store the state of the submenu
boolean submenu = false;
void setup() {
size(300,300);
menu = new Menu();
}
void draw() {
background(180);
menu.display_gui();
menu.display_menu();
}
void mouseClicked() {
menu.mouseClicked();
}
class Menu {
void display_gui() { //upper blue button
fill (140,160,220);
if (mouseX > 10 && mouseX < 110 && mouseY > 10 && mouseY < 30) {
fill(150,190,250);
}
rect(10,10,100,20);
}
void display_menu() {
if (button) {
fill(255);
rect(10,30,100,150); // white menu
fill (140,160,220);
if (mouseX > 10 && mouseX < 110 && mouseY > 100 && mouseY < 120){
fill(150,190,250);
}
rect(10,100,100,20); // lower blue button
// check that the mouse is hovering over the second button
if (mouseX > 10 && mouseX < 110 && mouseY > 100 && mouseY < 120) {
// if it is set the boolean variable to true
submenu = true;
}
// If the boolean is true we can display the submenu
if (submenu) {
fill(255);
rect(110,100,100,150);
// now we just need to check if the mouse strays from either
// the button or the submenu.
if (! (mouseX>=110 && mouseX<210 && mouseY>100 && mouseY<250 || mouseX > 10 && mouseX < 110 && mouseY > 100 && mouseY < 120) ){
// If it does we set the boolean to false and the submenu is hidden
submenu = false;
}
}// end of if(submenu)
}// end of if(button)
}
void mouseClicked() {
if (mouseX > 10 && mouseX < 110 && mouseY > 10 && mouseY < 30) {
button = !button;
}
}
}
That hopefully answers your questions: one extra boolean variable declared and no need for a second mouseClicked
Part of the trick is to make the check for mouseover on the second button (basically just checking that the mouse is within a certain area) separate from the display of the submenu. You can then display the submenu when it is set to true. So that takes care of only displaying the submenu when hovering over the second button, and not when the mouse is over the submenu.
The next step is to hide the menu when you roll the mouse out of the designated area. That's what the condition I posted previously checks. Since you only need to make this check if the submenu is currently visible you can place this condition within the if(submenu) condition.
Hopefully that all makes sense. Of course this setup is a little unforgiving. For instance if you move diagonally from the button to the submenu, you stray from the mouseover area so the submenu disappears before you get there. In an ideal world you'd start a timer at this point and if mouseover hadn't been restored after a certain delay only then would you set the submenu boolean to false... But perhaps that's a project for another time.
I'd also suggest you take some time to go over the
objects tutorial. As it stands your Menu class isn't doing a lot of work and isn't easily re-usable... but I guess that'll be your next step once you get to grips with all this