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/gl/blend.h>
12 #include <msp/gl/framebuffer.h>
13 #include <msp/gl/matrix.h>
14 #include <msp/gl/misc.h>
15 #include <msp/gl/projection.h>
16 #include <msp/gl/rendermode.h>
17 #include <msp/gl/select.h>
18 #include <msp/gl/tests.h>
19 #include <msp/gl/texture2d.h>
20 #include <msp/input/keys.h>
21 #include <msp/io/print.h>
22 #include <msp/strings/codec.h>
23 #include <msp/strings/lexicalcast.h>
24 #include <msp/strings/utf8.h>
25 #include <msp/strings/utils.h>
26 #include <msp/time/units.h>
27 #include <msp/time/utils.h>
28 #include "libmarklin/route.h"
29 #include "libmarklin/tracktype.h"
33 #include "manipulator.h"
35 #include "selection.h"
39 using namespace Marklin;
42 Application::RegApp<Designer> Designer::reg;
44 Designer::Designer(int argc, char **argv):
49 manipulator(*this, selection),
51 camera_ctl(window, camera)
53 window.set_title("Railway Designer");
54 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
56 selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
57 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
58 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
59 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
60 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
62 // Setup catalogue and layout
63 DataFile::load(catalogue, "tracks.dat");
65 cat_layout_3d = new Layout3D(catalogue.get_layout());
67 layout = new Layout(catalogue);
68 layout_3d = new Layout3D(*layout);
73 DataFile::load(*layout, argv[1]);
75 if(!layout->get_base().empty())
77 base_object = new GL::Object;
78 DataFile::load(*base_object, layout->get_base());
83 GL::enable(GL_CULL_FACE);
85 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
86 pipeline->set_camera(&camera);
87 pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
89 pipeline->add_renderable_for_pass(*base_object, 0);
90 pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
91 pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
93 light.set_position(0, -0.259, 0.966, 0);
94 lighting.attach(0, light);
96 GL::PipelinePass *pass = &pipeline->add_pass(0);
97 pass->lighting = &lighting;
98 pass->depth_test = &GL::DepthTest::lequal();
100 pass = &pipeline->add_pass("unlit");
101 pass->depth_test = &GL::DepthTest::lequal();
102 pass->blend = &GL::Blend::alpha();
104 camera.set_up_direction(GL::Vector3(0, 0, 1));
108 DataFile::load(ui_res, "marklin.res");
109 root = new GLtk::Root(ui_res, window);
110 root->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
111 root->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
112 root->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
113 root->signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
115 toolbar = new Toolbar(*this);
117 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
118 toolbar->set_focusable(false);
120 GLtk::Panel *statusbar = new GLtk::Panel(ui_res);
121 root->add(*statusbar);
122 statusbar->set_size(window.get_width(), 20);
123 statusbar->set_focusable(false);
125 lbl_status = new GLtk::Label(ui_res);
126 statusbar->add(*lbl_status);
127 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
129 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
131 const list<Track3D *> &tracks = layout_3d->get_tracks();
132 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
133 update_track_icon(**i);
138 Designer::~Designer()
146 delete cat_layout_3d;
155 return Application::main();
158 void Designer::save()
160 InputDialog *input = new InputDialog(*this, "Save layout", filename);
161 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
164 void Designer::quit()
169 void Designer::new_track()
174 void Designer::set_turnout_id()
176 Track *track = selection.get_track();
177 if(selection.size()==1 && track->get_type().is_turnout())
179 InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
180 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
184 void Designer::set_sensor_id()
186 const set<Track *> &tracks = selection.get_tracks();
189 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
191 if(!(*i)->get_type().is_turnout())
193 if(static_cast<int>((*i)->get_sensor_id())!=id)
196 id = (*i)->get_sensor_id();
204 InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
205 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
209 void Designer::rename_route()
214 InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
215 input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
218 void Designer::edit_route(Route *r)
224 void Designer::add_selection_to_route()
231 cur_route->add_tracks(selection.get_tracks());
233 catch(const Exception &e)
235 IO::print("%s\n", e.what());
238 show_route(cur_route);
241 Point Designer::map_pointer_coords(int x, int y)
243 float xf = x*2.0/window.get_width()-1.0;
244 float yf = y*2.0/window.get_height()-1.0;
245 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
246 const GL::Vector3 &pos = camera.get_position();
248 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
251 void Designer::tick()
253 const Msp::Time::TimeStamp t = Msp::Time::now();
254 float dt = (t-last_tick)/Msp::Time::sec;
257 window.get_display().tick();
263 window.swap_buffers();
266 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
268 key = Input::key_from_sys(key);
269 mod = Input::mod_from_sys(mod);
271 if(key==Msp::Input::KEY_N)
273 else if(key==Msp::Input::KEY_G)
275 manipulator.start_move();
278 else if(key==Msp::Input::KEY_R)
280 manipulator.start_rotate();
283 else if(key==Msp::Input::KEY_D)
285 manipulator.duplicate();
286 manipulator.start_move();
289 else if(key==Msp::Input::KEY_W)
291 else if(key==Msp::Input::KEY_PLUS)
292 selection.select_more();
293 else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
294 selection.select_blocks();
295 else if(key==Msp::Input::KEY_L)
296 selection.select_linked();
297 else if(key==Msp::Input::KEY_M)
302 else if(key==Msp::Input::KEY_Z)
304 manipulator.start_elevate();
307 else if(key==Msp::Input::KEY_ESC)
310 manipulator.cancel();
311 else if(mode==CATALOGUE)
316 else if(key==Msp::Input::KEY_X)
318 set<Track *> tracks = selection.get_tracks();
320 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
322 overlay->clear(layout_3d->get_track(**i));
323 layout->remove_track(**i);
327 else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
329 const set<Track *> &tracks = selection.get_tracks();
330 const set<Track *> <racks = layout->get_tracks();
331 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
333 (*i)->set_flex(!(*i)->get_flex());
335 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
337 (*i)->snap_to(**j, true);
339 update_track_icon(layout_3d->get_track(**i));
342 else if(key==Msp::Input::KEY_F)
343 manipulator.flatten();
344 else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
345 manipulator.even_slope(true);
346 else if(key==Msp::Input::KEY_E)
347 manipulator.even_slope();
348 else if(key==Msp::Input::KEY_T)
350 else if(key==Msp::Input::KEY_S)
352 else if(key==Msp::Input::KEY_A)
353 add_selection_to_route();
356 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
358 y = window.get_height()-y-1;
359 mod = Input::mod_from_sys(mod);
361 Point ground = map_pointer_coords(x, y);
367 Track3D *ctrack = pick_track(x, y);
370 Track *track = new Track(*layout, ctrack->get_track().get_type());
371 track->set_position(ground);
374 selection.add_track(track);
382 else if(mode==SELECT)
386 Track3D *track = pick_track(x, y);
389 if(!(mod&Input::MOD_SHIFT))
391 selection.toggle_track(&track->get_track());
395 else if(mode==MANIPULATE)
396 manipulator.button_press(x, y, ground.x, ground.y, btn);
397 else if(mode==MEASURE)
398 measure.button_press(x, y, ground.x, ground.y, btn);
401 void Designer::pointer_motion(int x, int y)
403 y = window.get_height()-y-1;
405 if(!root->get_child_at(x, y))
407 Point ground = map_pointer_coords(x, y);
408 manipulator.pointer_motion(x, y, ground.x, ground.y);
409 measure.pointer_motion(x, y, ground.x, ground.y);
413 void Designer::apply_camera()
417 GL::matrix_mode(GL::PROJECTION);
419 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
420 GL::matrix_mode(GL::MODELVIEW);
422 GL::translate(0, 0, -1);
428 void Designer::render()
430 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
435 cat_layout_3d->get_scene().render();
439 pipeline->render_all();
440 GL::enable(GL_CULL_FACE);
442 GL::Bind bind_blend(GL::Blend::alpha());
446 GL::Bind bind_depth(GL::DepthTest::lequal());
447 manipulator.render();
452 GL::matrix_mode(GL::PROJECTION);
454 GL::ortho_bottomleft(window.get_width(), window.get_height());
455 GL::matrix_mode(GL::MODELVIEW);
458 GL::disable(GL::DEPTH_TEST);
460 GL::Bind bind_blend(GL::Blend::alpha());
462 GL::Texture::unbind();
465 Track3D *Designer::pick_track(int x, int y)
467 Layout3D *l = layout_3d;
471 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
472 float yy = (float(y)/window.get_height()-0.5)*0.82843;
473 float size = 4.0/window.get_height()*0.82843;
477 return l->pick_track(xx, yy, size);
480 void Designer::update_track_icon(Track3D &track)
482 overlay->clear(track);
484 if(track.get_track().get_flex())
485 overlay->add_graphic(track, "flex");
487 if(unsigned sid = track.get_track().get_sensor_id())
489 overlay->add_graphic(track, "sensor");
490 overlay->set_label(track, lexical_cast(sid));
492 else if(unsigned tid = track.get_track().get_turnout_id())
496 overlay->add_graphic(track, "turnout");
497 overlay->set_label(track, lexical_cast(tid));
502 void Designer::selection_changed()
504 const set<Track *> &tracks = selection.get_tracks();
506 lbl_status->set_text(string());
510 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
511 len += (*i)->get_type().get_total_length();
512 lbl_status->set_text(format("%.2fm of track selected\n", len));
516 void Designer::manipulation_status(const string &status)
518 lbl_status->set_text(status);
521 void Designer::manipulation_done(bool)
527 void Designer::measure_changed()
529 float pard = measure.get_parallel_distance()*1000;
530 float perpd = measure.get_perpendicular_distance()*1000;
531 float d = sqrt(pard*pard+perpd*perpd);
532 float adiff = measure.get_angle_difference()*180/M_PI;
533 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
534 lbl_status->set_text(info);
537 void Designer::measure_done()
543 void Designer::turnout_id_accept(const string &text)
545 Track *track = selection.get_track();
546 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
547 track->set_turnout_id(id);
549 update_track_icon(layout_3d->get_track(*track));
552 void Designer::sensor_id_accept(const string &text)
554 const set<Track *> &tracks = selection.get_tracks();
555 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
556 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
558 (*i)->set_sensor_id(id);
560 update_track_icon(layout_3d->get_track(**i));
564 void Designer::route_name_accept(const string &text)
567 cur_route->set_name(text);
570 void Designer::view_all()
575 const list<Track3D *> &tracks = layout_3d->get_tracks();
576 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
580 (*i)->get_bounds(0, tmin, tmax);
581 minp.x = min(minp.x, tmin.x);
582 minp.y = min(minp.y, tmin.y);
583 maxp.x = max(maxp.x, tmax.x);
584 maxp.y = max(maxp.y, tmax.y);
587 float t = tan(camera.get_field_of_view()/2)*2;
588 float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
589 float cam_dist = size/t+size*0.25;
590 Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2);
591 camera.set_position(GL::Vector3(center.x, center.y-cam_dist*0.5, cam_dist*0.866));
592 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
595 string Designer::tooltip(int x, int y)
597 if(Track3D *t3d = pick_track(x, y))
599 const Track &track = t3d->get_track();
600 const TrackType &ttype = track.get_type();
601 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
602 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
603 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
604 if(track.get_turnout_id())
605 info += format(" (turnout %d)", track.get_turnout_id());
606 else if(track.get_sensor_id())
607 info += format(" (sensor %d)", track.get_sensor_id());
615 void Designer::show_route(const Route *route)
617 const set<Track *> <racks = layout->get_tracks();
618 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
620 Track3D &t3d = layout_3d->get_track(**i);
621 if(route && route->has_track(**i))
623 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
624 if((*i)->get_type().is_turnout())
626 unsigned tid = (*i)->get_turnout_id();
627 int path = (tid ? route->get_turnout(tid) : -1);
629 t3d.get_path().set_path(path);
631 t3d.get_path().set_mask((*i)->get_type().get_paths());
634 t3d.get_path().set_path(0);
637 t3d.get_path().set_mask(0);