//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 02/27/98  DPB  Initial coding.										     //
//---------------------------------------------------------------------------//
// Copyright (C) 1997, Virtual World Entertainment, Inc.                     //
//   All Rights reserved worldwide.                                          //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//
#include "stdafx.h"
#include "MW4GameEd.h"
#include "RotateCommand.h"
#include "Selection.h"
#include "EditorWayPoint.h"

using namespace Adept;

RotateCommand::RotateCommand()
				: Command()
{
	deltaPitch = 0;
	deltaYaw = 0;
	deltaRoll = 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
RotateCommand::~RotateCommand()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	RotateCommand::Undo()
{
	Rotate((0 - deltaPitch), (0 - deltaYaw), (0 - deltaRoll));

}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	RotateCommand::Rotate(Scalar pitchchange, Scalar yawchange, Scalar rollchange)
{
	CSelectionNode *selection_node;
	Entity *entity;
	YawPitchRoll starting_rotation;
	LinearMatrix4D new_local_to_world;
	Scalar new_pitch, new_yaw, new_roll;

	if (!selectedEntities.IsEmpty())
	{
		ChainIteratorOf<CSelectionNode*> iterator(&selectedEntities);
		iterator.First();
		while((selection_node = iterator.ReadAndNext()) != NULL)
		{	
			entity = selection_node->selectedEntity;
			Check_Object(entity);
#if 0																						// remove node radius changing
			if (entity->IsDerivedFrom(EditorLatticeNode::DefaultData))
			{
				// lattice nodes don't rotate, they change the size of their radius
				EditorLatticeNode* node;
				node = Cast_Object(EditorLatticeNode*,entity);

				node->railNode->Radius(node->railNode->Radius() + yawchange * 100);
			}
			else
#endif
			{
				starting_rotation = entity->GetLocalToParent();

				new_pitch = starting_rotation.pitch + pitchchange;
				new_yaw = starting_rotation.yaw + yawchange;
				new_roll = starting_rotation.roll + rollchange;

				YawPitchRoll new_rotation(new_yaw, new_pitch, new_roll);
				Point3D translation;
				translation = entity->GetLocalToParent();

				new_local_to_world.BuildRotation(new_rotation);
				new_local_to_world.BuildTranslation(translation);

				entity->SetNewLocalToParent(new_local_to_world);
				entity->SyncMatrices(true);

				deltaPitch += pitchchange;
				deltaYaw += yawchange;
				deltaRoll += rollchange;
				if (entity->IsDerivedFrom (EditorDropZonePoint::DefaultData))
				{
					EditorDropZonePoint *drop;
					drop = Cast_Object (EditorDropZonePoint *,entity);
					Check_Object (drop->dropNode);	
					drop->dropNode->UpdatePoints ();
				}
				else if (entity->IsDerivedFrom (EditorDropNode::DefaultData))
				{
					EditorDropNode *drop;

					drop = Cast_Object (EditorDropNode *,entity);
					drop->UpdatePoints ();
				}
			}
		}
	}
}
