3 This file is part of R²C²
4 Copyright © 2006-2011 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/tests.h>
18 #include <msp/gl/texture2d.h>
19 #include <msp/input/keys.h>
20 #include <msp/io/print.h>
21 #include <msp/strings/codec.h>
22 #include <msp/strings/lexicalcast.h>
23 #include <msp/strings/utf8.h>
24 #include <msp/strings/utils.h>
25 #include <msp/time/units.h>
26 #include <msp/time/utils.h>
27 #include "libr2c2/route.h"
28 #include "libr2c2/tracktype.h"
29 #include "libr2c2/zone.h"
33 #include "layoutbar.h"
34 #include "manipulator.h"
37 #include "selection.h"
38 #include "svgexporter.h"
40 #include "trackproperties.h"
42 #include "zoneproperties.h"
48 Application::RegApp<Designer> Designer::reg;
50 Designer::Designer(int argc, char **argv):
58 manipulator(*this, root, selection),
60 camera_ctl(*this, root, camera),
61 track_wrap(*this, selection)
63 window.set_title("Railway Designer");
64 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
66 selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
67 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
68 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
69 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
70 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
72 // Setup catalogue and layout
73 DataFile::load(catalogue, "tracks.dat");
75 cat_layout_3d = new Layout3D(catalogue.get_layout());
77 layout = new Layout(catalogue);
78 layout_3d = new Layout3D(*layout);
80 layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
81 layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
86 DataFile::load(*layout, argv[1]);
88 if(!layout->get_base().empty())
90 base_object = new GL::Object;
91 DataFile::load(*base_object, layout->get_base());
96 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
97 pipeline->set_camera(&camera);
98 pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
100 pipeline->add_renderable(*base_object);
101 pipeline->add_renderable_for_pass(track_wrap, "unlit");
102 pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
103 pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
105 light.set_position(0, -0.259, 0.966, 0);
106 lighting.attach(0, light);
108 GL::PipelinePass *pass = &pipeline->add_pass(0);
109 pass->lighting = &lighting;
110 pass->depth_test = &GL::DepthTest::lequal();
112 pass = &pipeline->add_pass("unlit");
113 pass->depth_test = &GL::DepthTest::lequal();
114 pass->blend = &GL::Blend::alpha();
116 pass = &pipeline->add_pass("blended");
117 pass->lighting = &lighting;
118 pass->depth_test = &GL::DepthTest::lequal();
119 pass->blend = &GL::Blend::alpha();
121 pass = &pipeline->add_pass("overlay");
122 pass->blend = &GL::Blend::alpha();
124 camera.set_up_direction(GL::Vector3(0, 0, 1));
125 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
128 root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
129 root.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
130 root.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
131 root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
133 toolbars.push_back(new Layoutbar(*this));
134 toolbars.push_back(new Trackbar(*this));
135 toolbars.push_back(new Routebar(*this));
136 toolbars.push_back(new Zonebar(*this));
137 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
140 if(i!=toolbars.begin())
142 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(this, &Designer::arrange_toolbars)));
146 GLtk::Panel *statusbar = new GLtk::Panel;
147 root.add(*statusbar);
148 statusbar->set_size(window.get_width(), 20);
149 statusbar->set_focusable(false);
151 lbl_status = new GLtk::Label;
152 statusbar->add(*lbl_status);
153 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
155 overlay = new Overlay3D(ui_res.get_default_font());
156 pipeline->add_renderable_for_pass(*overlay, "overlay");
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 Vector 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 Vector(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 Vector ground = map_pointer_to_ground(x, y);
473 Track *ctrack = pick_track(x, y);
476 Track *track = new Track(*layout, ctrack->get_type());
477 track->set_position(ground);
480 selection.add_track(track);
488 else if(mode==SELECT)
492 Track *track = pick_track(x, y);
495 if(!(mod&Input::MOD_SHIFT))
497 selection.toggle_track(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 Vector 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();
544 GL::Bind bind_depth(GL::DepthTest::lequal());
549 GL::matrix_mode(GL::PROJECTION);
551 GL::ortho_bottomleft(window.get_width(), window.get_height());
552 GL::matrix_mode(GL::MODELVIEW);
555 GL::Bind bind_blend(GL::Blend::alpha());
557 // XXX Should fix GLtk so these would not be needed
558 GL::Texture::unbind();
559 glColor3f(1.0, 1.0, 1.0);
562 void Designer::track_added(Track &trk)
564 new_tracks.push_back(&trk);
567 void Designer::track_removed(Track &trk)
569 list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
570 if(i!=new_tracks.end())
574 Track *Designer::pick_track(int x, int y)
576 float xx = x*2.0/window.get_width()-1.0;
577 float yy = y*2.0/window.get_height()-1.0;
579 return catalogue.get_layout().pick_track(Vector(0, 0, 1), Vector(xx*0.05523, yy*0.042421, -0.1));
582 const GL::Vector3 &cpos = camera.get_position();
583 GL::Vector4 cray = camera.unproject(GL::Vector4(xx, yy, 0, 0));
584 return layout->pick_track(Vector(cpos.x, cpos.y, cpos.z), Vector(cray.x, cray.y, cray.z));
588 void Designer::update_track_icon(Track3D &track)
590 overlay->clear(track);
592 if(track.get_track().get_flex())
593 overlay->add_graphic(track, "flex");
595 if(unsigned sid = track.get_track().get_sensor_id())
597 overlay->add_graphic(track, "sensor");
598 overlay->set_label(track, lexical_cast(sid));
600 else if(unsigned tid = track.get_track().get_turnout_id())
604 overlay->add_graphic(track, "turnout");
605 overlay->set_label(track, lexical_cast(tid));
610 void Designer::selection_changed()
612 const set<Track *> &tracks = selection.get_tracks();
614 lbl_status->set_text(string());
618 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
619 len += (*i)->get_type().get_total_length();
620 lbl_status->set_text(format("%.2fm of track selected\n", len));
624 void Designer::manipulation_status(const string &status)
626 lbl_status->set_text(status);
629 void Designer::manipulation_done(bool)
631 mode = MANIPULATE_DONE;
635 void Designer::measure_changed()
637 float pard = measure.get_parallel_distance()*1000;
638 float perpd = measure.get_perpendicular_distance()*1000;
639 float d = sqrt(pard*pard+perpd*perpd);
640 float adiff = measure.get_angle_difference()*180/M_PI;
641 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
642 lbl_status->set_text(info);
645 void Designer::measure_done()
651 void Designer::arrange_toolbars()
654 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
656 const GLtk::Geometry &geom = (*i)->get_geometry();
657 (*i)->set_position(x, window.get_height()-geom.h);
662 void Designer::track_properties_response(int)
664 const set<Track *> &tracks = selection.get_tracks();
665 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
666 update_track_icon(layout_3d->get_track(**i));
669 void Designer::route_name_accept(const string &text)
672 cur_route->set_name(text);
675 void Designer::svg_export_accept(const string &text)
677 SvgExporter svg_exp(*layout);
681 string Designer::tooltip(int x, int y)
683 if(Track *track = pick_track(x, y))
685 const TrackType &ttype = track->get_type();
686 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
687 if(mode!=CATALOGUE && abs(track->get_slope())>1e-4)
688 info += format(" (slope %.1f%%)", abs(track->get_slope()/ttype.get_total_length()*100));
689 if(track->get_turnout_id())
690 info += format(" (turnout %d)", track->get_turnout_id());
691 else if(track->get_sensor_id())
692 info += format(" (sensor %d)", track->get_sensor_id());
700 void Designer::clear_paths()
702 const set<Track *> <racks = layout->get_tracks();
703 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
705 Track3D &t3d = layout_3d->get_track(**i);
706 t3d.get_path().set_mask(0);
710 void Designer::show_route(const Route &route)
714 const set<Track *> &rtracks = route.get_tracks();
715 for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
717 Track3D &t3d = layout_3d->get_track(**i);
718 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
720 if(unsigned tid = (*i)->get_turnout_id())
721 path = route.get_turnout(tid);
723 t3d.get_path().set_path(path);
725 t3d.get_path().set_mask((*i)->get_type().get_paths());
729 void Designer::show_zone(const Zone &zone)
733 const Zone::TrackSet &ztracks = zone.get_tracks();
734 for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
736 Track3D &t3d = layout_3d->get_track(**i);
737 t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
738 t3d.get_path().set_mask((*i)->get_type().get_paths());