/*********************************************************************
*	SystemConsole
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 25.7.2008
*	Soubor: sys_console.cpp
*	Popis: Modul implementuje konzoli, ktera se pouziva pro textovou
* 			interakci s uzivatelem. Definje objekt, pomoci nehoz lze
* 			retezit datove typy do textoveho retezce a vzpisovat ho.
* 			Umoznuje logovani do souboru. 
**********************************************************************/

#include "sys_console.h"
#include <sstream>
#include "ctrl_gamecontrol.h"
#include <time.h>
#include "sys_controller.h"
#include "mathematic.h"

using namespace systemConsole;
using namespace std;

//definice extern promennych
systemConsole::Console systemConsole::console;
SystemConsole systemConsole::sys_console;


//definice objektu Console, ktery implementuje retezeni datovych typu do textoveho retezce
Console & Console::operator <<( const int text )
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}

Console& Console::operator<<( const string text )
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}

Console& Console::operator<<( const float text)
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}
Console& Console::operator<<( const double text )
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}
Console& Console::operator<<( const long long int text )
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}
Console& Console::operator<<( const unsigned int text )
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}
Console& Console::operator<<( const unsigned long long int text )
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}
Console& Console::operator<<( const char text )
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}
Console& Console::operator<<( const char *text )
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}

Console& Console::operator<<( const unsigned char text )
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}
Console& Console::operator<<( const unsigned char *text )
{
	ostringstream buf;	buf << text;	sys_console.print( buf.str() );		buf.clear();
	return *this;
}


/*	Definice tridy SystemConsole
 * 
 */

//funkce uklada text do bufferu a jakmile prijde znak \n 
//tak se teprve text posle na vystup
void SystemConsole::print( string text )
{
	for( size_t i = 0; i < text.length();i++)
		if( text.at(i) == '\n' )
		{
			m_bFlush = true;
			break;
		}
	buffer += text;
	if( m_bFlush )
	{
		cout << buffer;
		if( m_bHUDConsoleReady )
			insertToHUDConsole( buffer );
		if( m_bLogingEnabled )
			logMessage( buffer );
		m_bFlush = false;
		buffer.clear();
	}
		
}

void SystemConsole::command( string command )
{
	print( command );
}

//nastavi soubor pro logovani
bool SystemConsole::setLogingFile( const char *filename )
{
	m_Fout.open( filename, ios::out | ios::trunc );
	return m_Fout.is_open();
}

//zapne logovani
bool SystemConsole::startLoging()
{
	if( m_Fout.is_open() )
		m_bLogingEnabled = true;
	return 	m_bLogingEnabled;
}

//vypne logovani
void SystemConsole::stopLoging()
{
	m_bLogingEnabled = false;
	if( m_Fout.is_open() )
		m_Fout.close();
}

SystemConsole::~SystemConsole()
{
	stopLoging();
	deleteAll();
}

//funkce logovani - cas je docasne neimplementovany :)
void SystemConsole::logMessage( string text )
{
	if( m_bLogingEnabled && m_Fout.is_open() )
	{
		m_Fout << text;
		/*if( m_bPrintTime )
		{
			time_t hhh = time(NULL);
			tm *jjj = NULL;
			localtime_s(jjj, &hhh);
			char buf[20];
			asctime_s( buf, 20, jjj);
			for( unsigned int i = 0; i < strlen(buf) - 1; i++ )
				m_Fout.put( buf[i] );
			m_Fout << " ";
			m_bPrintTime = false;
		}

		for( size_t i = 0; i < text.length(); i++ )
		{
			if( text.at( i ) == '\n' )
			{
				m_Fout.put( text.at( i ) );
				m_bPrintTime = true;
				if( i+1 < text.length() )
					logMessage( text.substr( i+1 ) );
				break;

			}
			else
				m_Fout.put( text.at( i ) );
		}*/

	}
}


/***************************************************************
************	HUD CONSOLE SPECIFICATION		****************
***************************************************************/

