//
// Tools_Entity_LogicTrigger_Object.cpp
//
// Created by Richard Rice - 10/07/2004
// 
// Copyright (C) 2004 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "Entity/Tools_Entity_LogicTrigger_Object.h"

#include "Debugging/Asura_Assert.h"
#include "Entity/Asura_Entity_Classification.h"
#include "Entity/Asura_StaticMessage_System.h"
#include "Entity/Triggers/Asura_ServerEntity_LogicTrigger.h"
#include "Interface/Asura_Dialog.h"
#include "Interface/Asura_Overlay.h"
#include "Mesh/Asura_Mesh_System.h"
#include "Rendering/Asura_Text_Output.h"

#include "Entity/Tools_Entity_Data.h"
#include "Entity/Tools_MessageEditor.h"
#include "Entity/Tools_Message_Editing.h"
#include "Entity/Tools_Entity_Simulation.h"
#include "Objects/Tools_Object_System.h"
#include "Load&Save/Tools_SaveAsura.h"



Tools_Entity_LogicTrigger_Object::Tools_Entity_LogicTrigger_Object(Asura_Entity* pxEntity)
:Tools_Entity_Activatable_Object(pxEntity)
{
	Asura_Assert(pxEntity->GetClassification()==AsuraEntityClass_LogicTrigger,"");
	Type = TOT_Entity_LogicTrigger;
//	Mesh = Asura_Mesh_System::GetMesh("ToolsShape_Splitter");

	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;
	m_pbActive = new bool[pxAsuraEntity->m_uNumberOfInputs];
	for(unsigned i=0;i<pxAsuraEntity->m_uNumberOfInputs;i++)
	{
		m_pbActive[i] = pxAsuraEntity->m_pxInputs[i].m_bActive ? true : false;
	}

	UpdateMesh();
}

Tools_Entity_LogicTrigger_Object::~Tools_Entity_LogicTrigger_Object()
{
	delete [] m_pbActive;
}

void Tools_Entity_LogicTrigger_Object::AddOverlayControls(class Asura_Overlay* pxNewOverlay,class Asura_Vector_2& xOffset)
{
	Tools_Entity_Activatable_Object::AddOverlayControls(pxNewOverlay,xOffset);
	Asura_Dialog* pxNewDialog;

	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;


	//trigger type
	static const char* s_apszTypes[] =
	{
		"And",
		"Or",
		"Xor",
	};

	pxNewDialog = new Asura_DropDown_List_Dialog(xOffset,s_apszTypes,(int*)&pxAsuraEntity->m_uType,3,3,NULL,&Changed,"Type");
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.y += Asura_Dialog::LargeSpacing();


	pxNewDialog = new Asura_FlagTickbox_Dialog("Trigger On State Change",xOffset,&pxAsuraEntity->m_uLogicFlags,ASURA_LOGICTRIGGERFLAG_TRIGGER_ON_STATE_CHANGE,&Changed);
 	pxNewDialog->SetHelpText("If this is set then the on/off messages will be sent whenever the on/off state changes.\nOtherwise the messages will only be sent in response to a trigger message.");
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	//on/off messages
	char szText[100];

	sprintf(szText,"Edit On Messages (%d)",Asura_StaticMessage_System::GetNumberOfMessagesInBlock(pxAsuraEntity->m_usMessageBlockOn));
	pxNewDialog = new Asura_Function_Dialog(szText,xOffset,EditOnMessages);
	pxNewDialog->SetHelpText("Edit the messages to be sent when this enters the 'on' state.");
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	sprintf(szText,"Edit Off Messages (%d)",Asura_StaticMessage_System::GetNumberOfMessagesInBlock(pxAsuraEntity->m_usMessageBlockOff));
	pxNewDialog = new Asura_Function_Dialog(szText,xOffset,EditOffMessages);
	pxNewDialog->SetHelpText("Edit the messages to be sent when this enters the 'off' state.");
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.y += Asura_Dialog::LargeSpacing();


	//add/delete inputs
	pxNewDialog = new Asura_Function_Dialog("Add Input",xOffset,AddInput,(int)this);
 	pxNewDialog->SetHelpText("Add a new input to this trigger.");
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.x += 14.0f;

	pxNewDialog = new Asura_Function_Dialog("Delete Input",xOffset,DeleteInput,(int)this);
 	pxNewDialog->SetHelpText("Deletes an input from this trigger.");
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.x -= 14.0f;
	xOffset.y += Asura_Dialog::DefaultSpacing();

	pxNewDialog = new Asura_Function_Dialog("Rename Input",xOffset,RenameInput,(int)this);
 	pxNewDialog->SetHelpText("Rename an input on this trigger. This will also update any existing messages that are targeting the input.");
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.y += Asura_Dialog::LargeSpacing();


	pxNewDialog = new Asura_Dialog("Inputs :",xOffset);
	pxNewOverlay->AddDialog(pxNewDialog);
	xOffset.y += Asura_Dialog::DefaultSpacing();

	for(unsigned i=0;i<pxAsuraEntity->m_uNumberOfInputs;i++)
	{
		pxNewDialog = new Asura_Tickbox_Dialog(pxAsuraEntity->m_pxInputs[i].m_xName.GetString(),xOffset,&m_pbActive[i],&Changed);
	 	pxNewDialog->SetHelpText("Initial state for this input.");
		pxNewOverlay->AddDialog(pxNewDialog);
		xOffset.y += Asura_Dialog::DefaultSpacing();

	}

	
}

