Commit 1dd7a309 authored by Gaurav Arora (gaa721)'s avatar Gaurav Arora (gaa721)
Browse files

study ready

parents
import g4p_controls.*;
import com.sun.jna.platform.win32.*;
import gab.opencv.*;
import processing.video.*;
import java.util.Iterator;
import java.awt.*;
import com.sun.jna.*;
import com.sun.jna.platform.win32.WinDef.*;
import org.jnativehook.*;
import org.jnativehook.keyboard.NativeKeyListener;
import org.jnativehook.mouse.NativeMouseInputListener;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.keyboard.NativeKeyEvent;
import com.sun.jna.platform.win32.WinUser.WINDOWINFO;
private static final int WS_ICONIC = 0x20000000; //for check minimized
import java.util.ArrayList;
import shiffman.box2d.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.joints.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.contacts.*;
import java.io.*;
Box2DProcessing box2d;
boolean question;
int afterAttentionTime;
String participationID;
Capture video;
OpenCV opencv;
Rectangle[] faces;
int strBuffSize;
char[] windowText;
String windowTitle;
PImage before, grayDiff, current, first;
PFont f;
ArrayList<RECT> aboveWindows;
RECT rectOfInterest;
HWND windowOfInterest;
long lastKeyInput;
long lastMouseInput;
long lastFace;
long lastMotion;
boolean wasMinimized;
boolean foreground;
boolean motionDetected;
boolean obscured;
boolean attention;
int k;
boolean moving;
boolean saveornot=true;
boolean attentHere;
//int mover=10;
Person p;
boolean mp;
int i;
int drawdelay;
boolean eof=false;
float rx, ry;
float rx1, ry1, m, c;
FileInputStream fileIn, fileInE;
String currShape;
float cx, cy;
FileOutputStream fileOut, fileOutE;
float cradius;
ObjectOutputStream out, outE;
ObjectInputStream in, inE;
OutputStream os;
int t;
boolean complete;
int Stime, timer;
boolean questionMouse;
float questionBoxX, questionBoxY;
String[] Questions = {
"Select the portion which was drawn after the black screen", "Select The portion which was drawn recently", "Select the portion which had stayed longest after the black Screen", "Select the portion which was drawn after the black screen", "Select The portion which was drawn recently", "Select The portion which was drawn recently", "Select the objects which were moved", "Select The portion which was drawn recently", "Thnaks"
}
;
int QuestionCounter;
int frameCounter;
boolean startMenu ;
void setup()
{
QuestionCounter = 0;
createGUI();
frameCounter = 0;
afterAttentionTime=2;
complete= true;
frameCounter = 0;
startMenu = true;
Stime = millis();
question = false;
timer=0;
saveornot = false;
t=0;
mover=10;
if (saveornot)
{
try {
fileOut = new FileOutputStream("c:/Users/gaa721/Desktop/shapes.ser");
fileOutE = new FileOutputStream("c:/Users/gaa721/Desktop/Events.ser");
}
catch (FileNotFoundException ex) {
// Do something with mistake or ignore
print( ex.toString());
}
try {
out = new ObjectOutputStream(fileOut);
outE = new ObjectOutputStream(fileOutE);
}
catch (IOException e) {
System.err.println(e);
}
}
k=0;
eof=false;
drawdelay=10;
size(displayWidth-100, displayHeight-100);
attentHere=false;
strBuffSize = 512;
windowText = new char[strBuffSize];
windowTitle = "AttentionDrawing";
aboveWindows = new ArrayList<RECT>();
rectOfInterest = new RECT();
windowOfInterest = User32.INSTANCE.GetForegroundWindow();
// prepareExitHandler();
//remove this when webcam is on
/*
video = new Capture(this, 640/2, 480/2);
opencv = new OpenCV(this, 640/2, 480/2);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
video.start();
lastKeyInput = System.nanoTime();
lastMouseInput = System.nanoTime();
*/
rectMode(RADIUS);
moving=false;
background(255, 255, 255);
p = new Person("gaurav", 0, 0, 255);
i=1;
box2d = new Box2DProcessing(this);
box2d.createWorld();
// Turn on collision listening!
box2d.listenForCollisions();
//
rx=random(10, 490);
ry=random(10, 490);
rx1=random(10, 490);
ry1=random(10, 490);
//rx=20;
//ry=20;
//rx1=60;
//ry1=60;
m = (ry1-ry)/(rx1-rx);
c=ry1-(m*rx1);
questionMouse = true;
currShape = "b";
try {
fileIn = new FileInputStream("c:/Users/gaa721/Desktop/shapes.ser");
fileInE = new FileInputStream("c:/Users/gaa721/Desktop/Events.ser");
in = new ObjectInputStream(fileIn);
inE = new ObjectInputStream(fileInE);
}
catch(FileNotFoundException o)
{
print( o.toString()) ;
}
catch(IOException i)
{
i.printStackTrace();
return;
}
if (!saveornot)
{
try {
p.e = (Events)inE.readObject();
// print(p.e.e.size());
while (!eof)
{
ShapeData d= (ShapeData) in.readObject();
// print(d.movingIndex + " ");
Shape a = new Shape(d.shape, d.x, d.y, 255, 255, 0, d.radius, attention, d.xs, d.ys, d.movingIndex);
a.seen = true;
p.Eshapes.add(a);
}
}
catch(ClassNotFoundException i) {
}
catch(java.io.EOFException ex1)
{
eof=true;
}
catch(IOException i)
{
i.printStackTrace();
return;
}
}
//print(p.shapes.size());
for (Shape s: p.shapes)
{
//print(s.data.shape);
}
}
void draw()
{
if (!saveornot)
{
if (!startMenu)
{
// attention = true;
timer = millis() - Stime;
// print(timer + " ") ;
if (!question )
{
if (timer<=10000 )
{
attention = true;
drawing();
}
else
{
attention = false;
drawing();
background(0, 0, 0);
if (timer>=15000 )
{
timer = 0;
Stime = millis();
question = true;
drawing();
fill(0, 0, 0);
text(Questions[QuestionCounter], 600, 600);
}
}
}
if (question)
{
if (!questionMouse)
{
if (mousePressed)
{
pushMatrix();
background(255);
p.drawAll();
text(Questions[QuestionCounter], 600, 600);
rectMode(CORNERS);
fill(0, 0, 0, 80);
rect(questionBoxX, questionBoxY, mouseX, mouseY);
popMatrix();
}
}
}
} //drawing();
}
else
drawing();
}
void mousePressed()
{
if ((currShape.equals("c") || currShape.equals("r") ) && !question)
{
if (!mp)
{
cx=mouseX;
cy=mouseY;
}
mp=true;
}
// if(mousePressed && questionMouse)
// {
// questionMouse = false;
// questionBoxX=mouseX;
// questionBoxY=mouseY;
// }
if (question && questionMouse)
{
text("Select the portion which was drawn first after the black screen", 600, 600);
questionBoxX=mouseX;
questionBoxY=mouseY;
ellipse(questionBoxX, questionBoxY, 10, 10);
questionMouse = false;
}
}
void mouseReleased()
{
if (mp && !question)
{
mp=false;
p.e.addEvent('d', p.shapes.size());
p.addShape(currShape, cx, cy, cradius);
}
if (question)
{
//
// rectMode(CORNER);
// fill(0,0,0,80);
// rect(questionBoxX,questionBoxX,mouseX,mouseY);
questionMouse = true;
question = false;
QuestionCounter++;
if (QuestionCounter>=Questions.length)
noLoop();
timer = 0;
Stime = millis();
save("/"+participationID+ "/" + ++frameCounter + ".png");
}
moving=false;
for ( Shape s : p.shapes)
s.data.mving=false;
}
void keyPressed()
{
if (key=='r'||key=='R')
currShape="r";
else if (key=='c'||key=='C')
currShape="c";
else if (key=='m'||key=='M')
currShape="m";
else if (key=='B'||key=='b')
currShape="b";
else if (key=='e'||key=='E')
currShape="e";
if (key=='a')
attention= !attention ;
//else p.changeColor((int)random(0, 255), (int)random(0, 255), (int)random(0, 255));
}
void drawShape(float a, float b, float radius)
{
if (currShape.equals("c"))
ellipse(a, b, radius, radius);
if (currShape.equals("r"))
rect(a, b, radius, radius);
if (currShape.equals("b"))
ellipse(a, b, 10, 10);
}
//public void dispose()
//{
// print("i was called bro");
//}
@Override
public void exit()
{
if (saveornot)
{
try {
outE.writeObject(p.e);
}
catch(IOException i) {
}
for (Shape s:p.shapes)
{
if (saveornot)
{
try {
// print(s.data.xs.size());
out.writeObject(s.data);
}
catch(IOException i) {
}
}
}
try {
outE.close();
out.close();
}
catch(IOException i) {
}
}
super.exit();
}
void drawing()
{
box2d.step();
background(255, 255, 255);
/*
opencv.loadImage(video);
faces = opencv.detect();
opencv.diff(video);
opencv.threshold(70);
grayDiff = opencv.getOutput();
motionDetected = hasWhite(grayDiff);
obscured = isObscured();
long now = System.nanoTime();
wasMinimized = isMinimized(windowOfInterest);
if(User32.INSTANCE.GetForegroundWindow()!=null)
{
foreground = User32.INSTANCE.GetForegroundWindow().equals(windowOfInterest);
}
else
{
foreground = false;
}
attention = isPayingAttention();
*/
pushMatrix();
fill(p.r, p.g, p.b);
drawShape(mouseX, mouseY, 10);
popMatrix();
if (mousePressed &&!question)
{
if (currShape=="b")
{
cx=mouseX;
cy=mouseY;
// print(p.e);
if (saveornot)
p.e.addEvent('d', p.shapes.size());
p.addShape("b", cx, cy, 10);
}
cradius=sqrt( pow( ( mouseY - cy), 2) + pow( ( mouseX - cx), 2)) ;
drawShape(cx, cy, cradius);
p.checktranslation();
p.checkDeletion();
}
ShapeData d;
if (attention)
{
k=1;
}
if (!attention )
{
k++;
k=p.shapes.size();
}
if (!saveornot)
{
if (p.Ecounter<p.e.e.size())
{
Event temp = p.e.e.get(p.Ecounter);
if (temp.s=='d')
{
drawdelay--;
if (drawdelay<=0)
{
drawdelay=(int)random(0, 10);
if (temp.ind<p.Eshapes.size())
p.shapes.add(p.Eshapes.get(temp.ind));
if (!attention)
{
p.nshapes.add(p.Eshapes.get(temp.ind));
p.nshapes.get(p.nshapes.size()-1).seen = attention;
}
p.Ecounter++;
}
}
if (temp.s=='m')
{
Shape s = p.Eshapes.get(temp.ind);
print(s.data.xs.size());
if (!attention && !p.nshapes.contains(s))
{
p.nshapes.add(s);
p.nshapes.get(p.nshapes.size()-1).seen = attention;
}
if (s.t<s.data.xs.size())
{
if (!attention)
{
s.trailingShapes.x.add( s.data.xs.get(s.t));
s.trailingShapes.y.add( s.data.ys.get(s.t));
}
s.body.setTransform(box2d.coordPixelsToWorld(new Vec2(s.data.xs.get(s.t), s.data.ys.get(s.t) )), s.body.getAngle());
s.t++;
}
else
{
p.Ecounter++;
}
}
if (temp.s == 'e')
{
Shape s = p.Eshapes.get(temp.ind);
// if(!attention && !p.nshapes.contains(s))
// {
// p.nshapes.add(s);
// p.nshapes.get(p.nshapes.size()-1).seen = attention;
// }
if (!attention)
{
p.nshapes.add(s);
p.nshapes.get(p.nshapes.size()-1).seen = attention;
p.deletedShapes.add( p.Eshapes.get(temp.ind));
}
p.Eshapes.get(temp.ind).deleted = true;
p.Ecounter++;
}
}
}
p.drawAll();
}
boolean isPayingAttention()
{
long now = System.nanoTime();
if (windowingScore()>50&&(attentionScore()>50||now-lastKeyInput<1000000000||now-lastMouseInput<1000000000))
{
return true;
}
else
{
return false;
}
}
int windowingScore()
{
if (wasMinimized)
{
return 0;
}
if (foreground)
{
return 100;
}
if (!obscured)
{
return 80;
}
else
{
return 20;
}