//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------
// onWake
// Called by the engine when the options gui is first set.
//---------------------------------------------------------------------------------------------
function OptionsDlg::onWake (%this)
{//echo("OptionsDlg::onWake()");
	//$Game::Resolution = getWords(getRes(), 0, 1);
	$pref::Video::fullScreen = $Game::FullScreen;
	// Get the fullscreen value.
	FullscreenToggle.setValue($pref::Video::fullScreen);		// This sets the "default" preference fullscreen value!
	VerticalSyncToggle.setValue($Game::VerticalSync);			// This sets the "default" vertical sync value!
	// Fill the graphics drop down menu.
	%buffer = getDisplayDeviceList();
	%count = getFieldCount(%buffer);
	GraphicsDriverMenu.clear();
	for (%i = 0; %i < %count; %i++)
	{
		GraphicsDriverMenu.add(getField(%buffer, %i), %i);
    }
	// Grab the current graphics driver selection.
	%selId = GraphicsDriverMenu.findText($pref::Video::displayDevice);
	if (%selId == -1)
	{
		%selId = 0;
    }
	GraphicsDriverMenu.setSelected(%selId);
	GraphicsDriverMenu.onSelect(%selId, "");
	// Set audio.
	MusicAudioVolume.value = $Game::MusicVolume;
	EffectsAudioVolume.value = $Game::SoundVolume;
	// Fill the screenshot drop down menu.
	ScreenshotMenu.clear();
	ScreenshotMenu.add("PNG", 0);
	ScreenshotMenu.add("JPEG", 1);
	ScreenshotMenu.setValue($pref::Video::screenShotFormat);
	// Set up the keybind options.
	initializeKeybindOptions();
}

//---------------------------------------------------------------------------------------------
// GraphicsDriverMenu.onSelect
// Called when a graphics device is selected.
//---------------------------------------------------------------------------------------------
function GraphicsDriverMenu::onSelect (%this, %id, %text)
{//echo("GraphicsDriverMenu::onSelect()");
	// Attempt to keep the same res and bpp settings.
	%prevRes = getWords(getRes(), 0, 1);
	//echo("%prevRes =" SPC %prevRes);
	if (FullscreenToggle.getValue())
	{
		if (BPPMenu.size() > 0)
		{
			%prevBPP = BPPMenu.getText();
		}
		else
		{
			%prevBPP = getWord($pref::Video::windowedRes, 2);
		}
	}
	// Check if this device is full-screen only.
	if (isDeviceFullScreenOnly(%this.getText()))
	{
		FullscreenToggle.setValue(true);
		FullscreenToggle.setActive(false);
		FullscreenToggle.onAction();
	}
	else
	{
		FullscreenToggle.setActive(true);
	}
	// Fill the resolution and bit depth lists.
	ResolutionMenu.init(%this.getText(), FullscreenToggle.getValue());
	BPPMenu.init(%this.getText());
	// Try to select the previous settings, otherwise set the first in the list.
	%selId = ResolutionMenu.findText(%prevRes);
	// Save values for writing to xml config!
	$Game::Resolution = %prevRes;
	$Game::FullScreen = FullscreenToggle.getValue();
	if (%selId == -1)
	{
		%selId = ResolutionMenu.size() - 1;
	}
	ResolutionMenu.setSelected(%selId);
	// Bit depth only applies to full screen mode.
	if (FullscreenToggle.getValue())
	{
		%selId = BPPMenu.findText(%prevBPP);
		if (%selId == -1)
		{
			%selId = 0;
		}
		BPPMenu.setSelected(%selId);
		BPPMenu.setText(BPPMenu.getTextById(%selId));
	}
	else
	{
		//BPPMenu.setText("Default");
		BPPMenu.setText($Game::BitDepth);
	}
}

