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>
23 Layout3D::Layout3D(Layout &l):
25 catalogue(layout.get_catalogue())
27 layout.signal_track_added.connect(sigc::mem_fun(this, &Layout3D::track_added));
28 layout.signal_track_removed.connect(sigc::mem_fun(this, &Layout3D::track_removed));
29 layout.signal_train_added.connect(sigc::mem_fun(this, &Layout3D::train_added));
31 const set<Track *> <racks = layout.get_tracks();
32 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
38 while(!tracks.empty())
39 delete tracks.front();
40 while(!trains.empty())
41 delete trains.front();
44 void Layout3D::add_track(Track3D &t)
49 void Layout3D::remove_track(Track3D &t)
51 list<Track3D *>::iterator i = find(tracks.begin(), tracks.end(), &t);
56 Track3D &Layout3D::get_track(const Track &t) const
58 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
59 if(&(*i)->get_track()==&t)
62 throw KeyError("Unknown track");
65 Track3D *Layout3D::pick_track(float x, float y, float size) const
67 vector<GL::SelectRecord> select_buf;
68 GL::select_buffer(select_buf);
69 GL::render_mode(GL::SELECT);
72 GL::PushMatrix push_mat;
75 GL::ClipPlane(1, 0, x-size, 0).apply_to(0);
76 GL::ClipPlane(-1, 0, -x-size, 0).apply_to(1);
77 GL::ClipPlane(0, 1, y-size, 0).apply_to(2);
78 GL::ClipPlane(0, -1, -y-size, 0).apply_to(3);
83 GL::ClipPlane::disable(0);
84 GL::ClipPlane::disable(1);
85 GL::ClipPlane::disable(2);
86 GL::ClipPlane::disable(3);
88 GL::render_mode(GL::RENDER);
90 unsigned track_depth = numeric_limits<unsigned>::max();
91 for(vector<GL::SelectRecord>::iterator i=select_buf.begin(); i!=select_buf.end(); ++i)
92 if(i->min_depth<track_depth)
94 track = reinterpret_cast<Track3D *>(i->names.back());
95 track_depth = i->min_depth;
101 void Layout3D::add_train(Train3D &t)
103 trains.push_back(&t);
106 void Layout3D::remove_train(Train3D &t)
108 list<Train3D *>::iterator i = find(trains.begin(), trains.end(), &t);
113 Train3D &Layout3D::get_train(const Train &t) const
115 for(list<Train3D *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
116 if(&(*i)->get_train()==&t)
119 throw KeyError("Unknown train");
122 void Layout3D::track_added(Track &t)
124 new Track3D(*this, t);
127 void Layout3D::track_removed(Track &t)
129 for(list<Track3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
130 if(&(*i)->get_track()==&t)
137 void Layout3D::train_added(Train &t)
139 new Train3D(*this, t);
142 } // namespace Marklin