#include "CCheckBox.h"
#include "../Gui.h"
#include "../../Cvar.h"

CCheckBox::CCheckBox(int X, int Y, std::string TEXT, std::string VAR, std::string Name) : CElement(CPos(0,0), CPos(X,Y), 0, 0, Name), Text(TEXT), VarName(VAR)
{
	this->SetWidth( this->GetFont()->GetTextLenght( this->GetText().c_str()) + 17 );
	this->SetHeight( this->GetFont()->GetTextHeight() );
	if((float)GetCvarValue(this->VarName.c_str()) > 0)
		this->SetChecked(true);
	else
		this->SetChecked(false);
}


void CCheckBox::Draw()
{
	this->GetFont()->InitVolatileResources();

	Pos = *this->GetParent()->GetAbsPos() + *this->GetRelPos();

	if((float)GetCvarValue(this->VarName.c_str()) > 0)
		this->curState = myGui->CheckBox->m_States["Pressed"];
	else if((float)GetCvarValue(this->VarName.c_str()) <= 0)
		this->curState = myGui->CheckBox->m_States["Norm"];

	this->CheckBox = this->curState->GetTexture("CheckBox");

	this->CheckBox->Draw( Pos, 12, 12 );

/*	if(this->GetMouseOver())
		myGui->CheckBox->m_States["MouseOver"]->GetTexture("CheckBox")->Draw( Pos, 12, 12, 50);*/

	this->GetFont()->DrawText( Pos.GetX()+17, Pos.GetY()-1, BLACK, NULL, this->GetText().c_str() );
	this->GetFont()->FreeVolatileResources();
}

void CCheckBox::MouseMove( CMouse *Mouse )
{
	this->SetMouseOver( myGui->getMouse()->IsInArea( Pos.GetX(), Pos.GetY(), this->GetWidth(), this->GetHeight() ) );
}

void CCheckBox::KeyEvent( SKey Key )
{
	if( !Key.m_Key )
	{
		if(this->GetMouseOver() && myGui->getMouse()->GetLButton())
		{
			this->SetChecked(!this->GetChecked());
			SetCvarValue(this->VarName.c_str(), (float)GetChecked());
		}
	}
}

std::string CCheckBox::GetVarName()
{
	return this->VarName;
}

int CCheckBox::GetChecked()
{
	return this->m_Checked;
}

void CCheckBox::SetChecked( bool State )
{
	this->m_Checked = State;
}

std::string CCheckBox::GetText()
{
	return this->Text;
}
