3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
11 #include <msp/core/except.h>
12 #include <msp/core/getopt.h>
13 #include <msp/gbase/display.h>
14 #include <msp/gbase/window.h>
15 #include <msp/gl/immediate.h>
16 #include <msp/gl/matrix.h>
17 #include <msp/gl/projection.h>
18 #include <msp/gl/transform.h>
19 #include <msp/strings/formatter.h>
20 #include <msp/strings/lexicalcast.h>
21 #include <msp/strings/regex.h>
23 #include "mainpanel.h"
24 #include "trainpanel.h"
25 #include "trainproperties.h"
28 using namespace Marklin;
33 Engineer::Engineer(int argc, char **argv):
45 train_prop_stale(false)
49 string device="/dev/ttyS0";
53 getopt.add_option('r', "resolution", res, GetOpt::REQUIRED_ARG);
54 getopt.add_option('f', "fullscreen", fullscreen, GetOpt::NO_ARG);
55 getopt.add_option('g', "debug", debug, GetOpt::NO_ARG);
56 getopt.add_option('d', "device", device, GetOpt::REQUIRED_ARG);
57 getopt.add_option('q', "quality", quality, GetOpt::REQUIRED_ARG);
58 getopt.add_option('s', "simulate", simulate, GetOpt::NO_ARG);
59 getopt.add_option( "no-lighting", no_lighting, GetOpt::NO_ARG);
64 if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res))
66 screen_w=lexical_cast<unsigned>(m[1].str);
67 screen_h=lexical_cast<unsigned>(m[2].str);
70 throw UsageError("Invalid resolution");
76 control.set_debug(debug);
78 layout_3d.set_quality(quality);
80 catalogue.load("tracks.dat");
81 catalogue.load("locos.dat");
83 const vector<string> &args=getopt.get_args();
85 throw UsageError("No layout given");
86 layout.load(args.front());
88 trfc_mgr=new TrafficManager(control, layout);
89 trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
91 const map<unsigned, Sensor *> &sensors=control.get_sensors();
92 for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
93 i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_event), i->second));
103 void Engineer::add_train()
105 train_prop=new TrainProperties(*this, ui_res, 0);
106 root->add(*train_prop);
107 train_prop->signal_ok.connect(sigc::mem_fun(this, &Engineer::dismiss_train_prop));
108 train_prop_stale=false;
109 train_prop->set_visible(true);
112 Train *Engineer::add_train(const LocoType &type, unsigned addr)
114 Locomotive *loco=new Locomotive(type, control, addr);
115 Train *train=new Train(*trfc_mgr, *loco);
116 train->set_name(format("Train %d", trfc_mgr->get_trains().size()));
118 TrainPanel *tpanel=new TrainPanel(*this, ui_res, *train);
120 int y=main_panel->get_geometry().y;
121 for(list<TrainPanel *>::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
122 y-=(*i)->get_geometry().h;
123 tpanel->set_position(0, y-tpanel->get_geometry().h);
124 train_panels.push_back(tpanel);
125 tpanel->set_visible(true);
132 void Engineer::place_train(Train &train)
134 placing_train=&train;
136 main_panel->set_status_text("Select location");
141 dpy=new Graphics::Display;
143 Graphics::WindowOptions wopt;
145 wopt.height=screen_h;
146 wopt.fullscreen=fullscreen;
147 wnd=new Graphics::Window(*dpy, wopt);
149 Graphics::GLOptions glopt;
150 //glopt.multisample=4;
151 glc=new Graphics::GLContext(*wnd, glopt);
153 wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
154 wnd->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press));
155 wnd->signal_button_release.connect(sigc::mem_fun(this, &Engineer::button_release));
156 wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion));
158 glEnableClientState(GL_VERTEX_ARRAY);
159 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
160 glEnable(GL_COLOR_MATERIAL);
161 glDepthFunc(GL_LEQUAL);
163 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
165 DataFile::load(ui_res, "marklin.res");
166 root=new GLtk::Root(ui_res, *wnd);
167 root->set_visible(true);
169 list<GL::Texture *> texs=ui_res.get_list<GL::Texture>();
170 for(list<GL::Texture *>::iterator i=texs.begin(); i!=texs.end(); ++i)
172 (*i)->set_min_filter(GL::NEAREST);
173 (*i)->set_mag_filter(GL::NEAREST);
176 main_panel=new MainPanel(*this, ui_res);
177 root->add(*main_panel);
178 main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
179 main_panel->set_visible(true);
192 void Engineer::tick()
199 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
203 glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
204 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
208 glEnable(GL_LIGHTING);
215 glLightfv(GL_LIGHT0, GL_POSITION, params);
218 //glEnable(GL_DEPTH_TEST);
219 glEnable(GL_MULTISAMPLE);
223 glDisable(GL_LIGHTING);
224 glColor4f(1, 1, 1, 1);
225 const list<Track3D *> <racks=layout_3d.get_tracks();
226 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
228 Track &track=(*i)->get_track();
229 if(track.get_turnout_id())
231 Turnout &trnt=control.get_turnout(track.get_turnout_id());
232 (*i)->render_route(trnt.get_route());
235 (*i)->render_route(-1);
238 if(placing_train && placing_block)
242 const Marklin::Block::Endpoint &bep=placing_block->get_endpoints()[placing_entry];
243 float rot=bep.track->get_endpoint_direction(bep.track_ep);
244 Point pos=bep.track->get_endpoint_position(bep.track_ep);
246 GL::translate(pos.x, pos.y, pos.z+0.03);
247 GL::rotate(rot*180/M_PI+180, 0, 0, 1);
248 GL::Texture::unbind();
250 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
251 imm.color(1.0f, 1.0f, 1.0f);
252 imm.begin(GL::TRIANGLE_FAN);
254 imm.vertex(0.05, 0.03);
255 imm.vertex(0.05, 0.01);
257 imm.vertex(0, -0.01);
258 imm.vertex(0.05, -0.01);
259 imm.vertex(0.05, -0.03);
265 GL::matrix_mode(GL::PROJECTION);
267 GL::ortho_bottomleft(screen_w, screen_h);
268 GL::matrix_mode(GL::MODELVIEW);
271 glDisable(GL_DEPTH_TEST);
272 glDisable(GL_LIGHTING);
273 glDisable(GL_MULTISAMPLE);
286 void Engineer::key_press(unsigned key, unsigned mod, wchar_t ch)
289 train_prop->key_press(key, mod, ch);
292 void Engineer::button_press(int x, int y, unsigned btn, unsigned)
296 if(btn==1 && placing_block && !placing_block->get_train())
298 set_block_color(*placing_block, GL::Color(1, 1, 1));
300 placing_train->place(placing_block, placing_entry);
302 main_panel->set_status_text(string());
306 const vector<Block::Endpoint> &endpoints=placing_block->get_endpoints();
307 placing_entry=(placing_entry+1)%endpoints.size();
312 Track3D *track=pick_track(x, screen_h-y-1);
315 if(unsigned tid=track->get_track().get_turnout_id())
317 Turnout &turnout=control.get_turnout(tid);
318 turnout.set_route(1-turnout.get_route());
322 if(unsigned sid=track->get_track().get_sensor_id())
324 Sensor &sensor=control.get_sensor(sid);
325 control.signal_sensor_event.emit(sid, !sensor.get_state());
332 void Engineer::button_release(int, int, unsigned, unsigned)
336 void Engineer::pointer_motion(int x, int y)
340 Track3D *track=pick_track(x, screen_h-y-1);
341 if(track && placing_train)
343 Block &block=trfc_mgr->get_block_by_track(track->get_track());
344 if(&block!=placing_block)
347 reset_block_color(*placing_block);
348 placing_block=█
350 set_block_color(*placing_block, GL::Color(0.5, 1, 0.7));
353 else if(track && track->get_track().get_turnout_id())
354 main_panel->set_status_text(format("Turnout %d", track->get_track().get_turnout_id()));
355 else if(!placing_train)
356 main_panel->set_status_text(string());
360 void Engineer::view_all()
362 const list<Track3D *> &tracks=layout_3d.get_tracks();
365 float best_height=-1;
368 for(float angle=0; angle<M_PI; angle+=0.01)
374 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
377 (*i)->get_bounds(angle, minp, maxp);
378 min_x=min(min_x, minp.x);
379 max_x=max(max_x, maxp.x);
380 min_y=min(min_y, minp.y);
381 max_y=max(max_y, maxp.y);
384 float width=max_x-min_x;
385 float height=max_y-min_y;
386 height=max(height, width);
388 if(height<best_height || best_height<0)
392 mid_x=(min_x+max_x)/2;
393 mid_y=(min_y+max_y)/2;
397 float c=cos(cam_rot);
398 float s=sin(cam_rot);
399 cam_pos.x=c*mid_x-s*mid_y;
400 cam_pos.y=s*mid_x+c*mid_y;
401 cam_pos.z=max(best_height*1.05/0.82843, 0.15);
404 void Engineer::set_block_color(const Block &block, const GL::Color &color)
406 const set<Track *> &tracks=block.get_tracks();
407 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
408 layout_3d.get_track(**i).set_color(color);
411 void Engineer::reset_block_color(const Block &block)
413 if(unsigned sid=block.get_sensor_id())
415 Sensor &sensor=control.get_sensor(sid);
416 if(sensor.get_state())
418 set_block_color(block, GL::Color(1, 0.5, 0.3));
423 if(block.get_train())
424 set_block_color(block, GL::Color(1, 1, 0.3));
426 set_block_color(block, GL::Color(1, 1, 1));
429 void Engineer::sensor_event(bool, Sensor *sensor)
431 const list<Block *> &blocks=trfc_mgr->get_blocks();
432 for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
433 if((*i)->get_sensor_id()==sensor->get_address())
434 reset_block_color(**i);
437 void Engineer::block_reserved(const Block &block, const Train *)
439 reset_block_color(block);
442 void Engineer::project_3d()
444 glMatrixMode(GL_PROJECTION);
446 float offset=200.0/screen_w*0.055228;
447 glFrustum(-0.055228-offset, 0.055228-offset, -0.041421, 0.041421, 0.1, 10);
448 glMatrixMode(GL_MODELVIEW);
451 Track3D *Engineer::pick_track(int x, int y)
453 float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2-100)/screen_h)*0.82843;
454 float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
455 float size=4.0/screen_h*0.82843;
459 glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
460 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
462 return layout_3d.pick_track(xx, yy, size);
465 void Engineer::dismiss_train_prop()
467 train_prop_stale=true;
470 Application::RegApp<Engineer> Engineer::reg;