5 #include "signaltype.h"
15 Signal::Signal(Layout &l, const SignalType &t):
23 check_allocated_blocks(false),
26 layout.add_signal(*this);
28 layout.signal_block_reserved.connect(sigc::mem_fun(this, &Signal::block_reserved));
33 layout.remove_signal(*this);
36 Signal *Signal::clone(Layout *to_layout) const
38 Signal *sig = new Signal((to_layout ? *to_layout : layout), type);
39 sig->set_position(position);
40 sig->set_rotation(rotation);
44 void Signal::set_address(unsigned a)
48 if(layout.has_driver() && address)
49 layout.get_driver().add_signal(address, type);
52 void Signal::set_position(const Vector &p)
54 const set<Track *> &tracks = layout.get_tracks();
56 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
57 if(!(*i)->get_type().is_turnout())
61 sn.rotation = rotation;
62 (*i)->snap(sn, 1000, SNAP_SEGMENT);
63 float d = distance(p, sn.position);
66 position = sn.position;
67 rotation = sn.rotation;
76 void Signal::normalize_location()
78 block = &track->get_block();
80 unsigned n_endpoints = track->get_type().get_endpoints().size();
81 for(unsigned j=0; j<n_endpoints; ++j)
83 Angle a = wrap_with_base(track->get_snap_node(j).rotation-rotation, -Angle::quarter_turn());
84 if(a>=Angle::quarter_turn())
86 BlockIter biter = TrackIter(track, j).block_iter();
87 entry = biter.entry();
92 void Signal::set_rotation(const Angle &r)
94 Angle a = wrap_with_base(rotation-r, -Angle::quarter_turn());
95 if(a>=Angle::quarter_turn())
96 rotation = wrap_positive(rotation+Angle::half_turn());
101 unsigned Signal::get_n_snap_nodes() const
106 Snap Signal::get_snap_node(unsigned i) const
109 throw out_of_range("Signal::get_snap_node");
112 sn.position = position;
113 sn.rotation = rotation;
117 SnapType Signal::get_default_snap_type_to(const Object &other) const
119 if(dynamic_cast<const Track *>(&other))
125 void Signal::tick(const Time::TimeDelta &)
127 if(check_allocated_blocks)
129 unsigned n_blocks = 0;
130 BlockIter iter(block, entry);
132 while(iter && iter->get_train()==train)
134 if(iter->get_sensor_id())
138 check_allocated_blocks = false;
140 const list<SignalType::Indication> &indications = type.get_indications();
141 unsigned aspect = indications.back().aspect;
142 for(list<SignalType::Indication>::const_iterator i=indications.begin(); i!=indications.end(); ++i)
143 if(n_blocks>=i->free_blocks)
149 layout.get_driver().set_signal(address, aspect);
153 void Signal::block_reserved(const Block &b, Train *t)
159 int train_entry = t->get_entry_to_block(*block);
160 if(train_entry>=0 && static_cast<unsigned>(train_entry)==entry)
163 train_conn.disconnect();
166 train_conn = train->signal_advanced.connect(sigc::mem_fun(this, &Signal::train_advanced));
167 check_allocated_blocks = true;
172 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
176 else if(train && t==train)
177 check_allocated_blocks = true;
180 void Signal::train_advanced(Block &b)
184 else if(passing && b.get_sensor_id())
186 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
195 train_conn.disconnect();
196 check_allocated_blocks = false;
199 void Signal::save(list<DataFile::Statement> &st) const
201 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
202 st.push_back((DataFile::Statement("rotation"), rotation.radians()));
204 st.push_back((DataFile::Statement("address"), address));
208 Signal::Loader::Loader(Signal &s):
209 DataFile::ObjectLoader<Signal>(s)
211 add("address", &Loader::address);
212 add("position", &Loader::position);
213 add("rotation", &Loader::rotation);
216 void Signal::Loader::address(unsigned a)
221 void Signal::Loader::position(float x, float y, float z)
223 obj.set_position(Vector(x, y, z));
226 void Signal::Loader::rotation(float d)
228 obj.set_rotation(Angle::from_radians(d));