]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Support using a mesh as a background
[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 <msp/gl/rendermode.h>
13 #include <msp/gl/select.h>
14 #include <msp/gl/texture2d.h>
15 #include <msp/input/keys.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         base_mesh(0),
37         input(0),
38         mode(SELECT),
39         cam_yaw(M_PI/2),
40         cam_pitch(-M_PI/4),
41         cam_pos(0, -0.5, 0.5),
42         shift(false),
43         move_x(0),
44         move_y(0),
45         zoom(0),
46         rotate(0),
47         pitch(0)
48 {
49         catalogue.load("tracks.dat");
50
51         cat_layout=new Layout(catalogue);
52         cat_layout_3d=new Layout3D(*cat_layout);
53
54         const map<unsigned, TrackType *> &ctracks=catalogue.get_tracks();
55         unsigned n=0;
56         for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
57         {
58                 Track *track=new Track(*i->second);
59                 track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
60                 track->set_rotation(M_PI/2);
61                 cat_layout->add_track(*track);
62         }
63
64         manipulator=new Manipulator(*this);
65         manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
66         manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
67
68         layout=new Layout(catalogue);
69         layout_3d=new Layout3D(*layout);
70
71         if(argc>1)
72         {
73                 layout->load(argv[1]);
74                 const list<Track3D *> &ltracks=layout_3d->get_tracks();
75                 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
76                 {
77                         if((*i)->get_track().get_sensor_id())
78                                 (*i)->set_color(GL::Color(1, 1, 0.5));
79                         else if((*i)->get_track().get_turnout_id())
80                                 (*i)->set_color(GL::Color(0.5, 1, 1));
81                         else if((*i)->get_track().get_flex())
82                                 (*i)->set_color(GL::Color(1, 0.5, 1));
83                 }
84
85                 if(!layout->get_base().empty())
86                 {
87                         base_mesh=new GL::Mesh;
88                         DataFile::load(*base_mesh, layout->get_base());
89                 }
90         }
91
92         selection=new Selection;
93         manipulator->set_selection(selection);
94
95         measure=new Measure(*this);
96         measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
97         measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
98 }
99
100 Designer::~Designer()
101 {
102         delete manipulator;
103         delete selection;
104         delete layout;
105         delete layout_3d;
106         delete cat_layout;
107         delete cat_layout_3d;
108         delete measure;
109 }
110
111 int Designer::main()
112 {
113         dpy=new Graphics::Display;
114         wnd=new Graphics::Window(*dpy, screen_w, screen_h);
115         glc=new Graphics::GLContext(*wnd);
116
117         wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
118         wnd->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
119         wnd->signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release));
120         wnd->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
121         wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
122
123         wnd->show();
124
125         glEnableClientState(GL_VERTEX_ARRAY);
126         glEnable(GL_DEPTH_TEST);
127         glEnable(GL_BLEND);
128         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
129         glEnable(GL_CULL_FACE);
130
131         GL::Texture2D *font_tex=new GL::Texture2D;
132         font_tex->set_min_filter(GL::LINEAR);
133         font_tex->load_image("dejavu-20.png");
134         font=new GL::Font();
135         font->set_texture(*font_tex);
136         DataFile::load(*font, "dejavu-20.font");
137
138         mode=SELECT;
139
140         Application::main();
141
142         delete font;
143         delete font_tex;
144         delete input;
145
146         delete glc;
147         delete wnd;
148         delete dpy;
149
150         return exit_code;
151 }
152
153 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
154 {
155         float cos_pitch=cos(cam_pitch);
156         float sin_pitch=sin(cam_pitch);
157         float cos_yaw=cos(cam_yaw);
158         float sin_yaw=sin(cam_yaw);
159
160         float rx=sin_yaw*0.55228;
161         float ry=-cos_yaw*0.55228;
162
163         float ux=cos_yaw*-sin_pitch*0.41421;
164         float uy=sin_yaw*-sin_pitch*0.41421;
165         float uz=cos_pitch*0.41421;
166
167         float xf=static_cast<float>(x)*2/screen_w-1;
168         float yf=static_cast<float>(y)*2/screen_h-1;
169
170         float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
171         float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
172         float vz=sin_pitch + yf*uz;
173
174         gx=cam_pos.x-vx*cam_pos.z/vz;
175         gy=cam_pos.y-vy*cam_pos.z/vz;
176 }
177
178 void Designer::tick()
179 {
180         const Msp::Time::TimeStamp t=Msp::Time::now();
181         float dt=(t-last_tick)/Msp::Time::sec;
182         last_tick=t;
183
184         dpy->tick();
185
186         if(move_y)
187         {
188                 cam_pos.x+=cos(cam_yaw)*dt*move_y;
189                 cam_pos.y+=sin(cam_yaw)*dt*move_y;
190         }
191         if(move_x)
192         {
193                 cam_pos.x+=sin(cam_yaw)*dt*move_x;
194                 cam_pos.y+=-cos(cam_yaw)*dt*move_x;
195         }
196         if(zoom)
197         {
198                 cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
199                 cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
200                 cam_pos.z+=sin(cam_pitch)*dt*zoom;
201         }
202         if(rotate)
203         {
204                 float vx=cos(cam_yaw)*cos(cam_pitch);
205                 float vy=sin(cam_yaw)*cos(cam_pitch);
206                 float vz=sin(cam_pitch);
207
208                 float gx=cam_pos.x-vx*cam_pos.z/vz;
209                 float gy=cam_pos.y-vy*cam_pos.z/vz;
210                 float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
211
212                 cam_yaw+=M_PI*dt*rotate;
213                 if(cam_yaw>M_PI*2)
214                         cam_yaw-=M_PI*2;
215                 else if(cam_yaw<0)
216                         cam_yaw+=M_PI*2;
217
218                 cam_pos.x=gx+cos(cam_yaw)*d;
219                 cam_pos.y=gy+sin(cam_yaw)*d;
220         }
221         if(pitch)
222         {
223                 cam_pitch+=M_PI/2*dt*pitch;
224                 if(cam_pitch>M_PI/12)
225                         cam_pitch=M_PI/12;
226                 else if(cam_pitch<-M_PI/2)
227                         cam_pitch=-M_PI/2;
228         }
229
230         if(tooltip_timeout && t>tooltip_timeout)
231         {
232                 Track3D *t3d=0;
233
234                 if(mode==CATALOGUE)
235                         t3d=pick_track(pointer_x, pointer_y);
236                 else
237                         t3d=pick_track(pointer_x, pointer_y);
238
239                 if(t3d)
240                 {
241                         const Track &track=t3d->get_track();
242                         const TrackType &ttype=track.get_type();
243                         ostringstream ss;
244                         ss.precision(2);
245                         ss<<ttype.get_article_number()<<' '<<ttype.get_description();
246                         if(mode!=CATALOGUE)
247                                 ss<<" (slope "<<track.get_slope()/ttype.get_total_length()*100<<"%)";
248                         if(track.get_turnout_id())
249                                 ss<<" (turnout "<<track.get_turnout_id()<<')';
250                         else if(track.get_sensor_id())
251                                 ss<<" (sensor "<<track.get_sensor_id()<<')';
252                         tooltip=ss.str();
253
254                         move_tooltip(pointer_x, pointer_y);
255                 }
256                 else
257                         tooltip="";
258
259                 tooltip_timeout=Msp::Time::TimeStamp();
260         }
261
262         render();
263
264         glc->swap_buffers();
265 }
266
267 /*** private ***/
268
269 void Designer::key_press(unsigned code, unsigned mod, wchar_t ch)
270 {
271         unsigned key=Msp::Input::key_from_sys(code);
272
273         if(mode==INPUT)
274         {
275                 input->key_press(key, mod, ch);
276                 return;
277         }
278
279         if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
280                 shift=true;
281
282         if(key==Msp::Input::KEY_N)
283                 mode=CATALOGUE;
284         else if(key==Msp::Input::KEY_G)
285         {
286                 manipulator->start_move();
287                 mode=MANIPULATE;
288         }
289         else if(key==Msp::Input::KEY_R)
290         {
291                 manipulator->start_rotate();
292                 mode=MANIPULATE;
293         }
294         else if(key==Msp::Input::KEY_D)
295         {
296                 manipulator->duplicate();
297                 manipulator->start_move();
298                 mode=MANIPULATE;
299         }
300         else if(key==Msp::Input::KEY_W)
301         {
302                 input=new ::Input(*this, "Filename");
303                 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
304                 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
305                 mode=INPUT;
306         }
307         else if(key==Msp::Input::KEY_PLUS)
308                 selection->select_more();
309         else if(key==Msp::Input::KEY_L && (mod&1))
310         {
311                 const set<Track *> &tracks=layout->get_tracks();
312                 float len=0;
313                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
314                         len+=(*i)->get_type().get_total_length();
315                 cout<<"Total length: "<<len<<"m\n";
316         }
317         else if(key==Msp::Input::KEY_L)
318                 selection->select_linked();
319         else if(key==Msp::Input::KEY_M)
320         {
321                 measure->start();
322                 mode=MEASURE;
323         }
324         else if(key==Msp::Input::KEY_Z)
325         {
326                 manipulator->start_elevate();
327                 mode=MANIPULATE;
328         }
329         else if(key==Msp::Input::KEY_ESC)
330         {
331                 if(mode==MANIPULATE)
332                         manipulator->cancel();
333                 else if(mode==CATALOGUE)
334                         mode=SELECT;
335                 else
336                         selection->clear();
337         }
338         else if(key==Msp::Input::KEY_X)
339         {
340                 set<Track *> tracks=selection->get_tracks();
341                 selection->clear();
342                 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
343                 {
344                         layout->remove_track(**i);
345                         delete *i;
346                 }
347         }
348         else if(key==Msp::Input::KEY_F && (mod&1))
349         {
350                 const set<Track *> &tracks=selection->get_tracks();
351                 const set<Track *> &ltracks=layout->get_tracks();
352                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
353                 {
354                         (*i)->set_flex(!(*i)->get_flex());
355                         (*i)->break_links();
356                         for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
357                                 if(*j!=*i)
358                                         (*i)->snap_to(**j, true);
359
360                         Track3D &t3d=layout_3d->get_track(**i);
361                         if((*i)->get_flex())
362                                 t3d.set_color(GL::Color(1, 0.5, 1));
363                         else
364                                 t3d.set_color(GL::Color(1, 1, 1));
365                 }
366         }
367         else if(key==Msp::Input::KEY_F)
368                 manipulator->flatten();
369         else if(key==Msp::Input::KEY_E && (mod&1))
370                 manipulator->even_slope(true);
371         else if(key==Msp::Input::KEY_E)
372                 manipulator->even_slope();
373         else if(key==Msp::Input::KEY_T)
374         {
375                 Track *track=selection->get_track();
376                 if(selection->size()==1 && track->get_type().get_n_routes()>1)
377                 {
378                         ostringstream ss;
379                         ss<<track->get_turnout_id();
380                         input=new ::Input(*this, "Turnout ID", ss.str());
381                         input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
382                         input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
383                         mode=INPUT;
384                 }
385         }
386         else if(key==Msp::Input::KEY_S)
387         {
388                 Track *track=selection->get_track();
389                 if(selection->size()==1 && track->get_type().get_n_routes()==1)
390                 {
391                         ostringstream ss;
392                         ss<<track->get_sensor_id();
393                         input=new ::Input(*this, "Sensor ID", ss.str());
394                         input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
395                         input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
396                         mode=INPUT;
397                 }
398         }
399         else if(key==Msp::Input::KEY_RIGHT)
400                 rotate=-1;
401         else if(key==Msp::Input::KEY_LEFT)
402                 rotate=1;
403         else if(key==Msp::Input::KEY_UP)
404                 move_y=1;
405         else if(key==Msp::Input::KEY_DOWN)
406                 move_y=-1;
407         else if(key==Msp::Input::KEY_INSERT)
408                 zoom=-1;
409         else if(key==Msp::Input::KEY_PGUP)
410                 zoom=1;
411         else if(key==Msp::Input::KEY_HOME)
412                 pitch=1;
413         else if(key==Msp::Input::KEY_END)
414                 pitch=-1;
415         else if(key==Msp::Input::KEY_DELETE)
416                 move_x=-1;
417         else if(key==Msp::Input::KEY_PGDN)
418                 move_x=1;
419 }
420
421 void Designer::key_release(unsigned code, unsigned)
422 {
423         unsigned key=Msp::Input::key_from_sys(code);
424
425         if(mode==INPUT)
426                 return;
427
428         if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
429                 shift=false;
430         else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
431                 rotate=0;
432         else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
433                 move_y=0;
434         else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
435                 zoom=0;
436         else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
437                 pitch=0;
438         else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)
439                 move_x=0;
440 }
441
442 void Designer::button_press(int x, int y, unsigned btn, unsigned)
443 {
444         y=screen_h-y-1;
445
446         float gx, gy;
447         map_pointer_coords(x, y, gx, gy);
448
449         if(mode==CATALOGUE)
450         {
451                 if(btn==1)
452                 {
453                         Track3D *ctrack=pick_track(x, y);
454                         if(ctrack)
455                         {
456                                 Track *track=ctrack->get_track().copy();
457                                 track->set_position(Point(gx, gy, 0));
458                                 layout->add_track(*track);
459
460                                 selection->clear();
461                                 selection->add_track(track);
462
463                                 mode=SELECT;
464                         }
465                 }
466                 else
467                         mode=SELECT;
468         }
469         else if(mode==SELECT)
470         {
471                 if(btn==1)
472                 {
473                         Track3D *track=pick_track(x, y);
474                         if(track)
475                         {
476                                 if(!shift)
477                                         selection->clear();
478                                 selection->toggle_track(&track->get_track());
479                         }
480                 }
481         }
482         else if(mode==MANIPULATE)
483                 manipulator->button_press(x, y, gx, gy, btn);
484         else if(mode==MEASURE)
485                 measure->button_press(x, y, gx, gy, btn);
486 }
487
488 void Designer::pointer_motion(int x, int y)
489 {
490         y=screen_h-y-1;
491
492         float gx, gy;
493         map_pointer_coords(x, y, gx, gy);
494
495         if(mode==SELECT || mode==CATALOGUE)
496         {
497                 pointer_x=x;
498                 pointer_y=y;
499                 tooltip_timeout=Msp::Time::now()+100*Msp::Time::msec;
500         }
501
502         if(mode!=INPUT)
503         {
504                 manipulator->pointer_motion(x, y, gx, gy);
505                 measure->pointer_motion(x, y, gx, gy);
506         }
507
508         if(mode==MEASURE || mode==MANIPULATE)
509                 move_tooltip(x, y);
510 }
511
512 void Designer::project_3d()
513 {
514         glMatrixMode(GL_PROJECTION);
515         glLoadIdentity();
516         glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
517         glMatrixMode(GL_MODELVIEW);
518 }
519
520 void Designer::apply_camera()
521 {
522         glLoadIdentity();
523         if(mode==CATALOGUE)
524                 glTranslatef(0, 0, -1);
525         else
526         {
527                 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
528                 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
529                 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
530         }
531 }
532
533 void Designer::render()
534 {
535         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
536         glEnable(GL_DEPTH_TEST);
537
538         project_3d();
539         apply_camera();
540         if(mode==CATALOGUE)
541                 cat_layout_3d->render();
542         else
543         {
544                 if(base_mesh)
545                 {
546                         GL::Texture::unbind();
547                         base_mesh->draw();
548                 }
549                 layout_3d->render(true);
550                 manipulator->render();
551                 if(mode==MEASURE)
552                         measure->render();
553         }
554
555         glMatrixMode(GL_PROJECTION);
556         glLoadIdentity();
557         glOrtho(0, screen_w, 0, screen_h, 0, 1);
558         glMatrixMode(GL_MODELVIEW);
559         glLoadIdentity();
560
561         glDisable(GL_DEPTH_TEST);
562
563         if(!tooltip.empty())
564         {
565                 glTranslatef(tooltip_x, tooltip_y, 0);
566                 glScalef(20, 20, 20);
567                 float width=font->get_string_width(tooltip);
568                 glColor4f(0, 0, 0, 0.5);
569                 glBegin(GL_QUADS);
570                 glVertex2f(0, 0);
571                 glVertex2f(width, 0);
572                 glVertex2f(width, 1);
573                 glVertex2f(0, 1);
574                 glEnd();
575                 glColor4f(1, 1, 1, 1);
576                 font->draw_string(tooltip);
577         }
578
579         if(mode==INPUT)
580                 input->render();
581 }
582
583 Track3D *Designer::pick_track(int x, int y)
584 {
585         Layout3D *l=layout_3d;
586         if(mode==CATALOGUE)
587                 l=cat_layout_3d;
588
589         float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
590         float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
591         float size=4.0/screen_h*0.82843;
592
593         project_3d();
594         apply_camera();
595
596         return l->pick_track(xx, yy, size);
597 }
598
599 void Designer::manipulation_status(const string &status)
600 {
601         tooltip=status;
602 }
603
604 void Designer::manipulation_done(bool)
605 {
606         mode=SELECT;
607 }
608
609 void Designer::measure_changed()
610 {
611         float pard=measure->get_parallel_distance()*1000;
612         float perpd=measure->get_perpendicular_distance()*1000;
613         float d=sqrt(pard*pard+perpd*perpd);
614         float adiff=measure->get_angle_difference()*180/M_PI;
615         ostringstream ss;
616         ss.precision(3);
617         ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
618         tooltip=ss.str();
619 }
620
621 void Designer::measure_done()
622 {
623         mode=SELECT;
624 }
625
626 void Designer::move_tooltip(int x, int y)
627 {
628         int w=static_cast<int>(font->get_string_width(tooltip)*20);
629         tooltip_x=max(min(static_cast<int>(screen_w)-w, x), 0);
630         tooltip_y=max(min(static_cast<int>(screen_h)-20, y), 0);
631 }
632
633 void Designer::save_accept()
634 {
635         layout->save(input->get_text());
636
637         input_dismiss();
638 }
639
640 void Designer::turnout_id_accept()
641 {
642         Track *track=selection->get_track();
643         unsigned id=lexical_cast<unsigned>(input->get_text());
644         track->set_turnout_id(id);
645
646         Track3D &t3d=layout_3d->get_track(*track);
647         if(id)
648                 t3d.set_color(GL::Color(0.5, 1, 1));
649         else
650                 t3d.set_color(GL::Color(1, 1, 1));
651
652         input_dismiss();
653 }
654
655 void Designer::sensor_id_accept()
656 {
657         Track *track=selection->get_track();
658         unsigned id=lexical_cast<unsigned>(input->get_text());
659         track->set_sensor_id(id);
660
661         Track3D &t3d=layout_3d->get_track(*track);
662         if(id)
663                 t3d.set_color(GL::Color(1, 1, 0.5));
664         else
665                 t3d.set_color(GL::Color(1, 1, 1));
666
667         input_dismiss();
668 }
669
670 void Designer::input_dismiss()
671 {
672         delete input;
673         input=0;
674         mode=SELECT;
675 }
676
677 Application::RegApp<Designer> Designer::reg;