// -------------------------------------------------------------------- //
// FILE 		: HSClass.cpp											//
// DATE 		: 15/03/2004											//
// AUTOR		: Aymeric							//
// DESCRIPTION  : class THotSpotList and class THotSpot					//
// -------------------------------------------------------------------- //


#include <vcl.h>
#include "HSClass.h"


// -------------------------------------------------------------------- //
// ------------------------- THotSpotList ----------------------------- //
// -------------------------------------------------------------------- //


// -------------------------------------------------------------------- //
// THotSpotList() constructor											//
// in  : the current form, background's path, number of frames, 		//
//		 hotspotfile's path, blits' directory							//
// out : nothing														//
// -------------------------------------------------------------------- //

THotSpotList::THotSpotList(TForm *fm, AnsiString  bmpFilename, int NumFrames, AnsiString  hspFilename, AnsiString BlitDir)
{
        mCreateHotSpots(fm,bmpFilename,hspFilename,NumFrames,BlitDir);
        mScanStartToEnd=true;
}


// -------------------------------------------------------------------- //
// mCreateHotSpots() create the hotspots list							//
// in  : the same parameters that for the constructor					//
// out : nothing														//
// -------------------------------------------------------------------- //

void THotSpotList::mCreateHotSpots(TForm *fm,AnsiString  bmpFilename, AnsiString  hspFilename, int NumFrames, AnsiString BlitDir)
{
	// -- if the file doesn't exist we call an exception
	if (!FileExists(bmpFilename)) throw bmpFilename;

	mpbmpBackground = new Graphics::TBitmap;
	mpbmpBackground->LoadFromFile(bmpFilename);

	int iFileHandle;
	HSData pszBuffer;
        POINT *pTmp;

	// -- we read the file
	iFileHandle = FileOpen(hspFilename, fmOpenRead);

	// -- we start in the begining of the file
	FileSeek(iFileHandle,0,0);
	FileRead(iFileHandle, &mNumPolysInFile, sizeof (int));
	FileRead(iFileHandle, mszVersion, sizeof (char)*32);

	HotSpot = new THotSpot *[mNumPolysInFile];

	for (int i=0 ; i < mNumPolysInFile ; i++)
	{
		FileRead(iFileHandle, &(pszBuffer.HSFixed), sizeof (HSFixedData) - 3);
		pszBuffer.pText = new char[pszBuffer.HSFixed.TextLength+2];

		pszBuffer.pPtArray = new POINT[pszBuffer.HSFixed.NumPoints];
		pszBuffer.FontName = new char[pszBuffer.HSFixed.FontNameLen+1];

		FileRead(iFileHandle, pszBuffer.pText, sizeof (char)*pszBuffer.HSFixed.TextLength+1);

                FileRead(iFileHandle, pszBuffer.pPtArray, sizeof (POINT)*pszBuffer.HSFixed.NumPoints);
		FileRead(iFileHandle, pszBuffer.FontName, sizeof (char)*pszBuffer.HSFixed.FontNameLen+1);

		pszBuffer.DestForm=fm;
		pszBuffer.bltNumFrames=NumFrames;

		pszBuffer.BlitDirectory=BlitDir;

		HotSpot[i] = new THotSpot(pszBuffer);

	}

	FileClose(iFileHandle);
	delete pszBuffer.pPtArray;
	delete pszBuffer.pText;
	delete pszBuffer.FontName;

}


// -------------------------------------------------------------------- //
// ~THotSpotList() destructor											//
// in  : nothing														//
// out : nothing														//
// -------------------------------------------------------------------- //

THotSpotList::~THotSpotList()
{
	// -- we must delete each rows of the array before delete the array
	// (2 dimensions array)
//	for (int i=0; i < mNumPolysInFile; i++) delete HotSpot[i];
	delete [] HotSpot;
	delete mpbmpBackground;
}


// -------------------------------------------------------------------- //
// HotSpotActive() true if a hotspot & enabled							//
// in  : the number of the hotspot										//
// out : a boolean														//
// -------------------------------------------------------------------- //

bool THotSpotList::HotSpotActive(int HsNum)
{
	return ((HsNum >=0) && HotSpot[HsNum]->Enabled());
}


// -------------------------------------------------------------------- //
// GetHotSpot()	return index of hotspot or -1 if not in list.			//
// in  : mouse's coordonates											//
// out : an int (index or -1)											//
// -------------------------------------------------------------------- //

int THotSpotList::GetHotSpot(int X, int Y)
{
	for (int i=0; i < mNumPolysInFile; i++)
		if ( HotSpot[i]->InRegion(X,Y) ) return(i);
	return(-1);
}


// -------------------------------------------------------------------- //
// ScanStartToEnd() set new scan direction								//
// in  : a boolean														//
// out : a boolean (previous scan direction)							//
// -------------------------------------------------------------------- //

bool THotSpotList::ScanStartToEnd(bool Dir)
{
	bool buf=mScanStartToEnd;
	mScanStartToEnd=Dir;
	return(buf);
}


// -------------------------------------------------------------------- //
// EnableAllHotSpots() enable all hotspots								//
// in  : nothing														//
// out : nothing														//
// -------------------------------------------------------------------- //

void THotSpotList::EnableAllHotSpots()
{
	for (int i=0; i < mNumPolysInFile; i++)
		HotSpot[i]->Enable();
}


// -------------------------------------------------------------------- //
// DisableAllHotSpots() disable all hotspots							//
// in  : nothing														//
// out : nothing 														//
// -------------------------------------------------------------------- //

void THotSpotList::DisableAllHotSpots()
{
	for (int i=0; i < mNumPolysInFile; i++)
		HotSpot[i]->Disable();
}

