]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/beamgate.cpp
463a3a967e717c78d64efa81189a05926cc88c9c
[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         TrackAttachment::layout.add(*this);
17 }
18
19 BeamGate::~BeamGate()
20 {
21         TrackAttachment::layout.remove(*this);
22 }
23
24 BeamGate *BeamGate::clone(Layout *to_layout) const
25 {
26         BeamGate *gate = new BeamGate(to_layout ? *to_layout : TrackAttachment::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*TrackAttachment::layout.get_catalogue().get_gauge());
54
55         if(track)
56         {
57                 OrientedPoint p = track.point();
58                 position = p.position;
59                 rotation = p.rotation;
60                 tilt = p.tilt;
61         }
62 }
63
64 Block *BeamGate::get_block() const
65 {
66         if(track)
67                 return &track->get_block();
68         else
69                 return 0;
70 }
71
72 void BeamGate::save(list<DataFile::Statement> &st) const
73 {
74         st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
75         st.push_back((DataFile::Statement("rotation"), rotation.radians()));
76         if(address)
77                 st.push_back((DataFile::Statement("address"), address));
78 }
79
80
81 BeamGate::Loader::Loader(BeamGate &g):
82         DataFile::ObjectLoader<BeamGate>(g)
83 {
84         add("address",  &Loader::address);
85         add("position", &Loader::position);
86         add("rotation", &Loader::rotation);
87 }
88
89 void BeamGate::Loader::address(unsigned a)
90 {
91         obj.set_address(a);
92 }
93
94 void BeamGate::Loader::position(float x, float y, float z)
95 {
96         obj.set_position(Vector(x, y, z));
97 }
98
99 void BeamGate::Loader::rotation(float r)
100 {
101         obj.set_rotation(Angle::from_radians(r));
102 }
103
104 } // namespace R2C2