]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainproperties.cpp
Further adjustments to changes in GLtk
[r2c2.git] / source / engineer / trainproperties.cpp
1 #include <msp/gltk/label.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/lexicalcast.h>
4 #include "libr2c2/driver.h"
5 #include "libr2c2/trainrouter.h"
6 #include "libr2c2/vehicle.h"
7 #include "libr2c2/vehicletype.h"
8 #include "engineer.h"
9 #include "trainproperties.h"
10
11 using namespace std;
12 using namespace Msp;
13 using namespace R2C2;
14
15 TrainProperties::TrainProperties(Engineer &e, Train *t):
16         engineer(e),
17         train(t)
18 {
19         set_size(250, 305);
20
21         GLtk::Label *label;
22         add(*(label = new GLtk::Label("Train properties")));
23         label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
24
25         add(*(ent_addr = new GLtk::Entry));
26         ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
27
28         add(*(drp_protocol = new GLtk::Dropdown));
29         drp_protocol->set_geometry(GLtk::Geometry(60, geom.h-50, 60, 20));
30         for(unsigned i=0;; ++i)
31         {
32                 if(const char *proto = engineer.get_layout().get_driver().enumerate_protocols(i))
33                         drp_protocol->append(proto);
34                 else
35                         break;
36         }
37         drp_protocol->set_selected_index(0);
38
39         add(*(drp_type = new GLtk::Dropdown));
40         drp_type->set_geometry(GLtk::Geometry(130, geom.h-50, geom.w-140, 20));
41
42         const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
43         unsigned n = 0;
44         for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
45         {
46                 if(!i->second->is_locomotive())
47                         continue;
48
49                 drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
50                 if(train && i->second==&train->get_locomotive_type())
51                         drp_type->set_selected_index(n);
52
53                 ++n;
54         }
55
56         add(*(ent_name = new GLtk::Entry));
57         ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
58
59         add(*(drp_priority = new GLtk::Dropdown));
60         drp_priority->set_geometry(GLtk::Geometry(10, geom.h-100, geom.w-20, 20));
61         drp_priority->append("Standard freight");
62         drp_priority->append("Express freight");
63         drp_priority->append("Unspecified");
64         drp_priority->append("Standard passenger");
65         drp_priority->append("Express passenger");
66
67         add(*(lst_vehicles = new GLtk::List));
68         lst_vehicles->set_geometry(GLtk::Geometry(10, 100, geom.w-20, geom.h-205));
69
70         GLtk::Button *btn;
71
72         add(*(btn = new GLtk::Button("Rem")));
73         btn->set_geometry(GLtk::Geometry(10, 70, 40, 25));
74         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::remove_vehicle_clicked));
75
76         add(*(drp_new_vehicle = new GLtk::Dropdown));
77         drp_new_vehicle->set_geometry(GLtk::Geometry(10, 45, geom.w-20, 20));
78         drp_new_vehicle->append("(new vehicle)");
79         drp_new_vehicle->set_selected_index(0);
80         for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
81         {
82                 if(i->second->is_locomotive())
83                         continue;
84
85                 drp_new_vehicle->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
86         }
87         drp_new_vehicle->signal_item_selected.connect(sigc::mem_fun(this, &TrainProperties::new_vehicle_selected));
88
89         if(train)
90         {
91                 ent_addr->set_text(lexical_cast<string>(train->get_address()));
92                 ent_name->set_text(train->get_name());
93                 if(TrainRouter *router = train->get_ai_of_type<TrainRouter>())
94                         drp_priority->set_selected_index(router->get_priority()+2);
95
96                 unsigned n_vehicles = train->get_n_vehicles();
97                 for(unsigned i=1; i<n_vehicles; ++i)
98                 {
99                         const VehicleType &type = train->get_vehicle(i).get_type();
100                         lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
101                 }
102         }
103         else
104         {
105                 ent_name->set_text(format("Train %d", engineer.get_layout().get_trains().size()+1));
106                 drp_priority->set_selected_index(2);
107         }
108 }
109
110 void TrainProperties::on_ok_clicked()
111 {
112         if(!train)
113         {
114                 const VehicleType &type = get_vehicle_type(drp_type->get_selected_index(), true);
115                 unsigned addr = lexical_cast<unsigned>(ent_addr->get_text());
116                 train = new Train(engineer.get_layout(), type, addr, drp_protocol->get_selected());
117         }
118
119         train->set_name(ent_name->get_text());
120         if(TrainRouter *router = train->get_ai_of_type<TrainRouter>())
121                 router->set_priority(drp_priority->get_selected_index()-2);
122
123         // The locomotive is vehicle 0 so we need to add 1
124         for(set<unsigned>::const_iterator i=rem_vehicles.end(); i!=rem_vehicles.begin();)
125                 train->remove_vehicle(*--i+1);
126         for(vector<const VehicleType *>::const_iterator i=add_vehicles.begin(); i!=add_vehicles.end(); ++i)
127                 train->add_vehicle(**i);
128 }
129
130 void TrainProperties::new_vehicle_selected(unsigned n, const string &)
131 {
132         if(n==0)
133                 return;
134
135         const VehicleType &type = get_vehicle_type(n-1, false);
136         add_vehicles.push_back(&type);
137         lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
138
139         drp_new_vehicle->set_selected_index(0);
140 }
141
142 void TrainProperties::remove_vehicle_clicked()
143 {
144         int selected = lst_vehicles->get_selected_index();
145         if(selected<0)
146                 return;
147
148         lst_vehicles->remove(selected);
149
150         unsigned n_vehicles = (train ? train->get_n_vehicles()-rem_vehicles.size() : 0);
151         if(static_cast<unsigned>(selected)>=n_vehicles)
152                 add_vehicles.erase(add_vehicles.begin()+(selected-n_vehicles));
153         else
154         {
155                 for(set<unsigned>::const_iterator i=rem_vehicles.begin(); i!=rem_vehicles.end(); ++i)
156                         if(*i<=static_cast<unsigned>(selected))
157                                 ++selected;
158                 rem_vehicles.insert(selected);
159         }
160 }
161
162 const VehicleType &TrainProperties::get_vehicle_type(unsigned n, bool loco)
163 {
164         const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
165         Catalogue::VehicleMap::const_iterator i = vehs.begin();
166         while(i!=vehs.end())
167         {
168                 if(i->second->is_locomotive()==loco)
169                 {
170                         if(!n)
171                                 return *i->second;
172                         --n;
173                 }
174                 ++i;
175         }
176
177         throw out_of_range("TrainProperties::get_vehicle_type");
178 }