
////////////////////////////////////////////////////////////////////////////////
//
// Tools_Entity_FMVSpeaker.cpp
// Author    : Jason Turner
// Copyright (C) 2009 Rebellion, All Rights Reserved.
//
////////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

#ifdef ASURA_USE_VIDEO_TO_TEXTURE_SYSTEM

#include "Entity/Asura_Entity_Event.h"
#include "Entity/Tools_Entity_FMVSpeaker.h"
#include "Entity/FMVController/Asura_ServerEntity_FMVSpeaker.h"
#include "Entity/FMVController/Asura_ClientEntity_FMVSpeaker.h"
#include "FMV/Asura_VideoToTexture.h"
#include "Interface/Asura_Dialog.h"
#include "Interface/Asura_Overlay.h"
#include "Mesh/Asura_Mesh_System.h"
#include "Network/Asura_Message.h"

#include "Load&Save/Tools_SaveAsura.h"
#include "Misc/Tools_Modal_Overlay.h"
#include "Misc/Tools_Timed_Messages.h"
#include "Objects/Tools_Object_System.h"

#include "PC/FileSystem/Tools_FileSystem.h"

#include "Sound/Tools_Mode_Sound.h"

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////

// class implementation
Tools_Entity_FMVSpeaker::Tools_Entity_FMVSpeaker(Asura_Entity* pxEntity)
:Tools_Entity_Activatable_Object(pxEntity)
{
	Type = TOT_Entity_FMVSpeaker;
	Mesh = Asura_Mesh_System::GetMesh("ToolsShape_Sound");

	SetDistantIconTexture("\\tools\\Sound.dds");
}

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////

Tools_Entity_FMVSpeaker::~Tools_Entity_FMVSpeaker()
{
}

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////

