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):
51 manipulator(*this, mouse, selection),
54 window.set_title("Railway Designer");
55 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
57 selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
58 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
59 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
60 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
61 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
63 // Setup catalogue and layout
64 DataFile::load(catalogue, "tracks.dat");
66 cat_layout_3d = new Layout3D(catalogue.get_layout());
68 layout = new Layout(catalogue);
69 layout_3d = new Layout3D(*layout);
71 layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
72 layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
77 DataFile::load(*layout, argv[1]);
79 if(!layout->get_base().empty())
81 base_object = new GL::Object;
82 DataFile::load(*base_object, layout->get_base());
87 cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
89 main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
90 GL::Pipeline *pipeline = &main_view->get_pipeline();
92 GL::Pipeline::Pass *pass = &pipeline->add_pass("unlit");
93 pass->set_depth_test(&GL::DepthTest::lequal());
94 pass->set_blend(&GL::Blend::alpha());
96 pass = &pipeline->add_pass("blended");
97 pass->set_lighting(&layout_3d->get_lighting());
98 pass->set_depth_test(&GL::DepthTest::lequal());
99 pass->set_blend(&GL::Blend::alpha());
101 pass = &pipeline->add_pass("overlay");
102 pass->set_blend(&GL::Blend::alpha());
105 pipeline->add_renderable(*base_object);
106 pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
107 pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
110 keyboard.signal_button_press.connect(sigc::mem_fun(this, &Designer::key_press));
111 mouse.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
112 mouse.signal_axis_motion.connect(sigc::mem_fun(this, &Designer::axis_motion));
113 root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
115 toolbars.push_back(new Layoutbar(*this));
116 toolbars.push_back(new Trackbar(*this));
117 toolbars.push_back(new Routebar(*this));
118 toolbars.push_back(new Zonebar(*this));
119 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
122 if(i!=toolbars.begin())
124 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(this, &Designer::arrange_toolbars)));
128 GLtk::Panel *statusbar = new GLtk::Panel;
129 root.add(*statusbar);
130 statusbar->set_size(window.get_width(), 20);
131 statusbar->set_focusable(false);
133 lbl_status = new GLtk::Label;
134 statusbar->add(*lbl_status);
135 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
137 track_wrap = new TrackWrap(*layout_3d, selection);
138 pipeline->add_renderable_for_pass(*track_wrap, "unlit");
139 overlay = new Overlay3D(ui_res.get_default_font());
140 pipeline->add_renderable_for_pass(*overlay, "overlay");
142 camera_ctl = new CameraController(*main_view, keyboard, mouse);
143 cat_view->get_camera().set_look_direction(GL::Vector3(0, 0.13053, -0.99144));
144 cat_view->view_all(true);
145 main_view->view_all();
147 const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
148 for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
149 update_track_icon(*i->second);
152 Designer::~Designer()
154 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
164 delete cat_layout_3d;
173 return Application::main();
176 void Designer::save()
178 InputDialog *input = new InputDialog(*this, "Save layout", filename);
179 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
182 void Designer::quit()
187 void Designer::new_track()
193 lbl_status->set_text("Select new track or press Esc to cancel");
196 void Designer::erase_tracks()
201 set<Track *> tracks = selection.get_tracks();
203 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
205 overlay->clear(layout_3d->get_track(**i));
210 void Designer::track_properties()
212 if(mode!=SELECT || selection.empty())
215 TrackProperties *track_prop = new TrackProperties(selection);
216 root.add(*track_prop);
217 const GLtk::Geometry &root_geom = root.get_geometry();
218 const GLtk::Geometry &dlg_geom = track_prop->get_geometry();
219 track_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
221 track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
224 void Designer::extend_track()
229 if(manipulator.start_extend())
233 void Designer::connect_tracks()
238 manipulator.connect();
241 void Designer::flatten_tracks()
246 manipulator.flatten();
249 void Designer::svg_export()
251 InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
252 input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
255 void Designer::edit_route(Route *r)
260 show_route(*cur_route);
265 void Designer::rename_route()
267 if(mode!=SELECT || !cur_route)
270 InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
271 input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
274 void Designer::add_selection_to_route()
281 cur_route->add_tracks(selection.get_tracks());
283 catch(const exception &e)
285 lbl_status->set_text(e.what());
288 show_route(*cur_route);
291 void Designer::edit_zone(Zone *z)
296 show_zone(*cur_zone);
301 void Designer::zone_properties()
306 ZoneProperties *zone_prop = new ZoneProperties(*cur_zone);
307 root.add(*zone_prop);
308 const GLtk::Geometry &root_geom = root.get_geometry();
309 const GLtk::Geometry &dlg_geom = zone_prop->get_geometry();
310 zone_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
313 void Designer::add_selection_to_zone()
320 cur_zone->add_tracks(selection.get_tracks());
322 catch(const exception &e)
324 lbl_status->set_text(e.what());
327 show_zone(*cur_zone);
330 Vector Designer::map_pointer_to_ground(const Vector &p)
332 GL::Vector4 vec = main_view->get_camera().unproject(GL::Vector4(p.x, p.y, 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)
362 bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
364 if(key==Msp::Input::KEY_N && shift)
366 else if(key==Msp::Input::KEY_N)
368 else if(key==Msp::Input::KEY_G)
370 manipulator.start_move();
373 else if(key==Msp::Input::KEY_R)
375 manipulator.start_rotate();
378 else if(key==Msp::Input::KEY_D)
380 manipulator.duplicate();
381 manipulator.start_move();
384 else if(key==Msp::Input::KEY_W)
386 else if(key==Msp::Input::KEY_PLUS)
387 selection.select_more();
388 else if(key==Msp::Input::KEY_L && shift)
389 selection.select_blocks();
390 else if(key==Msp::Input::KEY_L)
391 selection.select_linked();
392 else if(key==Msp::Input::KEY_M)
397 else if(key==Msp::Input::KEY_Z)
399 manipulator.start_elevate();
402 else if(key==Msp::Input::KEY_ESC)
405 manipulator.cancel();
406 else if(mode==CATALOGUE)
411 else if(key==Msp::Input::KEY_X)
413 else if(key==Msp::Input::KEY_F && shift)
415 const set<Track *> &tracks = selection.get_tracks();
416 const set<Track *> <racks = layout->get_tracks();
417 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
419 (*i)->set_flex(!(*i)->get_flex());
421 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
423 (*i)->snap_to(**j, true);
425 update_track_icon(layout_3d->get_track(**i));
428 else if(key==Msp::Input::KEY_F)
430 else if(key==Msp::Input::KEY_E && shift)
431 manipulator.even_slope(true);
432 else if(key==Msp::Input::KEY_E)
433 manipulator.even_slope();
434 else if(key==Msp::Input::KEY_A)
437 add_selection_to_route();
439 add_selection_to_zone();
441 else if(key==Msp::Input::KEY_C)
443 else if(key==Msp::Input::KEY_V)
445 else if(key==Msp::Input::KEY_P)
449 void Designer::button_press(unsigned btn)
451 bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
453 Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1));
454 Vector ground = map_pointer_to_ground(pointer);
460 Track *ctrack = pick_track(pointer);
463 Track *track = new Track(*layout, ctrack->get_type());
464 track->set_position(ground);
467 selection.add_track(track);
475 else if(mode==SELECT)
479 Track *track = pick_track(pointer);
484 selection.toggle_track(track);
488 else if(mode==MEASURE)
489 measure.button_press(ground.x, ground.y, btn);
492 void Designer::axis_motion(unsigned, float, float)
494 Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1));
495 float wx = pointer.x*window.get_width();
496 float wy = pointer.y*window.get_height();
498 if(!root.get_child_at(wx, wy))
500 Vector ground = map_pointer_to_ground(pointer);
501 measure.pointer_motion(ground.x, ground.y);
505 void Designer::render()
507 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
509 View3D *view = (mode==CATALOGUE ? cat_view : main_view);
513 GL::Bind bind_depth(GL::DepthTest::lequal());
518 // XXX Should fix GLtk so this would not be needed
519 glColor3f(1.0, 1.0, 1.0);
522 void Designer::track_added(Track &trk)
524 new_tracks.push_back(&trk);
527 void Designer::track_removed(Track &trk)
529 list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
530 if(i!=new_tracks.end())
534 Track *Designer::pick_track(const Vector &pointer)
536 View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
537 const GL::Vector3 &cpos = view.get_camera().get_position();
538 GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(pointer.x, pointer.y, 0, 0));
540 return view.get_layout().get_layout().pick_track(Vector(cpos.x, cpos.y, cpos.z), Vector(cray.x, cray.y, cray.z));
543 void Designer::update_track_icon(Track3D &track)
545 overlay->clear(track);
547 if(track.get_track().get_flex())
548 overlay->add_graphic(track, "flex");
550 if(unsigned sid = track.get_track().get_sensor_id())
552 overlay->add_graphic(track, "sensor");
553 overlay->set_label(track, lexical_cast<string>(sid));
555 else if(unsigned tid = track.get_track().get_turnout_id())
559 overlay->add_graphic(track, "turnout");
560 overlay->set_label(track, lexical_cast<string>(tid));
565 void Designer::selection_changed()
567 const set<Track *> &tracks = selection.get_tracks();
569 lbl_status->set_text(string());
573 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
574 len += (*i)->get_type().get_total_length();
575 lbl_status->set_text(format("%.2fm of track selected\n", len));
579 void Designer::manipulation_status(const string &status)
581 lbl_status->set_text(status);
584 void Designer::manipulation_done(bool)
586 mode = MANIPULATE_DONE;
590 void Designer::measure_changed()
592 float pard = measure.get_parallel_distance()*1000;
593 float perpd = measure.get_perpendicular_distance()*1000;
594 float d = sqrt(pard*pard+perpd*perpd);
595 float adiff = measure.get_angle_difference()*180/M_PI;
596 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
597 lbl_status->set_text(info);
600 void Designer::measure_done()
606 void Designer::arrange_toolbars()
609 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
611 const GLtk::Geometry &geom = (*i)->get_geometry();
612 (*i)->set_position(x, window.get_height()-geom.h);
617 void Designer::track_properties_response(int)
619 const set<Track *> &tracks = selection.get_tracks();
620 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
621 update_track_icon(layout_3d->get_track(**i));
624 void Designer::route_name_accept(const string &text)
627 cur_route->set_name(text);
630 void Designer::svg_export_accept(const string &text)
632 SvgExporter svg_exp(*layout);
636 string Designer::tooltip(int x, int y)
638 if(Track *track = pick_track(Vector(x*2.0f/window.get_width()-1.0f, y*2.0f/window.get_height()-1.0f, 0)))
640 const TrackType &ttype = track->get_type();
641 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
642 if(mode!=CATALOGUE && abs(track->get_slope())>1e-4)
643 info += format(" (slope %.1f%%)", abs(track->get_slope()/ttype.get_total_length()*100));
644 if(track->get_turnout_id())
645 info += format(" (turnout %d)", track->get_turnout_id());
646 else if(track->get_sensor_id())
647 info += format(" (sensor %d)", track->get_sensor_id());
655 void Designer::clear_paths()
657 const set<Track *> <racks = layout->get_tracks();
658 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
660 Track3D &t3d = layout_3d->get_track(**i);
661 t3d.get_path().set_mask(0);
665 void Designer::show_route(const Route &route)
669 const set<Track *> &rtracks = route.get_tracks();
670 for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
672 Track3D &t3d = layout_3d->get_track(**i);
673 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
675 if(unsigned tid = (*i)->get_turnout_id())
676 path = route.get_turnout(tid);
678 t3d.get_path().set_path(path);
680 t3d.get_path().set_mask((*i)->get_type().get_paths());
684 void Designer::show_zone(const Zone &zone)
688 const Zone::TrackSet &ztracks = zone.get_tracks();
689 for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
691 Track3D &t3d = layout_3d->get_track(**i);
692 t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
693 t3d.get_path().set_mask((*i)->get_type().get_paths());