//---------------------------------------------------------------------------------------------
// ResolutionMenu
// Initialize the resolution menu based on the device and fullscreen settings.
//---------------------------------------------------------------------------------------------
function ResolutionMenu::init (%this, %device, %fullScreen)
{//echo("ResolutionMenu::init()");
	// Save this so applying AV twice doesn't cancel resolution!
	%newRes = ResolutionMenu.getText();
	// Clear out previous values.
	%this.clear();
	
	if (%fullScreen $= "")
	{
		%fullScreen = isFullScreen();
	}
	// Get the list of valid resolutions.
	%resList = getResolutionList(%device);
	%resCount = getFieldCount(%resList);
	%deskRes = getDesktopResolution();
	// Loop through all the valid resolutions.
	%count = 0;
	for (%i = 0; %i < %resCount; %i++)
	{
		// Grab the resolution.
		%res = getWords(getField(%resList, %i), 0, 1);
		//echo("%res =" SPC %res);	
		
		// Enforce only certain resolutions!
		/*if (//%res $= "640 480"	||
			//%res $= "800 600"	||
			//%res $= "1024 640"	||
			//%res $= "1024 768"||
			
			%res $= "800 600"	||
			//%res $= "1024 640"	||
			%res $= "1024 768"||
			%res $= "1280 720"	||
			%res $= "1280 800"	||
			%res $= "1280 960"	||	// Mac.
			%res $= "1280 1024"	||
			//%res $= "1344 840"	||	// Mac.
			//%res $= "1344 1008"	||	// Mac.
			//%res $= "1360 768"	||	// Mac.
			%res $= "1366 768"	||
			%res $= "1440 900"	||
			%res $= "1600 900"	||
			//%res $= "1600 1000"	||	// Mac.
			%res $= "1680 1050"	||
			%res $= "1920 1080"	||
			%res $= "1920 1200")
		{*/
			// Only add to list if it isn't there already.
			if (%this.findText(%res) == -1)
			{
				%this.add(%res, %count);
				%count++;
			}
		//}
	}
	ResolutionMenu.setText(%newRes);
}

//---------------------------------------------------------------------------------------------
// BPPMenu.init
// Initialize the bits per pixel menu.
//---------------------------------------------------------------------------------------------
function BPPMenu::init (%this, %device)
{//echo("BPPMenu::init()");
	// Clear previous values.
	%this.clear();
	if (%device $= "Voodoo2")
	{
		%this.add("16", 0);
	}
	else
	{
		%resList = getResolutionList(%device);
		%resCount = getFieldCount(%resList);
		%count = 0;
		for (%i = 0; %i < %resCount; %i++)
		{
			%bpp = getWord(getField(%resList, %i), 2);
			
			// Only add to list if it isn't there already.
			if (%this.findText(%bpp) == -1)
			{
				%this.add(%bpp, %count);
				%count++;
			}
		}
	}
}

//---------------------------------------------------------------------------------------------
// FullscreenToggle.onAction
// Called when the fullscreen checkbox is toggled.
//---------------------------------------------------------------------------------------------
function FullscreenToggle::onAction (%this)
{//echo("FullscreenToggle::onAction()");
	%prevRes = ResolutionMenu.getText();
	// Update the resolution menu with the new options
	ResolutionMenu.init(GraphicsDriverMenu.getText(), %this.getValue());
	// Set it back to the previous resolution if the new mode supports it.
	%selId = -1;
	if (%this.getValue() == false && $pref::Video::windowedRes !$= "")
	{
		%selId = ResolutionMenu.findText(GetWords($pref::Video::windowedRes, 0, 1));
	}
	else if (%this.getValue() == true && $pref::Video::Resolution !$= "")
	{
		%selId = ResolutionMenu.findText(GetWords($pref::Video::Resolution, 0, 1));
	}
	if (%selId == -1)
	{
		%selId = ResolutionMenu.findText(%prevRes);
	}
	if (%selId != -1)
	{
		ResolutionMenu.setSelected(%selId);
	}
	else
	{
		ResolutionMenu.setSelected(ResolutionMenu.size() - 1);
	}
}

