// TexturePanel.cpp : implementation file
//

#include "stdafx.h"
#include "pixelwhippro.h"
#include "TexturePanel.h"
#include "TextureBrowser.h"



extern CPixelWhipProApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CTexturePanel dialog


CTexturePanel::CTexturePanel(CWnd* pParent /*=NULL*/)
	: CDynaPanel(CTexturePanel::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTexturePanel)
	m_TextureName = _T("");
	//}}AFX_DATA_INIT
}


void CTexturePanel::DoDataExchange(CDataExchange* pDX)
{
	CDynaPanel::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTexturePanel)
	DDX_Text(pDX, IDC_TEXTURENAME, m_TextureName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTexturePanel, CDynaPanel)
	//{{AFX_MSG_MAP(CTexturePanel)
	ON_BN_CLICKED(IDC_TEXTUREBROWSE, OnTexturebrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTexturePanel message handlers

void CTexturePanel::Set(void *dat)
{
CurrentEffect=(gosFX::Effect *)dat;
	if(CurrentEffect)
	{

		gosFX::Effect::Specification *spec=CurrentEffect->GetSpecification();
		Check_Object(spec);
		if(spec->m_state.GetTextureHandle()!=0)
		{
		m_TextureName=((*MidLevelRenderer::MLRTexturePool::Instance)[&spec->m_state])->GetTextureName();
		}
		else
		{
		m_TextureName="No Texture";

		}
	}

}

void CTexturePanel::OnTexturebrowse() 
{
CTextureBrowser dlg;

if(dlg.DoModal()==IDOK)
{
m_TextureName=dlg.m_TextureName;
theApp.Modified=true;


	if(CurrentEffect)
	{
	gosFX::Effect::Specification *spec=CurrentEffect->GetSpecification();
		Check_Object(spec);
/*
	int PState=spec->m_state.GetRenderPermissionMask(); 
	PState=PState&(~(MidLevelRenderer::MLRState::TextureMask));
	spec->m_state.SetRenderPermissionMask(PState);
*/	
	MidLevelRenderer::MLRTexture *tex=MidLevelRenderer::MLRTexturePool::Instance->Add((LPCSTR)m_TextureName);
	spec->m_state.SetTextureHandle(tex->GetTextureHandle());
	MidLevelRenderer::MLRTexturePool::Instance->LoadImages();
	}
UpdateData(FALSE);
}
	
}
