/***************************************************************************
*                                                                           *
*                           3DS Max Test Plugin                             *
*                                                                           *
***************************************************************************
*                                                                           *
* Version : 1.0ß, March 11th, 1998                                          *
*                                                                           *
* Written By Loic Baumann from Fatal Design, specially for Gamasutra        *
*                                                                           *
***************************************************************************
*                                                                           *
* This Plugin demonstrates how to write a simple 3DS MAX Plugin, using MFCs *
*                                                                           *
***************************************************************************/
 
#include "stdafx.h"

#ifdef STRICT							// Already defined by stdafx,
#undef STRICT							// so we avoid warning linking msg
#endif
#ifdef _MBCS							// The same as above
#undef _MBCS
#endif

#include "LegMirrorPanel.h"
#include "HillAdjust.hpp"

//#ifdef _DEBUG
//#define new DEBUG_NEW
//#undef THIS_FILE
//static char THIS_FILE[] = __FILE__;
//#endif

#include <direct.h>
#include "MissionImp.h"
#include "ExportUtil.h"
//#include "mapsizedlg.h"

LegMirrorPanel g_legmirrorPanel;


////////////////////////////////////////////////////////////////////////////////
// JointPrefrencePanel Dialog Procedure to handle User and System Interactions
static BOOL CALLBACK LegMirrorPanelProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	
	switch (msg) {
	case WM_INITDIALOG:						//Panel's extra initialization
		g_legmirrorPanel.Init(hWnd);
		break;
	case WM_DESTROY:						//Panel's extra destruction
		g_legmirrorPanel.Destroy(hWnd);
		break;
	case WM_COMMAND :
		switch (LOWORD(wParam)) {			//Handle Panel's controls
		case IDC_ABOUT: {
			CAbout dlg;
			dlg.DoModal();
			break;}
		case IDC_MIRRORLEG: {
			g_legmirrorPanel.DoMirror();
			break;}
		case IDC_REVERSEANIM:
			{
				g_legmirrorPanel.DoReverse();
				break;}
		case IDC_REVSITES:
			{
				g_legmirrorPanel.RevSites();
				break;}
			
		case IDC_FIXEXP:
			{
				g_legmirrorPanel.DoFixExp();
				break;}
		case IDC_EXPORTANIM:
			{
				g_legmirrorPanel.DoExportAnims();
				break;}
		case IDC_FIXSCALE:
			{
				g_legmirrorPanel.FixScale();
				break;
			}
		case IDC_EXPORTLIGHTS:
			{
				g_legmirrorPanel.ExportLights();
				break;
			}
		case IDC_IMPORTCONTENTS:
			{
				g_legmirrorPanel.ImportMission();
				break;
			}
		}
		break;
		
		////////////////////////////////////////////////////////////////////////
		//The following lines are not necessary for MAX2 Plugins
		case WM_LBUTTONDOWN:
		case WM_LBUTTONUP:
		case WM_MOUSEMOVE:
			g_legmirrorPanel.m_ip->RollupMouseMessage(hWnd, msg, wParam, lParam);
			break;
			////////////////////////////////////////////////////////////////////////
			
		default:
			return FALSE;
	}
	return TRUE;
}


////////////////////////////////////////////////////////////////////////////////
// Display the Plugin's Panel
void LegMirrorPanel::BeginEditParams(Interface* ip, IUtil* iu)
{
	m_ip = ip;								//Get the Plugin's Interface
	m_iu = iu;								//Get the Utility Plugin's Interface
	
	//Display the plugin's Rollup Dialog
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	HINSTANCE loc_hInstance = AfxGetResourceHandle ();
	
	m_hPanel = ip->AddRollupPage(loc_hInstance, MAKEINTRESOURCE(IDD_LEGMIRROR_PANEL),
		LegMirrorPanelProc, GetString(IDS_LEGMIRROR_NAME),0);
}

////////////////////////////////////////////////////////////////////////////////
// Free the Plugins' Panel
void LegMirrorPanel::EndEditParams(Interface *ip, IUtil *iu)
{
	m_iu = NULL;
	m_ip = NULL;
	
	//Remove the Rollup Dialog
	ip->DeleteRollupPage(m_hPanel);
	m_hPanel = NULL;
}

////////////////////////////////////////////////////////////////////////////////
// Called when the Pick Button is pushed


