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);
110 lbl_tooltip = new GLtk::Label(ui_res);
111 lbl_tooltip->set_style("tooltip");
112 root->add(*lbl_tooltip);
113 lbl_tooltip->set_visible(false);
115 toolbar = new Toolbar(*this);
117 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
118 toolbar->set_visible(true);
120 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
122 const list<Track3D *> &tracks = layout_3d->get_tracks();
123 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
124 update_track_icon(**i);
127 Designer::~Designer()
132 delete cat_layout_3d;
141 return Application::main();
144 void Designer::save()
146 input = new ::Input(*this, "Save layout", filename);
147 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
148 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
152 void Designer::quit()
157 void Designer::edit_route(Route &r)
162 void Designer::add_selection_to_route()
169 const set<Track *> &stracks = selection.get_tracks();
170 set<const Track *> tracks(stracks.begin(), stracks.end());
171 cur_route->add_tracks(tracks);
173 catch(const Exception &e)
175 IO::print("%s\n", e.what());
179 Point Designer::map_pointer_coords(int x, int y)
181 float xf = x*2.0/window.get_width()-1.0;
182 float yf = y*2.0/window.get_height()-1.0;
183 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
184 const GL::Vector3 &pos = camera.get_position();
186 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
189 void Designer::tick()
191 const Msp::Time::TimeStamp t = Msp::Time::now();
192 float dt = (t-last_tick)/Msp::Time::sec;
195 window.get_display().tick();
198 if(tooltip_timeout && t>tooltip_timeout)
203 t3d = pick_track(pointer_x, pointer_y);
205 t3d = pick_track(pointer_x, pointer_y);
209 const Track &track = t3d->get_track();
210 const TrackType &ttype = track.get_type();
211 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
212 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
213 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
214 if(track.get_turnout_id())
215 info += format(" (turnout %d)", track.get_turnout_id());
216 else if(track.get_sensor_id())
217 info += format(" (sensor %d)", track.get_sensor_id());
219 set_tooltip(pointer_x, pointer_y, info);
224 tooltip_timeout = Msp::Time::TimeStamp();
229 window.swap_buffers();
232 void Designer::key_press(unsigned code, unsigned mod, wchar_t)
234 unsigned key = Msp::Input::key_from_sys(code);
239 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
242 if(key==Msp::Input::KEY_N)
244 else if(key==Msp::Input::KEY_G)
246 manipulator.start_move();
249 else if(key==Msp::Input::KEY_R)
251 manipulator.start_rotate();
254 else if(key==Msp::Input::KEY_D)
256 manipulator.duplicate();
257 manipulator.start_move();
260 else if(key==Msp::Input::KEY_W)
262 else if(key==Msp::Input::KEY_PLUS)
263 selection.select_more();
264 else if(key==Msp::Input::KEY_L && (mod&1))
266 const set<Track *> &tracks = layout->get_tracks();
268 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
269 len += (*i)->get_type().get_total_length();
270 IO::print("Total length: %.1fm\n", len);
272 else if(key==Msp::Input::KEY_L)
273 selection.select_linked();
274 else if(key==Msp::Input::KEY_M)
279 else if(key==Msp::Input::KEY_Z)
281 manipulator.start_elevate();
284 else if(key==Msp::Input::KEY_ESC)
287 manipulator.cancel();
288 else if(mode==CATALOGUE)
293 else if(key==Msp::Input::KEY_X)
295 set<Track *> tracks = selection.get_tracks();
297 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
299 layout->remove_track(**i);
303 else if(key==Msp::Input::KEY_F && (mod&1))
305 const set<Track *> &tracks = selection.get_tracks();
306 const set<Track *> <racks = layout->get_tracks();
307 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
309 (*i)->set_flex(!(*i)->get_flex());
311 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
313 (*i)->snap_to(**j, true);
315 update_track_icon(layout_3d->get_track(**i));
318 else if(key==Msp::Input::KEY_F)
319 manipulator.flatten();
320 else if(key==Msp::Input::KEY_E && (mod&1))
321 manipulator.even_slope(true);
322 else if(key==Msp::Input::KEY_E)
323 manipulator.even_slope();
324 else if(key==Msp::Input::KEY_T)
326 Track *track = selection.get_track();
327 if(selection.size()==1 && track->get_type().get_n_paths()>1)
329 input = new ::Input(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
330 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
331 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
335 else if(key==Msp::Input::KEY_S)
337 const set<Track *> &tracks = selection.get_tracks();
340 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
342 if((*i)->get_type().get_n_paths()==1)
344 if(static_cast<int>((*i)->get_sensor_id())!=id)
347 id = (*i)->get_sensor_id();
354 input = new ::Input(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
355 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
356 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
360 else if(key==Msp::Input::KEY_A)
361 add_selection_to_route();
364 void Designer::key_release(unsigned code, unsigned)
366 unsigned key = Msp::Input::key_from_sys(code);
371 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
375 void Designer::button_press(int x, int y, unsigned btn, unsigned)
377 y = window.get_height()-y-1;
379 Point ground = map_pointer_coords(x, y);
385 Track3D *ctrack = pick_track(x, y);
388 Track *track = ctrack->get_track().copy();
389 track->set_position(ground);
390 layout->add_track(*track);
393 selection.add_track(track);
401 else if(mode==SELECT)
405 Track3D *track = pick_track(x, y);
410 selection.toggle_track(&track->get_track());
414 else if(mode==MANIPULATE)
415 manipulator.button_press(x, y, ground.x, ground.y, btn);
416 else if(mode==MEASURE)
417 measure.button_press(x, y, ground.x, ground.y, btn);
420 void Designer::pointer_motion(int x, int y)
422 y = window.get_height()-y-1;
427 if(mode==SELECT || mode==CATALOGUE)
428 tooltip_timeout = Msp::Time::now()+100*Msp::Time::msec;
434 Point ground = map_pointer_coords(x, y);
435 manipulator.pointer_motion(x, y, ground.x, ground.y);
436 measure.pointer_motion(x, y, ground.x, ground.y);
440 void Designer::apply_camera()
444 GL::matrix_mode(GL::PROJECTION);
446 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
447 GL::matrix_mode(GL::MODELVIEW);
449 GL::translate(0, 0, -1);
455 void Designer::render()
457 GL::clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
458 GL::enable(GL::DEPTH_TEST);
459 GL::Texture::unbind();
464 cat_layout_3d->get_scene().render();
468 pipeline->render_all();
469 layout_3d->get_endpoint_scene().render();
470 GL::enable(GL_CULL_FACE);
471 GL::disable(GL::DEPTH_TEST);
473 GL::enable(GL::DEPTH_TEST);
476 glColor4f(0.5, 0.8, 1.0, 1.0);
477 const set<const Track *> &rtracks = cur_route->get_tracks();
478 const map<unsigned, int> &turnouts = cur_route->get_turnouts();
479 for(set<const Track *>::const_iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
482 if(unsigned tid=(*i)->get_turnout_id())
484 map<unsigned, int>::const_iterator j = turnouts.find(tid);
485 if(j!=turnouts.end())
488 layout_3d->get_track(**i).render_path(path);
492 manipulator.render();
497 GL::matrix_mode(GL::PROJECTION);
499 GL::ortho_bottomleft(window.get_width(), window.get_height());
500 GL::matrix_mode(GL::MODELVIEW);
503 GL::disable(GL::DEPTH_TEST);
508 Track3D *Designer::pick_track(int x, int y)
510 Layout3D *l = layout_3d;
514 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
515 float yy = (float(y)/window.get_height()-0.5)*0.82843;
516 float size = 4.0/window.get_height()*0.82843;
520 return l->pick_track(xx, yy, size);
523 void Designer::update_track_icon(Track3D &track)
525 overlay->clear(track);
527 if(track.get_track().get_flex())
528 overlay->add_graphic(track, "flex");
530 if(unsigned sid = track.get_track().get_sensor_id())
532 overlay->add_graphic(track, "sensor");
533 overlay->set_label(track, lexical_cast(sid));
535 else if(unsigned tid = track.get_track().get_turnout_id())
537 overlay->add_graphic(track, "turnout");
538 overlay->set_label(track, lexical_cast(tid));
542 void Designer::manipulation_status(const string &status)
544 set_tooltip(pointer_x, pointer_y, status);
547 void Designer::manipulation_done(bool)
552 void Designer::measure_changed()
554 float pard = measure.get_parallel_distance()*1000;
555 float perpd = measure.get_perpendicular_distance()*1000;
556 float d = sqrt(pard*pard+perpd*perpd);
557 float adiff = measure.get_angle_difference()*180/M_PI;
558 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
559 set_tooltip(pointer_x, pointer_y, info);
562 void Designer::measure_done()
567 void Designer::set_tooltip(int x, int y, const std::string &text)
569 int fontsize = ui_res.get_default_font().get_default_size();
571 int w = static_cast<int>(ui_res.get_default_font().get_string_width(text)*fontsize)+6;
572 x = max(min(static_cast<int>(window.get_width())-w, x), 0);
573 y = max(min(static_cast<int>(window.get_height())-h, y), 0);
574 lbl_tooltip->set_text(text);
575 lbl_tooltip->set_geometry(GLtk::Geometry(x, y, w, h));
576 lbl_tooltip->set_visible(true);
579 void Designer::clear_tooltip()
581 lbl_tooltip->set_visible(false);
584 void Designer::save_accept()
586 layout->save(input->get_text());
591 void Designer::turnout_id_accept()
593 Track *track = selection.get_track();
594 unsigned id = lexical_cast<unsigned>(input->get_text());
595 track->set_turnout_id(id);
597 update_track_icon(layout_3d->get_track(*track));
602 void Designer::sensor_id_accept()
604 const set<Track *> &tracks = selection.get_tracks();
605 unsigned id = lexical_cast<unsigned>(input->get_text());
606 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
608 (*i)->set_sensor_id(id);
610 update_track_icon(layout_3d->get_track(**i));
616 void Designer::input_dismiss()
623 void Designer::view_all()
628 const list<Track3D *> &tracks = layout_3d->get_tracks();
629 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
633 (*i)->get_bounds(0, tmin, tmax);
634 minp.x = min(minp.x, tmin.x);
635 minp.y = min(minp.y, tmin.y);
636 maxp.x = max(maxp.x, tmax.x);
637 maxp.y = max(maxp.y, tmax.y);
640 float t = tan(camera.get_field_of_view()/2)*2;
641 float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
642 float cam_dist = size/t+size*0.25;
643 Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2);
644 camera.set_position(GL::Vector3(center.x, center.y-cam_dist*0.5, cam_dist*0.866));
645 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));