]> git.tdb.fi Git - r2c2.git/blobdiff - source/designer/measure.cpp
Make use of the mspmath library
[r2c2.git] / source / designer / measure.cpp
index f115f9ddd5a287f9f7a14df8f170fc8ef1e346f4..c7fead1af705a4c60a8230b5e04750d8fd21cd5c 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 = Angle::zero();
+               snap_to_tracks(ssnap);
 
                state = ACTIVE;
        }
@@ -54,24 +54,19 @@ 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 = rotated_vector(pointer-ssnap.position, -ssnap.rotation);
 
-               par_dist = delta.x*c+delta.y*s;
-               perp_dist = delta.x*s-delta.y*c;
+               par_dist = delta.x;
+               perp_dist = delta.y;
 
-               adiff = dir-sdir+M_PI;
-               while(adiff<-M_PI)
-                       adiff += M_PI*2;
-               while(adiff>M_PI)
-                       adiff -= M_PI*2;
+               adiff = wrap_balanced(sn.rotation-ssnap.rotation+Angle::half_turn());
 
                update_mesh();
 
@@ -85,8 +80,8 @@ void Measure::render(GL::Renderer &renderer, const GL::Tag &) const
                return;
 
        GL::Renderer::Push push(renderer);
-       const Vector &pos = (state==ACTIVE ? spoint : pointer);
-       renderer.matrix_stack() *= GL::Matrix::translation(pos.x, pos.y, pos.z);
+       const Vector &pos = (state==ACTIVE ? ssnap.position : pointer);
+       renderer.matrix_stack() *= GL::Matrix::translation(pos);
 
        mesh.draw(renderer);
 }
@@ -108,25 +103,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;
 }