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/matrix.h>
12 #include <msp/gl/misc.h>
13 #include <msp/gl/projection.h>
14 #include <msp/gl/rendermode.h>
15 #include <msp/gl/select.h>
16 #include <msp/gl/tests.h>
17 #include <msp/gl/texture2d.h>
18 #include <msp/input/keys.h>
19 #include <msp/io/print.h>
20 #include <msp/strings/codec.h>
21 #include <msp/strings/lexicalcast.h>
22 #include <msp/strings/utf8.h>
23 #include <msp/strings/utils.h>
24 #include <msp/time/units.h>
25 #include <msp/time/utils.h>
26 #include "libmarklin/tracktype.h"
29 #include "manipulator.h"
31 #include "selection.h"
35 using namespace Marklin;
38 Application::RegApp<Designer> Designer::reg;
40 Designer::Designer(int argc, char **argv):
49 cam_pos(0, -0.5, 0.5),
57 DataFile::load(catalogue, "tracks.dat");
59 cat_layout = new Layout(catalogue);
60 cat_layout_3d = new Layout3D(*cat_layout);
62 const map<unsigned, TrackType *> &ctracks = catalogue.get_tracks();
64 for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
66 Track *track = new Track(*i->second);
67 track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
68 track->set_rotation(M_PI/2);
69 cat_layout->add_track(*track);
72 manipulator = new Manipulator(*this);
73 manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
74 manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
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());
94 selection = new Selection;
95 manipulator->set_selection(selection);
97 measure = new Measure(*this);
98 measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
99 measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
102 Designer::~Designer()
109 delete cat_layout_3d;
115 dpy = new Graphics::Display;
116 wnd = new Graphics::Window(*dpy, screen_w, screen_h);
117 glc = new Graphics::GLContext(*wnd);
118 wnd->set_title("Railway Designer");
120 wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
123 glEnableClientState(GL_VERTEX_ARRAY);
124 glEnable(GL_DEPTH_TEST);
126 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
127 glEnable(GL_CULL_FACE);
129 pipeline = new GL::Pipeline(screen_w, screen_h, false);
130 pipeline->add_renderable(layout_3d->get_scene());
132 pipeline->add_renderable(*base_object);
134 light.set_position(0, -0.259, 0.966, 0);
135 lighting.attach(0, light);
137 GL::PipelinePass *pass = &pipeline->add_pass(GL::Tag());
138 pass->lighting = &lighting;
140 DataFile::load(ui_res, "marklin.res");
141 root = new GLtk::Root(ui_res, *wnd);
143 lbl_tooltip = new GLtk::Label(ui_res);
144 lbl_tooltip->set_style("tooltip");
145 root->add(*lbl_tooltip);
146 lbl_tooltip->set_visible(false);
148 toolbar = new Toolbar(*this);
150 toolbar->set_position(0, screen_h-toolbar->get_geometry().h);
151 toolbar->set_visible(true);
154 wnd->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
155 wnd->signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release));
156 wnd->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
157 wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
172 void Designer::save()
174 input = new ::Input(*this, "Save layout", filename);
175 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
176 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
180 void Designer::quit()
185 void Designer::edit_route(Route &r)
190 void Designer::add_selection_to_route()
197 const set<Track *> &stracks = selection->get_tracks();
198 set<const Track *> tracks(stracks.begin(), stracks.end());
199 cur_route->add_tracks(tracks);
201 catch(const Exception &e)
203 IO::print("%s\n", e.what());
207 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
209 float cos_pitch = cos(cam_pitch);
210 float sin_pitch = sin(cam_pitch);
211 float cos_yaw = cos(cam_yaw);
212 float sin_yaw = sin(cam_yaw);
214 float rx = sin_yaw*0.55228;
215 float ry = -cos_yaw*0.55228;
217 float ux = cos_yaw*-sin_pitch*0.41421;
218 float uy = sin_yaw*-sin_pitch*0.41421;
219 float uz = cos_pitch*0.41421;
221 float xf = static_cast<float>(x)*2/screen_w-1;
222 float yf = static_cast<float>(y)*2/screen_h-1;
224 float vx = cos_yaw*cos_pitch + xf*rx + yf*ux;
225 float vy = sin_yaw*cos_pitch + xf*ry + yf*uy;
226 float vz = sin_pitch + yf*uz;
228 gx = cam_pos.x-vx*cam_pos.z/vz;
229 gy = cam_pos.y-vy*cam_pos.z/vz;
232 void Designer::tick()
234 const Msp::Time::TimeStamp t = Msp::Time::now();
235 float dt = (t-last_tick)/Msp::Time::sec;
242 cam_pos.x += cos(cam_yaw)*dt*move_y;
243 cam_pos.y += sin(cam_yaw)*dt*move_y;
247 cam_pos.x += sin(cam_yaw)*dt*move_x;
248 cam_pos.y += -cos(cam_yaw)*dt*move_x;
252 cam_pos.x += cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
253 cam_pos.y += sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
254 cam_pos.z += sin(cam_pitch)*dt*zoom;
258 float vx = cos(cam_yaw)*cos(cam_pitch);
259 float vy = sin(cam_yaw)*cos(cam_pitch);
260 float vz = sin(cam_pitch);
262 float gx = cam_pos.x-vx*cam_pos.z/vz;
263 float gy = cam_pos.y-vy*cam_pos.z/vz;
264 float d = sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
266 cam_yaw += M_PI*dt*rotate;
272 cam_pos.x = gx+cos(cam_yaw)*d;
273 cam_pos.y = gy+sin(cam_yaw)*d;
277 cam_pitch += M_PI/2*dt*pitch;
278 if(cam_pitch>M_PI/12)
280 else if(cam_pitch<-M_PI/2)
284 if(tooltip_timeout && t>tooltip_timeout)
289 t3d = pick_track(pointer_x, pointer_y);
291 t3d = pick_track(pointer_x, pointer_y);
295 const Track &track = t3d->get_track();
296 const TrackType &ttype = track.get_type();
297 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
298 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
299 info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
300 if(track.get_turnout_id())
301 info += format(" (turnout %d)", track.get_turnout_id());
302 else if(track.get_sensor_id())
303 info += format(" (sensor %d)", track.get_sensor_id());
305 set_tooltip(pointer_x, pointer_y, info);
310 tooltip_timeout = Msp::Time::TimeStamp();
318 void Designer::key_press(unsigned code, unsigned mod, wchar_t)
320 unsigned key = Msp::Input::key_from_sys(code);
325 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
328 if(key==Msp::Input::KEY_N)
330 else if(key==Msp::Input::KEY_G)
332 manipulator->start_move();
335 else if(key==Msp::Input::KEY_R)
337 manipulator->start_rotate();
340 else if(key==Msp::Input::KEY_D)
342 manipulator->duplicate();
343 manipulator->start_move();
346 else if(key==Msp::Input::KEY_W)
348 else if(key==Msp::Input::KEY_PLUS)
349 selection->select_more();
350 else if(key==Msp::Input::KEY_L && (mod&1))
352 const set<Track *> &tracks = layout->get_tracks();
354 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
355 len += (*i)->get_type().get_total_length();
356 IO::print("Total length: %.1fm\n", len);
358 else if(key==Msp::Input::KEY_L)
359 selection->select_linked();
360 else if(key==Msp::Input::KEY_M)
365 else if(key==Msp::Input::KEY_Z)
367 manipulator->start_elevate();
370 else if(key==Msp::Input::KEY_ESC)
373 manipulator->cancel();
374 else if(mode==CATALOGUE)
379 else if(key==Msp::Input::KEY_X)
381 set<Track *> tracks = selection->get_tracks();
383 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
385 layout->remove_track(**i);
389 else if(key==Msp::Input::KEY_F && (mod&1))
391 const set<Track *> &tracks = selection->get_tracks();
392 const set<Track *> <racks = layout->get_tracks();
393 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
395 (*i)->set_flex(!(*i)->get_flex());
397 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
399 (*i)->snap_to(**j, true);
401 update_track_color(layout_3d->get_track(**i));
404 else if(key==Msp::Input::KEY_F)
405 manipulator->flatten();
406 else if(key==Msp::Input::KEY_E && (mod&1))
407 manipulator->even_slope(true);
408 else if(key==Msp::Input::KEY_E)
409 manipulator->even_slope();
410 else if(key==Msp::Input::KEY_T)
412 Track *track = selection->get_track();
413 if(selection->size()==1 && track->get_type().get_n_paths()>1)
415 input = new ::Input(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
416 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
417 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
421 else if(key==Msp::Input::KEY_S)
423 const set<Track *> &tracks = selection->get_tracks();
426 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
428 if((*i)->get_type().get_n_paths()==1)
430 if(static_cast<int>((*i)->get_sensor_id())!=id)
433 id = (*i)->get_sensor_id();
440 input = new ::Input(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
441 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
442 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
446 else if(key==Msp::Input::KEY_A)
447 add_selection_to_route();
448 else if(key==Msp::Input::KEY_RIGHT)
450 else if(key==Msp::Input::KEY_LEFT)
452 else if(key==Msp::Input::KEY_UP)
454 else if(key==Msp::Input::KEY_DOWN)
456 else if(key==Msp::Input::KEY_INSERT)
458 else if(key==Msp::Input::KEY_PGUP)
460 else if(key==Msp::Input::KEY_HOME)
462 else if(key==Msp::Input::KEY_END)
464 else if(key==Msp::Input::KEY_DELETE)
466 else if(key==Msp::Input::KEY_PGDN)
470 void Designer::key_release(unsigned code, unsigned)
472 unsigned key = Msp::Input::key_from_sys(code);
477 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
479 else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
481 else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
483 else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
485 else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
487 else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)
491 void Designer::button_press(int x, int y, unsigned btn, unsigned)
496 map_pointer_coords(x, y, gx, gy);
502 Track3D *ctrack = pick_track(x, y);
505 Track *track = ctrack->get_track().copy();
506 track->set_position(Point(gx, gy, 0));
507 layout->add_track(*track);
510 selection->add_track(track);
518 else if(mode==SELECT)
522 Track3D *track = pick_track(x, y);
527 selection->toggle_track(&track->get_track());
531 else if(mode==MANIPULATE)
532 manipulator->button_press(x, y, gx, gy, btn);
533 else if(mode==MEASURE)
534 measure->button_press(x, y, gx, gy, btn);
537 void Designer::pointer_motion(int x, int y)
544 if(mode==SELECT || mode==CATALOGUE)
545 tooltip_timeout = Msp::Time::now()+100*Msp::Time::msec;
552 map_pointer_coords(x, y, gx, gy);
553 manipulator->pointer_motion(x, y, gx, gy);
554 measure->pointer_motion(x, y, gx, gy);
558 void Designer::project_3d()
560 glMatrixMode(GL_PROJECTION);
562 glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
563 glMatrixMode(GL_MODELVIEW);
566 void Designer::apply_camera()
570 glTranslatef(0, 0, -1);
573 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
574 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
575 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
579 void Designer::render()
581 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
582 GL::enable(GL::DEPTH_TEST);
583 GL::Texture::unbind();
588 cat_layout_3d->get_scene().render();
591 pipeline->render_all();
592 layout_3d->get_endpoint_scene().render();
593 GL::enable(GL_CULL_FACE);
596 glColor4f(0.5, 0.8, 1.0, 1.0);
597 const set<const Track *> &rtracks = cur_route->get_tracks();
598 const map<unsigned, int> &turnouts = cur_route->get_turnouts();
599 for(set<const Track *>::const_iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
602 if(unsigned tid=(*i)->get_turnout_id())
604 map<unsigned, int>::const_iterator j = turnouts.find(tid);
605 if(j!=turnouts.end())
608 layout_3d->get_track(**i).render_path(path);
612 manipulator->render();
617 GL::matrix_mode(GL::PROJECTION);
619 GL::ortho_bottomleft(screen_w, screen_h);
620 GL::matrix_mode(GL::MODELVIEW);
623 GL::disable(GL::DEPTH_TEST);
628 Track3D *Designer::pick_track(int x, int y)
630 Layout3D *l = layout_3d;
634 float xx = (static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
635 float yy = (static_cast<float>(y)/screen_h-0.5)*0.82843;
636 float size = 4.0/screen_h*0.82843;
641 return l->pick_track(xx, yy, size);
644 void Designer::update_track_color(Track3D &track)
646 if(track.get_track().get_sensor_id())
648 if(track.get_track().get_flex())
649 track.set_color(GL::Color(1, 0.6, 1));
651 track.set_color(GL::Color(0.7, 0.7, 1));
653 else if(track.get_track().get_turnout_id())
654 track.set_color(GL::Color(0.8, 1, 0.8));
655 else if(track.get_track().get_flex())
656 track.set_color(GL::Color(1, 0.8, 0.8));
658 track.set_color(GL::Color(1, 1, 1));
661 void Designer::manipulation_status(const string &status)
663 set_tooltip(pointer_x, pointer_y, status);
666 void Designer::manipulation_done(bool)
671 void Designer::measure_changed()
673 float pard = measure->get_parallel_distance()*1000;
674 float perpd = measure->get_perpendicular_distance()*1000;
675 float d = sqrt(pard*pard+perpd*perpd);
676 float adiff = measure->get_angle_difference()*180/M_PI;
677 string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
678 set_tooltip(pointer_x, pointer_y, info);
681 void Designer::measure_done()
686 void Designer::set_tooltip(int x, int y, const std::string &text)
688 int fontsize = ui_res.get_default_font().get_default_size();
690 int w = static_cast<int>(ui_res.get_default_font().get_string_width(text)*fontsize)+6;
691 x = max(min(static_cast<int>(screen_w)-w, x), 0);
692 y = max(min(static_cast<int>(screen_h)-h, y), 0);
693 lbl_tooltip->set_text(text);
694 lbl_tooltip->set_geometry(GLtk::Geometry(x, y, w, h));
695 lbl_tooltip->set_visible(true);
698 void Designer::clear_tooltip()
700 lbl_tooltip->set_visible(false);
703 void Designer::save_accept()
705 layout->save(input->get_text());
710 void Designer::turnout_id_accept()
712 Track *track = selection->get_track();
713 unsigned id = lexical_cast<unsigned>(input->get_text());
714 track->set_turnout_id(id);
716 update_track_color(layout_3d->get_track(*track));
721 void Designer::sensor_id_accept()
723 const set<Track *> &tracks = selection->get_tracks();
724 unsigned id = lexical_cast<unsigned>(input->get_text());
725 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
727 (*i)->set_sensor_id(id);
729 update_track_color(layout_3d->get_track(**i));
735 void Designer::input_dismiss()