@jetdv Could you help me again?
Please, I need the left direction behaviors as the same as the right direction logic.
The right direction does perfectly what it's supposed to do. But the left direction does not.
Where is my mistake?
public void Main(Vegas vegas, string direction, int duplicateCount)
{
myVegas = vegas;
List<TrackEvent> selectedEvents = new List<TrackEvent>();
Dictionary<TrackEvent, Track> eventTrackMap = new Dictionary<TrackEvent, Track>();
// List to store the new duplicated events
List<TrackEvent> newEvents = new List<TrackEvent>();
// Find all selected events and their tracks
foreach (Track track in myVegas.Project.Tracks)
{
foreach (TrackEvent evnt in track.Events)
{
if (evnt.Selected)
{
selectedEvents.Add(evnt);
eventTrackMap[evnt] = track;
}
}
}
// Check if any events are selected
if (selectedEvents.Count > 0)
{
// Sort selected events by their start time to handle them in order
selectedEvents.Sort((a, b) => a.Start.CompareTo(b.Start));
// Find the maximum end time of all selected events manually
Timecode lastGlobalEventEnd = new Timecode(0);
foreach (TrackEvent evnt in selectedEvents)
{
if (evnt.End > lastGlobalEventEnd)
{
lastGlobalEventEnd = evnt.End;
}
}
foreach (TrackEvent selectedEvent in selectedEvents)
{
Track myTrack = eventTrackMap[selectedEvent];
TrackEvent currentEvent = selectedEvent;
Timecode eventLength = selectedEvent.Length;
// Reset the position for the current event's duplicates
Timecode lastEventEnd = currentEvent.End > lastGlobalEventEnd ? currentEvent.End : lastGlobalEventEnd;
for (int i = 0; i < duplicateCount; i++)
{
TrackEvent newEvent = null;
Timecode newEventStart = currentEvent.Start; // Initialize to avoid unassigned error
if (direction == "Left")
{
// Calculate the start time for the next duplicate to avoid overlap
newEventStart = currentEvent.Start - Timecode.FromMilliseconds((i + 1) * eventLength.ToMilliseconds());
}
else if (direction == "Right")
{
// Calculate the start time for the next duplicate to avoid overlap
newEventStart = lastEventEnd;
}
// Duplicate the event with the calculated start time
if (direction == "Left" || direction == "Right")
{
if (myTrack.IsAudio())
{
AudioTrack aTrack = (AudioTrack)myTrack;
newEvent = (AudioEvent)currentEvent.Copy(aTrack, newEventStart);
}
else if (myTrack.IsVideo())
{
VideoTrack vTrack = (VideoTrack)myTrack;
newEvent = (VideoEvent)currentEvent.Copy(vTrack, newEventStart);
}
// Update the end position of the last duplicate event and the global tracker
lastEventEnd = newEvent.End;
lastGlobalEventEnd = lastEventEnd;
}
else if (direction == "Up" || direction == "Down")
{
Track targetTrack = null;
int trackIndex = myTrack.Index;
if (direction == "Up")
{
if (trackIndex > 0)
{
targetTrack = myVegas.Project.Tracks[trackIndex - 1];
}
if (targetTrack == null || (myTrack.IsAudio() && !targetTrack.IsAudio()) || (myTrack.IsVideo() && !targetTrack.IsVideo()))
{
if (myTrack.IsAudio())
{
targetTrack = new AudioTrack(trackIndex);
myVegas.Project.Tracks.Add(targetTrack);
}
else if (myTrack.IsVideo())
{
targetTrack = new VideoTrack(trackIndex);
myVegas.Project.Tracks.Add(targetTrack);
}
}
}
else if (direction == "Down")
{
if (trackIndex < myVegas.Project.Tracks.Count - 1)
{
targetTrack = myVegas.Project.Tracks[trackIndex + 1];
}
if (targetTrack == null || (myTrack.IsAudio() && !targetTrack.IsAudio()) || (myTrack.IsVideo() && !targetTrack.IsVideo()))
{
if (myTrack.IsAudio())
{
targetTrack = new AudioTrack(trackIndex + 1);
myVegas.Project.Tracks.Add(targetTrack);
}
else if (myTrack.IsVideo())
{
targetTrack = new VideoTrack(trackIndex + 1);
myVegas.Project.Tracks.Add(targetTrack);
}
}
}
if (myTrack.IsAudio() && targetTrack.IsAudio())
{
AudioTrack aTrack = (AudioTrack)targetTrack;
newEvent = (AudioEvent)currentEvent.Copy(aTrack, currentEvent.Start);
myTrack = aTrack;
}
else if (myTrack.IsVideo() && targetTrack.IsVideo())
{
VideoTrack vTrack = (VideoTrack)targetTrack;
newEvent = (VideoEvent)currentEvent.Copy(vTrack, currentEvent.Start);
myTrack = vTrack;
}
}
if (newEvent != null)
{
newEvents.Add(newEvent);
}
// Update lastGlobalEventEnd after each duplication to ensure proper spacing
lastGlobalEventEnd = newEvent.End;
}
}
}
else
{
MessageBox.Show("Please select events to duplicate.");
}
}
I think it is this section of the code where my mistake is:
if (direction == "Left")
{
// Calculate the start time for the next duplicate to avoid overlap
newEventStart = currentEvent.Start - Timecode.FromMilliseconds((i + 1) * eventLength.ToMilliseconds());
}
But I can't find a solution.