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

function initializeParticleEditor()
{   
   echo( "\n--------- Initializing Particle Builder ---------" );

   exec("./scripts/newParticleEditor.ed.cs");
   exec("./scripts/ParticleEditorEffectFunctions.ed.cs");
   exec("./scripts/ParticleEditorEmitterFunctions.ed.cs");
   exec("./scripts/undo.ed.cs");
}

function destroyParticleEditor()
{
   // Export Preferences.
   echo("Exporting Particle Editor preferences.");
   export("$particleEditor::*", "./Preferences/prefs.cs", false);
}

$particleEditor::NewEffectFileName = "newEffect.eff";
$particleEditor::layerToSave = "";
$particleEditor::postSaveCallback = "";

function ParticleEditor::saveEffect( %effect, %callback, %forceSaveAs )
{
   $particleEditor::effectToSave = %effect;
   $particleEditor::postSaveCallback = %callback;
   
   if( %forceSaveAs $= "" )
      %forceSaveAs = false;
   
   // Safeguard against the rare case when the filename gets set to ".../.eff"
   // which apparently is a writeable filename, but isn't writeable.
   if( fileBase( %effect.effectFile ) $= "" )
      %forceSaveAs = true;
   
   if( !%forceSaveAs && isFile( %effect.effectFile ) && ( filename( %effect.effectFile ) !$= $particleEditor::NewEffectFileName ) )
      ParticleEditor::doSaveEffect( %effect.effectFile );
   
   else
   {
      // getSaveFileName Here result to below
      %saveCallback = "ParticleEditor::doSaveEffect";
      %currentFile = $currentProject @ "/data/particles/";
      if (fileName(%effect.effectFile) !$= $particleEditor::NewEffectFileName)
         %currentFile = %currentFile @ fileName(%effect.effectFile);
      
      %dialog       =  SaveFileDlgEx;
      %filterList   =  %dialog.findObjectByInternalName("FilterList", true);
      
      // Clear Filters
      %dialog.ClearFilters();
      
      // Add Filters
      %filter = %dialog.AddFilter("*.eff","TGB Particle Effects");
      
      // Select Filter.
      %filterList.setSelected( %filter );
    
      // On acceptance, set the effect on this object.
      getSaveFileName("*.eff", %saveCallback, %currentFile);
   }
}

function ParticleEditor::doSaveEffect( %filename )
{
   if( !isObject( $particleEditor::effectToSave ) )
      return;
      
   %effectObject = $particleEditor::effectToSave;
      
   %effectObject.saveEffect( %fileName );
   %effectObject.onEffectSaved();
   %effectObject.loadEffect( %fileName );
   %effectObject.playEffect( true );
   
   if( $particleEditor::postSaveCallback !$= "" )
      eval( $particleEditor::postSaveCallback );
   
   // Update quick edit so emitters are properly referenced.
   GuiFormManager::SendContentMessage( $LBQuickEdit, %this, "inspectUpdate" );
}
