// FXPanel.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "FXPanel.h"
#include "GUIFXGenerator.h"
#include <Adept\Adept.hpp>
using namespace Adept;

/////////////////////////////////////////////////////////////////////////////
// CFXPanel dialog


CFXPanel::CFXPanel(GUIFXGenerator *gobj,UndoCommand **cmd,CWnd* pParent)
: CPanelDlg(CFXPanel::IDD, pParent)
{
	MyObj=gobj;
	CmdLst=cmd;
	//{{AFX_DATA_INIT(CFXPanel)
	m_Delay = 0.0f;
	m_NightOnly = FALSE;
	m_Random = FALSE;
	//}}AFX_DATA_INIT
}


void CFXPanel::DoDataExchange(CDataExchange* pDX)
{
	CPanelDlg::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFXPanel)
	DDX_Control(pDX, IDC_FXLIST, m_FXList);
	DDX_Text(pDX, IDC_DELAY, m_Delay);
	DDX_Check(pDX, IDC_NIGHTONLY, m_NightOnly);
	DDX_Check(pDX, IDC_RANDOM, m_Random);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFXPanel, CPanelDlg)
//{{AFX_MSG_MAP(CFXPanel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFXPanel message handlers

void CFXPanel::OnOK() 
{
	UpdateData(TRUE);
	if(m_FXList.GetCurSel()>=0)
	{
	MyObj->SetEffect(*((ResourceID *)m_FXList.GetItemDataPtr(m_FXList.GetCurSel())));
	}

	MyObj->SetRandom(m_Random?true:false);
	MyObj->SetDelay(m_Delay); 
	MyObj->SetNightOnly(m_NightOnly?true:false); 
	
}

void CFXPanel::FillEffectList()
{
	
	
	if(Adept::ResourceManager::Instance==NULL) return;
	Adept::Resource curres;
	curres.First(NULL);
	
	
	CString res_name,base_name,dir_name;
	int item;
	int depth,sp,i;
	
	
	m_FXList.ResetContent();
	
	while(curres.ReadAndNext())
	{
		res_name=(char *)curres.GetName();
		res_name.MakeLower();
		
		if(res_name.Right(5).Compare(".data")==NULL)
		{									// This is a Data File
			base_name=res_name.Mid(res_name.ReverseFind('\\')+1);
			base_name=base_name.Left(base_name.GetLength()-5);
			
			sp=res_name.Find("effects\\");
			
			depth=0;
			if(sp>=0)
				for(i=sp;i<res_name.GetLength();i++) 
					if(res_name[i]=='\\') 
						depth++;	// Count Tree Depth	
					
					if( sp!=-1 && depth==2)
					{
						item=m_FXList.AddString(base_name);
						m_FXList.SetItemDataPtr(item,new ResourceID(curres.GetResourceID()));
					}
					
					
		}
	}
	
	
	
	
}

CString CFXPanel::EffectBaseName(const char *str)
{
	CString res_name(str);
	res_name.MakeLower();
	CString base_name;
	base_name=res_name.Mid(res_name.ReverseFind('\\')+1);
	base_name=base_name.Left(base_name.GetLength()-5);
	return base_name;
}

void CFXPanel::OnCancel() 
{
}

BOOL CFXPanel::OnInitDialog() 
{
	CPanelDlg::OnInitDialog();
	FillEffectList();
	ResourceID resid=MyObj->GetEffect();
	
	int sel=0;
	if(resid!=ResourceID::Null)
	{
		Resource effect_res(resid);
		sel=-1;
		int i;
		for(i=0;i<m_FXList.GetCount() && sel<0;i++)
		{
			if(*((ResourceID *)m_FXList.GetItemDataPtr(i))==MyObj->GetEffect()) sel=i;
		}
	}
	m_FXList.SetCurSel(sel);
	
	m_Random = MyObj->IsRandom();
	m_Delay = MyObj->GetDelay();
	m_NightOnly = MyObj->IsNightOnly();
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}
