//============================================================
//============================================================
//==														==
//==				Configure Sound Routines				==
//==														==
//============================================================
//============================================================


//========================================================  Includes  

#include "Globals.h"
#include "UnivUtilities.h"
#include "ConfigureSound.h"
#include <Sound.h>


//========================================================  Functions  
//========================================================  UpdateVolumeIcon  

void UpdateVolumeIcon (DialogPtr theDialog)

{
	Str255			textStr;
	CIconHandle		cicnHandle;
	Handle			iHandle, iconHandle;
	Rect			iRect;
	short			iType;
	
	GetDItem(theDialog, kSoundIconRect, &iType, &iHandle, &iRect);
	if (isColor)
	{
		cicnHandle = GetCIcon(210 + soundVolume);
		PlotCIcon(&iRect, cicnHandle);
		DisposCIcon(cicnHandle);
	}
	else
	{
		iconHandle = GetIcon(210 + soundVolume);
		PlotIcon(&iRect, iconHandle);
	}
	
	GetDItem(theDialog, kSoundVolume, &iType, &iHandle, &iRect);
	NumToString((long)soundVolume, textStr);
	SetIText(iHandle, textStr);
}


//========================================================  UpdateSoundDialog  

void UpdateSoundDialog (DialogPtr theDialog)

{
	PicHandle		thePict;
	Str255			theTitle;
	Handle			iHandle;
	Rect			iRect;
	short			iType;
	
	DrawDialog(theDialog);
	UpdateVolumeIcon(theDialog);
	
	GetDItem(theDialog, kSoundBalloon, &iType, &iHandle, &iRect);
	if (isColor)
		thePict = GetPicture(kBalloon4PictID);
	else
		thePict = GetPicture(kBalloon1PictID);
	if (thePict != kNilPointer)
	{
		DrawPicture(thePict, &iRect);
		ReleaseResource((Handle)thePict);
	}
	
	GetIndString(theTitle, rMiscStrings, 4);
	if (theTitle[0] == 0)
		PasStringCopy((StringPtr)"\pSound OptionsÉ", (StringPtr)theTitle);
	MoveTo(kSoundTitleLeft, kSoundTitleTop);
	DrawString(theTitle);
	
	DrawDefaultButton(theDialog, kSoundOkay);
}


//========================================================  SoundFilter  

pascal Boolean SoundFilter (DialogPtr theDialog, EventRecord *theEvent, short *itemHit)

{
	switch (theEvent->what)
	{
		case keyDown:
			switch ((theEvent->message) & charCodeMask)
			{
				case kReturnKeyASCII:
				case kEnterKeyASCII:
					FlashDialogButton(theDialog, kSoundOkay);
					*itemHit = kSoundOkay;
					return(TRUE);
					break;
				case kLeftArrowKeyASCII:
				case kDownArrowKeyASCII:
					*itemHit = kSoundSofter;
					return(TRUE);
					break;
				case kRightArrowKeyASCII:
				case kUpArrowKeyASCII:
					*itemHit = kSoundLouder;
					return(TRUE);
					break;
				case k0KeyASCII:
				case k1KeyASCII:
				case k2KeyASCII:
				case k3KeyASCII:
				case k4KeyASCII:
				case k5KeyASCII:
				case k6KeyASCII:
				case k7KeyASCII:
					soundVolume = ((theEvent->message) & charCodeMask) - k0KeyASCII;
					UpdateVolumeIcon(theDialog);
					return(TRUE);
					break;
				default:
					return(FALSE);
			}
			break;
		case updateEvt:
			BeginUpdate((GrafPtr)theDialog);
			UpdateSoundDialog(theDialog);
			EndUpdate((GrafPtr)theDialog);
			theEvent->what = nullEvent;
			return(FALSE);
			break;
		default:
			return(FALSE);
			break;
	}
}


//========================================================  DoConfigureSound  

Boolean DoConfigureSound (void)

