//===========================================================================//
// File:	D3DRMVideoViewPoint_Tool.cpp                                     //
// Project: MUNGA           Brick: Win95 Video Renderer 	                 //
// Contents: Windows95 Layer Video Renderer									 //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 03/15/97  JTR  Initial coding.                                            //
//---------------------------------------------------------------------------//
// Copyright (C) 1997, Virtual World Entertainment, Inc.                     //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "AdeptHeaders.hpp"

#include "CameraComponent.hpp"
#include <ElementRenderer\StateChange.hpp>

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
CameraComponent::ClassData*
	CameraComponent::CreateFactoryRequest(FactoryRequestParameters *parameters)
{
	Check_Object(parameters);

	//
	//-------------------------------------------------------------------------
	// Allocate enough room for what we need to write out, then call our parent
	//-------------------------------------------------------------------------
	//
	MemoryStream *component_stream = parameters->m_stream;
	Check_Object(component_stream);
	component_stream->AllocateBytes(sizeof(CameraComponent));
	bool result = BaseClass::CreateFactoryRequest(parameters) != NULL;

	//
	//-----------------------
	// Read the viewing state
	//-----------------------
	//
	Page *page = parameters->m_page;
	Check_Object(page);
	MString prefix="Scene";
	ElementRenderer::StateChange states(page, prefix);
	ElementRenderer::WriteERFVersion(component_stream);
	states.Save(component_stream);

	//
	//------------------------
	// Read the viewing angles
	//------------------------
	//
	Scalar horizontal_angle=Pi_Over_3;
	page->GetEntry("HorizontalAngle", &horizontal_angle);
	*component_stream << horizontal_angle;

	Scalar x_position=1.0f;
	page->GetEntry("ViewPortXPosition", &x_position);
	Clamp(x_position, 0.0f, 1.0f);
	*component_stream << x_position;

	Scalar y_position=1.0f;
	page->GetEntry("ViewPortYPosition", &y_position);
	Clamp(y_position, 0.0f, 1.0f);
	*component_stream << y_position;

	Scalar width=1.0f;
	page->GetEntry("ViewPortWidth", &width);
	Clamp(width, 0.0f, x_position);
	*component_stream << width;

	Scalar height=1.0f;
	page->GetEntry("ViewPortHeight", &height);
	Clamp(height, 0.0f, y_position);
	*component_stream << height;

	bool is_main_camera = true;
	page->GetEntry("IsMainSceneCamera", &is_main_camera);
	*component_stream << is_main_camera;

	bool draw_sky = true;
	page->GetEntry("DrawSky", &draw_sky);
	*component_stream << draw_sky;

	Stuff::Scalar lod_offset = 0.0f;
	page->GetEntry("LODOffset", &lod_offset);
	if (lod_offset < 0.0f)
	{
		lod_offset = -(lod_offset * lod_offset);
	}
	else
	{
		lod_offset = lod_offset * lod_offset;
	}
	*component_stream << lod_offset;
	

	Check_Object(DefaultData);
	return (result) ? DefaultData : NULL;
}
