]> git.tdb.fi Git - r2c2.git/blob - source/engineer/controlpanel.cpp
Complete rewrite of the engineer UI
[r2c2.git] / source / engineer / controlpanel.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/core/raii.h>
3 #include <msp/gltk/button.h>
4 #include <msp/gltk/label.h>
5 #include <msp/gltk/row.h>
6 #include "libr2c2/vehicle.h"
7 #include "controlpanel.h"
8 #include "engineer.h"
9 #include "trainview.h"
10
11 using namespace std;
12 using namespace Msp;
13 using namespace R2C2;
14
15 ControlPanel::ControlPanel(Engineer &e, Train &t):
16         engineer(e),
17         train(t),
18         updating(false)
19 {
20         Loader::WidgetMap widgets;
21         DataFile::load(*this, "data/controlpanel.ui", widgets);
22
23         dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_protocol"))->set_text(train.get_protocol());
24         dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_address"))->set_text(lexical_cast<string>(train.get_address()));
25
26         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_place"))->signal_clicked.connect(sigc::mem_fun(this, &ControlPanel::place_clicked));
27         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_take"))->signal_clicked.connect(sigc::mem_fun(this, &ControlPanel::take_clicked));
28         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_view"))->signal_clicked.connect(sigc::mem_fun(this, &ControlPanel::view_clicked));
29
30         GLtk::Panel *pnl_functions = dynamic_cast<GLtk::Panel *>(get_item(widgets, "pnl_functions"));
31
32         const VehicleType::FunctionMap &funcs = train.get_vehicle(0).get_type().get_functions();
33         GLtk::Row row(*pnl_functions->get_layout());
34         for(VehicleType::FunctionMap::const_iterator i=funcs.begin(); i!=funcs.end(); ++i)
35         {
36                 GLtk::Toggle *tgl = new GLtk::Toggle(i->second);
37                 tgl->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &ControlPanel::ui_function_toggled), i->first));
38                 pnl_functions->add(*tgl);
39                 tgl_funcs[i->first] = tgl;
40         }
41
42         train.signal_function_changed.connect(sigc::mem_fun(this, &ControlPanel::train_function_changed));
43 }
44
45 void ControlPanel::ui_function_toggled(bool value, unsigned func)
46 {
47         if(!updating)
48                 train.set_function(func, value);
49 }
50
51 void ControlPanel::train_function_changed(unsigned func, bool value)
52 {
53         SetFlag setf(updating);
54         map<unsigned, GLtk::Toggle *>::iterator i = tgl_funcs.find(func);
55         if(i!=tgl_funcs.end())
56                 i->second->set_value(value);
57 }
58
59 void ControlPanel::place_clicked()
60 {
61         pick_conn = engineer.signal_pick_done.connect(sigc::mem_fun(this, &ControlPanel::place_pick_done));
62         engineer.pick(true);
63 }
64
65 void ControlPanel::place_pick_done(Track *track, unsigned ep)
66 {
67         pick_conn.disconnect();
68         train.place(TrackIter(track, ep).block_iter());
69 }
70
71 void ControlPanel::take_clicked()
72 {
73         train.unplace();
74 }
75
76 void ControlPanel::view_clicked()
77 {
78         GLtk::Container *root = parent;
79         while(root->get_parent())
80                 root = root->get_parent();
81
82         TrainView *dlg = new TrainView(engineer, train);
83         root->add(*dlg);
84         dlg->autosize();
85 }