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/tests.h>
10 #include <msp/gl/texture2d.h>
11 #include <msp/input/keys.h>
12 #include <msp/io/print.h>
13 #include <msp/stringcodec/codec.h>
14 #include <msp/stringcodec/utf8.h>
15 #include <msp/strings/lexicalcast.h>
16 #include <msp/strings/utils.h>
17 #include <msp/time/units.h>
18 #include <msp/time/utils.h>
19 #include "libr2c2/route.h"
20 #include "libr2c2/tracktype.h"
21 #include "libr2c2/zone.h"
25 #include "layoutbar.h"
26 #include "manipulator.h"
29 #include "selection.h"
30 #include "svgexporter.h"
32 #include "trackproperties.h"
34 #include "zoneproperties.h"
40 Designer::Designer(int argc, char **argv):
45 root(ui_res, &window, &keyboard, &mouse),
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::bind_return(sigc::mem_fun(this, &Designer::key_press), false));
111 mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Designer::button_press), false));
112 mouse.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Designer::axis_motion), false));
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 pipeline->add_renderable_for_pass(sel_wrap, "unlit");
138 overlay = new Overlay3D(ui_res.get_default_font());
139 pipeline->add_renderable_for_pass(*overlay, "overlay");
140 pipeline->add_renderable_for_pass(measure, "unlit");
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)
163 delete cat_layout_3d;
172 return Application::main();
175 void Designer::save()
177 InputDialog *input = new InputDialog(*this, "Save layout", filename);
178 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
181 void Designer::quit()
186 void Designer::new_track()
192 lbl_status->set_text("Select new track or press Esc to cancel");
195 void Designer::erase_tracks()
200 set<Track *> tracks = selection.get_objects<Track>();
202 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
204 overlay->clear(layout_3d->get_track(**i));
209 void Designer::track_properties()
211 if(mode!=SELECT || selection.empty())
214 TrackProperties *track_prop = new TrackProperties(selection);
215 root.add(*track_prop);
216 const GLtk::Geometry &root_geom = root.get_geometry();
217 const GLtk::Geometry &dlg_geom = track_prop->get_geometry();
218 track_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
220 track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
223 void Designer::extend_track()
228 if(manipulator.start_extend())
232 void Designer::connect_tracks()
237 manipulator.connect();
240 void Designer::flatten_tracks()
245 manipulator.flatten();
248 void Designer::svg_export()
250 InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
251 input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
254 void Designer::edit_route(Route *r)
259 show_route(*cur_route);
264 void Designer::rename_route()
266 if(mode!=SELECT || !cur_route)
269 InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
270 input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
273 void Designer::add_selection_to_route()
280 cur_route->add_tracks(selection.get_objects<Track>());
282 catch(const exception &e)
284 lbl_status->set_text(e.what());
287 show_route(*cur_route);
290 void Designer::edit_zone(Zone *z)
295 show_zone(*cur_zone);
300 void Designer::zone_properties()
305 ZoneProperties *zone_prop = new ZoneProperties(*cur_zone);
306 root.add(*zone_prop);
307 const GLtk::Geometry &root_geom = root.get_geometry();
308 const GLtk::Geometry &dlg_geom = zone_prop->get_geometry();
309 zone_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
312 void Designer::add_selection_to_zone()
319 cur_zone->add_tracks(selection.get_objects<Track>());
321 catch(const exception &e)
323 lbl_status->set_text(e.what());
326 show_zone(*cur_zone);
329 Vector Designer::map_pointer_to_ground(const Vector &p)
331 GL::Vector4 vec = main_view->get_camera().unproject(GL::Vector4(p.x, p.y, 0, 0));
332 const GL::Vector3 &pos = main_view->get_camera().get_position();
334 return pos-GL::Vector3(vec.x, vec.y, vec.z)*(pos.z/vec.z);
337 void Designer::tick()
339 const Msp::Time::TimeStamp t = Msp::Time::now();
340 float dt = (t-last_tick)/Msp::Time::sec;
343 if(mode==MANIPULATE_DONE)
348 camera_ctl->tick(dt);
350 for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
351 layout_3d->get_track(**i).get_path().set_mask(0);
356 window.swap_buffers();
359 void Designer::key_press(unsigned key)
361 bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
363 if(key==Msp::Input::KEY_N && shift)
365 else if(key==Msp::Input::KEY_N)
367 else if(key==Msp::Input::KEY_G)
369 manipulator.start_move();
372 else if(key==Msp::Input::KEY_R)
374 manipulator.start_rotate();
377 else if(key==Msp::Input::KEY_D)
379 manipulator.duplicate();
380 manipulator.start_move();
383 else if(key==Msp::Input::KEY_W)
385 else if(key==Msp::Input::KEY_PLUS)
386 selection.select_more();
387 else if(key==Msp::Input::KEY_L && shift)
388 selection.select_blocks();
389 else if(key==Msp::Input::KEY_L)
390 selection.select_linked();
391 else if(key==Msp::Input::KEY_M)
396 else if(key==Msp::Input::KEY_Z)
398 manipulator.start_elevate();
401 else if(key==Msp::Input::KEY_ESC)
404 manipulator.cancel();
405 else if(mode==CATALOGUE)
410 else if(key==Msp::Input::KEY_X)
412 else if(key==Msp::Input::KEY_F && shift)
414 const set<Track *> &tracks = selection.get_objects<Track>();
415 const set<Track *> <racks = layout->get_tracks();
416 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
418 (*i)->set_flex(!(*i)->get_flex());
420 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
422 (*i)->snap_to(**j, true);
424 update_track_icon(layout_3d->get_track(**i));
427 else if(key==Msp::Input::KEY_F)
429 else if(key==Msp::Input::KEY_E && shift)
430 manipulator.even_slope(true);
431 else if(key==Msp::Input::KEY_E)
432 manipulator.even_slope();
433 else if(key==Msp::Input::KEY_A)
436 add_selection_to_route();
438 add_selection_to_zone();
440 else if(key==Msp::Input::KEY_C)
442 else if(key==Msp::Input::KEY_V)
444 else if(key==Msp::Input::KEY_P)
448 void Designer::button_press(unsigned btn)
450 bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
452 Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
453 Vector ground = map_pointer_to_ground(pointer);
459 Object *cobj = pick_object(pointer);
462 Object *obj = cobj->clone(layout);
463 obj->set_position(ground);
466 selection.add_object(obj);
474 else if(mode==SELECT)
478 Object *obj = pick_object(pointer);
483 selection.toggle_object(obj);
487 else if(mode==MEASURE)
488 measure.button_press(ground.x, ground.y, btn);
491 void Designer::axis_motion(unsigned, float, float)
493 Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
494 float wx = pointer.x*window.get_width();
495 float wy = pointer.y*window.get_height();
497 if(!root.get_child_at(wx, wy))
499 Vector ground = map_pointer_to_ground(pointer);
500 measure.pointer_motion(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 // XXX Should fix GLtk so this would not be needed
513 glColor3f(1.0, 1.0, 1.0);
516 void Designer::track_added(Track &trk)
518 new_tracks.push_back(&trk);
521 void Designer::track_removed(Track &trk)
523 list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
524 if(i!=new_tracks.end())
528 Object *Designer::pick_object(const Vector &pointer)
530 View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
531 const GL::Vector3 &cpos = view.get_camera().get_position();
532 GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(pointer.x, pointer.y, 0, 0));
534 return view.get_layout().get_layout().pick_object(Ray(cpos, Vector(cray)));
537 void Designer::update_track_icon(Track3D &track)
539 overlay->clear(track);
541 if(track.get_track().get_flex())
542 overlay->add_graphic(track, "flex");
544 if(unsigned sid = track.get_track().get_sensor_id())
546 overlay->add_graphic(track, "sensor");
547 overlay->set_label(track, lexical_cast<string>(sid));
549 else if(unsigned tid = track.get_track().get_turnout_id())
553 overlay->add_graphic(track, "turnout");
554 overlay->set_label(track, lexical_cast<string>(tid));
559 void Designer::selection_changed()
561 const set<Track *> &tracks = selection.get_objects<Track>();
563 lbl_status->set_text(string());
567 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
568 len += (*i)->get_type().get_total_length();
569 lbl_status->set_text(format("%.2fm of track selected\n", len));
573 void Designer::manipulation_status(const string &status)
575 lbl_status->set_text(status);
578 void Designer::manipulation_done(bool)
580 mode = MANIPULATE_DONE;
584 void Designer::measure_changed()
586 float pard = measure.get_parallel_distance()*1000;
587 float perpd = measure.get_perpendicular_distance()*1000;
588 float d = sqrt(pard*pard+perpd*perpd);
589 const Angle &adiff = measure.get_angle_difference();
590 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff.degrees());
591 lbl_status->set_text(info);
594 void Designer::measure_done()
600 void Designer::arrange_toolbars()
603 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
605 const GLtk::Geometry &geom = (*i)->get_geometry();
606 (*i)->set_position(x, window.get_height()-geom.h);
611 void Designer::track_properties_response(int)
613 const set<Track *> &tracks = selection.get_objects<Track>();
614 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
615 update_track_icon(layout_3d->get_track(**i));
618 void Designer::route_name_accept(const string &text)
621 cur_route->set_name(text);
624 void Designer::svg_export_accept(const string &text)
626 SvgExporter svg_exp(*layout);
630 string Designer::tooltip(int x, int y)
632 if(Object *obj = pick_object(Vector(x*2.0f/window.get_width()-1.0f, y*2.0f/window.get_height()-1.0f, 0)))
634 const ObjectType &otype = obj->get_type();
635 string info = format("%d %s", otype.get_article_number(), otype.get_description());
636 if(Track *track = dynamic_cast<Track *>(obj))
638 const TrackType &ttype = track->get_type();
639 if(mode!=CATALOGUE && abs(track->get_slope())>1e-4)
640 info += format(" (slope %.1f%%)", abs(track->get_slope()/ttype.get_total_length()*100));
641 if(track->get_turnout_id())
642 info += format(" (turnout %d)", track->get_turnout_id());
643 else if(track->get_sensor_id())
644 info += format(" (sensor %d)", track->get_sensor_id());
652 void Designer::clear_paths()
654 const set<Track *> <racks = layout->get_tracks();
655 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
657 Track3D &t3d = layout_3d->get_track(**i);
658 t3d.get_path().set_mask(0);
662 void Designer::show_route(const Route &route)
666 const set<Track *> &rtracks = route.get_tracks();
667 for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
669 Track3D &t3d = layout_3d->get_track(**i);
670 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
672 if(unsigned tid = (*i)->get_turnout_id())
673 path = route.get_turnout(tid);
675 t3d.get_path().set_path(path);
677 t3d.get_path().set_mask((*i)->get_type().get_paths());
681 void Designer::show_zone(const Zone &zone)
685 const Zone::TrackSet &ztracks = zone.get_tracks();
686 for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
688 Track3D &t3d = layout_3d->get_track(**i);
689 t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
690 t3d.get_path().set_mask((*i)->get_type().get_paths());