#include <direct.h>

#include "stdafx.h"
#include "acp_base.h"
#include "geo.h"
#include "gli.h"
#include "ITF.h"
#include "caminter.hpp"

// popupmenu constants
enum {	C_PopUpTarget = 0, C_PopUpCenter, C_PopUpNoTarget, C_ZoomTarget,C_ZoomNotTargetedTarget, C_PopUpEasyPointTarget};






//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
BOOL Camera_Interface::fn_bDefinePopupMenu(EDT_PopUpMenu *p_oPopup, CPA_List<CPA_SuperObject> *p_oSelection, BOOL bIsAlone)
{
	DEV_ViewPort3D			*p_oViewPort;
	Camera					*p_oCamera;
	BOOL					bResult;
	CPA_BaseObject			*p_oObject;
	EDT_SubMenu				*p_oSubMenu;
	
	if (bIsAlone)
		return FALSE;

	// this part is executed only if we are NOT the current editor
	bResult = FALSE;
	// get the camera
	p_oViewPort = (DEV_ViewPort3D*)(M_GetMultiDevice3D()->GetFocusDevice()->GetViewPort());
	p_oCamera = (Camera*) p_oViewPort->GetCamera();
	
	p_oSubMenu = p_oPopup->fn_p_oGetNewSubMenu("Camera");

	if (GetInterface()->GetCurrentWorld()->GetCountSelected() == 1)
	{
		CPA_SuperObject *p_oSelection;
		CString csText;

		// get the selection
		p_oSelection = GetInterface()->GetCurrentWorld()->GetSingleSelection();

		switch (p_oCamera->GetTargetType())
		{
		case tNone:

			p_oObject = p_oSelection->GetObject();

			if (p_oObject != NULL)
			{
				// entry select new target
				csText = "Target " + p_oObject->GetName();
				p_oSubMenu->AddAnEntry(this,(char*)(LPCTSTR)csText,C_PopUpTarget);
				// entry center on object
				csText = "Center on " + p_oObject->GetName();
//CPA2 Corneliu Babiuc 21-05-98
				csText += "\t" + m_p_oKeyConfiguration->mfn_oConvertIniStringToKeyString((const CString*)&CString("Center Camera on Object"));
//END CPA2 Coreliu Babiuc 21-05-98
				p_oSubMenu->AddAnEntry(this,(char*)(LPCTSTR)csText, C_PopUpCenter);
				// entry zoom on object
				// get the object
				csText = "Zoom on " + p_oObject->GetName();
				p_oSubMenu->AddAnEntry(this,(char*)(LPCTSTR)csText,C_ZoomNotTargetedTarget);
				bResult = TRUE;
			}
			break;

		case tSuperObject:
			// if the current target is different from the selected object
			if (p_oSelection != p_oCamera->GetTargetSuperObject())
			{
				csText = "Set New Target to " + p_oSelection->GetObject()->GetName();
				p_oSubMenu->AddAnEntry(this,(char*)(LPCTSTR)csText,C_PopUpTarget);
				bResult = TRUE;
			}
			else	// current target == selected object
			{
				// get the object
				p_oObject = p_oCamera->GetTargetSuperObject()->GetObject();
				if (p_oObject != NULL)
				{
					csText = "Zoom on " + p_oObject->GetName();
					p_oSubMenu->AddAnEntry(this,(char*)(LPCTSTR)csText,C_ZoomTarget);
					bResult = TRUE;
				}
			}
			break;

		case tPoint:
			csText = "Set New Target to " + p_oSelection->GetObject()->GetName();
			p_oSubMenu->AddAnEntry(this,(char*)(LPCTSTR)csText,C_PopUpTarget);
			bResult = TRUE;
			break;
		}
	}
	else
	{
		// no object or more than one object selected.
		// if camera is targeted, then add ZOOM entry if valid
		if (p_oCamera->GetTargetType() == tSuperObject)
		{
			// get the object
			p_oObject = p_oCamera->GetTargetSuperObject()->GetObject();
			if (p_oObject != NULL)
			{
				CString csText = "Zoom on " + p_oObject->GetName();
				p_oSubMenu->AddAnEntry(this,(char*)(LPCTSTR)csText,C_ZoomTarget);
				bResult = TRUE;
			}
		}
	}

	if (p_oCamera->GetTargetType() != tNone)
	{
		p_oSubMenu->AddAnEntry(this,"Not Targeted",C_PopUpNoTarget);
		bResult = TRUE;
	}

	p_oSubMenu->AddAnEntry(this, "Easy point target", C_PopUpEasyPointTarget);
	bResult = TRUE;

	if (bResult)
		p_oPopup->AddASubMenu(p_oSubMenu);

	return bResult;
}

