6 #include "signaltype.h"
16 Signal::Signal(Layout &l, const SignalType &t):
21 check_allocated_blocks(false),
26 layout.signal_block_reserved.connect(sigc::mem_fun(this, &Signal::block_reserved));
31 if(layout.has_driver() && address)
32 layout.get_driver().remove_sensor(address);
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)
46 Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0);
49 driver->remove_signal(address);
52 driver->add_signal(address, type);
55 void Signal::set_position(const Vector &p)
63 void Signal::set_rotation(const Angle &r)
71 void Signal::update_attachment()
76 block = track.block_iter();
81 unsigned Signal::get_n_snap_nodes() const
86 Snap Signal::get_snap_node(unsigned i) const
89 throw out_of_range("Signal::get_snap_node");
92 sn.position = position;
93 sn.rotation = rotation;
97 SnapType Signal::get_default_snap_type_to(const Object &other) const
99 if(dynamic_cast<const Track *>(&other))
105 void Signal::tick(const Time::TimeDelta &)
107 if(check_allocated_blocks)
109 unsigned n_blocks = 0;
110 BlockIter iter = block.next();
111 while(iter && iter->get_train()==train)
113 if(iter->get_sensor_address())
117 check_allocated_blocks = false;
119 const list<SignalType::Indication> &indications = type.get_indications();
120 unsigned aspect = indications.back().aspect;
121 for(list<SignalType::Indication>::const_iterator i=indications.begin(); i!=indications.end(); ++i)
122 if(n_blocks>=i->free_blocks)
128 layout.get_driver().set_signal(address, aspect);
132 void Signal::block_reserved(const Block &b, Train *t)
134 if(&b==block.block())
138 const BlockIter &b_iter = t->get_block_allocator().iter_for(b);
139 if(b_iter && b_iter.entry()==block.entry())
142 train_conn.disconnect();
145 train_conn = train->signal_advanced.connect(sigc::mem_fun(this, &Signal::train_advanced));
146 check_allocated_blocks = true;
151 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
155 else if(train && t==train)
156 check_allocated_blocks = true;
159 void Signal::train_advanced(Block &b)
161 if(&b==block.block())
163 else if(passing && b.get_sensor_address())
165 layout.get_driver().set_signal(address, type.get_indications().back().aspect);
174 train_conn.disconnect();
175 check_allocated_blocks = false;
178 void Signal::save(list<DataFile::Statement> &st) const
180 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
181 st.push_back((DataFile::Statement("rotation"), rotation.radians()));
183 st.push_back((DataFile::Statement("address"), address));
187 Signal::Loader::Loader(Signal &s):
188 DataFile::ObjectLoader<Signal>(s)
190 add("address", &Loader::address);
191 add("position", &Loader::position);
192 add("rotation", &Loader::rotation);
195 void Signal::Loader::address(unsigned a)
200 void Signal::Loader::position(float x, float y, float z)
202 obj.set_position(Vector(x, y, z));
205 void Signal::Loader::rotation(float d)
207 obj.set_rotation(Angle::from_radians(d));