/** 
* PURPOSE OF THIS SCRIPT:
*
* Delete frames from cursor to start of event, and then move all events to the left by n frames.
*
*
* A video track must be selected. If an audio track is selected, nothing happens.
* The video event on the track that lies beneath the cursor 
* will be shortened from the cursor to the start of the event.
* If the track beneath the video track contains audio, the audio event in that track 
* that lies beneath the cursor will also be shortened by the same number of frames.
*
* Written By: John Meyer
* With thanks to Edward Troxel
* Written: June 4, 2003
*
**/ 

import System; 
import System.IO; 
import System.Windows.Forms; 
import SonicFoundry.Vegas; 


try { 

    //Global declarations
    var dStart : Double;
    var dLength : Double;
    var dCursor : Double;
    var trackEnum : Enumerator; 
    var evnt : TrackEvent;
  
    var EventBegin : Timecode = Vegas.Cursor;    // Use this to move cursor position.
    var track = FindSelectedTrack();             // Use this function to find the first selected track.  
    var eventEnum = new Enumerator(track.Events);
    var BeyondCursor : boolean = false;          // This flag used to notify if event is to right of cursor.

    if (track.IsVideo()) {                       // Proceed only if selected track is video track.

      if ( TrimEventAtCursor() ) {               // Function that trims event and moves all remaining events on track left.

        // Get set to look at track directly below the video track.
        BeyondCursor = false;
        trackEnum.moveNext();                    // Go to next track.


        if (!trackEnum.atEnd()) {                   // Only proceed if there is a track below the video track.
          track = Track(trackEnum.item());          // When doing the first track (above), these two lines were executed
          eventEnum = new Enumerator(track.Events); // in the FindSelectedTrack() function.
          if (track.IsAudio()) {                    // Only trim the event if this is an audio track.
            TrimEventAtCursor();
          }    
        }
      }
    }
    Vegas.UpdateUI();  

// Move cursor to start of selected event (cursor stays at same media location).
Vegas.Cursor = EventBegin; // Remove this line to keep cursor in same location.

} catch (e) { 
  MessageBox.Show(e); 
} 

// End of main program



// Beginning of functions

function FindSelectedTrack() : Track {
  trackEnum = new Enumerator(Vegas.Project.Tracks);
  while (!trackEnum.atEnd()) {
    var track : Track = Track(trackEnum.item());

    if (track.Selected) {
      return track;
    }
    trackEnum.moveNext();
  }
  return null;
}


/** 
*
* The following function finds the event on the selected track 
* that lies under the cursor. It also deselects all other events. 
*
**/

function TrimEventAtCursor() {

  var EventFound : boolean = false;  // Function returns false if no video media under cursor.
  var DeleteFrames : Timecode = new Timecode("00:00:00:00");
  var DeleteTime : Double = DeleteFrames.ToMilliseconds(); 

  dCursor = Vegas.Cursor.ToMilliseconds(); // Remember the cursor position.

  //Go through each event on the track.

  while (!eventEnum.atEnd()) {
    evnt = TrackEvent(eventEnum.item());
    evnt.Selected = false;  // De-select the event


    // Get the event's start and length timecode, in milliseconds.
    dStart = evnt.Start.ToMilliseconds();
    dLength = evnt.Length.ToMilliseconds();
    

    if (BeyondCursor) {                     // Move, but don't truncate events to the 
      evnt.Start=evnt.Start-DeleteFrames;   // right of the event under the cursor.
    }

/**
*     If the cursor timecode is between the beginning and end of the 
*     event timecodes, then select the event, and trim start by n frames.
*     Move selected event and all events to right of cursor to left by n frames.
**/ 

    if ( (dCursor >= dStart) && ( dCursor < (dLength + dStart) ) ) {
      evnt.Selected = true; // Select this event.
      EventFound = true;
      BeyondCursor = true;  // Remaining events are to right of cursor.
      DeleteFrames = Vegas.Cursor - Timecode(evnt.Start);
      DeleteTime = DeleteFrames.ToMilliseconds();
      EventBegin = evnt.Start;
      dLength = dLength - DeleteTime;

      // Next two lines truncate start of event, and then move it left
      // by the same amount that it was truncated.
      evnt.AdjustStartLength(new Timecode(dStart), new Timecode(dLength), false);
      evnt.ActiveTake.Offset = DeleteFrames+evnt.ActiveTake.Offset;
    }

    eventEnum.moveNext(); // Go to next event on this timeline.
    Vegas.UpdateUI();  

    } 
return EventFound;
}

