// ErfElemInfoView.cpp : implementation file
//

#include "stdafx.h"
#include "MFC Gos.h"
#include "ErfElemInfoView.h"
#include "MFC GosDoc.h"
#include "MFC GosView.h"


#include <GameOS\GameOS.hpp>
#include <ElementRenderer\ElementRenderer.hpp>
#include <ElementRenderer\Element.hpp>
#include <ElementRenderer\StateChange.hpp>
#include <ElementRenderer\ShapeLODElement.hpp>
#include <ElementRenderer\TreeElement.hpp>

#include <MLR\MLR.hpp>
#include <MLR\MLR_I_C_PMesh.hpp>
#include <MLR\MLR_I_DT_PMesh.hpp>
#include <MLR\MLR_I_C_DT_PMesh.hpp>



/////////////////////////////////////////////////////////////////////////////
// CErfElemInfoView

IMPLEMENT_DYNCREATE(CErfElemInfoView, CView)

CErfElemInfoView::CErfElemInfoView()
{
	InfoCtrl=NULL;
	
}

CErfElemInfoView::~CErfElemInfoView()
{
	if(InfoCtrl)
	{
		delete InfoCtrl;
	}
}


BEGIN_MESSAGE_MAP(CErfElemInfoView, CView)
//{{AFX_MSG_MAP(CErfElemInfoView)
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CErfElemInfoView drawing

void CErfElemInfoView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CErfElemInfoView diagnostics

#ifdef _DEBUG
void CErfElemInfoView::AssertValid() const
{
	CView::AssertValid();
}

void CErfElemInfoView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CErfElemInfoView message handlers

BOOL CErfElemInfoView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

void CErfElemInfoView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	if(!InfoCtrl)
	{
		CRect rct;
		GetClientRect(&rct);
		InfoCtrl=new CErfListBox;
		InfoCtrl->Create(LBS_NOTIFY|WS_CHILD|WS_VISIBLE|WS_VSCROLL,rct,this,NULL);   
		InfoCtrl->ShowWindow(SW_SHOW);
		InfoCtrl->MyView=this;
	}
	else
	{
		InfoCtrl->ResetContent();
	}
	// TODO: Add your specialized code here and/or call the base class
	
}

void CErfElemInfoView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	if(InfoCtrl)
	{
		InfoCtrl->SetWindowPos(NULL,0,0,cx,cy,SWP_NOZORDER|SWP_NOMOVE|SWP_SHOWWINDOW);     
	}
	// TODO: Add your message handler code here
	
}


