]> git.tdb.fi Git - r2c2.git/blob - source/engineer/traindialog.cpp
Rewrite goal initialization for TrainRouteMetric
[r2c2.git] / source / engineer / traindialog.cpp
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 "timetablepanel.h"
14 #include "traindialog.h"
15 #include "vehiclespanel.h"
16
17 using namespace std;
18 using namespace Msp;
19 using namespace R2C2;
20
21 TrainDialog::TrainDialog(Engineer &e, R2C2::Train &t):
22         engineer(e),
23         train(t),
24         updating(false)
25 {
26         Loader::WidgetMap widgets;
27         DataFile::load(*this, "data/traindialog.ui", widgets);
28
29         lbl_title = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_title"));
30         lbl_title->set_text(t.get_name());
31
32         btn_expand = dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_expand"));
33         btn_expand->signal_clicked.connect(sigc::mem_fun(this, &TrainDialog::expand_clicked));
34         lbl_speed = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_speed"));
35         sld_speed = dynamic_cast<GLtk::Slider *>(get_item(widgets, "sld_speed"));
36         float max_speed = train.get_maximum_speed();
37         if(max_speed>0)
38                 sld_speed->set_range(0, max_speed*3.6/train.get_layout().get_catalogue().get_scale());
39         sld_speed->signal_value_changed.connect(sigc::mem_fun(this, &TrainDialog::ui_speed_changed));
40         tgl_forward = dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_forward"));
41         tgl_forward->signal_toggled.connect(sigc::mem_fun(this, &TrainDialog::ui_forward_toggled));
42         lbl_status = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_status"));
43         pnl_expander = dynamic_cast<GLtk::Panel *>(get_item(widgets, "pnl_expander"));
44
45         Panel *pnl;
46
47         GLtk::Stack stack(*pnl_expander->get_layout());
48         stack.arrange(*get_item(widgets, "lbl_detail_placeholder"));
49
50         pnl_expander->add(*(pnl = new VehiclesPanel(train)));
51         panels.push_back(pnl);
52         dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_vehicles"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainDialog::toggle_panel), pnl));
53
54         pnl_expander->add(*(pnl = new ControlPanel(engineer, train)));
55         pnl->set_visible(false);
56         panels.push_back(pnl);
57         dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_control"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainDialog::toggle_panel), pnl));
58
59         pnl_expander->add(*(pnl = new RouterPanel(engineer, train)));
60         pnl->set_visible(false);
61         panels.push_back(pnl);
62         dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_router"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainDialog::toggle_panel), pnl));
63
64         pnl_expander->add(*(pnl = new TimetablePanel(engineer, train)));
65         pnl->set_visible(false);
66         panels.push_back(pnl);
67         dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_timetable"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainDialog::toggle_panel), pnl));
68
69         AIControl *control = train.get_ai_of_type<AIControl>();
70         if(!control)
71                 control = new AIControl(train);
72         update_forward(!control->get_reverse());
73         update_speed_display(control->get_target_speed());
74
75         TrainStatus *status = train.get_ai_of_type<TrainStatus>();
76         if(!status)
77                 status = new TrainStatus(train);
78         lbl_status->set_text(status->get_status());
79
80         train.signal_ai_event.connect(sigc::mem_fun(this, &TrainDialog::ai_event));
81 }
82
83 void TrainDialog::autosize_special(const GLtk::Part &part, GLtk::Geometry &ageom) const
84 {
85         GLtk::Dialog::autosize_special(part, ageom);
86
87         if(part.get_name()=="children")
88         {
89                 GLtk::Geometry egeom;
90                 pnl_expander->autosize(egeom);
91                 const GLtk::Sides &margin = layout->get_margin();
92                 ageom.w = max(ageom.w, margin.left+margin.right+egeom.w);
93         }
94 }
95
96 void TrainDialog::ai_event(TrainAI &, const TrainAI::Message &msg)
97 {
98         if(msg.type=="status-changed")
99                 lbl_status->set_text(msg.value.value<string>());
100         else if(msg.type=="target-speed-changed")
101                 update_speed_display(msg.value.value<float>());
102         else if(msg.type=="reverse-changed")
103                 update_forward(!msg.value.value<bool>());
104 }
105
106 void TrainDialog::update_speed_display(float speed)
107 {
108         SetFlag setf(updating);
109         float scale_speed = speed*3.6/train.get_layout().get_catalogue().get_scale();
110         lbl_speed->set_text(format("%3.0f", scale_speed));
111         sld_speed->set_value(scale_speed);
112 }
113
114 void TrainDialog::ui_speed_changed(double value)
115 {
116         if(!updating)
117         {
118                 float real_speed = value/3.6*train.get_layout().get_catalogue().get_scale();
119                 train.ai_message(TrainAI::Message("set-target-speed", real_speed));
120         }
121 }
122
123 void TrainDialog::update_forward(bool value)
124 {
125         SetFlag setf(updating);
126         tgl_forward->set_value(value);
127 }
128
129 void TrainDialog::ui_forward_toggled(bool value)
130 {
131         if(!updating)
132                 train.ai_message(TrainAI::Message("set-reverse", !value));
133 }
134
135 void TrainDialog::expand_clicked()
136 {
137         pnl_expander->set_visible(!pnl_expander->is_visible());
138         btn_expand->set_style(pnl_expander->is_visible() ? "arrow_up" : "arrow_down");
139         signal_autosize_changed.emit();
140         find_ancestor<GLtk::Root>()->get_layout()->update();
141 }
142
143 void TrainDialog::toggle_panel(bool show, GLtk::Panel *panel)
144 {
145         panel->set_visible(show);
146 }