#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){	 
	rotSmooth = 0;
	angleTarget = 0;
	
	for (int i = 0; i < 5; i++){
		LM[i] = new videoLineModule();
	}
	
	LM[5] = new graphicsLineModule();
	((graphicsLineModule *)LM[5])->module = new sinWaveModule();	// he is a sine wave...
	((graphicsLineModule *)LM[5])->module->setup();
	
	((videoLineModule *)LM[0])->setMovie("laser001.mov");
	((videoLineModule *)LM[1])->setMovie("laser002.mov");
	((videoLineModule *)LM[2])->setMovie("laser003.mov");
	((videoLineModule *)LM[3])->setMovie("laser004.mov");
	((videoLineModule *)LM[4])->setMovie("hand.mov");
	
	
	for (int i = 0; i < NUM_BOXES; i++){
		//LM[i]->setMovie("laser002.mov");
		LM[i]->setup();
	}
	currentModule = 5;
	prevModule = -1;
	LM[currentModule]->box->position.set(500,500,0);
}

//--------------------------------------------------------------
void testApp::update(){
	
	
	
	ofBackground(0,0,0);	
	
	LM[currentModule]->update();
	
	if (LM[currentModule]->bAmIDone() == true){
		int newModule = (int)(ofRandomuf() * 100.0f) % NUM_BOXES; //(currentModule + 1) % 5;
		LM[newModule]->reset();
		LM[newModule]->box->matchMyStartToPointAngle(LR.getCurrentPoint(), LR.getCurrentAngle());
		LM[currentModule]->reset();
		prevModule = currentModule;
		currentModule = newModule;
		LM[currentModule]->update();
	}
	
	
	if (LM[currentModule]->bHaveNewPoint()){
		LR.addPoint(LM[currentModule]->getNewPoint());
	}
	
	
}


//--------------------------------------------------------------
void testApp::draw(){
	
	//----------------------------------------- 
	// offscreen
	// pbuffer would be better, I think...
	
	if (LM[currentModule]->bNeedToDrawOffscreen()){
		// (a) draw
		LM[currentModule]->drawOffscreen();
		// (b) clear
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	}
	//----------------------------------------- 
	
	
	
	
	ofVec3f pos;
	float rot;
	pos.set(0,0,0);
	
	if (LR.nPts>1){
		pos = LR.pts[LR.nPts-1];
	} //LM[currentModule]->box->position;
	rot = -LM[currentModule]->box->rotation;
	
	if (rot != rotationOfBox){
		float diffAngle = rot - rotationOfBox;
		while(diffAngle < -PI) 	diffAngle += TWO_PI;
		while(diffAngle > PI) 	diffAngle -= TWO_PI;
		angleTarget += diffAngle;
	}
	
	rotationOfBox 	= rot;
	posSmooth 		= 0.98f * posSmooth + 0.02f * pos; 
	rotSmooth 		= 0.95f * rotSmooth + 0.05f * angleTarget ;

	glPushMatrix();
		glTranslatef(1024/2,768/2, 0);
		glRotatef(rotSmooth * (180/PI), 0,0,1);
		if (prevModule != -1){
			LM[prevModule]->draw(posSmooth.x, posSmooth.y);
		}
		LM[currentModule]->draw(posSmooth.x, posSmooth.y);
		//LR.draw(posSmooth.x, posSmooth.y);
	glPopMatrix();


	ofSetColor(0xff0000);
	char temp[255];
	sprintf(temp, "frame rate %f \n", ofGetFrameRate());
	ofDrawBitmapString(temp, 100,768-100);
}


//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	
}

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

}
