#include "arPlaneGuiScene.h"

//---------------------------------------
arPlaneGuiScene::arPlaneGuiScene(){

}

//---------------------------------------
void arPlaneGuiScene::exit(){

}

//---------------------------------------
void arPlaneGuiScene::setupScene(){
	sceneName = "plane gui scene!";
	addAppListener();
	addMouseListener();
	addKeyboardListener();
	setAutoRender(true);
	
	xml.loadFile("planeSettings.xml");
		
	planePts[0].x = xml.getValue("A:x", 10);
	planePts[0].y = xml.getValue("A:y", 10);
	planePts[1].x = xml.getValue("B:x", 100);
	planePts[1].y = xml.getValue("B:y", 10);
	planePts[2].x = xml.getValue("C:x", 100);
	planePts[2].y = xml.getValue("C:y", 100);	
	planePts[3].x = xml.getValue("D:x", 100);
	planePts[3].y = xml.getValue("D:y", 10);	
	int planeMM	  = xml.getValue("planeMM", 300);	
												
	//this is for setting the points with your mouse
	whichPt			= 0;
	recordingPts	= false;
	
}

//---------------------------------------
void arPlaneGuiScene::updatePlane(){
	planePtr->setPlanePoints(planePts[0], planePts[1], planePts[2], planePts[3]);
}

//---------------------------------------
void arPlaneGuiScene::update(){
	if(pause)return;
	
}

//---------------------------------------
void arPlaneGuiScene::render(){
	if( hide ) return;
		
	if(recordingPts){
		ofNoFill();
			ofSetColor(255, 0, 130);
			ofBeginShape();
				for(int i = 0; i < whichPt; i++){
					ofVertex(planePts[i].x, planePts[i].y);
				}
			ofEndShape(false);
			for(int i = 0; i < whichPt; i++){
				ofCircle(planePts[i].x, planePts[i].y, 4);
				ofDrawBitmapString(ofToString((i+1)),planePts[i].x, planePts[i].y);
			}
		ofFill();
	}else{
		ofSetColor(20, 255, 90);
		ofNoFill();
		ofBeginShape();
			for(int i = 0; i < 4; i++){
				ofVertex(planePts[i].x, planePts[i].y);
			}
		ofEndShape(true);
		for(int i = 0; i < 4; i++){
			ofDrawBitmapString(ofToString((i+1)),planePts[i].x, planePts[i].y);
		}
		ofFill();
	}
	if(debug){
		//drawTxtMsg("scene is: "+sceneName+" r to record points - s to save settings", 0, camH-40);
		//drawDebugTxt(0, camH-20);	
	}
		
}

//---------------------------------------		
void arPlaneGuiScene::keyPressed( int key ){
	if(pause)return;

	if(key == 'r'){
		recordingPts = true;
		whichPt = 0;
		debugTxt = "set plane points - click corners clockwise from top left";		
	}

	if( key == 's'){
		xml.setValue("A:x", planePts[0].x);
		xml.setValue("A:y", planePts[0].y);
		xml.setValue("B:x", planePts[1].x);
		xml.setValue("B:y", planePts[1].y);
		xml.setValue("C:x", planePts[2].x);
		xml.setValue("C:y", planePts[2].y);	
		xml.setValue("D:x", planePts[3].x);
		xml.setValue("D:y", planePts[3].y);
		xml.saveFile("planeSettings.xml");
		debugTxt = "plane settings saved";
	}
}

//---------------------------------------
void arPlaneGuiScene::keyReleased( int key ){
	if(pause)return;
	
}
	
//---------------------------------------
void arPlaneGuiScene::mouseMoved(int x, int y ){
	if(pause)return;
	

}

//---------------------------------------
void arPlaneGuiScene::mouseDragged( int x, int y, int button ){
	if(pause)return;

	if( recordingPts ){
		planePts[whichPt].x = x;
		planePts[whichPt].y = y;
	}
}

//---------------------------------------
void arPlaneGuiScene::mousePressed( int x, int y, int button ){
	if(pause)return;

	if( recordingPts ){
		planePts[whichPt].x = x;
		planePts[whichPt].y = y;
	}
}

//---------------------------------------
void arPlaneGuiScene::mouseReleased(int x, int y, int button ){
	if(pause)return;	
	
	if( recordingPts ){
		planePts[whichPt].x = x;
		planePts[whichPt].y = y;
		whichPt++;
		if( whichPt == 4){
			recordingPts = false;
			debugTxt = "points set - hit s key to save";
			updatePlane();
		}
	}
	printf("mouse released is %i %i\n", x, y);
}	

