5 #include <msp/core/error.h>
6 #include <msp/core/getopt.h>
7 #include <msp/strings/lexicalcast.h>
8 #include <msp/strings/regex.h>
10 #include "mainpanel.h"
11 #include "trainpanel.h"
14 using namespace Marklin;
19 Engineer::Engineer(int argc, char **argv):
30 string device="/dev/ttyS0";
34 getopt.add_option('r', "resolution", res, GetOpt::REQUIRED_ARG);
35 getopt.add_option('f', "fullscreen", fullscreen, GetOpt::NO_ARG);
36 getopt.add_option('g', "debug", debug, GetOpt::NO_ARG);
37 getopt.add_option('d', "device", device, GetOpt::REQUIRED_ARG);
38 getopt.add_option('q', "quality", quality, GetOpt::REQUIRED_ARG);
39 getopt.add_option( "no-lighting", no_lighting, GetOpt::NO_ARG);
44 if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res))
46 screen_w=lexical_cast<unsigned>(m[1].str);
47 screen_h=lexical_cast<unsigned>(m[2].str);
50 throw UsageError("Invalid resolution");
56 control.set_debug(debug);
58 layout_3d.set_quality(quality);
60 catalogue.load("tracks.dat");
62 const list<string> &args=getopt.get_args();
64 throw UsageError("No layout given");
65 layout.load(args.front());
67 trfc_mgr=new TrafficManager(control, layout);
71 /*const TrackSeq &tracks=layout.get_tracks();
73 for(TrackSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
75 if(unsigned trnt_id=(*i)->get_turnout_id())
77 Turnout *trnt=new Turnout(control, trnt_id);
78 trnt->signal_route_changed.connect(sigc::mem_fun(this, &Engineer::turnout_route_changed));
80 if(unsigned sens_id=(*i)->get_sensor_id())
82 Sensor *sens=new Sensor(control, sens_id);
83 sens->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_state_changed), sens_id));
92 void Engineer::add_train(unsigned addr)
94 if(control.get_locomotive(addr))
97 Locomotive *loco=new Locomotive(control, addr);
98 Train *train=new Train(*trfc_mgr, *loco);
100 TrainPanel *tpanel=new TrainPanel(*this, ui_res, *train);
101 int y=main_panel->get_geometry().y;
102 for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
103 y-=(*i)->get_geometry().h;
104 tpanel->set_position(0, y-tpanel->get_geometry().h);
105 train_panels.push_back(tpanel);
112 SDL_Init(SDL_INIT_VIDEO);
113 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
114 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
115 SDL_Surface *screen=SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL|(fullscreen?SDL_FULLSCREEN:0));
118 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
119 screen=SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL|(fullscreen?SDL_FULLSCREEN:0));
123 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
124 screen=SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL|(fullscreen?SDL_FULLSCREEN:0));
127 throw Exception("Couldn't create window");
129 glEnableClientState(GL_VERTEX_ARRAY);
130 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
131 glEnable(GL_COLOR_MATERIAL);
132 glDepthFunc(GL_LEQUAL);
134 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
140 Parser::load(*font, "dejavu-20.font");
145 Parser::load(*font, "dejavu-12.font");
148 Parser::load(ui_res, "engineer.res");
149 main_panel=new MainPanel(*this, ui_res);
150 main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
162 void Engineer::tick()
167 while(SDL_PollEvent(&event))
171 case SDL_MOUSEBUTTONDOWN:
172 button_press(event.button.x, screen_h-1-event.button.y, event.button.button);
174 case SDL_MOUSEBUTTONUP:
175 button_release(event.button.x, screen_h-1-event.button.y, event.button.button);
177 case SDL_MOUSEMOTION:
178 pointer_motion(event.motion.x, screen_h-1-event.motion.y);
181 key_press(event.key.keysym.sym, event.key.keysym.mod);
191 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
195 glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
196 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
200 glEnable(GL_LIGHTING);
207 glLightfv(GL_LIGHT0, GL_POSITION, params);
210 //glEnable(GL_DEPTH_TEST);
211 glEnable(GL_MULTISAMPLE);
215 glDisable(GL_LIGHTING);
216 glColor4f(1, 1, 1, 1);
217 const Track3DSeq <racks=layout_3d.get_tracks();
218 for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
220 Track &track=(*i)->get_track();
221 if(track.get_turnout_id())
223 Turnout *trnt=control.get_turnout(track.get_turnout_id());
225 (*i)->render_route(trnt->get_route());
227 (*i)->render_route(-1);
230 (*i)->render_route(-1);
233 glMatrixMode(GL_PROJECTION);
235 glOrtho(0, screen_w, 0, screen_h, 0, 1);
236 glMatrixMode(GL_MODELVIEW);
239 glDisable(GL_DEPTH_TEST);
240 glDisable(GL_LIGHTING);
241 glDisable(GL_MULTISAMPLE);
243 main_panel->render();
244 for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
248 glTranslatef(340, 10, 0);
249 glScalef(20, 20, 20);
250 glColor4f(1, 1, 1, 1);
251 font->draw_string(status_text);
253 SDL_GL_SwapBuffers();
256 void Engineer::key_press(unsigned, unsigned)
260 void Engineer::button_press(int x, int y, unsigned btn)
262 if(main_panel->get_geometry().is_inside(x, y))
264 main_panel->button_press(x, y, btn);
267 for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
268 if((*i)->get_geometry().is_inside(x, y))
270 (*i)->button_press(x, y, btn);
274 Track3D *track=pick_track(x, y);
279 Block *block=trfc_man->get_block_by_track(&track->get_track());
283 Turnout *turnout=control.get_turnout(track->get_track().get_turnout_id());
285 turnout->set_route(1-turnout->get_route());
290 void Engineer::button_release(int x, int y, unsigned btn)
292 if(main_panel->get_geometry().is_inside(x, y))
294 main_panel->button_release(x, y, btn);
297 for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
298 if((*i)->get_geometry().is_inside(x, y))
300 (*i)->button_release(x, y, btn);
305 void Engineer::pointer_motion(int x, int y)
307 if(main_panel->get_geometry().is_inside(x, y))
309 main_panel->pointer_motion(x, y);
312 for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
313 if((*i)->get_geometry().is_inside(x, y))
315 (*i)->pointer_motion(x, y);
319 Track3D *track=pick_track(x, y);
320 if(track && track->get_track().get_turnout_id())
323 ss<<"Turnout "<<track->get_track().get_turnout_id();
324 status_text=ss.str();
330 void Engineer::view_all()
332 const Track3DSeq &tracks=layout_3d.get_tracks();
335 float best_height=-1;
338 for(float angle=0; angle<M_PI; angle+=0.01)
344 for(Track3DSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
347 (*i)->get_bounds(angle, minp, maxp);
348 min_x=min(min_x, minp.x);
349 max_x=max(max_x, maxp.x);
350 min_y=min(min_y, minp.y);
351 max_y=max(max_y, maxp.y);
354 float width=max_x-min_x;
355 float height=max_y-min_y;
356 height=max(height, width);
358 if(height<best_height || best_height<0)
362 mid_x=(min_x+max_x)/2;
363 mid_y=(min_y+max_y)/2;
367 float c=cos(cam_rot);
368 float s=sin(cam_rot);
369 cam_pos.x=c*mid_x-s*mid_y;
370 cam_pos.y=s*mid_x+c*mid_y;
371 cam_pos.z=max(best_height*1.05/0.82843, 0.15);
374 void Engineer::turnout_route_changed(unsigned)
378 void Engineer::sensor_state_changed(bool state, unsigned addr)
380 const Track3DSeq <racks=layout_3d.get_tracks();
381 for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
382 if((*i)->get_track().get_sensor_id()==addr)
385 (*i)->set_color(Color(1, 0, 0));
387 (*i)->set_color(Color(1, 1, 1));
391 void Engineer::project_3d()
393 glMatrixMode(GL_PROJECTION);
395 //glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
396 glFrustum(-0.069036, 0.041421, -0.041421, 0.041421, 0.1, 10);
397 glMatrixMode(GL_MODELVIEW);
400 Track3D *Engineer::pick_track(int x, int y)
402 float xx=((float)(x-(int)screen_w*5/8)/screen_h)*0.82843;
403 //float xx=((float)(x-(int)screen_w/2)/screen_h)*0.82843;
404 float yy=((float)y/screen_h-0.5)*0.82843;
405 float size=(float)4/screen_h*0.82843;
409 glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
410 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
412 return layout_3d.pick_track(xx, yy, size);
414 /*unsigned select_buf[1024];
415 glSelectBuffer(1024, select_buf);
416 glRenderMode(GL_SELECT);
418 //XXX Hardcoded values
419 float xn=((float)(x-800)/960)*0.082843;
420 float yn=((float)(y-480)/960)*0.082843;
421 float size=(float)4/960*0.082843;
431 glClipPlane(GL_CLIP_PLANE0, clip);
432 glEnable(GL_CLIP_PLANE0);
436 glClipPlane(GL_CLIP_PLANE1, clip);
437 glEnable(GL_CLIP_PLANE1);
442 glClipPlane(GL_CLIP_PLANE2, clip);
443 glEnable(GL_CLIP_PLANE2);
447 glClipPlane(GL_CLIP_PLANE3, clip);
448 glEnable(GL_CLIP_PLANE3);
450 glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
451 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
455 glDisable(GL_CLIP_PLANE0);
456 glDisable(GL_CLIP_PLANE1);
457 glDisable(GL_CLIP_PLANE2);
458 glDisable(GL_CLIP_PLANE3);
460 unsigned n_records=glRenderMode(GL_RENDER);
465 unsigned track_depth=numeric_limits<unsigned>::max();
466 for(unsigned j=0; j<n_records; ++j)
468 unsigned ns_size=select_buf[i++];
469 unsigned min_depth=select_buf[i++];
470 ++i; // Skip max_depth
471 if(min_depth<track_depth)
473 track=(Track *)select_buf[i+ns_size-1];
474 track_depth=min_depth;
485 Application::RegApp<Engineer> Engineer::reg;