3 This file is part of R²C²
4 Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
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"
15 #include "routeselect.h"
16 #include "timetabledialog.h"
17 #include "trainpanel.h"
18 #include "trainproperties.h"
19 #include "trainview.h"
25 TrainPanel::TrainPanel(Engineer &e, Train &t):
33 AIControl *ai = new AIControl(train);
34 ai->signal_event.connect(sigc::mem_fun(this, &TrainPanel::ai_event));
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));
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));
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));
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));
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));
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));
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));
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);
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));
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));
86 const map<unsigned, string> &funcs = train.get_locomotive_type().get_functions();
88 for(map<unsigned, string>::const_iterator i=funcs.begin(); i!=funcs.end(); ++i, x+=36)
90 string fname = i->second;
91 fname[0] = toupper(fname[0]);
93 pnl_extra->add(*(tgl = new GLtk::Toggle));
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));
99 tgl_funcs[i->first] = tgl;
101 train.signal_function_changed.connect(sigc::mem_fun(this, &TrainPanel::train_function_changed));
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));
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));
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));
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));
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));
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));
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));
134 void TrainPanel::expand(bool e)
137 pnl_extra->set_visible(expanded);
140 set_size(geom.w, 205);
141 btn_expand->set_style("arrow_up");
145 set_size(geom.w, 65);
146 btn_expand->set_style("arrow_down");
148 pnl_basic->set_geometry(GLtk::Geometry(0, geom.h-58, geom.w, 45));
149 engineer.rearrange_panels();
152 void TrainPanel::ai_event(const TrainAI::Message &msg)
154 if(msg.type=="target-speed-changed")
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));
160 else if(msg.type=="reverse-changed")
161 tgl_forward->set_value(!msg.value.value<bool>());
164 void TrainPanel::train_function_changed(unsigned func, bool value)
166 map<unsigned, GLtk::Toggle *>::iterator i = tgl_funcs.find(func);
167 if(i!=tgl_funcs.end())
168 i->second->set_value(value);
171 void TrainPanel::train_route_changed(const Route *r)
174 lbl_route->set_text(r->get_name());
176 lbl_route->set_text("Free run");
179 void TrainPanel::train_status_changed(const string &s)
181 lbl_status->set_text(s);
184 void TrainPanel::place_clicked()
187 pick_conn = engineer.signal_pick_done.connect(sigc::mem_fun(this, &TrainPanel::place));
190 void TrainPanel::take_clicked()
195 void TrainPanel::edit_clicked()
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);
202 void TrainPanel::route_clicked()
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);
209 void TrainPanel::goto_clicked()
211 engineer.pick(false);
212 pick_conn = engineer.signal_pick_done.connect(sigc::mem_fun(this, &TrainPanel::go_to));
215 void TrainPanel::timetable_clicked()
217 Timetable *timetable = dynamic_cast<Timetable *>(train.get_tagged_ai("timetable"));
220 timetable = new Timetable(train);
221 timetable->set_tag("timetable");
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);
229 void TrainPanel::view_clicked()
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);
236 void TrainPanel::expand_clicked()
241 void TrainPanel::speed_slider_changed(double value)
243 float speed = value/3.6*engineer.get_layout().get_catalogue().get_scale();
244 train.ai_message(TrainAI::Message("set-target-speed", speed));
247 void TrainPanel::forward_toggled(bool value)
249 train.ai_message(TrainAI::Message("set-reverse", !value));
252 void TrainPanel::func_toggled(bool value, unsigned func)
254 train.set_function(func, value);
257 void TrainPanel::place(Track *track, unsigned ep)
259 pick_conn.disconnect();
261 Block &block = track->get_block();
262 TrackIter iter(track, ep);
264 while(block.has_track(*iter))
266 const vector<Block::Endpoint> &eps = block.get_endpoints();
268 for(unsigned i=0; (!ok && i<eps.size()); ++i)
269 if(eps[i].track==iter.track() && eps[i].track_ep==iter.entry())
271 train.place(block, i);
278 iter = iter.flip().reverse();
282 void TrainPanel::go_to(Track *track, unsigned)
284 pick_conn.disconnect();
286 if(!train.go_to(*track))
287 engineer.set_status("Could not set route");