void Tools_Entity_LogicTrigger_Object::AddOverlayControls_Input(struct Asura_StaticMessage* pxMessage,class Asura_Overlay* pxOverlay,class Asura_Vector_2& xOffset)
{
	Asura_Assert(pxMessage->m_pData,"");
	Asura_Message_LogicTrigger_Input* pxData = (Asura_Message_LogicTrigger_Input*) pxMessage->m_pData;

	Asura_Dialog* pxDialog = 0;

	//find the entity
	Asura_ServerEntity_LogicTrigger* pxEntity = Asura_ServerEntity_LogicTrigger::FindEntity(Tools_Message_Editing::GetFinalDestinationGuid(pxMessage));
	if(!pxEntity)
	{
		pxDialog = new Asura_Dialog("*Unknown Target*",xOffset);	
		pxOverlay->AddDialog(pxDialog);
		return;
	}


	Asura_Assert(pxEntity->ToolsData && pxEntity->ToolsData->ToolsObject,"");
	Tools_Entity_LogicTrigger_Object* pxObject = (Tools_Entity_LogicTrigger_Object*) pxEntity->ToolsData->ToolsObject;

	//get the name
	const char* pszName;
	char szText[100];
	if(pxData->m_uInputHash)
	{
		pszName = pxObject->GetInputNameFromHash(pxData->m_uInputHash);
	}
	else
	{
		pszName = "*Not Set*";
	}

	Asura_PrintfToBfr(szText,"Input : %s",100,true,pszName);
	pxDialog = new Asura_Function_Dialog(szText,xOffset,EditInputMessage,(int)pxMessage);
	pxDialog->SetHelpText("Select input to trigger");
	pxOverlay->AddDialog(pxDialog);
}


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

Tools_Entity_LogicTrigger_Object* Tools_Entity_LogicTrigger_Object::Create(bool bGenerateName)
{
	//get a name , and the position data
	Asura_Guid uGuid = Asura_Entity_System::GetNextToolsCreatedGuid();
	Tools_Entity_Data* pxToolsData = 0;
	if(bGenerateName)
	{
		pxToolsData = Tools_Entity_Object::CreateToolsDataWithDefaultName("Logic Trigger","Logic Trigger",uGuid,GetNamePrefix());
	}
	else
	{
		pxToolsData = Tools_Entity_Object::CreateToolsData("Logic Trigger",0,GetNamePrefix());
	}
	if(!pxToolsData) return 0 ;

	//create the asura data
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = new Asura_ServerEntity_LogicTrigger();
	pxAsuraEntity->ToolsData = pxToolsData;

	//create the tools pxObject
	Tools_Entity_LogicTrigger_Object* pxObject = new Tools_Entity_LogicTrigger_Object(pxAsuraEntity);

	Tools_SaveAsura::SetEntityModified();

	return pxObject;
}



