]> git.tdb.fi Git - r2c2.git/blob - source/engineer/engineer.cpp
Add train status display
[r2c2.git] / source / engineer / engineer.cpp
1 #include <cmath>
2 #include <limits>
3 #include <GL/gl.h>
4 #include <msp/core/except.h>
5 #include <msp/core/getopt.h>
6 #include <msp/gbase/display.h>
7 #include <msp/gbase/window.h>
8 #include <msp/gl/immediate.h>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/projection.h>
11 #include <msp/gl/transform.h>
12 #include <msp/strings/formatter.h>
13 #include <msp/strings/lexicalcast.h>
14 #include <msp/strings/regex.h>
15 #include "engineer.h"
16 #include "mainpanel.h"
17 #include "trainpanel.h"
18 #include "trainproperties.h"
19
20 using namespace std;
21 using namespace Marklin;
22 using namespace Msp;
23
24 #include <iostream>
25
26 Engineer::Engineer(int argc, char **argv):
27         screen_w(1280),
28         screen_h(960),
29         fullscreen(false),
30         layout(catalogue),
31         layout_3d(layout),
32         no_lighting(false),
33         placing_train(0),
34         placing_block(0),
35         placing_entry(0),
36         simulate(false),
37         train_prop(0),
38         train_prop_stale(false)
39 {
40         string res;
41         bool   debug=false;
42         string device="/dev/ttyS0";
43         unsigned quality=4;
44
45         GetOpt getopt;
46         getopt.add_option('r', "resolution",  res,         GetOpt::REQUIRED_ARG);
47         getopt.add_option('f', "fullscreen",  fullscreen,  GetOpt::NO_ARG);
48         getopt.add_option('g', "debug",       debug,       GetOpt::NO_ARG);
49         getopt.add_option('d', "device",      device,      GetOpt::REQUIRED_ARG);
50         getopt.add_option('q', "quality",     quality,     GetOpt::REQUIRED_ARG);
51         getopt.add_option('s', "simulate",    simulate,    GetOpt::NO_ARG);
52         getopt.add_option(     "no-lighting", no_lighting, GetOpt::NO_ARG);
53         getopt(argc, argv);
54
55         if(!res.empty())
56         {
57                 if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res))
58                 {
59                         screen_w=lexical_cast<unsigned>(m[1].str);
60                         screen_h=lexical_cast<unsigned>(m[2].str);
61                 }
62                 else
63                         throw UsageError("Invalid resolution");
64         }
65
66         if(device!="none")
67                 control.open(device);
68
69         control.set_debug(debug);
70
71         layout_3d.set_quality(quality);
72
73         catalogue.load("tracks.dat");
74
75         const vector<string> &args=getopt.get_args();
76         if(args.empty())
77                 throw UsageError("No layout given");
78         layout.load(args.front());
79
80         trfc_mgr=new TrafficManager(control, layout);
81         trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
82
83         const map<unsigned, Sensor *> &sensors=control.get_sensors();
84         for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
85                 i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_event), i->second));
86
87         view_all();
88 }
89
90 Engineer::~Engineer()
91 {
92         delete trfc_mgr;
93 }
94
95 Train *Engineer::add_train(unsigned addr)
96 {
97         if(addr==0)
98         {
99                 train_prop=new TrainProperties(*this, ui_res, 0);
100                 root->add(*train_prop);
101                 train_prop->signal_ok.connect(sigc::mem_fun(this, &Engineer::dismiss_train_prop));
102                 train_prop_stale=false;
103                 train_prop->set_visible(true);
104
105                 return 0;
106         }
107         else
108         {
109                 Locomotive *loco=new Locomotive(control, addr);
110                 Train *train=new Train(*trfc_mgr, *loco);
111                 train->set_name(format("Train %d", trfc_mgr->get_trains().size()));
112
113                 TrainPanel *tpanel=new TrainPanel(*this, ui_res, *train);
114                 root->add(*tpanel);
115                 int y=main_panel->get_geometry().y;
116                 for(list<TrainPanel *>::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
117                         y-=(*i)->get_geometry().h;
118                 tpanel->set_position(0, y-tpanel->get_geometry().h);
119                 train_panels.push_back(tpanel);
120                 tpanel->set_visible(true);
121
122                 place_train(*train);
123
124                 return train;
125         }
126 }
127
128 void Engineer::place_train(Train &train)
129 {
130         placing_train=&train;
131         placing_block=0;
132         main_panel->set_status_text("Select location");
133 }
134
135 int Engineer::main()
136 {
137         dpy=new Graphics::Display;
138
139         Graphics::WindowOptions wopt;
140         wopt.width=screen_w;
141         wopt.height=screen_h;
142         wopt.fullscreen=fullscreen;
143         wnd=new Graphics::Window(*dpy, wopt);
144
145         Graphics::GLOptions glopt;
146         //glopt.multisample=4;
147         glc=new Graphics::GLContext(*wnd, glopt);
148
149         wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
150         wnd->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press));
151         wnd->signal_button_release.connect(sigc::mem_fun(this, &Engineer::button_release));
152         wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion));
153
154         glEnableClientState(GL_VERTEX_ARRAY);
155         glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
156         glEnable(GL_COLOR_MATERIAL);
157         glDepthFunc(GL_LEQUAL);
158         glEnable(GL_BLEND);
159         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
160
161         DataFile::load(ui_res, "engineer.res");
162         root=new GLtk::Root(ui_res, *wnd);
163         root->set_visible(true);
164
165         list<GL::Texture *> texs=ui_res.get_list<GL::Texture>();
166         for(list<GL::Texture *>::iterator i=texs.begin(); i!=texs.end(); ++i)
167         {
168                 (*i)->set_min_filter(GL::NEAREST);
169                 (*i)->set_mag_filter(GL::NEAREST);
170         }
171
172         main_panel=new MainPanel(*this, ui_res);
173         root->add(*main_panel);
174         main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
175         main_panel->set_visible(true);
176
177         wnd->show();
178
179         Application::main();
180
181         delete glc;
182         delete wnd;
183         delete dpy;
184
185         return exit_code;
186 }
187
188 void Engineer::tick()
189 {
190         dpy->tick();
191
192         control.tick();
193         trfc_mgr->tick();
194
195         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
196
197         project_3d();
198         glLoadIdentity();
199         glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
200         glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
201
202         if(!no_lighting)
203         {
204                 glEnable(GL_LIGHTING);
205                 glEnable(GL_LIGHT0);
206                 float params[4];
207                 params[0]=0;
208                 params[1]=-0.2;
209                 params[2]=1;
210                 params[3]=0;
211                 glLightfv(GL_LIGHT0, GL_POSITION, params);
212         }
213
214         //glEnable(GL_DEPTH_TEST);
215         glEnable(GL_MULTISAMPLE);
216
217         layout_3d.render();
218
219         glDisable(GL_LIGHTING);
220         glColor4f(1, 1, 1, 1);
221         const list<Track3D *> &ltracks=layout_3d.get_tracks();
222         for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
223         {
224                 Track &track=(*i)->get_track();
225                 if(track.get_turnout_id())
226                 {
227                         Turnout &trnt=control.get_turnout(track.get_turnout_id());
228                         (*i)->render_route(trnt.get_route());
229                 }
230                 else
231                         (*i)->render_route(-1);
232         }
233
234         if(placing_train && placing_block)
235         {
236                 GL::push_matrix();
237
238                 const Marklin::Block::Endpoint &bep=placing_block->get_endpoints()[placing_entry];
239                 float rot=bep.track->get_endpoint_direction(bep.track_ep);
240                 Point pos=bep.track->get_endpoint_position(bep.track_ep);
241
242                 GL::translate(pos.x, pos.y, pos.z+0.03);
243                 GL::rotate(rot*180/M_PI+180, 0, 0, 1);
244                 GL::Texture::unbind();
245
246                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
247                 imm.color(1.0f, 1.0f, 1.0f);
248                 imm.begin(GL::TRIANGLE_FAN);
249                 imm.vertex(0.08,   0);
250                 imm.vertex(0.05,   0.03);
251                 imm.vertex(0.05,   0.01);
252                 imm.vertex(0,      0.01);
253                 imm.vertex(0,     -0.01);
254                 imm.vertex(0.05,  -0.01);
255                 imm.vertex(0.05,  -0.03);
256                 imm.end();
257
258                 GL::pop_matrix();
259         }
260
261         GL::matrix_mode(GL::PROJECTION);
262         GL::load_identity();
263         GL::ortho_bottomleft(screen_w, screen_h);
264         GL::matrix_mode(GL::MODELVIEW);
265         GL::load_identity();
266
267         glDisable(GL_DEPTH_TEST);
268         glDisable(GL_LIGHTING);
269         glDisable(GL_MULTISAMPLE);
270
271         root->render();
272
273         if(train_prop_stale)
274         {
275                 delete train_prop;
276                 train_prop=0;
277         }
278
279         glc->swap_buffers();
280 }
281
282 void Engineer::key_press(unsigned key, unsigned mod, wchar_t ch)
283 {
284         if(train_prop)
285                 train_prop->key_press(key, mod, ch);
286 }
287
288 void Engineer::button_press(int x, int y, unsigned btn, unsigned)
289 {
290         if(placing_train)
291         {
292                 if(btn==1 && placing_block && !placing_block->get_train())
293                 {
294                         set_block_color(*placing_block, GL::Color(1, 1, 1));
295
296                         placing_train->place(placing_block, placing_entry);
297                         placing_train=0;
298                         main_panel->set_status_text(string());
299                 }
300                 else if(btn==3)
301                 {
302                         const vector<Block::Endpoint> &endpoints=placing_block->get_endpoints();
303                         placing_entry=(placing_entry+1)%endpoints.size();
304                 }
305         }
306         else
307         {
308                 Track3D *track=pick_track(x, screen_h-y-1);
309                 if(track)
310                 {
311                         if(unsigned tid=track->get_track().get_turnout_id())
312                         {
313                                 Turnout &turnout=control.get_turnout(tid);
314                                 turnout.set_route(1-turnout.get_route());
315                         }
316                         else if(simulate)
317                         {
318                                 if(unsigned sid=track->get_track().get_sensor_id())
319                                 {
320                                         Sensor &sensor=control.get_sensor(sid);
321                                         control.signal_sensor_event.emit(sid, !sensor.get_state());
322                                 }
323                         }
324                 }
325         }
326 }
327
328 void Engineer::button_release(int, int, unsigned, unsigned)
329 {
330 }
331
332 void Engineer::pointer_motion(int x, int y)
333 {
334         if(placing_train)
335         {
336                 Track3D *track=pick_track(x, screen_h-y-1);
337                 if(track && placing_train)
338                 {
339                         Block &block=trfc_mgr->get_block_by_track(track->get_track());
340                         if(&block!=placing_block)
341                         {
342                                 if(placing_block)
343                                         reset_block_color(*placing_block);
344                                 placing_block=&block;
345                                 placing_entry=0;
346                                 set_block_color(*placing_block, GL::Color(0.5, 1, 0.7));
347                         }
348                 }
349                 else if(track && track->get_track().get_turnout_id())
350                         main_panel->set_status_text(format("Turnout %d", track->get_track().get_turnout_id()));
351                 else if(!placing_train)
352                         main_panel->set_status_text(string());
353         }
354 }
355
356 void Engineer::view_all()
357 {
358         const list<Track3D *> &tracks=layout_3d.get_tracks();
359
360         cam_rot=0;
361         float best_height=-1;
362         float mid_x=0;
363         float mid_y=0;
364         for(float angle=0; angle<M_PI; angle+=0.01)
365         {
366                 float min_x=0;
367                 float max_x=0;
368                 float min_y=0;
369                 float max_y=0;
370                 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
371                 {
372                         Point minp, maxp;
373                         (*i)->get_bounds(angle, minp, maxp);
374                         min_x=min(min_x, minp.x);
375                         max_x=max(max_x, maxp.x);
376                         min_y=min(min_y, minp.y);
377                         max_y=max(max_y, maxp.y);
378                 }
379
380                 float width=max_x-min_x;
381                 float height=max_y-min_y;
382                 height=max(height, width);
383
384                 if(height<best_height || best_height<0)
385                 {
386                         cam_rot=angle;
387                         best_height=height;
388                         mid_x=(min_x+max_x)/2;
389                         mid_y=(min_y+max_y)/2;
390                 }
391         }
392
393         float c=cos(cam_rot);
394         float s=sin(cam_rot);
395         cam_pos.x=c*mid_x-s*mid_y;
396         cam_pos.y=s*mid_x+c*mid_y;
397         cam_pos.z=max(best_height*1.05/0.82843, 0.15);
398 }
399
400 void Engineer::set_block_color(const Block &block, const GL::Color &color)
401 {
402         const set<Track *> &tracks=block.get_tracks();
403         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
404                 layout_3d.get_track(**i).set_color(color);
405 }
406
407 void Engineer::reset_block_color(const Block &block)
408 {
409         if(unsigned sid=block.get_sensor_id())
410         {
411                 Sensor &sensor=control.get_sensor(sid);
412                 if(sensor.get_state())
413                 {
414                         set_block_color(block, GL::Color(1, 0.5, 0.3));
415                         return;
416                 }
417         }
418
419         if(block.get_train())
420                 set_block_color(block, GL::Color(1, 1, 0.3));
421         else
422                 set_block_color(block, GL::Color(1, 1, 1));
423 }
424
425 void Engineer::sensor_event(bool, Sensor *sensor)
426 {
427         const list<Block *> &blocks=trfc_mgr->get_blocks();
428         for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
429                 if((*i)->get_sensor_id()==sensor->get_address())
430                         reset_block_color(**i);
431 }
432
433 void Engineer::block_reserved(const Block &block, const Train *)
434 {
435         reset_block_color(block);
436 }
437
438 void Engineer::project_3d()
439 {
440         glMatrixMode(GL_PROJECTION);
441         glLoadIdentity();
442         float offset=200.0/screen_w*0.055228;
443         glFrustum(-0.055228-offset, 0.055228-offset, -0.041421, 0.041421, 0.1, 10);
444         glMatrixMode(GL_MODELVIEW);
445 }
446
447 Track3D *Engineer::pick_track(int x, int y)
448 {
449         float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2-100)/screen_h)*0.82843;
450         float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
451         float size=4.0/screen_h*0.82843;
452
453         project_3d();
454         glLoadIdentity();
455         glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
456         glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
457
458         return layout_3d.pick_track(xx, yy, size);
459 }
460
461 void Engineer::dismiss_train_prop()
462 {
463         train_prop_stale=true;
464 }
465
466 Application::RegApp<Engineer> Engineer::reg;