// EDACDgIL.cpp : implementation file
/////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "EDACDgAC.hpp"

#include "_AInterf.hpp"
#include "EDACModl.hpp"

//External Modules
#include "IncTUT.h"
//End of External Modules

#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
    #include "_Actors.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define OAC_ACTOR_CHOICE_TIMER_ID				75
#define OAC_ACTOR_CHOICE_TIMER_DELAY		10
#define OAC_ACTOR_CHOICE_SCALING_STEP		8		//Percentage of size

/////////////////////////////////////////////////////////////////////////////
// EdActors_Dialog_ActorChoice dialog

BEGIN_MESSAGE_MAP(EdActors_Dialog_ActorChoice, CDialog)
	//{{AFX_MSG_MAP(EdActors_Dialog_ActorChoice)
	ON_LBN_DBLCLK(IDC_LIST_INSTANCES_OF_MODEL, OnDblclkListInstancesOfModel)
	ON_WM_TIMER()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//***************************************************************************
EdActors_Dialog_ActorChoice::EdActors_Dialog_ActorChoice(CPA_Actor *pclModel,
																												 CPoint cpPoint,
																												 CWnd* pParent /*=NULL*/)
														: CDialog(EdActors_Dialog_ActorChoice::IDD, &g_oBaseFrame)
{
	//{{AFX_DATA_INIT(EdActors_Dialog_ActorChoice)
	m_iSelectedItem = 0;
	//}}AFX_DATA_INIT

	m_hOldInstance = AfxGetResourceHandle();
	AfxSetResourceHandle(g_stActorEditorIdentity.hModule);
	
	m_pclSelectedActor = NULL;
	m_pclModel = pclModel;
	m_pri_pclFamily = NULL;
	m_cpRefPoint = cpPoint;
}

//***************************************************************************
EdActors_Dialog_ActorChoice::EdActors_Dialog_ActorChoice(CPA_Family *_pclFamily,
																												 CPoint cpPoint,
																												 CWnd* pParent /*=NULL*/)
														: CDialog(EdActors_Dialog_ActorChoice::IDD, &g_oBaseFrame)
{
	m_iSelectedItem = 0;

	m_hOldInstance = AfxGetResourceHandle();
	AfxSetResourceHandle(g_stActorEditorIdentity.hModule);
	
	m_pclSelectedActor = NULL;
	m_pclModel = NULL;
	m_pri_pclFamily = _pclFamily;
	m_cpRefPoint = cpPoint;
}

//**************************************************************************
EdActors_Dialog_ActorChoice::~EdActors_Dialog_ActorChoice()
{
	AfxSetResourceHandle(m_hOldInstance);
}

//***************************************************************************
void EdActors_Dialog_ActorChoice::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(EdActors_Dialog_ActorChoice)
	DDX_LBIndex(pDX, IDC_LIST_INSTANCES_OF_MODEL, m_iSelectedItem);
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// EdActors_Dialog_ActorChoice message handlers

