#include "handDrawnModule.h"

//-------------------------------------------------
handDrawnModule::handDrawnModule(){
	
}

//-------------------------------------------------	
void handDrawnModule::setup(){
	bgColor.set(255,255,255);
	arrow.loadImage("images/arrow.png");
	maxClickNum 	= 10;
	clicks 			= new ofVec3f[maxClickNum];
	ld				= new lineDrawer[maxClickNum];
	bDone 			= false;
	reset();
}

//-------------------------------------------------
void handDrawnModule::reset(){
	nClicks 			= 0;
	bDone 				= false;
	lastTimePressed 	= 0;
	pos.set(0,240,0);
	clicks[nClicks].set(pos);
	
	//------------------------------- 
	for (int i = 0; i < maxClickNum; i++){
		ld[i].reset();
	}
			
	// this is a hack to translate to the center of this object...
	addPoint(ofVec3f(640/2, 480/2,0));
	
	nClicks++;
}


//-------------------------------------------------
void handDrawnModule::update(){
	
	bool bAreWePressed = ofGetMousePressed();
	
	if (bAreWePressed == true && bPressed == false){
		
		// just happened!
		
		if (nClicks < maxClickNum){
			
			float clickDelay = ofGetElapsedTimef() - lastTimePressed;	
			if (clickDelay < 0.25){
				bDone = true;
			} else {
				clicks[nClicks].set(ofGetMouseX(), ofGetMouseY(), 0);
				
				ld[nClicks].reset();
				ofVec3f diff = clicks[nClicks-1] - clicks[nClicks];
				diff*=1.33f;
				ld[nClicks].calcAngleScaleOffset(ofVec3f(0,0,0), ofVec3f(ofGetMouseX(), ofGetMouseY(),0)+diff);
				ld[nClicks].bDrawing = true;
				
				addPoint(ofVec3f(clicks[nClicks].x, clicks[nClicks].y, 0));
				lastTimePressed = ofGetElapsedTimef();
				nClicks++;
			}
		} else {
			bDone = true;
		}
	}
	
	bPressed = ofGetMousePressed();
	
	for (int i = 0; i < maxClickNum; i++){
		ld[i].idle();
	}
}

//-------------------------------------------------
void handDrawnModule::draw(){
	
	
	glPushMatrix();
	//glTranslatef(0,480,0);
	//glScalef(1,-1,1);
	
	ofSetColor(0xffffff);
	
	if (nClicks > 1){
		glBegin(GL_LINE_STRIP);
		for (int i = 0; i < nClicks; i++){
			glVertex2f(clicks[i].x, clicks[i].y);
		}
		glEnd();
	} else {
		ofLine(clicks[0].x, clicks[0].y, ofGetMouseX(), ofGetMouseY());
	}
	
	// draw the arrows
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_COLOR, GL_ONE);
		ofSetColor(0xffffff);
		for (int i = 1; i < nClicks; i++){
			arrow.draw(clicks[i].x, clicks[i].y);
			
			clicks[i].x += ofRandomf(); 	// funny
			clicks[i].y += ofRandomf();
		}
		
		if (!bDone){
			arrow.draw(ofGetMouseX(), ofGetMouseY());
		}
	glDisable(GL_BLEND);
	
	ofEnableAlphaBlending();
	glBlendFunc(GL_DST_COLOR, GL_ZERO);
	for (int i = 0; i < maxClickNum; i++){
		ld[i].draw();
	}
	ofDisableAlphaBlending();
	
	glPopMatrix();
	
}

//-------------------------------------------------
bool handDrawnModule::bAmIdone(){
	return bDone;
}

//-------------------------------------------------
ofVec3f handDrawnModule::getStartPosition(){
	return ofVec3f(0,240,0);
}

//-------------------------------------------------
float handDrawnModule::getStartAngle(){
	return atan2(ofGetMouseY()-240, ofGetMouseX());;
}
		
		