//********************************************************************************************************
//********************************************************************************************************
//********************************************************************************************************
void Camera_Interface::_OnPopUpMenuCommand(UINT m_IDCmdMsg)
{
	CPA_SuperObject *p_oSelection;
	DEV_ViewPort3D	*p_oViewPort;
	Camera			*p_oCamera;
	
	// get the camera
	p_oViewPort = (DEV_ViewPort3D*)(M_GetMultiDevice3D()->GetFocusDevice()->GetViewPort());
	p_oCamera = (Camera*) p_oViewPort->GetCamera();
	// get the selected superobject
	p_oSelection = GetInterface()->GetCurrentWorld()->GetSingleSelection();

	switch (m_IDCmdMsg)
	{
	case C_PopUpTarget:
		ASSERT(p_oSelection != NULL);
		p_oCamera->SetTargetSuperObject(p_oSelection);
		p_oCamera->SetTargetType(tSuperObject);
		p_oCamera->Update();
		GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
		break;

	case C_PopUpCenter:
		p_oCamera->SetTargetSuperObject(p_oSelection);
		p_oCamera->SetTargetType(tSuperObject);
		p_oCamera->Update();
		p_oCamera->SetTargetType(tNone);
		GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
		break;
	
	case C_PopUpNoTarget:
		p_oCamera->SetTargetType(tNone);
		break;
	
	case C_ZoomTarget:
		p_oCamera->ZoomOnTarget();
		GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
		break;

	case C_ZoomNotTargetedTarget:
		ASSERT(p_oSelection != NULL);
		p_oCamera->ZoomOnTarget(p_oSelection);
		GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
		break;
	
	case C_PopUpEasyPointTarget:
		ChangeCameraTarget();
		return;
//ENDROMTEAM 3DEdition (Adrian Silvescu)
	}
}

/*===========================================================================
*		Description:	Add Camera entries to popup
*		Creation date:
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:       Author:
*=========================================================================*/
BOOL Camera_Interface::fn_bAddEntriesToTreePopup (CPA_DialogList *pDialog, CString csListName, CMenu *pMenu, UINT uiCustomEntriesStart)
{
	DEV_ViewPort3D			*p_oViewPort;
	Camera					*p_oCamera;
	BOOL					bResult;
	CPA_BaseObject			*p_oObject;
	
	// this part is executed only if we are NOT the current editor
	bResult = FALSE;
	// get the camera
	p_oViewPort = (DEV_ViewPort3D*)(M_GetMultiDevice3D()->GetFocusDevice()->GetViewPort());
	p_oCamera = (Camera*) p_oViewPort->GetCamera();
	
	if (GetInterface()->GetCurrentWorld()->GetCountSelected() == 1)
	{
		CPA_SuperObject *p_oSelection;
		CString csText;

		// get the selection
		p_oSelection = GetInterface()->GetCurrentWorld()->GetSingleSelection();

		switch (p_oCamera->GetTargetType())
		{
		case tNone:

			p_oObject = p_oSelection->GetObject();

			if (p_oObject != NULL)
			{
				// entry select new target
				csText = "Target " + p_oObject->GetName();
				pMenu->AppendMenu(MF_STRING, uiCustomEntriesStart + C_PopUpTarget, (char*)(LPCTSTR)csText);
				// entry center on object
				csText = "Center on " + p_oObject->GetName();
				pMenu->AppendMenu(MF_STRING, uiCustomEntriesStart + C_PopUpCenter, (char*)(LPCTSTR)csText);
				// entry zoom on object
				// get the object
				csText = "Zoom on " + p_oObject->GetName();
				pMenu->AppendMenu(MF_STRING, uiCustomEntriesStart + C_ZoomNotTargetedTarget, (char*)(LPCTSTR)csText);
				bResult = TRUE;
			}
			break;

		case tSuperObject:
			// if the current target is different from the selected object
			if (p_oSelection != p_oCamera->GetTargetSuperObject())
			{
				csText = "Set New Target to " + p_oSelection->GetObject()->GetName();
				pMenu->AppendMenu(MF_STRING, uiCustomEntriesStart + C_PopUpTarget, (char*)(LPCTSTR)csText);
				bResult = TRUE;
			}
			else	// current target == selected object
			{
				// get the object
				p_oObject = p_oCamera->GetTargetSuperObject()->GetObject();
				if (p_oObject != NULL)
				{
					csText = "Zoom on " + p_oObject->GetName();
					pMenu->AppendMenu(MF_STRING, uiCustomEntriesStart + C_ZoomTarget, (char*)(LPCTSTR)csText);
					bResult = TRUE;
				}
			}
			break;

		case tPoint:
			csText = "Set New Target to " + p_oSelection->GetObject()->GetName();
			pMenu->AppendMenu(MF_STRING, uiCustomEntriesStart + C_PopUpTarget, (char*)(LPCTSTR)csText);
			bResult = TRUE;
			break;
		}
	}
	else
	{
		// no object or more than one object selected.
		// if camera is targeted, then add ZOOM entry if valid
		if (p_oCamera->GetTargetType() == tSuperObject)
		{
			// get the object
			p_oObject = p_oCamera->GetTargetSuperObject()->GetObject();
			if (p_oObject != NULL)
			{
				CString csText = "Zoom on " + p_oObject->GetName();
				pMenu->AppendMenu(MF_STRING, uiCustomEntriesStart + C_ZoomTarget, (char*)(LPCTSTR)csText);
				bResult = TRUE;
			}
		}
	}

	if (p_oCamera->GetTargetType() != tNone)
	{
		pMenu->AppendMenu(MF_STRING, uiCustomEntriesStart + C_PopUpNoTarget, "Not Targeted");
		bResult = TRUE;
	}

	return bResult;	
}

