
#include "stdafx.h"
#include "CSliderGUI.h"
#include "../App/CBaseGL.h"
#include "../Math/Math2D.h"

//////////////////////////////////////////////////////////////////////////
// CONSTRUKTOR - 
//////////////////////////////////////////////////////////////////////////
C_SLIDER_0::C_SLIDER_0(std::string name, uint texture_id1, uint texture_id2, TRect rect, int max_ar, int hod_ar, int akt_pos)

: C_GIMAGE_0		(name, texture_id1, rect),
  _tex_background	(texture_id1),
  _tex_spin			(texture_id2),
  _max_ar			(max_ar),
  _hod_ar			(hod_ar),
  _akt_ar			(akt_pos),
  _mouse_click		(0),
  _mouse_click_spin	(0)
{
	if(akt_pos == -1) _akt_ar = max_ar/2;
}

//////////////////////////////////////////////////////////////////////////
// DESTRUKTOR - 
//////////////////////////////////////////////////////////////////////////
C_SLIDER_0::~C_SLIDER_0()
{
	
}

//////////////////////////////////////////////////////////////////////////
// KEYBOARD - 
//////////////////////////////////////////////////////////////////////////
void C_SLIDER_0::Keyboard(bool keys[255])
{
	if(keys[VK_RIGHT])	_akt_ar++;
	if(keys[VK_LEFT])	_akt_ar--;
}

//////////////////////////////////////////////////////////////////////////
// MOUSECLICK - 
//////////////////////////////////////////////////////////////////////////
void C_SLIDER_0::MouseClick(int button, int x, int y)
{
	if(button == W_MOUSE_LEFT)		_mouse_click = 1;
	else
	if(button == W_MOUSE_UNLEFT)	_mouse_click = 0;
}

//////////////////////////////////////////////////////////////////////////
// MOUSECLICKEX - 
//////////////////////////////////////////////////////////////////////////
void C_SLIDER_0::MouseClickEx(int button, int x, int y)
{
	POINT p = {x, y};
	if(button == W_MOUSE_LEFT && ColPointToRect(p, _rect_spin) )
	{
		_mouse_click_spin = 1;
		_mouse_click = 0;
		return;
	}else
	if(button == W_MOUSE_UNLEFT )
	{
		_mouse_click_spin = 0;
		_mouse_click = 0;
		return;
	}
}

//////////////////////////////////////////////////////////////////////////
// MOUSEMOVEEX - 
//////////////////////////////////////////////////////////////////////////
void C_SLIDER_0::MouseMoveEx(int x, int y)
{
	TRect rect = GetRect();
	float polWidthSpin = _rect_spin.width/2;	
	float fJedenDilek = (rect.width - polWidthSpin*2)/_max_ar;
	const int iDilekPixelu = rect.width/_max_ar;
	if(_mouse_click)
	{
		if(x > rect.x+iDilekPixelu*_akt_ar) _akt_ar++;
		if(x < rect.x+iDilekPixelu*_akt_ar) _akt_ar--;
	}
	if(_mouse_click_spin)
	{
		if(x > rect.x) _akt_ar = ((x-polWidthSpin) - rect.x)/fJedenDilek;
		if(_akt_ar < 0) _akt_ar = 0;
		if(_akt_ar > _max_ar) _akt_ar = _max_ar;
	}
}

//////////////////////////////////////////////////////////////////////////
// MouseMove
//////////////////////////////////////////////////////////////////////////
void C_SLIDER_0::MouseMove(int x, int y)
{
}

//////////////////////////////////////////////////////////////////////////
// Render
//////////////////////////////////////////////////////////////////////////
void C_SLIDER_0::Render(bool focus)
{
	this->doOrtho();
	// Draw background
	glEnable(GL_ALPHA_TEST);
	(_tex_background == 0) ? glDisable(GL_TEXTURE_2D) :	glEnable(GL_TEXTURE_2D);

	glLoadIdentity();
	TRGBi color = GetColor();
	glColor3ub(color.r, color.g, color.b);
	glBindTexture(GL_TEXTURE_2D, _tex_background);
	TRect rect = GetRect();

	glBegin(GL_QUADS);
		glTexCoord2i(0,1);		glVertex2f(rect.x,rect.y);
		glTexCoord2i(1,1);		glVertex2f(rect.x+rect.width,rect.y);
		glTexCoord2i(1,0);		glVertex2f(rect.x+rect.width,rect.y+rect.height);
		glTexCoord2i(0,0);		glVertex2f(rect.x,rect.y+rect.height);
	glEnd();
	// Draw spin

	// Testovani colise
	if(_akt_ar < 0) _akt_ar = 0;
	if(_akt_ar > _max_ar) _akt_ar = _max_ar;
	
	(_tex_spin == 0) ? glDisable(GL_TEXTURE_2D) :	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _tex_spin);
	float polWidthSpin = _rect_spin.width/2;	
	float fJedenDilek = (rect.width - polWidthSpin*2)/_max_ar;
	_rect_spin.x = rect.x+fJedenDilek*(_akt_ar) - _rect_spin.width/2 + polWidthSpin;
	_rect_spin.width = _hod_ar;
	_rect_spin.y = rect.y;
	_rect_spin.height = rect.height;

	glBegin(GL_QUADS);
		glTexCoord2i(0,1);		glVertex2f(_rect_spin.x,					_rect_spin.y);
		glTexCoord2i(1,1);		glVertex2f(_rect_spin.x+_rect_spin.width,	_rect_spin.y);
		glTexCoord2i(1,0);		glVertex2f(_rect_spin.x+_rect_spin.width,	_rect_spin.y+rect.height);
		glTexCoord2i(0,0);		glVertex2f(_rect_spin.x,					_rect_spin.y+rect.height);
	glEnd();
	
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_ALPHA_TEST);
	this->backOrtho();
}