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 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
57 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
58 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
59 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
61 // Setup catalogue and layout
62 DataFile::load(catalogue, "tracks.dat");
64 cat_layout_3d = new Layout3D(catalogue.get_layout());
66 layout = new Layout(catalogue);
67 layout_3d = new Layout3D(*layout);
72 DataFile::load(*layout, argv[1]);
74 if(!layout->get_base().empty())
76 base_object = new GL::Object;
77 DataFile::load(*base_object, layout->get_base());
82 GL::enable(GL_CULL_FACE);
84 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
85 pipeline->set_camera(&camera);
86 pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
88 pipeline->add_renderable_for_pass(*base_object, 0);
89 pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
91 light.set_position(0, -0.259, 0.966, 0);
92 lighting.attach(0, light);
94 GL::PipelinePass *pass = &pipeline->add_pass(0);
95 pass->lighting = &lighting;
96 pass->depth_test = &GL::DepthTest::lequal();
98 pass = &pipeline->add_pass("unlit");
99 pass->depth_test = &GL::DepthTest::lequal();
100 pass->blend = &GL::Blend::alpha();
102 camera.set_up_direction(GL::Vector3(0, 0, 1));
106 DataFile::load(ui_res, "marklin.res");
107 root = new GLtk::Root(ui_res, window);
108 root->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
109 root->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
110 root->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
111 root->signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
113 toolbar = new Toolbar(*this);
115 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
116 toolbar->set_focusable(false);
118 GLtk::Panel *statusbar = new GLtk::Panel(ui_res);
119 root->add(*statusbar);
120 statusbar->set_size(window.get_width(), 20);
121 statusbar->set_focusable(false);
123 lbl_status = new GLtk::Label(ui_res);
124 statusbar->add(*lbl_status);
125 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
127 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
129 const list<Track3D *> &tracks = layout_3d->get_tracks();
130 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
131 update_track_icon(**i);
134 Designer::~Designer()
142 delete cat_layout_3d;
151 return Application::main();
154 void Designer::save()
156 InputDialog *input = new InputDialog(*this, "Save layout", filename);
157 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
160 void Designer::quit()
165 void Designer::new_track()
170 void Designer::set_turnout_id()
172 Track *track = selection.get_track();
173 if(selection.size()==1 && track->get_type().is_turnout())
175 InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
176 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
180 void Designer::set_sensor_id()
182 const set<Track *> &tracks = selection.get_tracks();
185 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
187 if(!(*i)->get_type().is_turnout())
189 if(static_cast<int>((*i)->get_sensor_id())!=id)
192 id = (*i)->get_sensor_id();
200 InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
201 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
205 void Designer::edit_route(Route &r)
208 pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
214 void Designer::add_selection_to_route()
221 const set<Track *> &stracks = selection.get_tracks();
222 set<const Track *> tracks(stracks.begin(), stracks.end());
223 cur_route->add_tracks(tracks);
225 catch(const Exception &e)
227 IO::print("%s\n", e.what());
230 show_route(*cur_route);
233 Point Designer::map_pointer_coords(int x, int y)
235 float xf = x*2.0/window.get_width()-1.0;
236 float yf = y*2.0/window.get_height()-1.0;
237 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
238 const GL::Vector3 &pos = camera.get_position();
240 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
243 void Designer::tick()
245 const Msp::Time::TimeStamp t = Msp::Time::now();
246 float dt = (t-last_tick)/Msp::Time::sec;
249 window.get_display().tick();
255 window.swap_buffers();
258 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
260 key = Input::key_from_sys(key);
261 mod = Input::mod_from_sys(mod);
263 if(key==Msp::Input::KEY_N)
265 else if(key==Msp::Input::KEY_G)
267 manipulator.start_move();
270 else if(key==Msp::Input::KEY_R)
272 manipulator.start_rotate();
275 else if(key==Msp::Input::KEY_D)
277 manipulator.duplicate();
278 manipulator.start_move();
281 else if(key==Msp::Input::KEY_W)
283 else if(key==Msp::Input::KEY_PLUS)
284 selection.select_more();
285 else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
287 const set<Track *> &tracks = layout->get_tracks();
289 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
290 len += (*i)->get_type().get_total_length();
291 IO::print("Total length: %.1fm\n", len);
293 else if(key==Msp::Input::KEY_L)
294 selection.select_linked();
295 else if(key==Msp::Input::KEY_M)
300 else if(key==Msp::Input::KEY_Z)
302 manipulator.start_elevate();
305 else if(key==Msp::Input::KEY_ESC)
308 manipulator.cancel();
309 else if(mode==CATALOGUE)
314 else if(key==Msp::Input::KEY_X)
316 set<Track *> tracks = selection.get_tracks();
318 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
320 overlay->clear(layout_3d->get_track(**i));
321 layout->remove_track(**i);
325 else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
327 const set<Track *> &tracks = selection.get_tracks();
328 const set<Track *> <racks = layout->get_tracks();
329 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
331 (*i)->set_flex(!(*i)->get_flex());
333 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
335 (*i)->snap_to(**j, true);
337 update_track_icon(layout_3d->get_track(**i));
340 else if(key==Msp::Input::KEY_F)
341 manipulator.flatten();
342 else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
343 manipulator.even_slope(true);
344 else if(key==Msp::Input::KEY_E)
345 manipulator.even_slope();
346 else if(key==Msp::Input::KEY_T)
348 else if(key==Msp::Input::KEY_S)
350 else if(key==Msp::Input::KEY_A)
351 add_selection_to_route();
354 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
356 y = window.get_height()-y-1;
357 mod = Input::mod_from_sys(mod);
359 Point ground = map_pointer_coords(x, y);
365 Track3D *ctrack = pick_track(x, y);
368 Track *track = new Track(*layout, ctrack->get_track().get_type());
369 track->set_position(ground);
372 selection.add_track(track);
380 else if(mode==SELECT)
384 Track3D *track = pick_track(x, y);
387 if(!(mod&Input::MOD_SHIFT))
389 selection.toggle_track(&track->get_track());
393 else if(mode==MANIPULATE)
394 manipulator.button_press(x, y, ground.x, ground.y, btn);
395 else if(mode==MEASURE)
396 measure.button_press(x, y, ground.x, ground.y, btn);
399 void Designer::pointer_motion(int x, int y)
401 y = window.get_height()-y-1;
403 if(!root->get_child_at(x, y))
405 Point ground = map_pointer_coords(x, y);
406 manipulator.pointer_motion(x, y, ground.x, ground.y);
407 measure.pointer_motion(x, y, ground.x, ground.y);
411 void Designer::apply_camera()
415 GL::matrix_mode(GL::PROJECTION);
417 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
418 GL::matrix_mode(GL::MODELVIEW);
420 GL::translate(0, 0, -1);
426 void Designer::render()
428 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
433 cat_layout_3d->get_scene().render();
437 pipeline->render_all();
438 GL::enable(GL_CULL_FACE);
440 GL::Bind bind_blend(GL::Blend::alpha());
444 GL::Bind bind_depth(GL::DepthTest::lequal());
445 manipulator.render();
450 GL::matrix_mode(GL::PROJECTION);
452 GL::ortho_bottomleft(window.get_width(), window.get_height());
453 GL::matrix_mode(GL::MODELVIEW);
456 GL::disable(GL::DEPTH_TEST);
458 GL::Bind bind_blend(GL::Blend::alpha());
460 GL::Texture::unbind();
463 Track3D *Designer::pick_track(int x, int y)
465 Layout3D *l = layout_3d;
469 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
470 float yy = (float(y)/window.get_height()-0.5)*0.82843;
471 float size = 4.0/window.get_height()*0.82843;
475 return l->pick_track(xx, yy, size);
478 void Designer::update_track_icon(Track3D &track)
480 overlay->clear(track);
482 if(track.get_track().get_flex())
483 overlay->add_graphic(track, "flex");
485 if(unsigned sid = track.get_track().get_sensor_id())
487 overlay->add_graphic(track, "sensor");
488 overlay->set_label(track, lexical_cast(sid));
490 else if(unsigned tid = track.get_track().get_turnout_id())
494 overlay->add_graphic(track, "turnout");
495 overlay->set_label(track, lexical_cast(tid));
500 void Designer::manipulation_status(const string &status)
502 lbl_status->set_text(status);
505 void Designer::manipulation_done(bool)
507 lbl_status->set_text(string());
511 void Designer::measure_changed()
513 float pard = measure.get_parallel_distance()*1000;
514 float perpd = measure.get_perpendicular_distance()*1000;
515 float d = sqrt(pard*pard+perpd*perpd);
516 float adiff = measure.get_angle_difference()*180/M_PI;
517 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
518 lbl_status->set_text(info);
521 void Designer::measure_done()
523 lbl_status->set_text(string());
527 void Designer::turnout_id_accept(const string &text)
529 Track *track = selection.get_track();
530 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
531 track->set_turnout_id(id);
533 update_track_icon(layout_3d->get_track(*track));
536 void Designer::sensor_id_accept(const string &text)
538 const set<Track *> &tracks = selection.get_tracks();
539 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
540 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
542 (*i)->set_sensor_id(id);
544 update_track_icon(layout_3d->get_track(**i));
548 void Designer::view_all()
553 const list<Track3D *> &tracks = layout_3d->get_tracks();
554 for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
558 (*i)->get_bounds(0, tmin, tmax);
559 minp.x = min(minp.x, tmin.x);
560 minp.y = min(minp.y, tmin.y);
561 maxp.x = max(maxp.x, tmax.x);
562 maxp.y = max(maxp.y, tmax.y);
565 float t = tan(camera.get_field_of_view()/2)*2;
566 float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
567 float cam_dist = size/t+size*0.25;
568 Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2);
569 camera.set_position(GL::Vector3(center.x, center.y-cam_dist*0.5, cam_dist*0.866));
570 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
573 string Designer::tooltip(int x, int y)
575 if(Track3D *t3d = pick_track(x, y))
577 const Track &track = t3d->get_track();
578 const TrackType &ttype = track.get_type();
579 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
580 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
581 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
582 if(track.get_turnout_id())
583 info += format(" (turnout %d)", track.get_turnout_id());
584 else if(track.get_sensor_id())
585 info += format(" (sensor %d)", track.get_sensor_id());
593 void Designer::show_route(const Route &route)
595 const set<Track *> <racks = layout->get_tracks();
596 const set<const Track *> &rtracks = route.get_tracks();
597 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
599 Track3D &t3d = layout_3d->get_track(**i);
600 if(rtracks.count(*i))
602 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
603 if((*i)->get_type().is_turnout())
605 unsigned tid = (*i)->get_turnout_id();
606 int path = (tid ? route.get_turnout(tid) : -1);
608 t3d.get_path().set_path(path);
610 t3d.get_path().set_mask((*i)->get_type().get_paths());
613 t3d.get_path().set_path(0);
616 t3d.get_path().set_mask(0);