3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
13 #include <msp/gl/rendermode.h>
14 #include <msp/gl/select.h>
15 #include <msp/gl/texture2d.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):
40 cam_pos(0, -0.5, 0.5),
48 catalogue.load("tracks.dat");
50 cat_layout=new Layout(catalogue);
51 cat_layout_3d=new Layout3D(*cat_layout);
53 const map<unsigned, TrackType *> &ctracks=catalogue.get_tracks();
55 for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
57 Track *track=new Track(*i->second);
58 track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
59 track->set_rotation(M_PI/2);
60 cat_layout->add_track(*track);
63 manipulator=new Manipulator(*this);
64 manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
65 manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
67 layout=new Layout(catalogue);
68 layout_3d=new Layout3D(*layout);
72 layout->load(argv[1]);
73 const list<Track3D *> <racks=layout_3d->get_tracks();
74 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
76 if((*i)->get_track().get_sensor_id())
77 (*i)->set_color(GL::Color(1, 1, 0.5));
78 else if((*i)->get_track().get_turnout_id())
79 (*i)->set_color(GL::Color(0.5, 1, 1));
80 else if((*i)->get_track().get_flex())
81 (*i)->set_color(GL::Color(1, 0.5, 1));
85 selection=new Selection;
86 manipulator->set_selection(selection);
88 measure=new Measure(*this);
89 measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
90 measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
100 delete cat_layout_3d;
106 setenv("__GL_SYNC_TO_VBLANK", "1", 0);
107 SDL_Init(SDL_INIT_VIDEO);
108 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
109 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
110 SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL);
111 SDL_EnableUNICODE(1);
113 glEnableClientState(GL_VERTEX_ARRAY);
114 glEnable(GL_DEPTH_TEST);
116 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
117 glEnable(GL_CULL_FACE);
119 GL::Texture2D *font_tex=new GL::Texture2D;
120 font_tex->set_min_filter(GL::LINEAR);
121 font_tex->load_image("dejavu-20.png");
123 font->set_texture(*font_tex);
124 DataFile::load(*font, "dejavu-20.font");
139 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
141 float cos_pitch=cos(cam_pitch);
142 float sin_pitch=sin(cam_pitch);
143 float cos_yaw=cos(cam_yaw);
144 float sin_yaw=sin(cam_yaw);
146 float rx=sin_yaw*0.55228;
147 float ry=-cos_yaw*0.55228;
149 float ux=cos_yaw*-sin_pitch*0.41421;
150 float uy=sin_yaw*-sin_pitch*0.41421;
151 float uz=cos_pitch*0.41421;
153 float xf=static_cast<float>(x)*2/screen_w-1;
154 float yf=1-static_cast<float>(y)*2/screen_h;
156 float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
157 float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
158 float vz=sin_pitch + yf*uz;
160 gx=cam_pos.x-vx*cam_pos.z/vz;
161 gy=cam_pos.y-vy*cam_pos.z/vz;
164 void Designer::tick()
167 while(SDL_PollEvent(&event))
172 case SDL_MOUSEBUTTONDOWN:
173 map_pointer_coords(event.button.x, event.button.y, gx, gy);
174 button_press(event.button.x, screen_h-1-event.button.y, gx, gy, event.button.button);
176 case SDL_MOUSEMOTION:
177 map_pointer_coords(event.motion.x, event.motion.y, gx, gy);
178 pointer_motion(event.motion.x, screen_h-1-event.motion.y, gx, gy);
181 key_press(event.key.keysym.sym, event.key.keysym.mod, event.key.keysym.unicode);
184 key_release(event.key.keysym.sym, event.key.keysym.mod);
192 const Time::TimeStamp t=Time::now();
193 float dt=(t-last_tick)/Time::sec;
198 cam_pos.x+=cos(cam_yaw)*dt*move_y;
199 cam_pos.y+=sin(cam_yaw)*dt*move_y;
203 cam_pos.x+=sin(cam_yaw)*dt*move_x;
204 cam_pos.y+=-cos(cam_yaw)*dt*move_x;
208 cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
209 cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
210 cam_pos.z+=sin(cam_pitch)*dt*zoom;
214 float vx=cos(cam_yaw)*cos(cam_pitch);
215 float vy=sin(cam_yaw)*cos(cam_pitch);
216 float vz=sin(cam_pitch);
218 float gx=cam_pos.x-vx*cam_pos.z/vz;
219 float gy=cam_pos.y-vy*cam_pos.z/vz;
220 float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
222 cam_yaw+=M_PI*dt*rotate;
228 cam_pos.x=gx+cos(cam_yaw)*d;
229 cam_pos.y=gy+sin(cam_yaw)*d;
233 cam_pitch+=M_PI/2*dt*pitch;
234 if(cam_pitch>M_PI/12)
236 else if(cam_pitch<-M_PI/2)
240 if(tooltip_timeout && t>tooltip_timeout)
245 t3d=pick_track(pointer_x, pointer_y);
247 t3d=pick_track(pointer_x, pointer_y);
251 const Track &track=t3d->get_track();
252 const TrackType &ttype=track.get_type();
255 ss<<ttype.get_article_number()<<' '<<ttype.get_description();
257 ss<<" (slope "<<track.get_slope()/ttype.get_total_length()*100<<"%)";
258 if(track.get_turnout_id())
259 ss<<" (turnout "<<track.get_turnout_id()<<')';
260 else if(track.get_sensor_id())
261 ss<<" (sensor "<<track.get_sensor_id()<<')';
264 move_tooltip(pointer_x, pointer_y);
269 tooltip_timeout=Time::TimeStamp();
274 SDL_GL_SwapBuffers();
279 void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
283 input->key_press(key, mod, ch);
287 if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
294 manipulator->start_move();
299 manipulator->start_rotate();
304 manipulator->duplicate();
305 manipulator->start_move();
310 input=new Input(*this, "Filename");
311 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
312 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
315 else if(key==SDLK_PLUS)
316 selection->select_more();
317 else if(key==SDLK_l && (mod&KMOD_SHIFT))
319 const set<Track *> &tracks=layout->get_tracks();
321 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
322 len+=(*i)->get_type().get_total_length();
323 cout<<"Total length: "<<len<<"m\n";
326 selection->select_linked();
334 manipulator->start_elevate();
337 else if(key==SDLK_ESCAPE)
340 manipulator->cancel();
341 else if(mode==CATALOGUE)
348 set<Track *> tracks=selection->get_tracks();
350 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
352 layout->remove_track(**i);
356 else if(key==SDLK_f && (mod&KMOD_SHIFT))
358 const set<Track *> &tracks=selection->get_tracks();
359 const set<Track *> <racks=layout->get_tracks();
360 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
362 (*i)->set_flex(!(*i)->get_flex());
364 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
366 (*i)->snap_to(**j, true);
368 Track3D &t3d=layout_3d->get_track(**i);
370 t3d.set_color(GL::Color(1, 0.5, 1));
372 t3d.set_color(GL::Color(1, 1, 1));
376 manipulator->flatten();
377 else if(key==SDLK_e && (mod&KMOD_SHIFT))
378 manipulator->even_slope(true);
380 manipulator->even_slope();
383 Track *track=selection->get_track();
384 if(selection->size()==1 && track->get_type().get_n_routes()>1)
387 ss<<track->get_turnout_id();
388 input=new Input(*this, "Turnout 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::turnout_id_accept));
396 Track *track=selection->get_track();
397 if(selection->size()==1 && track->get_type().get_n_routes()==1)
400 ss<<track->get_sensor_id();
401 input=new Input(*this, "Sensor ID", ss.str());
402 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
403 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
407 else if(key==SDLK_RIGHT)
409 else if(key==SDLK_LEFT)
411 else if(key==SDLK_UP)
413 else if(key==SDLK_DOWN)
415 else if(key==SDLK_INSERT)
417 else if(key==SDLK_PAGEUP)
419 else if(key==SDLK_HOME)
421 else if(key==SDLK_END)
423 else if(key==SDLK_DELETE)
425 else if(key==SDLK_PAGEDOWN)
429 void Designer::key_release(unsigned key, unsigned)
434 if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
436 else if(key==SDLK_RIGHT || key==SDLK_LEFT)
438 else if(key==SDLK_UP || key==SDLK_DOWN)
440 else if(key==SDLK_INSERT || key==SDLK_PAGEUP)
442 else if(key==SDLK_HOME || key==SDLK_END)
444 else if(key==SDLK_DELETE || key==SDLK_PAGEDOWN)
448 void Designer::button_press(int x, int y, float gx, float gy, unsigned btn)
454 Track3D *ctrack=pick_track(x, y);
457 Track *track=ctrack->get_track().copy();
458 track->set_position(Point(gx, gy, 0));
459 layout->add_track(*track);
462 selection->add_track(track);
470 else if(mode==SELECT)
474 Track3D *track=pick_track(x, y);
479 selection->toggle_track(&track->get_track());
483 else if(mode==MANIPULATE)
484 manipulator->button_press(x, y, gx, gy, btn);
485 else if(mode==MEASURE)
486 measure->button_press(x, y, gx, gy, btn);
489 void Designer::pointer_motion(int x, int y, float gx, float gy)
491 if(mode==SELECT || mode==CATALOGUE)
495 tooltip_timeout=Time::now()+100*Time::msec;
500 manipulator->pointer_motion(x, y, gx, gy);
501 measure->pointer_motion(x, y, gx, gy);
504 if(mode==MEASURE || mode==MANIPULATE)
508 void Designer::project_3d()
510 glMatrixMode(GL_PROJECTION);
512 glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
513 glMatrixMode(GL_MODELVIEW);
516 void Designer::apply_camera()
520 glTranslatef(0, 0, -1);
523 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
524 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
525 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
529 void Designer::render()
531 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
532 glEnable(GL_DEPTH_TEST);
537 cat_layout_3d->render();
540 layout_3d->render(true);
541 manipulator->render();
546 glMatrixMode(GL_PROJECTION);
548 glOrtho(0, screen_w, 0, screen_h, 0, 1);
549 glMatrixMode(GL_MODELVIEW);
552 glDisable(GL_DEPTH_TEST);
556 glTranslatef(tooltip_x, tooltip_y, 0);
557 glScalef(20, 20, 20);
558 float width=font->get_string_width(tooltip);
559 glColor4f(0, 0, 0, 0.5);
562 glVertex2f(width, 0);
563 glVertex2f(width, 1);
566 glColor4f(1, 1, 1, 1);
567 font->draw_string(tooltip);
574 Track3D *Designer::pick_track(int x, int y)
576 Layout3D *l=layout_3d;
580 float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
581 float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
582 float size=4.0/screen_h*0.82843;
587 return l->pick_track(xx, yy, size);
590 void Designer::manipulation_status(const string &status)
595 void Designer::manipulation_done(bool)
600 void Designer::measure_changed()
602 float pard=measure->get_parallel_distance()*1000;
603 float perpd=measure->get_perpendicular_distance()*1000;
604 float d=sqrt(pard*pard+perpd*perpd);
605 float adiff=measure->get_angle_difference()*180/M_PI;
608 ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
612 void Designer::measure_done()
617 void Designer::move_tooltip(int x, int y)
619 int w=static_cast<int>(font->get_string_width(tooltip)*20);
620 tooltip_x=max(min(static_cast<int>(screen_w)-w, x), 0);
621 tooltip_y=max(min(static_cast<int>(screen_h)-20, y), 0);
624 void Designer::save_accept()
626 layout->save(input->get_text());
631 void Designer::turnout_id_accept()
633 Track *track=selection->get_track();
634 unsigned id=lexical_cast<unsigned>(input->get_text());
635 track->set_turnout_id(id);
637 Track3D &t3d=layout_3d->get_track(*track);
639 t3d.set_color(GL::Color(0.5, 1, 1));
641 t3d.set_color(GL::Color(1, 1, 1));
646 void Designer::sensor_id_accept()
648 Track *track=selection->get_track();
649 unsigned id=lexical_cast<unsigned>(input->get_text());
650 track->set_sensor_id(id);
652 Track3D &t3d=layout_3d->get_track(*track);
654 t3d.set_color(GL::Color(1, 1, 0.5));
656 t3d.set_color(GL::Color(1, 1, 1));
661 void Designer::input_dismiss()
668 Application::RegApp<Designer> Designer::reg;