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/rendermode.h>
13 #include <msp/gl/select.h>
14 #include <msp/gl/texture2d.h>
15 #include <msp/input/keys.h>
16 #include <msp/strings/codec.h>
17 #include <msp/strings/lexicalcast.h>
18 #include <msp/strings/utf8.h>
19 #include <msp/strings/utils.h>
20 #include <msp/time/units.h>
21 #include <msp/time/utils.h>
22 #include "libmarklin/tracktype.h"
25 #include "manipulator.h"
27 #include "selection.h"
30 using namespace Marklin;
33 Designer::Designer(int argc, char **argv):
41 cam_pos(0, -0.5, 0.5),
49 catalogue.load("tracks.dat");
51 cat_layout=new Layout(catalogue);
52 cat_layout_3d=new Layout3D(*cat_layout);
54 const map<unsigned, TrackType *> &ctracks=catalogue.get_tracks();
56 for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
58 Track *track=new Track(*i->second);
59 track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
60 track->set_rotation(M_PI/2);
61 cat_layout->add_track(*track);
64 manipulator=new Manipulator(*this);
65 manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
66 manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
68 layout=new Layout(catalogue);
69 layout_3d=new Layout3D(*layout);
73 layout->load(argv[1]);
74 const list<Track3D *> <racks=layout_3d->get_tracks();
75 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
77 if((*i)->get_track().get_sensor_id())
78 (*i)->set_color(GL::Color(1, 1, 0.5));
79 else if((*i)->get_track().get_turnout_id())
80 (*i)->set_color(GL::Color(0.5, 1, 1));
81 else if((*i)->get_track().get_flex())
82 (*i)->set_color(GL::Color(1, 0.5, 1));
85 if(!layout->get_base().empty())
87 base_mesh=new GL::Mesh;
88 DataFile::load(*base_mesh, layout->get_base());
92 selection=new Selection;
93 manipulator->set_selection(selection);
95 measure=new Measure(*this);
96 measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
97 measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
100 Designer::~Designer()
107 delete cat_layout_3d;
113 dpy=new Graphics::Display;
114 wnd=new Graphics::Window(*dpy, screen_w, screen_h);
115 glc=new Graphics::GLContext(*wnd);
117 wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
118 wnd->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
119 wnd->signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release));
120 wnd->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
121 wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
125 glEnableClientState(GL_VERTEX_ARRAY);
126 glEnable(GL_DEPTH_TEST);
128 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
129 glEnable(GL_CULL_FACE);
131 GL::Texture2D *font_tex=new GL::Texture2D;
132 font_tex->set_min_filter(GL::LINEAR);
133 font_tex->load_image("dejavu-20.png");
135 font->set_texture(*font_tex);
136 DataFile::load(*font, "dejavu-20.font");
153 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
155 float cos_pitch=cos(cam_pitch);
156 float sin_pitch=sin(cam_pitch);
157 float cos_yaw=cos(cam_yaw);
158 float sin_yaw=sin(cam_yaw);
160 float rx=sin_yaw*0.55228;
161 float ry=-cos_yaw*0.55228;
163 float ux=cos_yaw*-sin_pitch*0.41421;
164 float uy=sin_yaw*-sin_pitch*0.41421;
165 float uz=cos_pitch*0.41421;
167 float xf=static_cast<float>(x)*2/screen_w-1;
168 float yf=static_cast<float>(y)*2/screen_h-1;
170 float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
171 float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
172 float vz=sin_pitch + yf*uz;
174 gx=cam_pos.x-vx*cam_pos.z/vz;
175 gy=cam_pos.y-vy*cam_pos.z/vz;
178 void Designer::tick()
180 const Msp::Time::TimeStamp t=Msp::Time::now();
181 float dt=(t-last_tick)/Msp::Time::sec;
188 cam_pos.x+=cos(cam_yaw)*dt*move_y;
189 cam_pos.y+=sin(cam_yaw)*dt*move_y;
193 cam_pos.x+=sin(cam_yaw)*dt*move_x;
194 cam_pos.y+=-cos(cam_yaw)*dt*move_x;
198 cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
199 cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
200 cam_pos.z+=sin(cam_pitch)*dt*zoom;
204 float vx=cos(cam_yaw)*cos(cam_pitch);
205 float vy=sin(cam_yaw)*cos(cam_pitch);
206 float vz=sin(cam_pitch);
208 float gx=cam_pos.x-vx*cam_pos.z/vz;
209 float gy=cam_pos.y-vy*cam_pos.z/vz;
210 float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
212 cam_yaw+=M_PI*dt*rotate;
218 cam_pos.x=gx+cos(cam_yaw)*d;
219 cam_pos.y=gy+sin(cam_yaw)*d;
223 cam_pitch+=M_PI/2*dt*pitch;
224 if(cam_pitch>M_PI/12)
226 else if(cam_pitch<-M_PI/2)
230 if(tooltip_timeout && t>tooltip_timeout)
235 t3d=pick_track(pointer_x, pointer_y);
237 t3d=pick_track(pointer_x, pointer_y);
241 const Track &track=t3d->get_track();
242 const TrackType &ttype=track.get_type();
245 ss<<ttype.get_article_number()<<' '<<ttype.get_description();
247 ss<<" (slope "<<track.get_slope()/ttype.get_total_length()*100<<"%)";
248 if(track.get_turnout_id())
249 ss<<" (turnout "<<track.get_turnout_id()<<')';
250 else if(track.get_sensor_id())
251 ss<<" (sensor "<<track.get_sensor_id()<<')';
254 move_tooltip(pointer_x, pointer_y);
259 tooltip_timeout=Msp::Time::TimeStamp();
269 void Designer::key_press(unsigned code, unsigned mod, wchar_t ch)
271 unsigned key=Msp::Input::key_from_sys(code);
275 input->key_press(key, mod, ch);
279 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
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 input=new ::Input(*this, "Filename");
303 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
304 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
307 else if(key==Msp::Input::KEY_PLUS)
308 selection->select_more();
309 else if(key==Msp::Input::KEY_L && (mod&1))
311 const set<Track *> &tracks=layout->get_tracks();
313 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
314 len+=(*i)->get_type().get_total_length();
315 cout<<"Total length: "<<len<<"m\n";
317 else if(key==Msp::Input::KEY_L)
318 selection->select_linked();
319 else if(key==Msp::Input::KEY_M)
324 else if(key==Msp::Input::KEY_Z)
326 manipulator->start_elevate();
329 else if(key==Msp::Input::KEY_ESC)
332 manipulator->cancel();
333 else if(mode==CATALOGUE)
338 else if(key==Msp::Input::KEY_X)
340 set<Track *> tracks=selection->get_tracks();
342 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
344 layout->remove_track(**i);
348 else if(key==Msp::Input::KEY_F && (mod&1))
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 Track3D &t3d=layout_3d->get_track(**i);
362 t3d.set_color(GL::Color(1, 0.5, 1));
364 t3d.set_color(GL::Color(1, 1, 1));
367 else if(key==Msp::Input::KEY_F)
368 manipulator->flatten();
369 else if(key==Msp::Input::KEY_E && (mod&1))
370 manipulator->even_slope(true);
371 else if(key==Msp::Input::KEY_E)
372 manipulator->even_slope();
373 else if(key==Msp::Input::KEY_T)
375 Track *track=selection->get_track();
376 if(selection->size()==1 && track->get_type().get_n_routes()>1)
379 ss<<track->get_turnout_id();
380 input=new ::Input(*this, "Turnout ID", ss.str());
381 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
382 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
386 else if(key==Msp::Input::KEY_S)
388 const set<Track *> &tracks=selection->get_tracks();
391 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
393 if((*i)->get_type().get_n_routes()==1)
395 if(static_cast<int>((*i)->get_sensor_id())!=id)
398 id=(*i)->get_sensor_id();
408 input=new ::Input(*this, "Sensor ID", ss.str());
409 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
410 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
414 else if(key==Msp::Input::KEY_RIGHT)
416 else if(key==Msp::Input::KEY_LEFT)
418 else if(key==Msp::Input::KEY_UP)
420 else if(key==Msp::Input::KEY_DOWN)
422 else if(key==Msp::Input::KEY_INSERT)
424 else if(key==Msp::Input::KEY_PGUP)
426 else if(key==Msp::Input::KEY_HOME)
428 else if(key==Msp::Input::KEY_END)
430 else if(key==Msp::Input::KEY_DELETE)
432 else if(key==Msp::Input::KEY_PGDN)
436 void Designer::key_release(unsigned code, unsigned)
438 unsigned key=Msp::Input::key_from_sys(code);
443 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
445 else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
447 else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
449 else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
451 else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
453 else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)
457 void Designer::button_press(int x, int y, unsigned btn, unsigned)
462 map_pointer_coords(x, y, gx, gy);
468 Track3D *ctrack=pick_track(x, y);
471 Track *track=ctrack->get_track().copy();
472 track->set_position(Point(gx, gy, 0));
473 layout->add_track(*track);
476 selection->add_track(track);
484 else if(mode==SELECT)
488 Track3D *track=pick_track(x, y);
493 selection->toggle_track(&track->get_track());
497 else if(mode==MANIPULATE)
498 manipulator->button_press(x, y, gx, gy, btn);
499 else if(mode==MEASURE)
500 measure->button_press(x, y, gx, gy, btn);
503 void Designer::pointer_motion(int x, int y)
508 map_pointer_coords(x, y, gx, gy);
510 if(mode==SELECT || mode==CATALOGUE)
514 tooltip_timeout=Msp::Time::now()+100*Msp::Time::msec;
519 manipulator->pointer_motion(x, y, gx, gy);
520 measure->pointer_motion(x, y, gx, gy);
523 if(mode==MEASURE || mode==MANIPULATE)
527 void Designer::project_3d()
529 glMatrixMode(GL_PROJECTION);
531 glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
532 glMatrixMode(GL_MODELVIEW);
535 void Designer::apply_camera()
539 glTranslatef(0, 0, -1);
542 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
543 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
544 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
548 void Designer::render()
550 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
551 glEnable(GL_DEPTH_TEST);
556 cat_layout_3d->render();
561 GL::Texture::unbind();
564 layout_3d->render(true);
565 manipulator->render();
570 glMatrixMode(GL_PROJECTION);
572 glOrtho(0, screen_w, 0, screen_h, 0, 1);
573 glMatrixMode(GL_MODELVIEW);
576 glDisable(GL_DEPTH_TEST);
580 glTranslatef(tooltip_x, tooltip_y, 0);
581 glScalef(20, 20, 20);
582 float width=font->get_string_width(tooltip);
583 glColor4f(0, 0, 0, 0.5);
586 glVertex2f(width, 0);
587 glVertex2f(width, 1);
590 glColor4f(1, 1, 1, 1);
591 font->draw_string(tooltip);
598 Track3D *Designer::pick_track(int x, int y)
600 Layout3D *l=layout_3d;
604 float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
605 float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
606 float size=4.0/screen_h*0.82843;
611 return l->pick_track(xx, yy, size);
614 void Designer::manipulation_status(const string &status)
619 void Designer::manipulation_done(bool)
624 void Designer::measure_changed()
626 float pard=measure->get_parallel_distance()*1000;
627 float perpd=measure->get_perpendicular_distance()*1000;
628 float d=sqrt(pard*pard+perpd*perpd);
629 float adiff=measure->get_angle_difference()*180/M_PI;
632 ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
636 void Designer::measure_done()
641 void Designer::move_tooltip(int x, int y)
643 int w=static_cast<int>(font->get_string_width(tooltip)*20);
644 tooltip_x=max(min(static_cast<int>(screen_w)-w, x), 0);
645 tooltip_y=max(min(static_cast<int>(screen_h)-20, y), 0);
648 void Designer::save_accept()
650 layout->save(input->get_text());
655 void Designer::turnout_id_accept()
657 Track *track=selection->get_track();
658 unsigned id=lexical_cast<unsigned>(input->get_text());
659 track->set_turnout_id(id);
661 Track3D &t3d=layout_3d->get_track(*track);
663 t3d.set_color(GL::Color(0.5, 1, 1));
665 t3d.set_color(GL::Color(1, 1, 1));
670 void Designer::sensor_id_accept()
672 const set<Track *> &tracks=selection->get_tracks();
673 unsigned id=lexical_cast<unsigned>(input->get_text());
674 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
676 (*i)->set_sensor_id(id);
678 Track3D &t3d=layout_3d->get_track(**i);
680 t3d.set_color(GL::Color(1, 1, 0.5));
682 t3d.set_color(GL::Color(1, 1, 1));
688 void Designer::input_dismiss()
695 Application::RegApp<Designer> Designer::reg;