/*=========================================================================
* 
* SECdEdit.cpp :  Edited Sector Dialog - Implementation file
* 
*
* Version			1.0
* Creation date		
* Revision date
*
* Shaitan
*=======================================================================*/

#include "stdafx.h"
#include "acp_base.h"
#include "SECTint.hpp"

#include "SECdEdit.hpp"

#include "SECmodif.hpp"


#include "..\Main\Inc\_EditID.h"
#include "TUT.h"

#define C_xMinZfar 10.0

#define C_MinPriorityLevel C_SectorMinPriority - C_SectorNormalPriority + 1
#define C_MaxPriorityLevel C_SectorMaxPriority - C_SectorNormalPriority - 1 

//#################################################################################
// SECT_DialogEdit dialog
//#################################################################################

/*----------------------------------------
----------------------------------------*/
SECT_DialogEdit::SECT_DialogEdit(CWnd* pParent)
	: CFormView(SECT_DialogEdit::IDD)
{
	//{{AFX_DATA_INIT(SECT_DialogEdit)
	//}}AFX_DATA_INIT
}



/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(SECT_DialogEdit)
	DDX_Control(pDX, SECT_IDC_EDITEDSECTOR, m_cComboSector);
	DDX_Control(pDX, SECT_IDC_EDITVIRTUAL, m_cVirtual);
	DDX_Control(pDX, SECT_IDC_ZMAXCHECK, m_cZmaxCheck);
	DDX_Control(pDX, SECT_IDC_ZMAXVALUE, m_cZmaxEdit);
	DDX_Control(pDX, SECT_IDC_ZFARCHECK, m_cZfarCheck);
	DDX_Control(pDX, SECT_IDC_ZFARVALUE, m_cZfarEdit);
	DDX_Control(pDX, SECT_IDC_ZMAXDEFAULT, m_cDefault);
	DDX_Control(pDX, SECT_IDC_CAMERATYPE, m_cCamTypeEdit);
	DDX_Control(pDX, SECT_IDC_PRIORITY, m_cPriorityEdit);
	DDX_Control(pDX, SECT_IDC_PSPIN, m_cPrioritySpin);
	//}}AFX_DATA_MAP
}


/*----------------------------------------
----------------------------------------*/
BEGIN_MESSAGE_MAP(SECT_DialogEdit, CFormView)
	//{{AFX_MSG_MAP(SECT_DialogEdit)
	ON_WM_SIZE()
	ON_WM_DESTROY()
	ON_CBN_SELCHANGE(SECT_IDC_EDITEDSECTOR, OnSelChangeComboSector)
	ON_BN_CLICKED(SECT_IDC_EDITVIRTUAL, OnVirtual)
	ON_BN_CLICKED(SECT_IDC_ZMAXCHECK, OnZmaxCheck)
	ON_EN_KILLFOCUS(SECT_IDC_ZMAXVALUE, OnEditZmax)
	ON_BN_CLICKED(SECT_IDC_ZFARCHECK, OnZfarCheck)
	ON_EN_KILLFOCUS(SECT_IDC_ZFARVALUE, OnEditZfar)
	ON_BN_CLICKED(SECT_IDC_ZMAXDEFAULT, OnDefault)
	ON_EN_KILLFOCUS(SECT_IDC_CAMERATYPE, OnEditCamType)