void Tools_Entity_FMVSpeaker::AddOverlayControls(class Asura_Overlay* pxOverlay,class Asura_Vector_2& xOffset)
{
	PARENT::AddOverlayControls(pxOverlay,xOffset);
	Asura_Dialog* pxDialog;

	Asura_ServerEntity_FMVSpeaker* pxAsuraEntity = (Asura_ServerEntity_FMVSpeaker*) Entity;

	//
	// Dividing line
	//
	pxDialog = new Asura_Divider_Dialog( xOffset );
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_Numerical_Dialog("Inner Range",xOffset,&pxAsuraEntity->m_fInnerRange, 0.5f,9999.0f,0.1f,"%0.1f");
	pxDialog->SetHelpText("The point at which sound volume attenuation starts");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_Numerical_Dialog("Outer Range",xOffset,&pxAsuraEntity->m_fOuterRange, 0.5f,9999.0f,0.1f,"%0.1f");
	pxDialog->SetHelpText("The point at which sound attenuation volume is zero");
	pxOverlay->AddDialog(pxDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();
}

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////

void Tools_Entity_FMVSpeaker::Create()
{
	Tools_Object* pxObject = Create(false);
	Tools_Object_System::SelectObject(pxObject);
}

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////

Tools_Entity_FMVSpeaker* Tools_Entity_FMVSpeaker::Create(bool bGenerateName)
{
	//get a name , and the position data
	Asura_Guid uGuid = Asura_Entity_System::GetCreatedByToolsGuid();
	Tools_Entity_Data* pxToolsData = 0;
	if(bGenerateName)
	{
		pxToolsData = Tools_Entity_Object::CreateToolsDataWithDefaultName("FMV Speaker","FMV Speaker",uGuid,GetNamePrefix());
	}
	else
	{
		pxToolsData = Tools_Entity_Object::CreateToolsData("FMV Speaker",0,GetNamePrefix());
	}

	if(!pxToolsData)
	{
		Asura_Assert(0, "Tools_Entity_FMVSpeaker::Create() - no tools data");
		return NULL;
	}

	//create the asura data
	Asura_ServerEntity_FMVSpeaker* pxServerObject = new Asura_ServerEntity_FMVSpeaker(uGuid);
	if(!pxServerObject)
	{
		Asura_Assert(0, "Tools_Entity_FMVSpeaker::Create() - no server object" );
		return NULL;
	}

	pxServerObject->ToolsData = pxToolsData;

	Asura_ClientEntity_FMVSpeaker* pxClientObject = new Asura_ClientEntity_FMVSpeaker(uGuid);
	Asura_Assert(pxClientObject, "Tools_Entity_FMVSpeaker::Create() - no client object");

	// Create the tools object
	Tools_Entity_FMVSpeaker* pxToolsObject = new Tools_Entity_FMVSpeaker(pxServerObject);
	if (!pxToolsObject)
	{
		Asura_Assert(0, "Tools_Entity_FMVSpeaker::Create() - no tools object");
		return NULL;
	}

	Tools_Object_System::SelectObject(pxToolsObject);
	Tools_Timed_Message_System::AddMessage(0xffffff, "Created new FMV Speaker Entity");
	Tools_SaveAsura::SetEntityModified();

	return pxToolsObject;
}

////////////////////////////////////////////////////////////////////////////////
//
// return true if this entity needs this callback type
////////////////////////////////////////////////////////////////////////////////

bool Tools_Entity_FMVSpeaker::HandlesThisEvent(int iEventID) const
{
	return PARENT::HandlesThisEvent(iEventID);
}

////////////////////////////////////////////////////////////////////////////////
//
// return true if this entity accepts this message
////////////////////////////////////////////////////////////////////////////////

bool Tools_Entity_FMVSpeaker::HandlesThisMessage(int iMessageID)
{
	return PARENT::HandlesThisMessage(iMessageID);
}

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////

Tools_Object* Tools_Entity_FMVSpeaker::CreateObjectOfThisType() const
{
	return Create(true);	
}

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////

void Tools_Entity_FMVSpeaker::Copy(const Tools_Object* pxObject)
{
	PARENT::Copy(pxObject);

	const Tools_Entity_FMVSpeaker* pxFromObject = (const Tools_Entity_FMVSpeaker*) pxObject;

	Asura_ServerEntity_FMVSpeaker* pxServerFromEntity = (Asura_ServerEntity_FMVSpeaker*) pxFromObject->Entity;
	Asura_ServerEntity_FMVSpeaker* pxServerToEntity = (Asura_ServerEntity_FMVSpeaker*) Entity;

	pxServerToEntity->m_fInnerRange = pxServerFromEntity->m_fInnerRange;
	pxServerToEntity->m_fOuterRange = pxServerFromEntity->m_fOuterRange;

	Asura_ClientEntity_FMVSpeaker* pxClientToEntity = (Asura_ClientEntity_FMVSpeaker *) m_pxClientEntity;
	Asura_Assert(pxClientToEntity, "Couldn't find client entity to do copy");

	pxClientToEntity->m_fInnerRange = pxServerFromEntity->m_fInnerRange;
	pxClientToEntity->m_fOuterRange = pxServerFromEntity->m_fOuterRange;
}

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////

void Tools_Entity_FMVSpeaker::Render()
{
//	if (Tools_Mode_Sound::ShowOuterRadius)
	{
		if (IsSelected() || (Tools_Object_System::ObjectUnderCursorList.contains(this) && !Tools_Object_System::SelectedObject))
		{
			Asura_ServerEntity_FMVSpeaker* pxAsuraEntity = (Asura_ServerEntity_FMVSpeaker*) Entity;
			// draw inner/outer spheres
			{
				extern void Render_Sphere( const Asura_Vector_3& xCentre, float fRadius, int iColour );
				Render_Sphere(Position, pxAsuraEntity->m_fInnerRange, 0x7f0000ff);
				Render_Sphere(Position, pxAsuraEntity->m_fOuterRange, 0x7fff0000);
			}
		}
	}

	Tools_Object::Render();
}

////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////

void Tools_Entity_FMVSpeaker::UpdatePositionInAsuraData(bool bUpdateLinkedObjects)
{
	Asura_ServerEntity_FMVSpeaker* pxServerEntity = static_cast<Asura_ServerEntity_FMVSpeaker*>(m_pxServerEntity);

	// update the servers' position when the tools position changes
	pxServerEntity->SetPosition(Position);

	Tools_Entity_Object::UpdatePositionInAsuraData(bUpdateLinkedObjects);
}

#endif // ASURA_USE_VIDEO_TO_TEXTURE_SYSTEM
