3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
12 #include <msp/gl/blend.h>
13 #include <msp/gl/framebuffer.h>
14 #include <msp/gl/matrix.h>
15 #include <msp/gl/misc.h>
16 #include <msp/gl/projection.h>
17 #include <msp/gl/rendermode.h>
18 #include <msp/gl/select.h>
19 #include <msp/gl/tests.h>
20 #include <msp/gl/texture2d.h>
21 #include <msp/input/keys.h>
22 #include <msp/io/print.h>
23 #include <msp/strings/codec.h>
24 #include <msp/strings/lexicalcast.h>
25 #include <msp/strings/utf8.h>
26 #include <msp/strings/utils.h>
27 #include <msp/time/units.h>
28 #include <msp/time/utils.h>
29 #include "libmarklin/route.h"
30 #include "libmarklin/tracktype.h"
34 #include "manipulator.h"
36 #include "selection.h"
40 using namespace Marklin;
43 Application::RegApp<Designer> Designer::reg;
45 Designer::Designer(int argc, char **argv):
47 ui_res("marklin.res"),
52 manipulator(*this, selection),
54 camera_ctl(*this, root, camera),
55 track_wrap(*this, selection)
57 window.set_title("Railway Designer");
58 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
60 selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
61 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
62 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
63 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
64 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
66 // Setup catalogue and layout
67 DataFile::load(catalogue, "tracks.dat");
69 cat_layout_3d = new Layout3D(catalogue.get_layout());
71 layout = new Layout(catalogue);
72 layout_3d = new Layout3D(*layout);
74 layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
75 layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
80 DataFile::load(*layout, argv[1]);
82 if(!layout->get_base().empty())
84 base_object = new GL::Object;
85 DataFile::load(*base_object, layout->get_base());
90 GL::enable(GL_CULL_FACE);
92 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
93 pipeline->set_camera(&camera);
94 pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
96 pipeline->add_renderable(*base_object);
97 pipeline->add_renderable_for_pass(track_wrap, "unlit");
98 pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
99 pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
101 light.set_position(0, -0.259, 0.966, 0);
102 lighting.attach(0, light);
104 GL::PipelinePass *pass = &pipeline->add_pass(0);
105 pass->lighting = &lighting;
106 pass->depth_test = &GL::DepthTest::lequal();
108 pass = &pipeline->add_pass("unlit");
109 pass->depth_test = &GL::DepthTest::lequal();
110 pass->blend = &GL::Blend::alpha();
112 pass = &pipeline->add_pass("blended");
113 pass->lighting = &lighting;
114 pass->depth_test = &GL::DepthTest::lequal();
115 pass->blend = &GL::Blend::alpha();
117 camera.set_up_direction(GL::Vector3(0, 0, 1));
118 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
121 root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
122 root.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
123 root.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
124 root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
126 toolbar = new Toolbar(*this);
128 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
129 toolbar->set_focusable(false);
131 GLtk::Panel *statusbar = new GLtk::Panel(ui_res);
132 root.add(*statusbar);
133 statusbar->set_size(window.get_width(), 20);
134 statusbar->set_focusable(false);
136 lbl_status = new GLtk::Label(ui_res);
137 statusbar->add(*lbl_status);
138 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
140 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
142 camera_ctl.view_all();
144 const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
145 for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
146 update_track_icon(*i->second);
149 Designer::~Designer()
156 delete cat_layout_3d;
165 return Application::main();
168 void Designer::save()
170 InputDialog *input = new InputDialog(*this, "Save layout", filename);
171 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
174 void Designer::quit()
179 void Designer::new_track()
184 void Designer::set_turnout_id()
186 Track *track = selection.get_track();
187 if(selection.size()==1 && track->get_type().is_turnout())
189 InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
190 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
194 void Designer::set_sensor_id()
196 const set<Track *> &tracks = selection.get_tracks();
199 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
201 if(!(*i)->get_type().is_turnout())
203 if(static_cast<int>((*i)->get_sensor_id())!=id)
206 id = (*i)->get_sensor_id();
214 InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
215 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
219 void Designer::rename_route()
224 InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
225 input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
228 void Designer::edit_route(Route *r)
234 void Designer::add_selection_to_route()
241 cur_route->add_tracks(selection.get_tracks());
243 catch(const Exception &e)
245 lbl_status->set_text(e.what());
248 show_route(cur_route);
251 Point Designer::map_pointer_coords(int x, int y)
253 float xf = x*2.0/window.get_width()-1.0;
254 float yf = y*2.0/window.get_height()-1.0;
255 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
256 const GL::Vector3 &pos = camera.get_position();
258 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
261 void Designer::tick()
263 const Msp::Time::TimeStamp t = Msp::Time::now();
264 float dt = (t-last_tick)/Msp::Time::sec;
271 for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
272 layout_3d->get_track(**i).get_path().set_mask(0);
277 window.swap_buffers();
280 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
282 key = Input::key_from_sys(key);
283 mod = Input::mod_from_sys(mod);
285 if(key==Msp::Input::KEY_N && (mod&Input::MOD_SHIFT))
287 manipulator.start_extend();
290 else if(key==Msp::Input::KEY_N)
292 else if(key==Msp::Input::KEY_G)
294 manipulator.start_move();
297 else if(key==Msp::Input::KEY_R)
299 manipulator.start_rotate();
302 else if(key==Msp::Input::KEY_D)
304 manipulator.duplicate();
305 manipulator.start_move();
308 else if(key==Msp::Input::KEY_W)
310 else if(key==Msp::Input::KEY_PLUS)
311 selection.select_more();
312 else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
313 selection.select_blocks();
314 else if(key==Msp::Input::KEY_L)
315 selection.select_linked();
316 else if(key==Msp::Input::KEY_M)
321 else if(key==Msp::Input::KEY_Z)
323 manipulator.start_elevate();
326 else if(key==Msp::Input::KEY_ESC)
329 manipulator.cancel();
330 else if(mode==CATALOGUE)
335 else if(key==Msp::Input::KEY_X)
337 set<Track *> tracks = selection.get_tracks();
339 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
341 overlay->clear(layout_3d->get_track(**i));
342 layout->remove_track(**i);
346 else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
348 const set<Track *> &tracks = selection.get_tracks();
349 const set<Track *> <racks = layout->get_tracks();
350 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
352 (*i)->set_flex(!(*i)->get_flex());
354 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
356 (*i)->snap_to(**j, true);
358 update_track_icon(layout_3d->get_track(**i));
361 else if(key==Msp::Input::KEY_F)
362 manipulator.flatten();
363 else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
364 manipulator.even_slope(true);
365 else if(key==Msp::Input::KEY_E)
366 manipulator.even_slope();
367 else if(key==Msp::Input::KEY_T)
369 else if(key==Msp::Input::KEY_S)
371 else if(key==Msp::Input::KEY_A)
372 add_selection_to_route();
373 else if(key==Msp::Input::KEY_C)
374 manipulator.connect();
377 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
379 y = window.get_height()-y-1;
380 mod = Input::mod_from_sys(mod);
382 Point ground = map_pointer_coords(x, y);
388 Track3D *ctrack = pick_track(x, y);
391 Track *track = new Track(*layout, ctrack->get_track().get_type());
392 track->set_position(ground);
395 selection.add_track(track);
403 else if(mode==SELECT)
407 Track3D *track = pick_track(x, y);
410 if(!(mod&Input::MOD_SHIFT))
412 selection.toggle_track(&track->get_track());
416 else if(mode==MANIPULATE)
417 manipulator.button_press(x, y, ground.x, ground.y, btn);
418 else if(mode==MEASURE)
419 measure.button_press(x, y, ground.x, ground.y, btn);
422 void Designer::pointer_motion(int x, int y)
424 y = window.get_height()-y-1;
426 if(!root.get_child_at(x, y))
428 Point ground = map_pointer_coords(x, y);
429 manipulator.pointer_motion(x, y, ground.x, ground.y);
430 measure.pointer_motion(x, y, ground.x, ground.y);
434 void Designer::apply_camera()
438 GL::matrix_mode(GL::PROJECTION);
440 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
441 GL::matrix_mode(GL::MODELVIEW);
443 GL::translate(0, 0, -1);
449 void Designer::render()
451 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
456 cat_layout_3d->get_scene().render();
460 pipeline->render_all();
461 GL::enable(GL_CULL_FACE);
463 GL::Bind bind_blend(GL::Blend::alpha());
467 GL::Bind bind_depth(GL::DepthTest::lequal());
472 GL::matrix_mode(GL::PROJECTION);
474 GL::ortho_bottomleft(window.get_width(), window.get_height());
475 GL::matrix_mode(GL::MODELVIEW);
478 GL::disable(GL::DEPTH_TEST);
480 GL::Bind bind_blend(GL::Blend::alpha());
482 GL::Texture::unbind();
485 void Designer::track_added(Track &trk)
487 new_tracks.push_back(&trk);
490 void Designer::track_removed(Track &trk)
492 list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
493 if(i!=new_tracks.end())
497 Track3D *Designer::pick_track(int x, int y)
499 Layout3D *l = layout_3d;
503 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
504 float yy = (float(y)/window.get_height()-0.5)*0.82843;
505 float size = 4.0/window.get_height()*0.82843;
509 return l->pick_track(xx, yy, size);
512 void Designer::update_track_icon(Track3D &track)
514 overlay->clear(track);
516 if(track.get_track().get_flex())
517 overlay->add_graphic(track, "flex");
519 if(unsigned sid = track.get_track().get_sensor_id())
521 overlay->add_graphic(track, "sensor");
522 overlay->set_label(track, lexical_cast(sid));
524 else if(unsigned tid = track.get_track().get_turnout_id())
528 overlay->add_graphic(track, "turnout");
529 overlay->set_label(track, lexical_cast(tid));
534 void Designer::selection_changed()
536 const set<Track *> &tracks = selection.get_tracks();
538 lbl_status->set_text(string());
542 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
543 len += (*i)->get_type().get_total_length();
544 lbl_status->set_text(format("%.2fm of track selected\n", len));
548 void Designer::manipulation_status(const string &status)
550 lbl_status->set_text(status);
553 void Designer::manipulation_done(bool)
559 void Designer::measure_changed()
561 float pard = measure.get_parallel_distance()*1000;
562 float perpd = measure.get_perpendicular_distance()*1000;
563 float d = sqrt(pard*pard+perpd*perpd);
564 float adiff = measure.get_angle_difference()*180/M_PI;
565 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
566 lbl_status->set_text(info);
569 void Designer::measure_done()
575 void Designer::turnout_id_accept(const string &text)
577 Track *track = selection.get_track();
578 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
579 track->set_turnout_id(id);
581 update_track_icon(layout_3d->get_track(*track));
584 void Designer::sensor_id_accept(const string &text)
586 const set<Track *> &tracks = selection.get_tracks();
587 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
588 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
590 (*i)->set_sensor_id(id);
592 update_track_icon(layout_3d->get_track(**i));
596 void Designer::route_name_accept(const string &text)
599 cur_route->set_name(text);
602 string Designer::tooltip(int x, int y)
604 if(Track3D *t3d = pick_track(x, y))
606 const Track &track = t3d->get_track();
607 const TrackType &ttype = track.get_type();
608 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
609 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
610 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
611 if(track.get_turnout_id())
612 info += format(" (turnout %d)", track.get_turnout_id());
613 else if(track.get_sensor_id())
614 info += format(" (sensor %d)", track.get_sensor_id());
622 void Designer::show_route(const Route *route)
624 const set<Track *> <racks = layout->get_tracks();
625 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
627 Track3D &t3d = layout_3d->get_track(**i);
628 if(route && route->has_track(**i))
630 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
631 if((*i)->get_type().is_turnout())
633 unsigned tid = (*i)->get_turnout_id();
634 int path = (tid ? route->get_turnout(tid) : -1);
636 t3d.get_path().set_path(path);
638 t3d.get_path().set_mask((*i)->get_type().get_paths());
641 t3d.get_path().set_path(0);
644 t3d.get_path().set_mask(0);