]> git.tdb.fi Git - r2c2.git/blob - source/engineer/routeselect.cpp
ae3dd8340949304b22c4a08bf3ba313307a153b9
[r2c2.git] / source / engineer / routeselect.cpp
1 #include <msp/gltk/label.h>
2 #include "engineer.h"
3 #include "libr2c2/route.h"
4 #include "libr2c2/trainrouter.h"
5 #include "routeselect.h"
6
7 using namespace std;
8 using namespace Msp;
9 using namespace R2C2;
10
11 RouteSelect::RouteSelect(Engineer &e, Train &t):
12         engineer(e),
13         train(t)
14 {
15         set_size(200, 95);
16
17         GLtk::Label *label;
18         add(*(label = new GLtk::Label("Select route")));
19         label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
20
21         add(*(drp_route = new GLtk::Dropdown));
22         drp_route->set_geometry(GLtk::Geometry(10, geom.h-50, geom.w-20, 20));
23         drp_route->append("(none)");
24         drp_route->set_selected_index(0);
25
26         const Route *current_route = 0;
27         if(TrainRouter *router = train.get_ai_of_type<TrainRouter>())
28                 current_route = router->get_route();
29
30         const set<Route *> &routes = engineer.get_layout().get_all<Route>();
31         unsigned n = 1;
32         for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
33                 if(!(*i)->is_temporary())
34                 {
35                         drp_route->append((*i)->get_name());
36                         if(*i==current_route)
37                                 drp_route->set_selected_index(n);
38                         ++n;
39                 }
40 }
41
42 void RouteSelect::on_ok_clicked()
43 {
44         if(drp_route->get_selected_index()>0)
45         {
46                 const set<Route *> &routes = engineer.get_layout().get_all<Route>();
47                 set<Route *>::const_iterator i = routes.begin();
48                 unsigned n = drp_route->get_selected_index()-1;
49                 while(i!=routes.end())
50                 {
51                         if(!(*i)->is_temporary())
52                         {
53                                 if(!n)
54                                         break;
55                                 --n;
56                         }
57                         ++i;
58                 }
59
60                 train.ai_message(TrainAI::Message("set-route", *i));
61         }
62         else
63                 train.ai_message(TrainAI::Message("clear-route"));
64 }