Skip to content

Commit

Permalink
fix for #61
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekmotyl committed Jul 30, 2022
1 parent ea1a578 commit a588659
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions src/SimpleVideoCutter/VideoCutterTimeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Selection
{
public long Start;
public long End;
public long Duration => End - Start;

public bool Includes(long position)
{
Expand Down Expand Up @@ -158,7 +159,7 @@ public void AddSelection(long start, long end)
public bool Empty => selections.Count == 0;
public long? OverallStart => selections.FirstOrDefault()?.Start;
public long? OverallEnd => selections.LastOrDefault()?.End;
public long OverallDuration => OverallEnd ?? 0 - OverallStart ?? 0;
public long OverallDuration => selections.Sum(sel => sel.Duration);

public List<Selection> AllSelections => selections;

Expand Down Expand Up @@ -209,29 +210,51 @@ public void DeleteSelection(int index)
public bool SetSelectionStart(int index, long value)
{
var selection = this.selections[index];
var prev = index > 0 ? this.selections[index-1] : null;
if (prev != null && prev.End > value)
var prevStart = selection.Start;
try
{
selections[index].Start = prev.End+1;
return false;
var prev = index > 0 ? this.selections[index - 1] : null;
if (prev != null && prev.End > value)
{
selections[index].Start = prev.End + 1;
return false;
}

selections[index].Start = value > selections[index].End ? selections[index].End : value;
return true;
}
finally
{
if (selections[index].Start != prevStart)
{
OnSelectionsChanged();
}
}

selections[index].Start = value > selections[index].End ? selections[index].End : value;
return true;
}

public bool SetSelectionEnd(int index, long value)
{
var selection = this.selections[index];
var next = index < selections.Count - 1 ? this.selections[index + 1] : null;
if (next != null && next.Start < value)
var prevEnd = selection.End;
try
{
selections[index].End = next.Start - 1;
return false;
}
var next = index < selections.Count - 1 ? this.selections[index + 1] : null;
if (next != null && next.Start < value)
{
selections[index].End = next.Start - 1;
return false;
}

selections[index].End = value < selections[index].Start ? selections[index].Start : value;
return true;
selections[index].End = value < selections[index].Start ? selections[index].Start : value;
return true;
}
finally
{
if (selections[index].End != prevEnd)
{
OnSelectionsChanged();
}
}
}

public bool CanStartSelectionAtFrame(long frame)
Expand Down

0 comments on commit a588659

Please sign in to comment.