#include "stdafx.h"
#include "acp_base.h"
#include "itf/camslots.hpp"

#include "itf/camdllb.hpp"
#include "IncTUT.h"

#ifdef ACTIVE_EDITOR

//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
CPA_CameraSlots::CPA_CameraSlots()
{
	m_oCameraSlots.SetSize(10);

	m_lPrevCamSlotNum = -1;
//Stefan Dumitrean 24-06-98 ( slots )
	m_lCurrentSlot = -1;
	m_lSpecialSlotForModeChange = -1;
//End Stefan Dumitrean 24-06-98 ( slots )

	for (int i=0;i<10;i++)
	{
		CString name;

		m_oCameraSlots[i].p_oCamera = NULL;
		name.Format("Slot %i",i);
		m_oCameraSlots[i].csName = name;
		m_oCameraSlots[i].bReserved = FALSE;
		m_oCameraSlots[i].bActive = TRUE;
	}
	m_p_oInterface = NULL;
}


//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
CPA_CameraSlots::~CPA_CameraSlots()
{
	for (long i=0;i<m_oCameraSlots.GetSize();i++)
	{
		if (m_oCameraSlots[i].p_oCamera != NULL)
			delete m_oCameraSlots[i].p_oCamera;
	}
}

//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
long CPA_CameraSlots::ActivatePrevCamTip()
{
	if (m_lPrevCamSlotNum != -1)
		return m_lPrevCamSlotNum;
	
	// choose a slot num for the previous camera
	for (long i=0;i<m_oCameraSlots.GetSize();i++)
	{
		// the slot must be free!
		if ((m_oCameraSlots[i].p_oCamera == NULL) && (!m_oCameraSlots[i].bReserved) && (m_oCameraSlots[i].bActive))
		{
			m_lPrevCamSlotNum = i;
			m_oCameraSlots[i].bReserved = TRUE;
			m_oCameraSlots[i].csName = "Previous Camera";
			break;
		}
	}
	return m_lPrevCamSlotNum;
}



//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
long CPA_CameraSlots::ReserveASlot(CString csName)
{
	long lResult;

	// choose a free slot num  
	for (long i=0;i<m_oCameraSlots.GetSize();i++)
	{
		// the slot must be free!
		if ((m_oCameraSlots[i].p_oCamera == NULL) && (!m_oCameraSlots[i].bReserved) && (m_oCameraSlots[i].bActive))
		{
			lResult = i;
			m_oCameraSlots[i].bReserved = TRUE;
			m_oCameraSlots[i].csName = csName;
			break;
		}
	}
	return lResult;
}



//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
//Stefan Dumitrean 24-06-98 ( slots )
long CPA_CameraSlots::SaveCameraInSlot(long lSlotNum,CString csName,CPA_BaseObject *p_oCamera, tdeMakeCopy eCopy)
//End Stefan Dumitrean 24-06-98 ( slots )
{
	CPA_BaseObject *p_oSlotCamera;
//Stefan Dumitrean 1-07-98 ( slots )
	if( lSlotNum < -1 || lSlotNum > m_oCameraSlots.GetUpperBound() + 1)
		return -1;
//End Stefan Dumitrean 1-07-98 ( slots )
	if (p_oCamera == NULL)
	{
		p_oSlotCamera = NULL;
	}
	else if (eCopy == MakeACopy)
	{
		// copy camera
		p_oSlotCamera = m_p_oInterface->CopySlotCamera(p_oCamera);
	}
	else
	{
		p_oSlotCamera = p_oCamera;
	}

	// if lslotNum == -1, then add a new element at the end of the array
	if ((lSlotNum == -1) || (lSlotNum == m_oCameraSlots.GetUpperBound()+1))
	{
		tdstSlot	stNewSlot;

		stNewSlot.p_oCamera = p_oSlotCamera;
		stNewSlot.csName.Format("Slot %i",m_oCameraSlots.GetUpperBound()+1);
		stNewSlot.bReserved = FALSE;
		stNewSlot.bActive = TRUE;

		m_oCameraSlots.Add(stNewSlot);
		lSlotNum = m_oCameraSlots.GetUpperBound();
	}
	else
	{
		// delete slot camera
		if (m_oCameraSlots[lSlotNum].p_oCamera != NULL)
		{
			delete m_oCameraSlots[lSlotNum].p_oCamera;
			m_oCameraSlots[lSlotNum].p_oCamera = NULL;
		}
	}
		
	// rename slot
	if (csName != "")
		m_oCameraSlots[lSlotNum].csName = csName;
	// give the slot name to the camera
	if (p_oSlotCamera != NULL)
		p_oSlotCamera->fn_eRename(m_oCameraSlots[lSlotNum].csName);
	m_oCameraSlots[lSlotNum].p_oCamera = p_oSlotCamera;
//Stefan Dumitrean 2-07-98 ( slots )
//save also the extra slot information 
	m_oCameraSlots[lSlotNum].stExtraInfo.csEditorBaseName = GetCurrentEditorName();
	m_oCameraSlots[lSlotNum].stExtraInfo.csSelectedInstanceName = GetCurrentSelectionName();
//End Stefan Dumitrean 2-07-98 ( slots )

//Stefan Dumitrean 24-06-98 ( slots )
	return lSlotNum;
//End Stefan Dumitrean 24-06-98 ( slots )

}