void Tools_Entity_LogicTrigger_Object::Destroy()
{
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;
	//clear out any message blocks associated with this

	Asura_StaticMessage_System::DeleteMessageBlock(pxAsuraEntity->m_usMessageBlockOn);
	pxAsuraEntity->m_usMessageBlockOn = ASURA_STATICMESSAGE_EMPTYBLOCK;

	Asura_StaticMessage_System::DeleteMessageBlock(pxAsuraEntity->m_usMessageBlockOff);
	pxAsuraEntity->m_usMessageBlockOff = ASURA_STATICMESSAGE_EMPTYBLOCK;

	Tools_Entity_Activatable_Object::Destroy();
}

void Tools_Entity_LogicTrigger_Object::UpdateWhileSelected()
{
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;
		
	if(Changed)
	{
		//update the asura data
		for(unsigned i=0;i<pxAsuraEntity->m_uNumberOfInputs;i++)
		{
	   		pxAsuraEntity->m_pxInputs[i].m_bActive = m_pbActive[i];
		}

		UpdateMesh();
	}

	Tools_Entity_Activatable_Object::UpdateWhileSelected();
}

bool Tools_Entity_LogicTrigger_Object::HandlesThisMessage(int iMessageID)
{
	if(iMessageID == ASURA_MSG_LOGICTRIGGER_INPUTON ||
	   iMessageID == ASURA_MSG_LOGICTRIGGER_INPUTOFF ||
	   iMessageID == ASURA_MSG_ACTIVATE ||
	   iMessageID == ASURA_MSG_DEACTIVATE ||
	   iMessageID == ASURA_MSG_TRIGGER )
	{
		return true;
	}
	//not checking activatable object , since the other messages that it handles aren't appropriate.
	return false; 
}

bool Tools_Entity_LogicTrigger_Object::GetEventDescForMessageBlock(unsigned short usMessageBlockID,Asura_String& xOutputDesc)
{
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;

	if(pxAsuraEntity->m_usMessageBlockOn == usMessageBlockID)
	{
		xOutputDesc = "On Messages";
		return true;
	}

	if(pxAsuraEntity->m_usMessageBlockOff == usMessageBlockID)
	{
		xOutputDesc = "Off Messages";
		return true;
	}

	return false;
}

void Tools_Entity_LogicTrigger_Object::AddInput()
{
	Asura_Assert(ThisEntity,"");
	Tools_Entity_LogicTrigger_Object* pxToolsEntity = (Tools_Entity_LogicTrigger_Object*) this;
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;

	//get a name for the new input
	GetString_Overlay xOverlay("Enter name for the new input","");
	if(!xOverlay.DoModal()) return;

	const char* pszName = xOverlay.GetString();
	if(!strlen(pszName)) return;

	//make sure this name is okay
	Asura_Hash_ID uHash = Asura_GetHashID(pszName);
	for(u_int i=0;i<pxAsuraEntity->m_uNumberOfInputs;i++)
	{
		if(pxAsuraEntity->m_pxInputs[i].m_uHashID == uHash)
		{
			ShowMessageOverlay("An input with that name already exists.");
			return;
		}
	}
	
	
	//allocate an enlarged array of inputs , and copy over the old values
	Asura_LogicTriggerInput* pxInputs = new Asura_LogicTriggerInput[pxAsuraEntity->m_uNumberOfInputs+1];
	for(unsigned i=0;i<pxAsuraEntity->m_uNumberOfInputs;i++)
	{
		pxInputs[i] = pxAsuraEntity->m_pxInputs[i];
	}

	//add the new input
	int iIndex = pxAsuraEntity->m_uNumberOfInputs;
	pxInputs[iIndex].SetDefaultValues();
	pxInputs[iIndex].m_xName = pszName;
	pxInputs[iIndex].m_uHashID = uHash;

	delete [] pxAsuraEntity->m_pxInputs;
	pxAsuraEntity->m_pxInputs = pxInputs;
	pxAsuraEntity->m_uNumberOfInputs++;


	//update active input array
	delete [] pxToolsEntity->m_pbActive;
	pxToolsEntity->m_pbActive = new bool[pxAsuraEntity->m_uNumberOfInputs];
	for(unsigned i=0;i<pxAsuraEntity->m_uNumberOfInputs;i++)
	{
		pxToolsEntity->m_pbActive[i] = pxAsuraEntity->m_pxInputs[i].m_bActive ? true : false;
	}

	//update the overlay
	ThisEntity->DeleteOverlay();
	ThisEntity->CreateOverlay();
}