// -------------------------------------------------------------------- //
// --------------------------- THotSpot ------------------------------- //
// -------------------------------------------------------------------- //


// -------------------------------------------------------------------- //
// THotSpot() constructor												//
// in  : HSData passed by THotSpotList::mCreateHotSpots()				//
// out : nothing														//
// -------------------------------------------------------------------- //

THotSpot::THotSpot(HSData &InitVals)
{
	mID=InitVals.HSFixed.ID;
	mNumPoints=InitVals.HSFixed.NumPoints;
	mBlitLeft=InitVals.HSFixed.BlitLeft;
	mBlitTop=InitVals.HSFixed.BlitTop;
	mCentreX=InitVals.HSFixed.CentreX;
	mCentreY=InitVals.HSFixed.CentreY;
	mTag=InitVals.HSFixed.Tag;
	mFlags=InitVals.HSFixed.Flags;
	mPrimaryColour=InitVals.HSFixed.PrimaryColour;
	mSecondaryColour=InitVals.HSFixed.SecondaryColour;
	mpPtArray=new POINT[mNumPoints];
        for (int b=0; b < mNumPoints; b++)
        {
                mpPtArray[b].x=InitVals.pPtArray[b].x;
                mpPtArray[b].y=InitVals.pPtArray[b].y;
        }
	mpfmDest=InitVals.DestForm;
	mText=InitVals.pText;
	mbltNumFrames=InitVals.bltNumFrames;

	Graphics::TBitmap *tbmpBlit = new Graphics::TBitmap;

	if (mbltNumFrames != 0)
	{
		// -- we have got a part of the name file.
		// the file is "<directory>/Blit<mText>.bmp"
		char *file;
		file = new char[InitVals.BlitDirectory.Length()+4+4+InitVals.HSFixed.TextLength+1];
		strcpy(file,"");
		strncat(file,InitVals.BlitDirectory.c_str(),InitVals.BlitDirectory.Length());
		strncat(file,"Blit",4);
		strncat(file,mText.c_str(),InitVals.HSFixed.TextLength);
		strncat(file,".bmp",4);

		if ( FileExists(file) )
		{
			tbmpBlit->LoadFromFile(file);
			mbltStrip = new TImageList(NULL);
			mbltStrip->Height=tbmpBlit->Height;
			mbltStrip->Width=tbmpBlit->Width/mbltNumFrames;
			mbltStrip->Add(tbmpBlit, NULL);
		}

		delete tbmpBlit;
		delete file;
	}

	mHSEnabled=true;
	mHSHot=true;

	mhPolyRegion = CreatePolygonRgn(mpPtArray, mNumPoints, WINDING);

	/*
	-- I'm not sure that I do the right thing

	TextLabel = new TLabel(NULL);
	TextLabel->Owner = mpfmDest;
	TextLabel->Left=InitVals.HSFixed.LabelLeft;
	TextLabel->Top=InitVals.HSFixed.LabelTop;
	TextLabel->Font->Size=InitVals.HSFixed.FontSize;
	TextLabel->Font->Color=InitVals.HSFixed.FontColour;
	TextLabel->Font->Style=InitVals.HSFixed.FontStyle;
	*/
}


// -------------------------------------------------------------------- //
// ~THotSpot() destructor												//
// in  : nothing														//
// out : nothing														//
// -------------------------------------------------------------------- //

THotSpot::~THotSpot()
{
		delete mbltStrip;
		// delete TextLabel;
		delete mpPtArray;
		DeleteObject(mhPolyRegion);
}


// -------------------------------------------------------------------- //
// InRegion() true if mouse pos in PolyRegion							//
// in  : mouse's coordinates											//
// out : boolean														//
// -------------------------------------------------------------------- //

bool THotSpot::InRegion(int X, int Y)
{
        return (PtInRegion(mhPolyRegion,X,Y));
}


// -------------------------------------------------------------------- //
// FloodIt() flood the hotspot in any colour							//
// in  : the colour														//
// out : nothing														//
// -------------------------------------------------------------------- //

void THotSpot::FloodIt(TColor Colour)
{
	mpfmDest->Canvas->Brush->Color=Colour;
	PaintRgn(mpfmDest->Handle,mhPolyRegion);
}


// -------------------------------------------------------------------- //
// OutlineIt() outline hotspot											//
// in  : colour and thickness											//
// out : nothing														//
// -------------------------------------------------------------------- //

void THotSpot::OutlineIt(TColor Colour, int Thickness)
{
	/*
	-- I don't find the correct function to do it

	mpfmDest->Canvas->Brush->Color=Colour;
	FrameRgn(mpfmDest->Handle,mhPolyRegion,...,nWidth,nHeight);
	*/
}


// -------------------------------------------------------------------- //
// BlitImage() blit frame bltState from blit strip onto form			//
// in  : blit's state and a boolean										//
// out : nothing														//
// -------------------------------------------------------------------- //

void THotSpot::BlitImage(int bltState, bool Transparent)
{
        Graphics::TBitmap *blit=GetBlitFrame(bltState);
        blit->Transparent=Transparent;
        mpfmDest->Canvas->Draw(mBlitLeft,mBlitTop,blit);
}


// -------------------------------------------------------------------- //
// GetBlitFrame() get the requested frame from the image list and 		//
//				  return as a bitmap									//
// in  : blit state														//
// out : the bitmap														//
// -------------------------------------------------------------------- //

Graphics::TBitmap *THotSpot::GetBlitFrame(int BlitState)
{
        Graphics::TBitmap *blit=new Graphics::TBitmap;
        if (mbltStrip->GetBitmap(BlitState, blit)) return(blit);
        else
        {
            if (mbltStrip->GetBitmap(0, blit)) return(blit);
            else return(NULL);
        }
}

