]> git.tdb.fi Git - r2c2.git/commitdiff
Don't force signals to be positioned next to track
authorMikko Rasa <tdb@tdb.fi>
Sat, 25 May 2013 23:00:12 +0000 (02:00 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 25 May 2013 23:00:12 +0000 (02:00 +0300)
They can now be positioned freely and will logically attach to a nearby
track.

source/libr2c2/signal.cpp
source/libr2c2/signal.h

index a25f8eca4697fde499b993348a32a4de7f20e752..0139b1fbdaa9cf73ff6a3b25cdaff0b7f63eb984 100644 (file)
@@ -1,4 +1,5 @@
 #include "blockiter.h"
+#include "catalogue.h"
 #include "driver.h"
 #include "layout.h"
 #include "signal.h"
@@ -50,32 +51,36 @@ void Signal::set_address(unsigned a)
 }
 
 void Signal::set_position(const Vector &p)
+{
+       position = p;
+
+       update_location();
+}
+
+void Signal::update_location()
 {
        const set<Track *> &tracks = layout.get_tracks();
+       float limit = layout.get_catalogue().get_gauge()*2;
        float dist = -1;
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
                if(!(*i)->get_type().is_turnout())
                {
                        Snap sn;
-                       sn.position = p;
+                       sn.position = position;
                        sn.rotation = rotation;
-                       (*i)->snap(sn, 1000, SNAP_SEGMENT);
-                       float d = distance(p, sn.position);
+                       (*i)->snap(sn, limit, SNAP_SEGMENT);
+                       float d = distance(position, sn.position);
                        if(d<dist || dist<0)
                        {
-                               position = sn.position;
-                               rotation = sn.rotation;
                                track = *i;
                                dist = d;
                        }
                }
 
-       normalize_location();
-}
+       block = 0;
 
-void Signal::normalize_location()
-{
-       block = &track->get_block();
+       if(!track)
+               return;
 
        unsigned n_endpoints = track->get_type().get_endpoints().size();
        for(unsigned j=0; j<n_endpoints; ++j)
@@ -84,18 +89,20 @@ void Signal::normalize_location()
                if(a>=Angle::quarter_turn())
                {
                        BlockIter biter = TrackIter(track, j).block_iter();
-                       entry = biter.entry();
+                       if(biter)
+                       {
+                               block = &track->get_block();
+                               entry = biter.entry();
+                       }
                }
        }
 }
 
 void Signal::set_rotation(const Angle &r)
 {
-       Angle a = wrap_with_base(rotation-r, -Angle::quarter_turn());
-       if(a>=Angle::quarter_turn())
-               rotation = wrap_positive(rotation+Angle::half_turn());
+       rotation = r;
 
-       normalize_location();
+       update_location();
 }
 
 unsigned Signal::get_n_snap_nodes() const
index 886db6983710e6b4892599544d95956afc6f98e4..73f4bb34098f9356fb9a6ab5dee6eae75cc39b66 100644 (file)
@@ -50,7 +50,7 @@ public:
        virtual void set_position(const Vector &);
        virtual void set_rotation(const Angle &);
 private:
-       void normalize_location();
+       void update_location();
 public:
        virtual Track *get_parent() const { return track; }