/*===========================================================================
*=========================================================================*/
BOOL Camera_Interface::fn_bAddEntriesToListPopup (CPA_DialogList *pDialog, CString csListName, CMenu *pMenu, UINT uiCustomEntriesStart)
{
	return fn_bAddEntriesToTreePopup(pDialog, csListName, pMenu, uiCustomEntriesStart);
}

/*===========================================================================
*=========================================================================*/
void Camera_Interface::fn_vOnCommandInTreePopup (CPA_DialogList *pDialog, CString csListName, UINT uiCustomEntry)
{
	CPA_SuperObject *p_oSelection;
	DEV_ViewPort3D	*p_oViewPort;
	Camera			*p_oCamera;
	
	// get the camera
	p_oViewPort = (DEV_ViewPort3D*)(M_GetMultiDevice3D()->GetFocusDevice()->GetViewPort());
	p_oCamera = (Camera*) p_oViewPort->GetCamera();
	// get the selected superobject
	p_oSelection = GetInterface()->GetCurrentWorld()->GetSingleSelection();

	switch (uiCustomEntry)
	{
	case C_PopUpTarget:
		ASSERT(p_oSelection != NULL);
		p_oCamera->SetTargetSuperObject(p_oSelection);
		p_oCamera->SetTargetType(tSuperObject);
		p_oCamera->Update();
		GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
		break;

	case C_PopUpCenter:
		p_oCamera->SetTargetSuperObject(p_oSelection);
		p_oCamera->SetTargetType(tSuperObject);
		p_oCamera->Update();
		p_oCamera->SetTargetType(tNone);
		GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
		break;
	
	case C_PopUpNoTarget:
		p_oCamera->SetTargetType(tNone);
		break;
	
	case C_ZoomTarget:
		p_oCamera->ZoomOnTarget();
		GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
		break;

	case C_ZoomNotTargetedTarget:
		ASSERT(p_oSelection != NULL);
		p_oCamera->ZoomOnTarget(p_oSelection);
		GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
		break;
	
	case C_PopUpEasyPointTarget:
		ChangeCameraTarget();
		return;
	}
}

/*===========================================================================
*=========================================================================*/
void Camera_Interface::fn_vOnCommandInListPopup (CPA_DialogList *pDialog, CString csListName, UINT uiCustomEntry)
{
	fn_vOnCommandInTreePopup (pDialog, csListName, uiCustomEntry);
}

