// ObjProps.cpp: implementation of the CObjProps class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "mw4gameed2.h"
#include"EdGameObject.h"
#include "ObjProps.h"
#include "ObjPanelDlg.h"
#include "GameObjPropPanal.h"
#include "GUIObjPanel.h"
#include "TNPanel.h"
#include "UndoManager.h"


enum
{
	CTL_NAME			= 1 << 0,
	CTL_TYPE			= 1 << 1,
	CTL_ALIGNMENT		= 1 << 2,
	CTL_SCRIPT			= 1 << 3,
	CTL_DRIVER			= 1 << 4,
	CTL_HOVER_ELEVATION	= 1 << 5,
	CTL_SKIN			= 1 << 6,
	CTL_SNAP			= 1 << 7,
	CTL_RESOURCE		= 1 << 8,
	CTL_POS_X			= 1 << 9,
	CTL_POS_Y			= 1 << 10,
	CTL_POS_Z			= 1 << 11,
	CTL_ROT_YAW			= 1 << 12,
	CTL_ROT_PITCH		= 1 << 13,
	CTL_ROT_ROLL		= 1 << 14,
	CTL_MEMCELL_0		= 1 << 15,
	CTL_MEMCELL_1		= 1 << 16,
	CTL_MEMCELL_2		= 1 << 17,
	CTL_MEMCELL_3		= 1 << 18,
	CTL_MEMCELL_4		= 1 << 19,
	CTL_MEMCELL_5		= 1 << 20,
	CTL_MEMCELL_6		= 1 << 21,
	CTL_MEMCELL_7		= 1 << 22,
	CTL_MEMCELL_8		= 1 << 23,
	CTL_MEMCELL_9		= 1 << 24,
	CTL_UIG				= 1 << 25,
	CTL_INVUL			= 1 << 26
};


CObjProps::CObjProps() 
{
DiffTag=ModTag=0; 
Virgin=true;

Name="";
Pos=Point3D(0.0f,0.0f,0.0f);
Rot=YawPitchRoll(0.0f,0.0f,0.0f);
Snap=false;
HoverElev=0.0f;
Script="";
SrcResource="";
Skin=0;
Alignment=0;
for(int i=0;i<MEMCELLS;i++) MemoryCell[i]=0.0f;
GamePropsFlag=false;
UseInGame=false;
Invulnerable=false;

}

// CMultiObjProps message handlers
void CObjProps::SetModTagFrom(EdGUIObject *gobj)
{
	if(Virgin)
	{
		MissionName=gobj->GetMissionName();
		Name="Multiple Objects";
		Pos=gobj->GetPos();
		Rot=gobj->GetRot();
		Snap=gobj->GetSnapToGround();
		HoverElev=gobj->GetHoverElevation();
		DiffTag=0;
		UseInGame=gobj->HasLocalizedName();
	}
	else
	{
		if(Name!=gobj->GetName() )							DiffTag|=CTL_NAME; 
		if(Snap!=gobj->GetSnapToGround() )					DiffTag|=CTL_SNAP; 
		if(UseInGame!=gobj->HasLocalizedName())				DiffTag|=CTL_UIG;

		if(HoverElev!=gobj->GetHoverElevation() )			DiffTag|=CTL_HOVER_ELEVATION; 
		
		Point3D newpos=gobj->GetPos();
		if(Pos.x!=newpos.x ) DiffTag|=CTL_POS_X; 
		if(Pos.y!=newpos.y ) DiffTag|=CTL_POS_Y; 
		if(Pos.z!=newpos.z ) DiffTag|=CTL_POS_Z; 
		
		
		YawPitchRoll newrot;
		newrot=gobj->GetRot();
		if(Rot.yaw!=newrot.yaw )		DiffTag|=CTL_ROT_YAW; 
		if(Rot.pitch!=newrot.pitch )		DiffTag|=CTL_ROT_PITCH; 
		if(Rot.roll!=newrot.roll )		DiffTag|=CTL_ROT_ROLL; 
	}
	Virgin=false;
}

