// EditMLRStateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFC Gos.h"
#include "EditMLRStateDlg.h"
#include <GameOS\ToolOS.hpp>


/////////////////////////////////////////////////////////////////////////////
// CEditMLRStateDlg dialog


CEditMLRStateDlg::CEditMLRStateDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEditMLRStateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEditMLRStateDlg)
	m_AlphaList = -1;
	m_BackFaceList = -1;
	m_DitherList = -1;
	m_FilterList = -1;
	m_LightList = -1;
	m_TCorrectList = -1;
	m_TextureList = -1;
	m_ZBufList = -1;
	m_WireFrameList = -1;
	m_FogList = -1;
	m_PriList = -1;
	m_PAlpha = FALSE;
	m_PBackFace = FALSE;
	m_PDither = FALSE;
	m_PFilter = FALSE;
	m_PFog = FALSE;
	m_PLight = FALSE;
	m_PPriority = FALSE;
	m_PTCorrect = FALSE;
	m_PTexture = FALSE;
	m_PWireFrame = FALSE;
	m_PZBuf = FALSE;
	m_PColoring = FALSE;
	m_PSpecular = FALSE;
	m_ColoringList = -1;
	m_SpecularList = -1;
	//}}AFX_DATA_INIT
	States=NULL;
	LastState=-1;
}


void CEditMLRStateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	
	
	//{{AFX_DATA_MAP(CEditMLRStateDlg)
	DDX_Control(pDX, IDC_STATELIST, m_StateList);
	DDX_Control(pDX, IDC_TEXTUREDSP, m_TextureText);
	DDX_CBIndex(pDX, IDC_ALPHALIST, m_AlphaList);
	DDX_CBIndex(pDX, IDC_BACKFACELIST, m_BackFaceList);
	DDX_CBIndex(pDX, IDC_DITHERLIST, m_DitherList);
	DDX_CBIndex(pDX, IDC_FILTERLIST, m_FilterList);
	DDX_CBIndex(pDX, IDC_LIGHTLIST, m_LightList);
	DDX_CBIndex(pDX, IDC_TCORRECTLIST, m_TCorrectList);
	DDX_CBIndex(pDX, IDC_TEXTURELIST, m_TextureList);
	DDX_CBIndex(pDX, IDC_ZBUFLIST, m_ZBufList);
	DDX_CBIndex(pDX, IDC_WIREFRAMELIST, m_WireFrameList);
	DDX_CBIndex(pDX, IDC_FOGLIST, m_FogList);
	DDX_CBIndex(pDX, IDC_PRILEVEL, m_PriList);
	DDX_Check(pDX, IDC_PER_ALPHA, m_PAlpha);
	DDX_Check(pDX, IDC_PER_BACKFACE, m_PBackFace);
	DDX_Check(pDX, IDC_PER_DITHER, m_PDither);
	DDX_Check(pDX, IDC_PER_FILTER, m_PFilter);
	DDX_Check(pDX, IDC_PER_FOG, m_PFog);
	DDX_Check(pDX, IDC_PER_LIGHT, m_PLight);
	DDX_Check(pDX, IDC_PER_PRI, m_PPriority);
	DDX_Check(pDX, IDC_PER_TCORRECT, m_PTCorrect);
	DDX_Check(pDX, IDC_PER_TEXTURE, m_PTexture);
	DDX_Check(pDX, IDC_PER_WIREFRAME, m_PWireFrame);
	DDX_Check(pDX, IDC_PER_ZBUF, m_PZBuf);
	DDX_Check(pDX, IDC_PER_COLORING, m_PColoring);
	DDX_Check(pDX, IDC_PER_SPECULAR, m_PSpecular);
	DDX_CBIndex(pDX, IDC_COLORINGLIST, m_ColoringList);
	DDX_CBIndex(pDX, IDC_SPECULARLIST, m_SpecularList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CEditMLRStateDlg, CDialog)
//{{AFX_MSG_MAP(CEditMLRStateDlg)
ON_BN_CLICKED(IDC_SAVEBUT, OnSavebut)
ON_LBN_SELCHANGE(IDC_STATELIST, OnSelchangeStatelist)
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditMLRStateDlg message handlers