//---------------------------------------------------------------------------------------------
// updateChannelVolume
// Update an audio channels volume.
//---------------------------------------------------------------------------------------------
$AudioTestHandle = 0;
function updateChannelVolume (%channel, %volume)
{//echo("updateChannelVolume()");echo("%channel =" SPC %channel);echo("%volume =" SPC %volume);
	// Only valid channels are 1-8
	if (%channel < 1 || %channel > 8)
	{
		return;
	}
	alxSetChannelVolume(%channel, %volume);
	$pref::Audio::channelVolume[%channel] = %volume;
	
	// Dynamically set background music volume!
	alxSetVolume(%channel, %volume);
	switch (%channel)
	{
		case 1:	// SFX Channel.
			$Game::SoundVolume = %volume;
		case 2:	// Music Channel.
			$Game::MusicVolume = %volume;
	}
	
	// Play a test sound for volume feedback.
	if (!alxIsPlaying($AudioTestHandle))
	{
		alxPlay("Audio:volumeTest");
	}
}
// Just a dummy function to reduce console.log errors for now!
function alxSetVolume (%bgmo, %volume)
{
}
// Just a dummy function to reduce console.log errors for now!
function lockMouse ()
{
}

//---------------------------------------------------------------------------------------------
// applyAVOptions
// Apply the AV changes.
//---------------------------------------------------------------------------------------------
function applyAVOptions ()
{//echo("applyAVOptions()");
	%newDriver = GraphicsDriverMenu.getText();
	%newRes = ResolutionMenu.getText();
	%newBpp = BPPMenu.getText();
	%newFullScreen = FullscreenToggle.getValue();
	%newVerticalSync = VerticalSyncToggle.getValue();
	$pref::Video::screenShotFormat = ScreenshotMenu.getText();
	if (%newFullScreen	&&
		!isFullScreen())
	{
		$Video::WindowedDesktopSize = getDesktopResolution();
	}
	if (%newDriver !$= $pref::Video::displayDevice)
	{
		setDisplayDevice(%newDriver, firstWord(%newRes), getWord(%newRes, 1), %newBpp, %newFullScreen);
	}
	else
	{
		setScreenMode(firstWord(%newRes), getWord(%newRes, 1), %newBpp, %newFullScreen);
	}
	Canvas.popDialog(OptionsDlg);
	Canvas.pushDialog(OptionsDlg);
	FullscreenToggle.setValue(%newFullScreen);
	VerticalSyncToggle.setValue(%newVerticalSync);
	// Dynamically set music volume!
	updateChannelVolume(1, EffectsAudioVolume.value);
	// Dynamically set sound effects volume!
	updateChannelVolume(2, MusicAudioVolume.value);
	// Set new bit depth.
	$Game::BitDepth = %newBpp;
	// Set the new resolution, bit depth, and fullscreen values to save to the preference file.
	$Game::Resolution = %newRes SPC "32";						// Always set to 32 bit depth.
	$Game::FullScreen = %newFullScreen;
	$Game::VerticalSync = %newVerticalSync;
	$pref::Video::fullScreen = $Game::FullScreen;				// The game files use the preference video value, so just copy it.
	setGameResolution($Game::Resolution, $Game::FullScreen);	// New with T2D v3.0!
	setVerticalSync(%newVerticalSync);
	sceneWindow2D.onExtentChange();
	$monsterStepDelay = getScaledMonsterStepDelay();
	updateGuiElementPositioning();
	updateControlPanelTooltips();
	updateMainMenuFrameContainerPosition();
	schedule(50, 0, "updateMainMenuFrameContainerPosition");
	updateScrollingEventLogPosition();
	updateCameraPosition();
	schedule(50, 0, "updateCameraPosition");
	ResolutionMenu.setText(getWords($Game::Resolution, 0, 1));	// To make sure hitting apply AV doesn't bounce back and forth between old and new resolutions!

echo("$Game::VerticalSync =" SPC $Game::VerticalSync);
echo("VerticalSyncToggle.getValue() =" SPC VerticalSyncToggle.getValue());
}

