]> git.tdb.fi Git - r2c2.git/blob - routeselect.cpp
00bf6cfdcd19cf79d97c316df2def12be7c24785
[r2c2.git] / routeselect.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gltk/label.h>
9 #include "engineer.h"
10 #include "libr2c2/route.h"
11 #include "routeselect.h"
12
13 using namespace std;
14 using namespace Msp;
15 using namespace R2C2;
16
17 RouteSelect::RouteSelect(Engineer &e, Train &t):
18         engineer(e),
19         train(t)
20 {
21         set_size(200, 95);
22
23         GLtk::Label *label;
24         add(*(label = new GLtk::Label("Select route")));
25         label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
26
27         add(*(drp_route = new GLtk::Dropdown));
28         drp_route->set_geometry(GLtk::Geometry(10, geom.h-50, geom.w-20, 20));
29         drp_route->append("(none)");
30         drp_route->set_selected_index(0);
31         const set<Route *> &routes = engineer.get_layout().get_routes();
32         unsigned n = 1;
33         for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
34                 if(!(*i)->is_temporary())
35                 {
36                         drp_route->append((*i)->get_name());
37                         if(*i==train.get_route())
38                                 drp_route->set_selected_index(n);
39                         ++n;
40                 }
41 }
42
43 void RouteSelect::on_ok_clicked()
44 {
45         if(drp_route->get_selected_index()>0)
46         {
47                 const set<Route *> &routes = engineer.get_layout().get_routes();
48                 set<Route *>::const_iterator i = routes.begin();
49                 unsigned n = drp_route->get_selected_index()-1;
50                 while(i!=routes.end())
51                 {
52                         if(!(*i)->is_temporary())
53                         {
54                                 if(!n)
55                                         break;
56                                 --n;
57                         }
58                         ++i;
59                 }
60                 
61                 if(!train.set_route(*i))
62                         engineer.set_status("Could not set route");
63         }
64         else
65                 train.set_route(0);
66 }