//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
/*long CPA_CameraSlots::GetNbBusySlots()
{
	long lRes = 0;
	long i;

	for (i=0;i<m_oCameraSlots.GetSize();i++)
	{
		if (m_oCameraSlots[i].p_oCamera != NULL)
			lRes++;
	}
	return lRes;
}

*/


//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
BOOL CPA_CameraSlots::IsEmpty(long lSlotNum)
{
	return ((m_oCameraSlots[lSlotNum].p_oCamera == NULL) &&
			(m_oCameraSlots[lSlotNum].bActive) &&
			(!m_oCameraSlots[lSlotNum].bReserved));
}



//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
//Stefan Dumitrean 22-06-98 ( slots )
CPA_BaseObject *CPA_CameraSlots::GetCameraFromPopUpMenu( long &lSlotNum, CWnd *p_oParent, tdeMakeCopy eCopy )
//End Stefan Dumitrean 22-06-98 ( slots)
{
	CMenu	oMenu;
	POINT	stPoint;
	long lResult;
	BOOL bAddSeparator;

	oMenu.CreatePopupMenu();
	bAddSeparator = FALSE;

	// add previuos slot first (if it exists)
	if ((m_lPrevCamSlotNum != -1) &&
		(m_oCameraSlots[m_lPrevCamSlotNum].p_oCamera != NULL) &&
		(m_oCameraSlots[m_lPrevCamSlotNum].bActive)) 
	{
		oMenu.AppendMenu(MF_STRING|MF_ENABLED,m_lPrevCamSlotNum+1,m_oCameraSlots[m_lPrevCamSlotNum].csName);
		bAddSeparator = TRUE;
	}

	// add reserved slots first (do not add default slot)
	for (long i=0;i<m_oCameraSlots.GetSize();i++)
	{
		if (i != m_lPrevCamSlotNum && i != m_lSpecialSlotForModeChange && m_oCameraSlots[i].bReserved &&
			m_oCameraSlots[i].bActive && m_oCameraSlots[i].p_oCamera != NULL)
		{
//Stefan Dumitrean 23-06-98 ( slots )
			CString csName = m_oCameraSlots[i].csName;
			if ( i == m_lCurrentSlot )
				csName += "  «";
			oMenu.AppendMenu(MF_STRING|MF_ENABLED,i+1,/*m_oCameraSlots[i].*/csName);
//End Stefan Dumitrean 23-06-98 ( slots)
			bAddSeparator = TRUE;
		}
	}

	// add normal slots
	for (i=0;i<m_oCameraSlots.GetSize();i++)
	{
		if (!m_oCameraSlots[i].bReserved && m_oCameraSlots[i].bActive && m_oCameraSlots[i].p_oCamera != NULL)
		{
			// there is a normal entry --> add one and only one separator
			if (bAddSeparator)
			{
				oMenu.AppendMenu(MF_SEPARATOR,0,"");
				bAddSeparator = FALSE;
			}
//Stefan Dumitrean 23-06-98 ( slots )
			CString csName = m_oCameraSlots[i].csName;
			if( i == m_lCurrentSlot )
				csName += "  «";
			oMenu.AppendMenu(MF_STRING|MF_ENABLED,i+1,/*m_oCameraSlots[i].*/csName);
//End Stefan Dumitrean 23-06-98 ( slots )
		}
	}
		
	GetCursorPos(&stPoint);

    TUT_M_vGetTutDll    ();
    TUT_M_vRegisterMenu (p_oParent -> m_hWnd , oMenu . m_hMenu , stPoint . x , stPoint . y + 15);
	lResult = oMenu.TrackPopupMenu(TPM_NONOTIFY|TPM_RETURNCMD|TPM_LEFTBUTTON|TPM_CENTERALIGN,
								   stPoint.x,stPoint.y+15,p_oParent,NULL);

//Stefan Dumitrean 22-06-98 ( slots )
	lSlotNum = lResult - 1;
//End Stefan Dumitrean 22-06-98 ( slots )

	if (lResult == 0)
		return NULL;

	lResult -= 1;

	if (eCopy == MakeACopy)
		return m_p_oInterface->CopySlotCamera(m_oCameraSlots[lResult].p_oCamera);
	else
		return m_oCameraSlots[lResult].p_oCamera;
}