SystemConsole::SystemConsole()
{
	m_bPrintTime = true;
	m_pCommandLine = NULL;
	m_pConsoleText = NULL;
	m_pConsoleBox = NULL;
	m_bFadingIn = false;
	m_bAnimationFinishing = false;
	m_bFadingOut = false;
}

//vlozi text do rolovaciho popisku
void SystemConsole::insertToHUDConsole( string text )
{
	if( m_pConsoleText )
	{
		fontLibrary::DString *tmpString  = &m_pConsoleText->getCaption();
		if( tmpString->m_pFont == NULL )	//neexistuje jeste zadny text
			m_pConsoleText->setCaption( text );	//tak ho vytvorime
		else if( tmpString->m_pLineList  )
		{
			if( tmpString->m_pLineList->size() > HUDCONSOLE_LABEL_MAX_LINES )
			{
				
				tmpString->m_iHeight -=  (int)((float)(tmpString->m_pFont->m_iLetterHeight) * LINE_HEIGHT_MULTIPLIKATOR + (float)(tmpString->m_pFont->m_iLetterHeight/2));
				tmpString->m_pLineList->pop_front();

			}
			tmpString->add( text, (int)m_pConsoleText->getScissorBox().w );
			m_pConsoleText->update();
		}

		m_pConsoleText->setScrollBarValue( 1.0f );
	}
}

//inicializace grafickeho zobrazeni konzole
void SystemConsole::createHUDConsole( float width, float height )
{
	console << "Creating HUD Console...";
	setSize( width, height );

	m_pConsoleBox = new ContainerRelativeUpToDown( GFG_CONTAINER_REVERSE );
	m_pConsoleBox->setPadding( HUDCONSOLE_CONTAINER_PADDING );
	m_pConsoleBox->setPaddingBottom( 0.0f );
	m_pConsoleBox->setHorizontalPadding( HUDCONSOLE_CONTAINER_HORIZONTAL_PADDING_PERCENT );

	m_pConsoleBox->setSize( m_fWidth - 2.0f * HUDCONSOLE_RESIZING_BORDER, m_fHeight / 2.0f - 2.0f * HUDCONSOLE_RESIZING_BORDER );
	m_pConsoleBox->setPosition( m_fWidth, m_fHeight  );

	m_pCommandLine = new ConsoleTextBox(0,0);
	m_pCommandLine->setFont( HUDCONSOLE_FONT );
	m_pCommandLine->setColorText( HUDCONSOLE_TEXTBOX_COLOR );
	m_pCommandLine->setMaxLenght( HUDCONSOLE_COMMAND_LINE_MAX_LENGTH );
	m_pConsoleBox->addComponent( m_pCommandLine, HUDCONSOLE_TEXTBOX_HEIGHT );

	m_pConsoleText = new ConsoleScrollLabel(0,0);
	m_pConsoleText->setFont( HUDCONSOLE_LABEL_FONT );
	m_pConsoleText->setColorText( HUDCONSOLE_LABEL_COLOR );
	m_pConsoleBox->addComponent( m_pConsoleText, m_pConsoleBox->getScissorBox().h - HUDCONSOLE_TEXTBOX_HEIGHT );

	addComponent( m_pConsoleBox );

	m_pCommandLine->giveMeFocus();
	m_History.push_back( "" );
	resetHistoryPointer();


	m_pTexBackGround.image.setFilename( HUDCONSOLE_BACKGROUND_IMAGE );
	textureLibrary.applyTexture( &m_pTexBackGround );


	console << "done" << endline;
	m_bHUDConsoleReady = true;
	
}