LegMirrorPanel::LRFLAG LegMirrorPanel::TellSide(INode *nde)
{
	CString nname(nde->GetName());
	int cpos;
	cpos=nname.FindOneOf("_");
	if(cpos==-1) 
		return ISNEITHER;
	else
		if(nname[cpos+1]=='l' || nname[cpos+1]=='L')
			if(FindCorespondingNode(nde))
				return ISLEFT;
			else
				return ISNEITHER;
			
			else
				if(nname[cpos+1]=='r' || nname[cpos+1]=='R' )
					if(FindCorespondingNode(nde))
						return ISRIGHT;
					else
						return ISNEITHER;
					
					return ISNEITHER;
}

INode *LegMirrorPanel::FindCorespondingNode(INode *nde)
{
if(nde==NULL) return NULL;
	CString nname(nde->GetName());
	int cpos;
	cpos=nname.FindOneOf("_");
	if(cpos==-1) return NULL;
	if(nname[cpos+1]=='l' || nname[cpos+1]=='L')
		nname.SetAt(cpos+1,'r');
	else
		if(nname[cpos+1]=='r' || nname[cpos+1]=='R')
			nname.SetAt(cpos+1,'l');
		else
			return NULL;
		
		return FindNodeByName(m_ip->GetRootNode(),nname);
}

void LegMirrorPanel::RevSites()
{
	RevSiteOnNode(m_ip->GetRootNode());
	
	m_ip->ForceCompleteRedraw();
	
}

void LegMirrorPanel::RevSiteOnNode(INode *nde)
{
	
	CString str(nde->GetName());
	
	if(IsSite(str))
	{
		AngAxis rot(Point3(0.0f,0.0f,1.0f),PI);
		
		Matrix3 axis(MAT_IDENT);
		nde->Rotate(0, nde->GetObjectTM(0,NULL), rot,TRUE,FALSE);
		
	}
	
	for(int i=0;i<nde->NumberOfChildren();i++)
	{
		RevSiteOnNode(nde->GetChildNode(i));
	}
}

bool LegMirrorPanel::IsExprControl(Control *ctrl)
{
	Class_ID id;
	id=ctrl->ClassID();
	return (id==Class_ID(EXPR_POS_CONTROL_CLASS_ID,0)||
		id==Class_ID(EXPR_P3_CONTROL_CLASS_ID,0) ||
		id==Class_ID(EXPR_FLOAT_CONTROL_CLASS_ID,0) ||
		id==Class_ID(EXPR_SCALE_CONTROL_CLASS_ID,0) ||
		id==Class_ID(EXPR_ROT_CONTROL_CLASS_ID,0) );
}

Control *LegMirrorPanel::GetControlByName(Animatable *nde,CString &cnme,int *subanim)
{
	int i;
	Control *cpt;
	for(i=0;i<nde->NumSubs();i++)
	{
		CString ccn(nde->SubAnimName(i));
		if(!ccn.CompareNoCase(cnme)) 
		{
			*subanim=i;
			return (Control *)nde;
		}
		else
		{
			cpt=GetControlByName(nde->SubAnim(i),cnme,subanim);
			if(cpt) return cpt;
		}
		
	}
	
	return NULL;
}

void LegMirrorPanel::PlaceZeroKeys(INode *nde)
{
	
	CString str(nde->GetName());
	
	
	if(!IsSite(str))
	{
		Control *tm_con,*pos_con,*rot_con,*xp_con,*yp_con,*zp_con,*xr_con,*yr_con,*zr_con;
		IKeyControl *ikc;
		
		tm_con = nde->GetTMController();
		if(tm_con!=NULL)
		{
			pos_con = tm_con->GetPositionController();
			rot_con = tm_con->GetRotationController();
			
			if(rot_con)
			{
				xr_con=rot_con->GetXController();
				if(xr_con!=NULL)
				{
				}
				
				yr_con=rot_con->GetYController();
				if(yr_con!=NULL)
				{
					ikc = GetKeyControlInterface(yr_con);
					if(ikc && !ikc->GetNumKeys())
					{
						AnimateOn();
						nde->Rotate(1,IdentityTM(),Quat(0.0,0.0,0.0,0.0));
						AnimateOff();
					}
					
				}
				
				zr_con=rot_con->GetZController();
				if(zr_con!=NULL)
				{
					ikc = GetKeyControlInterface(zr_con);
					if(ikc && !ikc->GetNumKeys())
					{
						AnimateOn();
						nde->Rotate(1,IdentityTM(),Quat(0.0,0.0,0.0,0.0));
						AnimateOff();
					}
					
				}
			}
			
			
			
			if( !IsJoint(str) || ( IsJoint(str) && TellSide(nde)==ISNEITHER ) )
				if(pos_con)
				{
					xp_con=pos_con->GetXController();
					if(xp_con!=NULL)
					{
						ikc = GetKeyControlInterface(xp_con);
						if(ikc && !ikc->GetNumKeys())
						{
							{
								AnimateOn();
								nde->Move(1,IdentityTM(),Point3(0,0,0));
								AnimateOff();
							}
						}
					}
					
					yp_con=pos_con->GetYController();
					if(yp_con!=NULL)
					{
					}
					
					zp_con=pos_con->GetZController();
					if(zp_con!=NULL)
					{
					}
				}
				
		}
		
	}
	for(int i=0;i<nde->NumberOfChildren();i++)
	{
		PlaceZeroKeys(nde->GetChildNode(i));
	}
}

