/****************************************************************************************

		UVMaps.h

		Copyright: (c) 2001 Eovia Corp. All rights reserved.

		Author:	Ray Dream
		Date:	09/14/99

		Revision History

		when		who			what & why
		==========	==========	======================================================
		09/14/99	Charles		Created

*****************************************************************************************/

#ifndef __UVMAPS__
#define __UVMAPS__

#if CP_PRAGMA_ONCE
#pragma once
#endif

#include "IShRasterLayer.h"
#include "MCPtrArray.h"
#include "MCCountedPtrArray.h"
#include "I3DShInstance.h"


struct InvalidTextureRectCallback
{
	virtual boolean		GetInvalidTextureRect(TMCRect& invalidRect) = 0;
};


/// Stores the texture map calculated for an instance.
/// (see I3DShInstance::GetFlatennedTexturesSync)
///
class UVMap
{
public:

	UVMap() : fHasAlphaChannel(0,true)
	{
		fUsePixelBucket = false;
		fUseRasterLayer = false;
	}

	void SetUsePixelBucket()
	{
		fUsePixelBucket = true;
		fUseRasterLayer = false;
	}
	void SetUseRasterLayer()
	{
		fUsePixelBucket = false;
		fUseRasterLayer = true;
	}


	//-----------------------------------------------------------------------------------	
	void						   SetPixelBucket( TMCPixelBucket * fPixelBucket , const int32 uvSpaceID )
	//-----------------------------------------------------------------------------------
	{
		MCAssert(uvSpaceID >=0 );
		if(int32(fPixelBucketArray.GetElemCount()) < uvSpaceID+1)
			fPixelBucketArray.SetElemCount(uvSpaceID+1);
		fPixelBucketArray.SetElem(uvSpaceID,fPixelBucket);
	}

	//-----------------------------------------------------------------------------------
	TMCPixelBucket*				   GetPixelBucket( const int32 uvSpaceID )
	//-----------------------------------------------------------------------------------
	{
		if(int32(fPixelBucketArray.GetElemCount()) < uvSpaceID+1)
			return NULL;
		TMCPixelBucket *bucket;
		fPixelBucketArray.GetElem(uvSpaceID,bucket);
		return bucket;
	}

	//-----------------------------------------------------------------------------------
	TMCCountedPtr<IShRasterLayer>  GetRasterLayer( const int32 uvSpaceID ) const
	//-----------------------------------------------------------------------------------
	{	
		TMCCountedPtr<IShRasterLayer> temp;
		if(int32(fRasterLayerArray.GetElemCount()) >= uvSpaceID+1)
			fRasterLayerArray.GetElem(uvSpaceID, &temp);
		return temp;
	}

	//-----------------------------------------------------------------------------------
	void						   SetRasterLayer( TMCCountedPtr<IShRasterLayer> rasterLayer , const int32 uvSpaceID )
	//-----------------------------------------------------------------------------------
	{
		MCAssert(uvSpaceID >=0 );
		if(int32(fRasterLayerArray.GetElemCount()) < uvSpaceID+1)
			fRasterLayerArray.SetElemCount(uvSpaceID+1);
		fRasterLayerArray.SetElem(uvSpaceID,rasterLayer);
	}
	
	TMCPtrArray<TMCPixelBucket>&   GetPixelBucketArray() { return fPixelBucketArray; }
	
	void SetHasAlphaChannel(const int32 uvSpaceID,boolean hasAlpha)
	{ 
		if (int32(fHasAlphaChannel.GetElemCount()) < uvSpaceID+1)
		{
			fHasAlphaChannel.SetElemCount(uvSpaceID+1);
		}
		fHasAlphaChannel[uvSpaceID] = hasAlpha;
	}
	boolean GetHasAlphaChannel(const int32 uvSpaceID)
	{
		if (int32(fHasAlphaChannel.GetElemCount()) < uvSpaceID+1)
		{
			fHasAlphaChannel.SetElemCount(uvSpaceID+1);
		}
		return fHasAlphaChannel[uvSpaceID];
	}

	void SetInvalidTextureRectCallback(const int32 uvSpaceID, InvalidTextureRectCallback* callback)
	{
		if (int32(fInvalidTextureRectCallbacks.GetElemCount()) < uvSpaceID+1)
			fInvalidTextureRectCallbacks.SetElemCount(uvSpaceID+1);

		fInvalidTextureRectCallbacks[uvSpaceID] = callback;
	}
	InvalidTextureRectCallback* GetInvalidTextureRectCallback(const int32 uvSpaceID)
	{
		if (int32(fInvalidTextureRectCallbacks.GetElemCount()) < uvSpaceID+1)
			fInvalidTextureRectCallbacks.SetElemCount(uvSpaceID+1);

		return fInvalidTextureRectCallbacks[uvSpaceID];
	}

protected:
	TMCPtrArray<TMCPixelBucket>					fPixelBucketArray;
	TMCCountedPtrArray<IShRasterLayer>			fRasterLayerArray;	
	TMCArray<boolean>							fHasAlphaChannel;	// true if there is an alpha channel that can be transparent (not all ones)
	TMCPtrArray<InvalidTextureRectCallback>		fInvalidTextureRectCallbacks;

public:
	boolean fUsePixelBucket;
	boolean fUseRasterLayer;
};



/// Stores the texture maps (one per shader channel) calculated for an instance, and their size.
/// (see I3DShInstance::GetFlatennedTexturesSync)
///
class UVMaps
{
public:
	UVMaps()
	{
		fWidth = fHeight = 0;
	}

	boolean HasMap(uint32 index , int32 uvSpaceId )
	{
		return	(fMap[index].GetPixelBucket(uvSpaceId) != nil) || 
				(fMap[index].GetRasterLayer(uvSpaceId) != nil);
	}

	void Set(uint32 width, uint32 height)
	{
		fWidth = width;
		fHeight = height;
	}

public:

	UVMap fMap[kNbrUVMapsIndices];
	uint32 fWidth;
	uint32 fHeight;
};


#endif