BOOL CEditMLRStateDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CString tstr;
	int i;
	for(i=0;i<States->GetLength();i++)
	{
		tstr.Format("State#%i",i);
		m_StateList.InsertString(-1,tstr);
	} 
	
	
	LastState=0;
	m_StateList.SetCurSel(0);
	ReadState(0);
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CEditMLRStateDlg::ReadState(int stidx)
{
	Verify(stidx<States->GetLength());
	MidLevelRenderer::MLRState rstate;
	rstate=(*States)[stidx];
	MidLevelRenderer::MLRTexture *tex = NULL;
	
	if(rstate.GetTextureHandle() && ((tex = (*MidLevelRenderer::MLRTexturePool::Instance)[&rstate]) != NULL))
	{
		m_TextureText.SetWindowText(tex->GetTextureName());
	}
	else
		m_TextureText.SetWindowText("none");
	m_AlphaList=0;
	m_WireFrameList=0;
	m_FilterList=0;
	m_LightList=0;
	m_FogList=0;
	m_DitherList=0;
	m_TCorrectList=0;
	m_ZBufList=0;
	m_BackFaceList=0;
	m_PriList=0;
	m_SpecularList=0;
	m_ColoringList=0;
	
	int RDMask,PDMask;
	RDMask=rstate.GetRenderDeltaMask();
	PDMask=rstate.GetProcessDeltaMask();
	
	if(RDMask&MidLevelRenderer::MLRState::AlphaMask)
		switch(rstate.GetAlphaMode())
	{
	case MidLevelRenderer::MLRState::OneZeroMode:
		m_AlphaList=1;
		break;
	case MidLevelRenderer::MLRState::OneOneMode:
		m_AlphaList=2;
		break;
	case MidLevelRenderer::MLRState::AlphaOneMode:
		m_AlphaList=3;
		break;
	case MidLevelRenderer::MLRState::OneAlphaMode:
		m_AlphaList=4;
		break;
	case MidLevelRenderer::MLRState::AlphaInvAlphaMode:
		m_AlphaList=5;
		break;
	case MidLevelRenderer::MLRState::OneInvAlphaMode:
		m_AlphaList=6;
		break;
	case MidLevelRenderer::MLRState::KeyedAlphaMode:
		m_AlphaList=7;
		break;
	}	
	
	if(RDMask&MidLevelRenderer::MLRState::WireFrameMask)
		switch(rstate.GetWireFrameMode())
	{
	case MidLevelRenderer::MLRState::WireFrameOffMode:
		m_WireFrameList=1;
		break;
	case MidLevelRenderer::MLRState::WireFrameOnlyMode:
		m_WireFrameList=2;
		break;
	case MidLevelRenderer::MLRState::WireFrameAddMode:
		m_WireFrameList=3;
		break;
	}
	
	if(RDMask&MidLevelRenderer::MLRState::FilterMask)
		switch(rstate.GetFilterMode())
	{
	case MidLevelRenderer::MLRState::NoFilterMode:
		m_FilterList=1;
		break;
	case MidLevelRenderer::MLRState::BiLinearFilterMode:
		m_FilterList=2;
		break;
	case MidLevelRenderer::MLRState::TriLinearFilterMode:
		m_FilterList=3;
		break;
	}
	
	if(PDMask&MidLevelRenderer::MLRState::LightingMask)	
		if(	rstate.GetLightingMode()==MidLevelRenderer::MLRState::LightingOffMode)
			m_LightList=1;
		else
			if(	rstate.GetLightingMode()==(MidLevelRenderer::MLRState::VertexLightingMode|
				MidLevelRenderer::MLRState::FaceLightingMode|
				MidLevelRenderer::MLRState::LookupLightingMode))
				m_LightList=2;
			else
				if(	rstate.GetLightingMode()==(MidLevelRenderer::MLRState::VertexLightingMode|
					MidLevelRenderer::MLRState::FaceLightingMode))
					m_LightList=3;
				else
					if(	rstate.GetLightingMode()==(MidLevelRenderer::MLRState::LightMapLightingMode|
						MidLevelRenderer::MLRState::FaceLightingMode))
						m_LightList=4;
					else
						if(	rstate.GetLightingMode()==MidLevelRenderer::MLRState::LightMapLightingMode)
							m_LightList=5;
						else
							PAUSE(("Light Switch is in Invalid State"));
						
						
						
						if(RDMask&MidLevelRenderer::MLRState::FogMask)
							m_FogList=rstate.GetFogMode()+1;
						
						if(RDMask&MidLevelRenderer::MLRState::DitherMask)
							switch(rstate.GetDitherMode())
						{
	case MidLevelRenderer::MLRState::DitherOnMode:
		m_DitherList=1;
		break;
	case MidLevelRenderer::MLRState::DitherOffMode:
		m_DitherList=2;
		break;
						}
						
						if(RDMask&MidLevelRenderer::MLRState::TextureCorrectionMask)
							switch(rstate.GetTextureCorrectionMode())
						{
	case MidLevelRenderer::MLRState::TextureCorrectionOnMode:
		m_TCorrectList=1;
		break;
	case MidLevelRenderer::MLRState::TextureCorrectionOffMode:
		m_TCorrectList=2;
		break;
						}
						
						if(RDMask&MidLevelRenderer::MLRState::SpecularMask)
							switch(rstate.GetSpecularMode())
						{
	case MidLevelRenderer::MLRState::SpecularOnMode:
		m_SpecularList=1;
		break;
	case MidLevelRenderer::MLRState::SpecularOffMode:
		m_SpecularList=2;
		break;
						}
						
						if(RDMask&MidLevelRenderer::MLRState::FlatColoringMask)
							switch(rstate.GetFlatColoringMode())
						{
	case MidLevelRenderer::MLRState::FlatColoringOnMode:
		m_ColoringList=1;
		break;
	case MidLevelRenderer::MLRState::FlatColoringOffMode:
		m_ColoringList=2;
		break;
						}
						
						if( RDMask&(MidLevelRenderer::MLRState::ZBufferCompareMask|
							MidLevelRenderer::MLRState::ZBufferWriteMask) )
						{
							m_ZBufList=0;
							if(rstate.GetZBufferWriteMode()==MidLevelRenderer::MLRState::ZBufferWriteOnMode)
							{
								if(rstate.GetZBufferCompareMode()==MidLevelRenderer::MLRState::ZBufferCompareOnMode)
								{
									m_ZBufList=1;
								}
								else
								{
									m_ZBufList=2;					
								}
							}
							else
							{
								if(rstate.GetZBufferCompareMode()==MidLevelRenderer::MLRState::ZBufferCompareOnMode)
								{
									m_ZBufList=3;
								}
								else
								{
									m_ZBufList=4;					
								}
							}			
						}	
						
						if(PDMask&MidLevelRenderer::MLRState::BackFaceMask)
							m_BackFaceList=(rstate.GetBackFaceMode()==MidLevelRenderer::MLRState::BackFaceOnMode)?1:2;
						
						if(PDMask&MidLevelRenderer::MLRState::PriorityMask)	
							m_PriList=rstate.GetPriority()+1;
						
						int PerMask;
						PerMask=rstate.GetRenderPermissionMask();
						
						
						m_PAlpha=(PerMask&MidLevelRenderer::MLRState::AlphaMask)?TRUE:FALSE;
						m_PTexture=(PerMask&MidLevelRenderer::MLRState::TextureMask)?TRUE:FALSE;
						m_PFilter=(PerMask&MidLevelRenderer::MLRState::FilterMask)?TRUE:FALSE;
						m_PWireFrame=(PerMask&MidLevelRenderer::MLRState::WireFrameMask)?TRUE:FALSE;
						m_PFog=(PerMask&MidLevelRenderer::MLRState::FogMask)?TRUE:FALSE;
						m_PDither=(PerMask&MidLevelRenderer::MLRState::DitherMask)?TRUE:FALSE;
						m_PTCorrect=(PerMask&MidLevelRenderer::MLRState::TextureCorrectionMask)?TRUE:FALSE;
						m_PSpecular=(PerMask&MidLevelRenderer::MLRState::SpecularMask)?TRUE:FALSE;
						m_PColoring=(PerMask&MidLevelRenderer::MLRState::FlatColoringMask)?TRUE:FALSE;
						
						m_PLight=(rstate.GetProcessPermissionMask()&MidLevelRenderer::MLRState::LightingMask)?TRUE:FALSE;
						m_PBackFace=(rstate.GetProcessPermissionMask()&MidLevelRenderer::MLRState::BackFaceMask)?TRUE:FALSE;
						m_PPriority=(rstate.GetProcessPermissionMask()&MidLevelRenderer::MLRState::PriorityMask)?TRUE:FALSE;
						
						if((PerMask&MidLevelRenderer::MLRState::ZBufferCompareMask) && 
							(PerMask&MidLevelRenderer::MLRState::ZBufferWriteMask))
							m_PZBuf=TRUE;
						else
							m_PZBuf=FALSE;
						
						UpdateData(FALSE);
}