//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
long CPA_CameraSlots::GetSlotFromPopUpMenu(CWnd *p_oParent)
{
	CMenu	oMenu;
	POINT	stPoint;
	long lResult;

	oMenu.CreatePopupMenu();

	// add entries : checked if occuped, not checked if free
	for (long i=0;i<m_oCameraSlots.GetSize();i++)
	{
		if (m_oCameraSlots[i].bActive && !m_oCameraSlots[i].bReserved)
		{
			if (m_oCameraSlots[i].p_oCamera != NULL)
				oMenu.AppendMenu(MF_STRING|MF_ENABLED|MF_CHECKED,i+1,m_oCameraSlots[i].csName);
			else
				oMenu.AppendMenu(MF_STRING|MF_ENABLED,i+1,m_oCameraSlots[i].csName);
		}
	}

	// add the 'new slot' entry
	oMenu.AppendMenu(MF_STRING|MF_ENABLED,1000,"new entry");

	GetCursorPos(&stPoint);
    TUT_M_vGetTutDll    ();
    TUT_M_vRegisterMenu (p_oParent -> m_hWnd , oMenu . m_hMenu , stPoint . x , stPoint . y + 15);
	lResult = oMenu.TrackPopupMenu(TPM_NONOTIFY|TPM_RETURNCMD|TPM_LEFTBUTTON|TPM_CENTERALIGN,
								   stPoint.x,stPoint.y+15,p_oParent,NULL);

	if (lResult == 1000)
		return -2;

	return (lResult - 1);
}

//CPA2 Corneliu Babiuc 05-05-98
//***********************************************************************************************
// Method : CPA_BaseObject * CPA_CameraSlots::GetCameraFromSlot(long lNum, tdeMakeCopy eCopy = MakeACopy)
// Date   : 05-05-1998
//***********************************************************************************************
// Description : 
//    Returns the camera in a given number of slot by do or don't make a copy
// Parameters:
//  lNum       - Number of slot
//  eCopy      - do or don't make copy
// Author      : Corneliu Babiuc
//***********************************************************************************************
inline CPA_BaseObject * CPA_CameraSlots::GetCameraFromSlot(long lNum, tdeMakeCopy eCopy )
{
	ASSERT(lNum<m_oCameraSlots.GetSize());
	CPA_BaseObject *p_oSlotCamera;
	
	if (eCopy == MakeACopy)
	{
//CPA2 Corneliu Babiuc 15-05-98
		if (!m_oCameraSlots[lNum].p_oCamera)
			return NULL;
//END CPA2 Corneliu Babiuc 15-05-98
		// copy camera
		return m_p_oInterface->CopySlotCamera(m_oCameraSlots[lNum].p_oCamera);
	}
	else
	{
		return p_oSlotCamera = m_oCameraSlots[lNum].p_oCamera;
	}
}
//***********************************************************************************************
// Method : long CPA_CameraSlots::GetSlotNumber(CString csSlotName)
// Date   : 05-04-1998
//***********************************************************************************************
// Description : 
//    Function to get the slot number from name of a slot. Searching through camera slots
//    and return the first occurance of a slot with the name csName. If no slot is found return -1
// Parameters:
//  csSlotName       - name of the slots should be found
// Author      : Corneliu Babiuc
//***********************************************************************************************
long CPA_CameraSlots::GetSlotNumber(CString csSlotName)
{
	for (long i=0;i<m_oCameraSlots.GetSize();i++)
	{
		if (m_oCameraSlots[i].csName == csSlotName)
			return i;
	}
	return -1;
}
//END CPA2 Corneliu Babiuc 05-05-98

//Stefan Dumitrean 2-07-98 ( slots )

void CPA_CameraSlots::SetCurrentSlot( CString csSlotName )
{
	m_lCurrentSlot = GetSlotNumber( csSlotName );
}

void CPA_CameraSlots::SetCurrentSlot( long lSlotNum )
{
	m_lCurrentSlot = -1;
	if( lSlotNum >= 0 && lSlotNum < m_oCameraSlots.GetSize() )
		m_lCurrentSlot = lSlotNum;
}

long CPA_CameraSlots::GetCurrentSlot( )
{
	return m_lCurrentSlot;
}

