3 This file is part of R²C²
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
11 #include <msp/fs/utils.h>
12 #include <msp/gl/blend.h>
13 #include <msp/gl/framebuffer.h>
14 #include <msp/gl/matrix.h>
15 #include <msp/gl/misc.h>
16 #include <msp/gl/projection.h>
17 #include <msp/gl/rendermode.h>
18 #include <msp/gl/select.h>
19 #include <msp/gl/tests.h>
20 #include <msp/gl/texture2d.h>
21 #include <msp/input/keys.h>
22 #include <msp/io/print.h>
23 #include <msp/strings/codec.h>
24 #include <msp/strings/lexicalcast.h>
25 #include <msp/strings/utf8.h>
26 #include <msp/strings/utils.h>
27 #include <msp/time/units.h>
28 #include <msp/time/utils.h>
29 #include "libr2c2/route.h"
30 #include "libr2c2/tracktype.h"
31 #include "libr2c2/zone.h"
35 #include "layoutbar.h"
36 #include "manipulator.h"
39 #include "selection.h"
40 #include "svgexporter.h"
42 #include "trackproperties.h"
44 #include "zoneproperties.h"
50 Application::RegApp<Designer> Designer::reg;
52 Designer::Designer(int argc, char **argv):
60 manipulator(*this, root, selection),
62 camera_ctl(*this, root, camera),
63 track_wrap(*this, selection)
65 window.set_title("Railway Designer");
66 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
68 selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
69 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
70 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
71 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
72 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
74 // Setup catalogue and layout
75 DataFile::load(catalogue, "tracks.dat");
77 cat_layout_3d = new Layout3D(catalogue.get_layout());
79 layout = new Layout(catalogue);
80 layout_3d = new Layout3D(*layout);
82 layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
83 layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
88 DataFile::load(*layout, argv[1]);
90 if(!layout->get_base().empty())
92 base_object = new GL::Object;
93 DataFile::load(*base_object, layout->get_base());
98 GL::enable(GL_CULL_FACE);
100 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
101 pipeline->set_camera(&camera);
102 pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
104 pipeline->add_renderable(*base_object);
105 pipeline->add_renderable_for_pass(track_wrap, "unlit");
106 pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
107 pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
109 light.set_position(0, -0.259, 0.966, 0);
110 lighting.attach(0, light);
112 GL::PipelinePass *pass = &pipeline->add_pass(0);
113 pass->lighting = &lighting;
114 pass->depth_test = &GL::DepthTest::lequal();
116 pass = &pipeline->add_pass("unlit");
117 pass->depth_test = &GL::DepthTest::lequal();
118 pass->blend = &GL::Blend::alpha();
120 pass = &pipeline->add_pass("blended");
121 pass->lighting = &lighting;
122 pass->depth_test = &GL::DepthTest::lequal();
123 pass->blend = &GL::Blend::alpha();
125 camera.set_up_direction(GL::Vector3(0, 0, 1));
126 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
129 root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
130 root.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
131 root.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
132 root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
134 toolbars.push_back(new Layoutbar(*this));
135 toolbars.push_back(new Trackbar(*this));
136 toolbars.push_back(new Routebar(*this));
137 toolbars.push_back(new Zonebar(*this));
138 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
141 if(i!=toolbars.begin())
143 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(this, &Designer::arrange_toolbars)));
147 GLtk::Panel *statusbar = new GLtk::Panel;
148 root.add(*statusbar);
149 statusbar->set_size(window.get_width(), 20);
150 statusbar->set_focusable(false);
152 lbl_status = new GLtk::Label;
153 statusbar->add(*lbl_status);
154 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
156 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
158 camera_ctl.view_all();
160 const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
161 for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
162 update_track_icon(*i->second);
165 Designer::~Designer()
167 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
174 delete cat_layout_3d;
183 return Application::main();
186 void Designer::save()
188 InputDialog *input = new InputDialog(*this, "Save layout", filename);
189 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
192 void Designer::quit()
197 void Designer::new_track()
203 lbl_status->set_text("Select new track or press Esc to cancel");
206 void Designer::erase_tracks()
211 set<Track *> tracks = selection.get_tracks();
213 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
215 overlay->clear(layout_3d->get_track(**i));
220 void Designer::track_properties()
222 if(mode!=SELECT || selection.empty())
225 TrackProperties *track_prop = new TrackProperties(selection);
226 root.add(*track_prop);
227 const GLtk::Geometry &root_geom = root.get_geometry();
228 const GLtk::Geometry &dlg_geom = track_prop->get_geometry();
229 track_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
231 track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
234 void Designer::extend_track()
239 if(manipulator.start_extend())
243 void Designer::connect_tracks()
248 manipulator.connect();
251 void Designer::flatten_tracks()
256 manipulator.flatten();
259 void Designer::svg_export()
261 InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
262 input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
265 void Designer::edit_route(Route *r)
270 show_route(*cur_route);
275 void Designer::rename_route()
277 if(mode!=SELECT || !cur_route)
280 InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
281 input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
284 void Designer::add_selection_to_route()
291 cur_route->add_tracks(selection.get_tracks());
293 catch(const Exception &e)
295 lbl_status->set_text(e.what());
298 show_route(*cur_route);
301 void Designer::edit_zone(Zone *z)
306 show_zone(*cur_zone);
311 void Designer::zone_properties()
316 ZoneProperties *zone_prop = new ZoneProperties(*cur_zone);
317 root.add(*zone_prop);
318 const GLtk::Geometry &root_geom = root.get_geometry();
319 const GLtk::Geometry &dlg_geom = zone_prop->get_geometry();
320 zone_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
323 void Designer::add_selection_to_zone()
330 cur_zone->add_tracks(selection.get_tracks());
332 catch(const Exception &e)
334 lbl_status->set_text(e.what());
337 show_zone(*cur_zone);
340 Point Designer::map_pointer_to_ground(int x, int y)
342 float xf = x*2.0/window.get_width()-1.0;
343 float yf = y*2.0/window.get_height()-1.0;
344 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
345 const GL::Vector3 &pos = camera.get_position();
347 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
350 void Designer::tick()
352 const Msp::Time::TimeStamp t = Msp::Time::now();
353 float dt = (t-last_tick)/Msp::Time::sec;
356 if(mode==MANIPULATE_DONE)
363 for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
364 layout_3d->get_track(**i).get_path().set_mask(0);
369 window.swap_buffers();
372 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
374 key = Input::key_from_sys(key);
375 mod = Input::mod_from_sys(mod);
377 if(key==Msp::Input::KEY_N && (mod&Input::MOD_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 && (mod&Input::MOD_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 && (mod&Input::MOD_SHIFT))
428 const set<Track *> &tracks = selection.get_tracks();
429 const set<Track *> <racks = layout->get_tracks();
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_track(**i));
441 else if(key==Msp::Input::KEY_F)
443 else if(key==Msp::Input::KEY_E && (mod&Input::MOD_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(int x, int y, unsigned btn, unsigned mod)
464 y = window.get_height()-y-1;
465 mod = Input::mod_from_sys(mod);
467 Point ground = map_pointer_to_ground(x, y);
473 Track3D *ctrack = pick_track(x, y);
476 Track *track = new Track(*layout, ctrack->get_track().get_type());
477 track->set_position(ground);
480 selection.add_track(track);
488 else if(mode==SELECT)
492 Track3D *track = pick_track(x, y);
495 if(!(mod&Input::MOD_SHIFT))
497 selection.toggle_track(&track->get_track());
501 else if(mode==MEASURE)
502 measure.button_press(x, y, ground.x, ground.y, btn);
505 void Designer::pointer_motion(int x, int y)
507 y = window.get_height()-y-1;
509 if(!root.get_child_at(x, y))
511 Point ground = map_pointer_to_ground(x, y);
512 measure.pointer_motion(x, y, ground.x, ground.y);
516 void Designer::apply_camera()
520 GL::matrix_mode(GL::PROJECTION);
522 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
523 GL::matrix_mode(GL::MODELVIEW);
525 GL::translate(0, 0, -1);
531 void Designer::render()
533 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
538 cat_layout_3d->get_scene().render();
542 pipeline->render_all();
543 GL::enable(GL_CULL_FACE);
545 GL::Bind bind_blend(GL::Blend::alpha());
549 GL::Bind bind_depth(GL::DepthTest::lequal());
554 GL::matrix_mode(GL::PROJECTION);
556 GL::ortho_bottomleft(window.get_width(), window.get_height());
557 GL::matrix_mode(GL::MODELVIEW);
560 GL::disable(GL::DEPTH_TEST);
562 GL::Bind bind_blend(GL::Blend::alpha());
564 GL::Texture::unbind();
567 void Designer::track_added(Track &trk)
569 new_tracks.push_back(&trk);
572 void Designer::track_removed(Track &trk)
574 list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
575 if(i!=new_tracks.end())
579 Track3D *Designer::pick_track(int x, int y)
581 Layout3D *l = layout_3d;
585 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
586 float yy = (float(y)/window.get_height()-0.5)*0.82843;
587 float size = 4.0/window.get_height()*0.82843;
591 return l->pick_track(xx, yy, size);
594 void Designer::update_track_icon(Track3D &track)
596 overlay->clear(track);
598 if(track.get_track().get_flex())
599 overlay->add_graphic(track, "flex");
601 if(unsigned sid = track.get_track().get_sensor_id())
603 overlay->add_graphic(track, "sensor");
604 overlay->set_label(track, lexical_cast(sid));
606 else if(unsigned tid = track.get_track().get_turnout_id())
610 overlay->add_graphic(track, "turnout");
611 overlay->set_label(track, lexical_cast(tid));
616 void Designer::selection_changed()
618 const set<Track *> &tracks = selection.get_tracks();
620 lbl_status->set_text(string());
624 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
625 len += (*i)->get_type().get_total_length();
626 lbl_status->set_text(format("%.2fm of track selected\n", len));
630 void Designer::manipulation_status(const string &status)
632 lbl_status->set_text(status);
635 void Designer::manipulation_done(bool)
637 mode = MANIPULATE_DONE;
641 void Designer::measure_changed()
643 float pard = measure.get_parallel_distance()*1000;
644 float perpd = measure.get_perpendicular_distance()*1000;
645 float d = sqrt(pard*pard+perpd*perpd);
646 float adiff = measure.get_angle_difference()*180/M_PI;
647 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
648 lbl_status->set_text(info);
651 void Designer::measure_done()
657 void Designer::arrange_toolbars()
660 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
662 const GLtk::Geometry &geom = (*i)->get_geometry();
663 (*i)->set_position(x, window.get_height()-geom.h);
668 void Designer::track_properties_response(int)
670 const set<Track *> &tracks = selection.get_tracks();
671 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
672 update_track_icon(layout_3d->get_track(**i));
675 void Designer::route_name_accept(const string &text)
678 cur_route->set_name(text);
681 void Designer::svg_export_accept(const string &text)
683 SvgExporter svg_exp(*layout);
687 string Designer::tooltip(int x, int y)
689 if(Track3D *t3d = pick_track(x, y))
691 const Track &track = t3d->get_track();
692 const TrackType &ttype = track.get_type();
693 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
694 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
695 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
696 if(track.get_turnout_id())
697 info += format(" (turnout %d)", track.get_turnout_id());
698 else if(track.get_sensor_id())
699 info += format(" (sensor %d)", track.get_sensor_id());
707 void Designer::clear_paths()
709 const set<Track *> <racks = layout->get_tracks();
710 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
712 Track3D &t3d = layout_3d->get_track(**i);
713 t3d.get_path().set_mask(0);
717 void Designer::show_route(const Route &route)
721 const set<Track *> &rtracks = route.get_tracks();
722 for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
724 Track3D &t3d = layout_3d->get_track(**i);
725 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
727 if(unsigned tid = (*i)->get_turnout_id())
728 path = route.get_turnout(tid);
730 t3d.get_path().set_path(path);
732 t3d.get_path().set_mask((*i)->get_type().get_paths());
736 void Designer::show_zone(const Zone &zone)
740 const Zone::TrackSet &ztracks = zone.get_tracks();
741 for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
743 Track3D &t3d = layout_3d->get_track(**i);
744 t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
745 t3d.get_path().set_mask((*i)->get_type().get_paths());