void SystemConsole::user_draw()
{
	if( m_pTexBackGround.texture_id )
	{
		glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LINE_BIT );
		glEnable( GL_BLEND );
		glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
		glEnable( GL_TEXTURE_2D );

		float x = m_pConsoleBox->getXPosition();
		float y = m_pConsoleBox->getYPosition();
		float w = m_pConsoleBox->getWidth();
		float h = m_pConsoleBox->getHeight();

		glBindTexture( GL_TEXTURE_2D, m_pTexBackGround.texture_id );
		float tex_w = w / m_pTexBackGround.image.width;
		float tex_h = h / m_pTexBackGround.image.height;
		//vykresleni pozadi containeru
		glColor4f( 1,1,1,m_fMyAlpha * HUDCONSOLE_BACKGROUND_ALPHA_MULT );
		
		glBegin( GL_QUADS );
			glTexCoord2f( 0,0);	glVertex2f( x, y );
			glTexCoord2f( tex_w,0); glVertex2f( x+w, y );
			glTexCoord2f( tex_w,tex_h); glVertex2f( x+w, y+h );
			glTexCoord2f( 0,tex_h); glVertex2f( x, y+h );
		glEnd();

		glLineWidth( HUDCONSOLE_BOTTOM_LINE_SIZE );

		glColor4f( HUDCONSOLE_BOTTOM_LINE_COLOR.values[0],HUDCONSOLE_BOTTOM_LINE_COLOR.values[1],
			HUDCONSOLE_BOTTOM_LINE_COLOR.values[2],m_fMyAlpha * HUDCONSOLE_BOTTOM_LINE_COLOR.values[3] );

		glDisable( GL_TEXTURE_2D );

		glBegin(GL_LINES );
			glVertex2f( x, y );
			glVertex2f( x+w, y );
		glEnd();


		glPopAttrib();
	}
}

void SystemConsole::HUDConsole_use_this_run()
{
	if( m_fMyAlpha > 0.0f || m_fTargetAlpha > 0.0f )
	{
		run();
	}
}


//vstup vykreslovani
void SystemConsole::HUDConsole_use_this_draw()
{
	if( m_fMyAlpha > 0.0f )
	{
		glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT );
		
		glDisable(GL_DEPTH_TEST);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, m_fWidth, 0, m_fHeight, -1, 1);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		draw();
		glPopAttrib();
	}
}

//ovladani
void SystemConsole::HUDConsole_SDL_controller( SDL_Event &event )
{
	/*if( !m_bIsActive )
		return;*/

	if( event.type == SDL_KEYDOWN )
	{
		switch( event.key.keysym.sym )
		{
			case '`':
			case SDLK_ESCAPE:
				systemController::setLastRunningLevel();
				//systemConsole::sys_console.fadeSwitch();
				return;

			case SDLK_RETURN:
				processCommand();
				return;
			case SDLK_UP:
				historyStepUp();
				return;
			case SDLK_DOWN:
				historyStepDown();
				return;
		}
	}


	conv::input( event, this );
}

void SystemConsole::destroy()
{
	if( m_pTexBackGround.texture_id )
		glDeleteTextures( 1, &m_pTexBackGround.texture_id );


	deleteAll();
	m_bHUDConsoleReady = false;
}

//nacte predchozi prikaz z historie
void SystemConsole::historyStepUp()
{
	if( ++m_CurrentCommand == m_History.end() )
		m_CurrentCommand--;
	else
		setCommandLineCaption( m_CurrentCommand );
}

//nacte nasledujici prikaz z historie
void SystemConsole::historyStepDown()
{
	if( m_CurrentCommand != m_History.begin() )
	{
		m_CurrentCommand--;
		setCommandLineCaption( m_CurrentCommand );
	}
}

//nastavi text prikazu
void SystemConsole::setCommandLineCaption( list<string>::iterator &iter )
{
	if( m_pCommandLine && iter != m_History.end())
		m_pCommandLine->setCaption( *iter, HUDCONSOLE_FONT );
}

