#include "Image.h"

#include "menumanager.h"

Image::Image(MenuManager*manager,DWORD id) : GUIElement(manager,id)
{
	m_Texture = NULL;
	m_AlphaTest = false;
	m_AlphaBlend = false;
	m_Opacity = 1;
}

Image::~Image(void)
{
	SAFERELEASE(m_Texture);
}

void Image::OnDraw(void)
{
	if(!m_Texture || !m_Texture->GetSurface() || !GetManager()->GetShader()->IsCompiled() || m_Opacity <= 0)
		return;

	DXVector2 dpos = GetDrawPos();

	float xl = GetXl();
	float yl = GetYl();
	if(xl == -1)
		xl = m_Texture->GetXl();
	if(yl == -1)
		yl = m_Texture->GetYl();

	MenuManager::TexVertex *vb = (MenuManager::TexVertex*)GetManager()->GetVB()->LockVB(sizeof(MenuManager::TexVertex)*4);

	vb->pos = DXVector4(dpos.x - 0.5f,dpos.y - 0.5f,0,1);
	vb->u = 0; vb++->v = 0;

	vb->pos = DXVector4(dpos.x+xl - 0.5f,dpos.y - 0.5f,0,1);
	vb->u = 1; vb++->v = 0;

	vb->pos = DXVector4(dpos.x - 0.5f,dpos.y+yl - 0.5f,0,1);
	vb->u = 0; vb++->v = 1;

	vb->pos = DXVector4(dpos.x+xl - 0.5f,dpos.y+yl - 0.5f,0,1);
	vb->u = 1; vb++->v = 1;

	GetManager()->GetVB()->UnlockVB();
	GetManager()->GetVB()->SetVBStream(sizeof(MenuManager::TexVertex), 0);

	GetManager()->SetupRenderTex(m_AlphaTest, m_AlphaBlend, m_Texture);
	GetManager()->GetShader()->SetVector("Color", DXVector4(1, 1, 1, m_Opacity));
	GetManager()->GetShader()->SetTechnique("RenderTexColor");

	RENDERFX(GetManager()->GetShader(),
		GetManager()->GetDevice()->DrawPrimitive(PT_TRIANGLESTRIP,0,2);
	);
}

void Image::SetTexture(DXTexture*tex)
{
	SAFERELEASE(m_Texture);

	m_Texture = tex;
}