void SkillObjectEditor::OnNewSkill(wxCommandEvent &e)
{
	AddSkillDialog *dlg = new AddSkillDialog(this, wxID_ANY, /*_("Add Skill")*/wxT("添加新技能") );
	dlg->Centre();
	bool ok = dlg->ShowModal() == wxID_OK;
	if(ok)
	{
		// 获取所选的动作名称
		wxString selectedAnimName = dlg->mAnimComboBox->GetStringSelection();
		wxString skillName = dlg->mSkillNameTextCtrl->GetValue();

		Fairy::Skill *pSkill = Fairy::EffectManager::getSingleton().createSkillTemplate(skillName.c_str());
		pSkill->setAnimationName(selectedAnimName.c_str());
		mSkillTemplateName = pSkill->getSkillName();

		//mDObject->createSkill(skill->getSkillName(),false, false,10.0f);// mAnimationFadeOutTime);

		// 刷新动作进度条
		m_Frame->GetGraphDialog()->InitAnimTimePosSlider();
		InitSkillEditor(pSkill,mSkillTemplateName);
	}

	dlg->Destroy();

}
void ActorAnimationSettingDialog::OnAddSkillButtonDown(wxCommandEvent &e)
{	
	AddSkillDialog *dlg = new AddSkillDialog(this, wxID_ANY, /*_("Add Skill")*/wxT("添加新技能") );
	dlg->Centre();
	bool ok = dlg->ShowModal() == wxID_OK;
	if(ok)
	{
		// 获取所选的动作名称
		wxString selectedAnimName = dlg->mAnimComboBox->GetStringSelection();
		wxString skillName = dlg->mSkillNameTextCtrl->GetValue();

		Fairy::Skill *skill = Fairy::EffectManager::getSingleton().createSkillTemplate(skillName.c_str());
		skill->setAnimationName(selectedAnimName.c_str());

		mDObject->createSkill(skill->getSkillName(), mCheckBoxLoop->GetValue(), false, mAnimationFadeOutTime);

		// 刷新技能列表
		FillAllSkillListBox();

		// 刷新动作进度条
		InitAnimTimePosSlider();
	}

	dlg->Destroy();
}
void SkillObjectTree::AddSkillToTree(Fairy::Skill *pSkill)
{
	// 更新模板
	Fairy::Skill *skill = Fairy::EffectManager::getSingleton().getSkill(pSkill->getSkillName());
    skill = pSkill;

	int iSkillImage = SkillObjectTree::TreeCtrlIcon_Skill;
	wxTreeItemId SkillRootId = AddRoot(skill->getSkillName(),
		iSkillImage, iSkillImage,
		new SkillTreeItemData(wxT("Skill root item"),skill->getSkillName(),skill));

	int iPorpertyImage = SkillObjectTree::TreeCtrlIcon_SkilltProperty;

	wxTreeItemId BasicSettingId= AppendItem(SkillRootId,wxT("BasicSetting"),
		iPorpertyImage, iPorpertyImage,
		new SkillTreeItemData(wxT("Skill BasicSetting"),wxT("Skill BasicSetting"),pSkill));

	if ( BasicSettingId.IsOk() )
	{
		const Ogre::ParameterList &paraList = pSkill->getParameters();

		for ( size_t paraCount=0; paraCount < paraList.size(); ++paraCount )
		{
			Ogre::String name = paraList[paraCount].name;
			Ogre::String value = pSkill->getParameter(paraList[paraCount].name);

			wxTreeItemId itemId =AppendItem( BasicSettingId,(name + ":" + value).c_str(),iPorpertyImage,iPorpertyImage,
				new SkillTreeItemData(wxT("Skill property item"),name,skill));
		}
	}

	int iAnimEfectImage = SkillObjectTree::TreeCtrlIcon_AnimEffect;
	int iCameraShakeImage = SkillObjectTree::TreeCtrlIcon_CameraShake;
	int iRibbinImage = SkillObjectTree::TreeCtrlIcon_Ribbon;
	int iSoundImage = SkillObjectTree::TreeCtrlIcon_Sound;
	int iSceneLightImage = SkillObjectTree::TreeCtrlIcon_SceneLight;
	int iBulletFlowImage = SkillObjectTree::TreeCtrlIcon_BulletFlow;


	// 设置effect属性
	for ( unsigned short i=0; i < skill->getNumAnimationEffectInfos(); ++i )
	{
		Fairy::AnimationEffectInfo *effectInfo = skill->getAnimationEffectInfo(i);
		assert (effectInfo);

		Ogre::String effectIndexStr = "Effect Info " + Ogre::StringConverter::toString(i);
		wxTreeItemId AnimEffectId= AppendItem(SkillRootId,effectIndexStr.c_str(),
			iAnimEfectImage, iAnimEfectImage,
			new SkillTreeItemData(wxT("Skill Element"),wxT("AnimEffect"),effectInfo));

		if ( AnimEffectId.IsOk() )
		{

			const Ogre::ParameterList &paraList = effectInfo->getParameters();

			for ( size_t paraCount=0; paraCount < paraList.size(); ++paraCount )
			{
				Ogre::String name = paraList[paraCount].name;
				Ogre::String value = effectInfo->getParameter(paraList[paraCount].name);

				wxTreeItemId itemId =AppendItem( AnimEffectId,(name + ":" + value).c_str(),iPorpertyImage,iPorpertyImage,
					new SkillTreeItemData(wxT("Skill Element item"),name,effectInfo));

			}
		}
	}

	for ( unsigned short i=0; i < skill->getNumAnimationCameraShake(); ++i )
	{
		Fairy::AnimationCameraShake *cameraShake = skill->getAnimationCameraShake(i);

		assert (cameraShake);

		Ogre::String cameraShakeIndexStr = "CameraShake " + Ogre::StringConverter::toString(i);

		wxTreeItemId CameraShakeId= AppendItem(SkillRootId,cameraShakeIndexStr.c_str(),
			iCameraShakeImage, iCameraShakeImage,
			new SkillTreeItemData(wxT("Skill Element"),wxT("CameraShake"),cameraShake));

		if ( CameraShakeId.IsOk() )
		{

			const Ogre::ParameterList &paraList = cameraShake->getParameters();

			for ( size_t paraCount=0; paraCount < paraList.size(); ++paraCount )
			{
				Ogre::String name = paraList[paraCount].name;
				Ogre::String value = cameraShake->getParameter(paraList[paraCount].name);

				wxTreeItemId itemId =AppendItem( CameraShakeId,(name + ":" + value).c_str(),iPorpertyImage,iPorpertyImage,
					new SkillTreeItemData(wxT("Skill Element item"),name,cameraShake));

			}
		}		
	}

	for ( unsigned short i=0; i < skill->getNumAnimationRibbons(); ++i )
	{
		Fairy::AnimationRibbon *ribbon = skill->getAnimationRibbon(i);

		assert (ribbon);

		Ogre::String ribbonIndexStr = "Ribbon " + Ogre::StringConverter::toString(i);

		wxTreeItemId RibbonId= AppendItem(SkillRootId,ribbonIndexStr.c_str(),
			iRibbinImage, iRibbinImage,
			new SkillTreeItemData(wxT("Skill Element"),wxT("Ribbon"),ribbon));

		if ( RibbonId.IsOk() )
		{

			const Ogre::ParameterList &paraList = ribbon->getParameters();

			for ( size_t paraCount=0; paraCount < paraList.size(); ++paraCount )
			{
				Ogre::String name = paraList[paraCount].name;
				Ogre::String value = ribbon->getParameter(paraList[paraCount].name);

				wxTreeItemId itemId =AppendItem( RibbonId,(name + ":" + value).c_str(),iPorpertyImage,iPorpertyImage,
					new SkillTreeItemData(wxT("Skill Element item"),name,ribbon));

			}
		}		
	}

	for ( unsigned short i=0; i < skill->getNumAnimationSounds(); ++i )
	{
		Fairy::AnimationSound *sound = skill->getAnimationSound(i);

		assert (sound);

		Ogre::String soundIndexStr = "Sound " + Ogre::StringConverter::toString(i);

		wxTreeItemId SoundId= AppendItem(SkillRootId,soundIndexStr.c_str(),
			iSoundImage, iSoundImage,
			new SkillTreeItemData(wxT("Skill Element"),wxT("Sound"),sound));

		if ( SoundId.IsOk() )
		{

			const Ogre::ParameterList &paraList = sound->getParameters();

			for ( size_t paraCount=0; paraCount < paraList.size(); ++paraCount )
			{
				Ogre::String name = paraList[paraCount].name;
				Ogre::String value = sound->getParameter(paraList[paraCount].name);

				wxTreeItemId itemId =AppendItem( SoundId,(name + ":" + value).c_str(),iPorpertyImage,iPorpertyImage,
					new SkillTreeItemData(wxT("Skill Element item"),name,sound));

			}
		}	
	}

	for ( unsigned short i=0; i < skill->getNumAnimationSceneLightInfos(); ++i )
	{
		Fairy::AnimationSceneLightInfo *light = skill->getAnimationSceneLightInfo(i);

		assert (light);

		Ogre::String lightIndexStr = "SceneLight " + Ogre::StringConverter::toString(i);

		wxTreeItemId LightId= AppendItem(SkillRootId,lightIndexStr.c_str(),
			iSceneLightImage, iSceneLightImage,
			new SkillTreeItemData(wxT("Skill Element"),wxT("SceneLight"),light));

		if ( LightId.IsOk() )
		{

			const Ogre::ParameterList &paraList = light->getParameters();

			for ( size_t paraCount=0; paraCount < paraList.size(); ++paraCount )
			{
				Ogre::String name = paraList[paraCount].name;
				Ogre::String value = light->getParameter(paraList[paraCount].name);

				wxTreeItemId itemId =AppendItem( LightId,(name + ":" + value).c_str(),iPorpertyImage,iPorpertyImage,
					new SkillTreeItemData(wxT("Skill Element item"),name,light));

			}
		}		
	}

	for ( unsigned short i=0; i < skill->getNumAnimationBulletFlows(); ++i )
	{
		Fairy::AnimationBulletFlow *bulletFlow = skill->getAnimationBulletFlow(i);

		assert (bulletFlow);

		Ogre::String bulletFlowIndexStr = "BulletFlow " + Ogre::StringConverter::toString(i);

		wxTreeItemId BulletFlowId= AppendItem(SkillRootId,bulletFlowIndexStr.c_str(),
			iBulletFlowImage, iBulletFlowImage,
			new SkillTreeItemData(wxT("Skill Element"),wxT("BulletFlow"),bulletFlow));

		if ( BulletFlowId.IsOk() )
		{

			const Ogre::ParameterList &paraList = bulletFlow->getParameters();

			for ( size_t paraCount=0; paraCount < paraList.size(); ++paraCount )
			{
				Ogre::String name = paraList[paraCount].name;
				Ogre::String value = bulletFlow->getParameter(paraList[paraCount].name);

				wxTreeItemId itemId =AppendItem( BulletFlowId,(name + ":" + value).c_str(),iPorpertyImage,iPorpertyImage,
					new SkillTreeItemData(wxT("Skill Element item"),name,bulletFlow));

			}
		}		
	}

	Expand(SkillRootId);

}
void SkillObjectEditor::OnSaveSkillObject(wxCommandEvent &e)
{
	if (NULL == mSkill)
	{
		wxMessageBox("您还没有创建技能,请创建后重试!","提示");
		return;
	}

	if(mSaveFileName == "")
	{
		SaveSkillDialog *dlg = new SaveSkillDialog(this, wxID_ANY,
			_("保存技能"), mSkill);

		bool ok = (dlg->ShowModal() == wxID_OK);
		if(ok)
		{
			mSaveFileName = dlg->mComboBox->GetValue().c_str();

			//判断文件名
			Ogre::StringVector strings = Ogre::StringUtil::split( mSaveFileName, "." );

			if (strings.size() != 2 || strings[1] != "skill")
			{
				mSaveFileName.append(".skill");
			}
			dlg->Destroy();
		}
		else
		{
			dlg->Destroy();
			return;

		}

	}
	//if(mbRecoverSave)
	//{
	//	char buf[256];
	//	sprintf(buf,"是否覆盖文件%s中的%s效果数据!",mSaveFileName.c_str(),mSkillTemplateName.c_str());
	//	RecoverSaveDialog* dlg = new RecoverSaveDialog(m_Frame,wxID_ANY,wxT("覆盖文件"));
	//	dlg->SetText(wxString(buf));


	//	if(wxID_OK == dlg->ShowModal())
	//	{
	//		mbRecoverSave = false;
	//		dlg->Destroy();
	//	}
	//	else
	//	{
	//		dlg->Destroy();
	//		return;
	//	}
	//}

	Ogre::StringVector templates;

	const Ogre::String fileName = mSaveFileName;


	Fairy::EffectManager::getSingleton().getSkillTemplatesFromScriptFile(fileName, templates);

	std::ofstream outFile;

	Ogre::String pathFileName = EFFECT_PATH+fileName;
	outFile.open ( pathFileName.c_str(), std::ios::out | std::ios::trunc ); // append to file

	if (!outFile.is_open())
	{
		return;
	}

	bool newTemplate = true;

	// 把所有的模板都写入该文件中
	for ( size_t i=0; i<templates.size(); ++i )
	{
		//	if (templates[i] != mEffect->getTemplateName())
		///{
		Fairy::Skill *skill = Fairy::EffectManager::getSingleton().getSkill(templates[i]);
		assert (skill);

		if (skill->getSkillName() == mSkillTemplateName)
		{
			saveSkill(mSkill, mSkillTemplateName, outFile );

			*skill = *mSkill;

			newTemplate = false;
		}
		else
			saveSkill(skill, skill->getSkillName(), outFile);

	}


	if (newTemplate)
	{
		saveSkill(mSkill, mSkillTemplateName, outFile );
		mSkill->setSkillName(mSkillTemplateName);
		Fairy::EffectManager::getSingleton().addToSkillTemplateScriptFileMap(mSkillTemplateName, fileName);
	}

	outFile.close();
}
void SkillObjectEditor::OnSaveAsSkillObject(wxCommandEvent &e)
{
	if (NULL == mSkill)
	{
		wxMessageBox("您还没有创建技能,请创建后重试!","提示");
		return;
	}

	SaveAsSkillDialog *dlg = new SaveAsSkillDialog(this, wxID_ANY,
		_("另存技能"), mSkill);

	bool ok = (dlg->ShowModal() == wxID_OK);
	if(ok)
	{
		Ogre::StringVector templates;

		Ogre::String fileName = dlg->mComboBox->GetValue().c_str();
		Ogre::String templateName = dlg->mTextCtrl->GetValue().c_str();

		if(templateName.length() == 0)
		{
			dlg->Destroy();
			return;
		}

		//判断文件名
		Ogre::StringVector strings = Ogre::StringUtil::split( fileName, "." );
		if (strings.size() != 2 || strings[1] != "skill")
		{
			fileName.append(".skill");
		}

		Fairy::EffectManager::getSingleton().getSkillTemplatesFromScriptFile(fileName, templates);

		std::ofstream outFile;

		Ogre::String pathFileName = EFFECT_PATH+fileName;
		outFile.open ( pathFileName.c_str(), std::ios::out | std::ios::trunc ); // append to file

		if (!outFile.is_open())
		{
			dlg->Destroy();
			return;
		}

		bool newTemplate = true;


		// 把所有的模板都写入该文件中
		for ( size_t i=0; i<templates.size(); ++i )
		{
			//	if (templates[i] != mEffect->getTemplateName())
			///{
			Fairy::Skill *skill = Fairy::EffectManager::getSingleton().getSkill(templates[i]);
			assert (skill);

			if (skill->getSkillName() == dlg->mTextCtrl->GetValue().c_str())
			{
				saveSkill(mSkill, dlg->mTextCtrl->GetValue().c_str(), outFile );


				newTemplate = false;
			}
			else
				saveSkill(skill, skill->getSkillName(), outFile);

		}

		if (newTemplate)
		{
			
			// 刷新EffectManager中的模板内容
			Fairy::Skill *skill = Fairy::EffectManager::getSingleton().getSkill(templateName);
			if (NULL == skill)
			{
				skill = Fairy::EffectManager::getSingleton().createSkillTemplate(templateName);
			}

			*skill = *mSkill;

			saveSkill(skill, templateName, outFile );

			Fairy::EffectManager::getSingleton().addToSkillTemplateScriptFileMap(templateName, fileName);

			InitSkillEditor(skill, templateName);
		}

		outFile.close();
	}

	wxBusyInfo* busyInfo = new wxBusyInfo(wxT("更新技能数据 ..."), this);
	m_Frame->GetSkillSelector()->Reload();
	delete busyInfo;

	dlg->Destroy();

}