// Handles Screen Resolutions.
// Worthless Bums 2012.


function adjustCameraSize ()//(%width, %height)
{//echo("adjustCameraSize()");
//echo("levelscenegraph.cameraSize =" SPC levelscenegraph.cameraSize);
	// Make sure sceneWindow2D exists!
	if (!isObject(sceneWindow2D)	||
		!isObject(levelSceneGraph))
	{
		return;
	}
	// Dismount window.
	sceneWindow2D.dismount();
	%currentWidth = getWord($Game::Resolution, 0);
	%currentHeight = getWord($Game::Resolution, 1);
	// Calculate size to fit a 4:3 aspect ratio - width fit first!
	%sizeX = %currentWidth;
	%sizeY = %sizeX * 3.0 / 4.0;
	// Calculate screen area ratio.
	%currentArea = %currentWidth * %currentHeight;
	%newArea = %sizeX * %sizeY;
	%areaRatio = %newArea / %currentArea;
	// Apply area ratio to new window size.
	%sizeX = %sizeX * %areaRatio;
	%sizeY = %sizeY * %areaRatio;
//echo("%sizeX =" SPC %sizeX);
//echo("%sizeY =" SPC %sizeY);
	// Set new sizes.
	levelSceneGraph.cameraSize = %sizeX SPC %sizeY;
	sceneWindow2D.setExtent(%sizeX, %sizeY);
	if ($sceneWindowMountParent != $playerSquad[$activeCharacter])
	{
		sceneWindow2D.mount($playerSquad[$activeCharacter], $sceneWindow2DxOffset SPC $sceneWindow2DyOffset, $cameraMountForce, false);
		$sceneWindowMountParent = $playerSquad[$activeCharacter];
	}
}
//---------------------------------------------------------------------------------------------------------
// Custom change code to respect aspect ratio of the user!
$qInExtentChange = false;
function sceneWindow2D::onExtentChange (%this)//, %data)
{//echo("sceneWindow2D::onExtentChange()");//return;
return;	// Why is this being called so frequently?
	// NOTE: Really this should read the screen resolution and determine widescreen status!
	if ($qInExtentChange)
	{
		return;
	}
	$qInExtentChange = true;
	setAcceptableScreenRatio();  
	$qInExtentChange = false;
}
function setAcceptableScreenRatio ()
{//echo("setAcceptableScreenRatio()");
	%screenWidth = getWord(Canvas.extent, 0);
	%screenHeight = getWord(Canvas.extent, 1);
	
	%screenRatio = %screenWidth / %screenHeight;
	%optimalRatio = 800.0 / 600.0;//1600 / 1200;  // Steam Marines's design resolution is 800x600.
	
	%newWidth = %screenWidth;
	%newHeight = %screenHeight;
	
	if (%screenRatio < %optimalRatio)
	{
		%newHeight = %screenWidth / %optimalRatio;
	}
	else if (%screenRatio > %optimalRatio)
	{
		%newWidth = %screenHeight * %optimalRatio;
	}
		
	%newX = (%screenWidth - %newWidth) / 2.0;
	%newY = (%screenHeight - %newHeight) / 2.0;
	
	// NOTE: We should perform more widescreen calculations here!
	sceneWindow2D.setPosition(%newX, %newY);
	sceneWindow2D.setExtent(%newWidth, %newHeight);
	
	// Set screen gui sizes.
	setAppropriateScreenSize();
	
	// Set camera zoom level.
	setAppropriateZoomLevel();
}
function setAppropriateZoomLevel ()
{//echo("setAppropriateZoomLevel()");
	// Zoom handling.
	// 800x600 is the design resolution, so take the ratio of that and zoom the camera appropriately.
	/*%zoomRatio = 800 / getWord($Game::Resolution, 0);
	%zoomLevel = 1.0 * %zoomRatio;
	zoomCameraFast(%zoomLevel);*/
	
	// Zoom handling.
	// 1280x720 is the design resolution, so take the ratio of that and zoom the camera appropriately.
	%zoomRatio = 1280 / getWord($Game::Resolution, 0);
	%zoomLevel = 1.0 * %zoomRatio;
	zoomCameraFast(%zoomLevel);
}

