//Byung Kwan Choi
//Project 8

Balls redByung,greenByung,blueByung;
Button mybutton;
Button[] inc;
Button[] dec;
boolean button = false;
boolean button1 = false;
boolean button2 = false;
boolean button3 = false;
boolean button4 = false;
boolean button5 = false;
int ballX = 265;
int ballX1 = 332;
int ballX2 = 400;
int ballX3 = 475;
int ballX4 = 550;
int ballX5 = 625;
int ballY = 555;
int w = 50;
int h = 35;
int many=10;

void setup(){
  size(800,600);
  inc = new Button[many];
  dec = new Button[many];
  redByung = new Balls(255,0,0);  
  greenByung = new Balls(0, 255, 0 );
  blueByung = new Balls( 0, 0, 200 );
  mybutton = new Button();
  position();
}

void draw(){
  scene();
  moveBalls();
  showBalls();

  redByung.nameRed();
  greenByung.nameGreen();
  blueByung.nameBlue();
  redByung.checkHit(greenByung);
  greenByung.checkHit(blueByung);
  blueByung.checkHit(redByung);
}


void position(){
  redByung.replace();
  greenByung.replace();
  blueByung.replace();
}
void moveBalls(){
  redByung.move();
  greenByung.move();
  blueByung.move();
}
void showBalls(){
  redByung.showBall();
  greenByung.showBall();
  blueByung.showBall();
  mybutton.show();
}
void  scene() {
  // Draw the table
  background( 100,200,250 ); 
  fill( 100,0,200);
  //Table:
  fill( 50,25,155 );          // Light-blue table.
  stroke( 0,50,250 );          // Dark Blue.
  strokeWeight(10);
  rectMode(CORNER);
  rect(50,50, 700,500);        
  
}
void mousePressed() {
  if (mouseX > ballX && mouseX < ballX+w && mouseY > ballY && mouseY < ballY+h) {
    button = !button;
   
     } 
  if (mouseX > ballX1 && mouseX < ballX1+w && mouseY > ballY && mouseY < ballY+h) {
    button1 = !button1;
    
  }
   if (mouseX > ballX2 && mouseX < ballX2+w && mouseY > ballY && mouseY < ballY+h) {
    button2 = !button2;
   
  }
   if (mouseX > ballX3 && mouseX < ballX3+w && mouseY > ballY && mouseY < ballY+h) {
    button3 = !button3;
   
  }
   if (mouseX > ballX4 && mouseX < ballX4+w && mouseY > ballY && mouseY < ballY+h) {
    button4 = !button4;
   
     } 
      if (mouseX > ballX5 && mouseX < ballX5+w && mouseY > ballY && mouseY < ballY+h) {
    button5 = !button5;
   
     } 
}



class Balls{
  float ballX=0,ballY=0,xballX=5,yballY=5;
  float r,g,b;
  color colorful;
  Balls(float rr, float gg, float bb){
    r=rr; g=gg; b=bb;
    colorful = color(rr,gg,bb);
  }
  void move(){
    ballX += xballX;
    ballY += yballY;
    if (ballX > 720 || ballX < 75){ xballX = -xballX; ballX += xballX; 
    }
    if (ballY > 525 || ballY < 75){ yballY = -yballY; ballY += yballY;
    }
  }
  void showBall(){
    fill(colorful);
    ellipse(ballX,ballY,40,40);
  }
  void replace(){
    ballX = 110 + random(100);
    ballY = 110 + random(100);   
    xballX = 1 + random ( 5 );
    yballY = 1 + random ( 5 );
    
  } 
 void nameRed(){
   fill(255,0,0);
   text("RedByung",this.ballX,this.ballY);
 } 
 void nameGreen(){
   fill(0,255,0);
   text("GreenByung",this.ballX,this.ballY);
 } 
 void nameBlue(){
   fill(0,0,255);
   text("BlueByung",this.ballX,this.ballY);
 }
 void checkHit(Balls q){
   Balls xballX;
     if (hit( q )) 
    {
      float tmp=  q.xballX;          // Swap velocities (completely elastic)
      q.xballX=  this.xballX;
      this.xballX=  tmp;
      tmp=  q.yballY;
      q.yballY=  this.yballY;
      this.yballY=  tmp;
      fill(0,255,200);
      ellipse(ballX,ballY, 100,100);
    }
  }
  boolean hit(Balls q){
    return abs( this.ballX - q.ballX ) < 20   &&   abs( this.ballY - q.ballY ) < 20;
  } 

}
class Button {

     
void show() {  
  // inc
  if (button4)  {  }
  fill(230,20,120);
 rect(ballX4,ballY,w,h);
  fill(250,250,250);
  text("UP", ballX4,ballY);
   button4 = false;
   //dec
   if (button5)  {  }
  fill(230,20,120);
 rect(ballX5,ballY,w,h);
  fill(250,250,250);
  text("DOWN", ballX5,ballY);
   button5 = false;
   
  //button red
  if (button)  { redByung.replace( ); }
  fill(255,0,0);
 rect(ballX,ballY,w,h);
  fill(250,250,250);
  text("RED", ballX,ballY);
   button = false;

  //button green 
 if (button1)  { greenByung.replace( ); }
   fill(0,255,0);
 rect(ballX1,ballY,w,h);
  fill(255,250,20);
  text("GREEN", ballX1,ballY);
   button1 = false;
  

  //button blue
 if (button2)  { blueByung.replace( ); }  
   fill(0,0,255);
  rect(ballX2,ballY,w,h);
   fill(25,250,250);
  text("BLUE", ballX2,ballY);
    button2 = false;
  
  //button table
 if (button3)  {
 background( 100,100,250 ); 
  fill( 100,110,20);
  //Table:
  fill( 0,0,255 );           
  stroke( 100,50,150 );         
  strokeWeight(25);
  rectMode(CORNER);
  rect(50,50, 700,500);        

 }  
  fill(0);
  rect(ballX3,ballY,w,h);
   fill(255,25,250);
  text("TABLE", ballX3,ballY);
 // button3 = false;
}
void updown(){
  
}
}

