1 #include <msp/core/maputils.h>
2 #include <msp/core/raii.h>
3 #include <msp/gltk/part.h>
4 #include <msp/gltk/root.h>
5 #include <msp/gltk/stack.h>
6 #include <msp/strings/format.h>
7 #include "libr2c2/aicontrol.h"
8 #include "libr2c2/catalogue.h"
9 #include "libr2c2/layout.h"
10 #include "libr2c2/trainstatus.h"
11 #include "controlpanel.h"
12 #include "routerpanel.h"
13 #include "traindialog.h"
14 #include "vehiclespanel.h"
20 TrainDialog::TrainDialog(Engineer &e, R2C2::Train &t):
25 Loader::WidgetMap widgets;
26 DataFile::load(*this, "data/traindialog.ui", widgets);
28 lbl_title = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_title"));
29 lbl_title->set_text(t.get_name());
31 btn_expand = dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_expand"));
32 btn_expand->signal_clicked.connect(sigc::mem_fun(this, &TrainDialog::expand_clicked));
33 lbl_speed = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_speed"));
34 sld_speed = dynamic_cast<GLtk::Slider *>(get_item(widgets, "sld_speed"));
35 sld_speed->signal_value_changed.connect(sigc::mem_fun(this, &TrainDialog::ui_speed_changed));
36 tgl_forward = dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_forward"));
37 tgl_forward->signal_toggled.connect(sigc::mem_fun(this, &TrainDialog::ui_forward_toggled));
38 lbl_status = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_status"));
39 pnl_expander = dynamic_cast<GLtk::Panel *>(get_item(widgets, "pnl_expander"));
43 GLtk::Stack stack(*pnl_expander->get_layout());
44 stack.arrange(*get_item(widgets, "lbl_detail_placeholder"));
46 pnl_expander->add(*(pnl = new VehiclesPanel(train)));
47 panels.push_back(pnl);
48 dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_vehicles"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainDialog::toggle_panel), pnl));
50 pnl_expander->add(*(pnl = new ControlPanel(engineer, train)));
51 pnl->set_visible(false);
52 panels.push_back(pnl);
53 dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_control"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainDialog::toggle_panel), pnl));
55 pnl_expander->add(*(pnl = new RouterPanel(engineer, train)));
56 pnl->set_visible(false);
57 panels.push_back(pnl);
58 dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_router"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainDialog::toggle_panel), pnl));
60 AIControl *control = train.get_ai_of_type<AIControl>();
62 control = new AIControl(train);
63 update_forward(!control->get_reverse());
64 update_speed_display(control->get_target_speed());
66 TrainStatus *status = train.get_ai_of_type<TrainStatus>();
68 status = new TrainStatus(train);
69 lbl_status->set_text(status->get_status());
71 train.signal_ai_event.connect(sigc::mem_fun(this, &TrainDialog::ai_event));
74 void TrainDialog::autosize_special(const GLtk::Part &part, GLtk::Geometry &ageom) const
76 GLtk::Dialog::autosize_special(part, ageom);
78 if(part.get_name()=="children")
81 pnl_expander->autosize(egeom);
82 const GLtk::Sides &margin = layout->get_margin();
83 ageom.w = max(ageom.w, margin.left+margin.right+egeom.w);
87 void TrainDialog::ai_event(TrainAI &, const TrainAI::Message &msg)
89 if(msg.type=="status-changed")
90 lbl_status->set_text(msg.value.value<string>());
91 else if(msg.type=="target-speed-changed")
92 update_speed_display(msg.value.value<float>());
93 else if(msg.type=="reverse-changed")
94 update_forward(!msg.value.value<bool>());
97 void TrainDialog::update_speed_display(float speed)
99 SetFlag setf(updating);
100 float scale_speed = speed*3.6/train.get_layout().get_catalogue().get_scale();
101 lbl_speed->set_text(format("%3.0f", scale_speed));
102 sld_speed->set_value(scale_speed);
105 void TrainDialog::ui_speed_changed(double value)
109 float real_speed = value/3.6*train.get_layout().get_catalogue().get_scale();
110 train.ai_message(TrainAI::Message("set-target-speed", real_speed));
114 void TrainDialog::update_forward(bool value)
116 SetFlag setf(updating);
117 tgl_forward->set_value(value);
120 void TrainDialog::ui_forward_toggled(bool value)
123 train.ai_message(TrainAI::Message("set-reverse", !value));
126 void TrainDialog::expand_clicked()
128 pnl_expander->set_visible(!pnl_expander->is_visible());
129 btn_expand->set_style(pnl_expander->is_visible() ? "arrow_up" : "arrow_down");
130 signal_autosize_changed.emit();
131 find_ancestor<GLtk::Root>()->get_layout()->update();
134 void TrainDialog::toggle_panel(bool show, GLtk::Panel *panel)
136 panel->set_visible(show);