package com.haxademic.core.image;

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

import com.haxademic.core.app.P;
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 + "hires.png" );
		p.noSmooth();
	}

	public static PImage getScreenAsPImage( PApplet p ) {
//		PImage screenshot = null;
		
		PGraphics screenshot = p.createGraphics(p.width, p.height, P.P3D );
		p.beginRecord(screenshot);
//		p.smooth();
		p.draw();
		p.endRecord();
//		p.noSmooth();

		
//		try {
//			Robot robot = new Robot();
//			screenshot = new PImage(robot.createScreenCapture(new Rectangle(0,0,p.width,p.height)));
//		} catch (AWTException e) { }
		return screenshot.get();
	}
}




//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);
//}
@@ -24,24 +24,105 @@
 ore.image;%0A%0A
+import java.awt.AWTException;%0Aimport java.awt.Rectangle;%0Aimport java.awt.Robot;%0A%0A
 import proce
@@ -211,41 +211,8 @@
 e;%0A%0A
-import com.haxademic.core.app.P;%0A
 impo
@@ -883,16 +883,45 @@
 putDir +
+ SystemUtil.getTimestamp(p) +
  %22hires.
@@ -924,24 +924,24 @@
 res.png%22 );%0A
-
 %09%09p.noSmooth
@@ -1007,217 +1007,98 @@
 ) %7B%0A
-//%09%09PImage screenshot = null;%0A%09%09%0A%09%09PGraph
+%09%09return p.get();%0A%09%7D%0A%09%0A%09publ
 ic
-s
  s
-creenshot = p.createGraphics(p.width, p.height, P.P3D );%0A%09%09p.beginRecord(s
+tatic PImage getS
 creen
-shot);%0A//%09%09p.smooth();%0A%09%09p.draw();%0A%09%09p.endRecord();%0A//%09%09p.noSmooth();%0A%0A%09%09%0A//
+ShotAsPImage( PApplet p ) %7B%0A
 %09%09try %7B%0A
 //%09%09
@@ -1093,18 +1093,16 @@
 %09%09try %7B%0A
-//
 %09%09%09Robot
@@ -1127,13 +1127,18 @@
 ();%0A
-//
 %09%09%09
+PImage 
 scre
@@ -1222,18 +1222,38 @@
 ght)));%0A
-//
+%09%09%09return screenshot;%0A
 %09%09%7D catc
@@ -1284,32 +1284,20 @@
 %09return 
-screenshot.get()
+null
 ;%0A%09%7D%0A%7D%0A%0A

Update screenshot method