void CEditMLRStateDlg::OnSavebut() 
{
	UpdateState(m_StateList.GetCurSel());
	CDialog::OnOK();
}

void CEditMLRStateDlg::OnOK() 
{
	//	CDialog::OnOK();
}


void CEditMLRStateDlg::UpdateState(int idx)
{
	Verify(idx<States->GetLength());
	MidLevelRenderer::MLRState rstate;
	rstate=(*States)[idx];
	
	
	
	UpdateData(TRUE);
	//Set Delta Masks
	int RDMask,PDMask;
	RDMask=rstate.GetRenderDeltaMask();
	
	//	RDMask|=MidLevelRenderer::MLRState::TextureMask;
	RDMask|=MidLevelRenderer::MLRState::ZBufferCompareMask;
	RDMask|=MidLevelRenderer::MLRState::ZBufferWriteMask;
	RDMask|=MidLevelRenderer::MLRState::FilterMask;
	RDMask|=MidLevelRenderer::MLRState::WireFrameMask;
	RDMask|=MidLevelRenderer::MLRState::FogMask;
	RDMask|=MidLevelRenderer::MLRState::DitherMask;
	RDMask|=MidLevelRenderer::MLRState::TextureCorrectionMask;
	RDMask|=MidLevelRenderer::MLRState::SpecularMask;
	RDMask|=MidLevelRenderer::MLRState::FlatColoringMask;
	
	PDMask=rstate.GetProcessDeltaMask();
	PDMask|=MidLevelRenderer::MLRState::LightingMask;
	PDMask|=MidLevelRenderer::MLRState::BackFaceMask;
	PDMask|=MidLevelRenderer::MLRState::PriorityMask;
	
	//	RDMask=RDMask&~MidLevelRenderer::MLRState::TextureMask;

	CString texname;
	m_TextureText.GetWindowText(texname);

	MidLevelRenderer::MLRTexture *tex = NULL;
	if(rstate.GetTextureHandle() && ((tex = (*MidLevelRenderer::MLRTexturePool::Instance)[&rstate]) != NULL))
	{
		tex = MidLevelRenderer::MLRTexturePool::Instance->Add(texname);
		rstate.SetTextureHandle(tex->GetTextureHandle());
	}
	
	switch(m_AlphaList)
	{
	case 0:
		RDMask=RDMask&~MidLevelRenderer::MLRState::AlphaMask;
		break;
	case 1:
		rstate.SetAlphaMode(MidLevelRenderer::MLRState::OneZeroMode);
		RDMask|=MidLevelRenderer::MLRState::AlphaMask;
		break;
	case 2:
		rstate.SetAlphaMode(MidLevelRenderer::MLRState::OneOneMode);
		RDMask|=MidLevelRenderer::MLRState::AlphaMask;
		break;
	case 3:
		rstate.SetAlphaMode(MidLevelRenderer::MLRState::AlphaOneMode);
		RDMask|=MidLevelRenderer::MLRState::AlphaMask;
		break;
	case 4:
		rstate.SetAlphaMode(MidLevelRenderer::MLRState::OneAlphaMode);
		RDMask|=MidLevelRenderer::MLRState::AlphaMask;
		break;
	case 5:
		rstate.SetAlphaMode(MidLevelRenderer::MLRState::AlphaInvAlphaMode);
		RDMask|=MidLevelRenderer::MLRState::AlphaMask;
		break;
	case 6:
		rstate.SetAlphaMode(MidLevelRenderer::MLRState::OneInvAlphaMode);
		RDMask|=MidLevelRenderer::MLRState::AlphaMask;
		break;
	case 7:
		rstate.SetAlphaMode(MidLevelRenderer::MLRState::KeyedAlphaMode);
		RDMask|=MidLevelRenderer::MLRState::AlphaMask;
		break;
	}
	
	
	switch(m_WireFrameList)
	{
	case 0:
		RDMask=RDMask&~MidLevelRenderer::MLRState::WireFrameMask;
		break;
	case 1:
		rstate.SetWireFrameMode(MidLevelRenderer::MLRState::WireFrameOffMode);
		RDMask|=MidLevelRenderer::MLRState::WireFrameMask;
		break;
	case 2:
		rstate.SetWireFrameMode(MidLevelRenderer::MLRState::WireFrameOnlyMode);
		RDMask|=MidLevelRenderer::MLRState::WireFrameMask;
		break;
	case 3:
		rstate.SetWireFrameMode(MidLevelRenderer::MLRState::WireFrameAddMode);
		RDMask|=MidLevelRenderer::MLRState::WireFrameMask;
		break;
	}
	
	switch(m_FilterList)
	{
	case 0:
		RDMask=RDMask&~MidLevelRenderer::MLRState::FilterMask;
		break;
	case 1:
		rstate.SetFilterMode(MidLevelRenderer::MLRState::NoFilterMode);
		RDMask|=MidLevelRenderer::MLRState::FilterMask;
		break;
	case 2:
		rstate.SetFilterMode(MidLevelRenderer::MLRState::BiLinearFilterMode);
		RDMask|=MidLevelRenderer::MLRState::FilterMask;
		break;
	case 3:
		rstate.SetFilterMode(MidLevelRenderer::MLRState::TriLinearFilterMode);
		RDMask|=MidLevelRenderer::MLRState::FilterMask;
		break;
	}
	
	
	switch(m_LightList)
	{
	case 0:
		PDMask=PDMask^MidLevelRenderer::MLRState::LightingMask;
		break;
	case 1:
		rstate.SetLightingMode(MidLevelRenderer::MLRState::LightingOffMode);
		PDMask|=MidLevelRenderer::MLRState::LightingMask;
		break;
	case 2:
		rstate.SetLightingMode(	MidLevelRenderer::MLRState::VertexLightingMode|
			MidLevelRenderer::MLRState::FaceLightingMode|
			MidLevelRenderer::MLRState::LookupLightingMode);
		PDMask|=MidLevelRenderer::MLRState::LightingMask;
		break;
	case 3:
		rstate.SetLightingMode(MidLevelRenderer::MLRState::VertexLightingMode|
			MidLevelRenderer::MLRState::FaceLightingMode
			);
		PDMask|=MidLevelRenderer::MLRState::LightingMask;
		break;
	case 4:
		rstate.SetLightingMode(MidLevelRenderer::MLRState::LightMapLightingMode|
			MidLevelRenderer::MLRState::FaceLightingMode);
		PDMask|=MidLevelRenderer::MLRState::LightingMask;
		break;
	case 5:
		rstate.SetLightingMode(MidLevelRenderer::MLRState::LightMapLightingMode);
		PDMask|=MidLevelRenderer::MLRState::LightingMask;
		break;
	}
	
	if(m_FogList==0)
	{
		RDMask=RDMask&~MidLevelRenderer::MLRState::FogMask;
	}
	else
	{
		rstate.SetFogMode(m_FogList-1);
		RDMask|=MidLevelRenderer::MLRState::FogMask;
	}
	
	switch(m_DitherList)
	{
	case 0:
		RDMask=RDMask&~MidLevelRenderer::MLRState::DitherMask;
		break;
	case 1:
		rstate.SetDitherOn();
		RDMask|=MidLevelRenderer::MLRState::DitherMask;
		break;
	case 2:
		rstate.SetDitherOff();
		RDMask|=MidLevelRenderer::MLRState::DitherMask;
		break;
	}
	
	
	switch(m_TCorrectList)
	{
	case 0:
		RDMask=RDMask&~MidLevelRenderer::MLRState::TextureCorrectionMask;
		break;
	case 1:
		rstate.SetTextureCorrectionOn();
		RDMask|=MidLevelRenderer::MLRState::TextureCorrectionMask;
		break;
	case 2:
		rstate.SetTextureCorrectionOff();
		RDMask|=MidLevelRenderer::MLRState::TextureCorrectionMask;
		break;
	}
	
	switch(m_SpecularList)
	{
	case 0:
		RDMask=RDMask&~MidLevelRenderer::MLRState::SpecularMask;
		break;
	case 1:
		rstate.SetSpecularOn();
		RDMask|=MidLevelRenderer::MLRState::SpecularMask;
		break;
	case 2:
		rstate.SetSpecularOff();
		RDMask|=MidLevelRenderer::MLRState::SpecularMask;
		break;
	}
	
	switch(m_ColoringList)
	{
	case 0:
		RDMask=RDMask&~MidLevelRenderer::MLRState::FlatColoringMask;
		break;
	case 1:
		rstate.SetFlatColoringOn();
		RDMask|=MidLevelRenderer::MLRState::FlatColoringMask;
		break;
	case 2:
		rstate.SetFlatColoringOff();
		RDMask|=MidLevelRenderer::MLRState::FlatColoringMask;
		break;
	}
	
	
	switch(m_ZBufList)
	{
	case 0:
		RDMask=RDMask&~MidLevelRenderer::MLRState::ZBufferCompareMask;
		RDMask=RDMask&~MidLevelRenderer::MLRState::ZBufferWriteMask;
		break;
	case 1:
		rstate.SetZBufferCompareOn();
		rstate.SetZBufferWriteOn();
		RDMask|=MidLevelRenderer::MLRState::ZBufferCompareMask;
		RDMask|=MidLevelRenderer::MLRState::ZBufferWriteMask;
		break;
	case 2:
		rstate.SetZBufferCompareOn();
		rstate.SetZBufferWriteOff();
		RDMask|=MidLevelRenderer::MLRState::ZBufferCompareMask;
		RDMask|=MidLevelRenderer::MLRState::ZBufferWriteMask;
		break;
	case 3:
		rstate.SetZBufferCompareOff();
		rstate.SetZBufferWriteOn();
		RDMask|=MidLevelRenderer::MLRState::ZBufferCompareMask;
		RDMask|=MidLevelRenderer::MLRState::ZBufferWriteMask;
		break;
	case 4:
		rstate.SetZBufferCompareOff();
		rstate.SetZBufferWriteOff();
		RDMask|=MidLevelRenderer::MLRState::ZBufferCompareMask;
		RDMask|=MidLevelRenderer::MLRState::ZBufferWriteMask;
		break;
	}
	
	switch(m_BackFaceList)
	{
	case 0:
		PDMask=PDMask^MidLevelRenderer::MLRState::BackFaceMask;
		break;
	case 1:
		rstate.SetBackFaceOn();
		PDMask|=MidLevelRenderer::MLRState::BackFaceMask;
		break;
	case 2:
		rstate.SetBackFaceOff();
		PDMask|=MidLevelRenderer::MLRState::BackFaceMask;
		break;
	}
	
	if(m_PriList!=0)
	{
		rstate.SetPriority(m_PriList-1);
		PDMask|=MidLevelRenderer::MLRState::PriorityMask;
	}
	else
	{
		PDMask=PDMask^MidLevelRenderer::MLRState::PriorityMask;
	}
	
	rstate.SetRenderDeltaMask(RDMask);
	rstate.SetProcessDeltaMask(PDMask);
	
	
	//Set Permission Masks
	
	
	RDMask=rstate.GetRenderPermissionMask();
	
	RDMask=RDMask&~MidLevelRenderer::MLRState::TextureMask;
	RDMask=RDMask&~MidLevelRenderer::MLRState::ZBufferCompareMask;
	RDMask=RDMask&~MidLevelRenderer::MLRState::ZBufferWriteMask;
	RDMask=RDMask&~MidLevelRenderer::MLRState::AlphaMask;
	RDMask=RDMask&~MidLevelRenderer::MLRState::FilterMask;
	RDMask=RDMask&~MidLevelRenderer::MLRState::WireFrameMask;
	RDMask=RDMask&~MidLevelRenderer::MLRState::FogMask;
	RDMask=RDMask&~MidLevelRenderer::MLRState::DitherMask;
	RDMask=RDMask&~MidLevelRenderer::MLRState::TextureCorrectionMask;
	RDMask=RDMask&~MidLevelRenderer::MLRState::SpecularMask;
	RDMask=RDMask&~MidLevelRenderer::MLRState::FlatColoringMask;
	
	
	RDMask|=m_PTexture?MidLevelRenderer::MLRState::TextureMask:0;
	RDMask|=m_PZBuf?MidLevelRenderer::MLRState::ZBufferCompareMask:0;
	RDMask|=m_PZBuf?MidLevelRenderer::MLRState::ZBufferWriteMask:0;
	RDMask|=m_PAlpha?MidLevelRenderer::MLRState::AlphaMask:0;
	RDMask|=m_PFilter?MidLevelRenderer::MLRState::FilterMask:0;
	RDMask|=m_PWireFrame?MidLevelRenderer::MLRState::WireFrameMask:0;
	RDMask|=m_PFog?MidLevelRenderer::MLRState::FogMask:0;
	RDMask|=m_PDither?MidLevelRenderer::MLRState::DitherMask:0;
	RDMask|=m_PTCorrect?MidLevelRenderer::MLRState::TextureCorrectionMask:0;
	RDMask|=m_PSpecular?MidLevelRenderer::MLRState::SpecularMask:0;
	RDMask|=m_PColoring?MidLevelRenderer::MLRState::FlatColoringMask:0;
	
	rstate.SetRenderPermissionMask(RDMask);
	
	PDMask=rstate.GetProcessPermissionMask();
	
	
	PDMask=PDMask&~MidLevelRenderer::MLRState::LightingMask;
	PDMask=PDMask&~MidLevelRenderer::MLRState::BackFaceMask;
	PDMask=PDMask&~MidLevelRenderer::MLRState::PriorityMask;
	
	PDMask|=m_PLight?MidLevelRenderer::MLRState::LightingMask:0;
	PDMask|=m_PBackFace?MidLevelRenderer::MLRState::BackFaceMask:0;
	PDMask|=m_PPriority?MidLevelRenderer::MLRState::PriorityMask:0;
	
	rstate.SetProcessPermissionMask(PDMask);
	
	
	(*States)[idx]=rstate;
}

void CEditMLRStateDlg::SetStates(Stuff::DynamicArrayOf<MidLevelRenderer::MLRState> *stt)
{
	States=stt;
}

void CEditMLRStateDlg::OnSelchangeStatelist() 
{
	UpdateState(LastState);
	ReadState(m_StateList.GetCurSel());
	
}

extern Stuff::MString *erf_texture_path;

void CEditMLRStateDlg::OnBrowse() 
{
	CString texname;
	m_TextureText.GetWindowText(texname);
	char *path = *erf_texture_path;
	texname=CString(path)+texname;
	CString pngname = texname+".png";
	if (::gos_DoesFileExist(pngname))
	{
		texname = pngname;
	}
	else
		texname+=".tga";
	CFileDialog dlg(true,"*.png",texname,OFN_HIDEREADONLY,"Texture Files (*.png;*.tga)|*.png;*.tga||");
	if (IDOK==dlg.DoModal())
	{
		texname = dlg.GetFileTitle();
		m_TextureText.SetWindowText(texname);
	}
}
