]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/routerpanel.cpp
Don't crash if a train has no router
[r2c2.git] / source / engineer / routerpanel.cpp
index e69a436d8f10661b97b63d1644d9cd81e2c3de72..30857ceb7f9b0980de21ed0fce8e6fcc4b5493ef 100644 (file)
@@ -14,14 +14,17 @@ using namespace R2C2;
 
 string route_name(const Route *const &route)
 {
-       return route->get_name();
+       return route ? route->get_name() : "(none)";
 }
 
 RouterPanel::RouterPanel(Engineer &e, Train &t):
        engineer(e),
        train(t),
        routes(&route_name),
-       updating(false)
+       updating(false),
+       goto_pick(false),
+       goto_target(0),
+       goto_highlight(0)
 {
        Loader::WidgetMap widgets;
        DataFile::load(*this, "data/routerpanel.ui", widgets);
@@ -33,9 +36,11 @@ RouterPanel::RouterPanel(Engineer &e, Train &t):
 
        dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_goto"))->signal_clicked.connect(sigc::mem_fun(this, &RouterPanel::goto_clicked));
 
+       routes.append(0);
        const set<Route *> &lroutes = train.get_layout().get_all<Route>();
        for(set<Route *>::const_iterator i=lroutes.begin(); i!=lroutes.end(); ++i)
-               routes.append(*i);
+               if(!(*i)->is_temporary())
+                       routes.append(*i);
 
        TrainRouter *router = train.get_ai_of_type<TrainRouter>();
        if(!router)
@@ -72,12 +77,46 @@ void RouterPanel::route_selected(unsigned index)
 
 void RouterPanel::goto_clicked()
 {
-       engineer.pick(false);
-       pick_conn = engineer.signal_pick_done.connect(sigc::mem_fun(this, &RouterPanel::goto_pick_done));
+       goto_pick = true;
+       goto_target = 0;
+       signal_grab_pointer.emit();
 }
 
-void RouterPanel::goto_pick_done(Track *track, unsigned)
+void RouterPanel::button_press(int x, int y, unsigned btn)
 {
-       pick_conn.disconnect();
-       train.ai_message(TrainAI::Message("set-destination-block", &track->get_block()));
+       Panel::button_press(x, y, btn);
+
+       if(goto_pick)
+       {
+               signal_ungrab_pointer.emit();
+               goto_pick = false;
+
+               delete goto_highlight;
+               goto_highlight = 0;
+
+               if(goto_target && btn==1)
+                       train.ai_message(TrainAI::Message("set-destination", static_cast<const TrackChain *>(goto_target)));
+       }
+}
+
+void RouterPanel::pointer_motion(int x, int y)
+{
+       Panel::pointer_motion(x, y);
+       
+       if(goto_pick)
+       {
+               int rx = x;
+               int ry = y;
+               map_coords_to_ancestor(rx, ry, *find_ancestor<GLtk::Root>());
+               Ray ray = engineer.get_main_view().create_ray(rx, ry);
+               Track *track = engineer.get_layout().pick<Track>(ray);
+               if(track && &track->get_block()!=goto_target)
+               {
+                       goto_target = &track->get_block();
+                       delete goto_highlight;
+                       goto_highlight = new TrackChain3D(engineer.get_layout_3d(), *goto_target);
+                       goto_highlight->set_color(GL::Color(0.7));
+                       goto_highlight->set_layer(2);
+               }
+       }
 }