// EffectListTreeCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "pixelwhippro.h"
#include "EffectListTreeCtrl.h"
#include "EffectListWin.h"
#include "Eventprops.h"

#define MAXPARTICLES 20
#define MAXPROFILES 5
using namespace MidLevelRenderer;

extern CPixelWhipProApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CEffectListTreeCtrl

CEffectListTreeCtrl::CEffectListTreeCtrl()
{
	selitm=NULL;
}

CEffectListTreeCtrl::~CEffectListTreeCtrl()
{
}


BEGIN_MESSAGE_MAP(CEffectListTreeCtrl, CTreeCtrl)
//{{AFX_MSG_MAP(CEffectListTreeCtrl)
ON_NOTIFY_REFLECT(TVN_BEGINDRAG, OnBegindrag)
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
ON_WM_RBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEffectListTreeCtrl message handlers

void CEffectListTreeCtrl::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	
	*pResult = 0;
}

void CEffectListTreeCtrl::OnLButtonUp(UINT nFlags, CPoint point) 
{
	
	UINT pFlags; 
	HTREEITEM itm;
	itm=HitTest(point,&pFlags);
	
	CString str1,str2;
	str1=GetItemText(itm);
	str2=GetItemText(selitm);
	
	ReleaseCapture();
	if(selitm!=NULL &&  itm!=selitm)
	{
		gosFX::Effect::Specification *nspec=(gosFX::Effect::Specification *)GetItemData(selitm);
		Check_Object(nspec);
		gosFX::Effect::Specification *spec=NULL;
		int newid;
		
		if(itm!=NULL)
		{
			spec=(gosFX::Effect::Specification *)GetItemData(itm);
			Check_Object(spec);
			
			if(spec->GetClassID()==gosFX::EffectCloudClassID)
			{
				gosFX::EffectCloud__Specification *tspec;
				tspec=Cast_Pointer(gosFX::EffectCloud__Specification *,spec);
				Check_Object(tspec);
				
				if(tspec->m_particleEffectID!=BADINDEX) return;
				
			}
		}
		
		if(nFlags&MK_SHIFT)	
		{
			newid=DuplicateEffect(nspec->m_effectID);
		}
		else
		{
			
			if(GetParentItem(selitm)!=NULL)
			{	//Remove Effect from Curent List
				gosFX::Effect::Specification *parspec=(gosFX::Effect::Specification *)GetItemData(GetParentItem(selitm));
				if(parspec->GetClassID()==gosFX::EffectCloudClassID)
				{
					gosFX::EffectCloud__Specification *tspec;
					tspec=Cast_Pointer(gosFX::EffectCloud__Specification *,parspec);
					Check_Object(tspec);
					
					tspec->m_particleEffectID=BADINDEX;
				}
				else
				{
					//			gos_PushCurrentHeap(gosFX::Heap);
					Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(parspec->m_events));
					gosFX::Event *tmpevent;
					
					eitr.First();
					while(NULL!=(tmpevent=eitr.ReadAndNext()) && tmpevent->m_effectID!=nspec->m_effectID);
					ASSERT(tmpevent!=NULL); //Fatal Error
					Unregister_Object(tmpevent);
					delete tmpevent;
					//			gos_PopCurrentHeap();
				}
			}
			newid=nspec->m_effectID;
			
		}
		
		if(itm!=NULL)
		{	//Add the event
			if(spec->GetClassID()==gosFX::EffectCloudClassID)
			{
				gosFX::EffectCloud__Specification *tspec;
				tspec=Cast_Pointer(gosFX::EffectCloud__Specification *,spec);
				Check_Object(tspec);
				tspec->m_particleEffectID=newid;
			}
			else
			{
				gos_PushCurrentHeap(gosFX::Heap);
				gosFX::Event *event=new gosFX::Event;
				Register_Object(event);
				
				//Place in New List
				event->m_effectID=newid;
				event->m_time=0.0f;
				event->m_localToParent=Stuff::LinearMatrix4D::Identity;
				spec->AdoptEvent(event);
				
				SelectItem(itm);
				Expand(itm,TVE_EXPAND); 
				gos_PopCurrentHeap();
			}
		}
		((CEffectListWin *)GetParent())->Refresh();
	}
	selitm=NULL;
	UpdatePlayIcons();
	CTreeCtrl::OnLButtonUp(nFlags, point);
}

void CEffectListTreeCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CTreeCtrl::OnLButtonDown(nFlags, point);
	
	UINT pFlags; 
	selitm=HitTest(point,&pFlags);
	CString str2(GetItemText(selitm));
	SetCapture();
	
}

int CEffectListTreeCtrl::DuplicateEffect(int efnum)
{
	Verify(efnum<gosFX::EffectLibrary::Instance->m_effects.GetLength());
	
	Stuff::DynamicMemoryStream *stream=new Stuff::DynamicMemoryStream;
	gosFX::Effect::Specification *old_spec=(gosFX::Effect::Specification *)gosFX::EffectLibrary::Instance->m_effects[efnum];
	Register_Object(stream);
	old_spec->Save(stream);
	gosFX::Effect::Specification *new_spec;
	stream->Rewind();
	new_spec=gosFX::Effect::Specification::Create(stream,gosFX::CurrentGFXVersion);
	CString newname(new_spec->m_name);
	newname=MakeUnique(newname);
	new_spec->m_name=(LPCSTR)newname;
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = new_spec;	//(adopt pointer)
	new_spec->m_effectID = new_index;
	
	//	Unregister_Object(stream);
	//	delete stream;
	
	if(new_spec->GetClassID()==gosFX::EffectCloudClassID)
	{
		gosFX::EffectCloud__Specification *tspec;
		tspec=Cast_Pointer(gosFX::EffectCloud__Specification *,new_spec);
		Check_Object(tspec);
		
		tspec->m_particleEffectID=DuplicateEffect(tspec->m_particleEffectID);
	}
	else
	{
		Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(new_spec->m_events));
		gosFX::Event *tmpevent;
		
		eitr.First();
		while(NULL!=(tmpevent=eitr.ReadAndNext()))
		{
			tmpevent->m_effectID=DuplicateEffect(tmpevent->m_effectID);
		}
	}
	return new_index;
}


