]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Style fixes, including:
[r2c2.git] / source / designer / designer.cpp
1 #include <signal.h>
2 #include <cmath>
3 #include <iostream>
4 #include <GL/gl.h>
5 #include <SDL.h>
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"
16 #include "designer.h"
17 #include "input.h"
18 #include "manipulator.h"
19 #include "measure.h"
20 #include "selection.h"
21
22 using namespace std;
23 using namespace Marklin;
24 using namespace Msp;
25
26 Designer::Designer(int argc, char **argv):
27         screen_w(1280),
28         screen_h(960),
29         input(0),
30         mode(SELECT),
31         cam_yaw(M_PI/2),
32         cam_pitch(-M_PI/4),
33         cam_pos(0, -0.5, 0.5),
34         shift(false),
35         move_x(0),
36         move_y(0),
37         zoom(0),
38         rotate(0),
39         pitch(0)
40 {
41         catalogue.load("tracks.dat");
42
43         cat_layout=new Layout(catalogue);
44         cat_layout_3d=new Layout3D(*cat_layout);
45
46         const map<unsigned, TrackType *> &ctracks=catalogue.get_tracks();
47         unsigned n=0;
48         for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
49         {
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);
54         }
55
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));
59
60         layout=new Layout(catalogue);
61         layout_3d=new Layout3D(*layout);
62
63         if(argc>1)
64         {
65                 layout->load(argv[1]);
66                 const list<Track3D *> &ltracks=layout_3d->get_tracks();
67                 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
68                 {
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));
75                 }
76         }
77
78         selection=new Selection;
79         manipulator->set_selection(selection);
80
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));
84 }
85
86 Designer::~Designer()
87 {
88         delete manipulator;
89         delete selection;
90         delete layout;
91         delete layout_3d;
92         delete cat_layout;
93         delete cat_layout_3d;
94         delete measure;
95 }
96
97 int Designer::main()
98 {
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);
105
106         glEnableClientState(GL_VERTEX_ARRAY);
107         glEnable(GL_DEPTH_TEST);
108         glEnable(GL_BLEND);
109         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
110         glEnable(GL_CULL_FACE);
111
112         GL::Texture2D *font_tex=new GL::Texture2D;
113         font_tex->set_min_filter(GL::LINEAR);
114         font_tex->load_image("dejavu-20.png");
115         font=new GL::Font();
116         font->set_texture(*font_tex);
117         DataFile::load(*font, "dejavu-20.font");
118
119         mode=SELECT;
120
121         Application::main();
122
123         delete font;
124         delete font_tex;
125         delete input;
126
127         SDL_Quit();
128
129         return exit_code;
130 }
131
132 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
133 {
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);
138
139         float rx=sin_yaw*0.55228;
140         float ry=-cos_yaw*0.55228;
141
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;
145
146         float xf=static_cast<float>(x)*2/screen_w-1;
147         float yf=1-static_cast<float>(y)*2/screen_h;
148
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;
152
153         gx=cam_pos.x-vx*cam_pos.z/vz;
154         gy=cam_pos.y-vy*cam_pos.z/vz;
155 }
156
157 void Designer::tick()
158 {
159         SDL_Event event;
160         while(SDL_PollEvent(&event))
161         {
162                 float gx, gy;
163                 switch(event.type)
164                 {
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);
168                         break;
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);
172                         break;
173                 case SDL_KEYDOWN:
174                         key_press(event.key.keysym.sym, event.key.keysym.mod, event.key.keysym.unicode);
175                         break;
176                 case SDL_KEYUP:
177                         key_release(event.key.keysym.sym, event.key.keysym.mod);
178                         break;
179                 case SDL_QUIT:
180                         exit(0);
181                         break;
182                 }
183         }
184
185         const Time::TimeStamp t=Time::now();
186         float dt=(t-last_tick)/Time::sec;
187         last_tick=t;
188
189         if(move_y)
190         {
191                 cam_pos.x+=cos(cam_yaw)*dt*move_y;
192                 cam_pos.y+=sin(cam_yaw)*dt*move_y;
193         }
194         if(move_x)
195         {
196                 cam_pos.x+=sin(cam_yaw)*dt*move_x;
197                 cam_pos.y+=-cos(cam_yaw)*dt*move_x;
198         }
199         if(zoom)
200         {
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;
204         }
205         if(rotate)
206         {
207                 float vx=cos(cam_yaw)*cos(cam_pitch);
208                 float vy=sin(cam_yaw)*cos(cam_pitch);
209                 float vz=sin(cam_pitch);
210
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;
214
215                 cam_yaw+=M_PI*dt*rotate;
216                 if(cam_yaw>M_PI*2)
217                         cam_yaw-=M_PI*2;
218                 else if(cam_yaw<0)
219                         cam_yaw+=M_PI*2;
220
221                 cam_pos.x=gx+cos(cam_yaw)*d;
222                 cam_pos.y=gy+sin(cam_yaw)*d;
223         }
224         if(pitch)
225         {
226                 cam_pitch+=M_PI/2*dt*pitch;
227                 if(cam_pitch>M_PI/12)
228                         cam_pitch=M_PI/12;
229                 else if(cam_pitch<-M_PI/2)
230                         cam_pitch=-M_PI/2;
231         }
232
233         if(tooltip_timeout && t>tooltip_timeout)
234         {
235                 Track3D *t3d=0;
236
237                 if(mode==CATALOGUE)
238                         t3d=pick_track(pointer_x, pointer_y);
239                 else
240                         t3d=pick_track(pointer_x, pointer_y);
241
242                 if(t3d)
243                 {
244                         const Track &track=t3d->get_track();
245                         const TrackType &ttype=track.get_type();
246                         ostringstream ss;
247                         ss.precision(2);
248                         ss<<ttype.get_article_number()<<' '<<ttype.get_description();
249                         if(mode!=CATALOGUE)
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()<<')';
255                         tooltip=ss.str();
256
257                         move_tooltip(pointer_x, pointer_y);
258                 }
259                 else
260                         tooltip="";
261
262                 tooltip_timeout=Time::TimeStamp();
263         }
264
265         render();
266
267         SDL_GL_SwapBuffers();
268 }
269
270 /*** private ***/
271
272 void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
273 {
274         if(mode==INPUT)
275         {
276                 input->key_press(key, mod, ch);
277                 return;
278         }
279
280         if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
281                 shift=true;
282
283         if(key==SDLK_n)
284                 mode=CATALOGUE;
285         else if(key==SDLK_g)
286         {
287                 manipulator->start_move();
288                 mode=MANIPULATE;
289         }
290         else if(key==SDLK_r)
291         {
292                 manipulator->start_rotate();
293                 mode=MANIPULATE;
294         }
295         else if(key==SDLK_d)
296         {
297                 manipulator->duplicate();
298                 manipulator->start_move();
299                 mode=MANIPULATE;
300         }
301         else if(key==SDLK_w)
302         {
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));
306                 mode=INPUT;
307         }
308         else if(key==SDLK_PLUS)
309                 selection->select_more();
310         else if(key==SDLK_l && (mod&KMOD_SHIFT))
311         {
312                 const set<Track *> &tracks=layout->get_tracks();
313                 float len=0;
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";
317         }
318         else if(key==SDLK_l)
319                 selection->select_linked();
320         else if(key==SDLK_m)
321         {
322                 measure->start();
323                 mode=MEASURE;
324         }
325         else if(key==SDLK_z)
326         {
327                 manipulator->start_elevate();
328                 mode=MANIPULATE;
329         }
330         else if(key==SDLK_ESCAPE)
331         {
332                 if(mode==MANIPULATE)
333                         manipulator->cancel();
334                 else if(mode==CATALOGUE)
335                         mode=SELECT;
336                 else
337                         selection->clear();
338         }
339         else if(key==SDLK_x)
340         {
341                 set<Track *> tracks=selection->get_tracks();
342                 selection->clear();
343                 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
344                 {
345                         layout->remove_track(**i);
346                         delete *i;
347                 }
348         }
349         else if(key==SDLK_f && (mod&KMOD_SHIFT))
350         {
351                 const set<Track *> &tracks=selection->get_tracks();
352                 const set<Track *> &ltracks=layout->get_tracks();
353                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
354                 {
355                         (*i)->set_flex(!(*i)->get_flex());
356                         (*i)->break_links();
357                         for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
358                                 if(*j!=*i)
359                                         (*i)->snap_to(**j, true);
360
361                         Track3D &t3d=layout_3d->get_track(**i);
362                         if((*i)->get_flex())
363                                 t3d.set_color(GL::Color(1, 0.5, 1));
364                         else
365                                 t3d.set_color(GL::Color(1, 1, 1));
366                 }
367         }
368         else if(key==SDLK_f)
369                 manipulator->flatten();
370         else if(key==SDLK_e && (mod&KMOD_SHIFT))
371                 manipulator->even_slope(true);
372         else if(key==SDLK_e)
373                 manipulator->even_slope();
374         else if(key==SDLK_t)
375         {
376                 Track *track=selection->get_track();
377                 if(selection->size()==1 && track->get_type().get_n_routes()>1)
378                 {
379                         ostringstream ss;
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));
384                         mode=INPUT;
385                 }
386         }
387         else if(key==SDLK_s)
388         {
389                 Track *track=selection->get_track();
390                 if(selection->size()==1 && track->get_type().get_n_routes()==1)
391                 {
392                         ostringstream ss;
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));
397                         mode=INPUT;
398                 }
399         }
400         else if(key==SDLK_RIGHT)
401                 rotate=-1;
402         else if(key==SDLK_LEFT)
403                 rotate=1;
404         else if(key==SDLK_UP)
405                 move_y=1;
406         else if(key==SDLK_DOWN)
407                 move_y=-1;
408         else if(key==SDLK_INSERT)
409                 zoom=-1;
410         else if(key==SDLK_PAGEUP)
411                 zoom=1;
412         else if(key==SDLK_HOME)
413                 pitch=1;
414         else if(key==SDLK_END)
415                 pitch=-1;
416         else if(key==SDLK_DELETE)
417                 move_x=-1;
418         else if(key==SDLK_PAGEDOWN)
419                 move_x=1;
420 }
421
422 void Designer::key_release(unsigned key, unsigned)
423 {
424         if(mode==INPUT)
425                 return;
426
427         if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
428                 shift=false;
429         else if(key==SDLK_RIGHT || key==SDLK_LEFT)
430                 rotate=0;
431         else if(key==SDLK_UP || key==SDLK_DOWN)
432                 move_y=0;
433         else if(key==SDLK_INSERT || key==SDLK_PAGEUP)
434                 zoom=0;
435         else if(key==SDLK_HOME || key==SDLK_END)
436                 pitch=0;
437         else if(key==SDLK_DELETE || key==SDLK_PAGEDOWN)
438                 move_x=0;
439 }
440
441 void Designer::button_press(int x, int y, float gx, float gy, unsigned btn)
442 {
443         if(mode==CATALOGUE)
444         {
445                 if(btn==1)
446                 {
447                         Track3D *ctrack=pick_track(x, y);
448                         if(ctrack)
449                         {
450                                 Track *track=ctrack->get_track().copy();
451                                 track->set_position(Point(gx, gy, 0));
452                                 layout->add_track(*track);
453
454                                 selection->clear();
455                                 selection->add_track(track);
456
457                                 mode=SELECT;
458                         }
459                 }
460                 else
461                         mode=SELECT;
462         }
463         else if(mode==SELECT)
464         {
465                 if(btn==1)
466                 {
467                         Track3D *track=pick_track(x, y);
468                         if(track)
469                         {
470                                 if(!shift)
471                                         selection->clear();
472                                 selection->toggle_track(&track->get_track());
473                         }
474                 }
475         }
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);
480 }
481
482 void Designer::pointer_motion(int x, int y, float gx, float gy)
483 {
484         if(mode==SELECT || mode==CATALOGUE)
485         {
486                 pointer_x=x;
487                 pointer_y=y;
488                 tooltip_timeout=Time::now()+100*Time::msec;
489         }
490
491         if(mode!=INPUT)
492         {
493                 manipulator->pointer_motion(x, y, gx, gy);
494                 measure->pointer_motion(x, y, gx, gy);
495         }
496
497         if(mode==MEASURE || mode==MANIPULATE)
498                 move_tooltip(x, y);
499 }
500
501 void Designer::project_3d()
502 {
503         glMatrixMode(GL_PROJECTION);
504         glLoadIdentity();
505         glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
506         glMatrixMode(GL_MODELVIEW);
507 }
508
509 void Designer::apply_camera()
510 {
511         glLoadIdentity();
512         if(mode==CATALOGUE)
513                 glTranslatef(0, 0, -1);
514         else
515         {
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);
519         }
520 }
521
522 void Designer::render()
523 {
524         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
525         glEnable(GL_DEPTH_TEST);
526
527         project_3d();
528         apply_camera();
529         if(mode==CATALOGUE)
530                 cat_layout_3d->render();
531         else
532         {
533                 layout_3d->render(true);
534                 manipulator->render();
535                 if(mode==MEASURE)
536                         measure->render();
537         }
538
539         glMatrixMode(GL_PROJECTION);
540         glLoadIdentity();
541         glOrtho(0, screen_w, 0, screen_h, 0, 1);
542         glMatrixMode(GL_MODELVIEW);
543         glLoadIdentity();
544
545         glDisable(GL_DEPTH_TEST);
546
547         if(!tooltip.empty())
548         {
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);
553                 glBegin(GL_QUADS);
554                 glVertex2f(0, 0);
555                 glVertex2f(width, 0);
556                 glVertex2f(width, 1);
557                 glVertex2f(0, 1);
558                 glEnd();
559                 glColor4f(1, 1, 1, 1);
560                 font->draw_string(tooltip);
561         }
562
563         if(mode==INPUT)
564                 input->render();
565 }
566
567 Track3D *Designer::pick_track(int x, int y)
568 {
569         Layout3D *l=layout_3d;
570         if(mode==CATALOGUE)
571                 l=cat_layout_3d;
572
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;
576
577         project_3d();
578         apply_camera();
579
580         return l->pick_track(xx, yy, size);
581 }
582
583 void Designer::manipulation_status(const string &status)
584 {
585         tooltip=status;
586 }
587
588 void Designer::manipulation_done(bool)
589 {
590         mode=SELECT;
591 }
592
593 void Designer::measure_changed()
594 {
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;
599         ostringstream ss;
600         ss.precision(3);
601         ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
602         tooltip=ss.str();
603 }
604
605 void Designer::measure_done()
606 {
607         mode=SELECT;
608 }
609
610 void Designer::move_tooltip(int x, int y)
611 {
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);
615 }
616
617 void Designer::save_accept()
618 {
619         layout->save(input->get_text());
620
621         input_dismiss();
622 }
623
624 void Designer::turnout_id_accept()
625 {
626         Track *track=selection->get_track();
627         unsigned id=lexical_cast<unsigned>(input->get_text());
628         track->set_turnout_id(id);
629
630         Track3D &t3d=layout_3d->get_track(*track);
631         if(id)
632                 t3d.set_color(GL::Color(0.5, 1, 1));
633         else
634                 t3d.set_color(GL::Color(1, 1, 1));
635
636         input_dismiss();
637 }
638
639 void Designer::sensor_id_accept()
640 {
641         Track *track=selection->get_track();
642         unsigned id=lexical_cast<unsigned>(input->get_text());
643         track->set_sensor_id(id);
644
645         Track3D &t3d=layout_3d->get_track(*track);
646         if(id)
647                 t3d.set_color(GL::Color(1, 1, 0.5));
648         else
649                 t3d.set_color(GL::Color(1, 1, 1));
650
651         input_dismiss();
652 }
653
654 void Designer::input_dismiss()
655 {
656         delete input;
657         input=0;
658         mode=SELECT;
659 }
660
661 Application::RegApp<Designer> Designer::reg;