//provede prikaz, ktery je v prikazove radce
void SystemConsole::processCommand()
{
	if( !m_pCommandLine )
		return;

	string text = m_pCommandLine->getCaption();	//zakonceni
	console << text << endline;
	m_pCommandLine->setCaption("", HUDCONSOLE_FONT);	//reset textu radky
	gameControl::game_control.decodeCommand( text );
	addCommand( text );		//vlozeni prikazu do historie
}

//reset ukazatele prikazu v historii na zacatek
void SystemConsole::resetHistoryPointer()
{
	m_CurrentCommand = m_History.begin();
}

//vlozi prikaz do historie
void SystemConsole::addCommand( std::string sCommand )
{
	if( !m_History.empty() )
	{
		list<string>::iterator iter = m_History.begin();
		iter++;
		m_History.insert( iter, sCommand );
	}

	resetHistoryPointer();
}

//zapne/vypne konzoli
void SystemConsole::fadeSwitch()
{
	if( m_bIsActive )
		fadeoutAnimated();
	else
		fadeinAnimated();
}

void SystemConsole::fadeinAnimated( float value )
{ 
	//m_fTargetAlpha = value;
	fadein();
	m_bFadingIn = true;
	m_bFadingOut = false;
	m_bIsActive = true;
}

void SystemConsole::fadeoutAnimated( float value  )
{ 
	//m_fTargetAlpha = value;
	fadeout();
	m_bFadingIn = false;
	m_bFadingOut = true;
	m_bIsActive =false;
}