CString CEffectListTreeCtrl::MakeUnique(CString &istr)
{
	if(!InList(istr)) return istr;
	
	CString bstr;
	
	if(istr.FindOneOf("0123456789")==-1)
		bstr=istr;
	else
		bstr=istr.Left(istr.FindOneOf("0123456789"));
	
	int num=0;
	CString fstr;
	do
	{
		fstr.Format("%s%04i",bstr,num);
		num++;
	}
	while(InList(fstr));
	
	return fstr;
	
}

bool CEffectListTreeCtrl::InList(CString &str,HTREEITEM itm)
{
	int i;
	
	for(i=0;i<gosFX::EffectLibrary::Instance->m_effects.GetLength();i++)
	{
		gosFX::Effect::Specification *spec=(gosFX::Effect::Specification *)gosFX::EffectLibrary::Instance->m_effects[i];
		Check_Object(spec);
		CString ename(spec->m_name);
		if(!str.CompareNoCase(ename))
			return true;
	}
	
	return false;
}

void CEffectListTreeCtrl::DeleteEffect(gosFX::Effect::Specification *spec)
{
	int esum,added;
	
	esum=EffectsInBranch(spec);
	gos_PushCurrentHeap(gosFX::Heap);
	gosFX::Effect::Specification **ToDelete=new gosFX::Effect::Specification *[esum];
	gos_PopCurrentHeap();
	Register_Pointer(ToDelete);
	added=ListChildren(spec,ToDelete);
	Verify(added==esum);
	DetachReferences(spec,GetParentEffect(spec));
	int dfx,curfx;
	
	for(dfx=0;dfx<esum;dfx++)
	{
		
		for(curfx=ToDelete[dfx]->m_effectID;
		curfx<gosFX::EffectLibrary::Instance->m_effects.GetLength()-1;
		curfx++)
		{
			gosFX::EffectLibrary::Instance->m_effects[curfx]=gosFX::EffectLibrary::Instance->m_effects[curfx+1];
			gosFX::EffectLibrary::Instance->m_effects[curfx]->m_effectID--;
		}
		
		gosFX::EffectLibrary::Instance->m_effects.SetLength(gosFX::EffectLibrary::Instance->m_effects.GetLength()-1);
		
		theApp.FixEffectNameTable();
		
		for(curfx=0;
		curfx<gosFX::EffectLibrary::Instance->m_effects.GetLength();
		curfx++)
		{
			gosFX::Effect::Specification *lspec=gosFX::EffectLibrary::Instance->m_effects[curfx];
			if(lspec->GetClassID()==gosFX::EffectCloudClassID)
			{
				gosFX::EffectCloud__Specification *tspec;
				tspec=Cast_Pointer(gosFX::EffectCloud__Specification *,lspec);
				Check_Object(tspec);
				
				if(tspec->m_particleEffectID!=BADINDEX && tspec->m_particleEffectID>ToDelete[dfx]->m_effectID)
					tspec->m_particleEffectID--;
				
			}
			else
			{
				Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(lspec->m_events));
				gosFX::Event *tmpevent;
				eitr.First();
				while(NULL!=(tmpevent=eitr.ReadAndNext()) )
					if(tmpevent->m_effectID>ToDelete[dfx]->m_effectID)
					{
						tmpevent->m_effectID--;
						Verify(tmpevent->m_effectID!=lspec->m_effectID);
					}
			}
			
			
		}
	}
	
	theApp.FixEffectNameTable();
	theApp.VerifyEffectLib();
	Unregister_Pointer(ToDelete);
	delete [] ToDelete;
	
}

