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),
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));
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_CULL_FACE);
88 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
89 pipeline->set_camera(&camera);
90 pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
92 pipeline->add_renderable(*base_object);
93 pipeline->add_renderable_for_pass(track_wrap, "unlit");
94 pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
95 pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
97 light.set_position(0, -0.259, 0.966, 0);
98 lighting.attach(0, light);
100 GL::PipelinePass *pass = &pipeline->add_pass(0);
101 pass->lighting = &lighting;
102 pass->depth_test = &GL::DepthTest::lequal();
104 pass = &pipeline->add_pass("unlit");
105 pass->depth_test = &GL::DepthTest::lequal();
106 pass->blend = &GL::Blend::alpha();
108 pass = &pipeline->add_pass("blended");
109 pass->lighting = &lighting;
110 pass->depth_test = &GL::DepthTest::lequal();
111 pass->blend = &GL::Blend::alpha();
113 camera.set_up_direction(GL::Vector3(0, 0, 1));
117 DataFile::load(ui_res, "marklin.res");
118 root = new GLtk::Root(ui_res, window);
119 root->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
120 root->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
121 root->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
122 root->signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
124 toolbar = new Toolbar(*this);
126 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
127 toolbar->set_focusable(false);
129 GLtk::Panel *statusbar = new GLtk::Panel(ui_res);
130 root->add(*statusbar);
131 statusbar->set_size(window.get_width(), 20);
132 statusbar->set_focusable(false);
134 lbl_status = new GLtk::Label(ui_res);
135 statusbar->add(*lbl_status);
136 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
138 overlay = new Overlay3D(window, camera, ui_res.get_default_font());
140 const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
141 for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
142 update_track_icon(*i->second);
145 Designer::~Designer()
153 delete cat_layout_3d;
162 return Application::main();
165 void Designer::save()
167 InputDialog *input = new InputDialog(*this, "Save layout", filename);
168 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
171 void Designer::quit()
176 void Designer::new_track()
181 void Designer::set_turnout_id()
183 Track *track = selection.get_track();
184 if(selection.size()==1 && track->get_type().is_turnout())
186 InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
187 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
191 void Designer::set_sensor_id()
193 const set<Track *> &tracks = selection.get_tracks();
196 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
198 if(!(*i)->get_type().is_turnout())
200 if(static_cast<int>((*i)->get_sensor_id())!=id)
203 id = (*i)->get_sensor_id();
211 InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
212 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
216 void Designer::rename_route()
221 InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
222 input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
225 void Designer::edit_route(Route *r)
231 void Designer::add_selection_to_route()
238 cur_route->add_tracks(selection.get_tracks());
240 catch(const Exception &e)
242 lbl_status->set_text(e.what());
245 show_route(cur_route);
248 Point Designer::map_pointer_coords(int x, int y)
250 float xf = x*2.0/window.get_width()-1.0;
251 float yf = y*2.0/window.get_height()-1.0;
252 GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
253 const GL::Vector3 &pos = camera.get_position();
255 return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
258 void Designer::tick()
260 const Msp::Time::TimeStamp t = Msp::Time::now();
261 float dt = (t-last_tick)/Msp::Time::sec;
264 window.get_display().tick();
268 for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
269 layout_3d->get_track(**i).get_path().set_mask(0);
274 window.swap_buffers();
277 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
279 key = Input::key_from_sys(key);
280 mod = Input::mod_from_sys(mod);
282 if(key==Msp::Input::KEY_N)
284 else if(key==Msp::Input::KEY_G)
286 manipulator.start_move();
289 else if(key==Msp::Input::KEY_R)
291 manipulator.start_rotate();
294 else if(key==Msp::Input::KEY_D)
296 manipulator.duplicate();
297 manipulator.start_move();
300 else if(key==Msp::Input::KEY_W)
302 else if(key==Msp::Input::KEY_PLUS)
303 selection.select_more();
304 else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
305 selection.select_blocks();
306 else if(key==Msp::Input::KEY_L)
307 selection.select_linked();
308 else if(key==Msp::Input::KEY_M)
313 else if(key==Msp::Input::KEY_Z)
315 manipulator.start_elevate();
318 else if(key==Msp::Input::KEY_ESC)
321 manipulator.cancel();
322 else if(mode==CATALOGUE)
327 else if(key==Msp::Input::KEY_X)
329 set<Track *> tracks = selection.get_tracks();
331 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
333 overlay->clear(layout_3d->get_track(**i));
334 layout->remove_track(**i);
338 else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
340 const set<Track *> &tracks = selection.get_tracks();
341 const set<Track *> <racks = layout->get_tracks();
342 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
344 (*i)->set_flex(!(*i)->get_flex());
346 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
348 (*i)->snap_to(**j, true);
350 update_track_icon(layout_3d->get_track(**i));
353 else if(key==Msp::Input::KEY_F)
354 manipulator.flatten();
355 else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
356 manipulator.even_slope(true);
357 else if(key==Msp::Input::KEY_E)
358 manipulator.even_slope();
359 else if(key==Msp::Input::KEY_T)
361 else if(key==Msp::Input::KEY_S)
363 else if(key==Msp::Input::KEY_A)
364 add_selection_to_route();
367 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
369 y = window.get_height()-y-1;
370 mod = Input::mod_from_sys(mod);
372 Point ground = map_pointer_coords(x, y);
378 Track3D *ctrack = pick_track(x, y);
381 Track *track = new Track(*layout, ctrack->get_track().get_type());
382 track->set_position(ground);
385 selection.add_track(track);
393 else if(mode==SELECT)
397 Track3D *track = pick_track(x, y);
400 if(!(mod&Input::MOD_SHIFT))
402 selection.toggle_track(&track->get_track());
406 else if(mode==MANIPULATE)
407 manipulator.button_press(x, y, ground.x, ground.y, btn);
408 else if(mode==MEASURE)
409 measure.button_press(x, y, ground.x, ground.y, btn);
412 void Designer::pointer_motion(int x, int y)
414 y = window.get_height()-y-1;
416 if(!root->get_child_at(x, y))
418 Point ground = map_pointer_coords(x, y);
419 manipulator.pointer_motion(x, y, ground.x, ground.y);
420 measure.pointer_motion(x, y, ground.x, ground.y);
424 void Designer::apply_camera()
428 GL::matrix_mode(GL::PROJECTION);
430 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
431 GL::matrix_mode(GL::MODELVIEW);
433 GL::translate(0, 0, -1);
439 void Designer::render()
441 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
446 cat_layout_3d->get_scene().render();
450 pipeline->render_all();
451 GL::enable(GL_CULL_FACE);
453 GL::Bind bind_blend(GL::Blend::alpha());
457 GL::Bind bind_depth(GL::DepthTest::lequal());
462 GL::matrix_mode(GL::PROJECTION);
464 GL::ortho_bottomleft(window.get_width(), window.get_height());
465 GL::matrix_mode(GL::MODELVIEW);
468 GL::disable(GL::DEPTH_TEST);
470 GL::Bind bind_blend(GL::Blend::alpha());
472 GL::Texture::unbind();
475 void Designer::track_added(Track &trk)
477 new_tracks.push_back(&trk);
480 Track3D *Designer::pick_track(int x, int y)
482 Layout3D *l = layout_3d;
486 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
487 float yy = (float(y)/window.get_height()-0.5)*0.82843;
488 float size = 4.0/window.get_height()*0.82843;
492 return l->pick_track(xx, yy, size);
495 void Designer::update_track_icon(Track3D &track)
497 overlay->clear(track);
499 if(track.get_track().get_flex())
500 overlay->add_graphic(track, "flex");
502 if(unsigned sid = track.get_track().get_sensor_id())
504 overlay->add_graphic(track, "sensor");
505 overlay->set_label(track, lexical_cast(sid));
507 else if(unsigned tid = track.get_track().get_turnout_id())
511 overlay->add_graphic(track, "turnout");
512 overlay->set_label(track, lexical_cast(tid));
517 void Designer::selection_changed()
519 const set<Track *> &tracks = selection.get_tracks();
521 lbl_status->set_text(string());
525 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
526 len += (*i)->get_type().get_total_length();
527 lbl_status->set_text(format("%.2fm of track selected\n", len));
531 void Designer::manipulation_status(const string &status)
533 lbl_status->set_text(status);
536 void Designer::manipulation_done(bool)
542 void Designer::measure_changed()
544 float pard = measure.get_parallel_distance()*1000;
545 float perpd = measure.get_perpendicular_distance()*1000;
546 float d = sqrt(pard*pard+perpd*perpd);
547 float adiff = measure.get_angle_difference()*180/M_PI;
548 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
549 lbl_status->set_text(info);
552 void Designer::measure_done()
558 void Designer::turnout_id_accept(const string &text)
560 Track *track = selection.get_track();
561 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
562 track->set_turnout_id(id);
564 update_track_icon(layout_3d->get_track(*track));
567 void Designer::sensor_id_accept(const string &text)
569 const set<Track *> &tracks = selection.get_tracks();
570 unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
571 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
573 (*i)->set_sensor_id(id);
575 update_track_icon(layout_3d->get_track(**i));
579 void Designer::route_name_accept(const string &text)
582 cur_route->set_name(text);
585 void Designer::view_all()
590 const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
591 for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
595 i->second->get_bounds(0, tmin, tmax);
596 minp.x = min(minp.x, tmin.x);
597 minp.y = min(minp.y, tmin.y);
598 maxp.x = max(maxp.x, tmax.x);
599 maxp.y = max(maxp.y, tmax.y);
602 float t = tan(camera.get_field_of_view()/2)*2;
603 float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
604 float cam_dist = size/t+size*0.25;
605 Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2);
606 camera.set_position(GL::Vector3(center.x, center.y-cam_dist*0.5, cam_dist*0.866));
607 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
610 string Designer::tooltip(int x, int y)
612 if(Track3D *t3d = pick_track(x, y))
614 const Track &track = t3d->get_track();
615 const TrackType &ttype = track.get_type();
616 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
617 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
618 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
619 if(track.get_turnout_id())
620 info += format(" (turnout %d)", track.get_turnout_id());
621 else if(track.get_sensor_id())
622 info += format(" (sensor %d)", track.get_sensor_id());
630 void Designer::show_route(const Route *route)
632 const set<Track *> <racks = layout->get_tracks();
633 for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
635 Track3D &t3d = layout_3d->get_track(**i);
636 if(route && route->has_track(**i))
638 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
639 if((*i)->get_type().is_turnout())
641 unsigned tid = (*i)->get_turnout_id();
642 int path = (tid ? route->get_turnout(tid) : -1);
644 t3d.get_path().set_path(path);
646 t3d.get_path().set_mask((*i)->get_type().get_paths());
649 t3d.get_path().set_path(0);
652 t3d.get_path().set_mask(0);