void Tools_Entity_LogicTrigger_Object::DeleteInput()
{
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;

	//ask which input to delete
	int iIndex = SelectInputIndex("Delete which input?");
	if(iIndex<0) return;
	
	//shuffle down the remaining inputs
	for(u_int i=iIndex;i<pxAsuraEntity->m_uNumberOfInputs-1;i++)
	{
		pxAsuraEntity->m_pxInputs[i] = pxAsuraEntity->m_pxInputs[i+1];
	}

	pxAsuraEntity->m_uNumberOfInputs--;

	//overlay needs updating
	DeleteOverlay();
	CreateOverlay();

	SetEntityModified();
}

void Tools_Entity_LogicTrigger_Object::RenameInput()
{
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;

	//ask which input to rename
	int iIndex = SelectInputIndex("Rename which input?");
	if(iIndex<0) return;
	
	Asura_LogicTriggerInput* pxInput = &pxAsuraEntity->m_pxInputs[iIndex];
	Asura_Hash_ID uOldHash = pxInput->m_uHashID;

	//ask for the new input name
	GetString_Overlay xOverlay("Enter new name for input",pxInput->m_xName.GetString());
	if(!xOverlay.DoModal()) return;

	const char* szNewName = xOverlay.GetString();
	if(!szNewName) return;

	//make sure the name doesn't clash with any other inputs
	Asura_Hash_ID uNewHash = Asura_GetHashID(szNewName);

	for(u_int i=0;i<pxAsuraEntity->m_uNumberOfInputs;i++)
	{
		if(i==(u_int)iIndex) continue;
		
		if(pxAsuraEntity->m_pxInputs[i].m_uHashID == uNewHash)
		{
			ShowMessageOverlay("An input with that name already exists.");
			return;
		}
	}
	
	//update the input with the new name
	pxInput->m_xName = szNewName;
	pxInput->m_uHashID = uNewHash;



	//update any static messages that were targeting the input
	for(Tools_StaticMessageIterator xMessageIt;!xMessageIt.Done();xMessageIt.Next())
	{
		Asura_StaticMessage* pxMessage = xMessageIt.GetCurrent();
		//message targets this?
		if(pxMessage->m_uSendTo != pxAsuraEntity->GetGuid()) continue;
		//is it a logic trigger intput affecting message?
		if(pxMessage->m_usMsgID == ASURA_MSG_LOGICTRIGGER_INPUTON ||
			pxMessage->m_usMsgID == ASURA_MSG_LOGICTRIGGER_INPUTOFF)
		{
			Asura_Message_LogicTrigger_Input* pxData = (Asura_Message_LogicTrigger_Input*) pxMessage->m_pData;

			//update the targeted input , and set as modified
			if(pxData->m_uInputHash == uOldHash)
			{
				pxData->m_uInputHash = uNewHash;
				xMessageIt.SetModified();
			}
		}
	}
	


	//overlay needs updating
	DeleteOverlay();
	CreateOverlay();

	SetEntityModified();
}


void Tools_Entity_LogicTrigger_Object::EditOnMessages()
{
	Asura_Assert(ThisEntity,"");
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) ThisEntity->Entity;

	#ifdef ASURA_TOOLS_USE_NEW_MESSAGE_EDITOR
	Tools_MessageEditor::StartMessageEditingMode(&pxAsuraEntity->m_usMessageBlockOn);
	#else
	Tools_Object_System::EditMessageBlock("On Messages",&pxAsuraEntity->m_usMessageBlockOn);
	#endif
}

