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

$LevelBuilder::Version = "v1.1.1";

function initializeLevelEditor()
{   
   echo( "\n--------- Initializing Level Builder ---------" );
   
   // Load Preferences.
   exec("./preferences/defaultPrefs.ed.cs");
   exec("./preferences/prefs.cs");

   // Create Form Content Library.
   GuiFormManager::RegisterLibrary("LevelBuilder", "~/levelEditor/data/layouts/" );
   GuiFormManager::RegisterLibrary("LevelBuilderToolProperties", "~/levelEditor/data/layouts/" );
   GuiFormManager::RegisterLibrary("LevelBuilderSidebar", "~/levelEditor/data/layouts/sidebar/" );
   GuiFormManager::RegisterLibrary("LevelBuilderSidebarEdit", "~/levelEditor/data/layouts/sidebar/edit/" );
   GuiFormManager::RegisterLibrary("LevelBuilderSidebarCreate", "~/levelEditor/data/layouts/sidebar/create/" );
   GuiFormManager::RegisterLibrary("LevelBuilderQuickEditClasses" );

   // Load Up Available Layouts.
   GuiFormManager::InitLayouts( "LevelBuilder" );
   
   // Load scripts.
   exec("./gui/profiles.ed.cs");

   // Form Contents.
   %formFileSpec = expandFilename( "./scripts/" ) @ "*.cs" SPC expandFilename( "./scripts/" ) @ "*.edso";
   for (%file = findFirstFileMultiExpr(%formFileSpec); %file !$= ""; %file = findNextfileMultiExpr(%formFileSpec))
   {      
      if( !isFile( %file ) )
         continue;
         
      %dsoCheck = strstr( %file, ".edso" );
      if( %dsoCheck != -1 )
      {
         %csOriginal = strreplace( %file, ".edso", "" );
         if( !isFile( %csOriginal ) )
            exec( %csOriginal );
      }
      else
         exec( %file );
   }
   
   // Load guis.
   exec("./gui/LevelBuilderBase.ed.gui");
   exec("./gui/LBPlayLevel.ed.gui");
   exec("./gui/options.ed.gui");
   exec("./gui/TGBInsider.ed.gui");
   exec("./gui/TGBInsider.ed.cs");
     
   OptionsTabBook.add(LevelEditorOptionsPage);
   OptionsTabBook.selectPage(0);
   
   // Build LevelBuilder Menu
   LevelBuilderMenu.initialize();

   // Create Tool Manager.
   if (!isObject(ToolManager))
   {
      new LevelBuilderSceneEdit( ToolManager );
      LevelBuilderToolManager::initializeTools( ToolManager );
   }
 
   applyOptions();

   // Activate Default Layout
      if( GuiFormManager::FindLayout( "LevelBuilder", "User" ) )
         GuiFormManager::ActivateLayout( "LevelBuilder", "User", levelBuilderViewContainer );
      else
   GuiFormManager::ActivateLayout( "LevelBuilder", "Default", levelBuilderViewContainer );
   
   // Set Creator Panel Content
   %contentObj = GuiFormManager::FindFormContent( "LevelBuilder", "SideBar" );
   if( %contentObj == 0 )
      GuiFormClass::ClearControlContent( sideBarContentContainer, "LevelBuilder" );
   else
      GuiFormClass::SetControlContent( sideBarContentContainer, "LevelBuilder", %contentObj );
      
   // Level editor hotkey.
   //GlobalActionMap.bindCmd(keyboard, "f9", "toggleLevelEditor();", "");
   
   // Create the default config datablock. This is used to mask
   // the saving of unchanged scene object fields.
   setDefaultSceneObjectDatablock(new t2dSceneObjectDatablock());
       
}

function destroyLevelEditor()
{
   if (isObject(ToolManager.getLastWindow()))
      ToolManager.getLastWindow().endLevel();
   
   // Export Preferences.
   echo("Exporting Level Editor preferences.");
   export("$levelEditor::*", "./preferences/prefs.cs", false);
   setCanvasTitle("TGB");
   
   // Destroy RSS News Updater
   RSSUpdate::destroy();
}

