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

//---------------------------------------------------------------------------------------------
// initializeCanvas
// Constructs and initializes the default canvas window.
//---------------------------------------------------------------------------------------------
$canvasCreated = false;

function getFullscreenResolution()
{
   return getDesktopResolution();
   //%deskRes = getDesktopResolution(); 
   //%deskResX   = getWord(%deskRes, $WORD::RES_X);
   //%deskResY   = getWord(%deskRes, $WORD::RES_Y);
//
   //if (%deskResY >= 1152)
   //{
      ////%aspectRatio = %deskResX / %deskResY;
      ////if (%aspectRatio > 1.5)
         ////return 1280 SPC 720 SPC getWords(%deskRes, 2);
      ////else
      //return 1024 SPC 768 SPC getWords(%deskRes, 2);
   //}
   //
   //return %deskRes;
}

function configureCanvas()
{
   if ($pref::Video::ForcedResolution !$= "")
      %deskRes = $pref::Video::ForcedResolution;
   else
      %deskRes = getFullscreenResolution(); 
           
   %deskResX   = getWord(%deskRes, $WORD::RES_X);
   %deskResY   = getWord(%deskRes, $WORD::RES_Y);
   %deskResBPP = getWord(%deskRes, 2);
   
   %aspectRatio = %deskResX / %deskResY;
   
   
   // Build our $pref::Video::mode if needed
//   if ($pref::Video::mode $= "")
//   {
      // First some defaults
   if ($pref::Video::ForcedResolution !$= "")
      %res = $pref::Video::ForcedResolution;
   else
      %res = "800 600"; //"1024 768";
      
      %fs = true;
      %bpp = 32;
      %rate = 60;
      %fsaa = 0;

    
      if ($pref::Video::fullScreen !$= "")
         %fs = $pref::Video::fullScreen;      
         
      if (%fs $= "0" || %fs $= "false")
      {
         if ($pref::Video::windowedRes !$= "")
            %res = $pref::Video::windowedRes;
      }
      else
      {   
         if(%aspectRatio > 4/3)
         {
            %res = %deskResX SPC %deskResY;
//            if ($pref::Video::ResolutionW !$= "")
//               %res = $pref::Video::ResolutionW;         
         }
         else
         {
            if ($pref::Video::Resolution !$= "")
               %res = $pref::Video::Resolution;
         }
      }
      if ($pref::Video::BitsPerPixel !$= "")
         %bpp = $pref::Video::BitsPerPixel;
      if ($pref::Video::RefreshRate !$= "")
         %rate = $pref::Video::RefreshRate;
      if ($pref::Video::FSAALevel !$= "")
         %fsaa = $pref::Video::FSAALevel;
      
      $pref::Video::mode = %res SPC %fs SPC %bpp SPC %rate SPC %fsaa;
//   }

   %resX = getWord($pref::Video::mode, $WORD::RES_X);
   %resY = getWord($pref::Video::mode, $WORD::RES_Y);
   %fs = getWord($pref::Video::mode,   $WORD::FULLSCREEN);
   %bpp = getWord($pref::Video::mode,  $WORD::BITDEPTH);
   %rate = getWord($pref::Video::mode, $WORD::REFRESH);
   %fsaa = getWord($pref::Video::mode, $WORD::AA);
   
   echo("--------------");
   echo("Attempting to set resolution to \"" @ $pref::Video::mode @ "\"");
      
   // We shouldn't be getting this any more but just in case...
   if (%bpp $= "Default")
      %bpp = %deskResBPP;
      
   // Make sure we are running at a valid resolution
   if (%fs $= "0" || %fs $= "false")
   {
      // Windowed mode has to use the same bit depth as the desktop
      %bpp = %deskResBPP;
      
      // Windowed mode also has to run at a smaller resolution than the desktop
      if ((%resX >= %deskResX) || (%resY >= %deskResY))
      {
         warn("Warning: The requested windowed resolution is equal to or larger than the current desktop resolution. Attempting to find a better resolution");
      
         %resCount = Canvas.getModeCount();
         for (%i = (%resCount - 1); %i >= 0; %i--)
         {
            %testRes = Canvas.getMode(%i);
            %testResX = getWord(%testRes, $WORD::RES_X);
            %testResY = getWord(%testRes, $WORD::RES_Y);
            %testBPP  = getWord(%testRes, $WORD::BITDEPTH);

            if (%testBPP != %bpp)
               continue;
            
            if ((%testResX < %deskResX) && (%testResY < %deskResY))
            {
               // This will work as our new resolution
               %resX = %testResX;
               %resY = %testResY;
               
               warn("Warning: Switching to \"" @ %resX SPC %resY SPC %bpp @ "\"");
               
               break;
            }
         }
      }
   }
      
   $pref::Video::mode = %resX SPC %resY SPC %fs SPC %bpp SPC %rate SPC %fsaa;
   
   if (%fs == 1 || %fs $= "true")
      %fsLabel = "Yes";
   else
      %fsLabel = "No";

   echo("Accepted Mode: " NL
      "--Resolution : " @  %resX SPC %resY NL 
      "--Full Screen : " @ %fsLabel NL
      "--Bits Per Pixel : " @ %bpp NL
      "--Refresh Rate : " @ %rate NL
      "--FSAA Level " @ %fsaa NL
      "--------------");

   // Actually set the new video mode
   Canvas.setVideoMode(%resX, %resY, %fs, %bpp, %rate, %fsaa);
}