//zde je definovan prubeh animace pri otevreni/zavreni konzole
void SystemConsole::user_run()
{
	if( !m_pConsoleBox )
		return;
	
	float x = m_pConsoleBox->getXPosition();
	float y = m_pConsoleBox->getYPosition();
	float h = m_pConsoleBox->getHeight();
	float w = m_pConsoleBox->getWidth();

	if( m_bFadingIn || m_bFadingOut )
	{
		if( m_bAnimationFinishing )
		{
			float x_offset = 0.0f, y_offset = 0.0f;
			if( x > 0 && m_bFadingIn )
				x_offset = getFromRange( HUDCONSOLE_ANIM_FINNISH_SPEED, 0.0f, x );
			else if( x < HUDCONSOLE_RESIZING_BORDER && m_bFadingOut )
				x_offset = getFromRange( HUDCONSOLE_ANIM_FINNISH_SPEED, 0.0f, HUDCONSOLE_RESIZING_BORDER-x );

			if( y > m_fHeight / 2.0f && m_bFadingIn ) 
				y_offset = getFromRange( HUDCONSOLE_ANIM_FINNISH_SPEED, 0.0f, y - m_fHeight / 2.0f  );
			else if( y < m_fHeight / 2.0f + HUDCONSOLE_RESIZING_BORDER && m_bFadingOut )
				y_offset = getFromRange( HUDCONSOLE_ANIM_FINNISH_SPEED, 0.0f, m_fHeight / 2.0f + HUDCONSOLE_RESIZING_BORDER - y  );
		
			if( m_bFadingIn )
			{

				if( !compareFloats( x, 0.0f, FLOAT_COMPARE_ACCURACY_HIGH ) && 
					!compareFloats( y, m_fHeight / 2.0f, FLOAT_COMPARE_ACCURACY_HIGH ) )
					m_bFadingIn = false;
				else
				{
					m_pConsoleBox->setPositionOffset( -x_offset, -y_offset );
					m_pConsoleBox->setSizeOffset( 2.0f*x_offset, 2.0f*y_offset );
				}
			}
			else
			{
				if( !compareFloats( x, HUDCONSOLE_RESIZING_BORDER, FLOAT_COMPARE_ACCURACY_HIGH ) && 
					!compareFloats( y, m_fHeight / 2.0f + HUDCONSOLE_RESIZING_BORDER, FLOAT_COMPARE_ACCURACY_HIGH ) )
					m_bAnimationFinishing = false;
				else
				{
					m_pConsoleBox->setPositionOffset( x_offset, y_offset );
					m_pConsoleBox->setSizeOffset( -2.0f*x_offset, -2.0f*y_offset );
				}
			
			}
		}
		else
		{
			float difference_x = 0.0f, difference_y = 0.0f;
			if( x > HUDCONSOLE_RESIZING_BORDER && m_bFadingIn || x < m_fWidth && m_bFadingOut )
			{
				if( m_bFadingIn )
					difference_x = x - HUDCONSOLE_RESIZING_BORDER;
				else
					difference_x = m_fWidth - x;

				if( difference_x < HUDCONSOLE_MIN_ANIM_SPEED )
					difference_x = getFromRange( difference_x * HUDCONSOLE_ANIM_SPEED_MULTIPLICATOR, difference_x, HUDCONSOLE_MIN_ANIM_SPEED );
				else
					difference_x = getFromRange( difference_x * HUDCONSOLE_ANIM_SPEED_MULTIPLICATOR, HUDCONSOLE_MIN_ANIM_SPEED, difference_x );
				
			}
			if( y > HUDCONSOLE_RESIZING_BORDER + m_fHeight / 2.0f  && m_bFadingIn || y < m_fHeight && m_bFadingOut)
			{
				if( m_bFadingIn )
					difference_y = y - (HUDCONSOLE_RESIZING_BORDER + m_fHeight / 2.0f );
				else
					difference_y = m_fHeight - y;
				if( difference_y < HUDCONSOLE_MIN_ANIM_SPEED )
					difference_y = getFromRange( difference_y * HUDCONSOLE_ANIM_SPEED_MULTIPLICATOR, difference_y, HUDCONSOLE_MIN_ANIM_SPEED );
				else
					difference_y = getFromRange( difference_y * HUDCONSOLE_ANIM_SPEED_MULTIPLICATOR, HUDCONSOLE_MIN_ANIM_SPEED, difference_y );
			}
	
			if( m_bFadingIn )
			{
				if( !compareFloats( x, HUDCONSOLE_RESIZING_BORDER, FLOAT_COMPARE_ACCURACY_HIGH ) && 
					!compareFloats( y, HUDCONSOLE_RESIZING_BORDER + m_fHeight / 2.0f, FLOAT_COMPARE_ACCURACY_HIGH ) )
					m_bAnimationFinishing = true;
				else
					m_pConsoleBox->setPositionOffset( -difference_x, -difference_y );
			}
			else
			{
				if( !compareFloats( x, m_fWidth, FLOAT_COMPARE_ACCURACY_HIGH ) && 
					!compareFloats( y, m_fHeight, FLOAT_COMPARE_ACCURACY_HIGH ) )
				{
					m_bFadingOut = false;

				}
				else
					m_pConsoleBox->setPositionOffset( difference_x, difference_y );
			}
		}
	}
}



//Console Textbox specification

GLuint m_uiTexLeft, m_uiTexCenter, m_uiTexRight;
ConsoleTextBox::ConsoleTextBox( float w, float h )	: TextBox( w, h )
{
	TextureElement texture;
	texture.image.setFilename( HUDCONSOLE_TEXTBOX_LEFT );
	
	if( textureLibrary.applyTexture( &texture ) )
		m_uiTexLeft = texture.texture_id;

	texture.texture_id = 0;	//pro jistotu
	texture.shared_index = 0;
	texture.image.setFilename( HUDCONSOLE_TEXTBOX_CENTER );
	if( textureLibrary.applyTexture( &texture ) )
		m_uiTexCenter = texture.texture_id;

	texture.texture_id = 0;	//pro jistotu
	texture.shared_index = 0;
	texture.image.setFilename( HUDCONSOLE_TEXTBOX_RIGHT );
	if( textureLibrary.applyTexture( &texture ) )
		m_uiTexRight = texture.texture_id;

	setPaddingLeft( CONSOLE_TEXTBOX_TEX_WIDTH / 2.0f );
	setPaddingRight( CONSOLE_TEXTBOX_TEX_WIDTH / 2.0f );

}
ConsoleTextBox::~ConsoleTextBox()
{
	glDeleteTextures( 1, &m_uiTexLeft );
	glDeleteTextures( 1, &m_uiTexCenter );
	glDeleteTextures( 1, &m_uiTexRight );
}

