import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import processing.serial.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class sketch_140610a extends PApplet {



// The serial port:
Serial myPort;

int redRectX, redRectY;      // Position of square button
int greenRectX, greenRectY;      // Position of square button
int blueRectX, blueRectY;      // Position of square button

int rectSize = 200;     // Diameter of rect
int circleSize = 93;   // Diameter of circle
int redColor, greenColor, blueColor, baseColor;
int currentColor;

boolean redRectOver = false;
boolean greenRectOver = false;
boolean blueRectOver = false;

public void setup() {
  size(600, 200);
  redColor = color(255,0,0);
  greenColor = color(0,255,0);
  blueColor = color(0,0,255);
  baseColor = color(255);
  
  redRectX = 0;
  redRectY = 0;
  greenRectX = 200;
  greenRectY = 0;
  blueRectX = 400;
  blueRectY = 0;
  
  // List all the available serial ports:
  println(Serial.list());
  
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, "/dev/tty.usbserial-A100RTDG", 4800);
}

public void draw() {
  update(mouseX, mouseY);
  background(baseColor);
  
  fill(redColor);
  rect(redRectX, redRectY, rectSize, rectSize);
  
  fill(greenColor);
  rect(greenRectX, greenRectY, rectSize, rectSize);
  
  fill(blueColor);
  rect(blueRectX, blueRectY, rectSize, rectSize);
}

public void update(int x, int y) {
  if ( overRect(redRectX, redRectY, rectSize, rectSize) ) {
    redRectOver = true;
    greenRectOver = false;
    blueRectOver = false;
  } else if ( overRect(greenRectX, greenRectY, rectSize, rectSize) ) {
    redRectOver = false;
    greenRectOver = true;
    blueRectOver = false;
  } else if ( overRect(blueRectX, blueRectY, rectSize, rectSize) ) {
    redRectOver = false;
    greenRectOver = false;
    blueRectOver = true;
  } else {
    redRectOver = greenRectOver = blueRectOver = false;
  }
}

public void mousePressed() {
  
  if (redRectOver) {
    //currentColor = circleColor;
    myPort.write("2,900");
  }
  
  if (greenRectOver) {
    myPort.write("4,900");
  }
  
  if (blueRectOver) {
    myPort.write("0,900");
  }
  
}

public 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;
  }
}
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "--full-screen", "--bgcolor=#666666", "--stop-color=#cccccc", "sketch_140610a" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
