//
// Tools_PFX_Dialog.cpp
//
// Created by Richard Rice - 01/04/2005
// 
// Copyright (C) 2005 Rebellion, All Rights Reserved.
//


#include "Core/Asura.h"


#include "Particles/Tools_PFX_Dialog.h"
#include "Particles/Tools_PFX_Overlay.h"
#include "Rendering/Asura_Render.h"
#include "Rendering/Asura_Text_Output.h"
#include "Rendering/Asura_Direct3d.h"
#include "SpecialFX/PFX/Asura_PFX_System.h"



// Tools_PFXSelect_Dialog /////////////////////////////////////////////////////////////////////////
Tools_PFXSelect_Dialog::Tools_PFXSelect_Dialog( Mode eMode, Asura_Vector_2& xOffset, Asura_Hash_ID* puTemplate, bool* pbChangedFlag /*= 0*/)
: Asura_Dialog("[NONE]", xOffset), m_eMode(eMode)
{
	Asura_Gadget* pxNewGadget;

	m_xFilter = "";
	m_pbChangedFlag = pbChangedFlag;
	m_puTemplate = puTemplate;
	if (m_puTemplate)
	{
		SetTemplateHash(*m_puTemplate);
	} else {
		SetTemplateHash(ASURA_HASH_ID_UNSET);
	}

	Asura_Callback_MemberFunction<void, Tools_PFXSelect_Dialog> xCallback(this, &Tools_PFXSelect_Dialog::Clicked);
	pxNewGadget = m_pFuncGadget = new Asura_Function_Gadget( xOffset, xCallback, NULL);
	AddGadget( pxNewGadget );


	UpdateMax();

	//extend the width of the gadget , so that clicking on the text will activate it
	pxNewGadget->m_fWidth = m_xMaxCoord.x - pxNewGadget->m_xOffset.x;
}

void Tools_PFXSelect_Dialog::UpdateMax()
{
	float	fMaxWidth	= 0.0f;

	switch (m_eMode)
	{
		case MODE_EFFECTS:
		{
			const Asura_TemplateHashTree<Asura_String>&	xTemplateNameTree = Asura_PFX_System::GetEffectTempNames();

			for(int i=0; i< xTemplateNameTree.GetNumberOfNodes(); i++)
			{
				const Asura_String*	pxFullTemplateName	= xTemplateNameTree.GetDataByIndex(i);
				const char*			szFullTemplateName	= pxFullTemplateName->GetString();
				const float			fStringWidth		= Asura_Text_Output::WidthOfString(szFullTemplateName, ASURA_TEXT_FORMAT_SMALLFONT);

				fMaxWidth = Asura_Maths::Max(fMaxWidth, (fStringWidth + 5.0f));
			}
			break;
		}

		case MODE_SOURCES:
		{
			const Asura_TemplateHashTree<Asura_String>&	xTemplateNameTree = Asura_PFX_System::GetSourceTempNames();

			for(int i=0; i< xTemplateNameTree.GetNumberOfNodes(); i++)
			{
				const Asura_String*	pxFullTemplateName	= xTemplateNameTree.GetDataByIndex(i);
				const char*			szFullTemplateName	= pxFullTemplateName->GetString();
				const float			fStringWidth		= Asura_Text_Output::WidthOfString(szFullTemplateName, ASURA_TEXT_FORMAT_SMALLFONT);

				fMaxWidth = Asura_Maths::Max(fMaxWidth, (fStringWidth + 5.0f));
			}
			break;
		}

		case MODE_PARTICLES:
		{
			const Asura_TemplateHashTree<Asura_String>&	xTemplateNameTree = Asura_PFX_System::GetParticleTempNames();

			for(int i=0; i< xTemplateNameTree.GetNumberOfNodes(); i++)
			{
				const Asura_String*	pxFullTemplateName	= xTemplateNameTree.GetDataByIndex(i);
				const char*			szFullTemplateName	= pxFullTemplateName->GetString();
				const float			fStringWidth		= Asura_Text_Output::WidthOfString(szFullTemplateName, ASURA_TEXT_FORMAT_SMALLFONT);

				fMaxWidth = Asura_Maths::Max(fMaxWidth, (fStringWidth + 5.0f));
			}
			break;
		}
	}		// End of switch.

	m_xMaxCoord.x = m_xOffset.x + fMaxWidth;
	m_xMaxCoord.y = m_xOffset.y + 1.0f;
}


