Menu toolbar and custom events

Hi all,

I have been trying to work and play with creating a menu toolbar.
I recently found this post (https://forum.processing.org/two/discussion/21749/how-to-create-an-application-with-a-toolbar) and I have used some parts of Chrisir answer.
So, far I have all the structure working. But now I am trying to get the options to send something (it can be a simple println) when clicked. My doubt is how to achieve this.

  1. I was thinking of some custom Event when one option of one of the menu items is clicked. But I have read that creating custom Events is not trivial as well.
  2. Using the mousePressed() method. But that would mean that I would have to go through all the options to check which one was clicked as well, so I don’t know if this is the best idea.

Any tips or ideas are welcome!
Also, on improving the code as well :smiley:

Thanks in advance!

Main window:

import java.util.*;

LinkedHashMap<String,StringList> menuOptions;
ArrayList<DropdownItem> menuItems;
Menu menu;

void setup(){
  size(400,400);
 
  menuOptions = new LinkedHashMap<String,StringList>();
  menuOptions.put("File",new StringList("New","Open","Save","Exit"));
  menuOptions.put("Edit",new StringList("Copy","Past","Sellect all"));
  menuOptions.put("Sketch",new StringList("Run","Stop","Import","Debug","Comment"));
  
  menu = new Menu();
  menu.addItems(menuOptions);    
}

void draw(){
  background(240);

  menu.display();
}

void mousePressed(){
  
}

Menu class:

class Menu {
  int x, y, sx, sy;
  ArrayList<DropdownItem> menuItems;

  Menu() {
    this.x = 0;
    this.y = 0;
    this.sx = width;
    this.sy = 26;
    this.menuItems = new ArrayList<DropdownItem>();
  }

  void addItem(DropdownItem item) {
    menuItems.add(item);
    item.addOptions();
  }

  void addItems(LinkedHashMap<String, StringList> menuOptions) {
    int i = 0;
    for (Map.Entry<String, StringList> menuItem : menuOptions.entrySet()) {
      DropdownItem newItem = new DropdownItem(menuItem.getKey(),menuItem.getValue(),i*50,0);
      menuItems.add(newItem);
      newItem.addOptions();
      i++;
    }
  }

  void display() {
    stroke(226, 227, 227);              //text left lines
    strokeWeight(1);
    line(x, sy, sx, sy);
    stroke(255);
    line(x, sy+1, sx, sy+1);
    noStroke();
    for (int i = 0; i< menuItems.size(); i++) {
      menuItems.get(i).display();
    }
  }
}

Dropdown Items:

class DropdownItem {
  String itemName;
  StringList options;
  ArrayList<Option> allOptions;
  int x, y, sx, sy;
  color c1, c2, c3;
  boolean lock = false;
  int listSize = 0;
  int len = 0;
  int spacing_X = 6;
  int optionWidth = 0;
  
  DropdownItem(String _name, StringList _options, int _x, int _y) {
    this.itemName = _name;
    this.options = _options;
    this.x = _x;
    this.y = _y;
    this.sx = 50;
    this.sy = 25;
    allOptions = new ArrayList<Option>();
  }

  void addOptions() {
    listSize = options.size(); 
    
    for (int i = 0; i< listSize; i++) {
      allOptions.add(new Option(options.get(i), x + spacing_X, y + sy + 8 + i*(sy+3)));
    }
    optionWidth = allOptions.get(0).getWidth();
    len = 15 + (sy+3)*listSize;
  }

  void display() {
    if (over()) {
      c1=color(60, 127, 177); 
      c2=color(222, 242, 252); 
      c3=color(178, 224, 249);
    } else {
      c1=color(112, 112, 112); 
      c2=color(240, 240, 240); 
      c3=color(214, 214, 214);
    }


    noStroke();                //drop box colors
    fill(c2);
    rectMode(CORNER);
    rect(x, y, sx, sy);
    
    fill(0);
    textAlign(LEFT, CENTER);
    text(itemName, x+10, y+sy/2-2);

    if (lock) {
      
      strokeWeight(1);
      stroke(142);
      line(x+5, y+sy+len+2, x+optionWidth+(2*spacing_X), y+sy+len+2);
      line(x+optionWidth+(2*spacing_X), y+sy+len+2, x+optionWidth+(2*spacing_X), y+sy+5);
      stroke(171);
      line(x+6, y+sy+len+3, x+optionWidth+2*spacing_X+1, y+sy+len+3);
      line(x+optionWidth+(2*spacing_X+1), y+sy+len+3, x+optionWidth+(2*spacing_X+1), y+sy+6);
      stroke(213);
      line(x+7, y+sy+len+4, x+optionWidth+(2*spacing_X)+2, y+sy+len+4);
      line(x+optionWidth+(2*spacing_X+2), y+sy+len+4, x+optionWidth+(2*spacing_X+2), y+sy+7);
      stroke(241);
      line(x+8, y+sy+len+5, x+optionWidth+2*spacing_X+3, y+sy+len+5);
      line(x+optionWidth+2*spacing_X+3, y+sy+len+5,x+optionWidth+ 2*spacing_X+3, y+sy+8);
      
      stroke(151, 151, 151);              //list box
      fill(255);
      rect(x, y+sy+1, optionWidth+spacing_X*2, len);
      stroke(245, 245, 245);              //inner list box
      fill(241, 241, 241);
      rect(x+1, y+sy+2, optionWidth+spacing_X*2-2, len-2);
      stroke(226, 227, 227);              //text left lines
      strokeWeight(1);
      line(x+13, y+sy+5, x+13, y + sy + 8 +  (sy+3)* listSize);
      stroke(255);
      line(x+14, y+sy+5, x+14, y + sy + 8 +  (sy+3)* listSize);
      noStroke();
      for (Option opts : allOptions) {
        opts.display();
      }
    }
  }

  boolean over() {
    if (mouseX >= x && mouseX <= x+sx && mouseY >=y && mouseY <= y+sy) {
      if (mousePressed) lock=true;
      //else lock = false;
      return true;
    } else {
      if (mousePressed) lock=false;
      return false;
    }
  }
}

Options:

class Option {
  String option;
  int x, y,sx,sy;

  Option(String _option, int xpos, int ypos) {
    this.option = _option;
    this.x = xpos;
    this.y = ypos;
    this.sx = 100;
    this.sy = 20;
  }
  
  void display(){
    if(over()){
      stroke(168, 216, 235);
      fill(225, 238, 244);
      rectMode(CORNER);
      rect(x, y, sx, sy, 4);
    }
    fill(0);
    textAlign(LEFT,CENTER);
    text(option, x+18, y + sy/2);
    noStroke();
    noFill();
  }
  
    
  boolean over(){
    if(mouseX >= x && mouseX <= x+sx && mouseY >=y && mouseY <= y+sy) return true;
    else return false;
  }
  
  int getWidth(){
    return sx;
  }
}