Drop Down

// the menu: // drop down version for mouse and // keyboard, returns int as a result // when clicked. // use mouse. // use keyboard: space bar, esc, cursor, return. // the menu DropDownMenu dropDownMenu = new DropDownMenu; // example of usage String textToDisplay = ""; // -- // core functions void setup { size(320, 420); // define menu dropDownMenu.addNewLine("Chair", 0); dropDownMenu.addNewLine("Bed", 1); dropDownMenu.addNewLine("Table", 2); } // func void draw { background(0, 128, 0); // show menu dropDownMenu.display; // example of usage text(textToDisplay, 17, height-17); } // func // // Inputs mouse void mousePressed { int menuResult = -1; // handle the click menuResult = dropDownMenu.clicked; evalMenu(menuResult); } // func void mouseMoved { if (dropDownMenu.menuOpen) { dropDownMenu.selectedLineFromMouse; } }  // func // --- // Inputs keyboard void keyPressed { // get key if (key!=CODED) { // -- if (key==ESC) { if (dropDownMenu.menuOpen) { dropDownMenu.menuOpen=false; } else { textToDisplay=""; }       key=0; // kill esc } // esc else if (key==' ') { // open / close menu dropDownMenu.toggleMenuOpen; } else if (key==RETURN||key==ENTER) { // execute int menuResult = -1; // handle the click menuResult = dropDownMenu.selectedLineExecute; evalMenu(menuResult); } else { //     }    } // not coded // Coded - else { if (dropDownMenu.menuOpen) { if (keyCode==UP) { dropDownMenu.selectedLineUp; } else if (keyCode==DOWN) { dropDownMenu.selectedLineDown; }     } // if menu open } // else (coded) } // func // - // other functions void evalMenu(int menuResult1) { // example of usage switch (menuResult1) { case 0: textToDisplay="Here a Chair"; break; case 1: textToDisplay="Here a Bed"; break; case 2: textToDisplay= "This is a Table"; break; case -1: // do nothing break; default: // do nothing break; } // switch // } // func // ========================================== class DropDownMenu { // represents one menu //   // define the base rectangle with the triangle in it    MenuItem baseMenu; // is it open? boolean menuOpen=false; // stores the y pos for the next entry float positionY; // list of lines ArrayList listOfEntries = new ArrayList; // selectedLine int selectedLine = - 1; // constr DropDownMenu { // define the rectangle with the triangle in it     baseMenu = new MenuItem(""); baseMenu.x = 20; baseMenu.y = 20; baseMenu.w = 30; baseMenu.h = 30; // define our positionY variable positionY = baseMenu.y + baseMenu.h;   } // constr void addNewLine(String textOfTheNewEntry, int index_ ) { // define a new line MenuItem newItem = new MenuItem(textOfTheNewEntry); // set it properties newItem.x = baseMenu.x;     newItem.y = positionY; // our positionY variable is the new y pos newItem.w = 100; newItem.h = 20; newItem.index = index_; // add it to the list listOfEntries.add(newItem); // add its height to our positionY variable positionY += newItem.h;   }  // method void display { // display whole menu // show base rectangle baseMenu.display(false); // if open, show list if (menuOpen) { for (int i=0; i<listOfEntries.size ; i++) { if (i==selectedLine) listOfEntries.get(i).display(true); else listOfEntries.get(i).display(false); } // for } // if     // menu icon I : burger menu icon (the "three line" symbol) stroke(0); strokeWeight(3); int offSetX = 8;  // border left and right int offSetY = 6;  // space between lines (y-direction) float baseY = baseMenu.y + 9; for (int i = 0; i < 3; i++) { line(baseMenu.x+offSetX, baseY+i*offSetY,       (baseMenu.x+baseMenu.h)-offSetX, baseY+i*offSetY); }     strokeWeight(1); /*     // menu icon II : show triangle in base rectangle fill(255); if (menuOpen) { triangle(baseMenu.x+4, baseMenu.y+baseMenu.h-4, baseMenu.x+baseMenu.w-4, baseMenu.y+baseMenu.h-4, baseMenu.x+(baseMenu.w/2), baseMenu.y+4); } else { triangle(baseMenu.x+4, baseMenu.y+4, baseMenu.x+baseMenu.w-4, baseMenu.y+4, baseMenu.x+(baseMenu.w/2), baseMenu.y+baseMenu.h-4); }      */    } // method int clicked { // mouse click if (baseMenu.over) { // toggle toggleMenuOpen; } else if (menuOpen) { for (int i=0; i<listOfEntries.size ; i++) { MenuItem currEntry = listOfEntries.get(i); if (currEntry.over) { // println(currEntry.menuItemText); menuOpen=false; // quit return currEntry.index; } // if       } // for // no entry found menuOpen = false; } // else if     return -1; // undefined } // method void toggleMenuOpen { if (dropDownMenu.menuOpen) { dropDownMenu.menuOpen=false; } else { dropDownMenu.menuOpen=true; dropDownMenu.selectedLine = - 1; }   } // method void selectedLineDown { selectedLine++; if (selectedLine>dropDownMenu.listOfEntries.size-1) selectedLine=0; } // method void selectedLineUp { selectedLine--; if (selectedLine<0) selectedLine=dropDownMenu.listOfEntries.size-1; } // method void selectedLineFromMouse { // sets the selected line from the mouse pos for (int i=0; i<listOfEntries.size ; i++) { MenuItem currEntry = listOfEntries.get(i); if (currEntry.over) { selectedLine=i; // quit return; } // if     } // for } // method int selectedLineExecute { if (menuOpen) { for (int i=0; i<listOfEntries.size ; i++) { MenuItem currEntry = listOfEntries.get(i); if (i==selectedLine) { // println(currEntry.menuItemText); menuOpen=false; // quit return currEntry.index; } // if       } // for // no entry found menuOpen = false; } // else if     return -1; // undefined } // method // } // class // ============================================ class MenuItem { // one line in the menu String menuItemText;  // text float x, y, w, h;     // pos and size int index = -1;       // its index // constr MenuItem(String string_) { menuItemText = string_; } // constr void display(boolean selected) { // show rect // fill(over?196:128); // depending on over fill(selected?196:128); // depending on selected stroke(0); rect(x, y, w, h); // show text fill(255); text(menuItemText, x+5, y+15); } // method boolean over { return(mouseX>x && mouseY>y &&       mouseX<=x+w && mouseY<=y+h ); } // method // } // class //