function initializeCanvas(%windowName)
{
   // Don't duplicate the canvas.
   if($canvasCreated)
   {
      error("Cannot instantiate more than one canvas!");
      return;
   }

   if (!createCanvas(%windowName))
   {
      error("Canvas creation failed. Shutting down.");
      quit();
   }

   $canvasCreated = true;
}

//---------------------------------------------------------------------------------------------
// resetCanvas
// Forces the canvas to redraw itself.
//---------------------------------------------------------------------------------------------
function resetCanvas()
{
   if (isObject(Canvas))
      Canvas.repaint();
}

//---------------------------------------------------------------------------------------------
// Callbacks for window events.
//---------------------------------------------------------------------------------------------

function GuiCanvas::onLoseFocus(%this)
{
}

function GuiCanvas::onGainFocus(%this)
{
}

function GuiCanvas::onResize(%this, %width, %height)
{
   // Reset animation when the canvas is resized
//   StopMenuShrinkGrowMainMenu();
//   StopMenuShrinkGrowNewPlayerMenu();
//   StopMenuShrinkGrowSceneMenu();
//   StopMenuShrinkGrowTakeMenu();
//   StopMenuShrinkGrowInstructionsMenu();
//   StopMenuShrinkGrowCreditsMenu();
//   StopMenuShrinkGrowOptionsMenu();
   
//   %width = getWord($pref::Video::mode, 0);

   if($pref::Video::fullScreen $= "true" || $pref::Video::fullScreen $= 1)
   {
      %width = getWord($pref::Video::mode, $WORD::RES_X);
      %height = getWord($pref::Video::mode, $WORD::RES_Y);
   }
                      
   Canvas.scaleSize = %width / 800;
   
//   echo("");
//   echo("Window resize: " @ %width SPC %height);
//   echo("Canvas scale: " @ Canvas.scaleSize);
//   echo("");

   // Loading scene or world map
   if(isObject(LoadingGui))
      LoadingGui.onResize(%width, %height);
      
   if(isObject(LoadingWorldMapGui))
      LoadingWorldMapGui.onResize(%width, %height);   
   
   // Update subtitle box
   if(isObject(SubtitleBox))
      SubtitleBox.onResize(%width, %height);      
      
   // Update score box and HO speech bubbles
   if(isObject(SceneGui))
      SceneGui.onResize(%width, %height);
   
   if(isObject(IntroGui))
      IntroGui.onResize(%width, %height);
   
   if(isObject(OutroGui))
      OutroGui.onResize(%width, %height);
      
   if(isObject(HintSystem))
      HintSystem.onResize(%width, %height);
 
   if(isObject(ShoppingList))
      ShoppingList.onResize(%width, %height);
      
   if(isObject(Minigame))
      Minigame.onResize(%width, %height);
   
   // Update menus      
   if(isObject(CreditsMenuDlg))
      CreditsMenuDlg.onResize(%width, %height);
      
   if(isObject(InstructionsMenuDlg))
      InstructionsMenuDlg.onResize(%width, %height);
      
   if(isObject(NewPlayerMenuDlg))
      NewPlayerMenuDlg.onResize(%width, %height);
      
   if(isObject(OptionsMenuDlg))
      OptionsMenuDlg.onResize(%width, %height);
      
   if(isObject(OptionsHODlg))
      OptionsHODlg.onResize(%width, %height);
      
   if(isObject(SceneMenuDlg))
      SceneMenuDlg.onResize(%width, %height);
      
   if(isObject(SelectPlayerMenuDlg))
      SelectPlayerMenuDlg.onResize(%width, %height);
      
   if(isObject(PlayMinigameDlg))
      PlayMinigameDlg.onResize(%width, %height);
      
   if(isObject(MainMenuGui))
      MainMenuGui.onResize(%width, %height);
      
   if(isObject(WorldMapGui))
      WorldMapGui.onResize(%width, %height);
      
   if(isObject(AwardRoom))
      AwardRoom.onResize(%width, %height);
            
   // Minigames
   if(isObject(BejeweledMinigame))
      BejeweledMinigame.onResize(%width, %height);
      
   if(isObject(MazeMinigame))
      MazeMinigame.onResize(%width, %height);

   if(isObject(PicResortMinigame))
      PicResortMinigame.onResize(%width, %height);

   if(isObject(PipeMinigame))
      PipeMinigame.onResize(%width, %height);

   if(isObject(SimonMinigame))
      SimonMinigame.onResize(%width, %height);
   
   if(isObject(SliderMinigame))
      SliderMinigame.onResize(%width, %height);
   
   if(isObject(InsectMinigame))
      InsectMinigame.onResize(%width, %height);
      
   if(isObject(PunchMinigame))
      PunchMinigame.onResize(%width, %height);
}

