/*
*	MacInput.cpp
*	----------------------------------------
*	A part of "Intelligent AI"
*	© 2001 SCC Team 064
*	----------------------------------------
*
*	This file:
*		€ Handles the various key commands as directed by the EventDSLoop.
*/


#include "MacInput.h"


/*										IsKeyDown									*/
/*----------------------------------------------------------------------------------*/
Boolean 	IsKeyDown( unsigned char *keyMap, unsigned short theKey )
{
	long		keyMapIndex;
	Boolean		isKeyDown;
	short		bitToCheck;

	// Calculate the key map index
	keyMapIndex = keyMap[theKey/8];

	// Calculate the individual bit to check
	bitToCheck = theKey%8;

	// Check the status of the key
	isKeyDown = ( keyMapIndex >> bitToCheck ) & 0x01;

	// Return the status of the key
	return isKeyDown;
}


// The key presses handled here don't need register quite as quickly, so we'll let the
// MacOS event handler get them for use, they do need to be recognized as only hit once
// because they toggle variables (in this case), key presses gotten with IsKeyDown()
// will be registered every time it is called, therefor they register faster, but may register
// as multiple hits (because of the frequency of calls to it) which is very annoying when
// only trying to toggle variables.
/*									HandleKeyDown									*/
/*----------------------------------------------------------------------------------*/
void	HandleKeyDown( EventRecord *event )
{
	SInt8 			theKey;
	SInt8 			theCode;
	unsigned char	theKeyMap[16];
	SInt32 			menuResult;

	theKey = event->message & charCodeMask;
	theCode = (event->message & keyCodeMask) >> 8;
	
	GetKeys( ( unsigned long * )theKeyMap );

	// Apple Quit
	if ((event->modifiers & cmdKey) != 0)
	{
		menuResult = MenuKey(theKey);
		if (HiWord(menuResult) != 0)
			DoMenu (menuResult);
	}
}


/*									HandleMouseDown									*/
/*----------------------------------------------------------------------------------*/
void 	HandleMouseDown(EventRecord *event )
{
	Rect rectGrow;
	SInt32 menuResult;
	WindowRef whichWindow;
	GrafPtr pGrafSave;
	long grow;
	SInt16 whatPart;

	whatPart = FindWindow(event->where, &whichWindow);
	SelectWindow (whichWindow);
	switch (whatPart)
	{
		case inGoAway:
			break;
		case inMenuBar:
			DrawMenuBar();
//			theKey = event->message & charCodeMask;
//			theCode = (event->message & keyCodeMask) >> 8;
			menuResult = MenuSelect(event->where);
			if (HiWord(menuResult) != 0)
				DoMenu(menuResult);
			break;
		case inDrag:
			if (gpWindow == whichWindow)
				DragWindow (whichWindow, event->where, &(**LMGetGrayRgn()).rgnBBox);
				// must reset the drawable just incase we moved renderers
				aglSetDrawable(aglContext, (CGrafPtr) NULL);
				aglSetDrawable(aglContext, (CGrafPtr) gpWindow);
			break;
		case inGrow:
			SetRect (&rectGrow, 100, 100, 20000, 20000);
			grow = GrowWindow (whichWindow, event->where, &rectGrow);
			if (grow)
			{
				SizeWindow (whichWindow, grow & 0x0000FFFF, grow >> 16, true);
				// do content stuff here
				SetPort (whichWindow);
				InvalRect (&whichWindow->portRect);				// redraw all
				if ((whichWindow == gpWindow) && aglContext)
				{
					aglSetDrawable(aglContext, (CGrafPtr) NULL);
					aglSetDrawable(aglContext, (CGrafPtr) gpWindow);
					sprintf (gstrContext, "%d x %d", gpWindow->portRect.right - gpWindow->
						portRect.left, gpWindow->portRect.bottom - gpWindow->portRect.top);			
				}
			}
			break;
		case inSysWindow:
			SystemClick(event, whichWindow);
			break;
	}
}
