// TransDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed.h"
#include "TransDlg.h"
#include "EditorWaypoint.h"
#include <Adept\Map.hpp>
#include "OverviewWindow.h"

extern CMW4GameEdApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CTransDlg dialog


CTransDlg::CTransDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTransDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTransDlg)
	m_YOffset = 0.0f;
	m_X = 0.0f;
	m_Y = 0.0f;
	m_Yaw = 0.0f;
	m_Z = 0.0f;
	m_Pitch = 0.0f;
	m_Roll = 0.0f;
	//}}AFX_DATA_INIT
	
	object = NULL;
}

CTransDlg::~CTransDlg()
{
	object = NULL;
}

void CTransDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTransDlg)
	DDX_Text(pDX, IDC_EDITYOFF, m_YOffset);
	DDX_Text(pDX, IDC_EDITTRANSX, m_X);
	DDX_Text(pDX, IDC_EDITTRANSY, m_Y);
	DDX_Text(pDX, IDC_EDITTRANSYAW, m_Yaw);
	DDX_Text(pDX, IDC_EDITTRANSZ, m_Z);
	DDX_Text(pDX, IDC_EDITTRANSPITCH, m_Pitch);
	DDX_Text(pDX, IDC_EDITTRANSROLL, m_Roll);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTransDlg, CDialog)
//{{AFX_MSG_MAP(CTransDlg)
ON_EN_KILLFOCUS(IDC_EDITTRANSZ, OnKillfocusEdittransz)
ON_EN_KILLFOCUS(IDC_EDITTRANSYAW, OnKillfocusEdittransyaw)
ON_EN_KILLFOCUS(IDC_EDITTRANSY, OnKillfocusEdittransy)
ON_EN_KILLFOCUS(IDC_EDITTRANSX, OnKillfocusEdittransx)
ON_EN_KILLFOCUS(IDC_EDITTRANSROLL, OnKillfocusEdittransroll)
ON_EN_KILLFOCUS(IDC_EDITTRANSPITCH, OnKillfocusEdittranspitch)
ON_WM_CLOSE()
ON_BN_CLICKED(IDMYOK, OnMyok)
ON_EN_KILLFOCUS(IDC_EDITYOFF, OnKillfocusEdityoff)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTransDlg message handlers

float REDUCEANGLE(float &x)
{
	float whole, dec, div, tmp = x;
	
	whole = float(int(tmp));
	dec = tmp - whole;
	
	div = float(int(whole / 360));
	x = whole - (div * 360) + dec;
	
	return (x);
}


void CTransDlg::TransUpdate()
{
	UpdateData(TRUE);
	theApp.m_MissionSaved = false;
	
	if (object && object->GetParentEntity())
	{
		LinearMatrix4D	mat(true);
		Point3D pos;
		YawPitchRoll rot;
		
		pos.x = m_X;
		pos.y = m_Y+m_YOffset;
		pos.z = m_Z;
		ObjData->VOffset=m_YOffset;
		rot.yaw = m_Yaw * Radians_Per_Degree;
		rot.pitch = m_Pitch * Radians_Per_Degree;
		rot.roll = m_Roll * Radians_Per_Degree;
		
		mat.BuildTranslation(pos);
		mat.BuildRotation(rot);
		
		object->SetNewLocalToParent(mat);
		object->SyncMatrices(true);
		theApp.overviewWindow->UpdateCamera();
		
		if (object->IsDerivedFrom(EditorBoundaryNode::DefaultData))
		{
			EditorBoundaryNode* node;
			node = Cast_Object(EditorBoundaryNode*, object);
			
			(*node->myBoundary)[node->index].x = pos.x;
			(*node->myBoundary)[node->index].y = pos.z;
			
			node->CheckValidPos();
			node->UpdateLinkPointData();
		}
		else if (object->IsDerivedFrom(EditorLight::DefaultData))
		{
			EditorLight* light;
			light = Cast_Object(EditorLight*,object);
			light->SetLightToWorldMatrix(mat);
		}
		else if (object->IsDerivedFrom(EditorCameraShip::DefaultData))
		{
			EditorCameraShip* node;
			node = Cast_Object(EditorCameraShip*,object);
			node->cameraShip->SetNewLocalToParent(mat);
			node->cameraShip->SyncMatrices(true);
		}
		else if (object->IsDerivedFrom(EditorPathNode::DefaultData))
		{
			EditorPathNode* node;
			Point3D	target_in_world;
			target_in_world = object->GetLocalToWorld();
			
			node = Cast_Object(EditorPathNode*,object);
			node->pathSlot.GetCurrent()->pathSlot.GetCurrent()->pathPoints[node->index] = target_in_world;
			node->UpdateLinkPointData();
		}
		else if (object->IsDerivedFrom(EditorLatticeNode::DefaultData))
		{
			EditorLatticeNode* node;
			Point3D	target_in_world;

			target_in_world = object->GetLocalToWorld();
			node = Cast_Object(EditorLatticeNode*,object);
			node->railNode->Location(target_in_world);
			node->railNode->UpdateRadiusPointData();
		}
		else if (object->IsDerivedFrom(EditorDropNode::DefaultData))
		{
			EditorDropNode* node;
			node = Cast_Object(EditorDropNode*,object);
			node->UpdatePoints();
		}
		else if (object->IsDerivedFrom(EditorDropZonePoint::DefaultData))
		{
			EditorDropZonePoint* node;
			node = Cast_Object(EditorDropZonePoint*,object);
			node->dropNode->UpdatePoints();
		}
		
		
	}
}

