
#include "ElementProxyHeaders.hpp"

//----------------------------------------------------------------------------
//
bool
	SetExplicitFogModeProcess::SetExplicitFogMode(ChildProxy *child, int newFogMode)
{
	Check_Object(this);
	Check_Object(child);

	if (child->IsDerivedFrom(ElementGroupProxy::DefaultData))
	{
		ElementGroupProxy *group = Cast_Object(ElementGroupProxy*, child);
		return SetExplicitFogMode(group, newFogMode);
	}
	else if (child->IsDerivedFrom(ElementPolygonMeshProxy::DefaultData))
	{
		ElementPolygonMeshProxy *mesh = Cast_Object(ElementPolygonMeshProxy*, child);
		return SetExplicitFogMode(mesh, newFogMode);
	}
	else if (child->IsDerivedFrom(ElementListProxy::DefaultData))
	{
		return true;
	}
	STOP(("Shouldn't get here..."));
	SetExplicitFogModeCallback(child);
	return false;
}

//----------------------------------------------------------------------------
//
bool
	SetExplicitFogModeProcess::SetExplicitFogMode(ElementGroupProxy* group, int newFogMode)
{
	Check_Object(this);
	Check_Object(group);

	//
	//---------------------------------------
	// Make sure that the process says its OK
	//---------------------------------------
	//
	SetExplicitFogModeCallback(group);
	if (!continueProcess)
		return true;

	//
	//--------------------
	// Kill the group name
	//--------------------
	//
	group->SetName(NULL);

	//
	//-----------------------------
	// Run optimize on the children
	//-----------------------------
	//
	unsigned child_count = group->GetChildCount();
	ChildProxy *child = group->UseFirstChildProxy();
	for (unsigned i=0; i<child_count; ++i)
	{
		Check_Object(child);
		ChildProxy *next = child->UseNextSiblingProxy();
		if (SetExplicitFogMode(child, newFogMode))
			child->DetachReference();
		child = next;
		if (!continueProcess)
			break;
	}

	//
	//-------------------------------------------
	// Make sure to discard any remaining proxies
	//-------------------------------------------
	//
	if (child)
		child->DetachReference();

	return false;
}

//----------------------------------------------------------------------------
//
bool
SetExplicitFogModeProcess::SetExplicitFogMode(ElementPolygonMeshProxy* mesh, int newFogMode)
{
	Check_Object(this);
	Check_Object(mesh);

	//
	//---------------------------------------
	// Make sure that the process says its OK
	//---------------------------------------
	//
	SetExplicitFogModeCallback(mesh);
	if (!continueProcess)
		return true;

	//
	// Set the alpha mode
	//
	unsigned unique_combinations = mesh->GetPrimitiveCount();
	for (int i = 0; i < unique_combinations; i++)
	{
		MLR_I_PMesh *primitive = mesh->GetPrimitive(i);
		Check_Object(primitive);
		MLRState state = primitive->GetReferenceState();
		state.SetFogMode(newFogMode);
		primitive->SetReferenceState(state);
	}
	return true;
}

//----------------------------------------------------------------------------
//
void
	SetExplicitFogModeProcess::SetExplicitFogMode(ElementSceneProxy* scene, int newFogMode)
{
	Check_Object(this);
	Check_Object(scene);

	//
	//---------------------------------------
	// Make sure that the process says its OK
	//---------------------------------------
	//
	SetExplicitFogModeCallback(scene);
	if (!continueProcess)
		return;

	//
	//-----------------------------
	// Run optimize on the children
	//-----------------------------
	//
	ChildProxy *child = scene->UseFirstChildProxy();
	while (child)
	{
		Check_Object(child);
		ChildProxy *next = child->UseNextSiblingProxy();
		if (SetExplicitFogMode(child, newFogMode))
			child->DetachReference();
		child = next;
		if (!continueProcess)
			break;
	}

	//
	//-------------------------------------------
	// Make sure to discard any remaining proxies
	//-------------------------------------------
	//
	if (child)
		child->DetachReference();

}
