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):
50 manipulator(*this, selection),
52 track_wrap(*this, selection)
54 window.set_title("Railway Designer");
55 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
57 selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
58 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
59 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
60 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
61 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
63 // Setup catalogue and layout
64 DataFile::load(catalogue, "tracks.dat");
66 cat_layout_3d = new Layout3D(catalogue.get_layout());
68 layout = new Layout(catalogue);
69 layout_3d = new Layout3D(*layout);
71 layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
72 layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
77 DataFile::load(*layout, argv[1]);
79 if(!layout->get_base().empty())
81 base_object = new GL::Object;
82 DataFile::load(*base_object, layout->get_base());
87 GL::enable(GL_CULL_FACE);
89 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
90 pipeline->set_camera(&camera);
91 pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
93 pipeline->add_renderable(*base_object);
94 pipeline->add_renderable_for_pass(track_wrap, "unlit");
95 pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
96 pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
98 light.set_position(0, -0.259, 0.966, 0);
99 lighting.attach(0, light);
101 GL::PipelinePass *pass = &pipeline->add_pass(0);
102 pass->lighting = &lighting;
103 pass->depth_test = &GL::DepthTest::lequal();
105 pass = &pipeline->add_pass("unlit");
106 pass->depth_test = &GL::DepthTest::lequal();
107 pass->blend = &GL::Blend::alpha();
109 pass = &pipeline->add_pass("blended");
110 pass->lighting = &lighting;
111 pass->depth_test = &GL::DepthTest::lequal();
112 pass->blend = &GL::Blend::alpha();
114 camera.set_up_direction(GL::Vector3(0, 0, 1));
115 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
118 DataFile::load(ui_res, "marklin.res");
119 root = new GLtk::Root(ui_res, window);
120 root->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
121 root->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
122 root->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
123 root->signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
125 toolbar = new Toolbar(*this);
127 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
128 toolbar->set_focusable(false);
130 GLtk::Panel *statusbar = new GLtk::Panel(ui_res);
131 root->add(*statusbar);
132 statusbar->set_size(window.get_width(), 20);
133 statusbar->set_focusable(false);
135 lbl_status = new GLtk::Label(ui_res);
136 statusbar->add(*lbl_status);
137 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
139 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
141 camera_ctl = new CameraController(*this, *root, camera);
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()
158 delete cat_layout_3d;
167 return Application::main();
170 void Designer::save()
172 InputDialog *input = new InputDialog(*this, "Save layout", filename);
173 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
176 void Designer::quit()
181 void Designer::new_track()
186 void Designer::set_turnout_id()
188 Track *track = selection.get_track();
189 if(selection.size()==1 && track->get_type().is_turnout())
191 InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
192 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
196 void Designer::set_sensor_id()
198 const set<Track *> &tracks = selection.get_tracks();
201 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
203 if(!(*i)->get_type().is_turnout())
205 if(static_cast<int>((*i)->get_sensor_id())!=id)
208 id = (*i)->get_sensor_id();
216 InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
217 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
221 void Designer::rename_route()
226 InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
227 input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
230 void Designer::edit_route(Route *r)
236 void Designer::add_selection_to_route()
243 cur_route->add_tracks(selection.get_tracks());
245 catch(const Exception &e)
247 lbl_status->set_text(e.what());
250 show_route(cur_route);
253 Point Designer::map_pointer_coords(int x, int y)
255 float xf = x*2.0/window.get_width()-1.0;
256 float yf = y*2.0/window.get_height()-1.0;
257 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
258 const GL::Vector3 &pos = camera.get_position();
260 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
263 void Designer::tick()
265 const Msp::Time::TimeStamp t = Msp::Time::now();
266 float dt = (t-last_tick)/Msp::Time::sec;
269 window.get_display().tick();
271 camera_ctl->tick(dt);
273 for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
274 layout_3d->get_track(**i).get_path().set_mask(0);
279 window.swap_buffers();
282 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
284 key = Input::key_from_sys(key);
285 mod = Input::mod_from_sys(mod);
287 if(key==Msp::Input::KEY_N && (mod&Input::MOD_SHIFT))
289 manipulator.start_extend();
292 else if(key==Msp::Input::KEY_N)
294 else if(key==Msp::Input::KEY_G)
296 manipulator.start_move();
299 else if(key==Msp::Input::KEY_R)
301 manipulator.start_rotate();
304 else if(key==Msp::Input::KEY_D)
306 manipulator.duplicate();
307 manipulator.start_move();
310 else if(key==Msp::Input::KEY_W)
312 else if(key==Msp::Input::KEY_PLUS)
313 selection.select_more();
314 else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
315 selection.select_blocks();
316 else if(key==Msp::Input::KEY_L)
317 selection.select_linked();
318 else if(key==Msp::Input::KEY_M)
323 else if(key==Msp::Input::KEY_Z)
325 manipulator.start_elevate();
328 else if(key==Msp::Input::KEY_ESC)
331 manipulator.cancel();
332 else if(mode==CATALOGUE)
337 else if(key==Msp::Input::KEY_X)
339 set<Track *> tracks = selection.get_tracks();
341 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
343 overlay->clear(layout_3d->get_track(**i));
344 layout->remove_track(**i);
348 else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
350 const set<Track *> &tracks = selection.get_tracks();
351 const set<Track *> <racks = layout->get_tracks();
352 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
354 (*i)->set_flex(!(*i)->get_flex());
356 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
358 (*i)->snap_to(**j, true);
360 update_track_icon(layout_3d->get_track(**i));
363 else if(key==Msp::Input::KEY_F)
364 manipulator.flatten();
365 else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
366 manipulator.even_slope(true);
367 else if(key==Msp::Input::KEY_E)
368 manipulator.even_slope();
369 else if(key==Msp::Input::KEY_T)
371 else if(key==Msp::Input::KEY_S)
373 else if(key==Msp::Input::KEY_A)
374 add_selection_to_route();
375 else if(key==Msp::Input::KEY_C)
376 manipulator.connect();
379 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
381 y = window.get_height()-y-1;
382 mod = Input::mod_from_sys(mod);
384 Point ground = map_pointer_coords(x, y);
390 Track3D *ctrack = pick_track(x, y);
393 Track *track = new Track(*layout, ctrack->get_track().get_type());
394 track->set_position(ground);
397 selection.add_track(track);
405 else if(mode==SELECT)
409 Track3D *track = pick_track(x, y);
412 if(!(mod&Input::MOD_SHIFT))
414 selection.toggle_track(&track->get_track());
418 else if(mode==MANIPULATE)
419 manipulator.button_press(x, y, ground.x, ground.y, btn);
420 else if(mode==MEASURE)
421 measure.button_press(x, y, ground.x, ground.y, btn);
424 void Designer::pointer_motion(int x, int y)
426 y = window.get_height()-y-1;
428 if(!root->get_child_at(x, y))
430 Point ground = map_pointer_coords(x, y);
431 manipulator.pointer_motion(x, y, ground.x, ground.y);
432 measure.pointer_motion(x, y, ground.x, ground.y);
436 void Designer::apply_camera()
440 GL::matrix_mode(GL::PROJECTION);
442 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
443 GL::matrix_mode(GL::MODELVIEW);
445 GL::translate(0, 0, -1);
451 void Designer::render()
453 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
458 cat_layout_3d->get_scene().render();
462 pipeline->render_all();
463 GL::enable(GL_CULL_FACE);
465 GL::Bind bind_blend(GL::Blend::alpha());
469 GL::Bind bind_depth(GL::DepthTest::lequal());
474 GL::matrix_mode(GL::PROJECTION);
476 GL::ortho_bottomleft(window.get_width(), window.get_height());
477 GL::matrix_mode(GL::MODELVIEW);
480 GL::disable(GL::DEPTH_TEST);
482 GL::Bind bind_blend(GL::Blend::alpha());
484 GL::Texture::unbind();
487 void Designer::track_added(Track &trk)
489 new_tracks.push_back(&trk);
492 void Designer::track_removed(Track &trk)
494 list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
495 if(i!=new_tracks.end())
499 Track3D *Designer::pick_track(int x, int y)
501 Layout3D *l = layout_3d;
505 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
506 float yy = (float(y)/window.get_height()-0.5)*0.82843;
507 float size = 4.0/window.get_height()*0.82843;
511 return l->pick_track(xx, yy, size);
514 void Designer::update_track_icon(Track3D &track)
516 overlay->clear(track);
518 if(track.get_track().get_flex())
519 overlay->add_graphic(track, "flex");
521 if(unsigned sid = track.get_track().get_sensor_id())
523 overlay->add_graphic(track, "sensor");
524 overlay->set_label(track, lexical_cast(sid));
526 else if(unsigned tid = track.get_track().get_turnout_id())
530 overlay->add_graphic(track, "turnout");
531 overlay->set_label(track, lexical_cast(tid));
536 void Designer::selection_changed()
538 const set<Track *> &tracks = selection.get_tracks();
540 lbl_status->set_text(string());
544 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
545 len += (*i)->get_type().get_total_length();
546 lbl_status->set_text(format("%.2fm of track selected\n", len));
550 void Designer::manipulation_status(const string &status)
552 lbl_status->set_text(status);
555 void Designer::manipulation_done(bool)
561 void Designer::measure_changed()
563 float pard = measure.get_parallel_distance()*1000;
564 float perpd = measure.get_perpendicular_distance()*1000;
565 float d = sqrt(pard*pard+perpd*perpd);
566 float adiff = measure.get_angle_difference()*180/M_PI;
567 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
568 lbl_status->set_text(info);
571 void Designer::measure_done()
577 void Designer::turnout_id_accept(const string &text)
579 Track *track = selection.get_track();
580 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
581 track->set_turnout_id(id);
583 update_track_icon(layout_3d->get_track(*track));
586 void Designer::sensor_id_accept(const string &text)
588 const set<Track *> &tracks = selection.get_tracks();
589 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
590 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
592 (*i)->set_sensor_id(id);
594 update_track_icon(layout_3d->get_track(**i));
598 void Designer::route_name_accept(const string &text)
601 cur_route->set_name(text);
604 string Designer::tooltip(int x, int y)
606 if(Track3D *t3d = pick_track(x, y))
608 const Track &track = t3d->get_track();
609 const TrackType &ttype = track.get_type();
610 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
611 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
612 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
613 if(track.get_turnout_id())
614 info += format(" (turnout %d)", track.get_turnout_id());
615 else if(track.get_sensor_id())
616 info += format(" (sensor %d)", track.get_sensor_id());
624 void Designer::show_route(const Route *route)
626 const set<Track *> <racks = layout->get_tracks();
627 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
629 Track3D &t3d = layout_3d->get_track(**i);
630 if(route && route->has_track(**i))
632 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
633 if((*i)->get_type().is_turnout())
635 unsigned tid = (*i)->get_turnout_id();
636 int path = (tid ? route->get_turnout(tid) : -1);
638 t3d.get_path().set_path(path);
640 t3d.get_path().set_mask((*i)->get_type().get_paths());
643 t3d.get_path().set_path(0);
646 t3d.get_path().set_mask(0);