int CEffectListTreeCtrl::NewCardCloud() 
{
	CString DefaultName("CardCloud"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::CardCloud::Specification
		*spec = new gosFX::CardCloud::Specification;
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	spec->m_state.SetAlphaMode(MidLevelRenderer::MLRState::AlphaInvAlphaMode);
	spec->m_state.SetPriority(MidLevelRenderer::MLRState::AlphaPriority);
	spec->m_maxParticleCount=MAXPARTICLES;
	//End Defaults
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	
	return new_index;
}

int CEffectListTreeCtrl::NewPertcloud() 
{
	CString DefaultName("PertCloud"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	unsigned
		sides = 5;
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::PertCloud::Specification
		*spec = new gosFX::PertCloud::Specification(sides);
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	spec->m_state.SetAlphaMode(MidLevelRenderer::MLRState::AlphaInvAlphaMode);
	spec->m_state.SetPriority(MidLevelRenderer::MLRState::AlphaPriority);
	spec->m_maxParticleCount=MAXPARTICLES;
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	return new_index;
}

int CEffectListTreeCtrl::NewPointCloud() 
{
	CString DefaultName("PointCloud"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::PointCloud::Specification
		*spec = new gosFX::PointCloud::Specification(gosFX::PointCloudClassID);
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	spec->m_maxParticleCount=MAXPARTICLES;
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	
	gos_PopCurrentHeap();
	return new_index;
}

int CEffectListTreeCtrl::NewPointLight() 
{
	CString DefaultName("PointLight"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::PointLight::Specification
		*spec = new gosFX::PointLight::Specification;
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	
	gos_PopCurrentHeap();
	return new_index;
}

int CEffectListTreeCtrl::NewTube() 
{
	CString DefaultName("Tube"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::Tube::Specification
		*spec = new gosFX::Tube::Specification;
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	spec->m_maxProfileCount=MAXPROFILES;
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	
	gos_PopCurrentHeap();
	return new_index;
}

int CEffectListTreeCtrl::NewShapeCloud() 
{
	CString DefaultName("ShapeCloud"),AuxName;
	DefaultName=MakeUnique(DefaultName);
	

	
	gos_PushCurrentHeap(gosFX::Heap);
	gosFX::ShapeCloud::Specification
		*spec = new gosFX::ShapeCloud::Specification;
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	spec->m_maxParticleCount=MAXPARTICLES;
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	
	return new_index;
}

int CEffectListTreeCtrl::NewShardCloud() 
{
	CString DefaultName("ShardCloud"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::ShardCloud::Specification
		*spec = new gosFX::ShardCloud::Specification;
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	spec->m_maxParticleCount=MAXPARTICLES;
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	return new_index;
}

int CEffectListTreeCtrl::NewDebrisCloud()
{
	CString DefaultName("DebrisCloud"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::DebrisCloud::Specification
		*spec = new gosFX::DebrisCloud::Specification;
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	return new_index;
	
}

void CEffectListTreeCtrl::DeleteSelectedEffect() 
{
	if(NULL==GetSelectedItem()) return;
	DeleteEffect((gosFX::Effect::Specification *)GetItemData(GetSelectedItem()));
	
}

int CEffectListTreeCtrl::NewSingletonCard() 
{
	CString DefaultName("CardSingleton"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::Card::Specification
		*spec = new gosFX::Card::Specification(gosFX::CardClassID);
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	spec->m_state.SetAlphaMode(MidLevelRenderer::MLRState::AlphaInvAlphaMode);
	spec->m_state.SetPriority(MidLevelRenderer::MLRState::AlphaPriority);
	//spec->m_maxParticleCount=MAXPARTICLES;
	//End Defaults
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	
	return new_index;
}

int CEffectListTreeCtrl::NewSingletonFlare() 
{
	CString DefaultName("FlareSingleton"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::Flare::Specification
		*spec = new gosFX::Flare::Specification;
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	spec->m_state.SetAlphaMode(MidLevelRenderer::MLRState::AlphaInvAlphaMode);
	spec->m_state.SetPriority(MidLevelRenderer::MLRState::AlphaPriority);
	
	//End Defaults
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	
	return new_index;
}

int CEffectListTreeCtrl::NewSingletonShape() 
{
	CString DefaultName("ShapeSingleton"),AuxName;
	DefaultName=MakeUnique(DefaultName);
	
	
	MLRState state;
	
	gos_PushCurrentHeap(gosFX::Heap);
	gosFX::Shape::Specification			
		*spec = new gosFX::Shape::Specification;
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	//			spec->m_maxParticleCount=MAXPARTICLES;
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	
	return new_index;
}

int CEffectListTreeCtrl::NewEffectCloud()
{
	CString DefaultName("EffectCloud"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::EffectCloud::Specification
		*spec = new gosFX::EffectCloud::Specification;
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	spec->m_state.SetAlphaMode(MidLevelRenderer::MLRState::AlphaInvAlphaMode);
	spec->m_state.SetPriority(MidLevelRenderer::MLRState::AlphaPriority);
	spec->m_particleEffectID=BADINDEX;
	spec->m_maxParticleCount=MAXPARTICLES;
	//End Defaults
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	
	return new_index;
	
}

int CEffectListTreeCtrl::NewNullEffect()
{
	CString DefaultName("NullEffect"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	
	DefaultName=MakeUnique(DefaultName);
	gosFX::Effect::Specification
		*spec = new gosFX::Effect::Specification;
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	return new_index;
	
}

void CEffectListTreeCtrl::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	UINT pFlags; 
	
	HTREEITEM selitm=HitTest(cmousepos,&pFlags);;
	gosFX::Effect::Specification *nspec=(gosFX::Effect::Specification *)GetItemData(selitm);
	
	if(GetParentItem(selitm)!=NULL)
	{	//Remove Effect from Curent List
		gosFX::Effect::Specification *parspec=(gosFX::Effect::Specification *)GetItemData(GetParentItem(selitm));
		if(parspec->GetClassID()!=gosFX::EffectCloudClassID)
		{
			Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(parspec->m_events));
			gosFX::Event *tmpevent;
			
			eitr.First();
			while(NULL!=(tmpevent=eitr.ReadAndNext()) && tmpevent->m_effectID!=nspec->m_effectID);
			ASSERT(tmpevent!=NULL); //Fatal Error
			
			CEventProps propdlg;
			propdlg.DoModal(tmpevent);
			theApp.StopEffects();
			SortEvents(parspec);
			theApp.StartEffects();
			UpdatePlayIcons();
		}
	}
	*pResult = 0;
}


void CEffectListTreeCtrl::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CTreeCtrl::OnRButtonUp(nFlags, point);
}

void CEffectListTreeCtrl::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	cmousepos=point;
	CTreeCtrl::OnMouseMove(nFlags, point);
}

int CEffectListTreeCtrl::EffectsInBranch(gosFX::Effect::Specification *spec,int par)
{
	int bsum=1;
				if(spec->GetClassID()==gosFX::EffectCloudClassID)
				{
					gosFX::EffectCloud__Specification *tspec;
					tspec=Cast_Pointer(gosFX::EffectCloud__Specification *,spec);
					Check_Object(tspec);
					
					if(tspec->m_particleEffectID!=BADINDEX)
						bsum+=EffectsInBranch(gosFX::EffectLibrary::Instance->m_effects[tspec->m_particleEffectID],par);
				}
				else
				{
					Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(spec->m_events));
					gosFX::Event *tmpevent;
					eitr.First();
					while(NULL!=(tmpevent=eitr.ReadAndNext()))
						bsum+=EffectsInBranch(gosFX::EffectLibrary::Instance->m_effects[tmpevent->m_effectID],par);
				}
				
				return par+bsum;
}

int CEffectListTreeCtrl::ListChildren(gosFX::Effect::Specification *spec,gosFX::Effect::Specification **speclist)
{
	int added=0;
	speclist[added]=spec;
	added++;
				if(spec->GetClassID()==gosFX::EffectCloudClassID)
				{
					gosFX::EffectCloud__Specification *tspec;
					tspec=Cast_Pointer(gosFX::EffectCloud__Specification *,spec);
					Check_Object(tspec);
					
					if(tspec->m_particleEffectID!=BADINDEX)
						added+=ListChildren(gosFX::EffectLibrary::Instance->m_effects[tspec->m_particleEffectID],speclist+added);
				}
				else
				{
					Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(spec->m_events));
					gosFX::Event *tmpevent;
					eitr.First();
					while(NULL!=(tmpevent=eitr.ReadAndNext()))
						added+=ListChildren(gosFX::EffectLibrary::Instance->m_effects[tmpevent->m_effectID],speclist+added);
				}
				return added;
}

gosFX::Effect::Specification *CEffectListTreeCtrl::GetParentEffect(gosFX::Effect::Specification *spec)
{
	int cureff;
	
	
	for(cureff=0;cureff<gosFX::EffectLibrary::Instance->m_effects.GetLength();cureff++)
	{
		gosFX::Effect::Specification *lspec=gosFX::EffectLibrary::Instance->m_effects[cureff];
		if(lspec->GetClassID()==gosFX::EffectCloudClassID)
		{
			gosFX::EffectCloud__Specification *tspec;
			tspec=Cast_Pointer(gosFX::EffectCloud__Specification *,lspec);
			Check_Object(tspec);
			
			if(tspec->m_particleEffectID==spec->m_effectID) return tspec;
		}
		else
		{
			
			Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(lspec->m_events));
			gosFX::Event *tmpevent;
			
			eitr.First();
			while(NULL!=(tmpevent=eitr.ReadAndNext()))
			{
				if(tmpevent->m_effectID==spec->m_effectID)
					return lspec;
			}
		}
	}
	return NULL;
}

void CEffectListTreeCtrl::DetachReferences(gosFX::Effect::Specification *spec,gosFX::Effect::Specification *parspec)
{
	//Detach Children First
				if(spec->GetClassID()==gosFX::EffectCloudClassID)
				{
					gosFX::EffectCloud__Specification *tspec;
					tspec=Cast_Pointer(gosFX::EffectCloud__Specification *,spec);
					Check_Object(tspec);
					
					if(tspec->m_particleEffectID!=BADINDEX)
						DetachReferences(gosFX::EffectLibrary::Instance->m_effects[tspec->m_particleEffectID],spec);
					
				}
				else
				{
					Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(spec->m_events));
					gosFX::Event *tmpevent;
					eitr.First();
					while(NULL!=(tmpevent=eitr.ReadAndNext()))
						DetachReferences(gosFX::EffectLibrary::Instance->m_effects[tmpevent->m_effectID],spec);
				}
				
				
				//Detach Local
				if(parspec!=NULL)
				{
					if(parspec->GetClassID()==gosFX::EffectCloudClassID)
					{
						gosFX::EffectCloud__Specification *tspec;
						tspec=Cast_Pointer(gosFX::EffectCloud__Specification *,parspec);
						Check_Object(tspec);
						
						if(tspec->m_particleEffectID==spec->m_effectID)
						{
							tspec->m_particleEffectID=BADINDEX;
							return;
						}
						
					}
					else
					{
						Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(parspec->m_events));
						gosFX::Event *tmpevent;
						eitr.First();
						while(NULL!=(tmpevent=eitr.ReadAndNext()) )
							if(tmpevent->m_effectID==spec->m_effectID)
							{
								Unregister_Object(tmpevent);
								delete tmpevent;
								return;
							}
					}
				}
				
				
				
}


void CEffectListTreeCtrl::SortEvents(gosFX::Effect::Specification *spec)
{
	bool changed;
	
	int ESize,CEvent=0;
	
	Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(spec->m_events));
	ESize=eitr.GetSize();
	gosFX::Event **eventlist=new gosFX::Event*[ESize];
	gosFX::Event *tmpevent,*lastevent,evbuf;
	
	while(NULL!=(tmpevent=eitr.ReadAndNext() ))
	{
		eventlist[CEvent++]=tmpevent;
		spec->m_events.Remove(tmpevent);
	}
	
	
	do
	{
		changed=false;
		lastevent=eventlist[0];
		for(CEvent=1;CEvent<ESize;CEvent++)
		{
			tmpevent=eventlist[CEvent];
			
			if(tmpevent->m_time<lastevent->m_time)
			{
				evbuf=*tmpevent;
				*tmpevent=*lastevent;
				*lastevent=evbuf;
				changed=true;
			}
			
			lastevent=tmpevent;
			
		}
		
		
	}
	while(changed);
	
	
	
	for(CEvent=0;CEvent<ESize;CEvent++)
	{
		spec->m_events.Add(eventlist[CEvent]);
	}
	
	delete eventlist;
}

int CEffectListTreeCtrl::NewBeam()
{
	CString DefaultName("Beam"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	DefaultName=MakeUnique(DefaultName);
	
	gosFX::Beam::Specification
		*spec = new gosFX::Beam::Specification;
	
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	spec->m_maxProfileCount=MAXPROFILES;
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	
	gos_PopCurrentHeap();
	return new_index;
	
}

int CEffectListTreeCtrl::NewSpriteCloud()
{
	CString DefaultName("SpriteCloud"),AuxName;
	gos_PushCurrentHeap(gosFX::Heap);
	DefaultName=MakeUnique(DefaultName);
	
	gosFX::SpriteCloud::Specification
		*spec = new gosFX::SpriteCloud::Specification;
	
	spec->m_name = (LPCSTR)DefaultName;
	spec->BuildDefaults();
	
	
	long new_index = gosFX::EffectLibrary::Instance->m_effects.GetLength();
	gosFX::EffectLibrary::Instance->m_effects.SetLength(new_index + 1);
	gosFX::EffectLibrary::Instance->m_effects[new_index] = spec;	//(adopt pointer)
	spec->m_effectID = new_index;
	spec->m_maxParticleCount=MAXPARTICLES;
	
	
	gos_PopCurrentHeap();
	return new_index;
	
}


void CEffectListTreeCtrl::MakeAllNamesUnique()
{
	
	int cureff;
	CString tmpname;
	
	for(cureff=0;cureff<gosFX::EffectLibrary::Instance->m_effects.GetLength();cureff++)
	{
		gosFX::Effect::Specification *spec=gosFX::EffectLibrary::Instance->m_effects[cureff];
		tmpname=spec->m_name;
		spec->m_name="!@##$$#^%&%$%&$@#$";
		tmpname=MakeUnique(tmpname);
		spec->m_name=(char *)(LPCSTR)tmpname;
	}
	
}

void CEffectListTreeCtrl::UpdatePlayIcons()
{
	UpdatePlayIcons(GetSelectedItem());
}

void CEffectListTreeCtrl::UpdatePlayIcons(HTREEITEM itm)
{
	if(itm==NULL) return;
	gosFX::Effect::Specification *nspec=(gosFX::Effect::Specification *)GetItemData(itm);
	
	Stuff::Scalar dist=theApp.GetEffectDistance();
	
	
	if(GetParentItem(itm)!=NULL && GetSelectedItem()!=itm)
	{	//Remove Effect from Curent List
		gosFX::Effect::Specification *parspec=(gosFX::Effect::Specification *)GetItemData(GetParentItem(itm));
		if(parspec->GetClassID()!=gosFX::EffectCloudClassID)
		{
			Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(parspec->m_events));
			gosFX::Event *tmpevent;
			
			eitr.First();
			while(NULL!=(tmpevent=eitr.ReadAndNext()) && tmpevent->m_effectID!=nspec->m_effectID)
			{
			}
			if(tmpevent!=NULL)
			{
				ASSERT(tmpevent!=NULL); //Fatal Error
				
				if(dist>=sqrt(tmpevent->m_nearLimit) && dist<=sqrt(tmpevent->m_farLimit)) 
				{
					SetItemImage(itm,GetIconIdx(nspec->GetClassID(),true),GetIconIdx(nspec->GetClassID(),true));
				}
				else
				{
					SetItemImage(itm,GetIconIdx(nspec->GetClassID()),GetIconIdx(nspec->GetClassID()));
				}
			}
		}
	}
	else
	{
		SetItemImage(itm,GetIconIdx(nspec->GetClassID(),true),GetIconIdx(nspec->GetClassID(),true));
		
	}
	
	
	
	HTREEITEM nitm=GetChildItem(itm);
	
	while(nitm!=NULL)
	{
		UpdatePlayIcons(nitm);
		nitm=GetNextSiblingItem(nitm);
	}
	
}

int CEffectListTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	InitImages();
	// TODO: Add your specialized creation code here
	
	return 0;
}

