3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
11 #include <msp/gl/blend.h>
12 #include <msp/gl/matrix.h>
13 #include <msp/gl/misc.h>
14 #include <msp/gl/projection.h>
15 #include <msp/gl/rendermode.h>
16 #include <msp/gl/select.h>
17 #include <msp/gl/tests.h>
18 #include <msp/gl/texture2d.h>
19 #include <msp/input/keys.h>
20 #include <msp/io/print.h>
21 #include <msp/strings/codec.h>
22 #include <msp/strings/lexicalcast.h>
23 #include <msp/strings/utf8.h>
24 #include <msp/strings/utils.h>
25 #include <msp/time/units.h>
26 #include <msp/time/utils.h>
27 #include "libmarklin/tracktype.h"
30 #include "manipulator.h"
32 #include "selection.h"
36 using namespace Marklin;
39 Application::RegApp<Designer> Designer::reg;
41 Designer::Designer(int argc, char **argv):
46 manipulator(*this, selection),
51 cam_pos(0, -0.5, 0.5),
59 window.set_title("Railway Designer");
60 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
61 window.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
62 window.signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release));
63 window.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
64 window.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
66 manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
67 manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
68 measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
69 measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
71 // Setup catalogue and layout
72 DataFile::load(catalogue, "tracks.dat");
74 cat_layout_3d = new Layout3D(catalogue.get_layout());
76 layout = new Layout(catalogue);
77 layout_3d = new Layout3D(*layout);
82 DataFile::load(*layout, argv[1]);
83 const list<Track3D *> <racks = layout_3d->get_tracks();
84 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
85 update_track_color(**i);
87 if(!layout->get_base().empty())
89 base_object = new GL::Object;
90 DataFile::load(*base_object, layout->get_base());
95 GL::enable(GL::DEPTH_TEST);
96 GL::enable(GL::BLEND);
97 GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
98 GL::enable(GL_CULL_FACE);
100 pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
101 pipeline->add_renderable(layout_3d->get_scene());
103 pipeline->add_renderable(*base_object);
105 light.set_position(0, -0.259, 0.966, 0);
106 lighting.attach(0, light);
108 GL::PipelinePass *pass = &pipeline->add_pass(GL::Tag());
109 pass->lighting = &lighting;
112 DataFile::load(ui_res, "marklin.res");
113 root = new GLtk::Root(ui_res, window);
115 lbl_tooltip = new GLtk::Label(ui_res);
116 lbl_tooltip->set_style("tooltip");
117 root->add(*lbl_tooltip);
118 lbl_tooltip->set_visible(false);
120 toolbar = new Toolbar(*this);
122 toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
123 toolbar->set_visible(true);
126 Designer::~Designer()
131 delete cat_layout_3d;
140 return Application::main();
143 void Designer::save()
145 input = new ::Input(*this, "Save layout", filename);
146 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
147 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
151 void Designer::quit()
156 void Designer::edit_route(Route &r)
161 void Designer::add_selection_to_route()
168 const set<Track *> &stracks = selection.get_tracks();
169 set<const Track *> tracks(stracks.begin(), stracks.end());
170 cur_route->add_tracks(tracks);
172 catch(const Exception &e)
174 IO::print("%s\n", e.what());
178 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
180 float cos_pitch = cos(cam_pitch);
181 float sin_pitch = sin(cam_pitch);
182 float cos_yaw = cos(cam_yaw);
183 float sin_yaw = sin(cam_yaw);
185 float rx = sin_yaw*0.55228;
186 float ry = -cos_yaw*0.55228;
188 float ux = cos_yaw*-sin_pitch*0.41421;
189 float uy = sin_yaw*-sin_pitch*0.41421;
190 float uz = cos_pitch*0.41421;
192 float xf = static_cast<float>(x)*2/window.get_width()-1;
193 float yf = static_cast<float>(y)*2/window.get_height()-1;
195 float vx = cos_yaw*cos_pitch + xf*rx + yf*ux;
196 float vy = sin_yaw*cos_pitch + xf*ry + yf*uy;
197 float vz = sin_pitch + yf*uz;
199 gx = cam_pos.x-vx*cam_pos.z/vz;
200 gy = cam_pos.y-vy*cam_pos.z/vz;
203 void Designer::tick()
205 const Msp::Time::TimeStamp t = Msp::Time::now();
206 float dt = (t-last_tick)/Msp::Time::sec;
209 window.get_display().tick();
213 cam_pos.x += cos(cam_yaw)*dt*move_y;
214 cam_pos.y += sin(cam_yaw)*dt*move_y;
218 cam_pos.x += sin(cam_yaw)*dt*move_x;
219 cam_pos.y += -cos(cam_yaw)*dt*move_x;
223 cam_pos.x += cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
224 cam_pos.y += sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
225 cam_pos.z += sin(cam_pitch)*dt*zoom;
229 float vx = cos(cam_yaw)*cos(cam_pitch);
230 float vy = sin(cam_yaw)*cos(cam_pitch);
231 float vz = sin(cam_pitch);
233 float gx = cam_pos.x-vx*cam_pos.z/vz;
234 float gy = cam_pos.y-vy*cam_pos.z/vz;
235 float d = sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
237 cam_yaw += M_PI*dt*rotate;
243 cam_pos.x = gx+cos(cam_yaw)*d;
244 cam_pos.y = gy+sin(cam_yaw)*d;
248 cam_pitch += M_PI/2*dt*pitch;
249 if(cam_pitch>M_PI/12)
251 else if(cam_pitch<-M_PI/2)
255 if(tooltip_timeout && t>tooltip_timeout)
260 t3d = pick_track(pointer_x, pointer_y);
262 t3d = pick_track(pointer_x, pointer_y);
266 const Track &track = t3d->get_track();
267 const TrackType &ttype = track.get_type();
268 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
269 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
270 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
271 if(track.get_turnout_id())
272 info += format(" (turnout %d)", track.get_turnout_id());
273 else if(track.get_sensor_id())
274 info += format(" (sensor %d)", track.get_sensor_id());
276 set_tooltip(pointer_x, pointer_y, info);
281 tooltip_timeout = Msp::Time::TimeStamp();
286 window.swap_buffers();
289 void Designer::key_press(unsigned code, unsigned mod, wchar_t)
291 unsigned key = Msp::Input::key_from_sys(code);
296 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
299 if(key==Msp::Input::KEY_N)
301 else if(key==Msp::Input::KEY_G)
303 manipulator.start_move();
306 else if(key==Msp::Input::KEY_R)
308 manipulator.start_rotate();
311 else if(key==Msp::Input::KEY_D)
313 manipulator.duplicate();
314 manipulator.start_move();
317 else if(key==Msp::Input::KEY_W)
319 else if(key==Msp::Input::KEY_PLUS)
320 selection.select_more();
321 else if(key==Msp::Input::KEY_L && (mod&1))
323 const set<Track *> &tracks = layout->get_tracks();
325 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
326 len += (*i)->get_type().get_total_length();
327 IO::print("Total length: %.1fm\n", len);
329 else if(key==Msp::Input::KEY_L)
330 selection.select_linked();
331 else if(key==Msp::Input::KEY_M)
336 else if(key==Msp::Input::KEY_Z)
338 manipulator.start_elevate();
341 else if(key==Msp::Input::KEY_ESC)
344 manipulator.cancel();
345 else if(mode==CATALOGUE)
350 else if(key==Msp::Input::KEY_X)
352 set<Track *> tracks = selection.get_tracks();
354 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
356 layout->remove_track(**i);
360 else if(key==Msp::Input::KEY_F && (mod&1))
362 const set<Track *> &tracks = selection.get_tracks();
363 const set<Track *> <racks = layout->get_tracks();
364 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
366 (*i)->set_flex(!(*i)->get_flex());
368 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
370 (*i)->snap_to(**j, true);
372 update_track_color(layout_3d->get_track(**i));
375 else if(key==Msp::Input::KEY_F)
376 manipulator.flatten();
377 else if(key==Msp::Input::KEY_E && (mod&1))
378 manipulator.even_slope(true);
379 else if(key==Msp::Input::KEY_E)
380 manipulator.even_slope();
381 else if(key==Msp::Input::KEY_T)
383 Track *track = selection.get_track();
384 if(selection.size()==1 && track->get_type().get_n_paths()>1)
386 input = new ::Input(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
387 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
388 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
392 else if(key==Msp::Input::KEY_S)
394 const set<Track *> &tracks = selection.get_tracks();
397 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
399 if((*i)->get_type().get_n_paths()==1)
401 if(static_cast<int>((*i)->get_sensor_id())!=id)
404 id = (*i)->get_sensor_id();
411 input = new ::Input(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
412 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
413 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
417 else if(key==Msp::Input::KEY_A)
418 add_selection_to_route();
419 else if(key==Msp::Input::KEY_RIGHT)
421 else if(key==Msp::Input::KEY_LEFT)
423 else if(key==Msp::Input::KEY_UP)
425 else if(key==Msp::Input::KEY_DOWN)
427 else if(key==Msp::Input::KEY_INSERT)
429 else if(key==Msp::Input::KEY_PGUP)
431 else if(key==Msp::Input::KEY_HOME)
433 else if(key==Msp::Input::KEY_END)
435 else if(key==Msp::Input::KEY_DELETE)
437 else if(key==Msp::Input::KEY_PGDN)
441 void Designer::key_release(unsigned code, unsigned)
443 unsigned key = Msp::Input::key_from_sys(code);
448 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
450 else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
452 else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
454 else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
456 else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
458 else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)
462 void Designer::button_press(int x, int y, unsigned btn, unsigned)
464 y = window.get_height()-y-1;
467 map_pointer_coords(x, y, gx, gy);
473 Track3D *ctrack = pick_track(x, y);
476 Track *track = ctrack->get_track().copy();
477 track->set_position(Point(gx, gy, 0));
478 layout->add_track(*track);
481 selection.add_track(track);
489 else if(mode==SELECT)
493 Track3D *track = pick_track(x, y);
498 selection.toggle_track(&track->get_track());
502 else if(mode==MANIPULATE)
503 manipulator.button_press(x, y, gx, gy, btn);
504 else if(mode==MEASURE)
505 measure.button_press(x, y, gx, gy, btn);
508 void Designer::pointer_motion(int x, int y)
510 y = window.get_height()-y-1;
515 if(mode==SELECT || mode==CATALOGUE)
516 tooltip_timeout = Msp::Time::now()+100*Msp::Time::msec;
523 map_pointer_coords(x, y, gx, gy);
524 manipulator.pointer_motion(x, y, gx, gy);
525 measure.pointer_motion(x, y, gx, gy);
529 void Designer::project_3d()
531 glMatrixMode(GL_PROJECTION);
533 glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
534 glMatrixMode(GL_MODELVIEW);
537 void Designer::apply_camera()
541 glTranslatef(0, 0, -1);
544 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
545 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
546 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
550 void Designer::render()
552 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
553 GL::enable(GL::DEPTH_TEST);
554 GL::Texture::unbind();
559 cat_layout_3d->get_scene().render();
562 pipeline->render_all();
563 layout_3d->get_endpoint_scene().render();
564 GL::enable(GL_CULL_FACE);
567 glColor4f(0.5, 0.8, 1.0, 1.0);
568 const set<const Track *> &rtracks = cur_route->get_tracks();
569 const map<unsigned, int> &turnouts = cur_route->get_turnouts();
570 for(set<const Track *>::const_iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
573 if(unsigned tid=(*i)->get_turnout_id())
575 map<unsigned, int>::const_iterator j = turnouts.find(tid);
576 if(j!=turnouts.end())
579 layout_3d->get_track(**i).render_path(path);
583 manipulator.render();
588 GL::matrix_mode(GL::PROJECTION);
590 GL::ortho_bottomleft(window.get_width(), window.get_height());
591 GL::matrix_mode(GL::MODELVIEW);
594 GL::disable(GL::DEPTH_TEST);
599 Track3D *Designer::pick_track(int x, int y)
601 Layout3D *l = layout_3d;
605 float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
606 float yy = (float(y)/window.get_height()-0.5)*0.82843;
607 float size = 4.0/window.get_height()*0.82843;
612 return l->pick_track(xx, yy, size);
615 void Designer::update_track_color(Track3D &track)
617 if(track.get_track().get_sensor_id())
619 if(track.get_track().get_flex())
620 track.set_color(GL::Color(1, 0.6, 1));
622 track.set_color(GL::Color(0.7, 0.7, 1));
624 else if(track.get_track().get_turnout_id())
625 track.set_color(GL::Color(0.8, 1, 0.8));
626 else if(track.get_track().get_flex())
627 track.set_color(GL::Color(1, 0.8, 0.8));
629 track.set_color(GL::Color(1, 1, 1));
632 void Designer::manipulation_status(const string &status)
634 set_tooltip(pointer_x, pointer_y, status);
637 void Designer::manipulation_done(bool)
642 void Designer::measure_changed()
644 float pard = measure.get_parallel_distance()*1000;
645 float perpd = measure.get_perpendicular_distance()*1000;
646 float d = sqrt(pard*pard+perpd*perpd);
647 float adiff = measure.get_angle_difference()*180/M_PI;
648 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
649 set_tooltip(pointer_x, pointer_y, info);
652 void Designer::measure_done()
657 void Designer::set_tooltip(int x, int y, const std::string &text)
659 int fontsize = ui_res.get_default_font().get_default_size();
661 int w = static_cast<int>(ui_res.get_default_font().get_string_width(text)*fontsize)+6;
662 x = max(min(static_cast<int>(window.get_width())-w, x), 0);
663 y = max(min(static_cast<int>(window.get_height())-h, y), 0);
664 lbl_tooltip->set_text(text);
665 lbl_tooltip->set_geometry(GLtk::Geometry(x, y, w, h));
666 lbl_tooltip->set_visible(true);
669 void Designer::clear_tooltip()
671 lbl_tooltip->set_visible(false);
674 void Designer::save_accept()
676 layout->save(input->get_text());
681 void Designer::turnout_id_accept()
683 Track *track = selection.get_track();
684 unsigned id = lexical_cast<unsigned>(input->get_text());
685 track->set_turnout_id(id);
687 update_track_color(layout_3d->get_track(*track));
692 void Designer::sensor_id_accept()
694 const set<Track *> &tracks = selection.get_tracks();
695 unsigned id = lexical_cast<unsigned>(input->get_text());
696 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
698 (*i)->set_sensor_id(id);
700 update_track_color(layout_3d->get_track(**i));
706 void Designer::input_dismiss()