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),
49 camera_ctl(window, camera)
51 window.set_title("Railway Designer");
52 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
54 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
55 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
56 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
57 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
59 // Setup catalogue and layout
60 DataFile::load(catalogue, "tracks.dat");
62 cat_layout_3d = new Layout3D(catalogue.get_layout());
64 layout = new Layout(catalogue);
65 layout_3d = new Layout3D(*layout);
70 DataFile::load(*layout, argv[1]);
72 if(!layout->get_base().empty())
74 base_object = new GL::Object;
75 DataFile::load(*base_object, layout->get_base());
80 GL::enable(GL::DEPTH_TEST);
81 GL::enable(GL::BLEND);
82 GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
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(layout_3d->get_scene());
89 pipeline->add_renderable(*base_object);
91 light.set_position(0, -0.259, 0.966, 0);
92 lighting.attach(0, light);
94 GL::PipelinePass *pass = &pipeline->add_pass(GL::Tag());
95 pass->lighting = &lighting;
97 camera.set_up_direction(GL::Vector3(0, 0, 1));
101 DataFile::load(ui_res, "marklin.res");
102 root = new GLtk::Root(ui_res, window);
103 root->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
104 root->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
105 root->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
106 root->signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
108 toolbar = new Toolbar(*this);
110 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
111 toolbar->set_focusable(false);
113 GLtk::Panel *statusbar = new GLtk::Panel(ui_res);
114 root->add(*statusbar);
115 statusbar->set_size(window.get_width(), 20);
116 statusbar->set_focusable(false);
118 lbl_status = new GLtk::Label(ui_res);
119 statusbar->add(*lbl_status);
120 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
122 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
124 const list<Track3D *> &tracks = layout_3d->get_tracks();
125 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
126 update_track_icon(**i);
129 Designer::~Designer()
137 delete cat_layout_3d;
146 return Application::main();
149 void Designer::save()
151 InputDialog *input = new InputDialog(*this, "Save layout", filename);
152 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
155 void Designer::quit()
160 void Designer::new_track()
165 void Designer::set_turnout_id()
167 Track *track = selection.get_track();
168 if(selection.size()==1 && track->get_type().get_n_paths()>1)
170 InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
171 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
175 void Designer::set_sensor_id()
177 const set<Track *> &tracks = selection.get_tracks();
180 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
182 if((*i)->get_type().get_n_paths()==1)
184 if(static_cast<int>((*i)->get_sensor_id())!=id)
187 id = (*i)->get_sensor_id();
195 InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
196 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
200 void Designer::edit_route(Route &r)
205 void Designer::add_selection_to_route()
212 const set<Track *> &stracks = selection.get_tracks();
213 set<const Track *> tracks(stracks.begin(), stracks.end());
214 cur_route->add_tracks(tracks);
216 catch(const Exception &e)
218 IO::print("%s\n", e.what());
222 Point Designer::map_pointer_coords(int x, int y)
224 float xf = x*2.0/window.get_width()-1.0;
225 float yf = y*2.0/window.get_height()-1.0;
226 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
227 const GL::Vector3 &pos = camera.get_position();
229 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
232 void Designer::tick()
234 const Msp::Time::TimeStamp t = Msp::Time::now();
235 float dt = (t-last_tick)/Msp::Time::sec;
238 window.get_display().tick();
244 window.swap_buffers();
247 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
249 key = Input::key_from_sys(key);
250 mod = Input::mod_from_sys(mod);
252 if(key==Msp::Input::KEY_N)
254 else if(key==Msp::Input::KEY_G)
256 manipulator.start_move();
259 else if(key==Msp::Input::KEY_R)
261 manipulator.start_rotate();
264 else if(key==Msp::Input::KEY_D)
266 manipulator.duplicate();
267 manipulator.start_move();
270 else if(key==Msp::Input::KEY_W)
272 else if(key==Msp::Input::KEY_PLUS)
273 selection.select_more();
274 else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
276 const set<Track *> &tracks = layout->get_tracks();
278 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
279 len += (*i)->get_type().get_total_length();
280 IO::print("Total length: %.1fm\n", len);
282 else if(key==Msp::Input::KEY_L)
283 selection.select_linked();
284 else if(key==Msp::Input::KEY_M)
289 else if(key==Msp::Input::KEY_Z)
291 manipulator.start_elevate();
294 else if(key==Msp::Input::KEY_ESC)
297 manipulator.cancel();
298 else if(mode==CATALOGUE)
303 else if(key==Msp::Input::KEY_X)
305 set<Track *> tracks = selection.get_tracks();
307 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
309 overlay->clear(layout_3d->get_track(**i));
310 layout->remove_track(**i);
314 else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
316 const set<Track *> &tracks = selection.get_tracks();
317 const set<Track *> <racks = layout->get_tracks();
318 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
320 (*i)->set_flex(!(*i)->get_flex());
322 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
324 (*i)->snap_to(**j, true);
326 update_track_icon(layout_3d->get_track(**i));
329 else if(key==Msp::Input::KEY_F)
330 manipulator.flatten();
331 else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
332 manipulator.even_slope(true);
333 else if(key==Msp::Input::KEY_E)
334 manipulator.even_slope();
335 else if(key==Msp::Input::KEY_T)
337 else if(key==Msp::Input::KEY_S)
339 else if(key==Msp::Input::KEY_A)
340 add_selection_to_route();
343 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
345 y = window.get_height()-y-1;
346 mod = Input::mod_from_sys(mod);
348 Point ground = map_pointer_coords(x, y);
354 Track3D *ctrack = pick_track(x, y);
357 Track *track = ctrack->get_track().copy();
358 track->set_position(ground);
359 layout->add_track(*track);
362 selection.add_track(track);
370 else if(mode==SELECT)
374 Track3D *track = pick_track(x, y);
377 if(!(mod&Input::MOD_SHIFT))
379 selection.toggle_track(&track->get_track());
383 else if(mode==MANIPULATE)
384 manipulator.button_press(x, y, ground.x, ground.y, btn);
385 else if(mode==MEASURE)
386 measure.button_press(x, y, ground.x, ground.y, btn);
389 void Designer::pointer_motion(int x, int y)
391 y = window.get_height()-y-1;
393 if(!root->get_child_at(x, y))
395 Point ground = map_pointer_coords(x, y);
396 manipulator.pointer_motion(x, y, ground.x, ground.y);
397 measure.pointer_motion(x, y, ground.x, ground.y);
401 void Designer::apply_camera()
405 GL::matrix_mode(GL::PROJECTION);
407 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
408 GL::matrix_mode(GL::MODELVIEW);
410 GL::translate(0, 0, -1);
416 void Designer::render()
418 GL::clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
419 GL::enable(GL::DEPTH_TEST);
420 GL::Texture::unbind();
425 cat_layout_3d->get_scene().render();
429 pipeline->render_all();
430 layout_3d->get_endpoint_scene().render();
431 GL::enable(GL_CULL_FACE);
432 GL::disable(GL::DEPTH_TEST);
434 GL::enable(GL::DEPTH_TEST);
437 glColor4f(0.5, 0.8, 1.0, 1.0);
438 const set<const Track *> &rtracks = cur_route->get_tracks();
439 const map<unsigned, int> &turnouts = cur_route->get_turnouts();
440 for(set<const Track *>::const_iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
443 if(unsigned tid=(*i)->get_turnout_id())
445 map<unsigned, int>::const_iterator j = turnouts.find(tid);
446 if(j!=turnouts.end())
449 layout_3d->get_track(**i).render_path(path);
453 manipulator.render();
458 GL::matrix_mode(GL::PROJECTION);
460 GL::ortho_bottomleft(window.get_width(), window.get_height());
461 GL::matrix_mode(GL::MODELVIEW);
464 GL::disable(GL::DEPTH_TEST);
469 Track3D *Designer::pick_track(int x, int y)
471 Layout3D *l = layout_3d;
475 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
476 float yy = (float(y)/window.get_height()-0.5)*0.82843;
477 float size = 4.0/window.get_height()*0.82843;
481 return l->pick_track(xx, yy, size);
484 void Designer::update_track_icon(Track3D &track)
486 overlay->clear(track);
488 if(track.get_track().get_flex())
489 overlay->add_graphic(track, "flex");
491 if(unsigned sid = track.get_track().get_sensor_id())
493 overlay->add_graphic(track, "sensor");
494 overlay->set_label(track, lexical_cast(sid));
496 else if(unsigned tid = track.get_track().get_turnout_id())
498 overlay->add_graphic(track, "turnout");
499 overlay->set_label(track, lexical_cast(tid));
503 void Designer::manipulation_status(const string &status)
505 lbl_status->set_text(status);
508 void Designer::manipulation_done(bool)
510 lbl_status->set_text(string());
514 void Designer::measure_changed()
516 float pard = measure.get_parallel_distance()*1000;
517 float perpd = measure.get_perpendicular_distance()*1000;
518 float d = sqrt(pard*pard+perpd*perpd);
519 float adiff = measure.get_angle_difference()*180/M_PI;
520 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
521 lbl_status->set_text(info);
524 void Designer::measure_done()
526 lbl_status->set_text(string());
530 void Designer::turnout_id_accept(const string &text)
532 Track *track = selection.get_track();
533 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
534 track->set_turnout_id(id);
536 update_track_icon(layout_3d->get_track(*track));
539 void Designer::sensor_id_accept(const string &text)
541 const set<Track *> &tracks = selection.get_tracks();
542 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
543 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
545 (*i)->set_sensor_id(id);
547 update_track_icon(layout_3d->get_track(**i));
551 void Designer::view_all()
556 const list<Track3D *> &tracks = layout_3d->get_tracks();
557 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
561 (*i)->get_bounds(0, tmin, tmax);
562 minp.x = min(minp.x, tmin.x);
563 minp.y = min(minp.y, tmin.y);
564 maxp.x = max(maxp.x, tmax.x);
565 maxp.y = max(maxp.y, tmax.y);
568 float t = tan(camera.get_field_of_view()/2)*2;
569 float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
570 float cam_dist = size/t+size*0.25;
571 Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2);
572 camera.set_position(GL::Vector3(center.x, center.y-cam_dist*0.5, cam_dist*0.866));
573 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
576 string Designer::tooltip(int x, int y)
578 if(Track3D *t3d = pick_track(x, y))
580 const Track &track = t3d->get_track();
581 const TrackType &ttype = track.get_type();
582 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
583 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
584 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
585 if(track.get_turnout_id())
586 info += format(" (turnout %d)", track.get_turnout_id());
587 else if(track.get_sensor_id())
588 info += format(" (sensor %d)", track.get_sensor_id());