void CEffectListTreeCtrl::InitImages()
{
	IList.Create(16,16,ILC_COLOR24|ILC_MASK,IDX_TOTALICONS+2,0);
	IdxBlank=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_BLANK)));
	IdxPlay=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_PLAY)));
	SetImageList(&IList,TVSIL_NORMAL);
	
	IconIdx[IDX_SHAPE]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SHAPE)));
	IconIdx[IDX_NULL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_NULL)));
	IconIdx[IDX_POINTCLOUD]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_POINTCLOUD)));
	IconIdx[IDX_SHAPECLOUD]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SHAPECLOUD)));
	IconIdx[IDX_PERTCLOUD]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_PERTCLOUD)));
	IconIdx[IDX_DEBRISCLOUD]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_DEBRISCLOUD)));
	IconIdx[IDX_LASER]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_LASER)));
	IconIdx[IDX_TUBE]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_TUBE)));
	IconIdx[IDX_LIGHT]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_LIGHT)));
	IconIdx[IDX_FLARE]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_FLARE)));
	IconIdx[IDX_CARD]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_CARD)));
	IconIdx[IDX_SHARDCLOUD]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SHARDCLOUD)));
	IconIdx[IDX_CARDCLOUD]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_CARDCLOUD)));
	IconIdx[IDX_SHAPE]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SHAPE)));
	IconIdx[IDX_SPRITECLOUD]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SPRITECLOUD)));
	IconIdx[IDX_EFFECTCLOUD]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_EFFECTCLOUD)));
	
	IconIdx[IDX_NULLSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_NULLSEL)));
	IconIdx[IDX_POINTCLOUDSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_POINTCLOUDSEL)));
	IconIdx[IDX_SHAPECLOUDSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SHAPECLOUDSEL)));
	IconIdx[IDX_PERTCLOUDSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_PERTCLOUDSEL)));
	IconIdx[IDX_DEBRISCLOUDSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_DEBRISCLOUDSEL)));
	IconIdx[IDX_LASERSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_LASERSEL)));
	IconIdx[IDX_TUBESEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_TUBESEL)));
	IconIdx[IDX_LIGHTSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_LIGHTSEL)));
	IconIdx[IDX_FLARESEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_FLARESEL)));
	IconIdx[IDX_CARDSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_CARDSEL)));
	IconIdx[IDX_SHARDCLOUDSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SHARDCLOUDSEL)));
	IconIdx[IDX_CARDCLOUDSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_CARDCLOUDSEL)));
	IconIdx[IDX_SHAPESEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SHAPESEL)));
	IconIdx[IDX_SPRITECLOUDSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_SPRITECLOUDSEL)));
	IconIdx[IDX_EFFECTCLOUDSEL]=IList.Add(::LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_EFFECTCLOUDSEL)));
	
	
}