function LevelBuilderMenu::initialize( %this )
{
   Parent::initialize( %this );
   
   //-----------------------------------------------------------------------------
   // File Menu
   //-----------------------------------------------------------------------------
   %this.addMenu("File");
   %this.addMenuItem("File", "New Level...", "newLevel();");
   %this.addMenuItem("File", "Open Level...", "getLevelOpenName(loadLevelCallback);", "Ctrl O");
   %this.addMenuItem("File", "-");
   %this.addMenuItem("File", "New Project...", "newProject();");
   %this.addMenuItem("File", "Open Project...", "openProject();");
   %this.addMenuItem("File", "Build Project...", "togglePackagingUtility();");
   %this.addMenuItem("File", "-");
   %this.addMenuItem("File", "Save", "saveLevel();", "Ctrl S");
   %this.addMenuItem("File", "Save As...", "saveLevelAs();");
   %this.addMenuItem("File", "Save Selected Group...", "saveSelectedGroup();");
   %this.addMenuItem("File", "Save Selected Objects...", "saveSelectedObjects();");
   %this.addMenuItem("File", "-");
   %this.addMenuItem("File", "Import...", "getLevelOpenName(addToLevelCallback);");
   %this.addMenuItem("File", "-");
   %this.addMenuItem("File", "Quit", "quit();");

   //-----------------------------------------------------------------------------
   // Edit Menu
   //-----------------------------------------------------------------------------
   %this.addMenu("Edit", %menuCount);
   %this.addMenuItem("Edit", "Undo", "levelBuilderUndo(1);");
   %this.addMenuItem("Edit", "Redo", "levelBuilderRedo(1);");
   %this.addMenuItem("Edit", "-");
   %this.addMenuItem("Edit", "Cut", "levelBuilderCut(1);");
   %this.addMenuItem("Edit", "Copy", "levelBuilderCopy(1);");
   %this.addMenuItem("Edit", "Paste", "levelBuilderPaste(1);");
   %this.addMenuItem("Edit", "-");
   %this.addMenuItem("Edit", "Group Objects", "ToolManager.groupObjects();");
   %this.addMenuItem("Edit", "Ungroup Objects", "ToolManager.breakApart();");
   %this.addMenuItem("Edit", "-");
   %this.addMenuItem("Edit", "Send to back", "sendToBack(true);", "Ctrl B");
   %this.addMenuItem("Edit", "Bring to front", "bringToFront(true);", "Ctrl F");
   %this.addMenuItem("Edit", "-");
   %this.addMenuItem("Edit", "Preferences...", "Canvas.pushDialog(optionsDlg);");

   //-----------------------------------------------------------------------------
   // Project Menu
   //-----------------------------------------------------------------------------
   %this.addMenu("Project", %menuCount);
   %this.addMenuItem("Project", "Run Game", "runGame();");
   %this.addMenuItem("Project", "-");
   %this.addMenuItem("Project", "GUI Builder...", "GuiEdit();");
   %this.addMenuItem("Project", "-");
   %this.addMenuItem("Project", "Image Map Builder...", "launchNewImageMap();");
   %this.addMenuItem("Project", "Animation Builder...", "launchAnimationEditor();");
   %this.addMenuItem("Project", "-");
   %this.addMenuItem("Project", "Refresh Object Library", "GuiFormManager::SendContentMessage($LBCreateSiderBar, %this, \"refreshall false\");");
   %this.addMenuItem("Project", "Reload Textures", "reloadImageMaps();");
   %this.addMenuItem("Project", "-");
   %this.addMenuItem("Project", "Resources...", "Canvas.pushDialog(projectOptionsDlg);", "Ctrl R");

   //-----------------------------------------------------------------------------
   // View Menu
   //-----------------------------------------------------------------------------
   %this.addMenu("View", %menuCount);
   %this.addMenuItem("View", "Home", "levelBuilderHomeView(1);");
   %this.addMenuItem("View", "Show All", "levelBuilderZoomToFit(1);");
   %this.addMenuItem("View", "Show Selected", "levelBuilderZoomToSelected(1);");
   %this.addMenuItem("View", "-");
   %this.addMenuItem("View", "Zoom 25%", "levelBuilderZoomView(0.25);");
   %this.addMenuItem("View", "Zoom 50%", "levelBuilderZoomView(0.5);");
   %this.addMenuItem("View", "Zoom 100%", "levelBuilderZoomView(1.0);");
   %this.addMenuItem("View", "Zoom 200%", "levelBuilderZoomView(2.0);");
   %this.addMenuItem("View", "Zoom 400%", "levelBuilderZoomView(4.0);");

   //-----------------------------------------------------------------------------
   // Help Menu
   //-----------------------------------------------------------------------------
   %this.addMenu("Help", %menuCount);
   %this.addMenuItem("Help", "Documentation", "gotowebpage(\"tools/help.html\");");
   %this.addMenuItem("Help", "TGB Reference", "gotowebpage(\"tools/reference.html\");");
   %this.addMenuItem("Help", "-");
   %this.addMenuItem("Help", "Keyboard Shortcuts...", "Canvas.pushDialog(optionsdlg);OptionsTabBook.selectPage(1);");
   %this.addMenuItem("Help", "-");
   %this.addMenuItem("Help", "Sign-up for TGB Announcements", "gotoWebpage(\"http://www.garagegames.com/developer/torque/t2d/\");");
   %this.addMenuItem("Help", "-");
   %this.addMenuItem("Help", "GarageGames Community Page", "gotoWebpage(\"http://www.garagegames.com/mg/whatsnew/\");");
   %this.addMenuItem("Help", "TGB Developer Forums", "gotoWebpage(\"http://www.garagegames.com/mg/forums/result.area.php?qa=42\");");
   %this.addMenuItem("Help", "GarageGames News", "RSSUpdate::showNews();");
   %this.addMenuItem("Help", "-");
   %this.addMenuItem("Help", "About...", "TGBInsiderDlg.showAbout();");
}

