// MicroAndObjectsPage.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "MicroAndObjectsPage.h"

#include "snddbg.h"


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

/////////////////////////////////////////////////////////////////////////////
// CMicroAndObjectsPage property page

IMPLEMENT_DYNCREATE(CMicroAndObjectsPage, CPropertyPage)

CMicroAndObjectsPage::CMicroAndObjectsPage() : CPropertyPage(CMicroAndObjectsPage::IDD)
{
	//{{AFX_DATA_INIT(CMicroAndObjectsPage)
	m_dDopplerFactor = 0.0;
	//}}AFX_DATA_INIT
}

CMicroAndObjectsPage::~CMicroAndObjectsPage()
{
}

void CMicroAndObjectsPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMicroAndObjectsPage)
	DDX_Control(pDX, IDC_ASSO_MIC_LIST, m_MicroAssoList);
	DDX_Control(pDX, IDC_OBJECT_LIST, m_ObjectList);
	DDX_Control(pDX, IDC_MICRO_LIST, m_MicroList);
	DDX_Text(pDX, IDC_DOPPLER_EFFECT, m_dDopplerFactor);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMicroAndObjectsPage, CPropertyPage)
	//{{AFX_MSG_MAP(CMicroAndObjectsPage)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMicroAndObjectsPage message handlers

BOOL CMicroAndObjectsPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	//Initialise the ClistCtrl containing ressources descriptions
	LV_COLUMN* lvc;

	lvc = (LV_COLUMN*)malloc(sizeof(LV_COLUMN));
	//
	//Fill Micro list....
	//

	lvc->mask = LVCF_FMT |LVCF_SUBITEM 
		| LVCF_TEXT | LVCF_WIDTH;			// members contain valid information
    lvc->fmt = LVCFMT_LEFT;					// Alignment of the column
    lvc->cx = pSuperVisApp->m_RegKeyMicPage.iColMicId;							// width of the column, in pixels
    lvc->pszText = "Micro Id";					// column heading 
    lvc->iSubItem = 0;						// index of subitem associated with column
	m_MicroList.InsertColumn(0, lvc);


	lvc->pszText = "X";		// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyMicPage.iColX;							// width of the column, in pixels
    lvc->iSubItem = 1;						// index of subitem associated with column
	m_MicroList.InsertColumn(1, lvc);

	lvc->pszText = "Y";			// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyMicPage.iColY;							// width of the column, in pixels
    lvc->iSubItem = 2;						// index of subitem associated with column
	m_MicroList.InsertColumn(2, lvc);
	
	lvc->pszText = "Z";				// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyMicPage.iColZ;							// width of the column, in pixels
    lvc->iSubItem = 3;						// index of subitem associated with column
	m_MicroList.InsertColumn(3, lvc);
	
	lvc->pszText = "Nx";				// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyMicPage.iColNx;							// width of the column, in pixels
    lvc->iSubItem = 4;						// index of subitem associated with column
	m_MicroList.InsertColumn(4, lvc);

	
	lvc->cx = pSuperVisApp->m_RegKeyMicPage.iColNy;							// width of the column, in pixels
	lvc->pszText = "Ny";		// column heading 
    lvc->iSubItem = 5;						// index of subitem associated with column
	m_MicroList.InsertColumn(5, lvc);


	lvc->cx = pSuperVisApp->m_RegKeyMicPage.iColNz;							// width of the column, in pixels
	lvc->pszText = "Nz";				// column heading 
    lvc->iSubItem = 6;						// index of subitem associated with column
	m_MicroList.InsertColumn(6, lvc);


	lvc->pszText = "Tx";				// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyMicPage.iColTx;							// width of the column, in pixels
    lvc->iSubItem = 7;						// index of subitem associated with column
	m_MicroList.InsertColumn(7, lvc);

	
	lvc->cx = pSuperVisApp->m_RegKeyMicPage.iColTy;							// width of the column, in pixels
	lvc->pszText = "Ty";		// column heading 
    lvc->iSubItem = 8;						// index of subitem associated with column
	m_MicroList.InsertColumn(8, lvc);


	lvc->cx = pSuperVisApp->m_RegKeyMicPage.iColTz;							// width of the column, in pixels
	lvc->pszText = "Tz";				// column heading 
    lvc->iSubItem = 9;						// index of subitem associated with column
	m_MicroList.InsertColumn(9, lvc);

	//
	//Fill Object list....
	//
	
	lvc->mask = LVCF_FMT |LVCF_SUBITEM 
		| LVCF_TEXT | LVCF_WIDTH;			// members contain valid information
    lvc->fmt = LVCFMT_LEFT;					// Alignment of the column
    lvc->cx = pSuperVisApp->m_RegKeyObjPage.iColObjId;							// width of the column, in pixels
    lvc->pszText = "Object Id";					// column heading 
    lvc->iSubItem = 0;						// index of subitem associated with column
	m_ObjectList.InsertColumn(0, lvc);


	lvc->pszText = "X";						// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyObjPage.iColX;							// width of the column, in pixels
    lvc->iSubItem = 1;						// index of subitem associated with column
	m_ObjectList.InsertColumn(1, lvc);

	lvc->pszText = "Y";						// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyObjPage.iColY;							// width of the column, in pixels
    lvc->iSubItem = 2;						// index of subitem associated with column
	m_ObjectList.InsertColumn(2, lvc);
	
	lvc->pszText = "Z";						// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyObjPage.iColZ;							// width of the column, in pixels
    lvc->iSubItem = 3;						// index of subitem associated with column
	m_ObjectList.InsertColumn(3, lvc);
	
	lvc->pszText = "Frequence";				// column heading 
	lvc->cx = pSuperVisApp->m_RegKeyObjPage.iColFreq;							// width of the column, in pixels
    lvc->iSubItem = 4;						// index of subitem associated with column
	m_ObjectList.InsertColumn(4, lvc);

	
	lvc->cx = pSuperVisApp->m_RegKeyObjPage.iColVol;							// width of the column, in pixels
	lvc->pszText = "Volume";				// column heading 
    lvc->iSubItem = 5;						// index of subitem associated with column
	m_ObjectList.InsertColumn(5, lvc);


	lvc->cx = pSuperVisApp->m_RegKeyObjPage.iColAsso;							// width of the column, in pixels
	lvc->pszText = "Evts:ptr(Id)->Asso";				// column heading 
    lvc->iSubItem = 6;						// index of subitem associated with column
	m_ObjectList.InsertColumn(6, lvc);



	//
	//Fill Object micro association list....
	//
	
	lvc->mask = LVCF_FMT |LVCF_SUBITEM 
		| LVCF_TEXT | LVCF_WIDTH;			// members contain valid information
    lvc->fmt = LVCFMT_LEFT;					// Alignment of the column
    lvc->cx = 35;							// width of the column, in pixels
    lvc->pszText = "No.";					// column heading 
    lvc->iSubItem = 0;						// index of subitem associated with column
	m_MicroAssoList.InsertColumn(0, lvc);


	lvc->pszText = "Micro Id";				// column heading 
	lvc->cx = 75;							// width of the column, in pixels
    lvc->iSubItem = 1;						// index of subitem associated with column
	m_MicroAssoList.InsertColumn(1, lvc);

	lvc->pszText = "Distance";	// column heading 
	lvc->cx = 75;							// width of the column, in pixels
    lvc->iSubItem = 2;						// index of subitem associated with column
	m_MicroAssoList.InsertColumn(2, lvc);
	

	free( lvc );

	//We update the listctrl with micro or oject that could
	//have been added before the PropertyPage has been loaded
	RefreshMicroList();
	
	UpdateData(FALSE);	

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


