package com.haxademic.core.image;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;

import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;

import com.haxademic.core.system.SystemUtil;

public class ScreenUtil {
	
	public static String saveScreenshot( PApplet p, String outputDir ) {
		String filename = outputDir + SystemUtil.getTimestampFine( p ) + ".png";
		p.saveFrame( filename );
		return filename;
	}
	
	public static void screenshotHiRes( PApplet p, int scaleFactor, String p5Renderer, String outputDir ) {
		// from: http://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/
		PGraphics hires = p.createGraphics(p.width*scaleFactor, p.height*scaleFactor, p5Renderer );
		p.beginRecord(hires);
		hires.scale(scaleFactor);
		p.smooth();
		p.draw();
		p.endRecord();
		hires.save( outputDir + SystemUtil.getTimestamp(p) + "hires.png" );
		p.noSmooth();
	}

	public static PImage getScreenAsPImage( PApplet p ) {
		return p.get();
	}
	
	public static PImage getScreenShotAsPImage( PApplet p ) {
		try {
			Robot robot = new Robot();
			PImage screenshot = new PImage(robot.createScreenCapture(new Rectangle(0,0,p.width,p.height)));
			return screenshot;
		} catch (AWTException e) { }
		return null;
	}
}




//if(frameCount == -1) {
//p.camera();
//PGraphics screenshot = createGraphics(width, height, P3D );
//p.beginRecord(screenshot);
//p.camera();
//_modules.get( _curModule ).update();
//p.endRecord();
//PImage newImg = screenshot.get();
//
//println("screeshotting!");
////filter(THRESHOLD);
//
////		PImage newImg = createImage(1000, 1000, RGB);
////PImage newImg = ScreenUtil.getScreenAsPImage( p5 );
////		println(newImg.width+"x"+newImg.height);
//p.noStroke();
////		newImg.save( "output/saved_img/test.png" );
//p.tint(255, 0, 255, 200f);
////		image( screenshot, 20, 20 );
////		image( screenshot, 40, 40 );
////		image( screenshot, 60, 60 );
////translate(0,0,-100);
////rotateX(TWO_PI/10);
////rotateZ(TWO_PI/10);
//
//
////textureMode(IMAGE);
////imageMode(CORNERS);
////resetMatrix();
////beginShape(QUADS);
////texture(newImg);
////vertex(0, 0, 0, 0);
////vertex(width, 0, width, 0);
////vertex(width, height, width, height);
////vertex(0, 0, 0, height);
////endShape();
////
////translate(10,10,0);
////beginShape(QUADS);
////texture(newImg);
////vertex(0, 0, 0, 0);
////vertex(width, 0, width, 0);
////vertex(width, height, width, height);
////vertex(0, 0, 0, height);
////endShape();
////
////translate(10,10,0);
////translate(0,0,-200);
////beginShape(QUADS);
////texture(newImg);
////vertex(0, 0, 0, 0);
////vertex(width, 0, width, 0);
////vertex(width, height, width, height);
////vertex(0, 0, 0, height);
////endShape();
//
//imageMode(CORNER);
////translate(100,100,0);
//image(newImg, 0, 0, width, height);
//}
@@ -78,40 +78,146 @@
 awt.
-Rectangle;%0Aimport java.awt.Robot
+GraphicsDevice;%0Aimport java.awt.GraphicsEnvironment;%0Aimport java.awt.Rectangle;%0Aimport java.awt.Robot;%0Aimport java.awt.image.BufferedImage
 ;%0A%0Ai
@@ -865,24 +865,45 @@
 Renderer );%0A
+%09%09hires.beginDraw();%0A
 %09%09p.beginRec
@@ -985,16 +985,35 @@
 cord();%0A
+%09%09hires.endDraw();%0A
 %09%09hires.
@@ -1060,16 +1060,17 @@
 p(p) + %22
+-
 hires.pn
@@ -1440,16 +1440,591 @@
 ull;%0A%09%7D%0A
+%0A%09public static PImage getScreen(int x, int y, int width, int height) %7B%0A%09%09GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();%0A%09%09GraphicsDevice%5B%5D gs = ge.getScreenDevices();%0A%09%09//DisplayMode mode = gs%5B0%5D.getDisplayMode();%0A%09%09Rectangle bounds = new Rectangle(x, y, width, height);%0A%09%09BufferedImage desktop = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);%0A%0A%09%09try %7B%0A%09%09%09desktop = new Robot(gs%5B0%5D).createScreenCapture(bounds);%0A%09%09%7D%0A%09%09catch(AWTException e) %7B%0A%09%09%09System.err.println(%22Screen capture failed.%22);%0A%09%09%7D%0A%0A%09%09return new PImage(desktop);%0A%09%7D%0A%0A
 %7D%0A%0A%0A%0A%0A//

Add system screen-grabbing methods