void CTransDlg::OnOK() 
{
}

void CTransDlg::OnMyok() 
{
	TransUpdate();
	CDialog::OnOK();
}

void CTransDlg::OnCancel() 
{
	m_X = m_Oldx;
	m_Y = m_Oldy;
	m_Z = m_Oldz;
	m_YOffset=m_OldYOffset;
	m_Yaw = m_Oldyaw;
	m_Pitch = m_Oldpitch;
	m_Roll = m_Oldroll;
	
	UpdateData(FALSE);
	
	TransUpdate();
	
	//	CDialog::OnCancel();
}

void CTransDlg::OnKillfocusEdittransx()  { TransUpdate(); }

void CTransDlg::OnKillfocusEdittranspitch() 
{
	UpdateData(TRUE);
	REDUCEANGLE(m_Pitch);
	UpdateData(FALSE);
	TransUpdate();
}

void CTransDlg::OnKillfocusEdittransroll() 
{
	UpdateData(TRUE);
	REDUCEANGLE(m_Roll);
	UpdateData(FALSE);
	TransUpdate();
}
void CTransDlg::OnKillfocusEdittransyaw() 
{
	UpdateData(TRUE);
	REDUCEANGLE(m_Yaw);
	UpdateData(FALSE);
	TransUpdate();
}

void CTransDlg::OnKillfocusEdittransy()  {	TransUpdate(); }

void CTransDlg::OnKillfocusEdittransz()  {	TransUpdate(); }

void CTransDlg::OnKillfocusEdityoff()    { TransUpdate(); }

BOOL CTransDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CTransDlg::SetValues()
{
	if (object)
		SetObject(object,ObjData);
}

void CTransDlg::SetValues(float x, float y, float z,float yoff, float yaw, float pitch, float roll, Adept::Entity* obj)
{
	Check_Object(obj);
	
	if (obj->IsDerivedFrom(Map::DefaultData))	// terrain is a no-no
		return;
	
	m_OldYOffset =m_YOffset = yoff;
	m_Oldx = m_X = x;
	m_Oldy = m_Y = y;
	m_Oldz = m_Z = z;
	m_Oldyaw = m_Yaw = yaw;
	m_Oldpitch = m_Pitch = pitch;
	m_Oldroll = m_Roll = roll;
	
	object = obj;
	UpdateData(FALSE);
	TransUpdate();
}

void CTransDlg::SetObject(Adept::Entity* obj, CEditorEntityData *obdat)
{
	//	Do not Check_Object(obj)  NULL is valid argument
	if (!obj)
	{
		object = NULL;
		ShowWindow(SW_HIDE);
		return;
	}
	
	if (obj->IsDerivedFrom(Map::DefaultData))	// terrain is a no-no
		return;
	
	if (object && object != obj)
		TransUpdate();
	
	ObjData=obdat;
	Verify(ObjData);
	Point3D pos;
	YawPitchRoll rot;
	
	pos = obj->GetLocalToWorld();
	rot = obj->GetLocalToWorld();
	
	rot.yaw *= Degrees_Per_Radian;
	rot.pitch *= Degrees_Per_Radian;
	rot.roll *= Degrees_Per_Radian;
	
	SetValues(pos.x,pos.y-ObjData->VOffset,pos.z,ObjData->VOffset,rot.yaw,rot.pitch,rot.roll,obj);
	
	SetWindowText(obj->instanceName);
}

void CTransDlg::CheckObject(Adept::Entity* obj)
{
	Check_Object(obj);
	
	if (object == obj)
		SetObject(NULL,ObjData);
}

void CTransDlg::OnClose() 
{
	TransUpdate();
	//	CDialog::OnOK();
	
	CDialog::OnClose();
}


