]> git.tdb.fi Git - r2c2.git/blob - source/engineer/routerpanel.cpp
5097edaedf84046fc63765322c4cf8d6c1288a7f
[r2c2.git] / source / engineer / routerpanel.cpp
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"
8 #include "engineer.h"
9 #include "routerpanel.h"
10
11 using namespace std;
12 using namespace Msp;
13 using namespace R2C2;
14
15 string route_name(const Route *const &route)
16 {
17         return route->get_name();
18 }
19
20 RouterPanel::RouterPanel(Engineer &e, Train &t):
21         engineer(e),
22         train(t),
23         routes(&route_name),
24         updating(false),
25         goto_pick(false),
26         goto_target(0),
27         goto_highlight(0)
28 {
29         Loader::WidgetMap widgets;
30         DataFile::load(*this, "data/routerpanel.ui", widgets);
31
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));
36
37         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_goto"))->signal_clicked.connect(sigc::mem_fun(this, &RouterPanel::goto_clicked));
38
39         const set<Route *> &lroutes = train.get_layout().get_all<Route>();
40         for(set<Route *>::const_iterator i=lroutes.begin(); i!=lroutes.end(); ++i)
41                 routes.append(*i);
42
43         TrainRouter *router = train.get_ai_of_type<TrainRouter>();
44         if(!router)
45                 router = new TrainRouter(train);
46         update_route(router->get_route());
47
48         train.signal_ai_event.connect(sigc::mem_fun(this, &RouterPanel::ai_event));
49 }
50
51 void RouterPanel::ai_event(TrainAI &, const TrainAI::Message &msg)
52 {
53         if(msg.type=="route-changed")
54                 update_route(msg.value.value<const Route *>());
55 }
56
57 void RouterPanel::update_route(const Route *route)
58 {
59         SetFlag setf(updating);
60         if(route)
61                 lbl_route->set_text(route->get_name());
62         else
63                 lbl_route->set_text("Free run");
64         drp_routes->set_selected_index(routes.find(route));
65 }
66
67 void RouterPanel::route_selected(unsigned index)
68 {
69         if(!updating)
70         {
71                 const Route *route = routes.get(index);
72                 train.ai_message(TrainAI::Message("set-route", route));
73         }
74 }
75
76 void RouterPanel::goto_clicked()
77 {
78         goto_pick = true;
79         signal_grab_pointer.emit();
80 }
81
82 void RouterPanel::button_press(int x, int y, unsigned btn)
83 {
84         Panel::button_press(x, y, btn);
85
86         if(goto_pick)
87         {
88                 signal_ungrab_pointer.emit();
89                 goto_pick = false;
90
91                 delete goto_highlight;
92                 goto_highlight = 0;
93
94                 if(goto_target && btn==1)
95                         train.ai_message(TrainAI::Message("set-destination-block", goto_target));
96         }
97 }
98
99 void RouterPanel::pointer_motion(int x, int y)
100 {
101         Panel::pointer_motion(x, y);
102         
103         if(goto_pick)
104         {
105                 int rx = x;
106                 int ry = y;
107                 map_coords_to_ancestor(rx, ry, *find_ancestor<GLtk::Root>());
108                 Ray ray = engineer.get_main_view().create_ray(rx, ry);
109                 Track *track = engineer.get_layout().pick<Track>(ray);
110                 if(track)
111                 {
112                         goto_target = &track->get_block();
113                         delete goto_highlight;
114                         goto_highlight = new TrackChain3D(engineer.get_layout_3d(), *goto_target);
115                         goto_highlight->set_color(GL::Color(0.7));
116                         goto_highlight->set_layer(2);
117                 }
118         }
119 }