void CMicroAndObjectsPage::RefreshMicroList()
{

	long i;
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	
	
	//we refresh the global Doppler factor
	m_dDopplerFactor = M_RealToDoubleSnd(dbgSND_fn_rGetDopplerFactor());
		

	//Clear the list
	m_MicroList.DeleteAllItems();
	

	long lNbMicros = pSuperVisApp->m_apMicros.GetSize(); 

	//Cycle through the pointer Array to filter
	for (i = 0; i < lNbMicros; i ++ )
	{
		//Update the rsources list
		AddMicroToList(i);

	}	





}


void CMicroAndObjectsPage::AddMicroToList(long lIndex)
{

	MicroArrayElement *pMic;
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	pMic = (MicroArrayElement *)(pSuperVisApp->m_apMicros.GetAt(lIndex));

  	//Retrieve data from the control
	PostMessage(WM_USER_UPDATE_DATA_TRUE, 0, 0);

	char buffer[128];

	//Display item
	sprintf(buffer,"0x%x", pMic->lId);
	m_MicroList.InsertItem( 0, buffer );


	//Set the item data with the Micro Array index, to be used later for drag&drop, pop-memnu, etc.
	m_MicroList.SetItemData( 0, (DWORD)lIndex );


	//Micro Position X
	sprintf(buffer, "%6.3f", pMic->dPosX);
	m_MicroList.SetItem(0, 1, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Micro Position Y
	sprintf(buffer, "%6.3f", pMic->dPosY);
	m_MicroList.SetItem(0, 2, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Micro Position Z
	sprintf(buffer, "%6.3f", pMic->dPosZ);
	m_MicroList.SetItem(0, 3, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	
	//Micro Normal X
	sprintf(buffer, "%6.3f", pMic->dNormX);
	m_MicroList.SetItem(0, 4, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Micro Normal Y
	sprintf(buffer, "%6.3f", pMic->dNormY);
	m_MicroList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Micro Normal Z
	sprintf(buffer, "%6.3f", pMic->dNormZ);
	m_MicroList.SetItem(0, 6, LVIF_TEXT, buffer, 0, 0, 0, NULL );


	//Micro Transversal X
	sprintf(buffer, "%6.3f", pMic->dTransX);
	m_MicroList.SetItem(0, 7, LVIF_TEXT, buffer, 0, 0, 0, NULL );


	//Micro Transversal Y
	sprintf(buffer, "%6.3f", pMic->dTransY);
	m_MicroList.SetItem(0, 8, LVIF_TEXT, buffer, 0, 0, 0, NULL );


	//Micro Transversal Z
	sprintf(buffer, "%6.3f", pMic->dTransZ);
	m_MicroList.SetItem(0, 9, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Refresh data from the control
	PostMessage(WM_USER_UPDATE_DATA_FALSE, 0, 0);
	

}

BOOL CMicroAndObjectsPage::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_USER_UPDATE_DATA_TRUE)
	{
		//We send the updatedata(TRUE) here
		//We cannot pass the CWnd across thread and this cause problem while using updatedata
		UpdateData(TRUE);
	}

	if(pMsg->message == WM_USER_UPDATE_DATA_FALSE)
	{
		//We send the updatedata(TRUE) here
		//We cannot pass the CWnd across thread and this cause problem while using updatedata
		UpdateData(FALSE);
	}
	
	return CPropertyPage::PreTranslateMessage(pMsg);
}



void CMicroAndObjectsPage::RefreshObjectList()
{

	long i;

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	//Clear the list
	m_ObjectList.DeleteAllItems();

	long lNbObjects = pSuperVisApp->m_apObjects.GetSize();

	//Cycle through the pointer Array to filter
	for (i = 0; i < lNbObjects; i ++ )
	{
		//Update the rsources list
		AddObjectToList(i);

	}	


	//Set the asso mic with currently selected object
	if(lNbObjects > 0)
		SetObjectAssoMicroList(0);


}






void CMicroAndObjectsPage::SetObjectAssoMicroList(long lIndexObject)
{

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	long i;
	//Set the Associated Micro List 
	MicroArrayElement* pMic;
	ObjectArrayElement *pObject;
	
	SndVector Vector;
	char buffer[256];


	//Clear the list
	m_MicroAssoList.DeleteAllItems();
	
		
	
	pObject = (ObjectArrayElement *)(pSuperVisApp->m_apObjects.GetAt(lIndexObject));


	long lNbMicros = pSuperVisApp->m_apMicros.GetSize();
	
	for (i = 0; i < lNbMicros; i++){

		pMic = (MicroArrayElement*)pSuperVisApp->m_apMicros.GetAt(i);


		//Mic #
		sprintf(buffer,"%d", i );
		m_MicroAssoList.InsertItem( 0, buffer );

		//Mic Id
		sprintf(buffer,"0x%0x",pMic->lId );
		m_MicroAssoList.SetItem(0, 1, LVIF_TEXT, buffer, 0, 0, 0, NULL );

		//Object distance from current Micro
		Vector.x = (long)(pObject->dPosX - pMic->dPosX);
		Vector.y = (long)(pObject->dPosY - pMic->dPosY);
		Vector.z = (long)(pObject->dPosZ - pMic->dPosZ);

		sprintf(buffer,"%6.3f",M_RealToDoubleSnd(dbgSND_fn_rPseudoNormeVectorSnd(&Vector)));
		m_MicroAssoList.SetItem(0, 2, LVIF_TEXT, buffer, 0, 0, 0, NULL );


	}

}


void CMicroAndObjectsPage::AddObjectToList(long lIndex)
{

	ObjectArrayElement *pObject;
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	pObject = (ObjectArrayElement *)(pSuperVisApp->m_apObjects.GetAt(lIndex));

  	//Retrieve data from the control
	PostMessage(WM_USER_UPDATE_DATA_TRUE, 0, 0);

	char buffer[128];

	//Display item
	if (pObject->lId == SND_C_OBJET_FANTOME)
		sprintf(buffer,"OBJET_FANTOME");
	else
		sprintf(buffer,"0x%x", pObject->lId);
	m_ObjectList.InsertItem( 0, buffer );


	//Set the item data with the Micro Array index, to be used later for drag&drop, pop-memnu, etc.
	m_ObjectList.SetItemData( 0, (DWORD)lIndex );


	//Object Position X
	sprintf(buffer, "%6.3f", pObject->dPosX);
	m_ObjectList.SetItem(0, 1, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Object Position Y
	sprintf(buffer, "%6.3f", pObject->dPosY);
	m_ObjectList.SetItem(0, 2, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Object Position Z
	sprintf(buffer, "%6.3f", pObject->dPosZ);
	m_ObjectList.SetItem(0, 3, LVIF_TEXT, buffer, 0, 0, 0, NULL );
	
	//Object Frequency
	sprintf(buffer, "%6.3f", pObject->dFreq);
	m_ObjectList.SetItem(0, 4, LVIF_TEXT, buffer, 0, 0, 0, NULL );

	//Object Volume
	sprintf(buffer, "%6.3f", pObject->ucVol);
	m_ObjectList.SetItem(0, 5, LVIF_TEXT, buffer, 0, 0, 0, NULL );


	//Object Event pointer (the last one associated)
	sprintf(buffer, "0x%0x", pObject->evt_pstPtr);
	m_ObjectList.SetItem(0, 6, LVIF_TEXT, buffer, 0, 0, 0, NULL );



	//Refresh data from the control
	PostMessage(WM_USER_UPDATE_DATA_FALSE, 0, 0);

}



void CMicroAndObjectsPage::OnDestroy() 
{
	CPropertyPage::OnDestroy();
	

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();


	int ColWidth = m_MicroList.GetColumnWidth(0);
	pSuperVisApp->m_RegKeyMicPage.iColMicId= ColWidth;
	ColWidth = m_MicroList.GetColumnWidth(1);
	pSuperVisApp->m_RegKeyMicPage.iColX= ColWidth;
	ColWidth = m_MicroList.GetColumnWidth(2);
	pSuperVisApp->m_RegKeyMicPage.iColY= ColWidth;
	ColWidth = m_MicroList.GetColumnWidth(3);
	pSuperVisApp->m_RegKeyMicPage.iColZ= ColWidth;
	ColWidth = m_MicroList.GetColumnWidth(4);
	pSuperVisApp->m_RegKeyMicPage.iColNx= ColWidth;
	ColWidth = m_MicroList.GetColumnWidth(5);
	pSuperVisApp->m_RegKeyMicPage.iColNy = ColWidth;
	ColWidth = m_MicroList.GetColumnWidth(6);
	pSuperVisApp->m_RegKeyMicPage.iColNz= ColWidth;
	ColWidth = m_MicroList.GetColumnWidth(7);
	pSuperVisApp->m_RegKeyMicPage.iColTx= ColWidth;
	ColWidth = m_MicroList.GetColumnWidth(8);
	pSuperVisApp->m_RegKeyMicPage.iColTy = ColWidth;
	ColWidth = m_MicroList.GetColumnWidth(9);
	pSuperVisApp->m_RegKeyMicPage.iColTz = ColWidth;

	ColWidth = m_ObjectList.GetColumnWidth(0);
	pSuperVisApp->m_RegKeyObjPage.iColObjId= ColWidth;
	ColWidth = m_ObjectList.GetColumnWidth(1);
	pSuperVisApp->m_RegKeyObjPage.iColX= ColWidth;
	ColWidth = m_ObjectList.GetColumnWidth(2);
	pSuperVisApp->m_RegKeyObjPage.iColY= ColWidth;
	ColWidth = m_ObjectList.GetColumnWidth(3);
	pSuperVisApp->m_RegKeyObjPage.iColZ= ColWidth;
	ColWidth = m_ObjectList.GetColumnWidth(4);
	pSuperVisApp->m_RegKeyObjPage.iColFreq= ColWidth;
	ColWidth = m_ObjectList.GetColumnWidth(5);
	pSuperVisApp->m_RegKeyObjPage.iColVol = ColWidth;
	ColWidth = m_ObjectList.GetColumnWidth(6);
	pSuperVisApp->m_RegKeyObjPage.iColAsso= ColWidth;


}
