]> git.tdb.fi Git - r2c2.git/blob - source/engineer/controlpanel.cpp
cc2fa079fbebc03bd984ee8d74e4b44380f20a9e
[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->set_value(train.get_function(i->first));
40                 tgl->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &ControlPanel::ui_function_toggled), i->first));
41                 pnl_functions->add(*tgl);
42                 tgl_funcs[i->first] = tgl;
43         }
44
45         train.signal_function_changed.connect(sigc::mem_fun(this, &ControlPanel::train_function_changed));
46 }
47
48 void ControlPanel::ui_function_toggled(bool value, unsigned func)
49 {
50         if(!updating)
51                 train.set_function(func, value);
52 }
53
54 void ControlPanel::train_function_changed(unsigned func, bool value)
55 {
56         SetFlag setf(updating);
57         map<unsigned, GLtk::Toggle *>::iterator i = tgl_funcs.find(func);
58         if(i!=tgl_funcs.end())
59                 i->second->set_value(value);
60 }
61
62 void ControlPanel::place_clicked()
63 {
64         signal_grab_pointer.emit();
65         placing = true;
66
67         Layout3D &layout3d = engineer.get_layout_3d();
68         unsigned n_vehs = train.get_n_vehicles();
69         for(unsigned i=0; i<n_vehs; ++i)
70                 ghosts.push_back(new PlacementGhost(layout3d, train.get_vehicle(i).get_type()));
71 }
72
73 void ControlPanel::take_clicked()
74 {
75         train.unplace();
76 }
77
78 void ControlPanel::view_clicked()
79 {
80         TrainView *dlg = new TrainView(engineer, train);
81         find_ancestor<GLtk::Root>()->add(*dlg);
82         dlg->autosize();
83 }
84
85 void ControlPanel::button_press(int x, int y, unsigned btn)
86 {
87         Panel::button_press(x, y, btn);
88
89         if(placing)
90         {
91                 signal_ungrab_pointer.emit();
92                 placing = false;
93
94                 for(vector<PlacementGhost *>::iterator i=ghosts.begin(); i!=ghosts.end(); ++i)
95                         delete *i;
96                 ghosts.clear();
97
98                 if(btn==1 && place_location)
99                         train.place(place_location);
100         }
101 }
102
103 void ControlPanel::pointer_motion(int x, int y)
104 {
105         Panel::pointer_motion(x, y);
106
107         if(placing)
108         {
109                 int rx = x;
110                 int ry = y;
111                 map_coords_to_ancestor(rx, ry, *find_ancestor<GLtk::Root>());
112                 Ray ray = engineer.get_main_view().create_ray(rx, ry);
113                 Vector ground = ray.get_start()-ray.get_direction()*ray.get_start().z/ray.get_direction().z;
114                 Track *track = engineer.get_layout().pick<Track>(ray);
115                 if(track)
116                 {
117                         const vector<Block::Endpoint> &eps = track->get_block().get_endpoints();
118                         int closest_ep = -1;
119                         float closest_dist = -1;
120                         for(unsigned i=0; i<eps.size(); ++i)
121                         {
122                                 Snap sn = eps[i].track->get_snap_node(eps[i].track_ep);
123                                 float d = (sn.position-ground).norm();
124                                 if(d<closest_dist || closest_dist<0)
125                                 {
126                                         closest_ep = i;
127                                         closest_dist = d;
128                                 }
129                         }
130
131                         if(closest_ep>=0)
132                         {
133                                 place_location = BlockIter(&track->get_block(), closest_ep);
134                                 ghosts.back()->place(place_location.track_iter());
135                                 for(unsigned i=ghosts.size()-1; i--; )
136                                         ghosts[i]->place_before(*ghosts[i+1]);
137                         }
138                 }
139         }
140 }