void Tools_Entity_LogicTrigger_Object::EditOffMessages()
{
	Asura_Assert(ThisEntity,"");
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) ThisEntity->Entity;

	#ifdef ASURA_TOOLS_USE_NEW_MESSAGE_EDITOR
	Tools_MessageEditor::StartMessageEditingMode(&pxAsuraEntity->m_usMessageBlockOff);
	#else
	Tools_Object_System::EditMessageBlock("Off Messages",&pxAsuraEntity->m_usMessageBlockOff);
	#endif
	
}


bool Tools_Entity_LogicTrigger_Object::OwnsThisMessageBlock(unsigned short usBlockID)
{
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;

	if(pxAsuraEntity->m_usMessageBlockOn == usBlockID) return true;
	if(pxAsuraEntity->m_usMessageBlockOff == usBlockID) return true;
	
	return Tools_Entity_Activatable_Object::OwnsThisMessageBlock(usBlockID);
}

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

void Tools_Entity_LogicTrigger_Object::Copy(const Tools_Object* pxObject)
{
	Tools_Entity_Activatable_Object::Copy(pxObject);

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

	Asura_ServerEntity_LogicTrigger* pxFromEntity = (Asura_ServerEntity_LogicTrigger*) pxFromObject->Entity;
	Asura_ServerEntity_LogicTrigger* pxToEntity = (Asura_ServerEntity_LogicTrigger*) Entity;

	//destroy the old inputs
	Asura_StaticMessage_System::DeleteMessageBlock(pxToEntity->m_usMessageBlockOn);
	Asura_StaticMessage_System::DeleteMessageBlock(pxToEntity->m_usMessageBlockOff);
	
	delete [] pxToEntity->m_pxInputs;
	pxToEntity->m_pxInputs = 0;
	delete [] m_pbActive;
	m_pbActive = 0;
	
	
	//copy between the entities
	pxToEntity->m_uType = pxFromEntity->m_uType;
	pxToEntity->m_uNumberOfInputs = pxFromEntity->m_uNumberOfInputs;

	pxToEntity->m_usMessageBlockOn = Asura_StaticMessage_System::CopyMessageBlock(pxFromEntity->m_usMessageBlockOn,pxFromObject->GetGuid(),GetGuid());
	pxToEntity->m_usMessageBlockOff = Asura_StaticMessage_System::CopyMessageBlock(pxFromEntity->m_usMessageBlockOff,pxFromObject->GetGuid(),GetGuid());

	if(pxToEntity->m_uNumberOfInputs)
	{
		pxToEntity->m_pxInputs = new Asura_LogicTriggerInput[pxToEntity->m_uNumberOfInputs];
		for(u_int i=0;i<pxToEntity->m_uNumberOfInputs;i++)
		{
			pxToEntity->m_pxInputs[i] = pxFromEntity->m_pxInputs[i];
		}
	}

	//copy between the tools objects
	m_pbActive = new bool[pxToEntity->m_uNumberOfInputs];
	for(u_int i=0;i<pxToEntity->m_uNumberOfInputs;i++)
	{
		m_pbActive[i] = pxFromObject->m_pbActive[i];
	}

	UpdateMesh();
}

void Tools_Entity_LogicTrigger_Object::UpdateMesh()
{
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;

	Mesh = 0;
	switch(pxAsuraEntity->m_uType)
	{
		case ASURA_LOGICTRIGGER_AND :
			Mesh = Asura_Mesh_System::GetMesh("ToolsShape_And");
			break;

		case ASURA_LOGICTRIGGER_OR :
			Mesh = Asura_Mesh_System::GetMesh("ToolsShape_Or");
			break;

		case ASURA_LOGICTRIGGER_XOR :
			Mesh = Asura_Mesh_System::GetMesh("ToolsShape_Xor");
			break;


		default :
			Asura_Assert(0,"Unexpected trigger type");
			break;
	}
	
	if(!Mesh)
	{
		Mesh = Asura_Mesh_System::GetMesh("ToolsShape_CutsceneEvent");
	}
}