bool LegMirrorPanel::HasCircularReference(INode *nde, ExprControl *expc)
{
	int i,slot;
	TSTR nname;
	CString str(nde->GetName()),ndename;

	for(i=0;i<expc->getVarCount(VECTOR_EXPR);i++)
	{
		slot=expc->vVars[i].refID;
		expc->refTab[slot].client->NotifyDependents(FOREVER,(PartID)&nname,REFMSG_GET_NODE_NAME);
		ndename=nname;
		if(!str.CompareNoCase(ndename)) return true;
	}	
	
	
	for(i=0;i<expc->getVarCount(SCALAR_VAR);i++)
	{
		slot=expc->sVars[i].refID;
		expc->refTab[slot].client->NotifyDependents(FOREVER,(PartID)&nname,REFMSG_GET_NODE_NAME);
		ndename=nname;
		if(!str.CompareNoCase(ndename)) return true;
	}
	
	return false;
}

void LegMirrorPanel::FixExpressions(INode *nde)
{
	
	for(int i=0;i<nde->NumberOfChildren();i++)
	{
		FixExpressions(nde->GetChildNode(i));
	}
	
	CString str(nde->GetName());
	
	Control *tm_con,*pos_con,*rot_con,*xp_con,*yp_con,*zp_con,*xr_con,*yr_con,*zr_con;
	
	tm_con = nde->GetTMController();
	if(tm_con!=NULL)
	{
		pos_con = tm_con->GetPositionController();
		rot_con = tm_con->GetRotationController();
		
		if(rot_con)
		{
			xr_con=rot_con->GetXController();
			if(xr_con!=NULL)
			{
				if(!GetKeyControlInterface(xr_con) &&
					IsExprControl(xr_con) && 
					HasCircularReference(nde,(ExprControl *)xr_con))
				{
					FixExpControl(nde,(ExprControl *)xr_con,0);
				}
			}
			
			yr_con=rot_con->GetYController();
			if(yr_con!=NULL)
			{
				if(!GetKeyControlInterface(yr_con) 
					&& IsExprControl(yr_con)&& 
					HasCircularReference(nde,(ExprControl *)yr_con))
				{
					FixExpControl(nde,(ExprControl *)yr_con,1);
				}
			}
			
			zr_con=rot_con->GetZController();
			if(zr_con!=NULL)
			{
				if(!GetKeyControlInterface(zr_con) 
					&& IsExprControl(zr_con)&& 
					HasCircularReference(nde,(ExprControl *)zr_con))
				{
					FixExpControl(nde,(ExprControl *)zr_con,2);
				}
			}
		}
		
		
		if(pos_con)
		{
			xp_con=pos_con->GetXController();
			if(xp_con!=NULL)
			{
				if(!GetKeyControlInterface(xp_con)  
					&& IsExprControl(xp_con)&& 
					HasCircularReference(nde,(ExprControl *)xp_con))
				{
					FixExpControl(nde,(ExprControl *)xp_con,0);
				}
			}
			
			yp_con=pos_con->GetYController();
			if(yp_con!=NULL)
			{
				if(!GetKeyControlInterface(yp_con)  
					&& IsExprControl(yp_con)&& 
					HasCircularReference(nde,(ExprControl *)yp_con))
				{
					FixExpControl(nde,(ExprControl *)yp_con,1);
				}
			}
			
			zp_con=pos_con->GetZController();
			if(zp_con!=NULL)
			{
				if(!GetKeyControlInterface(zp_con)  
					&& IsExprControl(zp_con)&& 
					HasCircularReference(nde,(ExprControl *)zp_con))
				{
					FixExpControl(nde,(ExprControl *)zp_con,2);
				}
			}
		}
		
	}	
}