void CErfElemInfoView::UpdateInfo(ElementRenderer::Element *elmt)
{
	CString tstr;
	
	InfoCtrl->AddString("Element");
	
	tstr.Format("LocalToParent Translation = %.2f %.2f %.2f", 
		elmt->GetLocalToParent().operator()(3,0),
		elmt->GetLocalToParent().operator()(3,1),
		elmt->GetLocalToParent().operator()(3,2)
		);
	InfoCtrl->AddString(tstr);
	
	tstr.Format("LocalToWorld Translation = %.2f %.2f %.2f", 
		elmt->GetLocalToWorld().operator()(3,0),
		elmt->GetLocalToWorld().operator()(3,1),
		elmt->GetLocalToWorld().operator()(3,2)
		);
	InfoCtrl->AddString(tstr);
	
	tstr.Format("IsLocalToParentIdentity = %s",
		elmt->IsLocalToParentIdentity() ? "True" : "False");
	InfoCtrl->AddString(tstr);
	
	tstr.Format("AreBoundsLocked = %s",
		elmt->AreBoundsLocked() ? "True" : "False");
	InfoCtrl->AddString(tstr);
	
	tstr.Format("IsMatrixDirty = %s",
		elmt->IsMatrixDirty() ? "True" : "False");
	InfoCtrl->AddString(tstr);
	
	tstr.Format("AreBoundsWrong = %s",
		elmt->AreBoundsWrong() ? "True" : "False");
	InfoCtrl->AddString(tstr);
	
	tstr.Format("IsSyncDeferred = %s",
		elmt->IsSyncDeferred() ? "True" : "False");
	InfoCtrl->AddString(tstr);
	
	tstr.Format("IsBoundedByOBB = %s",
		elmt->IsBoundedByOBB() ? "True" : "False");
	InfoCtrl->AddString(tstr);
	
	tstr.Format("CullMode = %s",
		elmt->GetCullMode() == ElementRenderer::Element::RootMode ? "RootMode" : 
	elmt->GetCullMode() == ElementRenderer::Element::VolumeCullMode ? "VolumeCullMode" : 
	elmt->GetCullMode() == ElementRenderer::Element::NeverCullMode ? "NeverCullMode" : "AlwaysCullMode");
	InfoCtrl->AddString(tstr);
	
	tstr.Format("IsAlignX = %s",
		elmt->IsAlignX() ? "True" : "False");
	InfoCtrl->AddString(tstr);
	
	tstr.Format("IsAlignY = %s",
		elmt->IsAlignY() ? "True" : "False");
	InfoCtrl->AddString(tstr);
	
	if (elmt->GetCullMode() == ElementRenderer::Element::VolumeCullMode)
	{
		tstr.Format("Local CullSphereCenter = %.2f %.2f %.2f", 
			elmt->m_localOBB.localToParent(3,0),
			elmt->m_localOBB.localToParent(3,1),
			elmt->m_localOBB.localToParent(3,2)
			);
		InfoCtrl->AddString(tstr);
		
		tstr.Format("Local CullSphereRadius = %.2f", 
			elmt->m_localOBB.sphereRadius
			);
		InfoCtrl->AddString(tstr);
		
		if (elmt->IsBoundedByOBB())
		{
			tstr.Format("Local Axis Extents = %.2f %.2f %.2f", 
				elmt->m_localOBB.axisExtents.x,
				elmt->m_localOBB.axisExtents.y,
				elmt->m_localOBB.axisExtents.z
				);
			InfoCtrl->AddString(tstr);
		}
		
		tstr.Format("World CullSphereCenter = %.2f %.2f %.2f", 
			elmt->GetWorldOBB().localToParent(3,0),
			elmt->GetWorldOBB().localToParent(3,1),
			elmt->GetWorldOBB().localToParent(3,2)
			);
		InfoCtrl->AddString(tstr);
		
		tstr.Format("World CullSphereRadius = %.2f", 
			elmt->GetWorldOBB().sphereRadius
			);
		InfoCtrl->AddString(tstr);
		
		if (elmt->IsBoundedByOBB())
		{
			tstr.Format("World Axis Extents = %.2f %.2f %.2f", 
				elmt->GetWorldOBB().axisExtents.x,
				elmt->GetWorldOBB().axisExtents.y,
				elmt->GetWorldOBB().axisExtents.z
				);
			InfoCtrl->AddString(tstr);
		}
	}
	
	if (elmt->IsDerivedFrom(ElementRenderer::ShapeLODElementClassID))
	{
		ElementRenderer::ShapeLODElement *shape = Cast_Pointer(ElementRenderer::ShapeLODElement *,elmt);
		int size = shape->GetSize();
		for (int x=0;x<size;x++)
		{
			const ElementRenderer::LODElement::Entry *entry;
			MidLevelRenderer::MLRShape *mlrshape;
			shape->GetLOD(x,&mlrshape,&entry);
			tstr.Format("LOD %d Range = %.2f %.2f",x,sqrt(entry->m_nearSquared),sqrt(entry->m_farSquared));
			int itm;
			itm=InfoCtrl->AddString(tstr);
			CEditMessage *em=new CEditMessage;
			em->EType=CEditMessage::EM_LOD;
			em->Data=elmt;
			em->Index=x;
			InfoCtrl->SetItemDataPtr(itm,em);
		}
		
	}
	if (elmt->IsDerivedFrom(ElementRenderer::TreeElementClassID))
	{
		ElementRenderer::TreeElement *shape = Cast_Pointer(ElementRenderer::TreeElement *,elmt);
		int size = shape->GetSize();
		for (int x=0;x<size;x++)
		{
			const ElementRenderer::TreeElement::Entry *entry;
			MidLevelRenderer::MLRShape *mlrshape;
			shape->GetLOD(x,&mlrshape,&entry);
			tstr.Format("LOD %d Range = %.2f %.2f %.2f %.2f",
				x,
				sqrt(entry->m_nearSquared),sqrt(entry->m_fadeInSquared),
				sqrt(entry->m_fadeOutSquared),sqrt(entry->m_farSquared));
			int itm;
			itm=InfoCtrl->AddString(tstr);
			CEditMessage *em=new CEditMessage;
			em->EType=CEditMessage::EM_LOD;
			em->Data=elmt;
			em->Index=x;
			InfoCtrl->SetItemDataPtr(itm,em);
		}

		tstr.Format("Start Aligning At = LOD %d", shape->m_startAligningAt);
		int itm;
		itm=InfoCtrl->AddString(tstr);
		CEditMessage *em1=new CEditMessage;
		em1->EType=CEditMessage::EM_BILLBOARD;
		em1->Data=elmt;
		em1->Index=x;
		InfoCtrl->SetItemDataPtr(itm,em1);
		
		tstr.Format("Alignment Type = %s%s", 
			shape->m_alignType&ElementRenderer::Element::AlignXFlag?"X":"",
			shape->m_alignType&ElementRenderer::Element::AlignYFlag?"Y":"");
		itm=InfoCtrl->AddString(tstr);
		CEditMessage *em2=new CEditMessage;
		em2->EType=CEditMessage::EM_BILLBOARD;
		em2->Data=elmt;
		em2->Index=x;
		InfoCtrl->SetItemDataPtr(itm,em2);
	}

	ElementRenderer::StateChange	*stateChange;
	if((stateChange = elmt->GetStateChange()) != NULL)
	{
		UpdateInfo(&(stateChange->GetMLRState()));
	}
	
	/*
	switch(elmt->GetClassID())
	{
	case ElementRenderer::FXElementClassID:
	{
	}
	break;
	
	  case ElementRenderer::GroupElementClassID:
	  {	
	  ElementRenderer::GroupElement *group = Cast_Pointer(ElementRenderer::GroupElement *, elmt);
	  
		Stuff::ChainIteratorOf<ElementRenderer::Element*> *children = group->MakeIterator();
		Register_Object(children);
		
		  ElementRenderer::Element *child = NULL;
		  while ((child = children->ReadAndNext()) != NULL)
		  {
		  AddErfBranch(tmpitm, child);
		  }
		  Unregister_Object(children);
		  delete children;
		  }
		  break;
		  
			case ElementRenderer::ListElementClassID:
			{
			ElementRenderer::ListElement *group = Cast_Pointer(ElementRenderer::ListElement *, elmt);
			
			  for(int i=0;i<group->GetActiveCount();i++)
			  {
			  AddErfBranch(tmpitm,group->GetIndexedElement(i));
			  }				
			  }
			  break;
			  
				case ElementRenderer::LODElementClassID:
				{
				ElementRenderer::ListElement *group = Cast_Pointer(ElementRenderer::ListElement *, elmt);
				
				  for(int i=0;i<group->GetActiveCount();i++)
				  {
				  AddErfBranch(tmpitm,group->GetIndexedElement(i));
				  }				
				  }
				  break;
				  
					case ElementRenderer::PointCloudElementClassID:
					{
					}
					break;
					
					  case ElementRenderer::ScalableShapeElementClassID:
					  {
					  ElementRenderer::ShapeElement *group = Cast_Pointer(ElementRenderer::ShapeElement *, elmt);
					  MidLevelRenderer::MLRShape *shape=group->GetMLRShape();	
					  
						for(int i=0;i<shape->GetNum();i++)
						{
						AddErfBranch(tmpitm,shape->Find(i));
						}				
						}
						break;
						
						  case ElementRenderer::ScreenQuadsElementClassID:
						  {
						  }
						  break;
						  
							case ElementRenderer::ShapeElementClassID:
							{
							ElementRenderer::ShapeElement *group = Cast_Pointer(ElementRenderer::ShapeElement *, elmt);
							MidLevelRenderer::MLRShape *shape=group->GetMLRShape();	
							
							  for(int i=0;i<shape->GetNum();i++)
							  {
							  AddErfBranch(tmpitm,shape->Find(i));
							  }				
							  
								}
								break;
								
								  case ElementRenderer::SwitchElementClassID:
								  {
								  ElementRenderer::ListElement *group = Cast_Pointer(ElementRenderer::ListElement *, elmt);
								  
									for(int i=0;i<group->GetActiveCount();i++)
									{
									AddErfBranch(tmpitm,group->GetIndexedElement(i));
									}				
									}
									break;
									
									  case ElementRenderer::TriangleCloudElementClassID:
									  {
									  }
									  break;
									  }
									  */
}

