public void mouseDragged( MouseEvent e )
{
if( dndFirstEvent == null ) return; // not a valid drag gesture
long when, delta, delta2;
PointInTime pit, pit2, recentLatest;
int i;
double d1;
Span span;
if( !dndDragging ) { // test if mouse move was sufficient
dndDragging = true;
dndRecentRect = new Rectangle2D.Double();
dndLatest = (PointInTime) dndFreehandPoints.get( 0 );
dndLatestIdx = 0;
}
ptCurrentMouse = screenToVirtual( e.getPoint() );
// when = rt_pos;
// EEE
when = transport.getCurrentFrame();
recentLatest= dndLatest;
dndLatest = new PointInTime( ptCurrentMouse, when );
pit = (PointInTime) dndFreehandPoints.get( dndFreehandPoints.size() - 1 );
pit2 = (PointInTime) dndFreehandPoints.get( 0 );
if( recentLatest.getWhen() < when ) { // add after recent latest
dndLatestIdx++;
for( i = dndLatestIdx; i < dndFreehandPoints.size(); i++ ) {
if( ((PointInTime) dndFreehandPoints.get( i )).getWhen() > when ) break;
}
while( i > dndLatestIdx ) {
dndFreehandPoints.remove( --i );
}
dndFreehandPoints.add( dndLatestIdx, dndLatest );
} else if( recentLatest.getWhen() == when ) { // replace recent latest
dndFreehandPoints.set( dndLatestIdx, dndLatest );
} else { // loop has restarted
dndLatestIdx = 0;
for( i = 0; i < dndFreehandPoints.size(); i++ ) {
if( ((PointInTime) dndFreehandPoints.get( i )).getWhen() > when ) break;
}
while( i > 0 ) {
dndFreehandPoints.remove( --i );
}
span = doc.timeline.getSelectionSpan();
if( !span.isEmpty() && when > span.getStart() ) {
delta = span.getStop() - pit.getWhen();
delta2 = pit2.getWhen() - span.getStart();
if( delta > 0 && delta2 >= 0 ) {
if( delta2 > 0 && when > span.getStart() ) { // add interpolated point at loop start
d1 = (double) delta / (double) (delta + delta2);
pit = new PointInTime( pit.getX() + (pit2.getX() - pit.getX()) * d1,
pit.getY() + (pit2.getY() - pit.getY()) * d1, span.getStart() );
dndFreehandPoints.add( dndLatestIdx++, pit );
}
if( delta > 1 && when < span.getStop() - 1 ) { // add interpolated point at loop end
d1 = (double) (delta - 1) / (double) (delta + delta2);
pit = new PointInTime( pit.getX() + (pit2.getX() - pit.getX()) * d1,
pit.getY() + (pit2.getY() - pit.getY()) * d1, span.getStop() - 1 );
dndFreehandPoints.add( pit );
}
}
}
dndFreehandPoints.add( dndLatestIdx, dndLatest );