Implement snap-to for regions.

This commit is contained in:
Jonathan Moore Liles 2008-02-18 01:44:31 -06:00
parent 5ec40ad5ce
commit a055856c56
2 changed files with 43 additions and 4 deletions

View File

@ -216,7 +216,15 @@ Region::handle ( int m )
} }
if ( ox + X >= _track->x() ) if ( ox + X >= _track->x() )
position( ox + X, y() ); {
int nx = ox + X;
// nx = _track->snap( this, nx );
position( nx, y() );
_track->snap( this );
}
if ( Y > y() + h() ) if ( Y > y() + h() )
{ {

37
Track.H
View File

@ -60,9 +60,6 @@ public:
void add ( Region *r ) void add ( Region *r )
{ {
printf( "add" );
if ( r->track() ) if ( r->track() )
{ {
r->track()->remove_region( r ); r->track()->remove_region( r );
@ -80,6 +77,40 @@ public:
r->redraw(); r->redraw();
} }
/** snap /r/ to nearest edge */
void
snap ( Region *r )
{
const int snap_pixels = 10;
int rx1 = r->x();
int rx2 = r->x() + r->w();
for ( int i = children(); i-- ; )
{
const Fl_Widget *w = child( i );
if ( w == r )
continue;
int wx1 = w->x();
int wx2 = w->x() + w->w();
if ( abs( rx1 - wx2 ) < snap_pixels )
{
rx1 = wx2 - 1;
break;
}
if ( abs( rx2 - wx1 ) < snap_pixels )
{
rx1 = (wx1 - r->w()) + 1;
break;
}
}
r->position( rx1, y() );
}
int handle ( int m ) int handle ( int m )
{ {
switch ( m ) switch ( m )