void CErfElemInfoView::UpdateInfo(MidLevelRenderer::MLRPrimitiveBase *prim)
{
	InfoCtrl->AddString("MLR");
	
	int i;
	CString tstr;
	
	int point_count, index_count, color_count;
	const BYTE *index_array;
	const ColorType *color_array;
	
	tstr.Format("Layers = %d", prim->GetNumPasses());
	InfoCtrl->AddString(tstr);
	point_count = prim->GetNumVertices();
	tstr.Format("Vertices = %d", point_count);
	InfoCtrl->AddString(tstr);
	const MidLevelRenderer::Vector2DScalar *array;
	int tex_coord_count;
	prim->GetTexCoordData(&array, &tex_coord_count);
	tstr.Format("Tex. coords = %d", tex_coord_count);
	InfoCtrl->AddString(tstr);
	
	switch(prim->GetClassID())
	{
	case MidLevelRenderer::MLR_I_PMeshClassID:
		{
			MidLevelRenderer::MLR_I_PMesh *mesh = Cast_Pointer(MidLevelRenderer::MLR_I_PMesh*, prim);
			
			tstr.Format("Polygons = %d", mesh->GetNumPrimitives());
			InfoCtrl->AddString(tstr);
			mesh->GetIndexData(&index_array, &index_count);
			tstr.Format("Indices = %d", index_count);
			InfoCtrl->AddString(tstr);
			tstr.Format("Vertex/Index ratio = %f", (float)index_count/(float)point_count);
			InfoCtrl->AddString(tstr);
			
		}
		break;
		
	case MidLevelRenderer::MLR_I_C_PMeshClassID:
	case MidLevelRenderer::MLR_I_L_PMeshClassID:
		{
			MidLevelRenderer::MLR_I_C_PMesh *mesh = Cast_Pointer(MidLevelRenderer::MLR_I_C_PMesh*, prim);
			
			tstr.Format("Polygons = %d", mesh->GetNumPrimitives());
			InfoCtrl->AddString(tstr);
			mesh->GetIndexData(&index_array, &index_count);
			tstr.Format("Indices = %d", index_count);
			InfoCtrl->AddString(tstr);
			tstr.Format("Vertex/Index ratio = %f", (float)index_count/(float)point_count);
			InfoCtrl->AddString(tstr);
			mesh->GetColorData(&color_array, &color_count);
			tstr.Format("Colored Vertices = %d", color_count);
			InfoCtrl->AddString(tstr);
		}
		break;
		
	case MidLevelRenderer::MLR_I_DT_PMeshClassID:
		{
			MidLevelRenderer::MLR_I_DT_PMesh *mesh = Cast_Pointer(MidLevelRenderer::MLR_I_DT_PMesh*, prim);
			
			tstr.Format("Polygons = %d", mesh->GetNumPrimitives());
			InfoCtrl->AddString(tstr);
			mesh->GetIndexData(&index_array, &index_count);
			tstr.Format("Indices = %d", index_count);
			InfoCtrl->AddString(tstr);
			tstr.Format("Vertex/Index ratio = %f", (float)index_count/(float)point_count);
			InfoCtrl->AddString(tstr);
		}
		break;
		
	case MidLevelRenderer::MLR_I_C_DT_PMeshClassID:
	case MidLevelRenderer::MLR_I_L_DT_PMeshClassID:
		{
			MidLevelRenderer::MLR_I_C_DT_PMesh *mesh = Cast_Pointer(MidLevelRenderer::MLR_I_C_DT_PMesh*, prim);
			
			tstr.Format("Polygons = %d", mesh->GetNumPrimitives());
			InfoCtrl->AddString(tstr);
			mesh->GetIndexData(&index_array, &index_count);
			tstr.Format("Indices = %d", index_count);
			InfoCtrl->AddString(tstr);
			tstr.Format("Vertex/Index ratio = %f", (float)index_count/(float)point_count);
			InfoCtrl->AddString(tstr);
			mesh->GetColorData(&color_array, &color_count);
			tstr.Format("Colored Vertices = %d", color_count);
			InfoCtrl->AddString(tstr);
		}
		break;
	}
	
	InfoCtrl->AddString("");
	MidLevelRenderer::MLRState ref_state;
	MidLevelRenderer::MLRStateBase state;
	for(i=0;i<prim->GetNumPasses();i++)
	{
		tstr.Format("ReferenceState #%d:", i);
		InfoCtrl->AddString(tstr);
		ref_state = prim->GetReferenceState(i);
		UpdateInfo(&ref_state);
		InfoCtrl->AddString("");

		tstr.Format("Current State #%d:", i);
		InfoCtrl->AddString(tstr);
		state = prim->GetCurrentState(i);
		UpdateInfo(&state);
		InfoCtrl->AddString("");
	}
	
}

