We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Need help - creating a right click menu
Page Index Toggle Pages: 1
Need help - creating a right click menu (Read 761 times)
Need help - creating a right click menu
Mar 5th, 2007, 2:35pm
 
I'm trying to create a menu that appears whenever I right click, and goes away whenever I left click anywhere that's not on the menu. Making the menu appear on right click was easy, but I'm not sure how to make it disappear besides just redrawing everything (which I don't want to do). So, any ideas on how to make this work? I'm aware there are probably some external GUI libraries that could easily do this, but I'd like to try this myself. Here's my code so far:

RClickMenu menu = new RClickMenu(150, 250, 255);

void setup(){
 size(400, 400);
 background(0);
}

void mousePressed(){
menu.createMenu();
menu.destroyMenu();
}

void mouseReleased(){
}

void draw(){
}

class RClickMenu{
 //Class variables
 int w, h, f;
 //class constructor
 RClickMenu (int Width, int Height, int Fill){
 
   w = Width;
   h = Height;
   f = Fill;
 }
 
  void destroyMenu(){
  if (mouseButton == LEFT){
    fill(0);
    rect(0, 0, width, height);
  }
  }
 
 //creates the menu, goes in void mousePressed()
 void createMenu(){
   if (mouseButton == RIGHT) {
   //menu background
   stroke(f);
   fill(f);
   rect(mouseX, mouseY, w, h);
   
   //menu button
   stroke(f-100);
   fill(f-100);
   rect(mouseX + 2, mouseY + 10, w - 4, 10);
   
   //menu button 2
   stroke(f-100);
   fill(f-100);
   rect(mouseX + 2, mouseY + 25, w - 4, 10);
   
   
 }
 }
}
Re: Need help - creating a right click menu
Reply #1 - Mar 5th, 2007, 4:34pm
 
have the menu have it's own draw method, then add it to draw() like:

void draw() {
 menu.draw();
}

now you can have some variable that keeps the menu's state like:

class RClickMenu {
  int x, y;
  boolean hidden;
  ...
  void draw () {
   if ( !hidden ) {
     // draw your menu here ...
   }
 }

 void createMenu ( int _x, int _y ) // x = mouseX, ...
 {
     x = _x;
     y = _y;
     hidden = false;
 }

 void hideMenu ( ) {
  hidden = true;
  }
}

now inside mouseReleased do:

if (mouseButton == LEFT) {
   hideMenu();
} else if (mouseButton == RIGHT) {
   createMenu( mouseX, mouseY );
}

F
Re: Need help - creating a right click menu
Reply #2 - Mar 5th, 2007, 7:26pm
 
I tried that, and it only makes it so when hidden = true, I can't use right click to make the menu. However, if the menu has already been made, turning hidden to true doesn't make it dissappear. Here's the current code I have:

RClickMenu menu = new RClickMenu(150, 250, 255, false);
float mx = 0;
float my = 0;

void setup(){
 size(400, 400);
 background(0);
}

void mousePressed(){
 if (mouseButton == LEFT){
 menu.hideMenu();
 }
}

void draw(){
  menu.draw();
}

class RClickMenu{
 //Class variables
 int w, h, f;
 boolean hi;
 
 //class constructor
 RClickMenu (int Width, int Height, int Fill, boolean hidden){
 
   w = Width;
   h = Height;
   f = Fill;
   hi = hidden;
 }
 
 void draw () {
   if ( !hi ) {
   if (mouseButton == RIGHT) {
   mx = mouseX;
   my = mouseY;
   
   //menu background
   stroke(f);
   fill(f);
   rect(mx, my, w, h);
   
   //menu button
   stroke(f-100);
   fill(f-100);
   rect(mx + 2, my + 10, w - 4, 10);
   
   //menu button 2
   stroke(f-100);
   fill(f-100);
   rect(mx + 2, my + 25, w - 4, 10);  
   
   hi = true;
 }
 }
   }

  void hideMenu(){
  hi = true;
  }
}
Re: Need help - creating a right click menu
Reply #3 - Mar 5th, 2007, 7:47pm
 
Code:

RClickMenu menu;
float mx = 0;
float my = 0;

void setup () {
size(400, 400);
menu = new RClickMenu(150, 250, 255);
background(0);
}

void mousePressed () {
if (mouseButton == LEFT){
menu.hideMenu();
}
else if (mouseButton == RIGHT) {
menu.showMenu( mouseX, mouseY );
}
}

void draw () {
background(0);
menu.draw();
}

class RClickMenu{
//Class variables
int w, h, f;
int x, y;
boolean hidden = true;

//class constructor
RClickMenu (int Width, int Height, int Fill)
{

w = Width;
h = Height;
f = Fill;
}

void draw () {
if ( !hidden ) {

//menu background
stroke(f);
fill(f);
rect(x,y, w, h);

//menu button
stroke(f-100);
fill(f-100);
rect(x + 2, y + 10, w - 4, 10);

//menu button 2
stroke(f-100);
fill(f-100);
rect(x + 2, y + 25, w - 4, 10);
}
}

void hideMenu(){
hidden = true;
}
void showMenu(int _x, int _y){
x = _x;
y = _y;
hidden = false;
}
}
Re: Need help - creating a right click menu
Reply #4 - Mar 6th, 2007, 2:07am
 
I see what I did wrong then, I didn't put background(o); into void draw. However, when  do that, anything I drew in the setup phase goes away because the background is repeatedly drawn. I'd rather this not happen. I would also rather not put in the things I made in setup into draw because its unnessecary. They don't need to be repeatedly drawn.
Re: Need help - creating a right click menu
Reply #5 - Mar 6th, 2007, 9:08am
 
draw is where the frame get's updated, since your menu is gonna be on top of things there will always be an area you need to update.

to keep your previously drawn things around, why not just create an offscreen width x height PImage, yank the things you've drawn in setup in there and use that together with PImage.copy to update the area below the menu ...

F
Page Index Toggle Pages: 1