GUI Button Loop help

edited March 2014 in How To...

I'm trying to add a GUI button where i'd like the program to loop until another button is pressed. For example i'm playing with the example GUI Button sketch and i'm trying to loop it after a button is pressed until for example another button is pressed to indicate the program to move on to the rest of the program.

Answers

  • class Button{
      int x, y, w, h, v;
      Button(int ix, int iy, int iv){
        x=ix;
        y=iy;
        v=iv;
        w=20;
        h=20;
      }
      boolean mouseOver(){
        return( mouseX >= x && mouseX < x+w && mouseY >= y && mouseY < y+h );
      }
      void draw(){
        stroke(128);
        if(mouseOver()) stroke(255);
        fill(64);
        if(state==v) fill(0);
        rect(x,y,w,h);
      }
      void click(){
        if(mouseOver()) state = v;
      }
    }
    
    Button b0 = new Button(10,10,0);
    Button b1 = new Button(40,10,1);
    Button b2 = new Button(70,10,2);
    
    int state = 0;
    
    void setup(){
      size(220,220);
    }
    
    void draw(){
      background(0,0,255);
      pushMatrix();
      if(state==1){
        background(255,0,0);
        translate(width/2, height/2);
        rotate(map(millis()%5000,0,5000,0,TWO_PI));
        fill(128,0,0);
        stroke(0);
        rect(20,-10,20,20);
      }
      if(state==2){
        background(0,255,0);
        translate(width/2, height/2);
        rotate(-map(millis()%5000,0,5000,0,TWO_PI));
        fill(0,0,128);
        stroke(0);
        rect(20,-10,20,20);
      }
      popMatrix();
      b0.draw();
      b1.draw();
      b2.draw();
    }
    
    void mousePressed(){
      b0.click();
      b1.click();
      b2.click();
    }
    
Sign In or Register to comment.