void LegMirrorPanel::FixExpControl(INode *mynde, ExprControl *expc,int axis)
{
	int i,slot;
	TSTR nname;
	INode *nde,*tnde=0;
	Control *cpt;
	CString str(mynde->GetName());

	for(i=0;i<expc->getVarCount(VECTOR_EXPR);i++)
	{
		slot=expc->vVars[i].refID;
		expc->refTab[slot].client->NotifyDependents(FOREVER,(PartID)&nname,REFMSG_GET_NODE_NAME);
		
		tnde=NULL;
		if( IsTorsoJoint(str) )
		{
			
		if(axis==0)
			tnde=FindNodeByName(m_ip->GetRootNode(),CString("joint_hip"));
		else
			tnde=FindNodeByName(m_ip->GetRootNode(),CString("joint_ROOT"));
			
		}
		else
			if(TellSide(mynde)!=ISNEITHER)
			{
				nde=m_ip->GetINodeByName(nname);
				tnde=FindCorespondingNode(nde);
			}
			else
			{
				// Circular Reference on node cannot be handled
			}
			
			if(tnde!=NULL)
			{
				int sanim;
				CString conname(expc->refTab[slot].client->SubAnimName(expc->vVars[i].subNum));	
				if(tnde->GetTMController())
				{
					cpt=GetControlByName(tnde->GetTMController(),conname,&sanim);
				ASSERT(cpt);
				expc->assignController(VECTOR_EXPR,i,cpt,sanim);
				}
			}
			
	}	

	
	for(i=0;i<expc->getVarCount(SCALAR_VAR);i++)
	{
		slot=expc->sVars[i].refID;
		expc->refTab[slot].client->NotifyDependents(FOREVER,(PartID)&nname,REFMSG_GET_NODE_NAME);
		
		
		if( IsTorsoJoint(str) )
		{

		if(axis==0)
			tnde=FindNodeByName(m_ip->GetRootNode(),CString("joint_hip"));
		else
			tnde=FindNodeByName(m_ip->GetRootNode(),CString("joint_ROOT"));
			
		}
		else
			if(TellSide(mynde)!=ISNEITHER)
			{
				nde=m_ip->GetINodeByName(nname);
				tnde=FindCorespondingNode(nde);
			}
			else
			{
				// Circular Reference on node cannot be handled
			}
			
			if(tnde!=NULL)
			{
				int sanim;
				CString conname(expc->refTab[slot].client->SubAnimName(expc->sVars[i].subNum));	
				cpt=GetControlByName(tnde->GetTMController(),conname,&sanim);
				ASSERT(cpt);
				expc->assignController(SCALAR_VAR,i,cpt,sanim);
			}			
	}	
}

void LegMirrorPanel::DoFixExp()
{
	FixExpressions(m_ip->GetRootNode());
}

void LegMirrorPanel::DoExportAnims()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	HINSTANCE loc_hInstance = AfxGetResourceHandle ();
	CString tdir;
	CFileDialog fdiag(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT,"Max Files (*.max)|*.max||",NULL); 
	char *filebuf=new char[MAX_PATH*200];
	filebuf[0]=0;
	fdiag.m_ofn.lpstrFile=filebuf;
	fdiag.m_ofn.nMaxFile=MAX_PATH*200;

	if(fdiag.DoModal()==IDOK)
	{
		char pname[MAX_PATH];
		BROWSEINFO bi; 
		bi.hwndOwner =NULL; 
		
		bi.pidlRoot = NULL; 
		bi.pszDisplayName = NULL; 
		bi.lpszTitle = "Choose Destination Path..."; 
		bi.ulFlags = BIF_RETURNONLYFSDIRS; 
		bi.lpfn = NULL; 
		bi.lParam = (LPARAM)loc_hInstance; 
		
		LPITEMIDLIST pidl = SHBrowseForFolder(&bi); 
		
		if (SHGetPathFromIDList(pidl,pname))  
		{
			tdir=pname;
		}
		
		if (bi.pidlRoot) 
		{ 
			ILFree( const_cast< ITEMIDLIST* >( bi.pidlRoot ) );
		} 
		
		POSITION pos;
		
		pos=fdiag.GetStartPosition();
		CString finalname,fname;
		
		while(pos!=NULL)
		{
			
			fname=fdiag.GetNextPathName(pos);
			m_ip->LoadFromFile(fname,TRUE);

			finalname=fname.Left(fname.GetLength()-4);
			finalname=finalname.Right(finalname.GetLength()-finalname.ReverseFind('\\'));
			finalname=tdir+finalname+".MW4ANIM";
			
			m_ip->ExportToFile(finalname,TRUE);
	
		}

/*		finalname=fname.Left(fname.GetLength()-4);
		finalname=finalname.Right(finalname.GetLength()-finalname.ReverseFind('\\'));
		finalname=tdir+finalname+".armature";
			
		m_ip->ExportToFile(finalname,FALSE);
*/
	}

