4 #include <msp/fs/utils.h>
5 #include <msp/gl/blend.h>
6 #include <msp/gl/framebuffer.h>
7 #include <msp/gl/matrix.h>
8 #include <msp/gl/misc.h>
9 #include <msp/gl/projection.h>
10 #include <msp/gl/tests.h>
11 #include <msp/gl/texture2d.h>
12 #include <msp/input/keys.h>
13 #include <msp/io/print.h>
14 #include <msp/stringcodec/codec.h>
15 #include <msp/stringcodec/utf8.h>
16 #include <msp/strings/lexicalcast.h>
17 #include <msp/strings/utils.h>
18 #include <msp/time/units.h>
19 #include <msp/time/utils.h>
20 #include "libr2c2/route.h"
21 #include "libr2c2/tracktype.h"
22 #include "libr2c2/zone.h"
26 #include "layoutbar.h"
27 #include "manipulator.h"
30 #include "selection.h"
31 #include "svgexporter.h"
33 #include "trackproperties.h"
35 #include "zoneproperties.h"
41 Designer::Designer(int argc, char **argv):
49 manipulator(*this, root, selection),
52 window.set_title("Railway Designer");
53 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
55 selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
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);
69 layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
70 layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
75 DataFile::load(*layout, argv[1]);
77 if(!layout->get_base().empty())
79 base_object = new GL::Object;
80 DataFile::load(*base_object, layout->get_base());
85 cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
87 main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
88 GL::Pipeline *pipeline = &main_view->get_pipeline();
90 GL::Pipeline::Pass *pass = &pipeline->add_pass("unlit");
91 pass->set_depth_test(&GL::DepthTest::lequal());
92 pass->set_blend(&GL::Blend::alpha());
94 pass = &pipeline->add_pass("blended");
95 pass->set_lighting(&layout_3d->get_lighting());
96 pass->set_depth_test(&GL::DepthTest::lequal());
97 pass->set_blend(&GL::Blend::alpha());
99 pass = &pipeline->add_pass("overlay");
100 pass->set_blend(&GL::Blend::alpha());
103 pipeline->add_renderable(*base_object);
104 pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
105 pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
108 root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
109 root.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
110 root.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
111 root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
113 toolbars.push_back(new Layoutbar(*this));
114 toolbars.push_back(new Trackbar(*this));
115 toolbars.push_back(new Routebar(*this));
116 toolbars.push_back(new Zonebar(*this));
117 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
120 if(i!=toolbars.begin())
122 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(this, &Designer::arrange_toolbars)));
126 GLtk::Panel *statusbar = new GLtk::Panel;
127 root.add(*statusbar);
128 statusbar->set_size(window.get_width(), 20);
129 statusbar->set_focusable(false);
131 lbl_status = new GLtk::Label;
132 statusbar->add(*lbl_status);
133 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
135 track_wrap = new TrackWrap(*layout_3d, selection);
136 pipeline->add_renderable_for_pass(*track_wrap, "unlit");
137 overlay = new Overlay3D(ui_res.get_default_font());
138 pipeline->add_renderable_for_pass(*overlay, "overlay");
140 camera_ctl = new CameraController(*main_view, root);
141 cat_view->get_camera().set_look_direction(GL::Vector3(0, 0.13053, -0.99144));
142 cat_view->view_all(true);
143 main_view->view_all();
145 const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
146 for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
147 update_track_icon(*i->second);
150 Designer::~Designer()
152 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
162 delete cat_layout_3d;
171 return Application::main();
174 void Designer::save()
176 InputDialog *input = new InputDialog(*this, "Save layout", filename);
177 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
180 void Designer::quit()
185 void Designer::new_track()
191 lbl_status->set_text("Select new track or press Esc to cancel");
194 void Designer::erase_tracks()
199 set<Track *> tracks = selection.get_tracks();
201 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
203 overlay->clear(layout_3d->get_track(**i));
208 void Designer::track_properties()
210 if(mode!=SELECT || selection.empty())
213 TrackProperties *track_prop = new TrackProperties(selection);
214 root.add(*track_prop);
215 const GLtk::Geometry &root_geom = root.get_geometry();
216 const GLtk::Geometry &dlg_geom = track_prop->get_geometry();
217 track_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
219 track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
222 void Designer::extend_track()
227 if(manipulator.start_extend())
231 void Designer::connect_tracks()
236 manipulator.connect();
239 void Designer::flatten_tracks()
244 manipulator.flatten();
247 void Designer::svg_export()
249 InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
250 input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
253 void Designer::edit_route(Route *r)
258 show_route(*cur_route);
263 void Designer::rename_route()
265 if(mode!=SELECT || !cur_route)
268 InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
269 input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
272 void Designer::add_selection_to_route()
279 cur_route->add_tracks(selection.get_tracks());
281 catch(const exception &e)
283 lbl_status->set_text(e.what());
286 show_route(*cur_route);
289 void Designer::edit_zone(Zone *z)
294 show_zone(*cur_zone);
299 void Designer::zone_properties()
304 ZoneProperties *zone_prop = new ZoneProperties(*cur_zone);
305 root.add(*zone_prop);
306 const GLtk::Geometry &root_geom = root.get_geometry();
307 const GLtk::Geometry &dlg_geom = zone_prop->get_geometry();
308 zone_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
311 void Designer::add_selection_to_zone()
318 cur_zone->add_tracks(selection.get_tracks());
320 catch(const exception &e)
322 lbl_status->set_text(e.what());
325 show_zone(*cur_zone);
328 Vector Designer::map_pointer_to_ground(int x, int y)
330 float xf = x*2.0/window.get_width()-1.0;
331 float yf = y*2.0/window.get_height()-1.0;
332 GL::Vector4 vec = main_view->get_camera().unproject(GL::Vector4(xf, yf, 0, 0));
333 const GL::Vector3 &pos = main_view->get_camera().get_position();
335 return Vector(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
338 void Designer::tick()
340 const Msp::Time::TimeStamp t = Msp::Time::now();
341 float dt = (t-last_tick)/Msp::Time::sec;
344 if(mode==MANIPULATE_DONE)
349 camera_ctl->tick(dt);
351 for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
352 layout_3d->get_track(**i).get_path().set_mask(0);
357 window.swap_buffers();
360 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
362 key = Input::key_from_sys(key);
363 mod = Input::mod_from_sys(mod);
365 if(key==Msp::Input::KEY_N && (mod&Input::MOD_SHIFT))
367 else if(key==Msp::Input::KEY_N)
369 else if(key==Msp::Input::KEY_G)
371 manipulator.start_move();
374 else if(key==Msp::Input::KEY_R)
376 manipulator.start_rotate();
379 else if(key==Msp::Input::KEY_D)
381 manipulator.duplicate();
382 manipulator.start_move();
385 else if(key==Msp::Input::KEY_W)
387 else if(key==Msp::Input::KEY_PLUS)
388 selection.select_more();
389 else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
390 selection.select_blocks();
391 else if(key==Msp::Input::KEY_L)
392 selection.select_linked();
393 else if(key==Msp::Input::KEY_M)
398 else if(key==Msp::Input::KEY_Z)
400 manipulator.start_elevate();
403 else if(key==Msp::Input::KEY_ESC)
406 manipulator.cancel();
407 else if(mode==CATALOGUE)
412 else if(key==Msp::Input::KEY_X)
414 else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
416 const set<Track *> &tracks = selection.get_tracks();
417 const set<Track *> <racks = layout->get_tracks();
418 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
420 (*i)->set_flex(!(*i)->get_flex());
422 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
424 (*i)->snap_to(**j, true);
426 update_track_icon(layout_3d->get_track(**i));
429 else if(key==Msp::Input::KEY_F)
431 else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
432 manipulator.even_slope(true);
433 else if(key==Msp::Input::KEY_E)
434 manipulator.even_slope();
435 else if(key==Msp::Input::KEY_A)
438 add_selection_to_route();
440 add_selection_to_zone();
442 else if(key==Msp::Input::KEY_C)
444 else if(key==Msp::Input::KEY_V)
446 else if(key==Msp::Input::KEY_P)
450 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
452 y = window.get_height()-y-1;
453 mod = Input::mod_from_sys(mod);
455 Vector ground = map_pointer_to_ground(x, y);
461 Track *ctrack = pick_track(x, y);
464 Track *track = new Track(*layout, ctrack->get_type());
465 track->set_position(ground);
468 selection.add_track(track);
476 else if(mode==SELECT)
480 Track *track = pick_track(x, y);
483 if(!(mod&Input::MOD_SHIFT))
485 selection.toggle_track(track);
489 else if(mode==MEASURE)
490 measure.button_press(x, y, ground.x, ground.y, btn);
493 void Designer::pointer_motion(int x, int y)
495 y = window.get_height()-y-1;
497 if(!root.get_child_at(x, y))
499 Vector ground = map_pointer_to_ground(x, y);
500 measure.pointer_motion(x, y, ground.x, ground.y);
504 void Designer::render()
506 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
508 View3D *view = (mode==CATALOGUE ? cat_view : main_view);
512 GL::Bind bind_depth(GL::DepthTest::lequal());
517 // XXX Should fix GLtk so this would not be needed
518 glColor3f(1.0, 1.0, 1.0);
521 void Designer::track_added(Track &trk)
523 new_tracks.push_back(&trk);
526 void Designer::track_removed(Track &trk)
528 list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
529 if(i!=new_tracks.end())
533 Track *Designer::pick_track(int x, int y)
535 float xx = x*2.0/window.get_width()-1.0;
536 float yy = y*2.0/window.get_height()-1.0;
538 View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
539 const GL::Vector3 &cpos = view.get_camera().get_position();
540 GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(xx, yy, 0, 0));
542 return view.get_layout().get_layout().pick_track(Vector(cpos.x, cpos.y, cpos.z), Vector(cray.x, cray.y, cray.z));
545 void Designer::update_track_icon(Track3D &track)
547 overlay->clear(track);
549 if(track.get_track().get_flex())
550 overlay->add_graphic(track, "flex");
552 if(unsigned sid = track.get_track().get_sensor_id())
554 overlay->add_graphic(track, "sensor");
555 overlay->set_label(track, lexical_cast<string>(sid));
557 else if(unsigned tid = track.get_track().get_turnout_id())
561 overlay->add_graphic(track, "turnout");
562 overlay->set_label(track, lexical_cast<string>(tid));
567 void Designer::selection_changed()
569 const set<Track *> &tracks = selection.get_tracks();
571 lbl_status->set_text(string());
575 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
576 len += (*i)->get_type().get_total_length();
577 lbl_status->set_text(format("%.2fm of track selected\n", len));
581 void Designer::manipulation_status(const string &status)
583 lbl_status->set_text(status);
586 void Designer::manipulation_done(bool)
588 mode = MANIPULATE_DONE;
592 void Designer::measure_changed()
594 float pard = measure.get_parallel_distance()*1000;
595 float perpd = measure.get_perpendicular_distance()*1000;
596 float d = sqrt(pard*pard+perpd*perpd);
597 float adiff = measure.get_angle_difference()*180/M_PI;
598 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
599 lbl_status->set_text(info);
602 void Designer::measure_done()
608 void Designer::arrange_toolbars()
611 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
613 const GLtk::Geometry &geom = (*i)->get_geometry();
614 (*i)->set_position(x, window.get_height()-geom.h);
619 void Designer::track_properties_response(int)
621 const set<Track *> &tracks = selection.get_tracks();
622 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
623 update_track_icon(layout_3d->get_track(**i));
626 void Designer::route_name_accept(const string &text)
629 cur_route->set_name(text);
632 void Designer::svg_export_accept(const string &text)
634 SvgExporter svg_exp(*layout);
638 string Designer::tooltip(int x, int y)
640 if(Track *track = pick_track(x, y))
642 const TrackType &ttype = track->get_type();
643 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
644 if(mode!=CATALOGUE && abs(track->get_slope())>1e-4)
645 info += format(" (slope %.1f%%)", abs(track->get_slope()/ttype.get_total_length()*100));
646 if(track->get_turnout_id())
647 info += format(" (turnout %d)", track->get_turnout_id());
648 else if(track->get_sensor_id())
649 info += format(" (sensor %d)", track->get_sensor_id());
657 void Designer::clear_paths()
659 const set<Track *> <racks = layout->get_tracks();
660 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
662 Track3D &t3d = layout_3d->get_track(**i);
663 t3d.get_path().set_mask(0);
667 void Designer::show_route(const Route &route)
671 const set<Track *> &rtracks = route.get_tracks();
672 for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
674 Track3D &t3d = layout_3d->get_track(**i);
675 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
677 if(unsigned tid = (*i)->get_turnout_id())
678 path = route.get_turnout(tid);
680 t3d.get_path().set_path(path);
682 t3d.get_path().set_mask((*i)->get_type().get_paths());
686 void Designer::show_zone(const Zone &zone)
690 const Zone::TrackSet &ztracks = zone.get_tracks();
691 for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
693 Track3D &t3d = layout_3d->get_track(**i);
694 t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
695 t3d.get_path().set_mask((*i)->get_type().get_paths());