Made Handles example into Buttons

edited December 2013 in Share Your Work

I made changes in the Handles example to make simple buttons. Not sure if others had already done this but hope it helps some one.

regards, algwat

// Handle example made into buttons
//
Handle[] handles;

String lastbtn = " ";
int num = 8;
//  handles = new Handle[num];
  int hsize = 50;
  int btnblkx = 30;
  int btnblky = 30;
  int btnsz = 30;  
  int btngp = 20;
//
color fillVal = color(126);
//

void setup() {
  size(400,275);
  //
  handles = new Handle[num];
 //
 // for (int i = 0; i < handles.length; i++) {
 // handles[num-1] = new Handle("Name x", x, y, strch, bszx, bszy, handles);
    handles[0] = new Handle("Sbtn 0", 30, 30, 0, 60, 30, handles);
    handles[1] = new Handle("Sbtn 1", 30, 65, 0, 60, 30, handles);
    handles[2] = new Handle("Sbtn 2", 30, 100, 0, 60, 30, handles);
    handles[3] = new Handle("Sbtn 3", 95, 30, 0, 60, 30, handles);
    handles[4] = new Handle("Sbtn 4", 95, 65, 0, 60, 30, handles);
    handles[5] = new Handle("Big btn 1", 30, 135, 0, 240, 30, handles);
    handles[6] = new Handle("Big btn 2", 30, 170, 0, 240, 30, handles);
    handles[7] = new Handle("Big Btn 3", 30, 205, 0, 240, 30, handles);
  //}

  }

void draw() {
 background(0);
 //
 dobuttons();
 stroke(255);fill(255);
 text( "Last selection: "+lastbtn, 200, 30 );
//
}

void dobuttons() {
   for (int i = 0; i < handles.length; i++) {
    handles[i].update();
    handles[i].display();
    stroke(255);fill(255);
  }
}

void mouseReleased()  {
  for (int i = 0; i < handles.length; i++) {
    handles[i].releaseEvent();
   // handles[i].update();
   // handles[i].display();
   stroke(255);fill(255);
  }
 //
}

void keyPressed() {
  //
  if (key =='x') { 
    exit();
  } 
  if (key =='c') { 
    background(0);
  }
 //
}

class Handle {

  String bname;
  int x, y;
  int boxx, boxy;
  int stretch;
  int sizex;
  int sizey;
  boolean over;
  boolean press;
  boolean locked = false;
  boolean otherslocked = false;
  Handle[] others;

  Handle(String bnm , int ix, int iy, int il, int isx, int isy, Handle[] o) {
    bname = bnm;
    x = ix;
    y = iy;
    stretch = il;
    sizex = isx;
    sizey = isy;
   // boxx = sizex;
   // boxy = sizey;
    others = o;
  }

  void update() {
   // boxx = x+stretch;
   // boxy = y - size/2;

    for (int i=0; i<others.length; i++) {
      if (others[i].locked == true) {
        otherslocked = true;
        break;
      } else {
        otherslocked = false;
      }  
    }

    if (otherslocked == false) {
      overEvent();
      pressEvent();
    }

    //if (press) {
     // stretch = lock(mouseX-width/2-size/2, 0, width/2-size-1);
    //}
  }

  void overEvent() {
    if (overRect(x, y, (sizex)-4, (sizey)-4)) {
      over = true;
      text( " over : "+bname, 200, 50 );
      //lastbtn = bname ;
    } else {
      over = false;
    }
  }

  void pressEvent() {
    if (over && mousePressed || locked) {
      press = true;
      locked = true;
      // text( bname, 1024-60, 460 );
     lastbtn = bname ;
    } else {
      press = false;
    }
  }

  void releaseEvent() {
    locked = false;
   // text( bname, 1024-60, 450 );
   // lastbtn = bname ;
  }

  void display() {
    //line(x, y, x+stretch, y);
    fill(255);
    stroke(0);
    rect(x, y, sizex, sizey);
    stroke(128);
    fill(16);
    text(bname, x+10, y+20);
    //
    if (over || press || ( lastbtn==bname ) ) {
      stroke(32);
       fill(128);
       rect(x+2, y+2, (sizex)-4, (sizey)-4);
       stroke(128);
       fill(255);
       text(bname, x+10, y+20);
      //line(boxx, boxy, boxx+size, boxy+size);
      //line(boxx, boxy+size, boxx+size, boxy);
    }

  }
}

boolean overRect(int x, int y, int width, int height) {
  if (mouseX >= x && mouseX <= x+width && 
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

int lock(int val, int minv, int maxv) { 
  return  min(max(val, minv), maxv); 
} 
Tagged:
Sign In or Register to comment.