// ErfListBox.cpp : implementation file
//

#include "stdafx.h"
#include "mfc gos.h"
#include "ErfListBox.h"
#include "LODRangeDlg.h"
#include "ErfElemInfoView.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>

/////////////////////////////////////////////////////////////////////////////
// CErfListBox

CErfListBox::CErfListBox()
{
}

CErfListBox::~CErfListBox()
{
}


BEGIN_MESSAGE_MAP(CErfListBox, CListBox)
	//{{AFX_MSG_MAP(CErfListBox)
	ON_CONTROL_REFLECT(LBN_DBLCLK, OnDblclk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CErfListBox message handlers

void CErfListBox::OnDblclk() 
{
	
	int sel=GetCurSel();
	if(sel>=0)
	{
		CEditMessage *em=(CEditMessage *)GetItemDataPtr(sel);
		if(em)
			switch(em->EType)
		{
			case CEditMessage::EM_BILLBOARD:
				{
					ElementRenderer::Element *elmt=(ElementRenderer::Element *)em->Data;
					Check_Object(elmt);
					Verify(elmt->IsDerivedFrom(ElementRenderer::TreeElementClassID) );
					ElementRenderer::TreeElement *shape = Cast_Pointer(ElementRenderer::TreeElement *,elmt);
					CLodRangeDlg dlg;
					dlg.m_EditType = CLodRangeDlg::Billboard;
					dlg.m_AlignStartAt = shape->m_startAligningAt;
					dlg.m_AlignType = shape->m_alignType;
					dlg.m_NumLODs = shape->GetSize();

					if (elmt->IsDerivedFrom(ElementRenderer::TreeElementClassID)) {

						if(dlg.DoModal()==IDOK)
						{
							MyView->SetAlign(dlg.m_AlignStartAt, dlg.m_AlignType);
							MyView->RefreshData();
						}
					}										
				}	
				break;

			case CEditMessage::EM_LOD:
				{
					ElementRenderer::Element *elmt=(ElementRenderer::Element *)em->Data;
					Check_Object(elmt);
					Verify(elmt->IsDerivedFrom(ElementRenderer::ShapeLODElementClassID) ||
						elmt->IsDerivedFrom(ElementRenderer::TreeElementClassID) );
					CLodRangeDlg dlg;
					
					if (elmt->IsDerivedFrom(ElementRenderer::ShapeLODElementClassID)) {
						ElementRenderer::ShapeLODElement *shape = Cast_Pointer(ElementRenderer::ShapeLODElement *,elmt);
						Verify(shape->GetSize()>em->Index);
						const ElementRenderer::LODElement::Entry *entry;
						MidLevelRenderer::MLRShape *mlrshape;
						shape->GetLOD(em->Index,&mlrshape,&entry);
						dlg.m_Start=(float)sqrt(entry->m_nearSquared);
						dlg.m_End=(float)sqrt(entry->m_farSquared);
						dlg.m_EditType = CLodRangeDlg::Range;
						dlg.m_NumLODs = shape->GetSize();

						if(dlg.DoModal()==IDOK)
						{
							MyView->SetLodValue(em->Index,dlg.m_Start,dlg.m_End);
							MyView->RefreshData();
						}
					}
					
					if (elmt->IsDerivedFrom(ElementRenderer::TreeElementClassID)) {
						ElementRenderer::TreeElement *shape = Cast_Pointer(ElementRenderer::TreeElement *,elmt);
						Verify(shape->GetSize()>em->Index);
						const ElementRenderer::TreeElement::Entry *entry;
						MidLevelRenderer::MLRShape *mlrshape;
						shape->GetLOD(em->Index,&mlrshape,&entry);
						dlg.m_Start=(float)sqrt(entry->m_nearSquared);
						dlg.m_FadeIn=(float)sqrt(entry->m_fadeInSquared);
						dlg.m_FadeOut=(float)sqrt(entry->m_fadeOutSquared);
						dlg.m_End=(float)sqrt(entry->m_farSquared);
						dlg.m_EditType = CLodRangeDlg::CrossFade;
						dlg.m_NumLODs = shape->GetSize();

						if(dlg.DoModal()==IDOK)
						{
							MyView->SetLodValue(em->Index,dlg.m_Start,dlg.m_FadeIn,dlg.m_FadeOut,dlg.m_End);
							MyView->RefreshData();
						}
					}
				}	
				break;
		}
	}
	
}



