/*********************************************************************
*	Label
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 31.7.2008
*	Soubor: label.cpp
*	Popis: Zakladni verze popisku - zobrazuje text v oramovanem poli
**********************************************************************/

#include "gfg.h"

//konstruktor - inicializace
Label::Label( float w, float h )
{
	baseInit( w, h );
	init();
}

Label::Label()
{ 
	init();
}

void Label::init()
{
	m_ColorText = GFG_COLOR_LABEL_TEXT;
	setPadding( GFG_STANDARD_PADDING );
	setPaddingRight( 0 );

	m_iObjectType = GFG_LABEL;
	m_iAlign = FONT_ALIGN_LEFT;
	m_pFont = fontLibrary::getFont("SABIU");
}

//Vykresleni ohraniceni popisku
void Label::drawOutLines()
{
	float offsetX = m_fWidth - m_fWidth * GFG_OUTLINE_MULTIPLICATOR;
	float offsetY = m_fHeight - m_fHeight * GFG_OUTLINE_MULTIPLICATOR;
	glBegin( GL_LINE_STRIP );
			setColor( GFG_COLOR_SKIN_END_LINE );
			glVertex2f( m_fGlobalXpos , m_fGlobalYpos + m_fHeight);	

			setColor( GFG_COLOR_SKIN_START_LINE );
			glVertex2f( m_fGlobalXpos, m_fGlobalYpos );
			
			setColor( GFG_COLOR_SKIN_END_LINE );
			glVertex2f( m_fGlobalXpos + m_fWidth - offsetX, m_fGlobalYpos );
		
			
		glEnd();
		glBegin( GL_LINE_STRIP );
			setColor( GFG_COLOR_SKIN_END_LINE );
			glVertex2f( m_fGlobalXpos + offsetX , m_fGlobalYpos + m_fHeight );
			
			setColor( GFG_COLOR_SKIN_START_LINE );
			glVertex2f( m_fGlobalXpos + m_fWidth , m_fGlobalYpos + m_fHeight );
			
			setColor( GFG_COLOR_SKIN_END_LINE );
			glVertex2f( m_fGlobalXpos + m_fWidth , m_fGlobalYpos + offsetY );
		glEnd();

}

//Vykresleni textu
void Label::drawText( ScissorBox &scissorBox)
{
	if( m_dsCaption.m_pFont )
	{
		switch( m_dsCaption.m_iAlign )
		{
			case FONT_ALIGN_LEFT:
				m_dsCaption.draw( m_ScissorBox.x, m_ScissorBox.y + m_ScissorBox.h - (float)m_dsCaption.m_pFont->m_iLetterHeight );
				break;
			case FONT_ALIGN_CENTER:
				m_dsCaption.draw( m_ScissorBox.x + m_ScissorBox.w / 2.0f, m_ScissorBox.y + m_ScissorBox.h - (float)m_dsCaption.m_pFont->m_iLetterHeight );
				break;
			case FONT_ALIGN_RIGHT:
				m_dsCaption.draw( m_ScissorBox.x + m_ScissorBox.w, m_ScissorBox.y + m_ScissorBox.h - (float)m_dsCaption.m_pFont->m_iLetterHeight );
				break;
		}
	}
	
		
}

//Zaklad vykreslovani
void Label::draw( ScissorBox &scissorBox  )
{
	if( !m_bVisible )
		return;
	glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_SCISSOR_BIT );
	
	glEnable(GL_BLEND);
	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
	glColor4fv( m_ColorBackGround.values );
	glEnable(GL_SCISSOR_TEST );

	glBegin( GL_QUADS );
		glVertex2f( m_fGlobalXpos, m_fGlobalYpos );
		glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos );
		glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos + m_fHeight);
		glVertex2f( m_fGlobalXpos, m_fGlobalYpos + m_fHeight);
	glEnd();
	

	//glBegin( GL_QUADS );
	//	glVertex2f( m_fGlobalXpos, m_fGlobalYpos );
	//	glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos );
	//	glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos + m_fHeight);
	//	glVertex2f( m_fGlobalXpos, m_fGlobalYpos + m_fHeight);
	//glEnd();
		drawOutLines();
		drawOthers( scissorBox );
		
	glDisable(GL_BLEND);
	glEnable(GL_SCISSOR_TEST );

	ScissorBox intersected = m_ScissorBox.intersection( scissorBox );
	glScissor( (int)intersected.x, (int)intersected.y, (int)intersected.w, (int)intersected.h );
	
	setColor( m_ColorText );	
	if( !m_dsCaption.empty() )
		drawText( scissorBox );

	glPopAttrib();
}

void Label::setCaption(std::string text)
{
	m_dsCaption = fontLibrary::DString( text, m_pFont, m_iAlign, (int)m_ScissorBox.w );
}

//Reakce na zmenu velikosti
void Label::onScissorBoxResize( float w_offset, float h_offset )
{
	m_dsCaption.resize( (int)m_ScissorBox.w );
}

