3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include <msp/gl/clip.h>
11 #include <msp/gl/matrix.h>
12 #include <msp/gl/rendermode.h>
13 #include <msp/gl/select.h>
14 #include <msp/gl/texture.h>
15 #include <msp/datafile/parser.h>
25 Layout3D::Layout3D(Layout &l):
27 catalogue(layout.get_catalogue())
29 layout.signal_track_added.connect(sigc::mem_fun(this, &Layout3D::track_added));
30 layout.signal_track_removed.connect(sigc::mem_fun(this, &Layout3D::track_removed));
31 layout.signal_vehicle_added.connect(sigc::mem_fun(this, &Layout3D::vehicle_added));
33 const set<Track *> <racks = layout.get_tracks();
34 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
40 while(!tracks.empty())
41 delete tracks.begin()->second;
42 while(!vehicles.empty())
43 delete vehicles.begin()->second;
46 void Layout3D::add_track(Track3D &t)
48 if(tracks.count(&t.get_track()))
49 throw KeyError("Duplicate track");
51 tracks[&t.get_track()] = &t;
54 void Layout3D::remove_track(Track3D &t)
56 tracks.erase(&t.get_track());
59 Track3D &Layout3D::get_track(Track &t) const
61 TrackMap::const_iterator i = tracks.find(&t);
63 throw KeyError("Unknown track");
68 Track3D *Layout3D::pick_track(float x, float y, float size) const
70 vector<GL::SelectRecord> select_buf;
71 GL::select_buffer(select_buf);
72 GL::render_mode(GL::SELECT);
75 GL::PushMatrix push_mat;
78 GL::ClipPlane(1, 0, x-size, 0).apply_to(0);
79 GL::ClipPlane(-1, 0, -x-size, 0).apply_to(1);
80 GL::ClipPlane(0, 1, y-size, 0).apply_to(2);
81 GL::ClipPlane(0, -1, -y-size, 0).apply_to(3);
86 GL::ClipPlane::disable(0);
87 GL::ClipPlane::disable(1);
88 GL::ClipPlane::disable(2);
89 GL::ClipPlane::disable(3);
91 GL::render_mode(GL::RENDER);
93 unsigned track_depth = numeric_limits<unsigned>::max();
94 for(vector<GL::SelectRecord>::iterator i=select_buf.begin(); i!=select_buf.end(); ++i)
95 if(i->min_depth<track_depth && !i->names.empty())
97 track = reinterpret_cast<Track3D *>(i->names.back());
98 track_depth = i->min_depth;
104 void Layout3D::add_vehicle(Vehicle3D &v)
106 if(vehicles.count(&v.get_vehicle()))
107 throw KeyError("Duplicate vehicle");
109 vehicles[&v.get_vehicle()] = &v;
112 void Layout3D::remove_vehicle(Vehicle3D &v)
114 vehicles.erase(&v.get_vehicle());
117 Vehicle3D &Layout3D::get_vehicle(Vehicle &v) const
119 VehicleMap::const_iterator i = vehicles.find(&v);
120 if(i==vehicles.end())
121 throw KeyError("Unknown vehicle");
126 void Layout3D::track_added(Track &t)
128 new Track3D(*this, t);
131 void Layout3D::track_removed(Track &t)
133 TrackMap::iterator i = tracks.find(&t);
138 void Layout3D::vehicle_added(Vehicle &v)
140 new Vehicle3D(*this, v);
143 } // namespace Marklin