/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "core/camera.h"
		#include "log/log.h"

		using namespace GS::Core;
		using namespace GS::NML;


//------------------------------------------------------------------------------
bool			Camera::FromMetaTag(Tag &tag)
{
	if	(tag.name != "Camera")
		__ERR__(__LOG_E__ << "Could not parse light, incorrect root tag (" << tag.name << ").\n", false)

	registry.DeleteKey("PostProcess:Dof;");

	// Parse root tags.
	NMLTagForeach(pt, tag)
	{
				if	(pt->name == "Item")
				Item::FromMetaTag(*pt);

		else	if	(pt->name == "ZNear")
				z_near = Types::Max(pt->GetReal(), 0.2f);
		else	if	(pt->name == "ZFar")
				z_far = pt->GetReal();
		else	if	(pt->name == "ZoomFactor")
				zoom_factor = pt->GetReal();

		else	if	(pt->name == "Orthographic")
				is_orthographic = true;
		else	if	(pt->name == "OrthographicWidth")
				ortho_w = pt->GetReal();
		else	if	(pt->name == "OrthographicHeight")
				ortho_h = pt->GetReal();

	// Legacy support.
	#if	1
		else	if	(pt->name == "FStop")
				registry.CreateKey("PostProcess:Dof:FStop", pt->GetReal());
		else	if	(pt->name == "FocalDistance")
				registry.CreateKey("PostProcess:Dof:FDist", pt->GetReal());

		else	if	(pt->name == "ViewportOrigin");
		else	if	(pt->name == "ViewportSize");
	#endif

		else	if	(pt->name == "AspectRatioRefYAxis")
				aspect_ratio_ref_yaxis = pt->GetBool();

		else	__LOG_W__ << "Unknown tag '" << pt->name << "' in <Camera>.\n";
	}
	return true;
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
Tag				*Camera::AsMetaTag()
{
	Tag *root = new Tag("Camera");
	if	(!root)
		__ERR__(__LOG_E__ << "Could not serialize camera. Failed to create root tag.\n", NULL)

	// Store item.
	root->AddChild(Item::AsMetaTag());

	// Misc.
	if	(z_near != Units::Cm(10))
		root->AddChild("ZNear", z_near);
	if	(z_far != Units::Km(100))
		root->AddChild("ZFar", z_far);
	if	(zoom_factor != 3.2f)
		root->AddChild("ZoomFactor", zoom_factor);

	if	(is_orthographic)
		root->AddChild("Orthographic");
	if	(ortho_w != Units::Mtr(1.f))
		root->AddChild("OrthographicWidth", ortho_w);
	if	(ortho_h != Units::Mtr(1.f))
		root->AddChild("OrthographicHeight", ortho_h);

	root->AddChild("AspectRatioRefYAxis", aspect_ratio_ref_yaxis);
	return root;
}
//------------------------------------------------------------------------------
