/*********************************************************************
*	Option
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 2.7.2008
*	Soubor: option.cpp
*	Popis: Pole komponent RadioButton. Umoznuje vybrat ze seznamu prave jednu
*			moznost.
**********************************************************************/

#include "gfg.h"

void Option::init()
{
	m_pSelected = NULL;
	m_pContainer = NULL;
}

//nastaveni kontejneru, do ktereho se pak budou vkladat radio buttony
void Option::setContainer( CBaseContainer *pContainer )
{
	if( !pContainer )
		return;

	if( m_pContainer )
		delete m_pContainer;
	m_pContainer = pContainer;
	
	//jestli uvnitr neco je, tak to smazeme; dovolujeme jen object RadioButton
	if( !m_pContainer->empty() )
		m_pContainer->clear();

	//nastavine pozici a velikost
	m_pContainer->setPosition( m_ScissorBox.x, m_ScissorBox.y );
	m_pContainer->setSize( m_ScissorBox.w, m_ScissorBox.h );
	m_pContainer->setParent( this );
}

//pridani volby
void Option::addOption( RadioButton *button )
{
	if( !button || !m_pContainer )
		return;

	if( !m_pSelected )
	{
		m_pSelected = button;
		button->setSwitched( true );
	}
	else
		button->setSwitched( false );
	
	m_pContainer->addComponent( button );
}

//zpravy se predavaji kontejneru
void Option::draw( ScissorBox &scissorBox )
{
	if( m_pContainer )
		m_pContainer->draw( m_ScissorBox.intersection( scissorBox ) );
}

void Option::setAlphaMultiplier( float multiplier )
{
	m_fAlphaMultiplier = multiplier;
	if( m_pContainer )
		m_pContainer->setAlphaMultiplier( multiplier );
}
void Option::run()
{
	if( m_pContainer )
		m_pContainer->run();
}

void Option::setEnabled( bool enabled )
{
	if( m_bTemporaryDisabled )
	{
		m_bEnabled = enabled;
		return;
	}

	m_bEnabled = enabled;
	if( m_pContainer )
		m_pContainer->setTemporaryDisabled( !enabled );
}

void Option::setTemporaryDisabled( bool disabled )
{
	m_bTemporaryDisabled = disabled;	
	if( !m_bEnabled )
		return;

	if( m_pContainer )
		m_pContainer->setTemporaryDisabled( disabled );
}




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

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

//pouze u teto zpravy dochazi ke zpracovani
bool Option::inputController( int type, float x, float y, int param1, int param2 )
{
	if( !m_pContainer )
		return false;

	if( type == GFG_MOUSE_BUTTON && param2 == GFG_UP )
	{
		//uvolneni tlacitka mysi
		for( riterContElem_t iter = m_pContainer->getComponentListRBegin(); iter != m_pContainer->getComponentListREnd(); (iter)++ )
		{
			if(	(*iter)->m_pComponent->inputController( type, x, y, param1, param2 )  )
			{
				if( (*iter)->m_pComponent != m_pSelected   )
				{	//pokud se kliklo na nevybrane tlacitko, tak ho nastavime jako oznacene
					RadioButton *tmpOption = reinterpret_cast<RadioButton*>( m_pSelected );
					m_pSelected = (*iter)->m_pComponent;
					tmpOption->setSwitched( false );	//a posledne vybrane odznacime
				}
				else
				{
					//zde se nema nic stat, ale...
					//pokud se kliklo na jiz vybrane, tak ho musime prepnout, protoze po kliknuti se
					//preplo do stavu nevybrane
					RadioButton *tmpOption = reinterpret_cast<RadioButton*>( (*iter)->m_pComponent );
					tmpOption->setSwitched( true );
				}

				m_pContainer->changeFocusedItem( *iter, 0 );
				return true;
			}
		}
		return false;
		
	}
	else	//ostatni udalosti se predavaji
		return m_pContainer->inputController( type, x, y, param1, param2 );

	return false;
}

Option::~Option()
{
	delete m_pContainer;
}

RadioButton *Option::selectedOption()
{
	if( m_pSelected )
		return reinterpret_cast<RadioButton*>( m_pSelected );
	return NULL;
}
