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_object_added.connect(sigc::mem_fun(this, &Designer::object_added));
72 layout->signal_object_removed.connect(sigc::mem_fun(this, &Designer::object_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));
114 root_layout = new GLtk::Layout;
115 root_layout->set_margin(0);
116 root.set_layout(root_layout);
118 toolbars.push_back(new Layoutbar(*this));
119 toolbars.push_back(new Trackbar(*this));
120 toolbars.push_back(new Routebar(*this));
121 toolbars.push_back(new Zonebar(*this));
123 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
126 if(i!=toolbars.begin())
128 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(root_layout, &GLtk::Layout::update)));
131 root_layout->add_constraint(**i, GLtk::Layout::RIGHT_OF, *prev, 0);
132 root_layout->add_constraint(**i, GLtk::Layout::COPY_HEIGHT, *prev);
137 GLtk::Panel *statusbar = new GLtk::Panel;
138 root.add(*statusbar);
139 statusbar->set_focusable(false);
140 root_layout->set_gravity(*statusbar, -1, -1);
141 root_layout->set_expand(*statusbar, true, false);
143 lbl_status = new GLtk::Label;
144 statusbar->add(*lbl_status);
145 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
147 pipeline->add_renderable_for_pass(sel_wrap, "unlit");
148 overlay = new Overlay3D(ui_res.get_default_font());
149 pipeline->add_renderable_for_pass(*overlay, "overlay");
150 pipeline->add_renderable_for_pass(measure, "unlit");
152 camera_ctl = new CameraController(*main_view, keyboard, mouse);
153 cat_view->get_camera().set_look_direction(GL::Vector3(0, 0.13053, -0.99144));
154 cat_view->view_all(true);
155 main_view->view_all();
157 const set<Track *> &tracks = layout->get_all<Track>();
158 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
159 update_track_icon(layout_3d->get<Track3D>(**i));
162 Designer::~Designer()
164 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
173 delete cat_layout_3d;
182 return Application::main();
185 void Designer::save()
187 InputDialog *input = new InputDialog(*this, "Save layout", filename);
188 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
190 root_layout->set_gravity(*input, 0, 0);
193 void Designer::quit()
198 void Designer::new_track()
204 lbl_status->set_text("Select new track or press Esc to cancel");
207 void Designer::erase_tracks()
212 set<Track *> tracks = selection.get_objects<Track>();
214 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
216 overlay->clear(layout_3d->get<Track3D>(**i));
221 void Designer::track_properties()
223 if(mode!=SELECT || selection.empty())
226 TrackProperties *track_prop = new TrackProperties(selection);
227 root.add(*track_prop);
228 root_layout->set_gravity(*track_prop, 0, 0);
230 track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
233 void Designer::extend_track()
238 if(manipulator.start_extend())
242 void Designer::connect_tracks()
247 manipulator.connect();
250 void Designer::flatten_tracks()
255 manipulator.flatten();
258 void Designer::svg_export()
260 InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
261 input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
263 root_layout->set_gravity(*input, 0, 0);
266 void Designer::edit_route(Route *r)
271 show_route(*cur_route);
276 void Designer::rename_route()
278 if(mode!=SELECT || !cur_route)
281 InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
282 input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
284 root_layout->set_gravity(*input, 0, 0);
287 void Designer::add_selection_to_route()
294 cur_route->add_tracks(selection.get_objects<Track>());
296 catch(const exception &e)
298 lbl_status->set_text(e.what());
301 show_route(*cur_route);
304 void Designer::edit_zone(Zone *z)
309 show_zone(*cur_zone);
314 void Designer::zone_properties()
319 ZoneProperties *zone_prop = new ZoneProperties(*cur_zone);
320 root.add(*zone_prop);
321 const GLtk::Geometry &root_geom = root.get_geometry();
322 const GLtk::Geometry &dlg_geom = zone_prop->get_geometry();
323 zone_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
326 void Designer::add_selection_to_zone()
333 cur_zone->add_tracks(selection.get_objects<Track>());
335 catch(const exception &e)
337 lbl_status->set_text(e.what());
340 show_zone(*cur_zone);
343 Vector Designer::map_pointer_to_ground(const Vector &p)
345 GL::Vector4 vec = main_view->get_camera().unproject(GL::Vector4(p.x, p.y, 0, 0));
346 const GL::Vector3 &pos = main_view->get_camera().get_position();
348 return pos-GL::Vector3(vec.x, vec.y, vec.z)*(pos.z/vec.z);
351 void Designer::tick()
353 const Msp::Time::TimeStamp t = Msp::Time::now();
354 float dt = (t-last_tick)/Msp::Time::sec;
357 if(mode==MANIPULATE_DONE)
362 camera_ctl->tick(dt);
364 for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
365 layout_3d->get<Track3D>(**i).get_path().set_mask(0);
370 window.swap_buffers();
373 void Designer::key_press(unsigned key)
375 bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
377 if(key==Msp::Input::KEY_N && shift)
379 else if(key==Msp::Input::KEY_N)
381 else if(key==Msp::Input::KEY_G)
383 manipulator.start_move();
386 else if(key==Msp::Input::KEY_R)
388 manipulator.start_rotate();
391 else if(key==Msp::Input::KEY_D)
393 manipulator.duplicate();
394 manipulator.start_move();
397 else if(key==Msp::Input::KEY_W)
399 else if(key==Msp::Input::KEY_PLUS)
400 selection.select_more();
401 else if(key==Msp::Input::KEY_L && shift)
402 selection.select_blocks();
403 else if(key==Msp::Input::KEY_L)
404 selection.select_linked();
405 else if(key==Msp::Input::KEY_M)
410 else if(key==Msp::Input::KEY_Z)
412 manipulator.start_elevate();
415 else if(key==Msp::Input::KEY_ESC)
418 manipulator.cancel();
419 else if(mode==CATALOGUE)
424 else if(key==Msp::Input::KEY_X)
426 else if(key==Msp::Input::KEY_F && shift)
428 const set<Track *> &tracks = selection.get_objects<Track>();
429 const set<Track *> <racks = layout->get_all<Track>();
430 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
432 (*i)->set_flex(!(*i)->get_flex());
434 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
436 (*i)->snap_to(**j, true);
438 update_track_icon(layout_3d->get<Track3D>(**i));
441 else if(key==Msp::Input::KEY_F)
443 else if(key==Msp::Input::KEY_E && shift)
444 manipulator.even_slope(true);
445 else if(key==Msp::Input::KEY_E)
446 manipulator.even_slope();
447 else if(key==Msp::Input::KEY_A)
450 add_selection_to_route();
452 add_selection_to_zone();
454 else if(key==Msp::Input::KEY_C)
456 else if(key==Msp::Input::KEY_V)
458 else if(key==Msp::Input::KEY_P)
462 void Designer::button_press(unsigned btn)
464 bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
466 Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
467 Vector ground = map_pointer_to_ground(pointer);
473 Object *cobj = pick_object(pointer);
476 Object *obj = cobj->clone(layout);
477 obj->set_position(ground);
480 selection.add_object(obj);
488 else if(mode==SELECT)
492 Object *obj = pick_object(pointer);
497 selection.toggle_object(obj);
501 else if(mode==MEASURE)
502 measure.button_press(ground.x, ground.y, btn);
505 void Designer::axis_motion(unsigned, float, float)
507 Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
508 float wx = pointer.x*window.get_width();
509 float wy = pointer.y*window.get_height();
511 if(!root.get_child_at(wx, wy))
513 Vector ground = map_pointer_to_ground(pointer);
514 measure.pointer_motion(ground.x, ground.y);
518 void Designer::render()
520 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
522 View3D *view = (mode==CATALOGUE ? cat_view : main_view);
526 // XXX Should fix GLtk so this would not be needed
527 glColor3f(1.0, 1.0, 1.0);
530 void Designer::object_added(Object &obj)
532 if(Track *trk = dynamic_cast<Track *>(&obj))
533 new_tracks.push_back(trk);
536 void Designer::object_removed(Object &obj)
538 if(Track *trk = dynamic_cast<Track *>(&obj))
540 list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), trk);
541 if(i!=new_tracks.end())
546 Object *Designer::pick_object(const Vector &pointer)
548 View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
549 const GL::Vector3 &cpos = view.get_camera().get_position();
550 GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(pointer.x, pointer.y, 0, 0));
552 return view.get_layout().get_layout().pick<Object>(Ray(cpos, Vector(cray)));
555 void Designer::update_track_icon(Track3D &track)
557 overlay->clear(track);
559 if(track.get_track().get_flex())
560 overlay->add_graphic(track, "flex");
562 if(unsigned sid = track.get_track().get_sensor_id())
564 overlay->add_graphic(track, "sensor");
565 overlay->set_label(track, lexical_cast<string>(sid));
567 else if(unsigned tid = track.get_track().get_turnout_id())
571 overlay->add_graphic(track, "turnout");
572 overlay->set_label(track, lexical_cast<string>(tid));
577 void Designer::selection_changed()
579 const set<Track *> &tracks = selection.get_objects<Track>();
581 lbl_status->set_text(string());
585 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
586 len += (*i)->get_type().get_total_length();
587 lbl_status->set_text(format("%.2fm of track selected\n", len));
591 void Designer::manipulation_status(const string &status)
593 lbl_status->set_text(status);
596 void Designer::manipulation_done(bool)
598 mode = MANIPULATE_DONE;
602 void Designer::measure_changed()
604 float pard = measure.get_parallel_distance()*1000;
605 float perpd = measure.get_perpendicular_distance()*1000;
606 float d = sqrt(pard*pard+perpd*perpd);
607 const Angle &adiff = measure.get_angle_difference();
608 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff.degrees());
609 lbl_status->set_text(info);
612 void Designer::measure_done()
618 void Designer::track_properties_response(int)
620 const set<Track *> &tracks = selection.get_objects<Track>();
621 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
622 update_track_icon(layout_3d->get<Track3D>(**i));
625 void Designer::route_name_accept(const string &text)
628 cur_route->set_name(text);
631 void Designer::svg_export_accept(const string &text)
633 SvgExporter svg_exp(*layout);
637 string Designer::tooltip(int x, int y)
639 const GLtk::Geometry &rgeom = root.get_geometry();
640 if(Object *obj = pick_object(Vector(x*2.0f/rgeom.w-1.0f, y*2.0f/rgeom.h-1.0f, 0)))
642 const ObjectType &otype = obj->get_type();
643 string info = format("%d %s", otype.get_article_number(), otype.get_description());
644 if(Track *track = dynamic_cast<Track *>(obj))
646 if(mode!=CATALOGUE && abs(track->get_tilt()).radians()>1e-4)
647 info += format(" (slope %.1f%%)", abs(tan(track->get_tilt())*100));
648 if(track->get_turnout_id())
649 info += format(" (turnout %d)", track->get_turnout_id());
650 else if(track->get_sensor_id())
651 info += format(" (sensor %d)", track->get_sensor_id());
659 void Designer::clear_paths()
661 const set<Track *> <racks = layout->get_all<Track>();
662 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
664 Track3D &t3d = layout_3d->get<Track3D>(**i);
665 t3d.get_path().set_mask(0);
669 void Designer::show_route(const Route &route)
673 const set<Track *> &rtracks = route.get_tracks();
674 for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
676 Track3D &t3d = layout_3d->get<Track3D>(**i);
677 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
679 if(unsigned tid = (*i)->get_turnout_id())
680 path = route.get_turnout(tid);
682 t3d.get_path().set_path(path);
684 t3d.get_path().set_mask((*i)->get_type().get_paths());
688 void Designer::show_zone(const Zone &zone)
692 const Zone::TrackSet &ztracks = zone.get_tracks();
693 for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
695 Track3D &t3d = layout_3d->get<Track3D>(**i);
696 t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
697 t3d.get_path().set_mask((*i)->get_type().get_paths());