]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/beamgate.cpp
Move driver.add_sensor call where it belongs
[r2c2.git] / source / libr2c2 / beamgate.cpp
1 #include "beamgate.h"
2 #include "catalogue.h"
3 #include "layout.h"
4
5 using namespace std;
6 using namespace Msp;
7
8 namespace R2C2 {
9
10 BeamGate::BeamGate(Layout &l):
11         TrackAttachment(l),
12         Sensor(l)
13 {
14         invert = true;
15
16         layout.add(*this);
17 }
18
19 BeamGate::~BeamGate()
20 {
21         layout.remove(*this);
22 }
23
24 BeamGate *BeamGate::clone(Layout *to_layout) const
25 {
26         BeamGate *gate = new BeamGate(to_layout ? *to_layout : layout);
27         gate->set_position(position);
28         gate->set_rotation(rotation);
29         return gate;
30 }
31
32 const BeamGateType &BeamGate::get_type() const
33 {
34         return BeamGateType::instance();
35 }
36
37 void BeamGate::set_position(const Vector &p)
38 {
39         position = p;
40         update_attachment();
41         signal_moved.emit();
42 }
43
44 void BeamGate::set_rotation(const Angle &r)
45 {
46         rotation = r;
47         update_attachment();
48         signal_moved.emit();
49 }
50
51 void BeamGate::update_attachment()
52 {
53         attach_to_closest(100*layout.get_catalogue().get_gauge());
54
55         if(track)
56         {
57                 TrackPoint tp = track->get_point(track.entry(), offset);
58                 position = tp.pos;
59                 rotation = tp.dir;
60         }
61 }
62
63 Block *BeamGate::get_block() const
64 {
65         if(track)
66                 return &track->get_block();
67         else
68                 return 0;
69 }
70
71 void BeamGate::save(list<DataFile::Statement> &st) const
72 {
73         st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
74         st.push_back((DataFile::Statement("rotation"), rotation.radians()));
75         if(address)
76                 st.push_back((DataFile::Statement("address"), address));
77 }
78
79
80 BeamGate::Loader::Loader(BeamGate &g):
81         DataFile::ObjectLoader<BeamGate>(g)
82 {
83         add("address",  &Loader::address);
84         add("position", &Loader::position);
85         add("rotation", &Loader::rotation);
86 }
87
88 void BeamGate::Loader::address(unsigned a)
89 {
90         obj.set_address(a);
91 }
92
93 void BeamGate::Loader::position(float x, float y, float z)
94 {
95         obj.set_position(Vector(x, y, z));
96 }
97
98 void BeamGate::Loader::rotation(float r)
99 {
100         obj.set_rotation(Angle::from_radians(r));
101 }
102
103 } // namespace R2C2