3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
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):
50 cam_pos(0, -0.5, 0.5),
58 DataFile::load(catalogue, "tracks.dat");
60 cat_layout = new Layout(catalogue);
61 cat_layout_3d = new Layout3D(*cat_layout);
63 const map<unsigned, TrackType *> &ctracks = catalogue.get_tracks();
65 for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
67 Track *track = new Track(*i->second);
68 track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
69 track->set_rotation(M_PI/2);
70 cat_layout->add_track(*track);
73 manipulator = new Manipulator(*this);
74 manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
75 manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
77 layout = new Layout(catalogue);
78 layout_3d = new Layout3D(*layout);
83 DataFile::load(*layout, argv[1]);
84 const list<Track3D *> <racks = layout_3d->get_tracks();
85 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
86 update_track_color(**i);
88 if(!layout->get_base().empty())
90 base_mesh = new GL::Mesh;
91 DataFile::load(*base_mesh, layout->get_base());
95 selection = new Selection;
96 manipulator->set_selection(selection);
98 measure = new Measure(*this);
99 measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
100 measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
103 Designer::~Designer()
110 delete cat_layout_3d;
116 dpy = new Graphics::Display;
117 wnd = new Graphics::Window(*dpy, screen_w, screen_h);
118 glc = new Graphics::GLContext(*wnd);
119 wnd->set_title("Railway Designer");
121 wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
124 glEnableClientState(GL_VERTEX_ARRAY);
125 glEnable(GL_DEPTH_TEST);
127 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
128 glEnable(GL_CULL_FACE);
130 DataFile::load(ui_res, "marklin.res");
131 root = new GLtk::Root(ui_res, *wnd);
133 lbl_tooltip = new GLtk::Label(ui_res);
134 lbl_tooltip->set_style("tooltip");
135 root->add(*lbl_tooltip);
136 lbl_tooltip->set_visible(false);
138 toolbar = new Toolbar(*this);
140 toolbar->set_position(0, screen_h-toolbar->get_geometry().h);
141 toolbar->set_visible(true);
144 wnd->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
145 wnd->signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release));
146 wnd->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
147 wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
162 void Designer::save()
164 input = new ::Input(*this, "Save layout", filename);
165 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
166 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
170 void Designer::quit()
175 void Designer::edit_route(Route &r)
180 void Designer::add_selection_to_route()
187 const set<Track *> &stracks = selection->get_tracks();
188 set<const Track *> tracks(stracks.begin(), stracks.end());
189 cur_route->add_tracks(tracks);
191 catch(const Exception &e)
193 IO::print("%s\n", e.what());
197 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
199 float cos_pitch = cos(cam_pitch);
200 float sin_pitch = sin(cam_pitch);
201 float cos_yaw = cos(cam_yaw);
202 float sin_yaw = sin(cam_yaw);
204 float rx = sin_yaw*0.55228;
205 float ry = -cos_yaw*0.55228;
207 float ux = cos_yaw*-sin_pitch*0.41421;
208 float uy = sin_yaw*-sin_pitch*0.41421;
209 float uz = cos_pitch*0.41421;
211 float xf = static_cast<float>(x)*2/screen_w-1;
212 float yf = static_cast<float>(y)*2/screen_h-1;
214 float vx = cos_yaw*cos_pitch + xf*rx + yf*ux;
215 float vy = sin_yaw*cos_pitch + xf*ry + yf*uy;
216 float vz = sin_pitch + yf*uz;
218 gx = cam_pos.x-vx*cam_pos.z/vz;
219 gy = cam_pos.y-vy*cam_pos.z/vz;
222 void Designer::tick()
224 const Msp::Time::TimeStamp t = Msp::Time::now();
225 float dt = (t-last_tick)/Msp::Time::sec;
232 cam_pos.x += cos(cam_yaw)*dt*move_y;
233 cam_pos.y += sin(cam_yaw)*dt*move_y;
237 cam_pos.x += sin(cam_yaw)*dt*move_x;
238 cam_pos.y += -cos(cam_yaw)*dt*move_x;
242 cam_pos.x += cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
243 cam_pos.y += sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
244 cam_pos.z += sin(cam_pitch)*dt*zoom;
248 float vx = cos(cam_yaw)*cos(cam_pitch);
249 float vy = sin(cam_yaw)*cos(cam_pitch);
250 float vz = sin(cam_pitch);
252 float gx = cam_pos.x-vx*cam_pos.z/vz;
253 float gy = cam_pos.y-vy*cam_pos.z/vz;
254 float d = sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
256 cam_yaw += M_PI*dt*rotate;
262 cam_pos.x = gx+cos(cam_yaw)*d;
263 cam_pos.y = gy+sin(cam_yaw)*d;
267 cam_pitch += M_PI/2*dt*pitch;
268 if(cam_pitch>M_PI/12)
270 else if(cam_pitch<-M_PI/2)
274 if(tooltip_timeout && t>tooltip_timeout)
279 t3d = pick_track(pointer_x, pointer_y);
281 t3d = pick_track(pointer_x, pointer_y);
285 const Track &track = t3d->get_track();
286 const TrackType &ttype = track.get_type();
289 ss<<ttype.get_article_number()<<' '<<ttype.get_description();
291 ss<<" (slope "<<track.get_slope()/ttype.get_total_length()*100<<"%)";
292 if(track.get_turnout_id())
293 ss<<" (turnout "<<track.get_turnout_id()<<')';
294 else if(track.get_sensor_id())
295 ss<<" (sensor "<<track.get_sensor_id()<<')';
297 set_tooltip(pointer_x, pointer_y, ss.str());
302 tooltip_timeout = Msp::Time::TimeStamp();
310 void Designer::key_press(unsigned code, unsigned mod, wchar_t)
312 unsigned key = Msp::Input::key_from_sys(code);
317 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
320 if(key==Msp::Input::KEY_N)
322 else if(key==Msp::Input::KEY_G)
324 manipulator->start_move();
327 else if(key==Msp::Input::KEY_R)
329 manipulator->start_rotate();
332 else if(key==Msp::Input::KEY_D)
334 manipulator->duplicate();
335 manipulator->start_move();
338 else if(key==Msp::Input::KEY_W)
340 else if(key==Msp::Input::KEY_PLUS)
341 selection->select_more();
342 else if(key==Msp::Input::KEY_L && (mod&1))
344 const set<Track *> &tracks = layout->get_tracks();
346 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
347 len += (*i)->get_type().get_total_length();
348 cout<<"Total length: "<<len<<"m\n";
350 else if(key==Msp::Input::KEY_L)
351 selection->select_linked();
352 else if(key==Msp::Input::KEY_M)
357 else if(key==Msp::Input::KEY_Z)
359 manipulator->start_elevate();
362 else if(key==Msp::Input::KEY_ESC)
365 manipulator->cancel();
366 else if(mode==CATALOGUE)
371 else if(key==Msp::Input::KEY_X)
373 set<Track *> tracks = selection->get_tracks();
375 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
377 layout->remove_track(**i);
381 else if(key==Msp::Input::KEY_F && (mod&1))
383 const set<Track *> &tracks = selection->get_tracks();
384 const set<Track *> <racks = layout->get_tracks();
385 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
387 (*i)->set_flex(!(*i)->get_flex());
389 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
391 (*i)->snap_to(**j, true);
393 update_track_color(layout_3d->get_track(**i));
396 else if(key==Msp::Input::KEY_F)
397 manipulator->flatten();
398 else if(key==Msp::Input::KEY_E && (mod&1))
399 manipulator->even_slope(true);
400 else if(key==Msp::Input::KEY_E)
401 manipulator->even_slope();
402 else if(key==Msp::Input::KEY_T)
404 Track *track = selection->get_track();
405 if(selection->size()==1 && track->get_type().get_n_routes()>1)
408 ss<<track->get_turnout_id();
409 input = new ::Input(*this, "Turnout ID", ss.str());
410 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
411 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
415 else if(key==Msp::Input::KEY_S)
417 const set<Track *> &tracks = selection->get_tracks();
420 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
422 if((*i)->get_type().get_n_routes()==1)
424 if(static_cast<int>((*i)->get_sensor_id())!=id)
427 id = (*i)->get_sensor_id();
437 input = new ::Input(*this, "Sensor ID", ss.str());
438 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
439 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
443 else if(key==Msp::Input::KEY_A)
444 add_selection_to_route();
445 else if(key==Msp::Input::KEY_RIGHT)
447 else if(key==Msp::Input::KEY_LEFT)
449 else if(key==Msp::Input::KEY_UP)
451 else if(key==Msp::Input::KEY_DOWN)
453 else if(key==Msp::Input::KEY_INSERT)
455 else if(key==Msp::Input::KEY_PGUP)
457 else if(key==Msp::Input::KEY_HOME)
459 else if(key==Msp::Input::KEY_END)
461 else if(key==Msp::Input::KEY_DELETE)
463 else if(key==Msp::Input::KEY_PGDN)
467 void Designer::key_release(unsigned code, unsigned)
469 unsigned key = Msp::Input::key_from_sys(code);
474 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
476 else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
478 else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
480 else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
482 else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
484 else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)
488 void Designer::button_press(int x, int y, unsigned btn, unsigned)
493 map_pointer_coords(x, y, gx, gy);
499 Track3D *ctrack = pick_track(x, y);
502 Track *track = ctrack->get_track().copy();
503 track->set_position(Point(gx, gy, 0));
504 layout->add_track(*track);
507 selection->add_track(track);
515 else if(mode==SELECT)
519 Track3D *track = pick_track(x, y);
524 selection->toggle_track(&track->get_track());
528 else if(mode==MANIPULATE)
529 manipulator->button_press(x, y, gx, gy, btn);
530 else if(mode==MEASURE)
531 measure->button_press(x, y, gx, gy, btn);
534 void Designer::pointer_motion(int x, int y)
541 if(mode==SELECT || mode==CATALOGUE)
542 tooltip_timeout = Msp::Time::now()+100*Msp::Time::msec;
549 map_pointer_coords(x, y, gx, gy);
550 manipulator->pointer_motion(x, y, gx, gy);
551 measure->pointer_motion(x, y, gx, gy);
555 void Designer::project_3d()
557 glMatrixMode(GL_PROJECTION);
559 glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
560 glMatrixMode(GL_MODELVIEW);
563 void Designer::apply_camera()
567 glTranslatef(0, 0, -1);
570 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
571 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
572 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
576 void Designer::render()
578 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
579 GL::enable(GL::DEPTH_TEST);
584 cat_layout_3d->render();
589 GL::Texture::unbind();
592 layout_3d->render(true);
595 glColor4f(0.5, 0.8, 1.0, 1.0);
596 const set<const Track *> &rtracks = cur_route->get_tracks();
597 const map<unsigned, int> &turnouts = cur_route->get_turnouts();
598 for(set<const Track *>::const_iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
601 if(unsigned tid=(*i)->get_turnout_id())
603 map<unsigned, int>::const_iterator j = turnouts.find(tid);
604 if(j!=turnouts.end())
607 layout_3d->get_track(**i).render_route(route);
610 manipulator->render();
615 GL::matrix_mode(GL::PROJECTION);
617 GL::ortho_bottomleft(screen_w, screen_h);
618 GL::matrix_mode(GL::MODELVIEW);
621 GL::disable(GL::DEPTH_TEST);
626 Track3D *Designer::pick_track(int x, int y)
628 Layout3D *l = layout_3d;
632 float xx = (static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
633 float yy = (static_cast<float>(y)/screen_h-0.5)*0.82843;
634 float size = 4.0/screen_h*0.82843;
639 return l->pick_track(xx, yy, size);
642 void Designer::update_track_color(Track3D &track)
644 if(track.get_track().get_sensor_id())
646 if(track.get_track().get_flex())
647 track.set_color(GL::Color(1, 0.6, 1));
649 track.set_color(GL::Color(0.7, 0.7, 1));
651 else if(track.get_track().get_turnout_id())
652 track.set_color(GL::Color(0.8, 1, 0.8));
653 else if(track.get_track().get_flex())
654 track.set_color(GL::Color(1, 0.8, 0.8));
656 track.set_color(GL::Color(1, 1, 1));
659 void Designer::manipulation_status(const string &status)
661 set_tooltip(pointer_x, pointer_y, status);
664 void Designer::manipulation_done(bool)
669 void Designer::measure_changed()
671 float pard = measure->get_parallel_distance()*1000;
672 float perpd = measure->get_perpendicular_distance()*1000;
673 float d = sqrt(pard*pard+perpd*perpd);
674 float adiff = measure->get_angle_difference()*180/M_PI;
677 ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
678 set_tooltip(pointer_x, pointer_y, ss.str());
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()