#include "Panel.h"

#include "menumanager.h"

Panel::Panel(MenuManager*manager,DWORD id) : Image(manager,id)
{
	//m_Border = 0;
	//m_TextureBorder = 0;
	m_FixedBorder = 0;
}

Panel::~Panel(void)
{
}

void Panel::OnDraw(void)
{
	if(!GetTexture() || !GetTexture()->GetSurface())
		return;

	DXVector2 dpos = GetDrawPos();

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

	if (m_FixedBorder > 0)
	{
		MenuManager::TexVertex *vb = (MenuManager::TexVertex*)GetManager()->GetVB()->LockVB(sizeof(MenuManager::TexVertex)*16);

		float xfixed = (m_FixedBorder) / (float)GetTexture()->GetXl();
		float yfixed = (m_FixedBorder) / (float)GetTexture()->GetYl();

		vb->pos = DXVector4(dpos.x - 0.5f, dpos.y - 0.5f, 0, 1);						
		vb->u = 0; vb++->v = 0;
		vb->pos = DXVector4(dpos.x + m_FixedBorder - 0.5f, dpos.y - 0.5f, 0, 1);		
		vb->u = xfixed; vb++->v = 0;
		vb->pos = DXVector4(dpos.x + xl - m_FixedBorder - 0.5f, dpos.y - 0.5f, 0, 1); 
		vb->u = 1 - xfixed; 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 + m_FixedBorder - 0.5f, 0, 1);						
		vb->u = 0; vb++->v = yfixed;
		vb->pos = DXVector4(dpos.x + m_FixedBorder - 0.5f, dpos.y + m_FixedBorder - 0.5f, 0, 1);		
		vb->u = xfixed; vb++->v = yfixed;
		vb->pos = DXVector4(dpos.x + xl - m_FixedBorder - 0.5f, dpos.y + m_FixedBorder - 0.5f, 0, 1); 
		vb->u = 1 - xfixed; vb++->v = yfixed;
		vb->pos = DXVector4(dpos.x + xl - 0.5f, dpos.y + m_FixedBorder - 0.5f, 0, 1);					
		vb->u = 1; vb++->v = yfixed;

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

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

		WORD* ib = (WORD*)GetManager()->GetIB()->LockIB(sizeof(WORD) * 18*3);

		for(int y=0;y<3;y++)
		{

			for(int x=0;x<3;x++)
			{
				*ib++ = x + y*4;
				*ib++ = x+1 + y*4;
				*ib++ = x + (y+1)*4;

				*ib++ = x+1 + y*4;
				*ib++ = x+1 + (y+1)*4;
				*ib++ = x + (y+1)*4;
			}
		}

		GetManager()->GetIB()->UnlockIB();
		GetManager()->GetIB()->SetIndices();
	}
	else
	{
		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(),
		if (m_FixedBorder > 0)
		{
			GetManager()->GetDevice()->DrawIndexedPrimitive(PT_TRIANGLELIST, 0, 0, 16, GetManager()->GetIB()->GetIndexOffset() / sizeof(WORD), 18);
		}
		else
		{
			GetManager()->GetDevice()->DrawPrimitive(PT_TRIANGLESTRIP, 0, 2);
		}
	);
}