Implement snap to bar.

This commit is contained in:
Jonathan Moore Liles 2008-02-28 14:36:46 -06:00
parent 618d78eaeb
commit 8f0fb79543
3 changed files with 27 additions and 0 deletions

View File

@ -190,6 +190,23 @@ Timeline::beats_per_minute ( nframes_t when, float bpm )
tempo_track->add( new Tempo_Point( when, bpm ) ); tempo_track->add( new Tempo_Point( when, bpm ) );
} }
/** return the absolute pixel of the nearest measure line to /x/ */
int
Timeline::nearest_line ( int ix )
{
for ( int x = ix - 10; x < ix + 10; ++x )
{
const int measure = ts_to_x( (double)(sample_rate * 60) / beats_per_minute( x_to_ts( x ) + xoffset ));
// const int abs_x = ts_to_x( xoffset ) + x;
if ( 0 == x % measure )
return x;
}
return -1;
}
/* draw appropriate measure lines inside the given bounding box */ /* draw appropriate measure lines inside the given bounding box */
void void
Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color ) Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )

View File

@ -106,6 +106,8 @@ struct Timeline : public Fl_Group
float beats_per_minute ( nframes_t when ) const; float beats_per_minute ( nframes_t when ) const;
int beats_per_bar ( nframes_t when ) const; int beats_per_bar ( nframes_t when ) const;
void beats_per_minute ( nframes_t when, float bpm ); void beats_per_minute ( nframes_t when, float bpm );
int nearest_line ( int ix );
void draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color ); void draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color );
void position ( int X ); void position ( int X );
void draw ( void ); void draw ( void );

View File

@ -119,6 +119,14 @@ Track::snap ( Track_Widget *r )
int rx1 = r->x(); int rx1 = r->x();
int rx2 = r->x() + r->w(); int rx2 = r->x() + r->w();
int nx = timeline->nearest_line( r->abs_x() );
if ( nx >= 0 )
{
r->offset( timeline->x_to_ts( nx ) );
return;
}
for ( list <Track_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ ) for ( list <Track_Widget*>::iterator i = _widgets.begin(); i != _widgets.end(); i++ )
{ {
const Track_Widget *w = (*i); const Track_Widget *w = (*i);