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 list<Track3D *> <racks=layout_3d->get_tracks();
67 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
69 if((*i)->get_track().get_sensor_id())
70 (*i)->set_color(GL::Color(1, 1, 0.5));
71 else if((*i)->get_track().get_turnout_id())
72 (*i)->set_color(GL::Color(0.5, 1, 1));
73 else if((*i)->get_track().get_flex())
74 (*i)->set_color(GL::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));
99 setenv("__GL_SYNC_TO_VBLANK", "1", 0);
100 SDL_Init(SDL_INIT_VIDEO);
101 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
102 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
103 SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL);
104 SDL_EnableUNICODE(1);
106 glEnableClientState(GL_VERTEX_ARRAY);
107 glEnable(GL_DEPTH_TEST);
109 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
110 glEnable(GL_CULL_FACE);
112 GL::Texture2D *font_tex=new GL::Texture2D;
113 font_tex->set_min_filter(GL::LINEAR);
114 font_tex->load_image("dejavu-20.png");
116 font->set_texture(*font_tex);
117 DataFile::load(*font, "dejavu-20.font");
132 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
134 float cos_pitch=cos(cam_pitch);
135 float sin_pitch=sin(cam_pitch);
136 float cos_yaw=cos(cam_yaw);
137 float sin_yaw=sin(cam_yaw);
139 float rx=sin_yaw*0.55228;
140 float ry=-cos_yaw*0.55228;
142 float ux=cos_yaw*-sin_pitch*0.41421;
143 float uy=sin_yaw*-sin_pitch*0.41421;
144 float uz=cos_pitch*0.41421;
146 float xf=static_cast<float>(x)*2/screen_w-1;
147 float yf=1-static_cast<float>(y)*2/screen_h;
149 float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
150 float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
151 float vz=sin_pitch + yf*uz;
153 gx=cam_pos.x-vx*cam_pos.z/vz;
154 gy=cam_pos.y-vy*cam_pos.z/vz;
157 void Designer::tick()
160 while(SDL_PollEvent(&event))
165 case SDL_MOUSEBUTTONDOWN:
166 map_pointer_coords(event.button.x, event.button.y, gx, gy);
167 button_press(event.button.x, screen_h-1-event.button.y, gx, gy, event.button.button);
169 case SDL_MOUSEMOTION:
170 map_pointer_coords(event.motion.x, event.motion.y, gx, gy);
171 pointer_motion(event.motion.x, screen_h-1-event.motion.y, gx, gy);
174 key_press(event.key.keysym.sym, event.key.keysym.mod, event.key.keysym.unicode);
177 key_release(event.key.keysym.sym, event.key.keysym.mod);
185 const Time::TimeStamp t=Time::now();
186 float dt=(t-last_tick)/Time::sec;
191 cam_pos.x+=cos(cam_yaw)*dt*move_y;
192 cam_pos.y+=sin(cam_yaw)*dt*move_y;
196 cam_pos.x+=sin(cam_yaw)*dt*move_x;
197 cam_pos.y+=-cos(cam_yaw)*dt*move_x;
201 cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
202 cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
203 cam_pos.z+=sin(cam_pitch)*dt*zoom;
207 float vx=cos(cam_yaw)*cos(cam_pitch);
208 float vy=sin(cam_yaw)*cos(cam_pitch);
209 float vz=sin(cam_pitch);
211 float gx=cam_pos.x-vx*cam_pos.z/vz;
212 float gy=cam_pos.y-vy*cam_pos.z/vz;
213 float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
215 cam_yaw+=M_PI*dt*rotate;
221 cam_pos.x=gx+cos(cam_yaw)*d;
222 cam_pos.y=gy+sin(cam_yaw)*d;
226 cam_pitch+=M_PI/2*dt*pitch;
227 if(cam_pitch>M_PI/12)
229 else if(cam_pitch<-M_PI/2)
233 if(tooltip_timeout && t>tooltip_timeout)
238 t3d=pick_track(pointer_x, pointer_y);
240 t3d=pick_track(pointer_x, pointer_y);
244 const Track &track=t3d->get_track();
245 const TrackType &ttype=track.get_type();
248 ss<<ttype.get_article_number()<<' '<<ttype.get_description();
250 ss<<" (slope "<<track.get_slope()/ttype.get_total_length()*100<<"%)";
251 if(track.get_turnout_id())
252 ss<<" (turnout "<<track.get_turnout_id()<<')';
253 else if(track.get_sensor_id())
254 ss<<" (sensor "<<track.get_sensor_id()<<')';
257 move_tooltip(pointer_x, pointer_y);
262 tooltip_timeout=Time::TimeStamp();
267 SDL_GL_SwapBuffers();
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(GL::Color(1, 0.5, 1));
365 t3d.set_color(GL::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();
539 glMatrixMode(GL_PROJECTION);
541 glOrtho(0, screen_w, 0, screen_h, 0, 1);
542 glMatrixMode(GL_MODELVIEW);
545 glDisable(GL_DEPTH_TEST);
549 glTranslatef(tooltip_x, tooltip_y, 0);
550 glScalef(20, 20, 20);
551 float width=font->get_string_width(tooltip);
552 glColor4f(0, 0, 0, 0.5);
555 glVertex2f(width, 0);
556 glVertex2f(width, 1);
559 glColor4f(1, 1, 1, 1);
560 font->draw_string(tooltip);
567 Track3D *Designer::pick_track(int x, int y)
569 Layout3D *l=layout_3d;
573 float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
574 float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
575 float size=4.0/screen_h*0.82843;
580 return l->pick_track(xx, yy, size);
583 void Designer::manipulation_status(const string &status)
588 void Designer::manipulation_done(bool)
593 void Designer::measure_changed()
595 float pard=measure->get_parallel_distance()*1000;
596 float perpd=measure->get_perpendicular_distance()*1000;
597 float d=sqrt(pard*pard+perpd*perpd);
598 float adiff=measure->get_angle_difference()*180/M_PI;
601 ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
605 void Designer::measure_done()
610 void Designer::move_tooltip(int x, int y)
612 int w=static_cast<int>(font->get_string_width(tooltip)*20);
613 tooltip_x=max(min(static_cast<int>(screen_w)-w, x), 0);
614 tooltip_y=max(min(static_cast<int>(screen_h)-20, y), 0);
617 void Designer::save_accept()
619 layout->save(input->get_text());
624 void Designer::turnout_id_accept()
626 Track *track=selection->get_track();
627 unsigned id=lexical_cast<unsigned>(input->get_text());
628 track->set_turnout_id(id);
630 Track3D &t3d=layout_3d->get_track(*track);
632 t3d.set_color(GL::Color(0.5, 1, 1));
634 t3d.set_color(GL::Color(1, 1, 1));
639 void Designer::sensor_id_accept()
641 Track *track=selection->get_track();
642 unsigned id=lexical_cast<unsigned>(input->get_text());
643 track->set_sensor_id(id);
645 Track3D &t3d=layout_3d->get_track(*track);
647 t3d.set_color(GL::Color(1, 1, 0.5));
649 t3d.set_color(GL::Color(1, 1, 1));
654 void Designer::input_dismiss()
661 Application::RegApp<Designer> Designer::reg;