#include "graffLetter.h"


//----------------------------------------------
void graffLetter::setup(){
	width = 1024;
	height = 768;
	
	cvImgWhite.allocate(width,height);
	cvImgWhite.set(0);
	cvImgBlack.allocate(width,height);
	cvImgBlack.set(0);
	
	cvImgBottom.allocate(width,height);
	cvImgBottom.set(0);
	cvImgTop.allocate(width,height);
	cvImgTop.set(0);
	
	cvImgMix.allocate(width,height);
	cvImgMix.set(0);
	
	cvTopLayerMask.allocate(width,height);
	cvTopLayerMask.set(0);
	cvBottomLayerMask.allocate(width,height);
	cvBottomLayerMask.set(0);

	setupBrush(cvWhiteBrush,40);
	setupBrush(cvBlackBrush,32);	
	setupShadowBrush(cvDropShadowBrush,120);	
	
	
}

//----------------------------------------------
void graffLetter::setupBrush(ofCvGrayscaleImage &brush, int width){
	brush.allocate(width,width);
	unsigned char * temp = new unsigned char[width*width];
	float midPt 	= (float)width/2.0f;
	float radius 	= (float)width/2.0f;
	for (int i = 0; i < width; i++){
		for (int j = 0; j < width; j++){
			float dist = (sqrt((midPt-i)*(midPt-i) + (midPt-j)*(midPt-j)) / radius);
			if (dist < 1){
				float shape = powf((1-dist), 2.0f);
				temp[j*width + i] = 255;//(shape) * 100.0f;	
			} else {
				temp[j*width + i] = 0;
			}
				
		}
	}
	brush.setFromPixels(temp, width, width);
	delete temp;
}

//----------------------------------------------
void graffLetter::setupShadowBrush(ofCvGrayscaleImage &brush, int width){
	
	brush.allocate(width,width);
	unsigned char * temp = new unsigned char[width*width];
	float midPt 	= (float)width/2.0f;
	float radius 	= (float)width/2.0f;
	for (int i = 0; i < width; i++){
		for (int j = 0; j < width; j++){
			float dist = (sqrt((midPt-i)*(midPt-i) + (midPt-j)*(midPt-j)) / radius);
			if (dist < 1){
				float shape = powf((1-dist), 2.0f);
				temp[j*width + i] = (shape) * 100.0f;	
			} else {
				temp[j*width + i] = 0;
			}
				
		}
	}
	brush.setFromPixels(temp, width, width);
	brush.blurHeavily();
	delete temp;
}
		


//----------------------------------------------
void graffLetter::update(){
	cvImgBottom.set(0);
	cvImgBottom += cvImgWhite;
	cvImgBottom -= cvImgBlack;

	cvImgMix = cvImgTop;
	cvImgMix.composite(cvImgBottom, cvBottomLayerMask);
	
}

void graffLetter::newLetter(){
	
	cvImgBottom.set(0);
	cvImgBottom += cvImgWhite;
	cvImgBottom -= cvImgBlack;
	
	cvImgTop.composite(cvImgBottom, cvBottomLayerMask);
	cvImgWhite.set(0);
	cvImgBlack.set(0);
	cvBottomLayerMask.set(0);
}


//----------------------------------------------
void graffLetter::draw(){
	cvImgMix.draw(0,0);
	
							
}

void graffLetter::clear(){
	cvImgBottom.set(0);
	cvImgTop.set(0);
	cvImgWhite.set(0);
	cvImgBlack.set(0);
	cvBottomLayerMask.set(0);
}

//----------------------------------------------
void graffLetter::addLine(float x1, float y1, float x2, float y2){
	
	
	int nDivs 	= 10;
	float dx 	= (float)(x2 - x1)/(float)nDivs;
	float dy 	= (float)(y2 - y1)/(float)nDivs;
	
	// white
	for (int j = 0; j < nDivs; j++){
		cvImgWhite.addIntoMe(cvWhiteBrush, x1 + dx*j-20, y1 + dy*j-20);
		cvBottomLayerMask.addIntoMe(cvWhiteBrush, x1 + dx*j-20, y1 + dy*j-20);
	}
	
	// offset!
	for (int j = 0; j < nDivs; j++){
		cvImgWhite.addIntoMe(cvWhiteBrush, x1 + dx*j-30, y1 + dy*j-30);
		cvBottomLayerMask.addIntoMe(cvWhiteBrush, x1 + dx*j-30, y1 + dy*j-30);
	}
	
	cvBottomLayerMask.addIntoMe(cvDropShadowBrush, x1 - 60, y1 - 60);
	
	
	
	// black 
	for (int j = 0; j < nDivs; j++){
		cvImgBlack.addIntoMe(cvBlackBrush, x1 + dx*j-17, y1 + dy*j-17);
	}
	
}