//---------------------------------------------------------------------------------------------
// revertAVOptions
// Revert the AV options to the defaults. Does not apply the changes. Only resets the
// selections.
//---------------------------------------------------------------------------------------------
// Called from the options dialog when the default AV Options button is clicked.
function defaultAVOptions ()
{//echo("defaultAVOptions()");
	%selId = ResolutionMenu.findText("1280 720");
	if (%selId == -1)
	{
		%selId = 0;
	}
	ResolutionMenu.setSelected(%selId);
	BPPMenu.setText($Game::BitDepth);
	$Game::Resolution = "1280 720 32";		// Always set to 32 bit depth.
	$Game::FullScreen = 0;
	$Game::VerticalSync = 1;
	$pref::Video::fullScreen = $Game::FullScreen;		// The game files use the preference video value, so just copy it.
	FullscreenToggle.setValue($pref::Video::fullScreen);
	VerticalSyncToggle.setValue($Game::VerticalSync);
	%selId = GraphicsDriverMenu.findText("OpenGL");
	if (%selId == -1)
	{
		%selId = 0;
	}
	GraphicsDriverMenu.setSelected(%selId);
	GraphicsDriverMenu.onSelect(%selId, "");
	EffectsAudioVolume.setValue(0.8);
	MusicAudioVolume.setValue(0.8);
	updateChannelVolume($effectsAudioType, 0.8);
	updateChannelVolume($musicAudioType, 0.8);
	setGameResolution($Game::Resolution, $Game::FullScreen);	// New with T2D v3.0!
	ScreenshotMenu.clear();
	ScreenshotMenu.setValue("PNG");
	updateGuiElementPositioning();
	updateCameraPosition();
}

function revertAVOptions ()
{//echo("revertAVOptions()");
	//%selId = ResolutionMenu.findText("800 600");	// Why not 1280x720?
	%selId = ResolutionMenu.findText("1280 720");	// Why not 1280x720?
	if (%selId == -1)
	{
		%selId = 0;
	}
	ResolutionMenu.setSelected(%selId);
	FullscreenToggle.setValue(false);
	BPPMenu.setText("Default");
	%selId = GraphicsDriverMenu.findText("OpenGL");
	if (%selId == -1)
	{
		%selId = 0;
	}
	GraphicsDriverMenu.setSelected(%selId);
	GraphicsDriverMenu.onSelect(%selId, "");
	EffectsAudioVolume.setValue(0.8);
	MusicAudioVolume.setValue(0.8);
	updateChannelVolume($effectsAudioType, 0.8);
	updateChannelVolume($musicAudioType, 0.8);
	setGameResolution($Game::Resolution, $Game::FullScreen);	// New with T2D v3.0!
	ScreenshotMenu.clear();
	ScreenshotMenu.setValue("PNG");
	applyAVOptions();
	updateGuiElementPositioning();
	updateCameraPosition();
}

//---------------------------------------------------------------------------------------------
// revertAVOptionChanges
// Revert the AV changes made since the options menu was opened - which happen to be the
// values of the related $prefs.
//---------------------------------------------------------------------------------------------
function revertAVOptionChanges ()
{//echo("revertAVOptionChanges()");
	$pref::Video::fullScreen = $Game::FullScreen;		// The game files use the preference vide value, so just copy it.
	FullscreenToggle.setValue($pref::Video::fullScreen);
	if (FullscreenToggle.getValue())
	{
		%selId = ResolutionMenu.findText(getWords($pref::Video::resolution, 0, 1));
	}
	else
	{
		%selId = ResolutionMenu.findText(getWords($pref::Video::windowedRes, 0, 1));
	}
	if (%selId == -1)
	{
		%selId = 0;
	}
	ResolutionMenu.setSelected(%selId);
	// Bit depth only applies to full screen mode.
	if (FullscreenToggle.getValue())
	{
		%selId = BPPMenu.findText(getWord($pref::Video::resolution, 2));
		if (%selId == -1)
		{
			%selId = 0;
		}
		BPPMenu.setSelected(%selId);
		BPPMenu.setText(BPPMenu.getTextById(%selId));
	}
	else
	{
		BPPMenu.setText("Default");
	}
	%selId = GraphicsDriverMenu.findText($pref::Video::displayDevice);
	if (%selId == -1)
	{
		%selId = 0;
	}
	GraphicsDriverMenu.setSelected(%selId);
	GraphicsDriverMenu.onSelect(%selId, "");
	ScreenshotMenu.setValue($pref::video::screenshotFormat);
	setGameResolution($Game::Resolution, $Game::FullScreen);	// New with T2D v3.0!
	updateGuiElementPositioning();
	updateCameraPosition();
}