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 & HelpSyntax Questions › mousepressed / switch
Page Index Toggle Pages: 1
mousepressed / switch (Read 449 times)
mousepressed / switch
Feb 19th, 2009, 11:18pm
 
Hi, im trying to create a switch function with a for structure to create a switch matrix. This is where im stuck:

Quote:
color c = 255;
int x;
int y;
int w;
int h;
void setup() {
  size(400,400);
  smooth();
  rectMode(CENTER);
}
 
void draw() {
  background(#1F45AA);
  for(int i = 40; i < 375; i = i+75) {
  for(int j = 40; j < 375; j = j+75) {
  button(i,j,50,50);   //This is the button with the for structure
  }}
}
 
void button(int tempx, int tempy, int tempw, int temph) {  //This is the button function with parameters
  x = tempx;
  y = tempy;
  w = tempw;
  h = temph;
  strokeWeight(5);
  fill(c);
  rect(x,y,w,h);
}
void mousePressed() {
  if(c == 255 && mouseX <= x + w/2 && mouseX >= x - w/2 && mouseY >= y - h/2 && mouseY <= y + h/2) { //This line is supposed to constrain the mouse to fill only one instance of the array ... but it dont work...
    c = 0;
  } else {
    c = 255;
  }
}


When i remove the constrains i can switch all the buttons at once and it work! but when i use them in the mousePressed function none works...

here is the code without constrains:
color c = 255;
int x;
int y;
int w;
int h;
void setup() {
 size(400,400);
 smooth();
 rectMode(CENTER);
}

void draw() {
 background(#1F45AA);
 for(int i = 40; i < 375; i = i+75) {
 for(int j = 40; j < 375; j = j+75) {
 button(i,j,50,50);
 }}
}

void button(int tempx, int tempy, int tempw, int temph) {
 x = tempx;
 y = tempy;
 w = tempw;
 h = temph;
 strokeWeight(5);
 fill(c);
 rect(x,y,w,h);
}
void mousePressed() {
 if(c == 255) {
   c = 75;
 } else {
   c = 255;
 }
}

please help me fix it,
Odin
Re: mousepressed / switch
Reply #1 - Feb 20th, 2009, 8:02am
 
Something like this might be a little better..
Hope it works.. just typed it in the textarea


Button[] b;
void setup()
{
 size(500,500);
 b = new Button[20];
 for(int i = 0; i < b.length; i++){
    b[i] = new Button(0,i*25,75,20);
 }
}
void draw()
{
 background(0);
 for(int i = 0; i < b.length; i++){
    b[i].draw();
 }
}

void mousePressed()
{
 for(int i = 0; i < b.length; i++){
   if(b[i].pressed()) break;
 }
}

class Button
{
 boolean on = false;
 int x, y, w, h;
 Button(int _x, int _y, int _w, int _h)
 {
    x = _x;
    y = _y;
    w = _w;
    h = _h;
 }
 void draw()
 {
   fill(75);
   if(on){
     fill(255);
   }
   rect(x,y,w,h);
 }
 boolean over()
 {
   return mouseX>=x && mouseX<x+w && mouseY>=y && mouseY<y+h;
 }
 boolean pressed()
 {
   if(over()){
     on = !on;
     return true;
   }
   return false;
 }
}
Re: mousepressed / switch
Reply #2 - Feb 20th, 2009, 8:12pm
 
Nice, it work but ill have to make some changes in there.. and i dont understand well what you done with the boolean over and all those things... an other thing is that you are not using rectMode in your code and it makes the thing even more difficult to change.. But, it kinda help me.. but there is no other way to create a toggle function easier than this in a class?????
i tried to create a simple button function and add a void mousePressed with it so i can toggle the value but it wont work.... anyway,

Thanks you ALOT for everything,
Odin
Page Index Toggle Pages: 1