//delete filebuf;
}

void LegMirrorPanel::FixScale()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	HINSTANCE loc_hInstance = AfxGetResourceHandle ();
	CString tdir;
	CFileDialog fdiag(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT,"Max Files (*.max)|*.max||",NULL); 
	char *filebuf=new char[MAX_PATH*200];
	filebuf[0]=0;
	fdiag.m_ofn.lpstrFile=filebuf;
	fdiag.m_ofn.nMaxFile=MAX_PATH*200;

	if(fdiag.DoModal()==IDOK)
	{

		
		POSITION pos;
		
		pos=fdiag.GetStartPosition();
		CString finalname,fname;
		
		while(pos!=NULL)
		{
			
			fname=fdiag.GetNextPathName(pos);
			m_ip->LoadFromFile(fname,TRUE);

			
			INode *vel_node = g_hillAdjustPanel.FindNode(m_ip->GetRootNode(), "joint_VEL");
			Check_Pointer(vel_node);


			INode *root_node = g_hillAdjustPanel.FindNode(m_ip->GetRootNode(), "joint_ROOT");
			Check_Pointer(vel_node);


			Stuff::Point3D scale_factor;

			Matrix3 axis(MAT_IDENT);

			SuspendAnimate();
			AnimateOff();

			ScaleValue orig_value;


			Control *scale = vel_node->GetTMController()->GetScaleController();

			Quat quat;
			quat.Identity();

			ScaleValue scale_value(Point3(1.0f,1.0f,1.0f), quat);
			scale->GetValue(0, &orig_value, FOREVER, CTRL_ABSOLUTE);
			scale->SetValue(0, &scale_value, 1, CTRL_ABSOLUTE);

			scale_factor.x = orig_value.s.x;
			scale_factor.y = orig_value.s.y;
			scale_factor.z = orig_value.s.z;


			scale = root_node->GetTMController()->GetScaleController();
			scale->SetValue(0, &scale_value, 1, CTRL_ABSOLUTE);

			ScaleNodePosition(root_node, scale_factor);

			ResumeAnimate();
			
			m_ip->FileSave();

			
		}

		//finalname=fname.Left(fname.GetLength()-4);
		//finalname=finalname.Right(finalname.GetLength()-finalname.ReverseFind('\\'));
		//finalname=tdir+finalname+".armature";
			
		//m_ip->ExportToFile(finalname,FALSE);

	}

		m_ip->RedrawViews(m_ip->GetTime(),REDRAW_BEGIN);
		m_ip->RedrawViews(m_ip->GetTime(),REDRAW_NORMAL);
		m_ip->RedrawViews(m_ip->GetTime(),REDRAW_END);

//delete filebuf;
}


// This actually only scales the tangents
void LegMirrorPanel::ScaleKey(Control *c, IKeyControl *ikeys, Stuff::Scalar amount,float median,int key_number)
{
	{
		if (c->ClassID() == Class_ID(LININTERP_FLOAT_CLASS_ID,0))
		{
			return;
		}
		else if (c->ClassID() == Class_ID(TCBINTERP_FLOAT_CLASS_ID,0))
		{
			return;
		}

		else if(c->ClassID() == Class_ID(HYBRIDINTERP_FLOAT_CLASS_ID,0))
		{
			IBezFloatKey key;
			ikeys->GetKey(key_number, &key);

			key.val = ((key.val-median)*amount)+median;
			if (GetOutTanType(key.flags) == BEZKEY_USER && key_number < ikeys->GetNumKeys()-1)
			{
				key.outtan *= amount;
			}

			if (GetInTanType(key.flags) == BEZKEY_USER && key_number > 0)
			{
				key.intan *= amount;
			}

			ikeys->SetKey(key_number, &key);
		}
		return;
	}


}

