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