void CEffectListTreeCtrl::ClearPlayIcons(HTREEITEM itm)
{
	if(itm==NULL) return;
	
	if(itm!=TVI_ROOT)
	{
		gosFX::Effect::Specification *nspec=(gosFX::Effect::Specification *)GetItemData(itm);
		SetItemImage(itm,GetIconIdx(nspec->GetClassID()),GetIconIdx(nspec->GetClassID()));
	}
	
	HTREEITEM nitm=GetChildItem(itm);
	
	while(nitm!=NULL)
	{
		gosFX::Effect::Specification *nspec=(gosFX::Effect::Specification *)GetItemData(nitm);
		SetItemImage(nitm,GetIconIdx(nspec->GetClassID()),GetIconIdx(nspec->GetClassID()));
		ClearPlayIcons(nitm);
		nitm=GetNextSiblingItem(nitm);
	}
	
}


void CEffectListTreeCtrl::CopySelectedIntoLib(gosFX::EffectLibrary *fxLib)
{
	HTREEITEM itm=GetSelectedItem();
	CopySelectedIntoLib(fxLib,itm);
	
	
}

void CEffectListTreeCtrl::CopySelectedIntoLib(gosFX::EffectLibrary *fxLib,HTREEITEM itm)
{
	if(itm==NULL) return;
	
	
	
	gosFX::Effect::Specification *spec=(gosFX::Effect::Specification *)GetItemData(itm);
	long new_index = fxLib->m_effects.GetLength();
	fxLib->m_effects.SetLength(new_index + 1);
	fxLib->m_effects[new_index] = spec;	
	
	
	HTREEITEM nitm=GetChildItem(itm);
	
	while(nitm!=NULL)
	{
		CopySelectedIntoLib(fxLib,nitm);
		nitm=GetNextSiblingItem(nitm);
	}
	
	
	
}