void CObjProps::SetModTagFrom(EdGameObject *gobj)
{
GamePropsFlag=true;
	if(Virgin)
	{
		Script=gobj->GetScriptName();
		SrcResource=gobj->GetResourcePath();
		Skin=gobj->GetSkin();
		Alignment=gobj->GetAlignment();
		for(int i=0;i<MEMCELLS;i++) MemoryCell[i]=gobj->GetMemoryCell(i);
		DiffTag=0;
	}
	else
	{
		if(MemoryCell[0]!=gobj->GetMemoryCell(0) )	DiffTag|=CTL_MEMCELL_0; 
		if(MemoryCell[1]!=gobj->GetMemoryCell(1) )	DiffTag|=CTL_MEMCELL_1; 
		if(MemoryCell[2]!=gobj->GetMemoryCell(2) )	DiffTag|=CTL_MEMCELL_2; 
		if(MemoryCell[3]!=gobj->GetMemoryCell(3) )	DiffTag|=CTL_MEMCELL_3; 
		if(MemoryCell[4]!=gobj->GetMemoryCell(4) )	DiffTag|=CTL_MEMCELL_4; 
		if(MemoryCell[5]!=gobj->GetMemoryCell(5) )	DiffTag|=CTL_MEMCELL_5; 
		if(MemoryCell[6]!=gobj->GetMemoryCell(6) )	DiffTag|=CTL_MEMCELL_6; 
		if(MemoryCell[7]!=gobj->GetMemoryCell(7) )	DiffTag|=CTL_MEMCELL_7; 
		if(MemoryCell[8]!=gobj->GetMemoryCell(8) )	DiffTag|=CTL_MEMCELL_8; 
		if(MemoryCell[9]!=gobj->GetMemoryCell(9) )	DiffTag|=CTL_MEMCELL_9; 
		
		if(Skin!=gobj->GetSkin() )							DiffTag|=CTL_SKIN; 
		if(Invulnerable!=gobj->IsInvulnerable() )			DiffTag|=CTL_INVUL; 
		if(Script.CompareNoCase(gobj->GetScriptName()) )		DiffTag|=CTL_SCRIPT; 
		if(SrcResource.CompareNoCase(gobj->GetResourcePath()) )	DiffTag|=CTL_RESOURCE; 
		if(Alignment!=gobj->GetAlignment() )						DiffTag|=CTL_ALIGNMENT; 
	}
	
	SetModTagFrom((EdGUIObject *)gobj);
}

void CObjProps::SetData(EdGUIObject *gobj,UndoCommand **cmdlst)
{

	if(ModTag&CTL_NAME ) 
	{
		RenameCommand *cmd=new RenameCommand(gobj);
		gobj->SetName(Name);
		if(*cmdlst==NULL) *cmdlst=cmd; else (*cmdlst)->Attach(cmd);
	}
	
	if(ModTag&CTL_SNAP)  gobj->SetSnapToGround(Snap);
	if(ModTag&CTL_UIG)  gobj->UseLocalizedName(UseInGame);
	if(ModTag&CTL_HOVER_ELEVATION) gobj->SetHoverElevation(HoverElev);
	
if(ModTag&(CTL_POS_X|CTL_POS_Y|CTL_POS_Z))
{
	Point3D newpos=gobj->GetPos();
	if(ModTag&CTL_POS_X)  newpos.x=Pos.x; 
	if(ModTag&CTL_POS_Y)  newpos.y=Pos.y; 
	if(ModTag&CTL_POS_Z)  newpos.z=Pos.z; 

	gobj->SavePos(cmdlst);

	gobj->SetPos(newpos);
}	

	
if(ModTag&(CTL_ROT_YAW|CTL_ROT_PITCH|CTL_ROT_ROLL))
{
	YawPitchRoll newrot;
	newrot=gobj->GetRot();

	if(ModTag&CTL_ROT_YAW)	 newrot.yaw=Rot.yaw;		
	if(ModTag&CTL_ROT_PITCH) newrot.pitch=Rot.pitch;		
	if(ModTag&CTL_ROT_ROLL)	 newrot.roll=Rot.roll;	
	UnitQuaternion newrotquat;
	newrotquat=newrot;

	gobj->SaveRot(cmdlst);
	gobj->SetRot(newrotquat);
}	
	
	gobj->RemoveFromMap();
	gobj->AddToMap();
}

