#include "testApp.h"
#include "stdio.h"


//--------------------------------------------------------------
void testApp::setup(){	 
	capture = false;
	nCurveVertexes = 7;
	
	curveVertices[0].x = 326;
	curveVertices[0].y = 209;
	curveVertices[1].x = 306;
	curveVertices[1].y = 279;
	curveVertices[2].x = 265;
	curveVertices[2].y = 331;
	curveVertices[3].x = 304;
	curveVertices[3].y = 383;
	curveVertices[4].x = 374;
	curveVertices[4].y = 383;
	curveVertices[5].x = 418;
	curveVertices[5].y = 309;
	curveVertices[6].x = 345;
	curveVertices[6].y = 279;
	
	for (int i = 0; i < nCurveVertexes; i++){
		curveVertices[i].bOver 			= false;
		curveVertices[i].bBeingDragged 	= false;
		curveVertices[i].radius = 4;
	}

}

//--------------------------------------------------------------
void testApp::update(){	
	ofBackground(255, 255, 255);
}

//--------------------------------------------------------------
void testApp::draw(){

	//we don't want to capture every frame
	//so we only capture one frame when capture 
	//is set to true
	if(capture){
		output.beginEPS("test.ps");
	}	
	
	
	//lets draw the same content twice - once filled in
	//then once as outlines
	for(int i = 0; i < 2; i++){
		if(i == 0){
			output.fill();
		}else{
			output.noFill();
		}
		
		int shiftY = i * 350;
		
		//draw a triangle
		output.setColor(0xFF5555);
		output.triangle(130, 160 + shiftY, 160, 100 + shiftY, 190, 160 + shiftY);

		output.setColor(0x44FF44);
		
		//we draw this rectangle from the top left corner
		output.disableCenterRect();
		output.rect(300, 100 + shiftY, 60, 60);

		output.setColor(0xFFFF22);
		
		//we draw this rectangle from the center
		output.enableCenterRect();
		output.rect(330, 130 + shiftY, 20, 20);

		//circle
		output.setColor(0x4444FF);
		output.circle(500, 130 + shiftY, 35);
		
		//ellipse
		output.setColor(240, 50, 60);
		output.ellipse(650, 130 + shiftY, 40, 20);
		
		//a bezier curve - we define the start, the first control point, the second control point and the end
		output.setColor(0xFF6633);
		output.bezier(750, 160 + shiftY, 800, 80 + shiftY, 810, 120 + shiftY, 900, 160 + shiftY);
		
		//lifted straight from the polygon example:
		float x0 = 100;
		float y0 = 300 + shiftY;
		float x1 = 150+50*cos(ofGetElapsedTimef()*1.0f);
		float y1 = 300+100*sin(ofGetElapsedTimef()/3.5f) + shiftY;
		float x2 = 200+30*cos(ofGetElapsedTimef()*2.0f);
		float y2 = 300+100*sin(ofGetElapsedTimef()) + shiftY;
		float x3 = 250;
		float y3 = 300 + shiftY;

		output.setColor(0xFF9933);
		output.beginShape();
		output.polyVertex(x0,y0);
		output.bezierVertex(x1,y1,x2,y2,x3,y3);
		output.endShape();
		
		output.setColor(0x2bdbe6);
		output.beginShape();
		for (int i = 0; i < nCurveVertexes; i++){
			
			
			// sorry about all the if/states here, but to do catmull rom curves
			// we need to duplicate the start and end points so the curve acutally 
			// goes through them.
			
			// for i == 0, we just call the vertex twice
			// for i == nCurveVertexes-1 (last point) we call vertex 0 twice
			// otherwise just normal ofCurveVertex call
			
			if (i == 0){
				output.curveVertex(curveVertices[0].x, curveVertices[0].y); // we need to duplicate 0 for the curve to start at point 0
				output.curveVertex(curveVertices[0].x, curveVertices[0].y); // we need to duplicate 0 for the curve to start at point 0
			} else if (i == nCurveVertexes-1){
				output.curveVertex(curveVertices[i].x, curveVertices[i].y);
				output.curveVertex(curveVertices[0].x, curveVertices[0].y);	// to draw a curve from pt 6 to pt 0
				output.curveVertex(curveVertices[0].x, curveVertices[0].y);	// we duplicate the first point twice
			} else {
				output.curveVertex(curveVertices[i].x, curveVertices[i].y);
			}
		}
		output.endShape();
		
		output.setColor(0xCCCCCC);
		for (int i = 0; i < nCurveVertexes; i++){
			if (curveVertices[i].bOver == true) output.fill();
			else output.noFill();
			output.circle(curveVertices[i].x, curveVertices[i].y,4);
		}

		
	}
			
	//once we have done all our drawing
	//we end the ouput which saves the file
	//and then we stop capturing
	if(capture){
		output.endEPS();
		capture =false;
	}

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){ 
	if(key == ' '){
		capture = true;
	}
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){ 

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
for (int i = 0; i < nCurveVertexes; i++){
		float diffx = x - curveVertices[i].x;
		float diffy = y - curveVertices[i].y;
		float dist = sqrt(diffx*diffx + diffy*diffy);
		if (dist < curveVertices[i].radius){
			curveVertices[i].bOver = true;
		} else {
			curveVertices[i].bOver = false;
		}	
	}
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	for (int i = 0; i < nCurveVertexes; i++){
		if (curveVertices[i].bBeingDragged == true){
			curveVertices[i].x = x;
			curveVertices[i].y = y;
		}
	}

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	//output.disableDraw();
	for (int i = 0; i < nCurveVertexes; i++){
		float diffx = x - curveVertices[i].x;
		float diffy = y - curveVertices[i].y;
		float dist = sqrt(diffx*diffx + diffy*diffy);
		if (dist < curveVertices[i].radius){
			curveVertices[i].bBeingDragged = true;
		} else {
			curveVertices[i].bBeingDragged = false;
		}	
	}
}

//--------------------------------------------------------------
void testApp::mouseReleased(){
	//output.enableDraw();
	for (int i = 0; i < nCurveVertexes; i++){
		curveVertices[i].bBeingDragged = false;	
	}
}