void Tools_PFXSelect_Dialog::SetTemplateHash(Asura_Hash_ID uHash)
{
	if (uHash != ASURA_HASH_ID_UNSET)
	{
		const Asura_String *pxTempName;
		switch (m_eMode)
		{
		case MODE_EFFECTS:
			pxTempName = Asura_PFX_System::GetEffectTempNameByHash(uHash);
			break;
		case MODE_SOURCES:
			pxTempName = Asura_PFX_System::GetSourceTempNameByHash(uHash);
			break;
		case MODE_PARTICLES:
			pxTempName = Asura_PFX_System::GetSourceTempNameByHash(uHash);
			break;
		}
		m_xTemplateName = pxTempName ? *pxTempName : "[NONE]";
	} else {
		m_xTemplateName =  "[NONE]";
	}
	m_uHash = uHash;
	if (m_puTemplate)
	{
		*m_puTemplate = m_uHash;
	}

	UpdateLabel(m_xTemplateName.GetString());
}

void Tools_PFXSelect_Dialog::SetFilter(const char* pFilter)
{
	m_xFilter = pFilter;
}


void Tools_PFXSelect_Dialog::UpdateLabel(const char* text)
{
	m_xName = text;
}

void Tools_PFXSelect_Dialog::Render( const Asura_Vector_2& xOrigin )
{
	if( !IsHidden() )
	{
		//  		if( GadgetList )
		//  		{
		//  			GadgetList->RenderList( xOrigin );
		//  		}

		Asura_Render::Overlay.Colour = 0xffffffff;
		Asura_Render::Overlay.TextureIndex = -1;
		Asura_Render::Overlay.TranslucencyMode = ASURA_TRANSLUCENCY_OFF;

		Asura_Vector_2 xTopLeft = xOrigin + m_xOffset;
		xTopLeft.x = xTopLeft.x * ASURA_TEXT_OUTPUT_FONT_WIDTH - 1;
		xTopLeft.y = xTopLeft.y * ASURA_TEXT_OUTPUT_FONT_HEIGHT - 1;
		Asura_Vector_2 xDimensions;
		xDimensions.x = (m_pFuncGadget->m_fWidth) * ASURA_TEXT_OUTPUT_FONT_WIDTH + 2;
		xDimensions.y = ((m_pFuncGadget->m_fHeight) * ASURA_TEXT_OUTPUT_FONT_HEIGHT) + 2;


		Asura_Render::Overlay.SetRectXY( xTopLeft.x, xTopLeft.y, xDimensions.x, xDimensions.y );
		Asura_Render::Overlay.SetRectUV( 0.0f, 0.0f, 1.0f, 1.0f );

		Asura_Render::Overlay.m_bNonRotated = true;
		Asura_Render::Overlay.Render(TRUE);
		Asura_Render::Overlay.Colour = 0xff000000;
		Asura_Render::Overlay.SetRectXY( xTopLeft.x+1, xTopLeft.y+1, xDimensions.x-2, xDimensions.y-2);
		Asura_Render::Overlay.Render(TRUE);
		Asura_Direct3D::FlushQuadBatches();

		if( !m_xName.IsBlank() )
		{
			Asura_Vector_2 pos = xOrigin + m_xOffset + GetTextOffset();
			Asura_Text_Output::Print(pos.x + 2 ,pos.y,GetColour(),ASURA_TEXT_FORMAT_SMALLFONT,"%s",m_xName.GetString());
		}
	}
}


void Tools_PFXSelect_Dialog::Clicked()
{
	const char *pTitle = "";
	switch (m_eMode)
	{
	case MODE_EFFECTS:
		pTitle = "Choose PFX Effect template";
		break;
	case MODE_SOURCES:
		pTitle = "Choose PFX Source template";
		break;
	case MODE_PARTICLES:
		pTitle = "Choose PFX Particle template";
		break;
	}
	Tools_PFXSelect_Overlay xOverlay(pTitle, (Tools_PFXSelect_Overlay::Mode)m_eMode, m_xFilter);
	xOverlay.SetSelected(GetTemplateName());

	if (xOverlay.DoModal()) 
	{

		m_xTemplateName = xOverlay.GetTemplateName();
		m_uHash = xOverlay.GetTemplateHash();

		if (m_puTemplate)
		{
			*m_puTemplate = m_uHash;
		}
		if (m_pbChangedFlag)
		{
			*m_pbChangedFlag = true;
		}
		UpdateLabel(m_xTemplateName.GetString());
	}
}


/// End of file. ///
