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