6 #include <msp/gl/rendermode.h>
7 #include <msp/gl/select.h>
8 #include <msp/strings/codec.h>
9 #include <msp/strings/lexicalcast.h>
10 #include <msp/strings/utf8.h>
11 #include <msp/strings/utils.h>
12 #include <msp/time/units.h>
13 #include <msp/time/utils.h>
16 #include "manipulator.h"
18 #include "selection.h"
21 using namespace Marklin;
24 Designer::Designer(int argc, char **argv):
31 cam_pos(0, -0.5, 0.5),
39 catalogue.load("tracks.dat");
41 cat_layout=new Layout(catalogue);
42 cat_layout_3d=new Layout3D(*cat_layout);
44 const Catalogue::TrackMap &ctracks=catalogue.get_tracks();
46 for(Catalogue::TrackMap::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
48 Track *track=i->second->copy();
49 track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
50 track->set_rotation(M_PI/2);
51 cat_layout->add_track(track);
54 manipulator=new Manipulator(*this);
55 manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
56 manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
58 layout=new Layout(catalogue);
59 layout_3d=new Layout3D(*layout);
63 layout->load(argv[1]);
64 const Track3DSeq <racks=layout_3d->get_tracks();
65 for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
67 if((*i)->get_track().get_sensor_id())
68 (*i)->set_color(Color(1, 1, 0.5));
69 else if((*i)->get_track().get_turnout_id())
70 (*i)->set_color(Color(0.5, 1, 1));
71 else if((*i)->get_track().get_flex())
72 (*i)->set_color(Color(1, 0.5, 1));
76 selection=new Selection;
77 manipulator->set_selection(selection);
79 measure=new Measure(*this);
80 measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
81 measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
86 setenv("__GL_SYNC_TO_VBLANK", "1", 0);
87 SDL_Init(SDL_INIT_VIDEO);
88 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
89 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
90 SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL);
93 glEnableClientState(GL_VERTEX_ARRAY);
94 glEnable(GL_DEPTH_TEST);
96 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
97 glEnable(GL_CULL_FACE);
100 Parser::load(*font, "dejavu-20.font");
114 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
116 float cos_pitch=cos(cam_pitch);
117 float sin_pitch=sin(cam_pitch);
118 float cos_yaw=cos(cam_yaw);
119 float sin_yaw=sin(cam_yaw);
121 float rx=sin_yaw*0.55228;
122 float ry=-cos_yaw*0.55228;
124 float ux=cos_yaw*-sin_pitch*0.41421;
125 float uy=sin_yaw*-sin_pitch*0.41421;
126 float uz=cos_pitch*0.41421;
128 float xf=(float)x*2/screen_w-1;
129 float yf=1-(float)y*2/screen_h;
131 float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
132 float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
133 float vz=sin_pitch + yf*uz;
135 gx=cam_pos.x-vx*cam_pos.z/vz;
136 gy=cam_pos.y-vy*cam_pos.z/vz;
139 void Designer::tick()
142 while(SDL_PollEvent(&event))
147 case SDL_MOUSEBUTTONDOWN:
148 map_pointer_coords(event.button.x, event.button.y, gx, gy);
149 button_press(event.button.x, screen_h-1-event.button.y, gx, gy, event.button.button);
151 case SDL_MOUSEMOTION:
152 map_pointer_coords(event.motion.x, event.motion.y, gx, gy);
153 pointer_motion(event.motion.x, screen_h-1-event.motion.y, gx, gy);
156 key_press(event.key.keysym.sym, event.key.keysym.mod, event.key.keysym.unicode);
159 key_release(event.key.keysym.sym, event.key.keysym.mod);
167 const Time::TimeStamp t=Time::now();
168 float dt=(t-last_tick)/Time::sec;
173 cam_pos.x+=cos(cam_yaw)*dt*move_y;
174 cam_pos.y+=sin(cam_yaw)*dt*move_y;
178 cam_pos.x+=sin(cam_yaw)*dt*move_x;
179 cam_pos.y+=-cos(cam_yaw)*dt*move_x;
183 cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
184 cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
185 cam_pos.z+=sin(cam_pitch)*dt*zoom;
189 float vx=cos(cam_yaw)*cos(cam_pitch);
190 float vy=sin(cam_yaw)*cos(cam_pitch);
191 float vz=sin(cam_pitch);
193 float gx=cam_pos.x-vx*cam_pos.z/vz;
194 float gy=cam_pos.y-vy*cam_pos.z/vz;
195 float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
197 cam_yaw+=M_PI*dt*rotate;
203 cam_pos.x=gx+cos(cam_yaw)*d;
204 cam_pos.y=gy+sin(cam_yaw)*d;
208 cam_pitch+=M_PI/2*dt*pitch;
209 if(cam_pitch>M_PI/12)
211 else if(cam_pitch<-M_PI/2)
215 if(tooltip_timeout && t>tooltip_timeout)
220 t3d=pick_track(pointer_x, pointer_y);
222 t3d=pick_track(pointer_x, pointer_y);
226 const Track &track=t3d->get_track();
229 ss<<track.get_article_number()<<' '<<track.get_description();
231 ss<<" (slope "<<track.get_slope()/track.get_length()*100<<"%)";
232 if(track.get_turnout_id())
233 ss<<" (turnout "<<track.get_turnout_id()<<')';
234 else if(track.get_sensor_id())
235 ss<<" (sensor "<<track.get_sensor_id()<<')';
236 tooltip=decode<Utf8>(ss.str());
238 move_tooltip(pointer_x, pointer_y);
243 tooltip_timeout=Time::TimeStamp();
248 SDL_GL_SwapBuffers();
251 Designer::~Designer()
258 delete cat_layout_3d;
264 void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
268 input->key_press(key, mod, ch);
272 if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
279 manipulator->start_move();
284 manipulator->start_rotate();
289 manipulator->duplicate();
290 manipulator->start_move();
295 input=new Input(*this, "Filename");
296 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
297 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
300 else if(key==SDLK_PLUS)
301 selection->select_more();
302 else if(key==SDLK_l && (mod&KMOD_SHIFT))
304 const TrackSeq &tracks=layout->get_tracks();
306 for(TrackSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
307 len+=(*i)->get_total_length();
308 cout<<"Total length: "<<len<<"m\n";
311 selection->select_linked();
319 manipulator->start_elevate();
322 else if(key==SDLK_ESCAPE)
325 manipulator->cancel();
326 else if(mode==CATALOGUE)
333 Selection::TrackSet tracks=selection->get_tracks();
335 for(Selection::TrackSet::iterator i=tracks.begin(); i!=tracks.end(); ++i)
337 layout->remove_track(*i);
341 else if(key==SDLK_f && (mod&KMOD_SHIFT))
343 const Selection::TrackSet &tracks=selection->get_tracks();
344 const TrackSeq <racks=layout->get_tracks();
345 for(Selection::TrackSet::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
347 (*i)->set_flex(!(*i)->get_flex());
349 for(TrackSeq::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
351 (*i)->snap_to(**j, true);
353 Track3D *t3d=layout_3d->get_track(*i);
355 t3d->set_color(Color(1, 0.5, 1));
357 t3d->set_color(Color(1, 1, 1));
361 manipulator->flatten();
362 else if(key==SDLK_e && (mod&KMOD_SHIFT))
363 manipulator->even_slope(true);
365 manipulator->even_slope();
368 Track *track=selection->get_track();
369 if(selection->size()==1 && track->get_n_routes()>1)
372 ss<<track->get_turnout_id();
373 input=new Input(*this, "Turnout ID", ss.str());
374 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
375 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
381 Track *track=selection->get_track();
382 if(selection->size()==1 && track->get_n_routes()==1)
385 ss<<track->get_sensor_id();
386 input=new Input(*this, "Sensor ID", ss.str());
387 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
388 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
392 else if(key==SDLK_RIGHT)
394 else if(key==SDLK_LEFT)
396 else if(key==SDLK_UP)
398 else if(key==SDLK_DOWN)
400 else if(key==SDLK_INSERT)
402 else if(key==SDLK_PAGEUP)
404 else if(key==SDLK_HOME)
406 else if(key==SDLK_END)
408 else if(key==SDLK_DELETE)
410 else if(key==SDLK_PAGEDOWN)
414 void Designer::key_release(unsigned key, unsigned)
419 if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
421 else if(key==SDLK_RIGHT || key==SDLK_LEFT)
423 else if(key==SDLK_UP || key==SDLK_DOWN)
425 else if(key==SDLK_INSERT || key==SDLK_PAGEUP)
427 else if(key==SDLK_HOME || key==SDLK_END)
429 else if(key==SDLK_DELETE || key==SDLK_PAGEDOWN)
433 void Designer::button_press(int x, int y, float gx, float gy, unsigned btn)
439 Track3D *ctrack=pick_track(x, y);
442 Track *track=ctrack->get_track().copy();
443 track->set_position(Point(gx, gy, 0));
444 layout->add_track(track);
447 selection->add_track(track);
455 else if(mode==SELECT)
459 Track3D *track=pick_track(x, y);
464 selection->toggle_track(&track->get_track());
468 else if(mode==MANIPULATE)
469 manipulator->button_press(x, y, gx, gy, btn);
470 else if(mode==MEASURE)
471 measure->button_press(x, y, gx, gy, btn);
474 void Designer::pointer_motion(int x, int y, float gx, float gy)
476 if(mode==SELECT || mode==CATALOGUE)
480 tooltip_timeout=Time::now()+100*Time::msec;
485 manipulator->pointer_motion(x, y, gx, gy);
486 measure->pointer_motion(x, y, gx, gy);
489 if(mode==MEASURE || mode==MANIPULATE)
493 void Designer::project_3d()
495 glMatrixMode(GL_PROJECTION);
497 glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
498 glMatrixMode(GL_MODELVIEW);
501 void Designer::apply_camera()
505 glTranslatef(0, 0, -1);
508 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
509 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
510 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
514 void Designer::render()
516 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
517 glEnable(GL_DEPTH_TEST);
522 cat_layout_3d->render();
525 layout_3d->render(true);
526 manipulator->render();
532 glColor4f(1, 0, 0, 1);
534 glVertex3f(0.5, 0, 0);
535 glColor4f(0, 1, 0, 1);
537 glVertex3f(0, 0.5, 0);
538 glColor4f(0, 0, 1, 1);
540 glVertex3f(0, 0, 0.5);
543 glMatrixMode(GL_PROJECTION);
545 glOrtho(0, screen_w, 0, screen_h, 0, 1);
546 glMatrixMode(GL_MODELVIEW);
549 glDisable(GL_DEPTH_TEST);
553 glTranslatef(tooltip_x, tooltip_y, 0);
554 glScalef(20, 20, 20);
555 float width=font->get_string_width(tooltip);
556 glColor4f(0, 0, 0, 0.5);
559 glVertex2f(width, 0);
560 glVertex2f(width, 1);
563 glColor4f(1, 1, 1, 1);
564 font->draw_string(tooltip);
571 Track3D *Designer::pick_track(int x, int y)
573 Layout3D *l=layout_3d;
577 float xx=((float)(x-(int)screen_w/2)/screen_h)*0.82843;
578 float yy=((float)y/screen_h-0.5)*0.82843;
579 float size=(float)4/screen_h*0.82843;
584 return l->pick_track(xx, yy, size);
587 void Designer::manipulation_status(const string &status)
589 tooltip=decode<Utf8>(status);
592 void Designer::manipulation_done(bool)
597 void Designer::measure_changed()
599 float pard=measure->get_parallel_distance()*1000;
600 float perpd=measure->get_perpendicular_distance()*1000;
601 float d=sqrt(pard*pard+perpd*perpd);
602 float adiff=measure->get_angle_difference()*180/M_PI;
605 ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
606 tooltip=decode<Utf8>(ss.str());
609 void Designer::measure_done()
614 void Designer::move_tooltip(int x, int y)
616 int w=(int)(font->get_string_width(tooltip)*20);
617 tooltip_x=max(min((int)screen_w-w, x), 0);
618 tooltip_y=max(min((int)screen_h-20, y), 0);
621 void Designer::save_accept()
623 layout->save(input->get_text());
628 void Designer::turnout_id_accept()
630 Track *track=selection->get_track();
631 unsigned id=lexical_cast<unsigned>(input->get_text());
632 track->set_turnout_id(id);
634 Track3D *t3d=layout_3d->get_track(track);
636 t3d->set_color(Color(0.5, 1, 1));
638 t3d->set_color(Color(1, 1, 1));
643 void Designer::sensor_id_accept()
645 Track *track=selection->get_track();
646 unsigned id=lexical_cast<unsigned>(input->get_text());
647 track->set_sensor_id(id);
649 Track3D *t3d=layout_3d->get_track(track);
651 t3d->set_color(Color(1, 1, 0.5));
653 t3d->set_color(Color(1, 1, 1));
658 void Designer::input_dismiss()
665 Application::RegApp<Designer> Designer::reg;