]> git.tdb.fi Git - r2c2.git/blob - source/3d/layout.cpp
Use maps instead of lists in Layout3D
[r2c2.git] / source / 3d / layout.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
9 #include <limits>
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>
16 #include "layout.h"
17 #include "track.h"
18 #include "vehicle.h"
19
20 using namespace std;
21 using namespace Msp;
22
23 namespace Marklin {
24
25 Layout3D::Layout3D(Layout &l):
26         layout(l),
27         catalogue(layout.get_catalogue())
28 {
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));
32
33         const set<Track *> &ltracks = layout.get_tracks();
34         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
35                 track_added(**i);
36 }
37
38 Layout3D::~Layout3D()
39 {
40         while(!tracks.empty())
41                 delete tracks.begin()->second;
42         while(!vehicles.empty())
43                 delete vehicles.begin()->second;
44 }
45
46 void Layout3D::add_track(Track3D &t)
47 {
48         if(tracks.count(&t.get_track()))
49                 throw KeyError("Duplicate track");
50
51         tracks[&t.get_track()] = &t;
52 }
53
54 void Layout3D::remove_track(Track3D &t)
55 {
56         tracks.erase(&t.get_track());
57 }
58
59 Track3D &Layout3D::get_track(Track &t) const
60 {
61         TrackMap::const_iterator i = tracks.find(&t);
62         if(i==tracks.end())
63                 throw KeyError("Unknown track");
64
65         return *i->second;
66 }
67
68 Track3D *Layout3D::pick_track(float x, float y, float size) const
69 {
70         vector<GL::SelectRecord> select_buf;
71         GL::select_buffer(select_buf);
72         GL::render_mode(GL::SELECT);
73
74         {
75                 GL::PushMatrix push_mat;
76                 GL::load_identity();
77
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);
82         }
83
84         scene.render(0);
85
86         GL::ClipPlane::disable(0);
87         GL::ClipPlane::disable(1);
88         GL::ClipPlane::disable(2);
89         GL::ClipPlane::disable(3);
90
91         GL::render_mode(GL::RENDER);
92         Track3D *track = 0;
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())
96                 {
97                         track = reinterpret_cast<Track3D *>(i->names.back());
98                         track_depth = i->min_depth;
99                 }
100
101         return track;
102 }
103
104 void Layout3D::add_vehicle(Vehicle3D &v)
105 {
106         if(vehicles.count(&v.get_vehicle()))
107                 throw KeyError("Duplicate vehicle");
108
109         vehicles[&v.get_vehicle()] = &v;
110 }
111
112 void Layout3D::remove_vehicle(Vehicle3D &v)
113 {
114         vehicles.erase(&v.get_vehicle());
115 }
116
117 Vehicle3D &Layout3D::get_vehicle(Vehicle &v) const
118 {
119         VehicleMap::const_iterator i = vehicles.find(&v);
120         if(i==vehicles.end())
121                 throw KeyError("Unknown vehicle");
122
123         return *i->second;
124 }
125
126 void Layout3D::track_added(Track &t)
127 {
128         new Track3D(*this, t);
129 }
130
131 void Layout3D::track_removed(Track &t)
132 {
133         TrackMap::iterator i = tracks.find(&t);
134         if(i!=tracks.end())
135                 delete i->second;
136 }
137
138 void Layout3D::vehicle_added(Vehicle &v)
139 {
140         new Vehicle3D(*this, v);
141 }
142
143 } // namespace Marklin