/*********************************************************************
*	CMessageBox
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 3.7.2008
*	Soubor: messagebox.cpp
*	Popis: Jedna se o dialog. Muze nest jednu komponentu, takze je spise prostrednikem
*			nez vlastnim dialogem.
*			
**********************************************************************/

#include "gfg.h"

CMessageBox::CMessageBox( float x, float y, float w, float h )
{
	m_pComponent = NULL;
	baseInit( w, h );
	setPosition( x, y );
	m_ColorBackGround = GFG_COLOR_MESSAGEBOX_BACKGROUND;
	m_iObjectType = GFG_MESSAGEBOX;
}

CMessageBox::~CMessageBox()
{
	delete m_pComponent;
}

void CMessageBox::setComponent( CBaseComponent *component )
{ 
	m_pComponent = component; 
	m_pComponent->setParent( this );
	updateComponent();
}

void CMessageBox::updateComponent()
{
	if( m_pComponent )
	{
		m_pComponent->setPosition( m_ScissorBox.x, m_ScissorBox.y );
		m_pComponent->setSize( m_ScissorBox.w, m_ScissorBox.h );
	}
}

void CMessageBox::onScissorBoxTranslate( float offsetX, float offsetY )
{
	if( m_pComponent )
		m_pComponent->setPositionOffset( offsetX, offsetY );
}

void CMessageBox::onScissorBoxResize( float w_offset, float h_offset )
{
	if( m_pComponent )
		m_pComponent->setSizeOffset( w_offset, h_offset );
}

void CMessageBox::setTemporaryDisabled( bool disabled )
{
	m_bTemporaryDisabled = disabled;
	if( m_pComponent )
		m_pComponent->setTemporaryDisabled( disabled );
}

void CMessageBox::setEnabled( bool value )
{
	m_bEnabled = value;
	if( m_pComponent )
		m_pComponent->setEnabled( value );
}

void CMessageBox::setAlphaMultiplier( float multiplier )
{
	m_fAlphaMultiplier = multiplier;
	if( m_pComponent )
		m_pComponent->setAlphaMultiplier( multiplier );
}

void CMessageBox::draw( ScissorBox &scissorBox )
{
	glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_SCISSOR_BIT);
	
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND );
	glDisable( GL_TEXTURE_2D );
	
	setColor( m_ColorBackGround );
	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();
	ScissorBox intersected = m_ScissorBox.intersection( scissorBox );
	glScissor( (GLint)intersected.x, (GLint)intersected.y, (GLsizei)intersected.w, (GLsizei)intersected.h );
	
	if( m_pComponent )
		m_pComponent->draw( intersected );


	glPopAttrib();

}
bool CMessageBox::inputController( int type, float x, float y, int param1, int param2 )
{
	if( m_pComponent )
		return m_pComponent->inputController( type, x, y, param1, param2 );
	return false;
}
void CMessageBox::run()
{
	if( m_pComponent )
		return m_pComponent->run();
}

