6 #include "signaltype.h"
16 Signal::Signal(Layout &l, const SignalType &t):
22 check_allocated_blocks(false),
27 layout.signal_block_reserved.connect(sigc::mem_fun(this, &Signal::block_reserved));
32 if(layout.has_driver() && id)
33 layout.get_driver().remove_signal(id);
37 Signal *Signal::clone(Layout *to_layout) const
39 Signal *sig = new Signal((to_layout ? *to_layout : layout), type);
40 sig->set_position(position);
41 sig->set_rotation(rotation);
45 void Signal::set_address(unsigned a)
47 Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0);
50 driver->remove_signal(id);
53 id = driver->add_signal(address, type);
58 void Signal::set_position(const Vector &p)
66 void Signal::set_rotation(const Angle &r)
74 void Signal::update_attachment()
79 block = track.block_iter();
84 unsigned Signal::get_n_snap_nodes() const
89 Snap Signal::get_snap_node(unsigned i) const
92 throw out_of_range("Signal::get_snap_node");
95 sn.position = position;
96 sn.rotation = rotation;
100 SnapType Signal::get_default_snap_type_to(const Object &other) const
102 if(dynamic_cast<const Track *>(&other))
108 void Signal::tick(const Time::TimeDelta &)
110 if(check_allocated_blocks)
112 unsigned n_blocks = 0;
113 const Block *last = train->get_block_allocator().last().block();
114 if(block.block()!=last)
116 BlockIter iter = block.next();
117 while(iter && iter->get_train()==train && iter.block()!=last)
119 if(iter->get_sensor_address())
124 check_allocated_blocks = false;
126 const list<SignalType::Indication> &indications = type.get_indications();
127 unsigned aspect = indications.back().aspect;
128 for(list<SignalType::Indication>::const_iterator i=indications.begin(); i!=indications.end(); ++i)
129 if(n_blocks>=i->free_blocks)
135 layout.get_driver().set_signal(id, aspect);
139 void Signal::block_reserved(const Block &b, Train *t)
141 if(&b==block.block())
145 const BlockIter &b_iter = t->get_block_allocator().iter_for(b);
146 if(b_iter && b_iter.entry()==block.entry())
149 train_conn.disconnect();
152 train_conn = train->signal_advanced.connect(sigc::mem_fun(this, &Signal::train_advanced));
153 check_allocated_blocks = true;
158 layout.get_driver().set_signal(id, type.get_indications().back().aspect);
162 else if(train && t==train)
163 check_allocated_blocks = true;
166 void Signal::train_advanced(Block &b)
168 if(&b==block.block())
170 else if(passing && b.get_sensor_address())
172 layout.get_driver().set_signal(id, type.get_indications().back().aspect);
181 train_conn.disconnect();
182 check_allocated_blocks = false;
185 void Signal::save(list<DataFile::Statement> &st) const
187 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
188 st.push_back((DataFile::Statement("rotation"), rotation.radians()));
190 st.push_back((DataFile::Statement("address"), address));
194 Signal::Loader::Loader(Signal &s):
195 DataFile::ObjectLoader<Signal>(s)
197 add("address", &Loader::address);
198 add("position", &Loader::position);
199 add("rotation", &Loader::rotation);
202 void Signal::Loader::address(unsigned a)
207 void Signal::Loader::position(float x, float y, float z)
209 obj.set_position(Vector(x, y, z));
212 void Signal::Loader::rotation(float d)
214 obj.set_rotation(Angle::from_radians(d));