//
// Tools_LightingTest_Object.cpp
//
// Created by Richard Rice - 14/03/2003
// 
// Copyright (C) 2003 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "Lighting/Tools_LightingTest_Object.h"

#include "Animation/Asura_Animation.h"
#include "Animation/Asura_Hierarchy.h"
#include "Interface/Asura_Dialog.h"
#include "Interface/Asura_Overlay.h"
#include "Interface/Asura_Overlay_System.h"
#include "Mesh/Asura_Mesh.h"
#include "Mesh/Asura_Mesh_System.h"

#include "Tools_Overlays.h"
#include "Hierarchy/Tools_Animation_Files.h"
#include "Misc/Tools_Timed_Messages.h"

Asura_Vector_2 Tools_LightingTest_Object::s_xOverlayPosition(10,10);

Tools_LightingTest_Object::Tools_LightingTest_Object(const Asura_Vector_3* pxPosition,const Asura_Matrix_3x3* pxOrientation)
:Tools_Object(TOT_LightingTest)
{
	Position = *pxPosition;
	Orientation = *pxOrientation;
	Mesh = Asura_Mesh_System::GetMesh("ToolsShape_CutsceneActor");
	m_pxAnimation = NULL;

	ChooseSkin();

	SnapToLevel();
}

Tools_LightingTest_Object::~Tools_LightingTest_Object()
{
	if(m_pxAnimation) delete m_pxAnimation;
}


void Tools_LightingTest_Object::Render()
{
	if(m_pxAnimation)
	{
		//render the anim
		m_pxAnimation->Update(Position,Orientation);
	}
	else
	{
		//just render the default character mesh
		Tools_Object::Render();
	}
}

void Tools_LightingTest_Object::OnSelection()
{
	CreateOverlay();
}

void Tools_LightingTest_Object::OnDeselection()
{
	DeleteOverlay();
}


void Tools_LightingTest_Object::CreateOverlay()
{
	Asura_Overlay* pxOverlay;
	pxOverlay = new Asura_Overlay(GetTypeDescription(),false,&s_xOverlayPosition);
	
	Asura_Dialog* pxDialog;
	Asura_Vector_2 xOffset = Asura_Vector_2(1.0f,3.0f);

	pxDialog = new Asura_Function_Dialog("Select Skin",xOffset,ChooseSkin,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.y+=Asura_Dialog::DefaultSpacing();

	pxDialog = new Asura_Function_Dialog("Select Anim",xOffset,ChooseAnimation,(int)this);
	pxOverlay->AddDialog(pxDialog);
	xOffset.y+=Asura_Dialog::DefaultSpacing();

	
	pxOverlay->m_uID = TOOLS_OVERLAY_SELECTEDOBJECT;

 	Asura_Overlay_System::SubmitNewOverlay(pxOverlay,false);
	
}

void Tools_LightingTest_Object::DeleteOverlay()
{
	Asura_Overlay_System::CloseOverlays(TOOLS_OVERLAY_SELECTEDOBJECT);
}


void Tools_LightingTest_Object::ChooseSkin()
{
	Asura_Hierarchy_Skin* pxSkin = 0;
	Asura_Hierarchy_Anim* pxAnim = 0;

	//Choose a skin and animation
	if(!Tools_Animation_Files::ChooseSkin((const Asura_Hierarchy_Skin**)&pxSkin)) return;
	if(!Tools_Animation_Files::ChooseAnimation(pxSkin,(const Asura_Hierarchy_Anim**)&pxAnim)) return;

	delete m_pxAnimation;
	m_pxAnimation = new Asura_Animation(pxSkin->Name.GetString());
	m_pxAnimation->m_xPrimaryWrapper.StartAnim(pxAnim->Name.GetHash(),true,0.0f);
}

void Tools_LightingTest_Object::ChooseAnimation()
{
	if(!m_pxAnimation)
	{
 		Tools_Timed_Message_System::AddMessage(0xffffff,"Please select a skin first");
		return;
	}

	Asura_Hierarchy_Anim* pxAnim = 0;

	if(!Tools_Animation_Files::ChooseAnimation(m_pxAnimation->GetSkin(),(const Asura_Hierarchy_Anim**)&pxAnim)) return;

	m_pxAnimation->m_xPrimaryWrapper.StartAnim(pxAnim->Name.GetHash(),true,0.0f);
}