function getAppropriateZoomLevel ()
{//echo("getAppropriateZoomLevel()");
	// 1280x720 is the design resolution, so take the ratio of that and zoom the camera appropriately.
	%zoomRatio = 1280 / getWord($Game::Resolution, 0);
	return %zoomRatio;
}
function setZoom1x ()
{//echo("setZoom1x()");
	%xSize = getWord($Game::Resolution, 0) * 75 / getWord($Game::Resolution, 1);
	sceneWindow2D.setCameraSize(%xSize, 75);
	//sceneWindow2D.setCameraZoom(0.82);
	sceneWindow2D.setCameraZoom(1);
}
function setZoom2x ()
{//echo("setZoom2x()");
	%xSize = getWord($Game::Resolution, 0) * 75 / getWord($Game::Resolution, 1);
	sceneWindow2D.setCameraSize(%xSize, 75);
	//sceneWindow2D.setCameraZoom(1.6);		// Works for 1280x720!
	sceneWindow2D.setCameraZoom(1.6);
}
function setZoom3x ()
{//echo("setZoom3x()");
	%xSize = getWord($Game::Resolution, 0) * 75 / getWord($Game::Resolution, 1);
	sceneWindow2D.setCameraSize(%xSize, 75);
	//sceneWindow2D.setCameraZoom(2.24 * getAppropriateZoomLevel());
	sceneWindow2D.setCameraZoom(3);
}
function setSceneExtent (%width, %height)
{//echo("setSceneExtent()");
	sceneWindow2D.setExtent(%width, %height);
}
function isWidescreen ()
{//echo("isWidescreen()");
	// Detect widescreen resolutions!
	if (getWord($Game::Resolution, 1) / getWord($Game::Resolution, 0) >= 0.75)
	{
		return false;
	}
	else
	{
		return true;
	}
}
// Handle mainScreen.gui.
function setAppropriateScreenSize ()
{//echo("setAppropriateScreenSize()");
	// Get game resolution.
	%screenWidth	= getWord($Game::Resolution, 0);
	%screenHeight	= getWord($Game::Resolution, 1);
	
	// Truncate width to assume proper 4:3 aspect ratio.
	//%newWidth = %screenHeight * 4.0 / 3.0;
	//%newHeight = %screenHeight;
	
	// Detect non-4:3 aspect ratio resolutions!
	if (!isWidescreen())
	{
		%newWidth = %screenWidth;
		%newHeight = %screenHeight;
	}
	else
	{
		// Expand past window borders!
		// NOTE: Need to deal with GUI positioning with new resolution/sceneWindow2D mismatching on widescreen resolutions!
		// NOTE: Need to deal with camera mount offsets!
		%newWidth = %screenWidth;
		%newHeight = %newWidth * 3.0 / 4.0;
	}
	
	// Set gui sizes.
	mainScreenGui.extent = $Game::Resolution;
	sceneWindow2D.extent = %newWidth SPC %newHeight;
	
	// Set zoom level.
	switch ($scaleLevel)
	{
		case 1:	setZoom1x();
		case 2:	setZoom2x();
		case 3:	setZoom3x();
	}
	
	// Set window offsets.
	$sceneWindow2DxOffset = 0;
	$sceneWindow2DyOffset = 0;
}


//---------------------------------------------------------------------------------------------------------


// Set current widescreen.
function setCurrentWideScreen ()
{//echo("setCurrentWideScreen()");
	%info = getDesktopResolution();
	%width = getWord(%info, 0);
	%height = getWord(%info, 1);
	%screenRatio = %width / %height;
	//echo("%screenRatio =" SPC %screenRatio);
	if (%screenRatio > 1.34)		// 800x600 is 1.3333333...
	{
		$wideScreenIsOn = true;
	}
	else
	{
		$wideScreenIsOn = false;
	}
}

// 
function doMyToggleFullscreen ()
{//echo("doMyToggleFullscreen()");
	%info = getDesktopResolution();
	%width = getWord(%info, 0);
	%height = getWord(%info, 1);
	%depth = getWord(%info, 2);
	if (isFullScreen())
	{
		setScreenMode(800, 600, 32, false);
		resetWidescreen();
	}
	else
	{
		setScreenMode(%width, %height, 32, true);
		handleWidescreenChange();
	}
	
	
	// OTHER COMMANDS:
	// setRes(width, height, bitDepth);
	// toggleFullScreen();	// Keeps bit depth the same, so it can look blocky full-screen if you're not careful
	// isFullScreen();	// Is it currently fullscreen?
	// getResolutionList();
	// getDesktopResolution();	// returns width, height, and bitdepth
}

// Call this when returning to original window size.
function resetWidescreen ()
{//echo("resetWidescreen()");
	// Get the resolution of the window
	%info = getRes();
	%width = getWord(%info, 0);
	%height = getWord(%info, 1);
	
	// Set everything to be that same resolution
	mainScreenGui.setExtent(%width, %height);
	sceneWindow2D.setExtent(%width, %height);
	sceneWindow2D.setPosition(0, 0);
}

// NOTE: See "MainScreen.gui" in the "game/gui" folder for names of GUI elements
// that we access directly here. ("sceneWindow2D" and "mainScreenGui")
// I can add more GUI elements or rename them in that file.
function handleWidescreenChange ()
{//echo("handleWidescreenChange()");
	// Get the current resolution of our game *window* (not of the desktop)
	// NOTE: it's possible to be full-screen, and still a much lower resolution that the desktop res
	%info = getRes();
	%width = getWord(%info, 0);
	%height = getWord(%info, 1);
	
	// setExtent = set the size of the control
	mainScreenGui.setExtent(%width, %height);
	
	%info = sceneWindow2D.getExtent();
	%windowWidth = getWord(%info, 0);
	%windowHeight = getWord(%info, 1); 
	
	%ratio = 800 / 600;
	%newWidth = mCeil( %height * %ratio );
	
	// Is the window the correct size for the game anyway? Then don't allow any widescreen changes.
	%canHaveBlackBorders = (%newWidth != %windowWidth);
	
	// Were we already widescreen? Then switch to black borders on the sides.
	if ($wideScreenIsOn == false && %canHaveBlackBorders)
	{//echo("switching to blackborder widescreen!");
		mainScreenGui.setExtent(%newWidth, %height);
		// sceneWindow2D is a GUI control that is put INSIDE of the mainScreenGui control
		sceneWindow2D.setExtent(%newWidth, %height);
		sceneWindow2D.setPosition(0, 0);
	}
	// This just seems to stretch the screen!
	// Maybe just extend the window height to remain 4:3 ratio and render stuff off screen?
	else
	{//echo("switching to widescreen!");
		mainScreenGui.setExtent(%width, %height);
		sceneWindow2D.setExtent(%width, %height);
		sceneWindow2D.setPosition(0, 0);
	}
}
function toggleWidescreen ()
{//echo("toggleWidescreen()");
	$wideScreenIsOn = !$wideScreenIsOn;
	handleWidescreenChange();
}