int CEffectListTreeCtrl::GetIconIdx(Stuff::RegisteredClass::ClassID id,bool selected)
{
	
	if(selected==true)
	{
		switch(id)
		{
		case gosFX::CardClassID: return IconIdx[IDX_CARDSEL]; break;
		case gosFX::PointCloudClassID: return IconIdx[IDX_POINTCLOUDSEL]; break;
		case gosFX::ShapeClassID: return IconIdx[IDX_SHAPECLOUDSEL]; break;
		case gosFX::PertCloudClassID: return IconIdx[IDX_PERTCLOUDSEL]; break;
		case gosFX::DebrisCloudClassID: return IconIdx[IDX_DEBRISCLOUDSEL]; break;
		case gosFX::BeamClassID: return IconIdx[IDX_LASERSEL]; break;
		case gosFX::TubeClassID: return IconIdx[IDX_TUBESEL]; break;
		case gosFX::PointLightClassID: return IconIdx[IDX_LIGHTSEL]; break;
		case gosFX::FlareClassID: return IconIdx[IDX_FLARESEL]; break;
		case gosFX::EffectClassID: return IconIdx[IDX_NULLSEL]; break;
		case gosFX::CardCloudClassID: return IconIdx[IDX_CARDCLOUDSEL]; break;
		case gosFX::ShardCloudClassID: return IconIdx[IDX_SHARDCLOUDSEL]; break;
		case gosFX::ShapeCloudClassID: return IconIdx[IDX_SHAPECLOUDSEL]; break;
		case gosFX::SpriteCloudClassID: return IconIdx[IDX_SPRITECLOUDSEL]; break;
		case gosFX::EffectCloudClassID: return IconIdx[IDX_EFFECTCLOUDSEL]; break;
			
		default: return IdxBlank;
		}
	}
	else
	{
		switch(id)
		{
		case gosFX::CardClassID: return IconIdx[IDX_CARD]; break;
		case gosFX::PointCloudClassID: return IconIdx[IDX_POINTCLOUD]; break;
		case gosFX::ShapeClassID: return IconIdx[IDX_SHAPECLOUD]; break;
		case gosFX::PertCloudClassID: return IconIdx[IDX_PERTCLOUD]; break;
		case gosFX::DebrisCloudClassID: return IconIdx[IDX_DEBRISCLOUD]; break;
		case gosFX::BeamClassID: return IconIdx[IDX_LASER]; break;
		case gosFX::TubeClassID: return IconIdx[IDX_TUBE]; break;
		case gosFX::PointLightClassID: return IconIdx[IDX_LIGHT]; break;
		case gosFX::FlareClassID: return IconIdx[IDX_FLARE]; break;
		case gosFX::EffectClassID: return IconIdx[IDX_NULL]; break;
		case gosFX::CardCloudClassID: return IconIdx[IDX_CARDCLOUD]; break;
		case gosFX::ShardCloudClassID: return IconIdx[IDX_SHARDCLOUD]; break;
		case gosFX::ShapeCloudClassID: return IconIdx[IDX_SHAPECLOUD]; break;
		case gosFX::SpriteCloudClassID: return IconIdx[IDX_SPRITECLOUD]; break;
		case gosFX::EffectCloudClassID: return IconIdx[IDX_EFFECTCLOUD]; break;
			
		default: return IdxBlank;
		}
	}
	
	
}


