]> git.tdb.fi Git - r2c2.git/blobdiff - source/designer/measure.cpp
Add a generic snapping interface in Object
[r2c2.git] / source / designer / measure.cpp
index f115f9ddd5a287f9f7a14df8f170fc8ef1e346f4..69ada814e110ff53935e8151e70462762cf8e059 100644 (file)
@@ -28,9 +28,9 @@ void Measure::button_press(float gx, float gy, unsigned btn)
 
        if(btn==1)
        {
-               spoint = Vector(gx, gy, 0);
-               sdir = 0;
-               snap_to_tracks(spoint, sdir);
+               ssnap.position = Vector(gx, gy, 0);
+               ssnap.rotation = 0;
+               snap_to_tracks(ssnap);
 
                state = ACTIVE;
        }
@@ -54,20 +54,21 @@ void Measure::pointer_motion(float gx, float gy)
        if(!state)
                return;
 
-       pointer = Vector(gx, gy, 0);
-       float dir = sdir;
-       snap_to_tracks(pointer, dir);
+       Snap sn = ssnap;
+       sn.position = Vector(gx, gy, 0);
+       snap_to_tracks(sn);
+       pointer = sn.position;
 
        if(state!=STARTING)
        {
-               Vector delta(pointer.x-spoint.x, pointer.y-spoint.y, 0);
-               float c = cos(sdir);
-               float s = sin(sdir);
+               Vector delta(pointer.x-ssnap.position.x, pointer.y-ssnap.position.y, 0);
+               float c = cos(ssnap.rotation);
+               float s = sin(ssnap.rotation);
 
                par_dist = delta.x*c+delta.y*s;
                perp_dist = delta.x*s-delta.y*c;
 
-               adiff = dir-sdir+M_PI;
+               adiff = sn.rotation-ssnap.rotation+M_PI;
                while(adiff<-M_PI)
                        adiff += M_PI*2;
                while(adiff>M_PI)
@@ -85,7 +86,7 @@ void Measure::render(GL::Renderer &renderer, const GL::Tag &) const
                return;
 
        GL::Renderer::Push push(renderer);
-       const Vector &pos = (state==ACTIVE ? spoint : pointer);
+       const Vector &pos = (state==ACTIVE ? ssnap.position : pointer);
        renderer.matrix_stack() *= GL::Matrix::translation(pos.x, pos.y, pos.z);
 
        mesh.draw(renderer);
@@ -108,25 +109,25 @@ void Measure::update_mesh()
 
        if(state==ACTIVE)
        {
-               float c = cos(sdir);
-               float s = sin(sdir);
+               float c = cos(ssnap.rotation);
+               float s = sin(ssnap.rotation);
                bld.begin(GL::QUAD_STRIP);
                bld.vertex(0, 0, 0);
                bld.vertex(0, 0, 0.01);
                bld.vertex(c*par_dist, s*par_dist, 0);
                bld.vertex(c*par_dist, s*par_dist, 0.01);
-               bld.vertex(pointer.x-spoint.x, pointer.y-spoint.y, 0);
-               bld.vertex(pointer.x-spoint.x, pointer.y-spoint.y, 0.01);
+               bld.vertex(pointer.x-ssnap.position.x, pointer.y-ssnap.position.y, 0);
+               bld.vertex(pointer.x-ssnap.position.x, pointer.y-ssnap.position.y, 0.01);
                bld.vertex(0, 0, 0);
                bld.vertex(0, 0, 0.01);
                bld.end();
        }
 }
 
-void Measure::snap_to_tracks(Vector &pt, float &dir)
+void Measure::snap_to_tracks(Snap &sn)
 {
        const set<Track *> &ltracks = designer.get_layout().get_tracks();
        for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
-               if((*i)->snap(pt, dir))
+               if((*i)->snap(sn, 0.01, SNAP_NODE))
                        return;
 }