//	ON_EN_KILLFOCUS(SECT_IDC_PRIORITY, OnEditPriority)
	ON_NOTIFY(UDN_DELTAPOS, SECT_IDC_PSPIN, OnPrioritySpin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//#################################################################################
// SECT_DialogEdit Inits
//#################################################################################

/*===========================================================================
*		Description:	Init dialog
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
void SECT_DialogEdit::fn_vInitDialog (Sector_Interface *p_oDLL, CWnd *pParent, const CString csTag)
{
	// create the dialog
	m_bInitialised = FALSE;
	m_pSectorInterface = p_oDLL;
	CFormView::Create(NULL, "", AFX_WS_DEFAULT_VIEW, CRect(0,0,120,180), pParent, AFX_IDW_PANE_FIRST, NULL);

	// init data for dialog
	CFormView::UpdateData(FALSE);

// FOR TUTORIAL
	TUT_M_vGetTutDll();

    TUT_M_vRegisterControl(m_cComboSector.m_hWnd,	"OSC_DEDIT"+ csTag + "_COMBO_EDITEDSECTOR",		TUT_e_ComboBox);
    TUT_M_vRegisterControl(m_cVirtual.m_hWnd,		"OSC_DEDIT"+ csTag + "_CHECK_VIRTUALSECTOR",	TUT_e_Button );

    TUT_M_vRegisterControl(m_cZmaxCheck.m_hWnd,		"OSC_DEDIT"+ csTag + "_CHECK_ZMAXSECTOR",		TUT_e_Button );
	TUT_M_vRegisterControl(m_cZmaxEdit.m_hWnd,		"OSC_DEDIT"+ csTag + "_EDIT_ZMAXSECTOR",		TUT_e_TextEdit);
	
    TUT_M_vRegisterControl(m_cZfarCheck.m_hWnd,		"OSC_DEDIT"+ csTag + "_CHECK_ZFARSECTOR",		TUT_e_Button );
	TUT_M_vRegisterControl(m_cZfarEdit.m_hWnd,		"OSC_DEDIT"+ csTag + "_EDIT_ZFARSECTOR",		TUT_e_TextEdit);
	
	TUT_M_vRegisterControl(m_cCamTypeEdit.m_hWnd,	"OSC_DEDIT"+ csTag + "_EDIT_CAMERASECTOR",		TUT_e_TextEdit);

	TUT_M_vRegisterControl(m_cPrioritySpin.m_hWnd,	"OSC_DEDIT"+ csTag + "_SPIN_PRIORITYSECTOR",	TUT_e_Spin);
// END TUTORIAL

	// inti spin parameters
	m_cPrioritySpin.SetBuddy(&m_cPriorityEdit);
	m_cPrioritySpin.SetRange(C_MinPriorityLevel, C_MaxPriorityLevel);
	m_cPrioritySpin.SetPos(0);

	// init all controls
	fn_vReinitDialog(NULL);

	m_bInitialised = TRUE;
}



/*===========================================================================
*		Description:	Init all controls
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*=========================================================================*/
void SECT_DialogEdit::fn_vReinitDialog (CPA_SuperObject *pSector)
{
	CPA_BaseObjectList	*pListObject;
	CPA_BaseObject		*pElem;
	Sector_Object		*pSectObj;
	Position			pos;
	CString				csText;
	BOOL				bZmax;
	BOOL				bZfar;
	int					iInd;
	
	// init sector
	m_pEditedSector = pSector;

	// reinit combo
	m_cComboSector.ResetContent();
	pListObject = m_pSectorInterface->GetBaseObjectList(C_szSectorTypeName);
	if (pListObject->GetCount() > 0)
	{
		for (pElem = pListObject->GetHeadElement(pos); pElem; pElem = pListObject->GetNextElement(pos))
			m_cComboSector.AddString(pElem->GetName());
	}

	// select current sector
	if (!m_pEditedSector)
	{
		strcpy(m_szName, "");
		m_cComboSector.SetCurSel(-1);
		// virtual
		m_cVirtual.SetCheck(0);
		m_cVirtual.EnableWindow(FALSE);
		// zmax
		m_cZmaxCheck.SetCheck(0);
		m_cZmaxCheck.EnableWindow(FALSE);
		m_cZmaxEdit.SetWindowText("");
		m_cZmaxEdit.EnableWindow(FALSE);
		// zfar
		m_cZfarCheck.SetCheck(0);
		m_cZfarCheck.EnableWindow(FALSE);
		m_cZfarEdit.SetWindowText("");
		m_cZfarEdit.EnableWindow(FALSE);
		// camera
		m_cCamTypeEdit.SetWindowText("");
		m_cCamTypeEdit.EnableWindow(FALSE);
		// priority
		m_cPrioritySpin.SetPos(0);
		m_cPrioritySpin.EnableWindow(FALSE);
	}
	else
	{
		pSectObj = (Sector_Object *) m_pEditedSector->GetObject();
		// sector
		strcpy(m_szName, pSectObj->GetName());
		iInd = m_cComboSector.FindStringExact(-1,m_szName);
		m_cComboSector.SetCurSel(iInd);
		// vitual
		m_cVirtual.SetCheck(pSectObj->fn_bIsVirtual());
		m_cVirtual.EnableWindow(TRUE);


		// Zmax
		m_cZmaxCheck.EnableWindow(TRUE);
		bZmax = (pSectObj->GetZmaxOfBorder() != MTH_C_InfinitMinus);
		m_cZmaxCheck.SetCheck(bZmax);
		m_cZmaxEdit.EnableWindow(bZmax);
		if (bZmax)
			csText.Format("%5.2f", (float) pSectObj->GetZmaxOfBorder());
		else
			csText = "";
		m_cZmaxEdit.SetWindowText(csText);

		// Zfar
		m_cZfarCheck.EnableWindow(TRUE);
		bZfar = (pSectObj->GetZfarInSector() != MTH_C_ZERO );
		m_cZfarCheck.SetCheck(bZfar);
		m_cZfarEdit.EnableWindow(bZfar);
		if (bZfar)
			csText.Format("%5.2f", (float) pSectObj->GetZfarInSector());
		else
			csText = "";
		m_cZfarEdit.SetWindowText(csText);

		// camera type
		m_cCamTypeEdit.EnableWindow(TRUE);
		csText.Format("%d", (int) pSectObj->GetCameraType());
		m_cCamTypeEdit.SetWindowText(csText);

		// priority
		m_cPrioritySpin.EnableWindow(TRUE);
		m_cPrioritySpin.SetPos(pSectObj->GetPriority() - C_SectorNormalPriority);
		//csText.Format("%d", (int) pSectObj->GetPriority());
		//m_cPriorityEdit.SetWindowText(csText);
	}
}


//#################################################################################
// SECT_DialogEdit Messages
//#################################################################################

	
/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnSize (UINT nFlags, int cx, int cy)
{
	RECT WindowPosition;
	int ecx;

	if (!m_bInitialised)
		return;
	
	// calculate name position
	GetWindowRect(&WindowPosition);
	ecx = WindowPosition.right - WindowPosition.left - 10;
	ecx = (ecx < 60) ? 60 : ecx;

	// move name
	m_cComboSector.MoveWindow(5, 5, ecx-55, 20);
	m_cVirtual.MoveWindow(ecx-45, 5, 50, 20);

	// move Zmax
	m_cZmaxCheck.MoveWindow(5, 30, 100, 20);
	m_cZmaxEdit.MoveWindow(110, 30, 50, 20);

	// move Zfar
	m_cZfarCheck.MoveWindow(5, 50, 100, 20);
	m_cZfarEdit.MoveWindow(110, 50, 50, 20);

	// move camera
	GetDlgItem(SECT_IDC_STATICCAM)->MoveWindow(5, 70, 100, 20);
	m_cCamTypeEdit.MoveWindow(110, 70, 50, 20);

	// move priority
	GetDlgItem(SECT_IDC_STATICPRIORITY)->MoveWindow(5, 90, 100, 20);
	m_cPriorityEdit.MoveWindow(110, 90, 30, 20);
	m_cPrioritySpin.MoveWindow(140, 90, 20, 20);
}

/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnSelChangeComboSector (void)
{
	char szName[256];
	
	if (!m_bInitialised)
		return;
	
	// name was changed ?
	m_cComboSector.GetLBText(m_cComboSector.GetCurSel(), szName);
	m_pEditedSector = m_pSectorInterface->GetSectorWithName(szName);
	m_pSectorInterface->fn_vReinitEditor(m_pEditedSector);
}


/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnVirtual (void)
{
	SECT_ModifVirtualSector *pModif;
	Sector_Object			*pSector;
	
	pSector = (Sector_Object *) m_pEditedSector->GetObject();

	// check if virtual status can be changed
	if (m_pSectorInterface->fn_bCanChangeVirtual(m_pEditedSector, m_cVirtual.GetCheck()))
	{
		pModif = new SECT_ModifVirtualSector(m_pSectorInterface, E_tm_VirtualStatus, 
											 m_pEditedSector, m_cVirtual.GetCheck());
		m_pSectorInterface->M_GetEditManager()->AskFor(pModif);
	}
	else
		m_cVirtual.SetCheck(((Sector_Object *)m_pEditedSector->GetObject())->fn_bIsVirtual());
}


/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnZmaxCheck (void)
{
	Sector_Object	*pSector;
	MTH_tdxReal		xZmax;
	CString			csText;

	pSector = (Sector_Object *) m_pEditedSector->GetObject();

	// update sector
	xZmax = pSector->GetZmaxOfSector();
	pSector->SetZmaxOfBorder(xZmax);

	// cancel Zmax
	if (!m_cZmaxCheck.GetCheck())
	{
		// update sector
		pSector->SetZmaxOfBorder(MTH_C_InfinitMinus);
		// update dialog
		m_cZmaxEdit.SetWindowText("");
		m_cZmaxEdit.EnableWindow(FALSE);
	}
	// init Zmax
	else
	{
		// update dialog
		m_cZmaxEdit.EnableWindow(TRUE);
		csText.Format("%5.2f", (float) pSector->GetZmaxOfBorder());
		m_cZmaxEdit.SetWindowText(csText);
	}		
	
	m_pSectorInterface->GetInterface()->fn_vUpdateAll(E_mc_JustDraw);	
}

/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnEditZmax()
{
	Sector_Object	*pSector;
	MTH_tdxReal		xNewZmax;
	char			szVal[15];

	pSector = (Sector_Object *) m_pEditedSector->GetObject();

	if (m_cZmaxEdit.GetModify())
	{
		m_cZmaxEdit.GetWindowText(szVal, 15);
		xNewZmax = (float) atof(szVal);
		pSector->SetZmaxOfBorder(xNewZmax);
	}

	m_pSectorInterface->GetInterface()->fn_vUpdateAll(E_mc_JustDraw);	
}

/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnZfarCheck (void)
{
	Sector_Object	*pSector;
	CString			csText;

	pSector = (Sector_Object *) m_pEditedSector->GetObject();

	// cancel Zmax
	if (!m_cZfarCheck.GetCheck())
	{
		// update sector
		pSector->SetZfarInSector(MTH_C_ZERO);
		// update dialog
		m_cZfarEdit.SetWindowText("");
		m_cZfarEdit.EnableWindow(FALSE);
	}
	// init Zmax
	else
	{
		// update dialog
		m_cZfarEdit.EnableWindow(TRUE);
		pSector->SetZfarInSector(C_xMinZfar);
		csText.Format("%5.2f", (float) pSector->GetZfarInSector());
		m_cZfarEdit.SetWindowText(csText);
	}		
}

/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnEditZfar()
{
	Sector_Object	*pSector;
	MTH_tdxReal		xNewZfar;
	CString			csText;
	char			szVal[15];

	pSector = (Sector_Object *) m_pEditedSector->GetObject();

	if (m_cZfarEdit.GetModify())
	{
		m_cZfarEdit.GetWindowText(szVal, 15);
		xNewZfar = (float) atof(szVal);
		if (xNewZfar >= C_xMinZfar)
			pSector->SetZfarInSector(xNewZfar);
		else
		{
			csText.Format("%5.2f", (float) pSector->GetZfarInSector());
			m_cZfarEdit.SetWindowText(csText);
		}
	}
}

/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnEditCamType()
{
	Sector_Object	*pSector;
	char			szVal[15];
	int				iNewType;

	pSector = (Sector_Object *) m_pEditedSector->GetObject();

	if (m_cCamTypeEdit.GetModify())
	{
		m_cCamTypeEdit.GetWindowText(szVal, 15);
		iNewType = (int) atoi(szVal);
		pSector->SetCameraType((char) iNewType);
	}
}

/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnPrioritySpin (NMHDR * pNMHDR, LRESULT * pResult)
{
	Sector_Object			*pSectObj = NULL;
	LPNM_UPDOWN				pUpDown; 
	int						iFinalPos;
	int						iNewPriority;

	// get action code
	pUpDown = (LPNM_UPDOWN)pNMHDR; 
	if (pUpDown->hdr.code != UDN_DELTAPOS)
		return;

	// get parameters
	pSectObj = (Sector_Object *) m_pEditedSector->GetObject();

	// get final pos
	iFinalPos = pUpDown->iPos + pUpDown->iDelta;
	iNewPriority = iFinalPos + C_SectorNormalPriority;

	// update sector
	pSectObj->SetPriority((char) iNewPriority);
}

/*----------------------------------------
----------------------------------------*/
/*
void SECT_DialogEdit::OnEditPriority()
{
	Sector_Object	*pSector;
	CString			csText;
	char			szVal[15];
	int				iNewPriority;

	pSector = (Sector_Object *) m_pEditedSector->GetObject();

	if (m_cPriorityEdit.GetModify())
	{
		// get value
		m_cPriorityEdit.GetWindowText(szVal, 15);
		iNewPriority = (int) atoi(szVal);
		// check limits
		if (iNewPriority > C_SectorMaxPriority)
		{
			iNewPriority = C_SectorMaxPriority;
			csText.Format("%d", iNewPriority);
			m_cPriorityEdit.SetWindowText(csText);
		}
		if (iNewPriority < C_SectorMinPriority)
		{
			iNewPriority = C_SectorMinPriority;
			csText.Format("%d", iNewPriority);
			m_cPriorityEdit.SetWindowText(csText);
		}
		pSector->SetPriority((char) iNewPriority);
	}
}
*/

/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnDefault()
{
	if (m_cZmaxEdit.GetModify())
	{
		OnEditZmax();
		m_pSectorInterface->GetInterface()->fn_vUpdateAll(E_mc_JustDraw);	
		m_cZmaxEdit.SetFocus();
	}

	if (m_cZfarEdit.GetModify())
	{
		OnEditZfar();
		m_cZfarEdit.SetFocus();
	}

	if (m_cCamTypeEdit.GetModify())
	{
		OnEditCamType();
		m_cCamTypeEdit.SetFocus();
	}

/*
	if (m_cPriorityEdit.GetModify())
	{
		OnEditPriority();
		m_cPriorityEdit.SetFocus();
	}
*/
}


/*----------------------------------------
----------------------------------------*/
void SECT_DialogEdit::OnDestroy (void)
{
// FOR TUTORIAL
	TUT_M_vGetTutDll();

    TUT_M_vUnregisterControl(m_cComboSector.m_hWnd);
    TUT_M_vUnregisterControl(m_cVirtual.m_hWnd);
 
	TUT_M_vUnregisterControl(m_cZmaxCheck.m_hWnd);
	TUT_M_vUnregisterControl(m_cZmaxEdit.m_hWnd);
	
    TUT_M_vUnregisterControl(m_cZfarCheck.m_hWnd);
	TUT_M_vUnregisterControl(m_cZfarEdit.m_hWnd);
	
	TUT_M_vUnregisterControl(m_cCamTypeEdit.m_hWnd);

	TUT_M_vUnregisterControl(m_cPrioritySpin.m_hWnd);
// END TUTORIAL

	CFormView::OnDestroy();
}
