]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/beamgate.cpp
Use max/min for range capping
[r2c2.git] / source / libr2c2 / beamgate.cpp
1 #include "beamgate.h"
2 #include "layout.h"
3
4 using namespace std;
5 using namespace Msp;
6
7 namespace R2C2 {
8
9 BeamGate::BeamGate(Layout &l):
10         TrackAttachment(l),
11         Sensor(l)
12 {
13         invert = true;
14
15         TrackAttachment::layout.add(*this);
16 }
17
18 BeamGate::~BeamGate()
19 {
20         TrackAttachment::layout.remove(*this);
21 }
22
23 BeamGate *BeamGate::clone(Layout *to_layout) const
24 {
25         BeamGate *gate = new BeamGate(to_layout ? *to_layout : TrackAttachment::layout);
26         gate->set_position(position);
27         gate->set_rotation(rotation);
28         return gate;
29 }
30
31 const BeamGateType &BeamGate::get_type() const
32 {
33         return BeamGateType::instance();
34 }
35
36 void BeamGate::set_position(const Vector &p)
37 {
38         position = p;
39         update_attachment();
40         signal_moved.emit();
41 }
42
43 void BeamGate::set_rotation(const Angle &r)
44 {
45         rotation = r;
46         update_attachment();
47         signal_moved.emit();
48 }
49
50 void BeamGate::update_attachment()
51 {
52         attach_to_closest(100);
53
54         if(track)
55         {
56                 OrientedPoint p = track.point();
57                 position = p.position;
58                 rotation = p.rotation;
59                 tilt = p.tilt;
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