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/tracktype.h"
31 #include "manipulator.h"
33 #include "selection.h"
37 using namespace Marklin;
40 Application::RegApp<Designer> Designer::reg;
42 Designer::Designer(int argc, char **argv):
47 manipulator(*this, selection),
50 camera_ctl(window, camera),
53 window.set_title("Railway Designer");
54 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
55 window.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
56 window.signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release));
57 window.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
58 window.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
60 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
61 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
62 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
63 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
65 // Setup catalogue and layout
66 DataFile::load(catalogue, "tracks.dat");
68 cat_layout_3d = new Layout3D(catalogue.get_layout());
70 layout = new Layout(catalogue);
71 layout_3d = new Layout3D(*layout);
76 DataFile::load(*layout, argv[1]);
78 if(!layout->get_base().empty())
80 base_object = new GL::Object;
81 DataFile::load(*base_object, layout->get_base());
86 GL::enable(GL::DEPTH_TEST);
87 GL::enable(GL::BLEND);
88 GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
89 GL::enable(GL_CULL_FACE);
91 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
92 pipeline->set_camera(&camera);
93 pipeline->add_renderable(layout_3d->get_scene());
95 pipeline->add_renderable(*base_object);
97 light.set_position(0, -0.259, 0.966, 0);
98 lighting.attach(0, light);
100 GL::PipelinePass *pass = &pipeline->add_pass(GL::Tag());
101 pass->lighting = &lighting;
103 camera.set_up_direction(GL::Vector3(0, 0, 1));
107 DataFile::load(ui_res, "marklin.res");
108 root = new GLtk::Root(ui_res, window);
109 root->signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
111 toolbar = new Toolbar(*this);
113 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
114 toolbar->set_visible(true);
116 GLtk::Panel *statusbar = new GLtk::Panel(ui_res);
117 root->add(*statusbar);
118 statusbar->set_size(window.get_width(), 20);
119 statusbar->set_visible(true);
121 lbl_status = new GLtk::Label(ui_res);
122 statusbar->add(*lbl_status);
123 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
125 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
127 const list<Track3D *> &tracks = layout_3d->get_tracks();
128 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
129 update_track_icon(**i);
132 Designer::~Designer()
140 delete cat_layout_3d;
149 return Application::main();
152 void Designer::save()
154 input = new ::Input(*this, "Save layout", filename);
155 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
156 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
160 void Designer::quit()
165 void Designer::edit_route(Route &r)
170 void Designer::add_selection_to_route()
177 const set<Track *> &stracks = selection.get_tracks();
178 set<const Track *> tracks(stracks.begin(), stracks.end());
179 cur_route->add_tracks(tracks);
181 catch(const Exception &e)
183 IO::print("%s\n", e.what());
187 Point Designer::map_pointer_coords(int x, int y)
189 float xf = x*2.0/window.get_width()-1.0;
190 float yf = y*2.0/window.get_height()-1.0;
191 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
192 const GL::Vector3 &pos = camera.get_position();
194 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
197 void Designer::tick()
199 const Msp::Time::TimeStamp t = Msp::Time::now();
200 float dt = (t-last_tick)/Msp::Time::sec;
203 window.get_display().tick();
209 window.swap_buffers();
212 void Designer::key_press(unsigned code, unsigned mod, wchar_t)
214 unsigned key = Msp::Input::key_from_sys(code);
219 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
222 if(key==Msp::Input::KEY_N)
224 else if(key==Msp::Input::KEY_G)
226 manipulator.start_move();
229 else if(key==Msp::Input::KEY_R)
231 manipulator.start_rotate();
234 else if(key==Msp::Input::KEY_D)
236 manipulator.duplicate();
237 manipulator.start_move();
240 else if(key==Msp::Input::KEY_W)
242 else if(key==Msp::Input::KEY_PLUS)
243 selection.select_more();
244 else if(key==Msp::Input::KEY_L && (mod&1))
246 const set<Track *> &tracks = layout->get_tracks();
248 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
249 len += (*i)->get_type().get_total_length();
250 IO::print("Total length: %.1fm\n", len);
252 else if(key==Msp::Input::KEY_L)
253 selection.select_linked();
254 else if(key==Msp::Input::KEY_M)
259 else if(key==Msp::Input::KEY_Z)
261 manipulator.start_elevate();
264 else if(key==Msp::Input::KEY_ESC)
267 manipulator.cancel();
268 else if(mode==CATALOGUE)
273 else if(key==Msp::Input::KEY_X)
275 set<Track *> tracks = selection.get_tracks();
277 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
279 overlay->clear(layout_3d->get_track(**i));
280 layout->remove_track(**i);
284 else if(key==Msp::Input::KEY_F && (mod&1))
286 const set<Track *> &tracks = selection.get_tracks();
287 const set<Track *> <racks = layout->get_tracks();
288 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
290 (*i)->set_flex(!(*i)->get_flex());
292 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
294 (*i)->snap_to(**j, true);
296 update_track_icon(layout_3d->get_track(**i));
299 else if(key==Msp::Input::KEY_F)
300 manipulator.flatten();
301 else if(key==Msp::Input::KEY_E && (mod&1))
302 manipulator.even_slope(true);
303 else if(key==Msp::Input::KEY_E)
304 manipulator.even_slope();
305 else if(key==Msp::Input::KEY_T)
307 Track *track = selection.get_track();
308 if(selection.size()==1 && track->get_type().get_n_paths()>1)
310 input = new ::Input(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
311 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
312 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
316 else if(key==Msp::Input::KEY_S)
318 const set<Track *> &tracks = selection.get_tracks();
321 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
323 if((*i)->get_type().get_n_paths()==1)
325 if(static_cast<int>((*i)->get_sensor_id())!=id)
328 id = (*i)->get_sensor_id();
335 input = new ::Input(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
336 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
337 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
341 else if(key==Msp::Input::KEY_A)
342 add_selection_to_route();
345 void Designer::key_release(unsigned code, unsigned)
347 unsigned key = Msp::Input::key_from_sys(code);
352 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
356 void Designer::button_press(int x, int y, unsigned btn, unsigned)
358 y = window.get_height()-y-1;
360 Point ground = map_pointer_coords(x, y);
366 Track3D *ctrack = pick_track(x, y);
369 Track *track = ctrack->get_track().copy();
370 track->set_position(ground);
371 layout->add_track(*track);
374 selection.add_track(track);
382 else if(mode==SELECT)
386 Track3D *track = pick_track(x, y);
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;
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::clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
431 GL::enable(GL::DEPTH_TEST);
432 GL::Texture::unbind();
437 cat_layout_3d->get_scene().render();
441 pipeline->render_all();
442 layout_3d->get_endpoint_scene().render();
443 GL::enable(GL_CULL_FACE);
444 GL::disable(GL::DEPTH_TEST);
446 GL::enable(GL::DEPTH_TEST);
449 glColor4f(0.5, 0.8, 1.0, 1.0);
450 const set<const Track *> &rtracks = cur_route->get_tracks();
451 const map<unsigned, int> &turnouts = cur_route->get_turnouts();
452 for(set<const Track *>::const_iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
455 if(unsigned tid=(*i)->get_turnout_id())
457 map<unsigned, int>::const_iterator j = turnouts.find(tid);
458 if(j!=turnouts.end())
461 layout_3d->get_track(**i).render_path(path);
465 manipulator.render();
470 GL::matrix_mode(GL::PROJECTION);
472 GL::ortho_bottomleft(window.get_width(), window.get_height());
473 GL::matrix_mode(GL::MODELVIEW);
476 GL::disable(GL::DEPTH_TEST);
481 Track3D *Designer::pick_track(int x, int y)
483 Layout3D *l = layout_3d;
487 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
488 float yy = (float(y)/window.get_height()-0.5)*0.82843;
489 float size = 4.0/window.get_height()*0.82843;
493 return l->pick_track(xx, yy, size);
496 void Designer::update_track_icon(Track3D &track)
498 overlay->clear(track);
500 if(track.get_track().get_flex())
501 overlay->add_graphic(track, "flex");
503 if(unsigned sid = track.get_track().get_sensor_id())
505 overlay->add_graphic(track, "sensor");
506 overlay->set_label(track, lexical_cast(sid));
508 else if(unsigned tid = track.get_track().get_turnout_id())
510 overlay->add_graphic(track, "turnout");
511 overlay->set_label(track, lexical_cast(tid));
515 void Designer::manipulation_status(const string &status)
517 lbl_status->set_text(status);
520 void Designer::manipulation_done(bool)
522 lbl_status->set_text(string());
526 void Designer::measure_changed()
528 float pard = measure.get_parallel_distance()*1000;
529 float perpd = measure.get_perpendicular_distance()*1000;
530 float d = sqrt(pard*pard+perpd*perpd);
531 float adiff = measure.get_angle_difference()*180/M_PI;
532 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
533 lbl_status->set_text(info);
536 void Designer::measure_done()
538 lbl_status->set_text(string());
542 void Designer::save_accept()
544 layout->save(input->get_text());
549 void Designer::turnout_id_accept()
551 Track *track = selection.get_track();
552 unsigned id = lexical_cast<unsigned>(input->get_text());
553 track->set_turnout_id(id);
555 update_track_icon(layout_3d->get_track(*track));
560 void Designer::sensor_id_accept()
562 const set<Track *> &tracks = selection.get_tracks();
563 unsigned id = lexical_cast<unsigned>(input->get_text());
564 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
566 (*i)->set_sensor_id(id);
568 update_track_icon(layout_3d->get_track(**i));
574 void Designer::input_dismiss()
581 void Designer::view_all()
586 const list<Track3D *> &tracks = layout_3d->get_tracks();
587 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
591 (*i)->get_bounds(0, tmin, tmax);
592 minp.x = min(minp.x, tmin.x);
593 minp.y = min(minp.y, tmin.y);
594 maxp.x = max(maxp.x, tmax.x);
595 maxp.y = max(maxp.y, tmax.y);
598 float t = tan(camera.get_field_of_view()/2)*2;
599 float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
600 float cam_dist = size/t+size*0.25;
601 Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2);
602 camera.set_position(GL::Vector3(center.x, center.y-cam_dist*0.5, cam_dist*0.866));
603 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
606 string Designer::tooltip(int x, int y)
608 if(Track3D *t3d = pick_track(x, y))
610 const Track &track = t3d->get_track();
611 const TrackType &ttype = track.get_type();
612 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
613 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
614 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
615 if(track.get_turnout_id())
616 info += format(" (turnout %d)", track.get_turnout_id());
617 else if(track.get_sensor_id())
618 info += format(" (sensor %d)", track.get_sensor_id());