#include "VTransferFunctionElement.h"
#include <iostream>
#include "V2Ddraw.h"
#include "VHistogram.h"
#include <GL/glew.h>
#include <GL/glut.h>

using namespace std;

VTransferFunctionElement::VTransferFunctionElement(int* width, int* height, unsigned int* fps,int* hist):
	GUIelement(width,height,fps)
{
	this->x = 10;
	this->y = 10;
	this->width = (int)(0.6*(*this->wwidth));
	this->height = (int)(0.3*(*this->wheight));
	//this->alpha = 0.2;
	//background color blue
	this->bg = new float[3];
	this->bg[0] = (float)84/265;
	this->bg[1] = (float)160/256;
	this->bg[2] = (float)192/256;
	//foreground color black
	this->fg = new float[3];
	this->fg[0] = this->fg[1] = this->fg[2] = 0.0;
	//histogram color yellow
	this->histcol = new float[3];
	this->histcol[0] = this->histcol[1] = 1.0;
	this->histcol[2] = 0.0;
	//Transfer Function Elements
	this->background = new VTexture("transferfunc.tga");
	this->exit = new VExitBox(this->width+this->x-15,this->y+10,10,this->fg,this->histcol,&(this->alpha));
	this->hist = new VHistogram(hist,4096,this->x+10,this->y+10,this->width-30,this->height-50);
	this->function = new VTransferFunction(this->x+10,this->y+10,this->width-30,this->height-50,&(this->alpha));
	this->function->renderTexture();
	this->dragging = false;
}

VTransferFunctionElement::~VTransferFunctionElement(void)
{
	delete exit;
	delete bg;
	delete fg;
	delete histcol;
	delete hist;
	delete background;
}

void VTransferFunctionElement::render() {
	if(!this->active && this->blend) { //einblenden
		float frames = (float)*(this->fps);
		this->alpha+=sin((1.5/frames));
		this->draw();
		if(this->alpha >= 1) {
			this->alpha = 1;
			this->setActive(true);
		}
	}
	if(this->active && !this->blend) { //ausblenden
		float frames = (float)*(this->fps);
		this->alpha-=sin((1.6/frames));
		this->draw();
		if(this->alpha <= 0) {
			this->alpha = 0;
			this->setActive(false);
		}

	}
	if(this->active && this->blend) { //aktiv
		this->draw();
	}
}

void VTransferFunctionElement::draw() {
	//draw background box
	glColor4f(1.0,1.0,1.0,this->alpha);
	//glColor4f(this->bg[0],this->bg[1],this->bg[2],this->alpha);
	V2Ddraw::drawTexBox(this->background,this->x,this->y,this->width,this->height);
	//draw exit box
	this->exit->draw();
	//draw histogram
	glColor4f(this->histcol[0],this->histcol[1],this->histcol[2],this->alpha);
	this->hist->draw();
	//draw transferfunction
	glColor4f(this->fg[0],this->fg[1],this->fg[2],this->alpha);
	this->function->draw();
}

void VTransferFunctionElement::motion(int x, int y) {
	exit->inside(x,y);
}

void VTransferFunctionElement::pressed(int x, int y) {
	if(this->function->isInside(x,y))
		this->function->pressed(x,y);
}

void VTransferFunctionElement::released(int x, int y) {
	if(this->dragging) this->dragging = false;
	else {
		if(this->exit->isInside(x,y))
			this->setBlend(false);
	}
	if(this->function->isInside(x,y))
		this->function->released(x,y);
	this->function->getTexture();
}

void VTransferFunctionElement::drag(int x,int y) {
	this->dragging = true;
	if(this->function->isInside(x,y)) {
		this->function->drag(x,y);
	}
}

bool VTransferFunctionElement::isInside(int xm, int ym) {
	return xm > this->x && xm < this->x + this->width && ym > this->y && ym < this->y + this->height;
}

GLuint * VTransferFunctionElement::getTexture() {
	return this->function->getTexture();
}