// Example by Tom Igoe // The serial port: // List all the available serial ports: // I know that the first port in the serial list on my mac // is always my Keyspan adaptor, so I open Serial.list()[0]. // Open whatever port is the one you're using. // Send a capital A out the serial port ////////////////////// import processing.serial.*; Serial myPort; float bx; float by; int bs = 60; boolean bover = false; boolean locked = false; float bdifx = 0.0; float bdify = 0.0; PImage[] images= new PImage[4]; String[] imagesDirection=new String[4]; int ellipseX=500; int ellipseY=200; Boolean[] buttons=new Boolean[4]; int changeX=0; int changeY=0; void setup() { println(Serial.list()); myPort = new Serial(this, Serial.list()[0], 9600); buttons[0]=false; buttons[1]=false; buttons[2]=false; buttons[3]=false; imagesDirection[0]="up"; imagesDirection[1]="right"; imagesDirection[2]="down"; imagesDirection[3]="left"; //myPort = new Serial(this, Serial.list()[0], 9600); //println(Serial.list()); //myPort.write(65); size(700, 400); int i=0; background(255); smooth(); fill(0); rectMode(CORNER); //print(width); rect(350,50,300,300); for (i=0;i<=3;i++){ images[i] = loadImage(imagesDirection[i]+"1.gif"); } // Load the image into the program //noLoop(); // Makes draw() only run once } void draw() { //up button Test if (mouseX > 160 && mouseX < 160+bs && mouseY > 80 && mouseY < 80+bs) { buttons[0]=true; }else{ buttons[0]=false; } //Right button Test if (mouseX > 260 && mouseX < 260+bs && mouseY > 180 && mouseY < 180+bs) { buttons[1]=true; }else{ buttons[1]=false; } //down button Test if (mouseX > 160 && mouseX < 160+bs && mouseY > 280 && mouseY < 280+bs) { buttons[2]=true; }else{ buttons[2]=false; } //Left button Test if (mouseX > 60 && mouseX <60+bs && mouseY > 180 && mouseY < 180+bs) { buttons[3]=true; }else{ buttons[3]=false; } for (int i=0;i<=3;i++){ by=80; bx=160; if (i==1){ bx=bx+100; by=by+100; } if (i==2){ by=by+200; } if (i==3){ bx=bx-100; by=by+100; } image(images[i],bx, by); stroke(255); fill(255); rect(ellipseX+changeX,ellipseY+changeY,4,4); } // // Test if the cursor is over the box // if (mouseX > bx && mouseX < bx+bs && // mouseY > by && mouseY < by+bs) { // bover = true; // if(!locked) { // stroke(255); // fill(153); // } // } else { // stroke(153); // fill(153); // bover = false; // } // Draw the box // rect(bx, by, bs, bs); } void mousePressed() { // if(bover) { // locked = true; // fill(255, 255, 255); // } else { // locked = false; // } // bdifx = mouseX-bx; // bdify = mouseY-by; for (int i=0;i<=3;i++){ if (buttons[i]){ images[i] = loadImage(imagesDirection[i]+"2.gif"); doAction(i); } } } void mouseDragged() { if(locked) { bx = mouseX-bdifx; by = mouseY-bdify; } } void mouseReleased() { for (int i=0;i<=3;i++){ if (buttons[i]){ images[i] = loadImage(imagesDirection[i]+"1.gif"); } } } void doAction(int num){ switch(num) { case 0: changeY=changeY-2; break; case 1: changeX=changeX+2; break; case 2: changeY=changeY+2; break; case 3: changeX=changeX-2; break; default: break; } myPort.write(num); // Serial.write(num); }