void CEffectListTreeCtrl::ScaleSelectedEffect(float sfact,bool recurse,bool bright)
{
HTREEITEM itm=GetSelectedItem();


if(recurse)
{
ScaleEffectTree(itm,sfact,bright);
}
else
{
gosFX::Effect::Specification *spec=(gosFX::Effect::Specification *)GetItemData(itm);
ScaleEffect(spec,sfact,bright);
}

}

void CEffectListTreeCtrl::ScaleAllEffects(float sfact,bool bright)
{
	int effnum;
	gosFX::EffectLibrary::Instance->m_effectNames.SetLength(gosFX::EffectLibrary::Instance->m_effects.GetLength());
	for(effnum=0;effnum<gosFX::EffectLibrary::Instance->m_effects.GetLength();effnum++)
	{
		ScaleEffect(gosFX::EffectLibrary::Instance->m_effects[effnum],sfact,bright);
	}

}

void CEffectListTreeCtrl::ScaleEffectTree(HTREEITEM itm,float sfact,bool bright)
{
if(itm==NULL) return;

if(itm!=TVI_ROOT)
{
gosFX::Effect::Specification *spec=(gosFX::Effect::Specification *)GetItemData(itm);
ScaleEffect(spec,sfact,bright);


//Scale Events
		Stuff::ChainIteratorOf<gosFX::Event*> eitr(&(spec->m_events));
		gosFX::Event *tmpevent;
		
		eitr.First();
		while(NULL!=(tmpevent=eitr.ReadAndNext()))
		{
		Stuff::Point3D pnt(0,0,0);
		pnt*=tmpevent->m_localToParent;
		pnt.x*=sfact;
		pnt.y*=sfact;
		pnt.z*=sfact;
		tmpevent->m_localToParent=pnt;

		}


}	

HTREEITEM nitm=GetChildItem(itm);

	while(nitm!=NULL)
	{
		ScaleEffectTree(nitm,sfact,bright);
		nitm=GetNextSiblingItem(nitm);
	}

}