//***************************************************************************
BOOL EdActors_Dialog_ActorChoice::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//Fills listbox
	CListBox *pclListBox = (CListBox *)GetDlgItem(IDC_LIST_INSTANCES_OF_MODEL);

	if ( m_pclModel != NULL )
	{
		short wIndex;
		EdActors_EditorActorModel *m_pclEditorModel = (EdActors_EditorActorModel *)m_pclModel->m_fn_pclGetEditorActor();
		EdActors_EditorActor *pclCurrentInstance;
		POSITION pos = m_pclEditorModel->m_clInstancesList.GetHeadPosition();
		while ( pos != NULL )
		{
			pclCurrentInstance = m_pclEditorModel->m_clInstancesList.GetNext(pos);

			wIndex = pclListBox->AddString(pclCurrentInstance->m_fn_csGetActorName());
			pclListBox->SetItemDataPtr(wIndex, (void *)pclCurrentInstance);
		}
	}
	else if ( m_pri_pclFamily != NULL )
	{
		//Gets the list of Models which are childs of the Family
		CPA_List<CPA_BaseObject> clListOfModels;
		g_pclInterface->GetMainWorld()->fn_lFindObjects(&clListOfModels,
																										"",
																										C_szActorModelTypeName,
																										(CPA_BaseObject *)m_pri_pclFamily);

		short wIndex;
		CPA_Actor *pclCurrentModel;
		POSITION pos = clListOfModels.GetHeadPosition();
		while ( pos != NULL )
		{
			pclCurrentModel = (CPA_Actor *)clListOfModels.GetNext(pos);
			 
			//Proposes only loaded Models
			if ( pclCurrentModel->fn_bIsAvailable() )
			{
				wIndex = pclListBox->AddString(pclCurrentModel->GetName());
				pclListBox->SetItemDataPtr(wIndex, (void *)pclCurrentModel->m_fn_pclGetEditorActor());
			}
		}
	}

	//Places Window
	CRect crWindowRect;
	GetWindowRect(crWindowRect);
	crWindowRect.OffsetRect(m_cpRefPoint.x - crWindowRect.left, m_cpRefPoint.y - crWindowRect.top);
	MoveWindow(crWindowRect);

	m_cCurrentPercentage = 0;
	m_wCurrentWidth = 0;
	m_wCurrentHeight = 0;
	GetWindowRect(crWindowRect);
	m_wFinalWidth = (short)crWindowRect.Width();
	m_wFinalHeight = (short)crWindowRect.Height();
	m_wInitialTop = (short)crWindowRect.top;
	m_wInitialLeft = (short)crWindowRect.left;

	MoveWindow(CRect(0,0,0,0));

	SetTimer(OAC_ACTOR_CHOICE_TIMER_ID, OAC_ACTOR_CHOICE_TIMER_DELAY, NULL);

	//////////////
	//////////////
	//Registers for TUT Module
	TUT_M_vGetTutDll();
	
	TUT_M_vRegisterControlID(IDC_LIST_INSTANCES_OF_MODEL, "OAc_AdtorChoiceDialog_ListBox", TUT_e_ListBox);
	TUT_M_vRegisterControlID(IDOK, "OAc_AdtorChoiceDialog_OKButton", TUT_e_Button);
	TUT_M_vRegisterControlID(IDCANCEL, "OAc_AdtorChoiceDialog_CancelButton", TUT_e_Button);

	//End of Registers for TUT Module
	//////////////

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//***************************************************************************
void EdActors_Dialog_ActorChoice::OnDestroy() 
{
	//////////////
	//////////////
	//UnRegisters for TUT Module
	TUT_M_vGetTutDll();
	
	TUT_M_vUnregisterControlID(IDC_LIST_INSTANCES_OF_MODEL);
	TUT_M_vUnregisterControlID(IDOK);
	TUT_M_vUnregisterControlID(IDCANCEL);
	
	//End of UnRegisters for TUT Module
	//////////////
	

	CDialog::OnDestroy();
}

//***************************************************************************
void EdActors_Dialog_ActorChoice::OnOK() 
{
	UpdateData(TRUE);

	if ( m_iSelectedItem != LB_ERR )
	{
		CListBox *pclListBox = (CListBox *)GetDlgItem(IDC_LIST_INSTANCES_OF_MODEL);
		m_pclSelectedActor = (EdActors_EditorActor *)pclListBox->GetItemDataPtr(m_iSelectedItem);
	}

	CDialog::OnOK();
}

//***************************************************************************
void EdActors_Dialog_ActorChoice::OnDblclkListInstancesOfModel() 
{
	OnOK();
}

//***************************************************************************
void EdActors_Dialog_ActorChoice::OnTimer(UINT nIDEvent) 
{
	if ( nIDEvent == OAC_ACTOR_CHOICE_TIMER_ID )
	{
		m_wCurrentWidth	= (m_wFinalWidth * m_cCurrentPercentage) / 100 + 5;
		m_wCurrentHeight	= (m_wFinalHeight * m_cCurrentPercentage) / 100 + 5;
		m_cCurrentPercentage += OAC_ACTOR_CHOICE_SCALING_STEP;

		CRect crDestRect;
		crDestRect.top = m_wInitialTop;
		crDestRect.bottom = m_wInitialTop + m_wCurrentHeight;
		crDestRect.left = m_wInitialLeft;
		crDestRect.right = m_wInitialLeft + m_wCurrentWidth;

		MoveWindow(crDestRect);
		ShowWindow(SW_SHOW);

		if ( m_cCurrentPercentage	>= 100 )
			KillTimer(OAC_ACTOR_CHOICE_TIMER_ID);
	}
	
	CDialog::OnTimer(nIDEvent);
}
