Custom menu doesn't respond to mouseClick() attempts

edited May 2014 in Android Mode

Hi guys, so that's it. I'm testing it in Android 4.1.1, using Android 2.3.3 (default processing SDK), and I've created a Menu library that creates a vertical menu, and with a method called selectFromMenu(int x, int y), locates wich button I have pressed. Works fine in Java mode, but in Android mode, the draw_area works perfecty, but the menu buttons doesn't work. Dunno if it's a redraw() problem or a mouseClicked() detection problem, and the code look at it by yourself:

int menu_border = 0;
int menu_space = 0;
int center_x = 0, center_y = 0; 
int selected_menu = 0;
DrawArea draw_area = null;
VerticalMenuArea [] menus = new VerticalMenuArea[3];

void setup()
{
 background(255,255,255);
 size(displayWidth, displayHeight);
  draw_area = new DrawArea(); 
  String [] main_menu = {"File","Draw"};
   menus[0] = new VerticalMenuArea(0,2,main_menu,255,255,155);
  String [] file_menu = {"Exit","Save","Load","Delete","Back"};
   menus[1] = new VerticalMenuArea(1,5,file_menu,255,155,255);
  String [] draw_menu = {"Line","Square","Triangle","Circle","Back"}; 
   menus[2] = new VerticalMenuArea(2,5,draw_menu,155,255,255);
}

void draw()
{
 stroke(0);
 menu_border = displayWidth / 10;
  draw_area.receiveFromThread(selected_menu,0,displayHeight,menu_border,displayWidth);
  menus[0].receiveFromThread(selected_menu,0,displayHeight,0,menu_border);
  menus[1].receiveFromThread(selected_menu,0,displayHeight,0,menu_border);
  menus[2].receiveFromThread(selected_menu,0,displayHeight,0,menu_border);
}

void mouseClicked()
{
 if(mouseX < menu_border)
 {
  switch(selected_menu)
  {
   case 0:
    switch(menus[0].selectFromMenu(mouseX,mouseY))
    {
     case 0:
      selected_menu = 1;
     break;
     case 1:
      selected_menu = 2;
     break;
    }
   break;
   case 1:
    switch(menus[1].selectFromMenu(mouseX,mouseY))
    {
     case 0:
      exit();
     break;
     case 1:
      draw_area.saveDrawing("");
      selected_menu = 0;
     break;
     case 2:
      draw_area.loadDrawing("");
      selected_menu = 0;
     break;
     case 3:
      draw_area.changeDrawMode(4);
      selected_menu = 0;
     break;
     case 4:
      selected_menu = 0;
     break;
    }
   break;
   case 2:
    switch(menus[2].selectFromMenu(mouseX,mouseY))
    {
     case 0:
      draw_area.changeDrawMode(0);
      selected_menu = 0;
     break;
     case 1:
      draw_area.changeDrawMode(1);
      selected_menu = 0;
     break;
     case 2:
      draw_area.changeDrawMode(2);
      selected_menu = 0;
     break;
     case 3:
      draw_area.changeDrawMode(3);
      selected_menu = 0;
     break;
     case 4:
      selected_menu = 0;
     break;
    }
   break;
  }
 }
 redraw();
}

class VerticalMenuArea extends Area
{
 int menu_number = 0;
 int button_number = 0;
 String [] button_labels = null;
 int space = 0;
 int space_center_x = 0, space_center_y = 0;
 int [] menu_rgb = new int[3];

 VerticalMenuArea(int menu_number, int button_number, String [] button_labels, int r, int g, int b)
 {
  this.menu_number = menu_number;
  this.button_number = button_number;
  this.button_labels = button_labels;
  this.menu_rgb[0] = r;
  this.menu_rgb[1] = g;
  this.menu_rgb[2] = b;
 }

 void receiveFromThread(int selected_menu,int top, int bottom, int left, int right)
 {
  if(selected_menu == menu_number)
  {
   space = bottom / button_number;
   space_center_x = right / 2;
   space_center_y = space / 2;
    for(int i = 0; i < button_number; i++)
    {
     fill(menu_rgb[0],menu_rgb[1],menu_rgb[2]);
     rect(left,space*i,right,space);
     fill(0);
     text(button_labels[i],space_center_x,space_center_y + space*i);
    }
  }
 }

 int selectFromMenu(int mouse_x, int mouse_y)
 {
  int selected = 0;
   for(int i = 0; i < button_number; i++)
   {
    if(i == 0 && mouse_y < space)
    {
     selected = 0;
    }
    else if(mouse_y > space*i && mouse_y < (space * i) + space)
    {
     selected = i;
    }
   }
  return selected;
 }
}
Sign In or Register to comment.