//
// Tools_Dialog.cpp
//
// Created by Richard Rice - 19/07/2002
// 
// Copyright (C) 2002 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "Debugging/Asura_Assert.h"

#include "Core/Asura_TextureManagement.h"
#include "Interface/Asura_Gadget.h"
#include "Interface/Asura_Overlay.h"
#include "Rendering/Asura_Text_Output.h"

#include "Misc/Tools_Dialog.h"
#include "Misc/Tools_Flag_IniFile.h"


Tools_Enumeration_Dialog* Tools_Enumeration_Dialog::ClickedDialog = 0;

Tools_Enumeration_Dialog::Tools_Enumeration_Dialog(const char* title,Asura_Vector_2 offset,int* value,Tools_Value_List* enumerationList,bool* pbModifiedFlag /*= 0*/)
:Asura_Dialog(title,offset)
{
	Asura_Assert(value,"");
	Asura_Assert(enumerationList,"");
	Asura_Assert(title,"");

	Title = title;
	Value = value;
	EnumerationList = enumerationList;
	Clicked = false;
	m_pbModifiedFlag = pbModifiedFlag;

	//update the label
	char buffer[200];
	sprintf(buffer,"%s : %s",Title.GetString(),EnumerationList->GetName(*value));
	m_xName = buffer;

	//create the function gadget
	Asura_Vector_2 pos = offset;

	Asura_Gadget* newGadget;
	newGadget  = new Asura_Function_Gadget(pos,Asura_Callback_Void<void>(DialogClicked), &Clicked);
	AddGadget(newGadget);

	m_xOffset.x += newGadget->m_fWidth+1.0f;
	
	m_xMaxCoord.x = m_xOffset.x + Asura_Text_Output::LengthOfString(m_xName.GetString());
	m_xMaxCoord.y = m_xOffset.y + 1.0f;

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

void Tools_Enumeration_Dialog::Update(const Asura_Vector_2& origin)
{
	Asura_Dialog::Update(origin);

	if(Clicked)
	{
		//store the pointer to this dialog , so the static function knows which it is dealing with
		ClickedDialog = this;
		Clicked = false;
	}
}

void Tools_Enumeration_Dialog::UpdateLabel(const char* text)
{
	Asura_Dialog::UpdateLabel(text);
	//update the width on the gadget
	if(m_pxGadgetList)
		m_pxGadgetList->m_fWidth = m_xMaxCoord.x - m_pxGadgetList->m_xOffset.x;
}


void Tools_Enumeration_Dialog::DialogClicked()
{
	Asura_Assert(ClickedDialog,"");
	ClickedDialog->ChooseEnumerationValue();
	ClickedDialog = 0;
}

void Tools_Enumeration_Dialog::ChooseEnumerationValue()
{
	if(!EnumerationList->SelectValue(Title.GetString(),Value))
		return;

	if(m_pbModifiedFlag) *m_pbModifiedFlag = true;

	//build the new string for the overlay
	char buffer[200];
	sprintf(buffer,"%s : %s",Title.GetString(),EnumerationList->GetName(*Value));

	//update the overlay
	Asura_Assert(m_pxOverlay,"");
	m_pxOverlay->UpdateDialogLabel(this,buffer);
}

////////////////////////////////////////////////////////////////////////////////
static int g_iIconTexture = ASURA_TEXTURE_NONE;

int Tools_Dialog::GetIconTexture()
{
	if(g_iIconTexture == ASURA_TEXTURE_NONE)
	{
		g_iIconTexture = Asura_Texture_Management::AddTexture("\\Tools\\ToolsIcons.dds");
		Asura_Texture_Management::LoadTextures();
	}
	return g_iIconTexture;
}
