//---------------------------------------------------------------------------//
//   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 "RevolveCommand.h"
#include "MW4GameEd.h"
#include "DisplayWindow.h"
#include "Selection.h"
extern CMW4GameEdApp theApp;

RevolveCommand::RevolveCommand()
				: Command()
{
	deltaRevolution = 0;

	if (!selectedEntities.IsEmpty())
	{
		CSelectionNode *selection_node;
		Entity *entity;
		Point3D pos;
		Scalar max_x = -1.0f, min_x = 999999.9f, max_z = -1.0f, min_z = 999999.9f;

		ChainIteratorOf<CSelectionNode*> iterator(&selectedEntities);
		iterator.First();
		while((selection_node = iterator.ReadAndNext()) != NULL)
		{
			entity = selection_node->selectedEntity;
			Check_Object(entity);

			pos = entity->GetLocalToWorld();

			if (pos.x > max_x) max_x = pos.x;
			if (pos.x < min_x) min_x = pos.x;
			if (pos.z > max_z) max_z = pos.z;
			if (pos.z < min_z) min_z = pos.z;
		}
		origin.x = (min_x + max_x) / 2;
		origin.z = (min_z + max_z) / 2;
	}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
RevolveCommand::~RevolveCommand()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	RevolveCommand::Undo()
{
	Revolve(0 - deltaRevolution);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	RevolveCommand::Revolve(Scalar revolution)
{
	CSelectionNode *selection_node;
	Entity *entity;
	Point3D starting_position;
	YawPitchRoll starting_rotation;
	LinearMatrix4D new_local_to_world;
	Scalar new_x, new_y, new_z, off_x, off_z;

	if (!selectedEntities.IsEmpty())
	{
		ChainIteratorOf<CSelectionNode*> iterator(&selectedEntities);
		iterator.First();
		while((selection_node = iterator.ReadAndNext()) != NULL)
		{	
			entity = selection_node->selectedEntity;
			Check_Object(entity);

			starting_position = entity->GetLocalToParent();
			starting_rotation = entity->GetLocalToParent();

			off_x = starting_position.x - origin.x;
			off_z = starting_position.z - origin.z;

			new_x = origin.x + (Cos(revolution) * off_x + Sin(revolution) * off_z);
			new_z = origin.z + (Cos(revolution) * off_z - Sin(revolution) * off_x);
			new_y = theApp.displayWindow->GetMapPointVertical(new_x,new_z);

			starting_rotation.yaw += revolution;

			Point3D translation(new_x,new_y,new_z);

			new_local_to_world = LinearMatrix4D::Identity;
			new_local_to_world.BuildTranslation(translation);
			new_local_to_world.BuildRotation(starting_rotation);

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

			deltaRevolution += revolution;
		}
	}
}
