3 This file is part of the MSP Märklin suite
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 "libmarklin/route.h"
30 #include "libmarklin/tracktype.h"
34 #include "manipulator.h"
36 #include "selection.h"
37 #include "svgexporter.h"
41 using namespace Marklin;
44 Application::RegApp<Designer> Designer::reg;
46 Designer::Designer(int argc, char **argv):
48 ui_res("marklin.res"),
53 manipulator(*this, root, selection),
55 camera_ctl(*this, root, camera),
56 track_wrap(*this, selection)
58 window.set_title("Railway Designer");
59 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
61 selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
62 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
63 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
64 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
65 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
67 // Setup catalogue and layout
68 DataFile::load(catalogue, "tracks.dat");
70 cat_layout_3d = new Layout3D(catalogue.get_layout());
72 layout = new Layout(catalogue);
73 layout_3d = new Layout3D(*layout);
75 layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
76 layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
81 DataFile::load(*layout, argv[1]);
83 if(!layout->get_base().empty())
85 base_object = new GL::Object;
86 DataFile::load(*base_object, layout->get_base());
91 GL::enable(GL_CULL_FACE);
93 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
94 pipeline->set_camera(&camera);
95 pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
97 pipeline->add_renderable(*base_object);
98 pipeline->add_renderable_for_pass(track_wrap, "unlit");
99 pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
100 pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
102 light.set_position(0, -0.259, 0.966, 0);
103 lighting.attach(0, light);
105 GL::PipelinePass *pass = &pipeline->add_pass(0);
106 pass->lighting = &lighting;
107 pass->depth_test = &GL::DepthTest::lequal();
109 pass = &pipeline->add_pass("unlit");
110 pass->depth_test = &GL::DepthTest::lequal();
111 pass->blend = &GL::Blend::alpha();
113 pass = &pipeline->add_pass("blended");
114 pass->lighting = &lighting;
115 pass->depth_test = &GL::DepthTest::lequal();
116 pass->blend = &GL::Blend::alpha();
118 camera.set_up_direction(GL::Vector3(0, 0, 1));
119 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
122 root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
123 root.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
124 root.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
125 root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
127 toolbar = new Toolbar(*this);
129 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
130 toolbar->set_focusable(false);
132 GLtk::Panel *statusbar = new GLtk::Panel;
133 root.add(*statusbar);
134 statusbar->set_size(window.get_width(), 20);
135 statusbar->set_focusable(false);
137 lbl_status = new GLtk::Label;
138 statusbar->add(*lbl_status);
139 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
141 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
143 camera_ctl.view_all();
145 const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
146 for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
147 update_track_icon(*i->second);
150 Designer::~Designer()
157 delete cat_layout_3d;
166 return Application::main();
169 void Designer::save()
171 InputDialog *input = new InputDialog(*this, "Save layout", filename);
172 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
175 void Designer::quit()
180 void Designer::new_track()
185 void Designer::set_turnout_id()
187 Track *track = selection.get_track();
188 if(selection.size()==1 && track->get_type().is_turnout())
190 InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
191 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
195 void Designer::set_sensor_id()
197 const set<Track *> &tracks = selection.get_tracks();
200 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
202 if(!(*i)->get_type().is_turnout())
204 if(static_cast<int>((*i)->get_sensor_id())!=id)
207 id = (*i)->get_sensor_id();
215 InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
216 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
220 void Designer::rename_route()
225 InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
226 input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
229 void Designer::svg_export()
231 InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
232 input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
235 void Designer::edit_route(Route *r)
241 void Designer::add_selection_to_route()
248 cur_route->add_tracks(selection.get_tracks());
250 catch(const Exception &e)
252 lbl_status->set_text(e.what());
255 show_route(cur_route);
258 Point Designer::map_pointer_to_ground(int x, int y)
260 float xf = x*2.0/window.get_width()-1.0;
261 float yf = y*2.0/window.get_height()-1.0;
262 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
263 const GL::Vector3 &pos = camera.get_position();
265 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
268 void Designer::tick()
270 const Msp::Time::TimeStamp t = Msp::Time::now();
271 float dt = (t-last_tick)/Msp::Time::sec;
274 if(mode==MANIPULATE_DONE)
281 for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
282 layout_3d->get_track(**i).get_path().set_mask(0);
287 window.swap_buffers();
290 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
292 key = Input::key_from_sys(key);
293 mod = Input::mod_from_sys(mod);
295 if(key==Msp::Input::KEY_N && (mod&Input::MOD_SHIFT))
297 manipulator.start_extend();
300 else if(key==Msp::Input::KEY_N)
302 else if(key==Msp::Input::KEY_G)
304 manipulator.start_move();
307 else if(key==Msp::Input::KEY_R)
309 manipulator.start_rotate();
312 else if(key==Msp::Input::KEY_D)
314 manipulator.duplicate();
315 manipulator.start_move();
318 else if(key==Msp::Input::KEY_W)
320 else if(key==Msp::Input::KEY_PLUS)
321 selection.select_more();
322 else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
323 selection.select_blocks();
324 else if(key==Msp::Input::KEY_L)
325 selection.select_linked();
326 else if(key==Msp::Input::KEY_M)
331 else if(key==Msp::Input::KEY_Z)
333 manipulator.start_elevate();
336 else if(key==Msp::Input::KEY_ESC)
339 manipulator.cancel();
340 else if(mode==CATALOGUE)
345 else if(key==Msp::Input::KEY_X)
347 set<Track *> tracks = selection.get_tracks();
349 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
351 overlay->clear(layout_3d->get_track(**i));
352 layout->remove_track(**i);
356 else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
358 const set<Track *> &tracks = selection.get_tracks();
359 const set<Track *> <racks = layout->get_tracks();
360 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
362 (*i)->set_flex(!(*i)->get_flex());
364 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
366 (*i)->snap_to(**j, true);
368 update_track_icon(layout_3d->get_track(**i));
371 else if(key==Msp::Input::KEY_F)
372 manipulator.flatten();
373 else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
374 manipulator.even_slope(true);
375 else if(key==Msp::Input::KEY_E)
376 manipulator.even_slope();
377 else if(key==Msp::Input::KEY_T)
379 else if(key==Msp::Input::KEY_S)
381 else if(key==Msp::Input::KEY_A)
382 add_selection_to_route();
383 else if(key==Msp::Input::KEY_C)
384 manipulator.connect();
385 else if(key==Msp::Input::KEY_V)
389 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
391 y = window.get_height()-y-1;
392 mod = Input::mod_from_sys(mod);
394 Point ground = map_pointer_to_ground(x, y);
400 Track3D *ctrack = pick_track(x, y);
403 Track *track = new Track(*layout, ctrack->get_track().get_type());
404 track->set_position(ground);
407 selection.add_track(track);
415 else if(mode==SELECT)
419 Track3D *track = pick_track(x, y);
422 if(!(mod&Input::MOD_SHIFT))
424 selection.toggle_track(&track->get_track());
428 else if(mode==MEASURE)
429 measure.button_press(x, y, ground.x, ground.y, btn);
432 void Designer::pointer_motion(int x, int y)
434 y = window.get_height()-y-1;
436 if(!root.get_child_at(x, y))
438 Point ground = map_pointer_to_ground(x, y);
439 measure.pointer_motion(x, y, ground.x, ground.y);
443 void Designer::apply_camera()
447 GL::matrix_mode(GL::PROJECTION);
449 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
450 GL::matrix_mode(GL::MODELVIEW);
452 GL::translate(0, 0, -1);
458 void Designer::render()
460 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
465 cat_layout_3d->get_scene().render();
469 pipeline->render_all();
470 GL::enable(GL_CULL_FACE);
472 GL::Bind bind_blend(GL::Blend::alpha());
476 GL::Bind bind_depth(GL::DepthTest::lequal());
481 GL::matrix_mode(GL::PROJECTION);
483 GL::ortho_bottomleft(window.get_width(), window.get_height());
484 GL::matrix_mode(GL::MODELVIEW);
487 GL::disable(GL::DEPTH_TEST);
489 GL::Bind bind_blend(GL::Blend::alpha());
491 GL::Texture::unbind();
494 void Designer::track_added(Track &trk)
496 new_tracks.push_back(&trk);
499 void Designer::track_removed(Track &trk)
501 list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
502 if(i!=new_tracks.end())
506 Track3D *Designer::pick_track(int x, int y)
508 Layout3D *l = layout_3d;
512 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
513 float yy = (float(y)/window.get_height()-0.5)*0.82843;
514 float size = 4.0/window.get_height()*0.82843;
518 return l->pick_track(xx, yy, size);
521 void Designer::update_track_icon(Track3D &track)
523 overlay->clear(track);
525 if(track.get_track().get_flex())
526 overlay->add_graphic(track, "flex");
528 if(unsigned sid = track.get_track().get_sensor_id())
530 overlay->add_graphic(track, "sensor");
531 overlay->set_label(track, lexical_cast(sid));
533 else if(unsigned tid = track.get_track().get_turnout_id())
537 overlay->add_graphic(track, "turnout");
538 overlay->set_label(track, lexical_cast(tid));
543 void Designer::selection_changed()
545 const set<Track *> &tracks = selection.get_tracks();
547 lbl_status->set_text(string());
551 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
552 len += (*i)->get_type().get_total_length();
553 lbl_status->set_text(format("%.2fm of track selected\n", len));
557 void Designer::manipulation_status(const string &status)
559 lbl_status->set_text(status);
562 void Designer::manipulation_done(bool)
564 mode = MANIPULATE_DONE;
568 void Designer::measure_changed()
570 float pard = measure.get_parallel_distance()*1000;
571 float perpd = measure.get_perpendicular_distance()*1000;
572 float d = sqrt(pard*pard+perpd*perpd);
573 float adiff = measure.get_angle_difference()*180/M_PI;
574 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
575 lbl_status->set_text(info);
578 void Designer::measure_done()
584 void Designer::turnout_id_accept(const string &text)
586 Track *track = selection.get_track();
587 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
588 track->set_turnout_id(id);
590 update_track_icon(layout_3d->get_track(*track));
593 void Designer::sensor_id_accept(const string &text)
595 const set<Track *> &tracks = selection.get_tracks();
596 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
597 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
599 (*i)->set_sensor_id(id);
601 update_track_icon(layout_3d->get_track(**i));
605 void Designer::route_name_accept(const string &text)
608 cur_route->set_name(text);
611 void Designer::svg_export_accept(const string &text)
613 SvgExporter svg_exp(*layout);
617 string Designer::tooltip(int x, int y)
619 if(Track3D *t3d = pick_track(x, y))
621 const Track &track = t3d->get_track();
622 const TrackType &ttype = track.get_type();
623 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
624 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
625 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
626 if(track.get_turnout_id())
627 info += format(" (turnout %d)", track.get_turnout_id());
628 else if(track.get_sensor_id())
629 info += format(" (sensor %d)", track.get_sensor_id());
637 void Designer::show_route(const Route *route)
639 const set<Track *> <racks = layout->get_tracks();
640 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
642 Track3D &t3d = layout_3d->get_track(**i);
643 if(route && route->has_track(**i))
645 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
646 if((*i)->get_type().is_turnout())
648 unsigned tid = (*i)->get_turnout_id();
649 int path = (tid ? route->get_turnout(tid) : -1);
651 t3d.get_path().set_path(path);
653 t3d.get_path().set_mask((*i)->get_type().get_paths());
656 t3d.get_path().set_path(0);
659 t3d.get_path().set_mask(0);