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