3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
11 #include <msp/gl/blend.h>
12 #include <msp/gl/framebuffer.h>
13 #include <msp/gl/matrix.h>
14 #include <msp/gl/misc.h>
15 #include <msp/gl/projection.h>
16 #include <msp/gl/rendermode.h>
17 #include <msp/gl/select.h>
18 #include <msp/gl/tests.h>
19 #include <msp/gl/texture2d.h>
20 #include <msp/input/keys.h>
21 #include <msp/io/print.h>
22 #include <msp/strings/codec.h>
23 #include <msp/strings/lexicalcast.h>
24 #include <msp/strings/utf8.h>
25 #include <msp/strings/utils.h>
26 #include <msp/time/units.h>
27 #include <msp/time/utils.h>
28 #include "libmarklin/route.h"
29 #include "libmarklin/tracktype.h"
33 #include "manipulator.h"
35 #include "selection.h"
39 using namespace Marklin;
42 Application::RegApp<Designer> Designer::reg;
44 Designer::Designer(int argc, char **argv):
49 manipulator(*this, selection),
51 camera_ctl(window, camera)
53 window.set_title("Railway Designer");
54 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
56 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
57 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
58 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
59 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
61 // Setup catalogue and layout
62 DataFile::load(catalogue, "tracks.dat");
64 cat_layout_3d = new Layout3D(catalogue.get_layout());
66 layout = new Layout(catalogue);
67 layout_3d = new Layout3D(*layout);
72 DataFile::load(*layout, argv[1]);
74 if(!layout->get_base().empty())
76 base_object = new GL::Object;
77 DataFile::load(*base_object, layout->get_base());
82 GL::enable(GL::DEPTH_TEST);
83 GL::enable(GL::BLEND);
84 GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
85 GL::enable(GL_CULL_FACE);
87 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
88 pipeline->set_camera(&camera);
89 pipeline->add_renderable(layout_3d->get_scene());
91 pipeline->add_renderable(*base_object);
93 light.set_position(0, -0.259, 0.966, 0);
94 lighting.attach(0, light);
96 GL::PipelinePass *pass = &pipeline->add_pass(GL::Tag());
97 pass->lighting = &lighting;
99 camera.set_up_direction(GL::Vector3(0, 0, 1));
103 DataFile::load(ui_res, "marklin.res");
104 root = new GLtk::Root(ui_res, window);
105 root->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
106 root->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
107 root->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
108 root->signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
110 toolbar = new Toolbar(*this);
112 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
113 toolbar->set_focusable(false);
115 GLtk::Panel *statusbar = new GLtk::Panel(ui_res);
116 root->add(*statusbar);
117 statusbar->set_size(window.get_width(), 20);
118 statusbar->set_focusable(false);
120 lbl_status = new GLtk::Label(ui_res);
121 statusbar->add(*lbl_status);
122 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
124 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
126 const list<Track3D *> &tracks = layout_3d->get_tracks();
127 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
128 update_track_icon(**i);
131 Designer::~Designer()
139 delete cat_layout_3d;
148 return Application::main();
151 void Designer::save()
153 InputDialog *input = new InputDialog(*this, "Save layout", filename);
154 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
157 void Designer::quit()
162 void Designer::new_track()
167 void Designer::set_turnout_id()
169 Track *track = selection.get_track();
170 if(selection.size()==1 && track->get_type().is_turnout())
172 InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
173 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
177 void Designer::set_sensor_id()
179 const set<Track *> &tracks = selection.get_tracks();
182 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
184 if(!(*i)->get_type().is_turnout())
186 if(static_cast<int>((*i)->get_sensor_id())!=id)
189 id = (*i)->get_sensor_id();
197 InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
198 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
202 void Designer::edit_route(Route &r)
208 void Designer::add_selection_to_route()
215 const set<Track *> &stracks = selection.get_tracks();
216 set<const Track *> tracks(stracks.begin(), stracks.end());
217 cur_route->add_tracks(tracks);
219 catch(const Exception &e)
221 IO::print("%s\n", e.what());
224 show_route(*cur_route);
227 Point Designer::map_pointer_coords(int x, int y)
229 float xf = x*2.0/window.get_width()-1.0;
230 float yf = y*2.0/window.get_height()-1.0;
231 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
232 const GL::Vector3 &pos = camera.get_position();
234 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
237 void Designer::tick()
239 const Msp::Time::TimeStamp t = Msp::Time::now();
240 float dt = (t-last_tick)/Msp::Time::sec;
243 window.get_display().tick();
249 window.swap_buffers();
252 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
254 key = Input::key_from_sys(key);
255 mod = Input::mod_from_sys(mod);
257 if(key==Msp::Input::KEY_N)
259 else if(key==Msp::Input::KEY_G)
261 manipulator.start_move();
264 else if(key==Msp::Input::KEY_R)
266 manipulator.start_rotate();
269 else if(key==Msp::Input::KEY_D)
271 manipulator.duplicate();
272 manipulator.start_move();
275 else if(key==Msp::Input::KEY_W)
277 else if(key==Msp::Input::KEY_PLUS)
278 selection.select_more();
279 else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
281 const set<Track *> &tracks = layout->get_tracks();
283 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
284 len += (*i)->get_type().get_total_length();
285 IO::print("Total length: %.1fm\n", len);
287 else if(key==Msp::Input::KEY_L)
288 selection.select_linked();
289 else if(key==Msp::Input::KEY_M)
294 else if(key==Msp::Input::KEY_Z)
296 manipulator.start_elevate();
299 else if(key==Msp::Input::KEY_ESC)
302 manipulator.cancel();
303 else if(mode==CATALOGUE)
308 else if(key==Msp::Input::KEY_X)
310 set<Track *> tracks = selection.get_tracks();
312 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
314 overlay->clear(layout_3d->get_track(**i));
315 layout->remove_track(**i);
319 else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
321 const set<Track *> &tracks = selection.get_tracks();
322 const set<Track *> <racks = layout->get_tracks();
323 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
325 (*i)->set_flex(!(*i)->get_flex());
327 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
329 (*i)->snap_to(**j, true);
331 update_track_icon(layout_3d->get_track(**i));
334 else if(key==Msp::Input::KEY_F)
335 manipulator.flatten();
336 else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
337 manipulator.even_slope(true);
338 else if(key==Msp::Input::KEY_E)
339 manipulator.even_slope();
340 else if(key==Msp::Input::KEY_T)
342 else if(key==Msp::Input::KEY_S)
344 else if(key==Msp::Input::KEY_A)
345 add_selection_to_route();
348 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
350 y = window.get_height()-y-1;
351 mod = Input::mod_from_sys(mod);
353 Point ground = map_pointer_coords(x, y);
359 Track3D *ctrack = pick_track(x, y);
362 Track *track = new Track(*layout, ctrack->get_track().get_type());
363 track->set_position(ground);
366 selection.add_track(track);
374 else if(mode==SELECT)
378 Track3D *track = pick_track(x, y);
381 if(!(mod&Input::MOD_SHIFT))
383 selection.toggle_track(&track->get_track());
387 else if(mode==MANIPULATE)
388 manipulator.button_press(x, y, ground.x, ground.y, btn);
389 else if(mode==MEASURE)
390 measure.button_press(x, y, ground.x, ground.y, btn);
393 void Designer::pointer_motion(int x, int y)
395 y = window.get_height()-y-1;
397 if(!root->get_child_at(x, y))
399 Point ground = map_pointer_coords(x, y);
400 manipulator.pointer_motion(x, y, ground.x, ground.y);
401 measure.pointer_motion(x, y, ground.x, ground.y);
405 void Designer::apply_camera()
409 GL::matrix_mode(GL::PROJECTION);
411 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
412 GL::matrix_mode(GL::MODELVIEW);
414 GL::translate(0, 0, -1);
420 void Designer::render()
422 GL::clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
423 GL::enable(GL::DEPTH_TEST);
424 GL::Texture::unbind();
429 cat_layout_3d->get_scene().render();
433 pipeline->render_all();
434 layout_3d->get_endpoint_scene().render();
436 layout_3d->get_path_scene().render();
437 GL::enable(GL_CULL_FACE);
438 GL::disable(GL::DEPTH_TEST);
440 GL::enable(GL::DEPTH_TEST);
442 manipulator.render();
447 GL::matrix_mode(GL::PROJECTION);
449 GL::ortho_bottomleft(window.get_width(), window.get_height());
450 GL::matrix_mode(GL::MODELVIEW);
453 GL::disable(GL::DEPTH_TEST);
458 Track3D *Designer::pick_track(int x, int y)
460 Layout3D *l = layout_3d;
464 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
465 float yy = (float(y)/window.get_height()-0.5)*0.82843;
466 float size = 4.0/window.get_height()*0.82843;
470 return l->pick_track(xx, yy, size);
473 void Designer::update_track_icon(Track3D &track)
475 overlay->clear(track);
477 if(track.get_track().get_flex())
478 overlay->add_graphic(track, "flex");
480 if(unsigned sid = track.get_track().get_sensor_id())
482 overlay->add_graphic(track, "sensor");
483 overlay->set_label(track, lexical_cast(sid));
485 else if(unsigned tid = track.get_track().get_turnout_id())
487 overlay->add_graphic(track, "turnout");
488 overlay->set_label(track, lexical_cast(tid));
492 void Designer::manipulation_status(const string &status)
494 lbl_status->set_text(status);
497 void Designer::manipulation_done(bool)
499 lbl_status->set_text(string());
503 void Designer::measure_changed()
505 float pard = measure.get_parallel_distance()*1000;
506 float perpd = measure.get_perpendicular_distance()*1000;
507 float d = sqrt(pard*pard+perpd*perpd);
508 float adiff = measure.get_angle_difference()*180/M_PI;
509 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
510 lbl_status->set_text(info);
513 void Designer::measure_done()
515 lbl_status->set_text(string());
519 void Designer::turnout_id_accept(const string &text)
521 Track *track = selection.get_track();
522 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
523 track->set_turnout_id(id);
525 update_track_icon(layout_3d->get_track(*track));
528 void Designer::sensor_id_accept(const string &text)
530 const set<Track *> &tracks = selection.get_tracks();
531 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
532 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
534 (*i)->set_sensor_id(id);
536 update_track_icon(layout_3d->get_track(**i));
540 void Designer::view_all()
545 const list<Track3D *> &tracks = layout_3d->get_tracks();
546 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
550 (*i)->get_bounds(0, tmin, tmax);
551 minp.x = min(minp.x, tmin.x);
552 minp.y = min(minp.y, tmin.y);
553 maxp.x = max(maxp.x, tmax.x);
554 maxp.y = max(maxp.y, tmax.y);
557 float t = tan(camera.get_field_of_view()/2)*2;
558 float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
559 float cam_dist = size/t+size*0.25;
560 Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2);
561 camera.set_position(GL::Vector3(center.x, center.y-cam_dist*0.5, cam_dist*0.866));
562 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
565 string Designer::tooltip(int x, int y)
567 if(Track3D *t3d = pick_track(x, y))
569 const Track &track = t3d->get_track();
570 const TrackType &ttype = track.get_type();
571 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
572 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
573 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
574 if(track.get_turnout_id())
575 info += format(" (turnout %d)", track.get_turnout_id());
576 else if(track.get_sensor_id())
577 info += format(" (sensor %d)", track.get_sensor_id());
585 void Designer::show_route(const Route &route)
587 const set<Track *> <racks = layout->get_tracks();
588 const set<const Track *> &rtracks = route.get_tracks();
589 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
591 Track3D &t3d = layout_3d->get_track(**i);
592 if(rtracks.count(*i))
594 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
595 if((*i)->get_type().is_turnout())
597 unsigned tid = (*i)->get_turnout_id();
598 int path = (tid ? route.get_turnout(tid) : -1);
600 t3d.get_path().set_path(path);
602 t3d.get_path().set_mask((*i)->get_type().get_paths());
605 t3d.get_path().set_path(0);
608 t3d.get_path().set_mask(0);