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),
40 catalogue.load("tracks.dat");
41 cout<<catalogue.get_tracks().size()<<'\n';
43 cat_layout=new Layout(catalogue);
44 cat_layout_3d=new Layout3D(*cat_layout);
46 const Catalogue::TrackMap &ctracks=catalogue.get_tracks();
48 for(Catalogue::TrackMap::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
50 Track *track=i->second->copy();
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);
102 Parser::load(*font, "dejavu.font");
116 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
118 float cos_pitch=cos(cam_pitch);
119 float sin_pitch=sin(cam_pitch);
120 float cos_yaw=cos(cam_yaw);
121 float sin_yaw=sin(cam_yaw);
123 float rx=sin_yaw*0.55228;
124 float ry=-cos_yaw*0.55228;
126 float ux=cos_yaw*-sin_pitch*0.41421;
127 float uy=sin_yaw*-sin_pitch*0.41421;
128 float uz=cos_pitch*0.41421;
130 float xf=(float)x*2/screen_w-1;
131 float yf=1-(float)y*2/screen_h;
133 float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
134 float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
135 float vz=sin_pitch + yf*uz;
137 gx=cam_pos.x-vx*cam_pos.z/vz;
138 gy=cam_pos.y-vy*cam_pos.z/vz;
141 void Designer::tick()
144 while(SDL_PollEvent(&event))
149 case SDL_MOUSEBUTTONDOWN:
150 map_pointer_coords(event.button.x, event.button.y, gx, gy);
151 button_press(event.button.x, screen_h-1-event.button.y, gx, gy, event.button.button);
153 case SDL_MOUSEMOTION:
154 map_pointer_coords(event.motion.x, event.motion.y, gx, gy);
155 pointer_motion(event.motion.x, screen_h-1-event.motion.y, gx, gy);
158 key_press(event.key.keysym.sym, event.key.keysym.mod, event.key.keysym.unicode);
161 key_release(event.key.keysym.sym, event.key.keysym.mod);
169 const Time::TimeStamp t=Time::now();
170 float dt=(t-last_tick)/Time::sec;
175 cam_pos.x+=cos(cam_yaw)*dt*move_y;
176 cam_pos.y+=sin(cam_yaw)*dt*move_y;
180 cam_pos.x+=sin(cam_yaw)*dt*move_x;
181 cam_pos.y+=-cos(cam_yaw)*dt*move_x;
185 cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
186 cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
187 cam_pos.z+=sin(cam_pitch)*dt*zoom;
191 float vx=cos(cam_yaw)*cos(cam_pitch);
192 float vy=sin(cam_yaw)*cos(cam_pitch);
193 float vz=sin(cam_pitch);
195 float gx=cam_pos.x-vx*cam_pos.z/vz;
196 float gy=cam_pos.y-vy*cam_pos.z/vz;
197 float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
199 cam_yaw+=M_PI*dt*rotate;
205 cam_pos.x=gx+cos(cam_yaw)*d;
206 cam_pos.y=gy+sin(cam_yaw)*d;
210 cam_pitch+=M_PI/2*dt*pitch;
211 if(cam_pitch>M_PI/12)
213 else if(cam_pitch<-M_PI/2)
217 if(tooltip_timeout && t>tooltip_timeout)
222 t3d=pick_track(pointer_x, pointer_y);
224 t3d=pick_track(pointer_x, pointer_y);
228 const Track &track=t3d->get_track();
231 ss<<track.get_article_number()<<' '<<track.get_description();
233 ss<<" (slope "<<track.get_slope()/track.get_length()*100<<"%)";
234 if(track.get_turnout_id())
235 ss<<" (turnout "<<track.get_turnout_id()<<')';
236 else if(track.get_sensor_id())
237 ss<<" (sensor "<<track.get_sensor_id()<<')';
238 tooltip=decode<Utf8>(ss.str());
240 move_tooltip(pointer_x, pointer_y);
245 tooltip_timeout=Time::TimeStamp();
250 SDL_GL_SwapBuffers();
253 Designer::~Designer()
260 delete cat_layout_3d;
266 void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
270 input->key_press(key, mod, ch);
274 if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
281 manipulator->start_move();
286 manipulator->start_rotate();
291 manipulator->duplicate();
292 manipulator->start_move();
297 input=new Input(*this, "Filename");
298 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
299 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
302 else if(key==SDLK_PLUS)
303 selection->select_more();
304 else if(key==SDLK_l && (mod&KMOD_SHIFT))
306 const TrackSeq &tracks=layout->get_tracks();
308 for(TrackSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
309 len+=(*i)->get_total_length();
310 cout<<"Total length: "<<len<<"m\n";
313 selection->select_linked();
321 manipulator->start_elevate();
324 else if(key==SDLK_ESCAPE)
327 manipulator->cancel();
328 else if(mode==CATALOGUE)
335 Selection::TrackSet tracks=selection->get_tracks();
337 for(Selection::TrackSet::iterator i=tracks.begin(); i!=tracks.end(); ++i)
339 layout->remove_track(*i);
343 else if(key==SDLK_f && (mod&KMOD_SHIFT))
345 const Selection::TrackSet &tracks=selection->get_tracks();
346 const TrackSeq <racks=layout->get_tracks();
347 for(Selection::TrackSet::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
349 (*i)->set_flex(!(*i)->get_flex());
351 for(TrackSeq::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
353 (*i)->snap_to(**j, true);
355 Track3D *t3d=layout_3d->get_track(*i);
357 t3d->set_color(Color(1, 0.5, 1));
359 t3d->set_color(Color(1, 1, 1));
363 manipulator->flatten();
364 else if(key==SDLK_e && (mod&KMOD_SHIFT))
365 manipulator->even_slope(true);
367 manipulator->even_slope();
370 Track *track=selection->get_track();
371 if(selection->size()==1 && track->get_n_routes()>1)
374 ss<<track->get_turnout_id();
375 input=new Input(*this, "Turnout ID", ss.str());
376 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
377 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
383 Track *track=selection->get_track();
384 if(selection->size()==1 && track->get_n_routes()==1)
387 ss<<track->get_sensor_id();
388 input=new Input(*this, "Sensor ID", ss.str());
389 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
390 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
394 else if(key==SDLK_RIGHT)
396 else if(key==SDLK_LEFT)
398 else if(key==SDLK_UP)
400 else if(key==SDLK_DOWN)
402 else if(key==SDLK_INSERT)
404 else if(key==SDLK_PAGEUP)
406 else if(key==SDLK_HOME)
408 else if(key==SDLK_END)
410 else if(key==SDLK_DELETE)
412 else if(key==SDLK_PAGEDOWN)
416 void Designer::key_release(unsigned key, unsigned)
421 if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
423 else if(key==SDLK_RIGHT || key==SDLK_LEFT)
425 else if(key==SDLK_UP || key==SDLK_DOWN)
427 else if(key==SDLK_INSERT || key==SDLK_PAGEUP)
429 else if(key==SDLK_HOME || key==SDLK_END)
431 else if(key==SDLK_DELETE || key==SDLK_PAGEDOWN)
435 void Designer::button_press(int x, int y, float gx, float gy, unsigned btn)
441 Track3D *ctrack=pick_track(x, y);
444 Track *track=ctrack->get_track().copy();
445 track->set_position(Point(gx, gy, 0));
446 layout->add_track(track);
449 selection->add_track(track);
457 else if(mode==SELECT)
461 Track3D *track=pick_track(x, y);
466 selection->toggle_track(&track->get_track());
470 else if(mode==MANIPULATE)
471 manipulator->button_press(x, y, gx, gy, btn);
472 else if(mode==MEASURE)
473 measure->button_press(x, y, gx, gy, btn);
476 void Designer::pointer_motion(int x, int y, float gx, float gy)
478 if(mode==SELECT || mode==CATALOGUE)
482 tooltip_timeout=Time::now()+100*Time::msec;
487 manipulator->pointer_motion(x, y, gx, gy);
488 measure->pointer_motion(x, y, gx, gy);
491 if(mode==MEASURE || mode==MANIPULATE)
495 void Designer::project_3d()
497 glMatrixMode(GL_PROJECTION);
499 glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
500 glMatrixMode(GL_MODELVIEW);
503 void Designer::apply_camera()
507 glTranslatef(0, 0, -1);
510 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
511 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
512 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
516 void Designer::render()
518 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
519 glEnable(GL_DEPTH_TEST);
524 cat_layout_3d->render();
527 layout_3d->render(true);
528 manipulator->render();
534 glColor4f(1, 0, 0, 1);
536 glVertex3f(0.5, 0, 0);
537 glColor4f(0, 1, 0, 1);
539 glVertex3f(0, 0.5, 0);
540 glColor4f(0, 0, 1, 1);
542 glVertex3f(0, 0, 0.5);
545 glMatrixMode(GL_PROJECTION);
547 glOrtho(0, screen_w, 0, screen_h, 0, 1);
548 glMatrixMode(GL_MODELVIEW);
551 glDisable(GL_DEPTH_TEST);
555 glTranslatef(tooltip_x, tooltip_y, 0);
556 glScalef(20, 20, 20);
557 float width=font->get_string_width(tooltip);
558 glColor4f(0, 0, 0, 0.5);
561 glVertex2f(width, 0);
562 glVertex2f(width, 1);
565 glColor4f(1, 1, 1, 1);
566 font->draw_string(tooltip);
573 Track3D *Designer::pick_track(int x, int y)
575 Layout3D *l=layout_3d;
579 float xx=((float)(x-(int)screen_w/2)/screen_h)*0.82843;
580 float yy=((float)y/screen_h-0.5)*0.82843;
581 float size=(float)4/screen_h*0.82843;
586 return l->pick_track(xx, yy, size);
589 void Designer::manipulation_status(const string &status)
591 tooltip=decode<Utf8>(status);
594 void Designer::manipulation_done(bool)
599 void Designer::measure_changed()
601 float pard=measure->get_parallel_distance()*1000;
602 float perpd=measure->get_perpendicular_distance()*1000;
603 float d=sqrt(pard*pard+perpd*perpd);
604 float adiff=measure->get_angle_difference()*180/M_PI;
607 ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
608 tooltip=decode<Utf8>(ss.str());
611 void Designer::measure_done()
616 void Designer::move_tooltip(int x, int y)
618 int w=(int)(font->get_string_width(tooltip)*20);
619 tooltip_x=max(min((int)screen_w-w, x), 0);
620 tooltip_y=max(min((int)screen_h-20, y), 0);
623 void Designer::save_accept()
625 layout->save(input->get_text());
630 void Designer::turnout_id_accept()
632 Track *track=selection->get_track();
633 unsigned id=lexical_cast<unsigned>(input->get_text());
634 track->set_turnout_id(id);
636 Track3D *t3d=layout_3d->get_track(track);
638 t3d->set_color(Color(0.5, 1, 1));
640 t3d->set_color(Color(1, 1, 1));
645 void Designer::sensor_id_accept()
647 Track *track=selection->get_track();
648 unsigned id=lexical_cast<unsigned>(input->get_text());
649 track->set_sensor_id(id);
651 Track3D *t3d=layout_3d->get_track(track);
653 t3d->set_color(Color(1, 1, 0.5));
655 t3d->set_color(Color(1, 1, 1));
660 void Designer::input_dismiss()
667 Application::RegApp<Designer> Designer::reg;