void CErfElemInfoView::UpdateInfo(const MidLevelRenderer::MLRState *state)
{
	CString tstr,tstr2;
	MidLevelRenderer::MLRTexture *tex = NULL;
	if	(MidLevelRenderer::MLRTexturePool::Instance &&
		(state->GetTextureHandle() < MidLevelRenderer::Limits::Max_Number_Textures * (1<<MidLevelRenderer::MLRTexturePool::Instance->GetInstanceDepth()))
		)
	{
		if(state->GetTextureHandle())
		{
			if((tex = (*MidLevelRenderer::MLRTexturePool::Instance)[state]) != NULL)
			{
				tstr.Format("%c%c Texture=%s",
					(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::TextureMask) ? 'D' : '0',
					(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::TextureMask) ? 'P' : '0',
					tex->GetTextureName());
				InfoCtrl->AddString(tstr);
			}
		}
		else
		{
			tstr.Format("%c%c Texture=None",
				(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::TextureMask) ? 'D' : '0',
				(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::TextureMask) ? 'P' : '0');
			InfoCtrl->AddString(tstr);
		}
	}
	else
	{
		InfoCtrl->AddString("?? Texture=Error");
	}
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::AlphaMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::AlphaMask) ? 'P' : '0');
	switch(state->GetAlphaMode())
	{
	case MidLevelRenderer::MLRState::OneZeroMode:
		tstr.Format("AlphaMode=OneZero");
		break;
	case MidLevelRenderer::MLRState::OneOneMode:
		tstr.Format("AlphaMode=OneOne");
		break;
	case MidLevelRenderer::MLRState::AlphaOneMode:
		tstr.Format("AlphaMode=AlphaOne");
		break;
	case MidLevelRenderer::MLRState::OneAlphaMode:
		tstr.Format("AlphaMode=OneAlpha");
		break;
	case MidLevelRenderer::MLRState::AlphaInvAlphaMode:
		tstr.Format("AlphaMode=AlphaInvAlpha");
		break;
	case MidLevelRenderer::MLRState::OneInvAlphaMode:
		tstr.Format("AlphaMode=OneInvAlpha");
		break;
	case MidLevelRenderer::MLRState::KeyedAlphaMode:
		tstr.Format("AlphaMode=KeyedAlphaMode");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::DitherMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::DitherMask) ? 'P' : '0');
	switch(state->GetDitherMode())
	{
	case MidLevelRenderer::MLRState::DitherOffMode:
		tstr.Format("Dithering=Off");
		break;
	case MidLevelRenderer::MLRState::DitherOnMode:
		tstr.Format("Dithering=On");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::FilterMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::FilterMask) ? 'P' : '0');
	switch(state->GetFilterMode())
	{
	case MidLevelRenderer::MLRState::NoFilterMode:
		tstr.Format("FilterMode=None");
		break;
	case MidLevelRenderer::MLRState::BiLinearFilterMode:
		tstr.Format("FilterMode=BiLinear");
		break;
	case MidLevelRenderer::MLRState::TriLinearFilterMode:
		tstr.Format("FilterMode=TriLinear");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::FlatColoringMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::FlatColoringMask) ? 'P' : '0');
	switch(state->GetFlatColoringMode())
	{
	case MidLevelRenderer::MLRState::FlatColoringOffMode:
		tstr.Format("FlatColoringing=Off");
		break;
	case MidLevelRenderer::MLRState::FlatColoringOnMode:
		tstr.Format("FlatColoringing=On");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::FogMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::FogMask) ? 'P' : '0');
	switch(state->GetFogMode())
	{
	case 0:
		tstr.Format("Fog=Disabled");
		break;
	case 1:
		tstr.Format("Fog=General");
		break;
	case 2:
		tstr.Format("Fog=Light");
		break;
	case 3:
		tstr.Format("Fog=Custom");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::SpecularMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::SpecularMask) ? 'P' : '0');
	switch(state->GetSpecularMode())
	{
	case MidLevelRenderer::MLRState::SpecularOffMode:
		tstr.Format("Specular=Off");
		break;
	case MidLevelRenderer::MLRState::SpecularOnMode:
		tstr.Format("Specular=On");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::TextureCorrectionMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::TextureCorrectionMask) ? 'P' : '0');
	switch(state->GetTextureCorrectionMode())
	{
	case MidLevelRenderer::MLRState::TextureCorrectionOffMode:
		tstr.Format("TextureCorrection=Off");
		break;
	case MidLevelRenderer::MLRState::TextureCorrectionOnMode:
		tstr.Format("TextureCorrection=On");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::TextureWrapMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::TextureWrapMask) ? 'P' : '0');
	switch(state->GetTextureWrapMode())
	{
	case MidLevelRenderer::MLRState::TextureClamp:
		tstr.Format("TextureWrap=Off");
		break;
	case MidLevelRenderer::MLRState::TextureWrap:
		tstr.Format("TextureWrap=On");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::WireFrameMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::WireFrameMask) ? 'P' : '0');
	switch(state->GetWireFrameMode())
	{
	case MidLevelRenderer::MLRState::WireFrameOffMode:
		tstr.Format("WireFrame=Off");
		break;
	case MidLevelRenderer::MLRState::WireFrameOnlyMode:
		tstr.Format("WireFrame=Only");
		break;
	case MidLevelRenderer::MLRState::WireFrameAddMode:
		tstr.Format("WireFrame=Add");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::ZBufferWriteMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::ZBufferWriteMask) ? 'P' : '0');
	switch(state->GetZBufferWriteMode())
	{
	case MidLevelRenderer::MLRState::ZBufferWriteOffMode:
		tstr.Format("ZBufferWrite=Off");
		break;
	case MidLevelRenderer::MLRState::ZBufferWriteOnMode:
		tstr.Format("ZBufferWrite=On");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetRenderDeltaMask() & MidLevelRenderer::MLRState::ZBufferCompareMask) ? 'D' : '0',
		(state->GetRenderPermissionMask() & MidLevelRenderer::MLRState::ZBufferCompareMask) ? 'P' : '0');
	switch(state->GetZBufferCompareMode())
	{
	case MidLevelRenderer::MLRState::ZBufferCompareOffMode:
		tstr.Format("ZBufferCompare=Off");
		break;
	case MidLevelRenderer::MLRState::ZBufferCompareOnMode:
		tstr.Format("ZBufferCompare=On");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	
	tstr2.Format("%c%c ",
		(state->GetProcessDeltaMask() & MidLevelRenderer::MLRState::BackFaceMask) ? 'D' : '0',
		(state->GetProcessPermissionMask() & MidLevelRenderer::MLRState::BackFaceMask) ? 'P' : '0');
	
	switch(state->GetBackFaceMode())
	{
	case MidLevelRenderer::MLRState::BackFaceOffMode:
		tstr.Format("BackFace=Off");
		break;
	case MidLevelRenderer::MLRState::BackFaceOnMode:
		tstr.Format("BackFace=On");
		break;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	tstr2.Format("%c%c ",
		(state->GetProcessDeltaMask() & MidLevelRenderer::MLRState::LightingMask) ? 'D' : '0',
		(state->GetProcessPermissionMask() & MidLevelRenderer::MLRState::LightingMask) ? 'P' : '0');
	
	tstr.Format("Lighting=");
	if(state->GetLightingMode())
	{
		if (state->GetLightingMode() & MidLevelRenderer::MLRState::TerrainLightingMode)
		{
			CString tstr3;
			tstr3.Format("LightMap+");
			tstr += tstr3;
		}
		if (state->GetLightingMode() & MidLevelRenderer::MLRState::LightMapLightingMode)
		{
			CString tstr3;
			tstr3.Format("LightMap+");
			tstr += tstr3;
		}
		if (state->GetLightingMode() & MidLevelRenderer::MLRState::VertexLightingMode)
		{
			CString tstr3;
			tstr3.Format("Vertex+");
			tstr += tstr3;
		}
		if (state->GetLightingMode() & MidLevelRenderer::MLRState::LookupLightingMode)
		{
			CString tstr3;
			tstr3.Format("Lookup+");
			tstr += tstr3;
		}
		if (state->GetLightingMode() & MidLevelRenderer::MLRState::FaceLightingMode)
		{
			CString tstr3;
			tstr3.Format("Face+");
			tstr += tstr3;
		}
	}
	else
	{
		CString tstr3;
		tstr3.Format("Off");
		tstr += tstr3;
	}
	tstr = tstr2+tstr;
	InfoCtrl->AddString(tstr);
	
	tstr.Format("%c%c Priority=%d",
		(state->GetProcessDeltaMask() & MidLevelRenderer::MLRState::PriorityMask) ? 'D' : '0',
		(state->GetProcessPermissionMask() & MidLevelRenderer::MLRState::PriorityMask) ? 'P' : '0',
		state->GetPriority());
	
	InfoCtrl->AddString(tstr);
	
}

void CErfElemInfoView::UpdateInfo(const MidLevelRenderer::MLRStateBase *state)
{
	CString tstr;
	MidLevelRenderer::MLRTexture *tex = NULL;
	if	(MidLevelRenderer::MLRTexturePool::Instance &&
		(state->GetTextureHandle() < MidLevelRenderer::Limits::Max_Number_Textures * (1<<MidLevelRenderer::MLRTexturePool::Instance->GetInstanceDepth()))
		)
	{
		if(state->GetTextureHandle())
		{
			if((tex = (*MidLevelRenderer::MLRTexturePool::Instance)[state]) != NULL)
			{
				tstr.Format("Texture=%s", tex->GetTextureName());
				InfoCtrl->AddString(tstr);
			}
		}
		else
		{
			tstr.Format("Texture=None");
			InfoCtrl->AddString(tstr);
		}
	}
	else
	{
		InfoCtrl->AddString("Texture=Error");
	}
	
	switch(state->GetAlphaMode())
	{
	case MidLevelRenderer::MLRState::OneZeroMode:
		tstr.Format("AlphaMode=OneZero");
		break;
	case MidLevelRenderer::MLRState::OneOneMode:
		tstr.Format("AlphaMode=OneOne");
		break;
	case MidLevelRenderer::MLRState::AlphaOneMode:
		tstr.Format("AlphaMode=AlphaOne");
		break;
	case MidLevelRenderer::MLRState::OneAlphaMode:
		tstr.Format("AlphaMode=OneAlpha");
		break;
	case MidLevelRenderer::MLRState::AlphaInvAlphaMode:
		tstr.Format("AlphaMode=AlphaInvAlpha");
		break;
	case MidLevelRenderer::MLRState::OneInvAlphaMode:
		tstr.Format("AlphaMode=OneInvAlpha");
		break;
	case MidLevelRenderer::MLRState::KeyedAlphaMode:
		tstr.Format("AlphaMode=KeyedAlphaMode");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	
	switch(state->GetDitherMode())
	{
	case MidLevelRenderer::MLRState::DitherOffMode:
		tstr.Format("Dithering=Off");
		break;
	case MidLevelRenderer::MLRState::DitherOnMode:
		tstr.Format("Dithering=On");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	
	switch(state->GetFilterMode())
	{
	case MidLevelRenderer::MLRState::NoFilterMode:
		tstr.Format("FilterMode=None");
		break;
	case MidLevelRenderer::MLRState::BiLinearFilterMode:
		tstr.Format("FilterMode=BiLinear");
		break;
	case MidLevelRenderer::MLRState::TriLinearFilterMode:
		tstr.Format("FilterMode=TriLinear");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	switch(state->GetFlatColoringMode())
	{
	case MidLevelRenderer::MLRState::FlatColoringOffMode:
		tstr.Format("FlatColoringing=Off");
		break;
	case MidLevelRenderer::MLRState::FlatColoringOnMode:
		tstr.Format("FlatColoringing=On");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	switch(state->GetFogMode())
	{
	case 0:
		tstr.Format("Fog=Disabled");
		break;
	case 1:
		tstr.Format("Fog=General");
		break;
	case 2:
		tstr.Format("Fog=Light");
		break;
	case 3:
		tstr.Format("Fog=Custom");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	switch(state->GetSpecularMode())
	{
	case MidLevelRenderer::MLRState::SpecularOffMode:
		tstr.Format("Specular=Off");
		break;
	case MidLevelRenderer::MLRState::SpecularOnMode:
		tstr.Format("Specular=On");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	switch(state->GetTextureCorrectionMode())
	{
	case MidLevelRenderer::MLRState::TextureCorrectionOffMode:
		tstr.Format("TextureCorrection=Off");
		break;
	case MidLevelRenderer::MLRState::TextureCorrectionOnMode:
		tstr.Format("TextureCorrection=On");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	switch(state->GetTextureWrapMode())
	{
	case MidLevelRenderer::MLRState::TextureClamp:
		tstr.Format("TextureWrap=Off");
		break;
	case MidLevelRenderer::MLRState::TextureWrap:
		tstr.Format("TextureWrap=On");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	switch(state->GetWireFrameMode())
	{
	case MidLevelRenderer::MLRState::WireFrameOffMode:
		tstr.Format("WireFrame=Off");
		break;
	case MidLevelRenderer::MLRState::WireFrameOnlyMode:
		tstr.Format("WireFrame=Only");
		break;
	case MidLevelRenderer::MLRState::WireFrameAddMode:
		tstr.Format("WireFrame=Add");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	switch(state->GetZBufferWriteMode())
	{
	case MidLevelRenderer::MLRState::ZBufferWriteOffMode:
		tstr.Format("ZBufferWrite=Off");
		break;
	case MidLevelRenderer::MLRState::ZBufferWriteOnMode:
		tstr.Format("ZBufferWrite=On");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	switch(state->GetZBufferCompareMode())
	{
	case MidLevelRenderer::MLRState::ZBufferCompareOffMode:
		tstr.Format("ZBufferCompare=Off");
		break;
	case MidLevelRenderer::MLRState::ZBufferCompareOnMode:
		tstr.Format("ZBufferCompare=On");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	switch(state->GetBackFaceMode())
	{
	case MidLevelRenderer::MLRState::BackFaceOffMode:
		tstr.Format("BackFace=Off");
		break;
	case MidLevelRenderer::MLRState::BackFaceOnMode:
		tstr.Format("BackFace=On");
		break;
	}
	InfoCtrl->AddString(tstr);
	
	tstr.Format("Lighting=");
	if(state->GetLightingMode())
	{
		if (state->GetLightingMode() & MidLevelRenderer::MLRState::TerrainLightingMode)
		{
			CString tstr3;
			tstr3.Format("LightMap+");
			tstr += tstr3;
		}
		if (state->GetLightingMode() & MidLevelRenderer::MLRState::LightMapLightingMode)
		{
			CString tstr3;
			tstr3.Format("LightMap+");
			tstr += tstr3;
		}
		if (state->GetLightingMode() & MidLevelRenderer::MLRState::VertexLightingMode)
		{
			CString tstr3;
			tstr3.Format("Vertex+");
			tstr += tstr3;
		}
		if (state->GetLightingMode() & MidLevelRenderer::MLRState::LookupLightingMode)
		{
			CString tstr3;
			tstr3.Format("Lookup+");
			tstr += tstr3;
		}
		if (state->GetLightingMode() & MidLevelRenderer::MLRState::FaceLightingMode)
		{
			CString tstr3;
			tstr3.Format("Face+");
			tstr += tstr3;
		}
	}
	else
	{
		CString tstr3;
		tstr3.Format("Off");
		tstr += tstr3;
	}
	InfoCtrl->AddString(tstr);
	
	tstr.Format("Priority=%d", state->GetPriority());
	InfoCtrl->AddString(tstr);
}

void CErfElemInfoView::UpdateInfo(ElementRenderer::StateChange *stateChange)
{
	const MidLevelRenderer::MLRState& mlrState = stateChange->GetMLRState();
	
	UpdateInfo(&mlrState);
}

void CErfElemInfoView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(!InfoCtrl) return;
	
	RefreshData(lHint?true:false);
}

BOOL CErfElemInfoView::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{	
	return CView::OnNotify(wParam, lParam, pResult);
}

BOOL CErfElemInfoView::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	return CView::OnCommand(wParam, lParam);
}

void CErfElemInfoView::RefreshData(bool leaveempty)
{
	CDocument* pDoc = GetDocument();
	CMFCGosDoc *GDoc=(CMFCGosDoc *)pDoc;
	
	for(int isl=0;isl<InfoCtrl->GetCount();isl++) delete InfoCtrl->GetItemDataPtr(isl);
	InfoCtrl->ResetContent();
	
	if(leaveempty) return;
	
	if(GDoc->cele)
	{
		UpdateInfo(GDoc->cele);
	}
	else
		if(GDoc->cmlr)
		{
			UpdateInfo(GDoc->cmlr);
		}
		else
		{
			InfoCtrl->AddString("No Data");
		} 
		

}

void CErfElemInfoView::SetLodValue(int level,float min,float max)
{
	CMFCGosView::Instance->erftree->SetLodValue(level,min,max);
}

void CErfElemInfoView::SetLodValue(int level,float min,float fadeIn,float fadeOut,float max)
{
	CMFCGosView::Instance->erftree->SetLodValue(level,min,fadeIn,fadeOut,max);
}

void CErfElemInfoView::SetAlign(int startAt,int alignType)
{
	CMFCGosView::Instance->erftree->SetAlign(startAt,alignType);
}