void ConsoleTextBox::draw( ScissorBox &scissorBox )
{
	if( !m_bVisible )
		return;

	glPushAttrib( GL_ENABLE_BIT | GL_CURRENT_BIT | GL_SCISSOR_BIT );
	glEnable(GL_BLEND);
	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
	glPushMatrix();
	glEnable( GL_TEXTURE_2D );
	glBindTexture( GL_TEXTURE_2D, m_uiTexLeft );

	if( m_bMouseOver )
		setColor( HUDCONSOLE_COLOR_MOUSE_OVER );
	else
		setColor( HUDCONSOLE_TEXTBOX_OUTLINE_COLOR );

	float tex_width = min( m_fWidth, CONSOLE_TEXTBOX_TEX_WIDTH );
	glBegin( GL_QUADS );
		glTexCoord2i( 0,0);	glVertex2f( m_fGlobalXpos, m_fGlobalYpos );
		glTexCoord2i( 1,0); glVertex2f( m_fGlobalXpos + tex_width, m_fGlobalYpos );
		glTexCoord2i( 1,1); glVertex2f( m_fGlobalXpos + tex_width, m_fGlobalYpos + m_fHeight );
		glTexCoord2i( 0,1); glVertex2f( m_fGlobalXpos, m_fGlobalYpos + m_fHeight );
	glEnd();

	glBindTexture( GL_TEXTURE_2D, m_uiTexCenter );
	glBegin( GL_QUADS );
		glTexCoord2i( 0,0);	glVertex2f( m_fGlobalXpos + tex_width, m_fGlobalYpos );
		glTexCoord2i( 1,0); glVertex2f( m_fGlobalXpos + m_fWidth - tex_width, m_fGlobalYpos );
		glTexCoord2i( 1,1); glVertex2f( m_fGlobalXpos + m_fWidth - tex_width, m_fGlobalYpos + m_fHeight );
		glTexCoord2i( 0,1); glVertex2f( m_fGlobalXpos + tex_width, m_fGlobalYpos + m_fHeight );
	glEnd();
	
	glBindTexture( GL_TEXTURE_2D, m_uiTexRight );
	glBegin( GL_QUADS );
		glTexCoord2i( 0,0);	glVertex2f( m_fGlobalXpos + m_fWidth - tex_width, m_fGlobalYpos );
		glTexCoord2i( 1,0); glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos );
		glTexCoord2i( 1,1); glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos + m_fHeight );
		glTexCoord2i( 0,1); glVertex2f( m_fGlobalXpos + m_fWidth - tex_width, m_fGlobalYpos + m_fHeight );
	glEnd();
	
	/*float offset = m_fWidth - m_fWidth * GFG_OUTLINE_MULTIPLICATOR;
	if( m_bMouseOver || m_bFocused )
	{
		glBegin( GL_LINES );
			setColor( GFG_COLOR_TEXTBOX_END_LINE_MOUSEOVER );
			glVertex2f( m_fGlobalXpos, m_fGlobalYpos );
			setColor( GFG_COLOR_TEXTBOX_START_LINE_MOUSEOVER );
			glVertex2f( m_fGlobalXpos + m_fWidth - offset , m_fGlobalYpos );
		glEnd();
		glBegin( GL_LINES );
			setColor( GFG_COLOR_TEXTBOX_END_LINE_MOUSEOVER );
			glVertex2f( m_fGlobalXpos + m_fWidth, m_fGlobalYpos + m_fHeight );
			setColor( GFG_COLOR_TEXTBOX_START_LINE_MOUSEOVER );
			glVertex2f( m_fGlobalXpos + offset  , m_fGlobalYpos + m_fHeight );
		glEnd();
	}
	else
	{
		glBegin( GL_LINES );
			setColor( GFG_COLOR_SKIN_END_LINE );
			glVertex2f( m_fGlobalXpos, m_fGlobalYpos );
			setColor( GFG_COLOR_SKIN_START_LINE );
			glVertex2f( m_fGlobalXpos + m_fWidth - offset, m_fGlobalYpos );
		glEnd();
		glBegin( GL_LINES );
			setColor( GFG_COLOR_SKIN_END_LINE );
			glVertex2f( m_fGlobalXpos + m_fWidth , m_fGlobalYpos + m_fHeight );
			setColor( GFG_COLOR_SKIN_START_LINE );
			glVertex2f( m_fGlobalXpos + offset , m_fGlobalYpos + m_fHeight );

		glEnd();
	}*/

	glDisable( GL_TEXTURE_2D );
	ScissorBox intersected = m_ScissorBox.intersection( scissorBox );
	glScissor( (GLint)intersected.x, (GLint)intersected.y, (GLint)(intersected.w+1), (GLint)intersected.h );
	
	//posunuti dle animace
	glTranslatef( -m_fAnimateTranslation, 0, 0 );

	setColor( m_ColorBackGround );
	//vykresleni textu
	m_dsCaption.draw( m_ScissorBox.x, m_ScissorBox.y + m_ScissorBox.h/2 );
	
	if( m_bFocused && m_bCursorVisible && m_bEnabled && !m_bTemporaryDisabled)
	{	//a kurzoru
		setColor( HUDCONSOLE_TEXTBOX_OUTLINE_COLOR );	
		glBegin( GL_LINES );
			glVertex2f( m_ScissorBox.x + m_iCursorPos, m_ScissorBox.y + CONSOLE_TEXTBOX_CURSOR_PADDING);
			glVertex2f( m_ScissorBox.x + m_iCursorPos, m_ScissorBox.y + m_ScissorBox.h - CONSOLE_TEXTBOX_CURSOR_PADDING);
		glEnd();
	}
	glPopMatrix();
	glPopAttrib();


}
ConsoleScrollLabel::ConsoleScrollLabel( float w, float h ) : ScrollLabel( w,h)
{
	if( verticalScroll )
	{
		Button *top = verticalScroll->getButtonTop();
		Button *center = verticalScroll->getButtonCenter();
		Button *bottom = verticalScroll->getButtonBottom();
		if( !top || !center || !bottom )
			return;
		
		top->setColorMouseOver( HUDCONSOLE_COLOR_MOUSE_OVER );
		top->setColorIdle( HUDCONSOLE_SCROLLBAR_COLOR_IDLE );
		bottom->setColorMouseOver( HUDCONSOLE_COLOR_MOUSE_OVER );
		bottom->setColorIdle( HUDCONSOLE_SCROLLBAR_COLOR_IDLE );

		center->setColorMouseOver( HUDCONSOLE_COLOR_MOUSE_OVER );
		center->setColorIdle( HUDCONSOLE_SCROLLBAR_COLOR_IDLE );
		
		center->setColorPushed( GFG_COLOR_SCROLLBAR_MOUSEOVER );
		center->setColorPushedMouseOver( HUDCONSOLE_COLOR_MOUSE_OVER );

		top->setColorPushed( HUDCONSOLE_SCROLLBAR_COLOR_IDLE );
		top->setColorPushedMouseOver( HUDCONSOLE_COLOR_MOUSE_OVER );

		bottom->setColorPushed( HUDCONSOLE_SCROLLBAR_COLOR_IDLE );
		bottom->setColorPushedMouseOver( HUDCONSOLE_COLOR_MOUSE_OVER );
	
	
	}


}
void ConsoleScrollLabel::setScrollBarValue( float value )
{
	if( verticalScroll )
		verticalScroll ->setValue( value );

}
