6 #include <msp/gl/rendermode.h>
7 #include <msp/gl/select.h>
8 #include <msp/gl/texture2d.h>
9 #include <msp/strings/codec.h>
10 #include <msp/strings/lexicalcast.h>
11 #include <msp/strings/utf8.h>
12 #include <msp/strings/utils.h>
13 #include <msp/time/units.h>
14 #include <msp/time/utils.h>
15 #include "libmarklin/tracktype.h"
18 #include "manipulator.h"
20 #include "selection.h"
23 using namespace Marklin;
26 Designer::Designer(int argc, char **argv):
33 cam_pos(0, -0.5, 0.5),
41 catalogue.load("tracks.dat");
43 cat_layout=new Layout(catalogue);
44 cat_layout_3d=new Layout3D(*cat_layout);
46 const map<unsigned, TrackType *> &ctracks=catalogue.get_tracks();
48 for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
50 Track *track=new Track(*i->second);
51 track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
52 track->set_rotation(M_PI/2);
53 cat_layout->add_track(*track);
56 manipulator=new Manipulator(*this);
57 manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
58 manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
60 layout=new Layout(catalogue);
61 layout_3d=new Layout3D(*layout);
65 layout->load(argv[1]);
66 const Track3DSeq <racks=layout_3d->get_tracks();
67 for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
69 if((*i)->get_track().get_sensor_id())
70 (*i)->set_color(Color(1, 1, 0.5));
71 else if((*i)->get_track().get_turnout_id())
72 (*i)->set_color(Color(0.5, 1, 1));
73 else if((*i)->get_track().get_flex())
74 (*i)->set_color(Color(1, 0.5, 1));
78 selection=new Selection;
79 manipulator->set_selection(selection);
81 measure=new Measure(*this);
82 measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
83 measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
88 setenv("__GL_SYNC_TO_VBLANK", "1", 0);
89 SDL_Init(SDL_INIT_VIDEO);
90 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
91 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
92 SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL);
95 glEnableClientState(GL_VERTEX_ARRAY);
96 glEnable(GL_DEPTH_TEST);
98 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
99 glEnable(GL_CULL_FACE);
101 GL::Texture2D *font_tex=new GL::Texture2D;
102 font_tex->set_min_filter(GL::LINEAR);
103 font_tex->load_image("dejavu-20.png");
105 font->set_texture(*font_tex);
106 DataFile::load(*font, "dejavu-20.font");
121 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
123 float cos_pitch=cos(cam_pitch);
124 float sin_pitch=sin(cam_pitch);
125 float cos_yaw=cos(cam_yaw);
126 float sin_yaw=sin(cam_yaw);
128 float rx=sin_yaw*0.55228;
129 float ry=-cos_yaw*0.55228;
131 float ux=cos_yaw*-sin_pitch*0.41421;
132 float uy=sin_yaw*-sin_pitch*0.41421;
133 float uz=cos_pitch*0.41421;
135 float xf=(float)x*2/screen_w-1;
136 float yf=1-(float)y*2/screen_h;
138 float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
139 float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
140 float vz=sin_pitch + yf*uz;
142 gx=cam_pos.x-vx*cam_pos.z/vz;
143 gy=cam_pos.y-vy*cam_pos.z/vz;
146 void Designer::tick()
149 while(SDL_PollEvent(&event))
154 case SDL_MOUSEBUTTONDOWN:
155 map_pointer_coords(event.button.x, event.button.y, gx, gy);
156 button_press(event.button.x, screen_h-1-event.button.y, gx, gy, event.button.button);
158 case SDL_MOUSEMOTION:
159 map_pointer_coords(event.motion.x, event.motion.y, gx, gy);
160 pointer_motion(event.motion.x, screen_h-1-event.motion.y, gx, gy);
163 key_press(event.key.keysym.sym, event.key.keysym.mod, event.key.keysym.unicode);
166 key_release(event.key.keysym.sym, event.key.keysym.mod);
174 const Time::TimeStamp t=Time::now();
175 float dt=(t-last_tick)/Time::sec;
180 cam_pos.x+=cos(cam_yaw)*dt*move_y;
181 cam_pos.y+=sin(cam_yaw)*dt*move_y;
185 cam_pos.x+=sin(cam_yaw)*dt*move_x;
186 cam_pos.y+=-cos(cam_yaw)*dt*move_x;
190 cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
191 cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
192 cam_pos.z+=sin(cam_pitch)*dt*zoom;
196 float vx=cos(cam_yaw)*cos(cam_pitch);
197 float vy=sin(cam_yaw)*cos(cam_pitch);
198 float vz=sin(cam_pitch);
200 float gx=cam_pos.x-vx*cam_pos.z/vz;
201 float gy=cam_pos.y-vy*cam_pos.z/vz;
202 float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
204 cam_yaw+=M_PI*dt*rotate;
210 cam_pos.x=gx+cos(cam_yaw)*d;
211 cam_pos.y=gy+sin(cam_yaw)*d;
215 cam_pitch+=M_PI/2*dt*pitch;
216 if(cam_pitch>M_PI/12)
218 else if(cam_pitch<-M_PI/2)
222 if(tooltip_timeout && t>tooltip_timeout)
227 t3d=pick_track(pointer_x, pointer_y);
229 t3d=pick_track(pointer_x, pointer_y);
233 const Track &track=t3d->get_track();
234 const TrackType &ttype=track.get_type();
237 ss<<ttype.get_article_number()<<' '<<ttype.get_description();
239 ss<<" (slope "<<track.get_slope()/ttype.get_total_length()*100<<"%)";
240 if(track.get_turnout_id())
241 ss<<" (turnout "<<track.get_turnout_id()<<')';
242 else if(track.get_sensor_id())
243 ss<<" (sensor "<<track.get_sensor_id()<<')';
246 move_tooltip(pointer_x, pointer_y);
251 tooltip_timeout=Time::TimeStamp();
256 SDL_GL_SwapBuffers();
259 Designer::~Designer()
266 delete cat_layout_3d;
272 void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
276 input->key_press(key, mod, ch);
280 if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
287 manipulator->start_move();
292 manipulator->start_rotate();
297 manipulator->duplicate();
298 manipulator->start_move();
303 input=new Input(*this, "Filename");
304 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
305 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
308 else if(key==SDLK_PLUS)
309 selection->select_more();
310 else if(key==SDLK_l && (mod&KMOD_SHIFT))
312 const set<Track *> &tracks=layout->get_tracks();
314 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
315 len+=(*i)->get_type().get_total_length();
316 cout<<"Total length: "<<len<<"m\n";
319 selection->select_linked();
327 manipulator->start_elevate();
330 else if(key==SDLK_ESCAPE)
333 manipulator->cancel();
334 else if(mode==CATALOGUE)
341 set<Track *> tracks=selection->get_tracks();
343 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
345 layout->remove_track(**i);
349 else if(key==SDLK_f && (mod&KMOD_SHIFT))
351 const set<Track *> &tracks=selection->get_tracks();
352 const set<Track *> <racks=layout->get_tracks();
353 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
355 (*i)->set_flex(!(*i)->get_flex());
357 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
359 (*i)->snap_to(**j, true);
361 Track3D *t3d=layout_3d->get_track(*i);
363 t3d->set_color(Color(1, 0.5, 1));
365 t3d->set_color(Color(1, 1, 1));
369 manipulator->flatten();
370 else if(key==SDLK_e && (mod&KMOD_SHIFT))
371 manipulator->even_slope(true);
373 manipulator->even_slope();
376 Track *track=selection->get_track();
377 if(selection->size()==1 && track->get_type().get_n_routes()>1)
380 ss<<track->get_turnout_id();
381 input=new Input(*this, "Turnout ID", ss.str());
382 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
383 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
389 Track *track=selection->get_track();
390 if(selection->size()==1 && track->get_type().get_n_routes()==1)
393 ss<<track->get_sensor_id();
394 input=new Input(*this, "Sensor ID", ss.str());
395 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
396 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
400 else if(key==SDLK_RIGHT)
402 else if(key==SDLK_LEFT)
404 else if(key==SDLK_UP)
406 else if(key==SDLK_DOWN)
408 else if(key==SDLK_INSERT)
410 else if(key==SDLK_PAGEUP)
412 else if(key==SDLK_HOME)
414 else if(key==SDLK_END)
416 else if(key==SDLK_DELETE)
418 else if(key==SDLK_PAGEDOWN)
422 void Designer::key_release(unsigned key, unsigned)
427 if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
429 else if(key==SDLK_RIGHT || key==SDLK_LEFT)
431 else if(key==SDLK_UP || key==SDLK_DOWN)
433 else if(key==SDLK_INSERT || key==SDLK_PAGEUP)
435 else if(key==SDLK_HOME || key==SDLK_END)
437 else if(key==SDLK_DELETE || key==SDLK_PAGEDOWN)
441 void Designer::button_press(int x, int y, float gx, float gy, unsigned btn)
447 Track3D *ctrack=pick_track(x, y);
450 Track *track=ctrack->get_track().copy();
451 track->set_position(Point(gx, gy, 0));
452 layout->add_track(*track);
455 selection->add_track(track);
463 else if(mode==SELECT)
467 Track3D *track=pick_track(x, y);
472 selection->toggle_track(&track->get_track());
476 else if(mode==MANIPULATE)
477 manipulator->button_press(x, y, gx, gy, btn);
478 else if(mode==MEASURE)
479 measure->button_press(x, y, gx, gy, btn);
482 void Designer::pointer_motion(int x, int y, float gx, float gy)
484 if(mode==SELECT || mode==CATALOGUE)
488 tooltip_timeout=Time::now()+100*Time::msec;
493 manipulator->pointer_motion(x, y, gx, gy);
494 measure->pointer_motion(x, y, gx, gy);
497 if(mode==MEASURE || mode==MANIPULATE)
501 void Designer::project_3d()
503 glMatrixMode(GL_PROJECTION);
505 glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
506 glMatrixMode(GL_MODELVIEW);
509 void Designer::apply_camera()
513 glTranslatef(0, 0, -1);
516 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
517 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
518 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
522 void Designer::render()
524 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
525 glEnable(GL_DEPTH_TEST);
530 cat_layout_3d->render();
533 layout_3d->render(true);
534 manipulator->render();
540 glColor4f(1, 0, 0, 1);
542 glVertex3f(0.5, 0, 0);
543 glColor4f(0, 1, 0, 1);
545 glVertex3f(0, 0.5, 0);
546 glColor4f(0, 0, 1, 1);
548 glVertex3f(0, 0, 0.5);
551 glMatrixMode(GL_PROJECTION);
553 glOrtho(0, screen_w, 0, screen_h, 0, 1);
554 glMatrixMode(GL_MODELVIEW);
557 glDisable(GL_DEPTH_TEST);
561 glTranslatef(tooltip_x, tooltip_y, 0);
562 glScalef(20, 20, 20);
563 float width=font->get_string_width(tooltip);
564 glColor4f(0, 0, 0, 0.5);
567 glVertex2f(width, 0);
568 glVertex2f(width, 1);
571 glColor4f(1, 1, 1, 1);
572 font->draw_string(tooltip);
579 Track3D *Designer::pick_track(int x, int y)
581 Layout3D *l=layout_3d;
585 float xx=((float)(x-(int)screen_w/2)/screen_h)*0.82843;
586 float yy=((float)y/screen_h-0.5)*0.82843;
587 float size=(float)4/screen_h*0.82843;
592 return l->pick_track(xx, yy, size);
595 void Designer::manipulation_status(const string &status)
600 void Designer::manipulation_done(bool)
605 void Designer::measure_changed()
607 float pard=measure->get_parallel_distance()*1000;
608 float perpd=measure->get_perpendicular_distance()*1000;
609 float d=sqrt(pard*pard+perpd*perpd);
610 float adiff=measure->get_angle_difference()*180/M_PI;
613 ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
617 void Designer::measure_done()
622 void Designer::move_tooltip(int x, int y)
624 int w=(int)(font->get_string_width(tooltip)*20);
625 tooltip_x=max(min((int)screen_w-w, x), 0);
626 tooltip_y=max(min((int)screen_h-20, y), 0);
629 void Designer::save_accept()
631 layout->save(input->get_text());
636 void Designer::turnout_id_accept()
638 Track *track=selection->get_track();
639 unsigned id=lexical_cast<unsigned>(input->get_text());
640 track->set_turnout_id(id);
642 Track3D *t3d=layout_3d->get_track(track);
644 t3d->set_color(Color(0.5, 1, 1));
646 t3d->set_color(Color(1, 1, 1));
651 void Designer::sensor_id_accept()
653 Track *track=selection->get_track();
654 unsigned id=lexical_cast<unsigned>(input->get_text());
655 track->set_sensor_id(id);
657 Track3D *t3d=layout_3d->get_track(track);
659 t3d->set_color(Color(1, 1, 0.5));
661 t3d->set_color(Color(1, 1, 1));
666 void Designer::input_dismiss()
673 Application::RegApp<Designer> Designer::reg;