{
	DialogPtr		soundDialog;
	Handle			iHandle;
	Rect			iRect;
	short			itemHit, iType, wasVolume;
	Boolean			leaveDialog, userOkayed, wasState;
	
	CenterDialog(rSoundDialogID);
	if (doZooms)
		ZoomOutDialogRect(rSoundDialogID);
	soundDialog = GetNewDialog(rSoundDialogID, kNilPointer, kPutInFront);
	SetPort((GrafPtr)soundDialog);
	ShowWindow((GrafPtr)soundDialog);
	
	wasVolume = soundVolume;
	UpdateSoundDialog(soundDialog);
	
	if (!soundOn)
	{
		GetDItem(soundDialog, kSoundBeam, &iType, &iHandle, &iRect);
		HiliteControl((ControlHandle)iHandle, 255);
		GetDItem(soundDialog, kSoundIncidental, &iType, &iHandle, &iRect);
		HiliteControl((ControlHandle)iHandle, 255);
		GetDItem(soundDialog, kSoundCollisions, &iType, &iHandle, &iRect);
		HiliteControl((ControlHandle)iHandle, 255);
		GetDItem(soundDialog, kSoundCrowd, &iType, &iHandle, &iRect);
		HiliteControl((ControlHandle)iHandle, 255);
	}
	
	GetDItem(soundDialog, kSoundEnable, &iType, &iHandle, &iRect);
	SetCtlValue((ControlHandle)iHandle, (short)soundOn);
	
	GetDItem(soundDialog, kSoundBeam, &iType, &iHandle, &iRect);
	SetCtlValue((ControlHandle)iHandle, (short)beamSoundOn);
	
	GetDItem(soundDialog, kSoundIncidental, &iType, &iHandle, &iRect);
	SetCtlValue((ControlHandle)iHandle, (short)incidentSoundOn);
	
	GetDItem(soundDialog, kSoundCollisions, &iType, &iHandle, &iRect);
	SetCtlValue((ControlHandle)iHandle, (short)collisionSoundOn);
	
	GetDItem(soundDialog, kSoundCrowd, &iType, &iHandle, &iRect);
	SetCtlValue((ControlHandle)iHandle, (short)crowdSoundOn);
	
	leaveDialog = FALSE;
	
	while (!leaveDialog)
	{
		ModalDialog(SoundFilter, &itemHit);
		
		switch (itemHit)
		{
			case kSoundOkay:
				GetDItem(soundDialog, kSoundEnable, &iType, &iHandle, &iRect);
				soundOn = (Boolean)GetCtlValue((ControlHandle)iHandle);
				
				GetDItem(soundDialog, kSoundBeam, &iType, &iHandle, &iRect);
				beamSoundOn = (Boolean)GetCtlValue((ControlHandle)iHandle);
				
				GetDItem(soundDialog, kSoundIncidental, &iType, &iHandle, &iRect);
				incidentSoundOn = (Boolean)GetCtlValue((ControlHandle)iHandle);
				
				GetDItem(soundDialog, kSoundCollisions, &iType, &iHandle, &iRect);
				collisionSoundOn = (Boolean)GetCtlValue((ControlHandle)iHandle);
				
				GetDItem(soundDialog, kSoundCrowd, &iType, &iHandle, &iRect);
				crowdSoundOn = (Boolean)GetCtlValue((ControlHandle)iHandle);
				
				SetSoundVol(soundVolume);
				
				userOkayed = TRUE;
				leaveDialog = TRUE;
				break;
				
			case kSoundCancel:
				soundVolume = wasVolume;
				userOkayed = FALSE;
				leaveDialog = TRUE;
				break;
			
			case kSoundLouder:
				if (soundVolume < 7)
				{
					soundVolume++;
					UpdateVolumeIcon(soundDialog);
				}
				break;
			
			case kSoundSofter:
				if (soundVolume > 0)
				{
					soundVolume--;
					UpdateVolumeIcon(soundDialog);
				}
				break;
			
			case kSoundEnable:
				GetDItem(soundDialog, kSoundEnable, &iType, &iHandle, &iRect);
				wasState = (Boolean)GetCtlValue((ControlHandle)iHandle);
				wasState = !wasState;
				SetCtlValue((ControlHandle)iHandle, (short)wasState);
				if (wasState)
				{
					GetDItem(soundDialog, kSoundBeam, &iType, &iHandle, &iRect);
					HiliteControl((ControlHandle)iHandle, 0);
					GetDItem(soundDialog, kSoundIncidental, &iType, &iHandle, &iRect);
					HiliteControl((ControlHandle)iHandle, 0);
					GetDItem(soundDialog, kSoundCollisions, &iType, &iHandle, &iRect);
					HiliteControl((ControlHandle)iHandle, 0);
					GetDItem(soundDialog, kSoundCrowd, &iType, &iHandle, &iRect);
					HiliteControl((ControlHandle)iHandle, 0);
				}
				else
				{
					GetDItem(soundDialog, kSoundBeam, &iType, &iHandle, &iRect);
					HiliteControl((ControlHandle)iHandle, 255);
					GetDItem(soundDialog, kSoundIncidental, &iType, &iHandle, &iRect);
					HiliteControl((ControlHandle)iHandle, 255);
					GetDItem(soundDialog, kSoundCollisions, &iType, &iHandle, &iRect);
					HiliteControl((ControlHandle)iHandle, 255);
					GetDItem(soundDialog, kSoundCrowd, &iType, &iHandle, &iRect);
					HiliteControl((ControlHandle)iHandle, 255);
				}
				break;
			
			case kSoundBeam:
			case kSoundIncidental:
			case kSoundCollisions:
			case kSoundCrowd:
				GetDItem(soundDialog, itemHit, &iType, &iHandle, &iRect);
				wasState = (Boolean)GetCtlValue((ControlHandle)iHandle);
				wasState = !wasState;
				SetCtlValue((ControlHandle)iHandle, (short)wasState);
				break;
				
			default:
				break;
		}
	}
	
	DisposDialog(soundDialog);
	return (userOkayed);
}