function Canvas::setWidescreen(%this, %obj, %scheduleTime, %hide)
{
   if(%hide $= "true" || %hide $= 1)
      %obj.setVisible(false);
   %this.schedule(%scheduleTime, "setWidescreenHelper", %obj);
}

function Canvas::setWidescreenHelper(%this, %obj)
{
   if($pref::Video::fullScreen $= "true" || $pref::Video::fullScreen $= 1)
   {
      %width = getWord($pref::Video::mode, $WORD::RES_X);
      %height = getWord($pref::Video::mode, $WORD::RES_Y);
   }
   else
   {   
      %width = getWord(Canvas.getExtent(),0);
      %height = getWord(Canvas.getExtent(),1);
   }
   
   // Fix resolution for widescreen
   if(%width > %height * $GameAspectRatio)
   {
      %newWidth  = %height * $GameAspectRatio;
      %newHeight = %height;
   }
   else
   {
      %newWidth  = %width;
      %newHeight = %width / $GameAspectRatio;
   }
   
//   echo("Setting extent to" SPC %newWidth SPC %newHeight);   
   
   %posX = (%width - %newWidth)/2;
   %posY = (%height - %newHeight)/2;
   %extentX = %newWidth;
   %extentY = %newHeight;
   
   %obj.setPosition(%posX, %posY);   
   %obj.setExtent(%extentX, %extentY);
//   echo("New pos (" @ %obj.getPosition() @ ") and extent (" @ %obj.getExtent() @ ")");   

   %obj.setVisible(true);
}
