]> git.tdb.fi Git - r2c2.git/blob - trainpanel.cpp
51e7a88fe8e820f54b76e03a1a0f94749c170365
[r2c2.git] / trainpanel.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2006-2011  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include <msp/strings/formatter.h>
10 #include "libr2c2/aicontrol.h"
11 #include "libr2c2/timetable.h"
12 #include "libr2c2/trackiter.h"
13 #include "libr2c2/vehicletype.h"
14 #include "engineer.h"
15 #include "routeselect.h"
16 #include "timetabledialog.h"
17 #include "trainpanel.h"
18 #include "trainproperties.h"
19 #include "trainview.h"
20
21 using namespace std;
22 using namespace Msp;
23 using namespace R2C2;
24
25 TrainPanel::TrainPanel(Engineer &e, Train &t):
26         engineer(e),
27         train(t),
28         status(train),
29         expanded(false)
30 {
31         set_size(200, 65);
32
33         AIControl *ai = new AIControl(train);
34         ai->signal_event.connect(sigc::mem_fun(this, &TrainPanel::ai_event));
35
36         add(*(pnl_basic = new GLtk::Panel));
37         pnl_basic->set_style("group");
38         pnl_basic->set_geometry(GLtk::Geometry(0, geom.h-58, geom.w, 45));
39
40         pnl_basic->add(*(lbl_addr = new GLtk::Label(format("%2d", train.get_address()))));
41         lbl_addr->set_style("digital");
42         lbl_addr->set_geometry(GLtk::Geometry(10, 28, 35, 20));
43
44         pnl_basic->add(*(lbl_name = new GLtk::Label(train.get_name())));
45         lbl_name->set_style("digital");
46         lbl_name->set_geometry(GLtk::Geometry(50, 28, geom.w-77, 20));
47         train.signal_name_changed.connect(sigc::mem_fun(lbl_name, &GLtk::Label::set_text));
48
49         pnl_basic->add(*(lbl_speed = new GLtk::Label("  0")));
50         lbl_speed->set_style("digital");
51         lbl_speed->set_geometry(GLtk::Geometry(10, 3, 35, 20));
52
53         pnl_basic->add(*(sld_speed = new GLtk::HSlider));
54         sld_speed->set_geometry(GLtk::Geometry(50, 8, geom.w-80, 10));
55         sld_speed->set_range(0, 200);
56         sld_speed->set_step(5);
57         sld_speed->signal_value_changed.connect(sigc::mem_fun(this, &TrainPanel::speed_slider_changed));
58
59         pnl_basic->add(*(tgl_forward = new GLtk::Toggle));
60         tgl_forward->set_text("Fwd");
61         tgl_forward->set_geometry(GLtk::Geometry(geom.w-30, 0, 20, 27));
62         tgl_forward->set_value(true);
63         tgl_forward->signal_toggled.connect(sigc::mem_fun(this, &TrainPanel::forward_toggled));
64
65         pnl_basic->add(*(btn_expand = new GLtk::Button));
66         btn_expand->set_style("arrow_down");
67         btn_expand->set_geometry(GLtk::Geometry(geom.w-22, 28, 12, 20));
68         btn_expand->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::expand_clicked));
69
70         add(*(pnl_extra = new GLtk::Panel));
71         pnl_extra->set_style("group");
72         pnl_extra->set_geometry(GLtk::Geometry(0, 10, geom.w, 135));
73         pnl_extra->set_visible(false);
74
75         const Route *route = train.get_route();
76         pnl_extra->add(*(lbl_route = new GLtk::Label((route ? route->get_name() : "Free run"))));
77         lbl_route->set_style("digital");
78         lbl_route->set_geometry(GLtk::Geometry(10, 85, geom.w-20, 20));
79         train.signal_route_changed.connect(sigc::mem_fun(this, &TrainPanel::train_route_changed));
80
81         pnl_extra->add(*(lbl_status = new GLtk::Label(status.get_status())));
82         lbl_status->set_style("digital");
83         lbl_status->set_geometry(GLtk::Geometry(10, 60, geom.w-20, 20));
84         status.signal_changed.connect(sigc::mem_fun(this, &TrainPanel::train_status_changed));
85
86         const map<unsigned, string> &funcs = train.get_locomotive_type().get_functions();
87         unsigned x = 10;
88         for(map<unsigned, string>::const_iterator i=funcs.begin(); i!=funcs.end(); ++i, x+=36)
89         {
90                 string fname = i->second;
91                 fname[0] = toupper(fname[0]);
92                 GLtk::Toggle *tgl;
93                 pnl_extra->add(*(tgl = new GLtk::Toggle));
94                 tgl->set_text(fname);
95                 tgl->set_geometry(GLtk::Geometry(x, 108, 36, 27));
96                 tgl->set_value(train.get_function(i->first));
97                 tgl->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainPanel::func_toggled), i->first));
98
99                 tgl_funcs[i->first] = tgl;
100         }
101         train.signal_function_changed.connect(sigc::mem_fun(this, &TrainPanel::train_function_changed));
102
103         GLtk::Button *btn;
104
105         pnl_extra->add(*(btn = new GLtk::Button("Edit")));
106         btn->set_geometry(GLtk::Geometry(10, 30, 36, 25));
107         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::edit_clicked));
108
109         pnl_extra->add(*(btn = new GLtk::Button("Place")));
110         btn->set_geometry(GLtk::Geometry(10, 0, 36, 25));
111         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::place_clicked));
112
113         pnl_extra->add(*(btn = new GLtk::Button("Take")));
114         btn->set_geometry(GLtk::Geometry(46, 0, 36, 25));
115         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::take_clicked));
116
117         pnl_extra->add(*(btn = new GLtk::Button("GoTo")));
118         btn->set_geometry(GLtk::Geometry(100, 0, 36, 25));
119         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::goto_clicked));
120
121         pnl_extra->add(*(btn = new GLtk::Button("Route")));
122         btn->set_geometry(GLtk::Geometry(100, 30, 36, 25));
123         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::route_clicked));
124
125         pnl_extra->add(*(btn = new GLtk::Button("TTbl")));
126         btn->set_geometry(GLtk::Geometry(46, 30, 36, 25));
127         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::timetable_clicked));
128
129         pnl_extra->add(*(btn = new GLtk::Button("View")));
130         btn->set_geometry(GLtk::Geometry(geom.w-46, 30, 36, 25));
131         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::view_clicked));
132 }
133
134 void TrainPanel::expand(bool e)
135 {
136         expanded = e;
137         pnl_extra->set_visible(expanded);
138         if(expanded)
139         {
140                 set_size(geom.w, 205);
141                 btn_expand->set_style("arrow_up");
142         }
143         else
144         {
145                 set_size(geom.w, 65);
146                 btn_expand->set_style("arrow_down");
147         }
148         pnl_basic->set_geometry(GLtk::Geometry(0, geom.h-58, geom.w, 45));
149         engineer.rearrange_panels();
150 }
151
152 void TrainPanel::ai_event(const TrainAI::Message &msg)
153 {
154         if(msg.type=="target-speed-changed")
155         {
156                 float speed = msg.value.value<float>()/engineer.get_layout().get_catalogue().get_scale()*3.6;
157                 sld_speed->set_value(speed);
158                 lbl_speed->set_text(format("%3.0f", speed));
159         }
160         else if(msg.type=="reverse-changed")
161                 tgl_forward->set_value(!msg.value.value<bool>());
162 }
163
164 void TrainPanel::train_function_changed(unsigned func, bool value)
165 {
166         map<unsigned, GLtk::Toggle *>::iterator i = tgl_funcs.find(func);
167         if(i!=tgl_funcs.end())
168                 i->second->set_value(value);
169 }
170
171 void TrainPanel::train_route_changed(const Route *r)
172 {
173         if(r)
174                 lbl_route->set_text(r->get_name());
175         else
176                 lbl_route->set_text("Free run");
177 }
178
179 void TrainPanel::train_status_changed(const string &s)
180 {
181         lbl_status->set_text(s);
182 }
183
184 void TrainPanel::place_clicked()
185 {
186         engineer.pick(true);
187         pick_conn = engineer.signal_pick_done.connect(sigc::mem_fun(this, &TrainPanel::place));
188 }
189
190 void TrainPanel::take_clicked()
191 {
192         train.unplace();
193 }
194
195 void TrainPanel::edit_clicked()
196 {
197         TrainProperties *dialog = new TrainProperties(engineer, &train);
198         engineer.get_root().add(*dialog);
199         dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h);
200 }
201
202 void TrainPanel::route_clicked()
203 {
204         RouteSelect *dialog = new RouteSelect(engineer, train);
205         engineer.get_root().add(*dialog);
206         dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h);
207 }
208
209 void TrainPanel::goto_clicked()
210 {
211         engineer.pick(false);
212         pick_conn = engineer.signal_pick_done.connect(sigc::mem_fun(this, &TrainPanel::go_to));
213 }
214
215 void TrainPanel::timetable_clicked()
216 {
217         Timetable *timetable = dynamic_cast<Timetable *>(train.get_tagged_ai("timetable"));
218         if(!timetable)
219         {
220                 timetable = new Timetable(train);
221                 timetable->set_tag("timetable");
222         }
223
224         TimetableDialog *dialog = new TimetableDialog(*timetable);
225         engineer.get_root().add(*dialog);
226         dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h);
227 }
228
229 void TrainPanel::view_clicked()
230 {
231         TrainView *dialog = new TrainView(engineer, train);
232         engineer.get_root().add(*dialog);
233         dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h);
234 }
235
236 void TrainPanel::expand_clicked()
237 {
238         expand(!expanded);
239 }
240
241 void TrainPanel::speed_slider_changed(double value)
242 {
243         float speed = value/3.6*engineer.get_layout().get_catalogue().get_scale();
244         train.ai_message(TrainAI::Message("set-target-speed", speed));
245 }
246
247 void TrainPanel::forward_toggled(bool value)
248 {
249         train.ai_message(TrainAI::Message("set-reverse", !value));
250 }
251
252 void TrainPanel::func_toggled(bool value, unsigned func)
253 {
254         train.set_function(func, value);
255 }
256
257 void TrainPanel::place(Track *track, unsigned ep)
258 {
259         pick_conn.disconnect();
260
261         Block &block = track->get_block();
262         TrackIter iter(track, ep);
263
264         while(block.has_track(*iter))
265         {
266                 const vector<Block::Endpoint> &eps = block.get_endpoints();
267                 bool ok = false;
268                 for(unsigned i=0; (!ok && i<eps.size()); ++i)
269                         if(eps[i].track==iter.track() && eps[i].track_ep==iter.entry())
270                         {
271                                 train.place(block, i);
272                                 ok = true;
273                         }
274
275                 if(ok)
276                         break;
277
278                 iter = iter.flip().reverse();
279         }
280 }
281
282 void TrainPanel::go_to(Track *track, unsigned)
283 {
284         pick_conn.disconnect();
285
286         if(!train.go_to(*track))
287                 engineer.set_status("Could not set route");
288 }