3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
11 #include <msp/core/except.h>
12 #include <msp/core/getopt.h>
13 #include <msp/fs/stat.h>
14 #include <msp/gbase/display.h>
15 #include <msp/gbase/window.h>
16 #include <msp/gl/immediate.h>
17 #include <msp/gl/matrix.h>
18 #include <msp/gl/projection.h>
19 #include <msp/gl/transform.h>
20 #include <msp/strings/formatter.h>
21 #include <msp/strings/lexicalcast.h>
22 #include <msp/strings/regex.h>
23 #include <msp/time/units.h>
24 #include "libmarklin/except.h"
25 #include "libmarklin/tracktype.h"
27 #include "mainpanel.h"
28 #include "trainpanel.h"
29 #include "trainproperties.h"
32 using namespace Marklin;
35 Engineer::Engineer(int argc, char **argv):
50 string device = "/dev/ttyS0";
55 getopt.add_option('r', "resolution", res, GetOpt::REQUIRED_ARG);
56 getopt.add_option('f', "fullscreen", fullscreen, GetOpt::NO_ARG);
57 getopt.add_option('g', "debug", debug, GetOpt::NO_ARG);
58 getopt.add_option('d', "device", device, GetOpt::REQUIRED_ARG);
59 getopt.add_option('q', "quality", quality, GetOpt::REQUIRED_ARG);
60 getopt.add_option('s', "simulate", simulate, GetOpt::NO_ARG);
61 getopt.add_option('n', "network", network, GetOpt::NO_ARG);
62 getopt.add_option( "no-lighting", no_lighting, GetOpt::NO_ARG);
67 if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res))
69 screen_w = lexical_cast<unsigned>(m[1].str);
70 screen_h = lexical_cast<unsigned>(m[2].str);
73 throw UsageError("Invalid resolution");
79 control.set_debug(debug);
81 layout_3d.set_quality(quality);
83 DataFile::load(catalogue, "tracks.dat");
84 DataFile::load(catalogue, "locos.dat");
86 const vector<string> &args = getopt.get_args();
88 throw UsageError("No layout given");
89 DataFile::load(layout, args.front());
91 trfc_mgr = new TrafficManager(control, layout);
92 if(FS::exists("engineer.state"))
93 DataFile::load(*trfc_mgr, "engineer.state");
94 trfc_mgr->signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
95 trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
99 server = new Server(*trfc_mgr);
100 server->use_event_dispatcher(event_disp);
103 const map<unsigned, Sensor *> &sensors = control.get_sensors();
104 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
105 i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_event), i->second));
110 Engineer::~Engineer()
113 trfc_mgr->save("engineer.state");
117 void Engineer::place_train(Train &train)
119 placing_train = &train;
121 main_panel->set_status_text("Select location");
126 dpy = new Graphics::Display;
128 Graphics::WindowOptions wopt;
129 wopt.width = screen_w;
130 wopt.height = screen_h;
131 wopt.fullscreen = fullscreen;
132 wnd = new Graphics::Window(*dpy, wopt);
134 Graphics::GLOptions glopt;
135 //glopt.multisample = 4;
136 glc = new Graphics::GLContext(*wnd, glopt);
138 wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
139 wnd->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press));
140 wnd->signal_button_release.connect(sigc::mem_fun(this, &Engineer::button_release));
141 wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion));
143 glEnableClientState(GL_VERTEX_ARRAY);
144 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
145 glEnable(GL_COLOR_MATERIAL);
146 glDepthFunc(GL_LEQUAL);
148 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
150 DataFile::load(ui_res, "marklin.res");
151 root = new GLtk::Root(ui_res, *wnd);
152 root->set_visible(true);
154 list<GL::Texture *> texs = ui_res.get_list<GL::Texture>();
155 for(list<GL::Texture *>::iterator i=texs.begin(); i!=texs.end(); ++i)
157 (*i)->set_min_filter(GL::NEAREST);
158 (*i)->set_mag_filter(GL::NEAREST);
161 main_panel = new MainPanel(*this, ui_res);
162 root->add(*main_panel);
163 main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
164 main_panel->set_visible(true);
166 const list<Train *> &trains = trfc_mgr->get_trains();
167 int y = main_panel->get_geometry().y;
168 for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
170 TrainPanel *tpanel = new TrainPanel(*this, ui_res, **i);
172 tpanel->set_position(0, y-tpanel->get_geometry().h);
173 train_panels.push_back(tpanel);
174 tpanel->set_visible(true);
175 y -= tpanel->get_geometry().h;
189 void Engineer::tick()
195 event_disp.tick(Time::zero);
197 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
201 glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
202 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
206 glEnable(GL_LIGHTING);
213 glLightfv(GL_LIGHT0, GL_POSITION, params);
216 //glEnable(GL_DEPTH_TEST);
217 glEnable(GL_MULTISAMPLE);
221 glDisable(GL_LIGHTING);
222 glColor4f(1, 1, 1, 1);
223 const list<Track3D *> <racks = layout_3d.get_tracks();
224 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
226 Track &track = (*i)->get_track();
227 if(track.get_turnout_id())
229 Turnout &trnt = control.get_turnout(track.get_turnout_id());
230 (*i)->render_route(trnt.get_route());
233 (*i)->render_route(-1);
236 if(placing_train && placing_block)
240 const Marklin::Block::Endpoint &bep = placing_block->get_endpoints()[placing_entry];
241 float rot = bep.track->get_endpoint_direction(bep.track_ep);
242 Point pos = bep.track->get_endpoint_position(bep.track_ep);
244 GL::translate(pos.x, pos.y, pos.z+0.03);
245 GL::rotate(rot*180/M_PI+180, 0, 0, 1);
246 GL::Texture::unbind();
248 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
249 imm.color(1.0f, 1.0f, 1.0f);
250 imm.begin(GL::TRIANGLE_FAN);
252 imm.vertex(0.05, 0.03);
253 imm.vertex(0.05, 0.01);
255 imm.vertex(0, -0.01);
256 imm.vertex(0.05, -0.01);
257 imm.vertex(0.05, -0.03);
263 const list<Train *> &trains = trfc_mgr->get_trains();
264 for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
266 GL::PushMatrix _push;
268 const Point &tp = (*i)->get_position();
269 GL::translate(tp.x, tp.y, 0.02);
270 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
271 imm.color(0.8f, 0.8f, 1.0f);
272 imm.begin(GL::TRIANGLE_FAN);
274 for(unsigned j=0; j<=12; ++j)
275 imm.vertex(0.02*cos(j*M_PI/6), 0.02*sin(j*M_PI/6));
278 GL::rotate(cam_rot*180/M_PI, 0, 0, 1);
279 GL::translate(0.03, -0.02, 0);
280 GL::scale_uniform(0.04);
281 ui_res.get_default_font().draw_string((*i)->get_name());
282 GL::Texture::unbind();
285 GL::matrix_mode(GL::PROJECTION);
287 GL::ortho_bottomleft(screen_w, screen_h);
288 GL::matrix_mode(GL::MODELVIEW);
291 glDisable(GL_DEPTH_TEST);
292 glDisable(GL_LIGHTING);
293 glDisable(GL_MULTISAMPLE);
300 void Engineer::button_press(int x, int y, unsigned btn, unsigned)
304 if(btn==1 && placing_block && !placing_block->get_train())
306 set_block_color(*placing_block, GL::Color(1, 1, 1));
308 placing_train->place(placing_block, placing_entry);
310 main_panel->set_status_text(string());
314 const vector<Block::Endpoint> &endpoints = placing_block->get_endpoints();
315 placing_entry = (placing_entry+1)%endpoints.size();
320 Track3D *track = pick_track(x, screen_h-y-1);
323 if(unsigned tid=track->get_track().get_turnout_id())
325 Turnout &turnout = control.get_turnout(tid);
328 turnout.set_route((turnout.get_route()+1)%track->get_track().get_type().get_n_routes());
329 main_panel->set_status_text(format("Turnout %d switched", turnout.get_address()));
331 catch(const TurnoutBusy &e)
333 main_panel->set_status_text(e.what());
338 if(unsigned sid=track->get_track().get_sensor_id())
340 Sensor &sensor = control.get_sensor(sid);
341 control.signal_sensor_event.emit(sid, !sensor.get_state());
348 void Engineer::button_release(int, int, unsigned, unsigned)
352 void Engineer::pointer_motion(int x, int y)
356 Track3D *track = pick_track(x, screen_h-y-1);
357 if(track && placing_train)
359 Block &block = trfc_mgr->get_block_by_track(track->get_track());
360 if(&block!=placing_block)
363 reset_block_color(*placing_block);
364 placing_block = █
366 set_block_color(*placing_block, GL::Color(0.5, 1, 0.7));
369 else if(track && track->get_track().get_turnout_id())
370 main_panel->set_status_text(format("Turnout %d", track->get_track().get_turnout_id()));
371 else if(!placing_train)
372 main_panel->set_status_text(string());
376 void Engineer::view_all()
378 const list<Track3D *> &tracks = layout_3d.get_tracks();
381 float best_height = -1;
384 for(float angle=0; angle<M_PI; angle+=0.01)
390 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
393 (*i)->get_bounds(angle, minp, maxp);
394 min_x = min(min_x, minp.x);
395 max_x = max(max_x, maxp.x);
396 min_y = min(min_y, minp.y);
397 max_y = max(max_y, maxp.y);
400 float width = max_x-min_x;
401 float height = max_y-min_y;
402 height = max(height, width);
404 if(height<best_height || best_height<0)
407 best_height = height;
408 mid_x = (min_x+max_x)/2;
409 mid_y = (min_y+max_y)/2;
413 float c = cos(cam_rot);
414 float s = sin(cam_rot);
415 cam_pos.x = c*mid_x-s*mid_y;
416 cam_pos.y = s*mid_x+c*mid_y;
417 cam_pos.z = max(best_height*1.05/0.82843, 0.15);
420 void Engineer::set_block_color(const Block &block, const GL::Color &color)
422 const set<Track *> &tracks = block.get_tracks();
423 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
424 layout_3d.get_track(**i).set_color(color);
427 void Engineer::reset_block_color(const Block &block)
429 if(unsigned sid=block.get_sensor_id())
431 Sensor &sensor = control.get_sensor(sid);
432 if(sensor.get_state())
434 set_block_color(block, GL::Color(1, 0.5, 0.3));
439 if(block.get_train())
440 set_block_color(block, GL::Color(1, 1, 0.3));
442 set_block_color(block, GL::Color(1, 1, 1));
445 void Engineer::sensor_event(bool, Sensor *sensor)
447 const list<Block *> &blocks = trfc_mgr->get_blocks();
448 for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
449 if((*i)->get_sensor_id()==sensor->get_address())
450 reset_block_color(**i);
453 void Engineer::block_reserved(const Block &block, const Train *)
455 reset_block_color(block);
458 void Engineer::project_3d()
460 glMatrixMode(GL_PROJECTION);
462 float offset = 200.0/screen_w*0.055228;
463 glFrustum(-0.055228-offset, 0.055228-offset, -0.041421, 0.041421, 0.1, 10);
464 glMatrixMode(GL_MODELVIEW);
467 Track3D *Engineer::pick_track(int x, int y)
469 float xx = (static_cast<float>(x-static_cast<int>(screen_w)/2-100)/screen_h)*0.82843;
470 float yy = (static_cast<float>(y)/screen_h-0.5)*0.82843;
471 float size = 4.0/screen_h*0.82843;
475 glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
476 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
478 return layout_3d.pick_track(xx, yy, size);
481 void Engineer::train_added(Train &train)
483 TrainPanel *tpanel = new TrainPanel(*this, ui_res, train);
485 int y = main_panel->get_geometry().y;
486 for(list<TrainPanel *>::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
487 y -= (*i)->get_geometry().h;
488 tpanel->set_position(0, y-tpanel->get_geometry().h);
489 train_panels.push_back(tpanel);
490 tpanel->set_visible(true);
495 Application::RegApp<Engineer> Engineer::reg;