#include "Label.h"

Label::Label(MenuManager*manager,DWORD id) : GUIElement(manager,id)
{
	m_TextSize.x = 0.f;
	m_TextSize.y = 0.f;
	m_TextAlign = AL_LEFTBOTTOM;
	m_Static = false;
	m_Texture = NULL;
	m_Color = 0xffffffff;
	m_Font = NULL;
	m_TextureOutdated = false;
}

Label::~Label(void)
{
//	SAFERELEASE(m_Font);
}

void Label::OnDraw(void)
{
	if(!m_Font)
		return;

	DXVector2 dpos = GetDrawPos();

	switch(m_TextAlign)
	{
		case AL_RIGHTBOTTOM: dpos.x += GetXl() - m_TextSize.x; dpos.y += GetYl() - m_TextSize.y; break;
		case AL_RIGHTCENTER: dpos.x += GetXl() - m_TextSize.x; dpos.y += GetYl()/2 - m_TextSize.y/2; break;
		case AL_RIGHTTOP: dpos.x += GetXl() - m_TextSize.x; break;
		case AL_LEFTBOTTOM: dpos.y += GetYl() - m_TextSize.y; break;
		case AL_LEFTCENTER: dpos.y += GetYl()/2 - m_TextSize.y/2; break;
		case AL_LEFTTOP: break;
		case AL_CENTERBOTTOM: dpos.x += GetXl()/2 - m_TextSize.x/2; dpos.y += GetYl() - m_TextSize.y; break;
		case AL_CENTER: dpos.x += GetXl()/2 - m_TextSize.x/2; dpos.y += GetYl()/2 - m_TextSize.y/2; break;
		case AL_CENTERTOP: dpos.x += GetXl()/2 - m_TextSize.x/2; break;
		default: break;
	}

	if(m_Static)
	{
		if(m_TextureOutdated)
			BuildTexture();	
	}
	else
	{
		m_Font->Draw(m_Text, (int)dpos.x, (int)dpos.y, m_Color, GetXl());
	}
}

void Label::OnUpdate(void)
{
	
}

void Label::SetFont(Font*font)
{
	SAFERELEASE(m_Font);

	m_Font = font;

	if (m_Font)
		m_TextSize = m_Font->TextSize(m_Text);
	
	if(m_Static)
		m_TextureOutdated = true;
}

void Label::SetText(const XString&text)
{
	m_Text = text;

	if(m_Font)
		m_TextSize = m_Font->TextSize(m_Text);
	else
		m_TextSize = DXVector2(0,0);

	if(m_Static)
		m_TextureOutdated = true;
}

void Label::BuildTexture(void)
{
	
}