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

#include "eventwn3.hpp"

// --- EyesObject Class -----------------------------------------------------

EyesObject::EyesObject(void) : ZafBitmap(9, 0, 8, 2, ZAF_NULLP(ZafBitmapData))
{
	eyesBitmap[0][0] = new ZafBitmapData(ZAF_ITEXT("EYES_NW"), *zafObjectPersistence);
	eyesBitmap[0][1] = new ZafBitmapData(ZAF_ITEXT("EYES_N"), *zafObjectPersistence);
	eyesBitmap[0][2] = new ZafBitmapData(ZAF_ITEXT("EYES_NE"), *zafObjectPersistence);
	eyesBitmap[1][0] = new ZafBitmapData(ZAF_ITEXT("EYES_W"), *zafObjectPersistence);
	eyesBitmap[1][1] = new ZafBitmapData(ZAF_ITEXT("EYES_CROSS"), *zafObjectPersistence);
	eyesBitmap[1][2] = new ZafBitmapData(ZAF_ITEXT("EYES_E"), *zafObjectPersistence);
	eyesBitmap[2][0] = new ZafBitmapData(ZAF_ITEXT("EYES_SW"), *zafObjectPersistence);
	eyesBitmap[2][1] = new ZafBitmapData(ZAF_ITEXT("EYES_S"), *zafObjectPersistence);
	eyesBitmap[2][2] = new ZafBitmapData(ZAF_ITEXT("EYES_SE"), *zafObjectPersistence);

	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			eyesBitmap[i][j]->SetDestroyable(false);

	SetBitmapData(eyesBitmap[0][1]);
	SetBordered(true);
}

EyesObject::~EyesObject(void)
{
	SetBitmapData(ZAF_NULLP(ZafBitmapData));

	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			delete eyesBitmap[i][j];
}

ZafEventType EyesObject::Event(const ZafEventStruct &event)
{
	ZafEventType ccode = LogicalEvent(event);
	switch (ccode)
	{
	case L_BEGIN_SELECT:
		// Capture the mouse.
		windowManager->CaptureMouse(this);
		ccode = ZafBitmap::Event(event);
		break;

	case L_END_SELECT:
		// Release the mouse capture.
		if (windowManager->MouseEventRoute() == this)
			windowManager->CaptureMouse(ZAF_NULLP(ZafWindowObject));
		ccode = ZafBitmap::Event(event);
		break;

	case L_CONTINUE_SELECT:
		{
		static int lastX = 1, lastY = 0;
		int x, y;

		// Move the eyes according to the mouse position.
		if (event.position.column < 0)
			x = 0;
		else if (event.position.column < zafRegion.Width())
			x = 1;
		else
			x = 2;
		if (event.position.line < 0)
			y = 0;
		else if (event.position.line < zafRegion.Height())
			y = 1;
		else
			y = 2;

		if (x != lastX || y != lastY)
		{
			// Draw the new bitmap.
			SetBitmapData(eyesBitmap[y][x]);
			RedisplayData();
			lastX = x;
			lastY = y;
		}
		}
		// Continue to default case.

	default:
		ccode = ZafBitmap::Event(event);
	}

	return (ccode);
}
