]> git.tdb.fi Git - r2c2.git/blobdiff - source/designer/designer.cpp
Use raycasting instead of OpenGL selection mode to pick tracks
[r2c2.git] / source / designer / designer.cpp
index 3b43efd11ff5226b64cfa0cc9478a72adca90fde..a64c8eb931c9a2d37cf970533bd0627f0a8e65f2 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of R²C²
-Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -468,10 +468,10 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
        {
                if(btn==1)
                {
-                       Track3D *ctrack = pick_track(x, y);
+                       Track *ctrack = pick_track(x, y);
                        if(ctrack)
                        {
-                               Track *track = new Track(*layout, ctrack->get_track().get_type());
+                               Track *track = new Track(*layout, ctrack->get_type());
                                track->set_position(ground);
 
                                selection.clear();
@@ -487,12 +487,12 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
        {
                if(btn==1)
                {
-                       Track3D *track = pick_track(x, y);
+                       Track *track = pick_track(x, y);
                        if(track)
                        {
                                if(!(mod&Input::MOD_SHIFT))
                                        selection.clear();
-                               selection.toggle_track(&track->get_track());
+                               selection.toggle_track(track);
                        }
                }
        }
@@ -573,19 +573,18 @@ void Designer::track_removed(Track &trk)
                new_tracks.erase(i);
 }
 
-Track3D *Designer::pick_track(int x, int y)
+Track *Designer::pick_track(int x, int y)
 {
-       Layout3D *l = layout_3d;
+       float xx = x*2.0/window.get_width()-1.0;
+       float yy = y*2.0/window.get_height()-1.0;
        if(mode==CATALOGUE)
-               l = cat_layout_3d;
-
-       float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
-       float yy = (float(y)/window.get_height()-0.5)*0.82843;
-       float size = 4.0/window.get_height()*0.82843;
-
-       apply_camera();
-
-       return l->pick_track(xx, yy, size);
+               return catalogue.get_layout().pick_track(Vector(0, 0, 1), Vector(xx*0.05523, yy*0.042421, -0.1));
+       else
+       {
+               const GL::Vector3 &cpos = camera.get_position();
+               GL::Vector4 cray = camera.unproject(GL::Vector4(xx, yy, 0, 0));
+               return layout->pick_track(Vector(cpos.x, cpos.y, cpos.z), Vector(cray.x, cray.y, cray.z));
+       }
 }
 
 void Designer::update_track_icon(Track3D &track)
@@ -683,17 +682,16 @@ void Designer::svg_export_accept(const string &text)
 
 string Designer::tooltip(int x, int y)
 {
-       if(Track3D *t3d = pick_track(x, y))
+       if(Track *track = pick_track(x, y))
        {
-               const Track &track = t3d->get_track();
-               const TrackType &ttype = track.get_type();
+               const TrackType &ttype = track->get_type();
                string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
-               if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
-                       info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
-               if(track.get_turnout_id())
-                       info += format(" (turnout %d)", track.get_turnout_id());
-               else if(track.get_sensor_id())
-                       info += format(" (sensor %d)", track.get_sensor_id());
+               if(mode!=CATALOGUE && abs(track->get_slope())>1e-4)
+                       info += format(" (slope %.1f%%)", abs(track->get_slope()/ttype.get_total_length()*100));
+               if(track->get_turnout_id())
+                       info += format(" (turnout %d)", track->get_turnout_id());
+               else if(track->get_sensor_id())
+                       info += format(" (sensor %d)", track->get_sensor_id());
 
                return info;
        }