//
// Tools_WorldSpaceRectangle_Object.cpp
//
// Created by Andrew Weinkove - 18/07/2002
// 
// Copyright (C) 2002 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "Debugging/Asura_Assert.h"

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

#include "Objects/Tools_WorldSpaceRectangle_Object.h"
#include "Tools_Overlays.h"
#include "Misc/Tools_Render.h"
#include "Misc/Tools_LOS.h"
#include "Load&Save/Tools_SaveAsura.h"
//#include "Asura_ServerEntity_Glass.h"

Asura_Vector_2						Tools_WorldSpaceRectangle_Object::OverlayPosition(10,10);
Tools_WorldSpaceRectangle_Object*	Tools_WorldSpaceRectangle_Object::ThisVolume = NULL;


Tools_WorldSpaceRectangle_Object::Tools_WorldSpaceRectangle_Object(const char* name, Asura_WorldSpace_Rectangle* pane, Tools_WorldSpaceRectangle_Object** referenceToThis)
:Tools_Object(TOT_WorldSpaceRectangle)
{
	Name = name;
	Pane = pane;
	Changed = false;
	ChangedEulerByHand = false;
	Wireframe = false;
	LockXOrient = false;
	LockYOrient = false;
	LockZOrient = false;

	Asura_String mesh_name = "ToolsShape_CutsceneTrackPoint";
	Mesh = Asura_Mesh_System::GetMesh(mesh_name);

	Position = Pane->CornerPosition;
	Orientation = Pane->Orientation;
	EulerOrientation.FromMatrix(Orientation);

	ReferenceToThis = referenceToThis;
	if(ReferenceToThis) *ReferenceToThis = this;
}


Tools_WorldSpaceRectangle_Object::~Tools_WorldSpaceRectangle_Object()
{
	if(ReferenceToThis)
	{
		Asura_Assert(*ReferenceToThis==this,"");
		//clear the pointer to this volume
		*ReferenceToThis = 0;
	}
}

void Tools_WorldSpaceRectangle_Object::UpdatePositionInAsuraData(bool)
{
	Changed = true;
	Pane->CornerPosition = Position;
	Pane->Orientation = Orientation;
	EulerOrientation.FromMatrix(Orientation);
}

void Tools_WorldSpaceRectangle_Object::OnSelection()
{
	ThisVolume = this;
	CreateOverlay();
}

void Tools_WorldSpaceRectangle_Object::OnDeselection()
{
	DeleteOverlay();
	ThisVolume = 0;
}

void Tools_WorldSpaceRectangle_Object::UpdateWhileSelected()
{
	if (LockXOrient)
	{
		if (EulerOrientation.x != 0.0f)
		{
			EulerOrientation.x = 0.0f;
			ChangedEulerByHand = true;
		}
	}
	if (LockYOrient)
	{
		if (EulerOrientation.y != 0.0f)
		{
			EulerOrientation.y = 0.0f;
			ChangedEulerByHand = true;
		}
	}
	if (LockZOrient)
	{
		if (EulerOrientation.z != 0.0f)
		{
			EulerOrientation.z = 0.0f;
			ChangedEulerByHand = true;
		}
	}

	if (ChangedEulerByHand)
	{
		ChangedEulerByHand = false;
		EulerOrientation.ToMatrix(Orientation);
		Changed = true;
	}
	
	if(Changed)
	{
		//update the asura data
		Changed = false;
		UpdatePositionInAsuraData(false);
		// Not 100% sure about this next line.
		// It assumes that by editing this object, you are editing an entity (like glass).
		// This will be incorrect if an object (i.e. Not an entity) uses this class.aw.
		Tools_SaveAsura::SetEntityModified();
	}

	Tools_Object_To_Position->Position = Pane->CalculateCentre();
	Tools_Object_To_Position->Orientation = Pane->Orientation;
	Tools_Object_To_Position->UpdatePositionInAsuraData(false);

	Tools_Object::UpdateWhileSelected();
}


void Tools_WorldSpaceRectangle_Object::Render()
{
	//render the flyable bit
	Tools_Object::Render();
}


void Tools_WorldSpaceRectangle_Object::PrintInfo(float& x, float &y)
{
	Tools_Object::PrintInfo(x,y);
}

void Tools_WorldSpaceRectangle_Object::SetObjectToPosition(Tools_Object *tools_Object_To_Position)
{
	Tools_Object_To_Position = tools_Object_To_Position;
}

void Tools_WorldSpaceRectangle_Object::CreateOverlay()
{
	Asura_Overlay* newOverlay;
	newOverlay = new Asura_Overlay(GetTypeDescription(),false,&OverlayPosition);
	
	Asura_Dialog* newDialog;
	Asura_Vector_2 offset = Asura_Vector_2(1.0f,3.0f);

	newDialog = new Asura_Numerical_Dialog("Length X",offset,&Pane->Dimensions.x,0,1000,0.1f,"%.3f",&Changed);
	newOverlay->AddDialog(newDialog);
	offset.y+=Asura_Dialog::DefaultSpacing();

	newDialog = new Asura_Numerical_Dialog("Length Y",offset,&Pane->Dimensions.y,0,1000,0.1f,"%.3f",&Changed);
	newOverlay->AddDialog(newDialog);
	offset.y+=Asura_Dialog::DefaultSpacing();
	
	newDialog = new Asura_Tickbox_Dialog("Wireframe",offset,&Wireframe);
	newOverlay->AddDialog(newDialog);
	offset.y+=Asura_Dialog::DefaultSpacing();
	
	newDialog = new Asura_Numerical_Dialog("Orient X",offset,&EulerOrientation.x, -Asura_Maths::Pi, Asura_Maths::Pi, 0.1f, "%.3f", &ChangedEulerByHand);
	newOverlay->AddDialog(newDialog);
	offset.y+=Asura_Dialog::DefaultSpacing();

	newDialog = new Asura_Numerical_Dialog("Orient Y",offset,&EulerOrientation.y, -Asura_Maths::Pi, Asura_Maths::Pi, 0.1f, "%.3f", &ChangedEulerByHand);
	newOverlay->AddDialog(newDialog);
	offset.y+=Asura_Dialog::DefaultSpacing();

	newDialog = new Asura_Numerical_Dialog("Orient Z",offset,&EulerOrientation.z, -Asura_Maths::Pi, Asura_Maths::Pi, 0.1f, "%.3f", &ChangedEulerByHand);
	newOverlay->AddDialog(newDialog);
	offset.y+=Asura_Dialog::DefaultSpacing();

	newDialog = new Asura_Tickbox_Dialog("Lock X Orient", offset, &LockXOrient);
	newOverlay->AddDialog(newDialog);
	offset.y+=Asura_Dialog::DefaultSpacing();

	newDialog = new Asura_Tickbox_Dialog("Lock Y Orient", offset, &LockYOrient);
	newOverlay->AddDialog(newDialog);
	offset.y+=Asura_Dialog::DefaultSpacing();

	newDialog = new Asura_Tickbox_Dialog("Lock Z Orient", offset, &LockZOrient);
	newOverlay->AddDialog(newDialog);
	offset.y+=Asura_Dialog::DefaultSpacing();
	
	newOverlay->m_uID = TOOLS_OVERLAY_SELECTEDOBJECT;

	newOverlay->m_xOrigin = OverlayPosition;
 	Asura_Overlay_System::SubmitNewOverlay(newOverlay,false);
}

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