// COPYRIGHT (C) 1990-1999.  All Rights Reserved. - BITMAP4.CPP
// Zinc Software Incorporated.  Pleasant Grove, Utah  USA
// May be freely copied, used and distributed.

#include <zaf.hpp>
#include <z_utils.hpp>
#include "bitmap2.hpp"
#include "bitmap4.hpp"

// Constructor for the animated bitmap class.
ZafAnimatedBitmap::ZafAnimatedBitmap(int left, int top, int width, int height,
 	ZafIChar *tBitmapSeriesName, int tNumberOfBitmaps, int tTimerInterval) :
 	MyBitmap(left, top, width, height, ZAF_NULLP(ZafIChar)),
	bitmapSeriesName(ZAF_NULLP(ZafIChar)), timerInterval(tTimerInterval)
{
	// Set the initial information and general attributes.
	SetBitmapIndex(0);
	SetNoncurrent(true);

	// Create and add the timer to the event manager.
	bitmapTimer = new ZafTimer(D_ON, (unsigned long)TimerInterval());
	bitmapTimer->SetQueueEvents(false);
	bitmapTimer->Add(this);

	// Initialize the series name and number of bitmaps.
	SetNumberOfBitmaps(tNumberOfBitmaps);
	SetBitmapSeriesName(tBitmapSeriesName);

	// Set various other attributes.
	SetAnimated(true);
	SetAutoSize(true);
	SetTimerInterval(tTimerInterval);

	zafEventManager->Add(bitmapTimer);
}

// Destructor for the animated bitmap class is used for cleanup.
ZafAnimatedBitmap::~ZafAnimatedBitmap(void)
{
	if (bitmapTimer)
	{
		bitmapTimer->Subtract(this);
		zafEventManager->Subtract(bitmapTimer);
		delete bitmapTimer;
	}

	delete []bitmapSeriesName;

	// Prevent bitmapData from being deleted by base class destructor.
	bitmapData = ZAF_NULLP(ZafBitmapData);
}

// All event processing is handled in the Event function.
ZafEventType ZafAnimatedBitmap::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = event.type;
	switch(ccode)
	{
	// The N_TIMER event is used to increment the counter and add the next bitmap.
	case N_TIMER:
		// Get the next bitmap if animated.
		if (Animated())
			SetBitmapData();
		break;

	default:
		ccode = MyBitmap::Event(event);
	}
	return (ccode);
}

// Set the current bitmap index.
void ZafAnimatedBitmap::SetBitmapIndex(int tBitmapIndex)
{
	bitmapIndex = tBitmapIndex;
}

// Set the animation, true = on, false = off.
bool ZafAnimatedBitmap::SetAnimated(bool tAnimated)
{
	animated = tAnimated;
	return (animated);
}

// Get the next bitmap in the series to be displayed.
void ZafAnimatedBitmap::SetBitmapData(void)
{
	// Set up the next bitmap name in the series.
	sprintf(bitmapName, ZAF_ITEXT("%s%i"), bitmapSeriesName, bitmapIndex % numberOfBitmaps);
	bitmapIndex++;
	if (bitmapIndex >= numberOfBitmaps)
		bitmapIndex = 0;

	// The zafDataManager can load the next bitmap
	// from the persistence file using AllocateData.
	bitmapData = DynamicPtrCast(zafDataManager->AllocateData(
			bitmapName, ZafBitmapData::className, ZafBitmapData::classID), 
			ZafBitmapData);

	RedisplayData();
}

// Set the bitmap series name.
void ZafAnimatedBitmap::SetBitmapSeriesName(ZafIChar *tBitmapSeriesName)
{
	bitmapSeriesName = strdup(tBitmapSeriesName);
	bitmapName = new ZafIChar[ZAF_MAXNAMELEN];
	SetBitmapData();
}

// Set the number of bitmaps.
void ZafAnimatedBitmap::SetNumberOfBitmaps(int tNumberOfBitmaps)
{
	numberOfBitmaps = tNumberOfBitmaps;
}

// Set the timer interval.
void ZafAnimatedBitmap::SetTimerInterval(int tInterval)
{
	timerInterval = tInterval;
	bitmapTimer->SetInterval((unsigned long)timerInterval);
}
