]> git.tdb.fi Git - r2c2.git/blob - source/engineer/controlpanel.cpp
Minor fixes
[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 "placementghost.h"
10 #include "trainview.h"
11
12 using namespace std;
13 using namespace Msp;
14 using namespace R2C2;
15
16 ControlPanel::ControlPanel(Engineer &e, Train &t):
17         engineer(e),
18         train(t),
19         updating(false),
20         placing(false)
21 {
22         Loader::WidgetMap widgets;
23         DataFile::load(*this, "data/controlpanel.ui", widgets);
24
25         dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_protocol"))->set_text(train.get_protocol());
26         dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_address"))->set_text(lexical_cast<string>(train.get_address()));
27
28         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_place"))->signal_clicked.connect(sigc::mem_fun(this, &ControlPanel::place_clicked));
29         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_take"))->signal_clicked.connect(sigc::mem_fun(this, &ControlPanel::take_clicked));
30         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_view"))->signal_clicked.connect(sigc::mem_fun(this, &ControlPanel::view_clicked));
31
32         GLtk::Panel *pnl_functions = dynamic_cast<GLtk::Panel *>(get_item(widgets, "pnl_functions"));
33
34         const VehicleType::FunctionMap &funcs = train.get_vehicle(0).get_type().get_functions();
35         GLtk::Row row(*pnl_functions->get_layout());
36         for(VehicleType::FunctionMap::const_iterator i=funcs.begin(); i!=funcs.end(); ++i)
37         {
38                 GLtk::Toggle *tgl = new GLtk::Toggle(i->second);
39                 tgl->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &ControlPanel::ui_function_toggled), i->first));
40                 pnl_functions->add(*tgl);
41                 tgl_funcs[i->first] = tgl;
42         }
43
44         train.signal_function_changed.connect(sigc::mem_fun(this, &ControlPanel::train_function_changed));
45 }
46
47 void ControlPanel::ui_function_toggled(bool value, unsigned func)
48 {
49         if(!updating)
50                 train.set_function(func, value);
51 }
52
53 void ControlPanel::train_function_changed(unsigned func, bool value)
54 {
55         SetFlag setf(updating);
56         map<unsigned, GLtk::Toggle *>::iterator i = tgl_funcs.find(func);
57         if(i!=tgl_funcs.end())
58                 i->second->set_value(value);
59 }
60
61 void ControlPanel::place_clicked()
62 {
63         signal_grab_pointer.emit();
64         placing = true;
65
66         Layout3D &layout3d = engineer.get_layout_3d();
67         unsigned n_vehs = train.get_n_vehicles();
68         for(unsigned i=0; i<n_vehs; ++i)
69                 ghosts.push_back(new PlacementGhost(layout3d, train.get_vehicle(i).get_type()));
70 }
71
72 void ControlPanel::take_clicked()
73 {
74         train.unplace();
75 }
76
77 void ControlPanel::view_clicked()
78 {
79         TrainView *dlg = new TrainView(engineer, train);
80         find_ancestor<GLtk::Root>()->add(*dlg);
81         dlg->autosize();
82 }
83
84 void ControlPanel::button_press(int x, int y, unsigned btn)
85 {
86         Panel::button_press(x, y, btn);
87
88         if(placing)
89         {
90                 signal_ungrab_pointer.emit();
91                 placing = false;
92
93                 for(vector<PlacementGhost *>::iterator i=ghosts.begin(); i!=ghosts.end(); ++i)
94                         delete *i;
95                 ghosts.clear();
96
97                 if(btn==1 && place_location)
98                         train.place(place_location);
99         }
100 }
101
102 void ControlPanel::pointer_motion(int x, int y)
103 {
104         Panel::pointer_motion(x, y);
105
106         if(placing)
107         {
108                 int rx = x;
109                 int ry = y;
110                 map_coords_to_ancestor(rx, ry, *find_ancestor<GLtk::Root>());
111                 Ray ray = engineer.get_main_view().create_ray(rx, ry);
112                 Track *track = engineer.get_layout().pick<Track>(ray);
113                 if(track)
114                 {
115                         place_location = TrackIter(track, 0).block_iter();
116                         if(place_location)
117                         {
118                                 ghosts.back()->place(place_location.track_iter());
119                                 for(unsigned i=ghosts.size()-1; i--; )
120                                         ghosts[i]->place_before(*ghosts[i+1]);
121                         }
122                 }
123         }
124 }