function GuiMenuBar::onMenuItemSelect(%this, %menuId, %menu, %itemId, %item)
{
   if(%this.scriptCommand[%menu, %itemId] !$= "")
      eval(%this.scriptCommand[%menu, %itemId]);
}

function runSpaceScroller()
{
   LBProjectObj.open( "spacescroller" );
}

function runFishDemo()
{
   LBProjectObj.open( "fishdemo" ); 
}

function runCheckersDemo()
{
   LBProjectObj.open( "checkersdemo" ); 
}

// --------------------------------------------------------------------
// Show Level Editor.
// --------------------------------------------------------------------

$LevelEditorWindow::lockMask = -1;
$LevelEditorWindow::hideMask = -1;
function showLevelEditor()
{
   %load = true;
   if( $runningGame )
   {
      %load = false;   
      setRes( $LevelBuilder::editorResolutionX, $LevelBuilder::editorResolutionY );
   }
   
   endRunGame();
   
   $pref::startupProject = $currentProject;
   
   Canvas.setContent(LevelBuilderBase);
      
   applyOptions();
   levelEditorMap.push();
   
   // Set Level-Editor State.
   $LevelEditorActive = true;
   
   // Make sure the last level is both an existing file and in the current project.
   if (isFile($levelEditor::LastLevel[$currentProject]))
   {
      setCanvasTitle( $currentProject @ " - " @ fileName( $levelEditor::LastLevel[ $currentProject ] ) );
      if( %load )
      {
         if (!loadLevelCallback($levelEditor::LastLevel[$currentProject]))
            newLevel();
      }
   }
   else
      newLevel();
   
   //ToolManager.clearAcquisition();
   //GuiFormManager::SendContentMessage( $LBQuickEdit, %this, "inspect" );
   
   LevelBuilderToolManager::setTool(LevelEditorSelectionTool);
   
   if( !LBProjectObj.isActive() )
      LBProjectObj.open( $currentProject );
   
   %sceneWindow = ToolManager.getLastWindow();
   %scenegraph = %sceneWindow.getSceneGraph();
   %cameraPosition = "0 0";
   %cameraSize = $levelEditor::DefaultCameraSize;
   if (%scenegraph.cameraPosition)
      %cameraPosition = %scenegraph.cameraPosition;
   if (%scenegraph.cameraSize)
      %cameraSize = %scenegraph.cameraSize;
      
   %sceneWindow.setCurrentCameraPosition(%cameraPosition, %cameraSize);
   %sceneWindow.setCurrentCameraZoom(1.0);
   
   // Check for RSS News updates.
   if( $levelEditor::checkRSSFeeds $= true )
      RSSUpdate::initialize( "RSSUpdate::onNewNewsItems" );
   else if( $levelEditor::checkRSSFeeds $= "" )
      TGBInsiderDlg.showFirstRun();
   
   %sceneWindow.setLayerMask( $LevelEditorWindow::lockMask );
   %sceneWindow.setRenderMasks( $LevelEditorWindow::hideMask );
   
   GuiFormManager::SendContentMessage( $LBQuickEdit, %this, "inspectUpdate" );
}

// --------------------------------------------------------------------
// Hide Level Editor.
// --------------------------------------------------------------------
function hideLevelEditor()
{
   %window = ToolManager.getLastWindow();
   $LevelEditorWindow::lockMask = %window.getLayerMask();
   $LevelEditorWindow::hideMask = %window.getRenderLayerMask();

   //GuiFormManager::SendContentMessage( $LBQuickEdit, 0, "clearCache" );
   
   //if (isObject(ToolManager.getLastWindow()))
      //ToolManager.getLastWindow().endLevel();
      
   Canvas.setContent(mainScreenGui);
   setCanvasTitle( $currentProject );
   
   // Set Level-Editor State.
   $LevelEditorActive = false;

   levelEditorMap.pop();
}

// --------------------------------------------------------------------
// Toggle Level Editor.
// --------------------------------------------------------------------
$LevelEditorActive = false;
function toggleLevelEditor()
{
   // Toggle Level-Editor.
   if ($LevelEditorActive)
      hideLevelEditor();
   else
      showLevelEditor();
}
