1 #include <msp/core/maputils.h>
2 #include <msp/core/raii.h>
3 #include <msp/gltk/button.h>
4 #include <msp/gltk/column.h>
5 #include "libr2c2/layout.h"
6 #include "libr2c2/route.h"
7 #include "libr2c2/trainrouter.h"
9 #include "routerpanel.h"
15 string route_name(const Route *const &route)
17 return route ? route->get_name() : "(none)";
20 RouterPanel::RouterPanel(Engineer &e, Train &t):
29 Loader::WidgetMap widgets;
30 DataFile::load(*this, "data/routerpanel.ui", widgets);
32 lbl_route = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_route"));
33 drp_routes = dynamic_cast<GLtk::Dropdown *>(get_item(widgets, "drp_routes"));
34 drp_routes->set_data(routes);
35 drp_routes->signal_item_selected.connect(sigc::mem_fun(this, &RouterPanel::route_selected));
37 dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_goto"))->signal_clicked.connect(sigc::mem_fun(this, &RouterPanel::goto_clicked));
40 const set<Route *> &lroutes = train.get_layout().get_all<Route>();
41 for(set<Route *>::const_iterator i=lroutes.begin(); i!=lroutes.end(); ++i)
42 if(!(*i)->is_temporary())
45 TrainRouter *router = train.get_ai_of_type<TrainRouter>();
47 router = new TrainRouter(train);
48 update_route(router->get_route());
50 train.signal_ai_event.connect(sigc::mem_fun(this, &RouterPanel::ai_event));
53 void RouterPanel::ai_event(TrainAI &, const TrainAI::Message &msg)
55 if(msg.type=="route-changed")
56 update_route(msg.value.value<const Route *>());
59 void RouterPanel::update_route(const Route *route)
61 SetFlag setf(updating);
63 lbl_route->set_text(route->get_name());
65 lbl_route->set_text("Free run");
66 drp_routes->set_selected_index(routes.find(route));
69 void RouterPanel::route_selected(unsigned index)
73 const Route *route = routes.get(index);
74 train.ai_message(TrainAI::Message("set-route", route));
78 void RouterPanel::goto_clicked()
82 signal_grab_pointer.emit();
85 void RouterPanel::button_press(int x, int y, unsigned btn)
87 Panel::button_press(x, y, btn);
91 signal_ungrab_pointer.emit();
94 delete goto_highlight;
97 if(goto_target && btn==1)
98 train.ai_message(TrainAI::Message("set-destination-block", goto_target));
102 void RouterPanel::pointer_motion(int x, int y)
104 Panel::pointer_motion(x, y);
110 map_coords_to_ancestor(rx, ry, *find_ancestor<GLtk::Root>());
111 Ray ray = engineer.get_main_view().create_ray(rx, ry);
112 Track *track = engineer.get_layout().pick<Track>(ray);
113 if(track && &track->get_block()!=goto_target)
115 goto_target = &track->get_block();
116 delete goto_highlight;
117 goto_highlight = new TrackChain3D(engineer.get_layout_3d(), *goto_target);
118 goto_highlight->set_color(GL::Color(0.7));
119 goto_highlight->set_layer(2);