int Tools_Entity_LogicTrigger_Object::SelectInputIndex(const char* pszMessage)
{
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;

	if(!pxAsuraEntity->m_uNumberOfInputs)
	{
		ShowMessageOverlay("This trigger doesn't have any inputs");
		return -1;
	}

	ListSelection_Overlay xOverlay(pszMessage);
	//add the names of all the inputs
	for(u_int i=0;i<pxAsuraEntity->m_uNumberOfInputs;i++)
	{
		xOverlay.AddItem(pxAsuraEntity->m_pxInputs[i].m_xName.GetString(),i);
	}

	if(!xOverlay.DoModal()) return -1;

	return xOverlay.GetSelectionData();
	
}

Asura_Hash_ID Tools_Entity_LogicTrigger_Object::SelectInputHash(const char* pszMessage)
{
	Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;

	int iIndex = SelectInputIndex(pszMessage);
	if(iIndex<0) return ASURA_HASH_ID_UNSET;

	return pxAsuraEntity->m_pxInputs[iIndex].m_uHashID;
}



const char* Tools_Entity_LogicTrigger_Object::GetInputNameFromHash(Asura_Hash_ID uNameHash) const
{
	const Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (const Asura_ServerEntity_LogicTrigger*) Entity;

	for(u_int i=0;i<pxAsuraEntity->m_uNumberOfInputs;i++)
	{
		if(uNameHash == pxAsuraEntity->m_pxInputs[i].m_uHashID)
		{
			return pxAsuraEntity->m_pxInputs[i].m_xName.GetString();
		}
	}

	return "*Unknown*";
}

void Tools_Entity_LogicTrigger_Object::EditInputMessage(int iMessage)
{
	EditInputMessage((Asura_StaticMessage*)iMessage);
	Tools_Message_Editing::UpdateMessageOverlay();
}

void Tools_Entity_LogicTrigger_Object::EditInputMessage(Asura_StaticMessage* pxMessage)
{
	Asura_Assert(pxMessage->m_pData,"");
	Asura_Message_LogicTrigger_Input* pxData = (Asura_Message_LogicTrigger_Input*) pxMessage->m_pData;

	//find the entity and tools object for this
	Asura_ServerEntity_LogicTrigger* pxEntity = Asura_ServerEntity_LogicTrigger::FindEntity(Tools_Message_Editing::GetFinalDestinationGuid(pxMessage));
	if(!pxEntity) return;
	if(!pxEntity->ToolsData) return;
	if(!pxEntity->ToolsData->ToolsObject) return;

	Tools_Entity_LogicTrigger_Object* pxObject = (Tools_Entity_LogicTrigger_Object*) pxEntity->ToolsData->ToolsObject;

	//ask for the input to trigger
	Asura_Hash_ID uHash = pxObject->SelectInputHash((pxMessage->m_usMsgID==ASURA_MSG_LOGICTRIGGER_INPUTON) ? "Select input to switch on." : "Select input to switch off.");
	if(uHash)
	{
		pxData->m_uInputHash = uHash;

		Tools_MessageEditor::UpdateMessageEditorTable();
	}
}


void Tools_Entity_LogicTrigger_Object::PrintInfo(float& x,float& y)
{
	Tools_Entity_Activatable_Object::PrintInfo(x,y);
	
	if(Tools_Entity_Simulation::IsActive())
	{
		Asura_ServerEntity_LogicTrigger* pxAsuraEntity = (Asura_ServerEntity_LogicTrigger*) Entity;
	
		for(unsigned i=0;i<pxAsuraEntity->m_uNumberOfInputs;i++)
		{
			Asura_Text_Output::Print(x,y++,0xffffff00,0,"%s : %s",pxAsuraEntity->m_pxInputs[i].m_xName.GetString(),pxAsuraEntity->m_pxInputs[i].m_bActive ? "On" : "Off");
		}
		//show current state
		Asura_Text_Output::Print(x,y++,0xffffff00,0,"State : %s",pxAsuraEntity->EvaluateState() ? "On" : "Off");
	}
}
