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 › Button Event Once Until Mouse Released
Page Index Toggle Pages: 1
Button Event Once Until Mouse Released (Read 1972 times)
Button Event Once Until Mouse Released
May 8th, 2010, 3:51pm
 
Hello,

I'm trying to create a button class (Which will then be put in a grid class) based upon the example here http://processing.org/learning/topics/imagebutton.html however on the button press I want an event to be created only once! At the moment whatever is in the pressed() method gets executed every loop of draw.
I've been doing some research and all the examples are either too complicated (using threads or mouseEvent or registerDraw) or not applicable to my usage.
Any help would be much appreciated!
Thanks,
Tim
Re: Button Event Once Until Mouse Released
Reply #1 - May 8th, 2010, 5:05pm
 
Do you have a pressed() method defined outside of the Button class [same scope as draw() or setup()]?

Could you post some runnable code for us to look at?
It would make it easier to track down.
Re: Button Event Once Until Mouse Released
Reply #2 - May 8th, 2010, 5:16pm
 
In the example you link to, the Button class's pressed() method:
Code:

void pressed() {
if(over && mousePressed) {
pressed = true;
} else {
pressed = false;
}
}

could be changed to something like:
Code:

void pressed() {
if(over && mousePressed) {
if (pressed==false) {
pressed = true;
doThingYouOnlyWantDoneOnce();
}
} else {
pressed = false;
}
}

Then the method doThingYouOnlyWantDoneOnce() should only be executed once per mouse click.
Re: Button Event Once Until Mouse Released
Reply #3 - May 9th, 2010, 5:22am
 
Thanks Smitty,
That did it! I was trying overly complicated examples that I could have tried out had I more time but this does what I want it to do and with little complication.
This is a small part of a larger project I'm working on and if anyone's interested I'll be giving away my final code (I'm doing a final year BA at uni) for anyone that wants it.
Thanks very much,

Tim
Re: Button Event Once Until Mouse Released
Reply #4 - May 9th, 2010, 11:20am
 
I have a new problem relating to the other problem so I thought I'd ask here instead of creating a new thread.
I'm trying to create a grid of buttons for people to press. The buttons are similar to the imagebuttons that I posted above.
I'm having trouble populating the grid as the number of buttons may not be a square number and so not completely populate the grid I'm getting array out of bounds problems when I try and populate a cell with a button that doesn't exist.
Here's some code to maybe clarify:

Quote:
class Grid{

  //names of the individual cells to load as images
  String[] cellNames;
  //the elements themselves
  ImageButtons[][] grid;
  //PImage array to hold all the cell images
  PImage[] bImage;
  //Grid element holder.
  //  Object[][] grid;

  //calculate number of sides needed to create grid
  int sides;
  //Height of the individual cells.
  int cellHeight;
  int cellWidth;
  //The overall height and width of the grid.
  int gridHeight;
  int gridWidth;
  //Amount of columns and rows.
  int cols;
  int rows;

  Grid(String[] cellNames){

    gridHeight=100;
    gridWidth=100;
    //    this.cols=cols-1;
    //    this.rows=rows-1;

    //this.cellNames=cellNames;

    sides=int(ceil(sqrt(cellNames.length)));

    cols=sides;
    rows=sides;
    println(cols);
    println(rows);

    //this.grid=new Object[sides][sides];

    grid=new ImageButtons[cols][rows];
    for(int i=0; i<cols; i++){
      for(int j=0; j<rows; j++){
        if(cellNames[i]!=null){
          PImage tmp;
          tmp=loadImage(cellNames[i+j]);
          println(cellNames[i+j]);
          grid[i][j]=new ImageButtons(i*cellWidth, j*cellHeight, cellWidth, cellHeight, cellNames[i], tmp);
          println("success");
        }
      }
    }
  }

  void display(){
    for(int i=0; i<cols; i++){
      for(int j=0; j<rows; j++){
        if(grid[i][j]!=null){
          grid[i][j].update();
          grid[i][j].display();
        }
      }
    }
  }
}


Lots of erroneous code and only the grid class. If the whole code is needed/wanted let me know and I'll post here.

Thanks for anybody's help,
Tim
Re: Button Event Once Until Mouse Released
Reply #5 - May 9th, 2010, 4:35pm
 
Okay, let me make sure I understand:

You're trying to create a set of buttons corresponding to strings in the array cellNames[].  You want to place these buttons in a square array, but there's no guarantee that the number of strings in cellNames[] is a perfect square.  You're running into errors trying to populate a square grid with a non-square number of strings in cellNames[].

Is that correct?

Assuming I'm not misunderstanding you, I suggest the following changes to your code:
Code:

   grid=new ImageButtons[cols][rows];
   for(int i=0; i<cols; i++){
for(int j=0; j<rows; j++){
 int c = i*rows + j;  // Note 1
 if (c < cellNames.length){  // Note 2
   PImage tmp;
   tmp=loadImage(cellNames[c]);
   println(cellNames[c]);
   grid[i][j]=new ImageButtons(i*cellWidth, j*cellHeight, cellWidth, cellHeight, cellNames[c], tmp);
   println("success");
 }
}
   }


Note 1: Doing this will use each element of cellNames[] exactly once, which I think is what you want.

Note 2: This line checks to see if cellNames[] is big enough to contain an element for this grid box (which I think is what you were trying to do when you were checking for a null value before, but that way doesn't work.)
Page Index Toggle Pages: 1