FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   mousePressed() and objects
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: mousePressed() and objects  (Read 397 times)
DaleH


mousePressed() and objects
« on: May 26th, 2004, 7:05pm »

I created a sketch based on the array_objects example. In my case, I simply have 10x10 rectangles moving around the window. I would like to be able to click on one rectangle and cycle its fill color. I tried to use if (mousePressed == true)in the draw method but it seemed too sensitive. And adding mousePressed() doesn't seem to do anything. Any suggestions?
 
amoeba

WWW
Re: mousePressed() and objects
« Reply #1 on: May 26th, 2004, 7:25pm »

Try using something like this:
 
Code:
boolean isOver(float x1,float y1,float x2,float y2) {
  if(mouseX>=x1 && mouseX<=x2 && mouseY>=y1 && mouseY<=430) return true;
  else return false;
}
 
void mousePressed(){
  if(isOver(100,100, 200,200)) changeColor(1);
  else if(isOver(250,250,300,300)) changeColor(2);
}

mousePressed only tells you that the mouse button is currently pressed. You can use the mousePressed() event handler I sketched above, and fill in with isOver() checks for the locations of your rectangles.
 

marius watz // amoeba
http://processing.unlekker.net/
DaleH


Re: mousePressed() and objects
« Reply #2 on: May 26th, 2004, 8:12pm »

Seems like that might work OK for 2 rectangles but get cumbersome as the number of rectangles increases. I tried creating a global boolean click that I toggled with the pressed and released functions like so...
 
Code:
void mousePressed()  
{  
  click = true;  
}  
 
void mouseReleased()  
{  
  click = false;  
}  
 
Since the rectangle objects know when the mouse is over them, using mouseX and mouseY in their move method, I can check if click is true or not. Unfortunately it doesn't seem to work any better than (mousePressed == true) did.
 
ess

Email
Re: mousePressed() and objects
« Reply #3 on: May 26th, 2004, 9:02pm »

is your sketch written in an Object Oriented manner?
 
it might help if you post your code, then we can take a look.
 
-ess
 
ps- don't forget to use the [ code ] and [ /code ] tags (but without the extra spaces):
 
Code:
like this
 
kevinP

Email
Re: mousePressed() and objects
« Reply #4 on: May 26th, 2004, 9:20pm »

on May 26th, 2004, 8:12pm, DaleH wrote:
Seems like that might work OK for 2 rectangles but get cumbersome as the number of rectangles increases.

 
Then why not...
Code:

void mousePressed(){  
 // loop through array of rectangle objects
 for(int i=0; i<arrayOfRect.length; i++) {
  if(arrayOfRect[i].isOver() {
   arrayOfRect[i].changeColor();
  }
 }
}

« Last Edit: May 26th, 2004, 9:21pm by kevinP »  

Kevin Pfeiffer
DaleH


Re: mousePressed() and objects
« Reply #5 on: May 27th, 2004, 1:34am »

Here's what my code looks like now.
 
Code:

 
MRect[] packet;
 
float yspeed = 0.5;
int bover = 1;
 
boolean click = false;
 
 
void setup()
{
  size(200,200);
  fill(255);
  stroke(255);
 
  packet = new MRect[5];
  for (int i=0; i<packet.length; i++) {
    packet[i] = new MRect(5+i*15, 0, 10, 10, 1);
  }
 
}
 
void loop()
{
  background(0);
  fill(255);
 
 
  for(int i=0; i < packet.length; i++) {
 packet[i].draw();
 packet[i].moveY();
  }
 
}
 
class MRect
{
  float xpos; // rect xposition
  float H; // rect height
  float ypos ; // rect yposition
  float W; // rect width
  int ydirection;
  boolean toggle = true;
 
 
  MRect(float xp, float yp, float w, float h, int yd) {
    xpos = xp;
    H = h;
    ypos = yp;
    W = w;
    ydirection = yd;
  }
 
  void moveY () {
    if (ypos > height-H) {
 ydirection *= -1;
    }
    if (ypos < 0) {
 ydirection *= -1;
    }
 
    if (mouseX >= xpos && mouseX <= xpos+W &&
    mouseY >= ypos && mouseY <= ypos+H) {
 bover = 0;
 if (click) {
   toggle = !toggle;
 }
    } else {
 bover = 1;
    }
    ypos = ypos + (yspeed * ydirection * bover);
}
 
  void draw() {
    if (toggle){
 fill(#FF0000);
    } else {
 fill(#00FF00);
    }
    rect(xpos, ypos, W, H);
 
  }
}
 
void mousePressed()
{
  click = true;
}
 
void mouseReleased()
{
  click = false;
}
 
 

 
I will try the for loop to see if it works better. Also, how could I get all the rectangles to freeze when I roll over any one of them?
 
kevinP

Email
Re: mousePressed() and objects
« Reply #6 on: May 27th, 2004, 6:55pm »

By cycle you mean toggle to the second color?
 
You were clearing the click var only when the mouse was released, so as long as the mouse is down, it was being reset (or was that desired, but only slower?).
 
Code:

//...
  if (click) {  
    toggle = !toggle;  
    click = false;      // <---- added
  }  
 
//...
 
 //void mouseReleased()  
 //{  
 //  click = false;  
 //}  

 
Regarding question 2, no time now, but later...
 
-K
« Last Edit: May 27th, 2004, 8:05pm by kevinP »  

Kevin Pfeiffer
kevinP

Email
Re: mousePressed() and objects
« Reply #7 on: May 28th, 2004, 12:35am »

on May 27th, 2004, 1:34am, DaleH wrote:

Also, how could I get all the rectangles to freeze when I roll over any one of them

 
I think you would have to first loop through all rects to determine if any are under the cursor and then do your movement (if allowed) and redraw...
Code:

  stopped = false;
   
  // check all rects to see if we are allowed to move
  for(int i=0; i < packet.length; i++) {
    if(packet[i].isOver() == true) { //
 stopped = true;
    }
  }
   
  // now move and redraw
  for(int i=0; i<packet.length; i++) {
    if(stopped != true) {
 packet[i].moveY();
    }
    packet[i].draw();
  }

 
I also broke some things out into separate and/or smaller methods...
Code:

  boolean isOver() // curser is over rectangle
  {
    if (mouseX >= xpos && mouseX <= xpos+W && mouseY >= ypos && mouseY <= ypos+H) {
 return true;
    } else {
 return false;
    }
  }
 
  void toggleColor() // just toggle between 2 colors
  {
    if (toggle == false){
 clr = RED;
 toggle = true;
    } else {
 clr = GREEN;
 toggle = false;
    }
  }
 
  void draw() {      // just do the redraw
    fill(clr);
    rect(xpos, ypos, W, H);
  }
 
// ...
 
void mousePressed()
{
  for(int i=0; i < packet.length; i++) {
    if(packet[i].isOver()) {
 packet[i].toggleColor();
 break;  // done, no need to go on
    }
  }
}

 
No guarantees; others maybe can give you better tips.
 
-K
 

Kevin Pfeiffer
DaleH


Re: mousePressed() and objects
« Reply #8 on: May 28th, 2004, 6:56am »

Thanks. That works great.
 
kevinP

Email
Re: mousePressed() and objects
« Reply #9 on: May 28th, 2004, 7:41am »

on May 28th, 2004, 12:35am, kevinP wrote:

I think you would have to first loop through all rects to determine if any are under the cursor and then do your movement (if allowed) and redraw...

 
Casey has a better example of this:
http://processing.org/learning/examples/handles.html
 
When he creates a new object he also passes in a reference to the array that holds all his objects (as an object field it gets the name "others"); this gives each object access to the fields of other objects and saves having to loop through your array twice (or so it seems to me).
 
-K
« Last Edit: May 28th, 2004, 7:47am by kevinP »  

Kevin Pfeiffer
Pages: 1 

« Previous topic | Next topic »