3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
12 #include <msp/core/except.h>
13 #include <msp/core/getopt.h>
14 #include <msp/fs/stat.h>
15 #include <msp/gbase/display.h>
16 #include <msp/gbase/window.h>
17 #include <msp/gl/immediate.h>
18 #include <msp/gl/matrix.h>
19 #include <msp/gl/projection.h>
20 #include <msp/gl/transform.h>
21 #include <msp/io/print.h>
22 #include <msp/strings/formatter.h>
23 #include <msp/strings/lexicalcast.h>
24 #include <msp/strings/regex.h>
25 #include <msp/time/units.h>
26 #include "libmarklin/except.h"
27 #include "libmarklin/tracktype.h"
29 #include "mainpanel.h"
30 #include "trainpanel.h"
31 #include "trainproperties.h"
34 using namespace Marklin;
37 Engineer::Engineer(int argc, char **argv):
52 string device = "/dev/ttyS0";
57 getopt.add_option('r', "resolution", res, GetOpt::REQUIRED_ARG);
58 getopt.add_option('f', "fullscreen", fullscreen, GetOpt::NO_ARG);
59 getopt.add_option('g', "debug", debug, GetOpt::NO_ARG);
60 getopt.add_option('d', "device", device, GetOpt::REQUIRED_ARG);
61 getopt.add_option('q', "quality", quality, GetOpt::REQUIRED_ARG);
62 getopt.add_option('s', "simulate", simulate, GetOpt::NO_ARG);
63 getopt.add_option('n', "network", network, GetOpt::NO_ARG);
64 getopt.add_option( "no-lighting", no_lighting, GetOpt::NO_ARG);
69 if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res))
71 screen_w = lexical_cast<unsigned>(m[1].str);
72 screen_h = lexical_cast<unsigned>(m[2].str);
75 throw UsageError("Invalid resolution");
81 control.set_debug(debug);
83 layout_3d.set_quality(quality);
85 DataFile::load(catalogue, "tracks.dat");
86 DataFile::load(catalogue, "locos.dat");
88 const vector<string> &args = getopt.get_args();
90 throw UsageError("No layout given");
91 DataFile::load(layout, args.front());
93 trfc_mgr = new TrafficManager(control, layout);
94 if(FS::exists("engineer.state"))
95 DataFile::load(*trfc_mgr, "engineer.state");
96 trfc_mgr->signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
97 trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
101 server = new Server(*trfc_mgr);
102 server->use_event_dispatcher(event_disp);
105 const map<unsigned, Sensor *> &sensors = control.get_sensors();
106 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
107 i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_event), i->second));
111 catch_signal(SIGINT);
112 catch_signal(SIGTERM);
113 catch_signal(SIGSEGV);
114 catch_signal(SIGILL);
115 catch_signal(SIGFPE);
116 catch_signal(SIGABRT);
119 Engineer::~Engineer()
121 const list<Train *> &trains = trfc_mgr->get_trains();
122 for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
125 while(control.get_queue_length())
129 trfc_mgr->save("engineer.state");
134 void Engineer::place_train(Train &train)
136 placing_train = &train;
138 main_panel->set_status_text("Select location");
143 dpy = new Graphics::Display;
145 Graphics::WindowOptions wopt;
146 wopt.width = screen_w;
147 wopt.height = screen_h;
148 wopt.fullscreen = fullscreen;
149 wnd = new Graphics::Window(*dpy, wopt);
151 Graphics::GLOptions glopt;
152 //glopt.multisample = 4;
153 glc = new Graphics::GLContext(*wnd, glopt);
155 wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
156 wnd->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press));
157 wnd->signal_button_release.connect(sigc::mem_fun(this, &Engineer::button_release));
158 wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion));
160 glEnableClientState(GL_VERTEX_ARRAY);
161 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
162 glEnable(GL_COLOR_MATERIAL);
163 glDepthFunc(GL_LEQUAL);
165 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
167 DataFile::load(ui_res, "marklin.res");
168 root = new GLtk::Root(ui_res, *wnd);
169 root->set_visible(true);
171 list<GL::Texture *> texs = ui_res.get_list<GL::Texture>();
172 for(list<GL::Texture *>::iterator i=texs.begin(); i!=texs.end(); ++i)
174 (*i)->set_min_filter(GL::NEAREST);
175 (*i)->set_mag_filter(GL::NEAREST);
178 main_panel = new MainPanel(*this, ui_res);
179 root->add(*main_panel);
180 main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
181 main_panel->set_visible(true);
183 const list<Train *> &trains = trfc_mgr->get_trains();
184 int y = main_panel->get_geometry().y;
185 for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
187 TrainPanel *tpanel = new TrainPanel(*this, ui_res, **i);
189 tpanel->set_position(0, y-tpanel->get_geometry().h);
190 train_panels.push_back(tpanel);
191 tpanel->set_visible(true);
192 y -= tpanel->get_geometry().h;
195 const list<Block *> &blocks = trfc_mgr->get_blocks();
196 for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
197 reset_block_color(**i);
211 void Engineer::tick()
217 event_disp.tick(Time::zero);
219 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
223 glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
224 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
228 glEnable(GL_LIGHTING);
235 glLightfv(GL_LIGHT0, GL_POSITION, params);
238 //glEnable(GL_DEPTH_TEST);
239 glEnable(GL_MULTISAMPLE);
241 layout_3d.get_scene().render();
243 glDisable(GL_LIGHTING);
244 glColor4f(1, 1, 1, 1);
245 /*const list<Track3D *> <racks = layout_3d.get_tracks();
246 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
248 Track &track = (*i)->get_track();
249 if(track.get_turnout_id())
251 Turnout &trnt = control.get_turnout(track.get_turnout_id());
252 (*i)->render_path(trnt.get_path());
255 (*i)->render_path(-1);
258 if(placing_train && placing_block)
262 const Marklin::Block::Endpoint &bep = placing_block->get_endpoints()[placing_entry];
263 float rot = bep.track->get_endpoint_direction(bep.track_ep);
264 Point pos = bep.track->get_endpoint_position(bep.track_ep);
266 GL::translate(pos.x, pos.y, pos.z+0.03);
267 GL::rotate(rot*180/M_PI+180, 0, 0, 1);
268 GL::Texture::unbind();
270 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
271 imm.color(1.0f, 1.0f, 1.0f);
272 imm.begin(GL::TRIANGLE_FAN);
274 imm.vertex(0.05, 0.03);
275 imm.vertex(0.05, 0.01);
277 imm.vertex(0, -0.01);
278 imm.vertex(0.05, -0.01);
279 imm.vertex(0.05, -0.03);
285 const list<Train *> &trains = trfc_mgr->get_trains();
286 for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
288 if(!(*i)->is_placed())
291 GL::PushMatrix _push;
293 const Point &tp = (*i)->get_position();
294 GL::translate(tp.x, tp.y, tp.z+0.02);
295 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
296 imm.color(0.8f, 0.8f, 1.0f);
297 imm.begin(GL::TRIANGLE_FAN);
299 for(unsigned j=0; j<=12; ++j)
300 imm.vertex(0.02*cos(j*M_PI/6), 0.02*sin(j*M_PI/6));
303 GL::rotate(cam_rot*180/M_PI, 0, 0, 1);
304 GL::translate(0.03, -0.02, 0);
305 GL::scale_uniform(0.04);
306 ui_res.get_default_font().draw_string((*i)->get_name());
307 GL::Texture::unbind();
310 GL::matrix_mode(GL::PROJECTION);
312 GL::ortho_bottomleft(screen_w, screen_h);
313 GL::matrix_mode(GL::MODELVIEW);
316 glDisable(GL_DEPTH_TEST);
317 glDisable(GL_LIGHTING);
318 glDisable(GL_MULTISAMPLE);
325 void Engineer::button_press(int x, int y, unsigned btn, unsigned)
329 if(btn==1 && placing_block && !placing_block->get_train())
331 set_block_color(*placing_block, GL::Color(1, 1, 1));
333 placing_train->place(*placing_block, placing_entry);
335 main_panel->set_status_text(string());
339 const vector<Block::Endpoint> &endpoints = placing_block->get_endpoints();
340 placing_entry = (placing_entry+1)%endpoints.size();
345 Track3D *track = pick_track(x, screen_h-y-1);
348 if(unsigned tid=track->get_track().get_turnout_id())
350 Turnout &turnout = control.get_turnout(tid);
353 turnout.set_path((turnout.get_path()+1)%track->get_track().get_type().get_n_paths());
354 main_panel->set_status_text(format("Turnout %d switched", turnout.get_address()));
356 catch(const TurnoutBusy &e)
358 main_panel->set_status_text(e.what());
363 if(unsigned sid=track->get_track().get_sensor_id())
365 Sensor &sensor = control.get_sensor(sid);
366 control.signal_sensor_event.emit(sid, !sensor.get_state());
373 void Engineer::button_release(int, int, unsigned, unsigned)
377 void Engineer::pointer_motion(int x, int y)
381 Track3D *track = pick_track(x, screen_h-y-1);
382 if(track && placing_train)
384 Block &block = trfc_mgr->get_block_by_track(track->get_track());
385 if(&block!=placing_block)
388 reset_block_color(*placing_block);
389 placing_block = █
391 set_block_color(*placing_block, GL::Color(0.5, 1, 0.7));
394 else if(track && track->get_track().get_turnout_id())
395 main_panel->set_status_text(format("Turnout %d", track->get_track().get_turnout_id()));
396 else if(!placing_train)
397 main_panel->set_status_text(string());
401 void Engineer::view_all()
403 const list<Track3D *> &tracks = layout_3d.get_tracks();
406 float best_height = -1;
409 for(float angle=0; angle<M_PI; angle+=0.01)
415 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
418 (*i)->get_bounds(angle, minp, maxp);
419 min_x = min(min_x, minp.x);
420 max_x = max(max_x, maxp.x);
421 min_y = min(min_y, minp.y);
422 max_y = max(max_y, maxp.y);
425 float width = max_x-min_x;
426 float height = max_y-min_y;
427 height = max(height, width);
429 if(height<best_height || best_height<0)
432 best_height = height;
433 mid_x = (min_x+max_x)/2;
434 mid_y = (min_y+max_y)/2;
438 float c = cos(cam_rot);
439 float s = sin(cam_rot);
440 cam_pos.x = c*mid_x-s*mid_y;
441 cam_pos.y = s*mid_x+c*mid_y;
442 cam_pos.z = max(best_height*1.05/0.82843, 0.15);
445 void Engineer::set_block_color(const Block &block, const GL::Color &color)
447 const set<Track *> &tracks = block.get_tracks();
448 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
449 layout_3d.get_track(**i).set_color(color);
452 void Engineer::reset_block_color(const Block &block)
454 if(unsigned sid=block.get_sensor_id())
456 Sensor &sensor = control.get_sensor(sid);
457 if(sensor.get_state())
459 set_block_color(block, GL::Color(1, 0.5, 0.3));
464 if(block.get_train())
465 set_block_color(block, GL::Color(1, 1, 0.3));
467 set_block_color(block, GL::Color(1, 1, 1));
470 void Engineer::sensor_event(bool, Sensor *sensor)
472 const list<Block *> &blocks = trfc_mgr->get_blocks();
473 for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
474 if((*i)->get_sensor_id()==sensor->get_address())
475 reset_block_color(**i);
478 void Engineer::block_reserved(const Block &block, const Train *)
480 reset_block_color(block);
483 void Engineer::project_3d()
485 glMatrixMode(GL_PROJECTION);
487 float offset = 200.0/screen_w*0.055228;
488 glFrustum(-0.055228-offset, 0.055228-offset, -0.041421, 0.041421, 0.1, 10);
489 glMatrixMode(GL_MODELVIEW);
492 Track3D *Engineer::pick_track(int x, int y)
494 float xx = (static_cast<float>(x-static_cast<int>(screen_w)/2-100)/screen_h)*0.82843;
495 float yy = (static_cast<float>(y)/screen_h-0.5)*0.82843;
496 float size = 4.0/screen_h*0.82843;
500 glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
501 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
503 return layout_3d.pick_track(xx, yy, size);
506 void Engineer::train_added(Train &train)
508 TrainPanel *tpanel = new TrainPanel(*this, ui_res, train);
510 int y = main_panel->get_geometry().y;
511 for(list<TrainPanel *>::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
512 y -= (*i)->get_geometry().h;
513 tpanel->set_position(0, y-tpanel->get_geometry().h);
514 train_panels.push_back(tpanel);
515 tpanel->set_visible(true);
520 void Engineer::sighandler(int sig)
522 if(sig==SIGSEGV || sig==SIGILL || sig==SIGFPE || sig==SIGABRT)
524 signal(sig, SIG_DFL);
525 IO::print(IO::cerr, "Fatal signal received, terminating\n");
526 const map<unsigned, Locomotive *> &locos = control.get_locomotives();
527 for(map<unsigned, Locomotive *>::const_iterator i=locos.begin(); i!=locos.end(); ++i)
528 i->second->set_speed(0);
532 else if(sig==SIGTERM || sig==SIGINT)
536 Application::RegApp<Engineer> Engineer::reg;