// ErfDataTree.cpp : implementation file
//

#include "stdafx.h"
#include "MFC Gos.h"
#include "ErfDataTree.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>

/////////////////////////////////////////////////////////////////////////////
// CErfDataTree

CErfDataTree::CErfDataTree()
{
}

void CErfDataTree::SetLodValue(int level,float min,float max,HTREEITEM itm)
{
	if(itm!=NULL && itm !=TVI_ROOT)
	{	
		Stuff::RegisteredClass *rcls;
		rcls=(Stuff::RegisteredClass *)GetItemData(itm);
		if(rcls != 0)
		{
			
			if(rcls->IsDerivedFrom(ElementRenderer::ElementClassID))
			{
				ElementRenderer::Element *elmt=Cast_Pointer(ElementRenderer::Element *, rcls);
				if (elmt->IsDerivedFrom(ElementRenderer::ShapeLODElementClassID))
				{
					ElementRenderer::ShapeLODElement *shape = Cast_Pointer(ElementRenderer::ShapeLODElement *,elmt);
					ElementRenderer::LODElement::Entry new_entry;
					new_entry.m_nearSquared=min*min;
					new_entry.m_farSquared=max*max;
					if(shape->GetSize()>level)
						shape->SetLOD(level,new_entry);
					
				}
			}
		}				
	}	

	HTREEITEM child=GetChildItem(itm);
	while(child!=NULL)
	{
		SetLodValue(level,min,max,child);
		child=GetNextSiblingItem(child);
	}
		
}

void CErfDataTree::SetLodValue(int level,float min,float fadeIn,float fadeOut,float max,HTREEITEM itm)
{
	if(itm!=NULL && itm !=TVI_ROOT)
	{	
		Stuff::RegisteredClass *rcls;
		rcls=(Stuff::RegisteredClass *)GetItemData(itm);
		if(rcls != 0)
		{
			
			if(rcls->IsDerivedFrom(ElementRenderer::ElementClassID))
			{
				ElementRenderer::Element *elmt=Cast_Pointer(ElementRenderer::Element *, rcls);
				if (elmt->IsDerivedFrom(ElementRenderer::TreeElementClassID))
				{
					ElementRenderer::TreeElement *shape = Cast_Pointer(ElementRenderer::TreeElement *,elmt);
					ElementRenderer::TreeElement::Entry new_entry;
					new_entry.m_nearSquared=min*min;
					new_entry.m_fadeInSquared=fadeIn*fadeIn;
					new_entry.m_fadeOutSquared=fadeOut*fadeOut;;
					new_entry.m_farSquared=max*max;
					if(shape->GetSize()>level)
						shape->SetLOD(level,new_entry);
					
				}
			}
		}				
	}	

	HTREEITEM child=GetChildItem(itm);
	while(child!=NULL)
	{
		SetLodValue(level,min,fadeIn,fadeOut,max,child);
		child=GetNextSiblingItem(child);
	}
		
}

void CErfDataTree::SetAlign(int startAt,int alignType,HTREEITEM itm)
{
	if(itm!=NULL && itm !=TVI_ROOT)
	{	
		Stuff::RegisteredClass *rcls;
		rcls=(Stuff::RegisteredClass *)GetItemData(itm);
		if(rcls != 0)
		{
			
			if(rcls->IsDerivedFrom(ElementRenderer::ElementClassID))
			{
				ElementRenderer::Element *elmt=Cast_Pointer(ElementRenderer::Element *, rcls);
				if (elmt->IsDerivedFrom(ElementRenderer::TreeElementClassID))
				{
					ElementRenderer::TreeElement *shape = Cast_Pointer(ElementRenderer::TreeElement *,elmt);
					shape->m_startAligningAt = startAt;
					shape->m_alignType = alignType;
				}
			}
		}				
	}	

	HTREEITEM child=GetChildItem(itm);
	while(child!=NULL)
	{
		SetAlign(startAt,alignType,child);
		child=GetNextSiblingItem(child);
	}
		
}

CErfDataTree::~CErfDataTree()
{
}


BEGIN_MESSAGE_MAP(CErfDataTree, CTreeCtrl)
//{{AFX_MSG_MAP(CErfDataTree)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CErfDataTree message handlers
