import processing.video.*; import java.io.*; import java.net.*; import java.util.*; PFont f; Movie myMovie; int spotX, spotY, spotW, spotH; float spotT; int i = 0; int bustedCount = 0; SocketCommunicator mySocket; int xpos = 0; int ypos = 0; String spot[] = loadStrings("/positions.txt"); String hotSpot = join(spot,""); boolean showHotSpot = false; boolean showEye = true; boolean catchUp = true; boolean mouseMode = false; void setup(){ frameRate(30); f = loadFont("HelveticaNeue-Light-48.vlw"); textFont(f); //mySocket = new SocketCommunicator("127.0.0.1",9001); mySocket = new SocketCommunicator("localhost",9001); size(1426,591); myMovie = new Movie(this, "007clip.mov"); //myMovie.speed(0.2); myMovie.play(); myMovie.jump(0); } void draw(){ if (myMovie.time() < myMovie.duration()){ image(myMovie,0,0); if (mouseMode){ ypos = mouseY; xpos = mouseX; } while (catchUp){ spotT = Float.parseFloat(spot[i].substring(spot[i].indexOf("T")+2,spot[i].indexOf(" X"))); if (myMovie.time() - spotT < 0.1){ spotX = Integer.parseInt(spot[i].substring(spot[i].indexOf("X")+2,spot[i].indexOf(" Y"))); spotY = Integer.parseInt(spot[i].substring(spot[i].indexOf("Y")+2,spot[i].indexOf(" W"))); spotW = Integer.parseInt(spot[i].substring(spot[i].indexOf("W")+2,spot[i].indexOf(" H"))); spotH = Integer.parseInt(spot[i].substring(spot[i].indexOf("H")+2,spot[i].indexOf("H")+5)); catchUp = false; } else { i++; } //println(spotT + " " + myMovie.time()); } if (i >= spot.length) i = 0; fill(255,0,0,255); textSize(100); if (xpos >= spotX - spotW/2 && xpos <= spotX + spotW/2 && ypos <= spotY + spotH/2 && ypos >= spotY - spotH/2){ text("BUSTED!!", xpos - 200, ypos); bustedCount++; } fill(255,255,255, 50); stroke(255,0,0); if(showHotSpot){ fill(255,255,255,50); rectMode(CENTER); rect(spotX, spotY, spotW, spotH); } if (showEye){ ellipse(xpos,ypos,25,25); } catchUp = true; } else { background(0); text("Your BUSTED index is " + bustedCount + "!!", 100, height/2); } } void gotMessage(String _msg){ //println("Input: " + _msg); String[] parts = _msg.split(","); if (_msg.startsWith("X")){ //this means you are in calibration mode println("Calibrating"); //display the target xpos = Integer.parseInt(parts[1]); ypos = Integer.parseInt(parts[2]); //println("x: " + xpos + "y:" + ypos); } else{ //regular mode just display where the person is looking if (parts.length >3){ xpos = Integer.parseInt(parts[0]); ypos = Integer.parseInt(parts[1]); int diffx = Integer.parseInt(parts[2]); int diffy = Integer.parseInt(parts[3]); } } } void keyPressed() { if(key== 'x') { //asks to go into calibration mode mySocket.sendMessage("X," + width + "," + height); } else if (key == 'h'){ if (showHotSpot) showHotSpot = false; else showHotSpot = true; } else if (key == 'e'){ if (showEye) showEye = false; else showEye = true; } else if (key == 'm'){ mouseMode = true; } } void movieEvent(Movie m) { m.read(); } /////////////////// //////////////////NO NEED TO LOOK BELOW THIS LINE, This is an object for listening (hard part, uses thread) and sending over a socket ///////////////// class SocketCommunicator extends Thread { BufferedReader textLineIn ; PrintStream ps ; Socket socket; boolean listening = true; SocketCommunicator(String _server, int _portnum) { try { socket = new Socket(_server, _portnum); textLineIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); ps = new PrintStream(socket.getOutputStream()); this.start(); ps.println("J"); } catch (IOException e) { System.out.println("Error: " + e); } } void disconnect(){ listening= false; try{ socket.close(); } catch (IOException e) { System.out.println("Trouble closing: " + e); } } public void run () { String msg = null; StringBuffer contentParts[] = null; String contents = null; while (listening) { try { msg = textLineIn.readLine(); if (msg == null) { disconnect(); break; } gotMessage(msg); } catch (IOException e) { disconnect(); System.out.println("Errorpoo: Server Probably Died" + e); } } } void sendMessage(String _s) { if (socket != null) { ps.println(_s ); println("out " + _s); } } }