void CEffectListTreeCtrl::ScaleEffect(gosFX::Effect::Specification *spec,float sfact,bool bright)
{
if(sfact==1.0f) return;
	RegisteredClass::ClassData *fx_class_data;
	fx_class_data=RegisteredClass::FindClassData(spec->GetClassID());
	Check_Object(spec);
	if(bright)
	{
		if(fx_class_data->IsDerivedFrom(gosFX::ParticleCloud::DefaultData))	
	{
		gosFX::ParticleCloud__Specification *tspec;
		tspec=Cast_Pointer(gosFX::ParticleCloud__Specification *,spec);
		
		tspec->m_pRed.AxisScale(sfact,0.0f);
		tspec->m_pGreen.AxisScale(sfact,0.0f);
		tspec->m_pBlue.AxisScale(sfact,0.0f);
		
	}

	if(fx_class_data->IsDerivedFrom(gosFX::PertCloud::DefaultData))	
	{
		gosFX::PertCloud__Specification *tspec;
		tspec=Cast_Pointer(gosFX::PertCloud__Specification *,spec);
		
		tspec->m_pRed.AxisScale(sfact,0.0f);
		tspec->m_pGreen.AxisScale(sfact,0.0f);
		tspec->m_pBlue.AxisScale(sfact,0.0f);
		
	}

	
	if(fx_class_data->IsDerivedFrom(gosFX::Singleton::DefaultData))	
	{
		gosFX::Singleton__Specification *tspec;
		tspec=Cast_Pointer(gosFX::Singleton__Specification *,spec);
		Check_Object(tspec);
		tspec->m_red.AxisScale(sfact,0.0f);
		tspec->m_green.AxisScale(sfact,0.0f);	
		tspec->m_blue.AxisScale(sfact,0.0f);

	}	
	
	
	if(fx_class_data->IsDerivedFrom(gosFX::Tube::DefaultData))	
	{
		gosFX::Tube__Specification *tspec;
		tspec=Cast_Pointer(gosFX::Tube__Specification *,spec);
		Check_Object(tspec);
		
		tspec->m_pRed.AxisScale(sfact,0.0f);
		tspec->m_pGreen.AxisScale(sfact,0.0f);
		tspec->m_pBlue.AxisScale(sfact,0.0f);
	}

	if(fx_class_data->IsDerivedFrom(gosFX::Beam::DefaultData))	
	{
		gosFX::Beam__Specification *tspec;
		tspec=Cast_Pointer(gosFX::Beam__Specification *,spec);
		Check_Object(tspec);
		
		tspec->m_pRed.AxisScale(sfact,0.0f);
		tspec->m_pGreen.AxisScale(sfact,0.0f);
		tspec->m_pBlue.AxisScale(sfact,0.0f);
	}

	if(fx_class_data->IsDerivedFrom(gosFX::PointLight::DefaultData))	
	{
		gosFX::PointLight__Specification *tspec;
		tspec=Cast_Pointer(gosFX::PointLight__Specification *,spec);
		Check_Object(tspec);
		
		tspec->m_red.AxisScale(sfact,0.0f);
		tspec->m_green.AxisScale(sfact,0.0f);	
		tspec->m_blue.AxisScale(sfact,0.0f);
	}


	
	}
	else
	{
	if(fx_class_data->IsDerivedFrom(gosFX::ParticleCloud::DefaultData))	
	{
		gosFX::ParticleCloud__Specification *tspec;
		tspec=Cast_Pointer(gosFX::ParticleCloud__Specification *,spec);
		
		tspec->m_startingSpeed.AxisScale(sfact,0.0f);
		tspec->m_emitterSizeX.AxisScale(sfact,0.0f);
		tspec->m_emitterSizeY.AxisScale(sfact,0.0f);
		tspec->m_emitterSizeZ.AxisScale(sfact,0.0f);
		tspec->m_pEtherVelocityX.AxisScale(sfact,0.0f);
		tspec->m_pEtherVelocityY.AxisScale(sfact,0.0f);
		tspec->m_pEtherVelocityZ.AxisScale(sfact,0.0f);
		tspec->m_pAccelerationX.AxisScale(sfact,0.0f);
		tspec->m_pAccelerationY.AxisScale(sfact,0.0f);
		tspec->m_pAccelerationZ.AxisScale(sfact,0.0f);
		
	}
	
	if(fx_class_data->IsDerivedFrom(gosFX::SpinningCloud::DefaultData))	
	{
		gosFX::SpinningCloud__Specification *tspec;
		tspec=Cast_Pointer(gosFX::SpinningCloud__Specification *,spec);
		Check_Object(tspec);
		tspec->m_pScale.AxisScale(sfact,0.0f);
	}
	
	if(fx_class_data->IsDerivedFrom(gosFX::CardCloud::DefaultData))	
	{
		gosFX::CardCloud__Specification *tspec;
		tspec=Cast_Pointer(gosFX::CardCloud__Specification *,spec);
		Check_Object(tspec);
//		tspec->m_halfHeight.AxisScale(sfact,0.0f);
	}
	
	if(fx_class_data->IsDerivedFrom(gosFX::ShapeCloud::DefaultData))	
	{
		gosFX::ShapeCloud__Specification *tspec;
		tspec=Cast_Pointer(gosFX::ShapeCloud__Specification *,spec);
		Check_Object(tspec);
		CString str;
		str.Format("Shape referenced in %s will Have to be scaled",(char *)tspec->m_name);
		MessageBox(str,"Scale Warning",MB_OK);
	}
	
	if(fx_class_data->IsDerivedFrom(gosFX::Shape::DefaultData))	
	{
		gosFX::Shape__Specification *tspec;
		tspec=Cast_Pointer(gosFX::Shape__Specification *,spec);
		Check_Object(tspec);
		CString str;
		str.Format("Shape referenced in %s will Have to be scaled",(char *)tspec->m_name);
		MessageBox(str,"Scale Warning",MB_OK);
	}
	
	if(fx_class_data->IsDerivedFrom(gosFX::Singleton::DefaultData))	
	{
		gosFX::Singleton__Specification *tspec;
		tspec=Cast_Pointer(gosFX::Singleton__Specification *,spec);
		Check_Object(tspec);
		tspec->m_scale.AxisScale(sfact,0.0f);
	}	
	
	
	if(fx_class_data->IsDerivedFrom(gosFX::Tube::DefaultData))	
	{
		gosFX::Tube__Specification *tspec;
		tspec=Cast_Pointer(gosFX::Tube__Specification *,spec);
		Check_Object(tspec);
		
		tspec->m_emitterSizeX.AxisScale(sfact,0.0f);
		tspec->m_emitterSizeY.AxisScale(sfact,0.0f);
		tspec->m_emitterSizeZ.AxisScale(sfact,0.0f);
		tspec->m_pDisplacement.AxisScale(sfact,0.0f);
		tspec->m_pScale.AxisScale(sfact,0.0f);
	}
	
	if(fx_class_data->IsDerivedFrom(gosFX::DebrisCloud::DefaultData))	
	{
		gosFX::DebrisCloud__Specification *tspec;
		tspec=Cast_Pointer(gosFX::DebrisCloud__Specification *,spec);
		Check_Object(tspec);
		tspec->m_startingSpeed.AxisScale(sfact,0.0f);
		tspec->m_pEtherVelocityY.AxisScale(sfact,0.0f);
		tspec->m_pAccelerationY.AxisScale(sfact,0.0f);
		tspec->m_centerOfForce*=sfact;
	}
	}
}