BOOL CPA_CameraSlots::IsSlotReserved( CString csSlotName )
{
	long lSlotNum = GetSlotNumber( csSlotName );
	if( lSlotNum >= 0 && lSlotNum < m_oCameraSlots.GetSize() )
		return m_oCameraSlots[lSlotNum].bReserved;
	return TRUE;
}

BOOL CPA_CameraSlots::IsSlotReserved( long lSlotNum )
{
	if( lSlotNum >= 0 && lSlotNum < m_oCameraSlots.GetSize() )
		return m_oCameraSlots[lSlotNum].bReserved;
	return TRUE;
}

long CPA_CameraSlots::FindFirstEmptySlot( )
{
	for (long i=0;i<m_oCameraSlots.GetSize();i++)
	{
		if( IsEmpty( i ) )
			return i;
	}
	return i;
}

CString CPA_CameraSlots::GetCurrentEditorName()
{
	CString csName = "no selection";
	if( m_p_oInterface && m_p_oInterface->GetMainWorld() &&
		m_p_oInterface->GetMainWorld()->GetCurrentEditor()
	  )
	  csName = m_p_oInterface->GetMainWorld()->GetCurrentEditor()->GetEditorInfo()->csName;
	return csName;
}

CString CPA_CameraSlots::GetCurrentSelectionName()
{
	CString csName = "no selection";
	if( m_p_oInterface && m_p_oInterface->GetMainWorld() &&
		m_p_oInterface->GetMainWorld()->GetInterface() &&
		m_p_oInterface->GetMainWorld()->GetInterface()->GetCurrentWorld() &&
		m_p_oInterface->GetMainWorld()->GetInterface()->GetCurrentWorld()->GetSingleSelection()
	  )
		csName = m_p_oInterface->GetMainWorld()->GetInterface()->GetCurrentWorld()->GetSingleSelection()->GetName();
	return csName;
}

void CPA_CameraSlots::SetCurrentEditorForSlotNr( long lSlotNum )
{
	CPA_EditorBase *p_oEditor;
	CString csName;

	if( lSlotNum < 0 || lSlotNum > m_oCameraSlots.GetUpperBound() )
		return;
	
	csName = m_oCameraSlots[lSlotNum].stExtraInfo.csEditorBaseName;
	if( m_p_oInterface && m_p_oInterface->GetMainWorld() )
		if( p_oEditor = m_p_oInterface->GetMainWorld()->GetEditorByName( csName ) ) 
			m_p_oInterface->GetMainWorld()->fn_bActivateEditor( p_oEditor, NULL );
}

void CPA_CameraSlots::SetCurrentSelectionForSlotNr( long lSlotNum )
{
	CPA_SuperObject *p_oSuperObject;
	CString csName;

	if( lSlotNum < 0 || lSlotNum > m_oCameraSlots.GetUpperBound() )
		return;

	csName = m_oCameraSlots[lSlotNum].stExtraInfo.csSelectedInstanceName;
	if( m_p_oInterface && m_p_oInterface->GetMainWorld() &&
		m_p_oInterface->GetMainWorld()->GetInterface() 
	  )
	{
		p_oSuperObject = m_p_oInterface->GetMainWorld()->GetInterface()->GetSuperObject( "All Types", csName );
		m_p_oInterface->GetMainWorld()->GetInterface()->fn_bSelectObject( p_oSuperObject, FALSE );
	}
}

CString CPA_CameraSlots::GetEditorNameForSlotNr( long lSlotNum )
{
	if( lSlotNum < 0 || lSlotNum > m_oCameraSlots.GetUpperBound() )
		return "";
	return m_oCameraSlots[lSlotNum].stExtraInfo.csEditorBaseName;
}

CString CPA_CameraSlots::GetSelectionNameForSlotNr( long lSlotNum )
{
	if( lSlotNum < 0 || lSlotNum > m_oCameraSlots.GetUpperBound() )
		return "";
	return m_oCameraSlots[lSlotNum].stExtraInfo.csSelectedInstanceName;
}

void CPA_CameraSlots::SetEditorNameForSlotNr( long lSlotNum, CString csName )
{
	if( lSlotNum < 0 || lSlotNum > m_oCameraSlots.GetUpperBound() )
		return;
	m_oCameraSlots[lSlotNum].stExtraInfo.csEditorBaseName = csName;
}

void CPA_CameraSlots::SetSelectionNameForSlotNr( long lSlotNum, CString csName )
{
	if( lSlotNum < 0 || lSlotNum > m_oCameraSlots.GetUpperBound() )
		return;
	m_oCameraSlots[lSlotNum].stExtraInfo.csSelectedInstanceName = csName;
}

//End Stefan Dumitrean 2-07-98 ( slots )

#endif //ACTIVE_EDITOR