void CObjProps::SetData(EdGameObject *gobj,UndoCommand **cmdlst)
{
	
	if(ModTag&CTL_MEMCELL_0)   gobj->SetMemoryCell(0,MemoryCell[0]);
	if(ModTag&CTL_MEMCELL_1)   gobj->SetMemoryCell(1,MemoryCell[1]);
	if(ModTag&CTL_MEMCELL_2)   gobj->SetMemoryCell(2,MemoryCell[2]);
	if(ModTag&CTL_MEMCELL_3)   gobj->SetMemoryCell(3,MemoryCell[3]);
	if(ModTag&CTL_MEMCELL_4)   gobj->SetMemoryCell(4,MemoryCell[4]);
	if(ModTag&CTL_MEMCELL_5)   gobj->SetMemoryCell(5,MemoryCell[5]);
	if(ModTag&CTL_MEMCELL_6)   gobj->SetMemoryCell(6,MemoryCell[6]);
	if(ModTag&CTL_MEMCELL_7)   gobj->SetMemoryCell(7,MemoryCell[7]);
	if(ModTag&CTL_MEMCELL_8)   gobj->SetMemoryCell(8,MemoryCell[8]);
	if(ModTag&CTL_MEMCELL_9)   gobj->SetMemoryCell(9,MemoryCell[9]);
	
	if(ModTag&CTL_SKIN)       gobj->SetSkin(Skin);
	if(ModTag&CTL_INVUL)      gobj->SetInvulnerable(Invulnerable);
	if(ModTag&CTL_SCRIPT)	  gobj->SetScriptName(Script);
	if(ModTag&CTL_ALIGNMENT)  gobj->SetAlignment(Alignment);
	if(ModTag&CTL_RESOURCE)   gobj->ChangeTo(SrcResource);
	
	
	SetData((EdGUIObject *)gobj,cmdlst);
}

void CObjProps::AddPanel(CObjPanelDlg *pnl)
{
	pnl->AddPanel(new CTNPanel(this,(CWnd *)pnl));

	pnl->AddPanel(new GUIObjPanel(this,(CWnd *)pnl));

if(GamePropsFlag)
	pnl->AddPanel(new CGameObjPropPanal(this,(CWnd *)pnl));
}

void CObjProps::SetName(CString s)				{ModTag|=CTL_NAME; Name=s;}
void CObjProps::SetScript(CString s)			{ModTag|=CTL_SCRIPT; Script=s;}
void CObjProps::SetDriver(CString s)			{ModTag|=CTL_DRIVER; Driver=s;}
void CObjProps::SetSrcResource(CString s)		{ModTag|=CTL_RESOURCE; SrcResource=s;}
void CObjProps::SetSnap(bool b)					{ModTag|=CTL_SNAP; Snap=b;}
void CObjProps::SetUseInGame(bool b)					{ModTag|=CTL_UIG; UseInGame=b;}
void CObjProps::SetHoverElev(float f)			{ModTag|=CTL_HOVER_ELEVATION; HoverElev=f;}
void CObjProps::SetAlignment(int a)			{ ModTag|=CTL_ALIGNMENT; Alignment=a;}
void CObjProps::SetSkin(int s)					{ ModTag|=CTL_SKIN; Skin=s;}
void CObjProps::SetInvulnerable(bool s)					{ ModTag|=CTL_INVUL; Invulnerable=s;}
 void CObjProps::SetPosX(float f)
 {
 	 ModTag|=CTL_POS_X; 
	Pos.x=f;
 }
 void CObjProps::SetPosY(float f)				
 {
 	 ModTag|=CTL_POS_Y; 
	Pos.y=f;
 }
 void CObjProps::SetPosZ(float f)				
 {
 	 ModTag|=CTL_POS_Z; 
	Pos.z=f;
 }

 void CObjProps::SetYaw(float f)		
{
 ModTag|=CTL_ROT_YAW;
	Rot.yaw=f;
}
 
	 void CObjProps::SetPitch(float f)		
{
 ModTag|=CTL_ROT_PITCH;
	Rot.pitch=f;
}
 
	 void CObjProps::SetRoll(float f)		
{
	 ModTag|=CTL_ROT_ROLL;
	Rot.roll=f;
}

void CObjProps::SetPos(Point3D p)				
{
	SetPosX(p.x);
	SetPosY(p.y);
	SetPosZ(p.z);
}

void CObjProps::SetRot(YawPitchRoll r)			
{
	SetYaw(r.yaw);
	SetPitch(r.pitch);
	SetRoll(r.roll);
}

void CObjProps::SetMemoryCell(int i,float f)	
{
	Verify(i<MEMCELLS);
	

		switch(i)
	{
		case 0: ModTag|=CTL_MEMCELL_0; break;
		case 1: ModTag|=CTL_MEMCELL_1; break;
		case 2: ModTag|=CTL_MEMCELL_2; break;
		case 3: ModTag|=CTL_MEMCELL_3; break;
		case 4: ModTag|=CTL_MEMCELL_4; break;
		case 5: ModTag|=CTL_MEMCELL_5; break;
		case 6: ModTag|=CTL_MEMCELL_6; break;
		case 7: ModTag|=CTL_MEMCELL_7; break;
		case 8: ModTag|=CTL_MEMCELL_8; break;
		case 9: ModTag|=CTL_MEMCELL_9; break;
	}
}