//===========================================================================//

void LegMirrorPanel::ScaleNodeAll(Control *c, float scale)
{
	if (_isnan(scale))
		return;

	IKeyControl *keys = GetKeyControlInterface(c);

	if (keys == NULL)
	{
		//SPEW(("jerryeds", "FUCKED UP"));
		ScaleKey(c, keys, scale,0.0, -1);
		return;
	}

	float min = 0.0,max = 0.0;

	/*for (int i = 0; i < keys->GetNumKeys(); ++i)
	{
		int time = GetKeyTime(c, keys, i);
		float value;
		c->GetValue(time,&value,FOREVER,CTRL_ABSOLUTE);
		if (value < min)
			min = value;
		if (value > max)
			max = value;
	}*/

	if (keys->GetNumKeys() >= 0)
	{
		for (int i= 0; i < keys->GetNumKeys(); ++i)
		{
			ScaleKey(c, keys, scale, min+(max-min)/2,i);
		}
	}
}

//===========================================================================//

void LegMirrorPanel::ScaleNodePosition(INode *node, Stuff::Point3D scale)
{
		
	Control *tm_control = node->GetTMController();
	Check_Pointer(tm_control);

	Control *position_controler = tm_control->GetPositionController();
	Check_Pointer(position_controler);

	
	Control *x_controler = position_controler->GetXController();
	Check_Pointer(x_controler);

	Control *y_controler = position_controler->GetYController();
	Check_Pointer(y_controler);

	Control *z_controler = position_controler->GetZController();
	Check_Pointer(z_controler);

	ScaleNodeAll(x_controler, scale.x);
	ScaleNodeAll(y_controler, scale.y);
	ScaleNodeAll(z_controler, scale.z);
}

//===========================================================================//
/*
int LegMirrorPanel::GetKeyTime(Control *c, IKeyControl *ikeys, int key_number)
{
	int value = 0;

	if (c->ClassID() == Class_ID(LININTERP_FLOAT_CLASS_ID,0))
	{
		ILinFloatKey key;
		ikeys->GetKey(key_number, &key);

		value = key.time;
	}
	else if (c->ClassID() == Class_ID(TCBINTERP_FLOAT_CLASS_ID,0))
	{
		ITCBFloatKey key;
		ikeys->GetKey(key_number, &key);

		value = key.time;
	}

	else if(c->ClassID() == Class_ID(HYBRIDINTERP_FLOAT_CLASS_ID,0))
	{
		IBezFloatKey key;
		ikeys->GetKey(key_number, &key);
		value = key.time;
	}

	return value;
}

*/

void LegMirrorPanel::ILFree(LPITEMIDLIST pidl) 
{ 
	LPMALLOC pMalloc; 
	
    if (pidl) 
    { 
#ifdef DEBUG 
		UINT cbSize = ILGetSize(pidl); 
		VALIDATE_PIDL(pidl); 
		
		// Fill the memory with some bad value... 
		_fmemset(pidl, 0xE5, cbSize); 
		
		// If large enough try to save the call return address of who 
		// freed us in the 3-6 byte of the structure. 
		if (cbSize >= 6) 
			*((UINT*)((LPSTR)pidl + 2)) =  *(((UINT*)&pidl) - 1); 
		
#endif 
		SHGetMalloc(&pMalloc); 
		pMalloc->Free( pidl); 
		pMalloc->Release(); 
		
    } 
} 

void LegMirrorPanel::ExportLights()
{
	MessageBox(NULL,"Disabled.  Use the File | Export menu option to export lights.","Animation Suite",MB_OK);
}

void LegMirrorPanel::ImportMission()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	HINSTANCE loc_hInstance = AfxGetResourceHandle ();
	CString tdir;
	CFileDialog fdiag(TRUE,NULL,NULL,0,"Contents Files (*.contents)|*.contents||",NULL); 
	//char *filebuf=new char[MAX_PATH*200];
	//filebuf[0]=0;
	//fdiag.m_ofn.lpstrFile=filebuf;
	//fdiag.m_ofn.nMaxFile=MAX_PATH*200;

	CString gamepath = GetGamePath(m_ip);
	fdiag.m_ofn.lpstrInitialDir = gamepath+"content\\missions";
	if(fdiag.DoModal()==IDOK)
	{
		m_ip->ImportFromFile(fdiag.GetPathName(), TRUE);
	}
}



