]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Allow setting sensor ID for multiple tracks at once
[r2c2.git] / source / designer / designer.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 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                 const set<Track *> &tracks=selection->get_tracks();
389                 bool ok=false;
390                 int id=-1;
391                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
392                 {
393                         if((*i)->get_type().get_n_routes()==1)
394                                 ok=true;
395                         if(static_cast<int>((*i)->get_sensor_id())!=id)
396                         {
397                                 if(id==-1)
398                                         id=(*i)->get_sensor_id();
399                                 else
400                                         id=-2;
401                         }
402                 }
403                 if(ok)
404                 {
405                         ostringstream ss;
406                         if(id>=0)
407                                 ss<<id;
408                         input=new ::Input(*this, "Sensor ID", ss.str());
409                         input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
410                         input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
411                         mode=INPUT;
412                 }
413         }
414         else if(key==Msp::Input::KEY_RIGHT)
415                 rotate=-1;
416         else if(key==Msp::Input::KEY_LEFT)
417                 rotate=1;
418         else if(key==Msp::Input::KEY_UP)
419                 move_y=1;
420         else if(key==Msp::Input::KEY_DOWN)
421                 move_y=-1;
422         else if(key==Msp::Input::KEY_INSERT)
423                 zoom=-1;
424         else if(key==Msp::Input::KEY_PGUP)
425                 zoom=1;
426         else if(key==Msp::Input::KEY_HOME)
427                 pitch=1;
428         else if(key==Msp::Input::KEY_END)
429                 pitch=-1;
430         else if(key==Msp::Input::KEY_DELETE)
431                 move_x=-1;
432         else if(key==Msp::Input::KEY_PGDN)
433                 move_x=1;
434 }
435
436 void Designer::key_release(unsigned code, unsigned)
437 {
438         unsigned key=Msp::Input::key_from_sys(code);
439
440         if(mode==INPUT)
441                 return;
442
443         if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
444                 shift=false;
445         else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
446                 rotate=0;
447         else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
448                 move_y=0;
449         else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
450                 zoom=0;
451         else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
452                 pitch=0;
453         else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)
454                 move_x=0;
455 }
456
457 void Designer::button_press(int x, int y, unsigned btn, unsigned)
458 {
459         y=screen_h-y-1;
460
461         float gx, gy;
462         map_pointer_coords(x, y, gx, gy);
463
464         if(mode==CATALOGUE)
465         {
466                 if(btn==1)
467                 {
468                         Track3D *ctrack=pick_track(x, y);
469                         if(ctrack)
470                         {
471                                 Track *track=ctrack->get_track().copy();
472                                 track->set_position(Point(gx, gy, 0));
473                                 layout->add_track(*track);
474
475                                 selection->clear();
476                                 selection->add_track(track);
477
478                                 mode=SELECT;
479                         }
480                 }
481                 else
482                         mode=SELECT;
483         }
484         else if(mode==SELECT)
485         {
486                 if(btn==1)
487                 {
488                         Track3D *track=pick_track(x, y);
489                         if(track)
490                         {
491                                 if(!shift)
492                                         selection->clear();
493                                 selection->toggle_track(&track->get_track());
494                         }
495                 }
496         }
497         else if(mode==MANIPULATE)
498                 manipulator->button_press(x, y, gx, gy, btn);
499         else if(mode==MEASURE)
500                 measure->button_press(x, y, gx, gy, btn);
501 }
502
503 void Designer::pointer_motion(int x, int y)
504 {
505         y=screen_h-y-1;
506
507         float gx, gy;
508         map_pointer_coords(x, y, gx, gy);
509
510         if(mode==SELECT || mode==CATALOGUE)
511         {
512                 pointer_x=x;
513                 pointer_y=y;
514                 tooltip_timeout=Msp::Time::now()+100*Msp::Time::msec;
515         }
516
517         if(mode!=INPUT)
518         {
519                 manipulator->pointer_motion(x, y, gx, gy);
520                 measure->pointer_motion(x, y, gx, gy);
521         }
522
523         if(mode==MEASURE || mode==MANIPULATE)
524                 move_tooltip(x, y);
525 }
526
527 void Designer::project_3d()
528 {
529         glMatrixMode(GL_PROJECTION);
530         glLoadIdentity();
531         glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
532         glMatrixMode(GL_MODELVIEW);
533 }
534
535 void Designer::apply_camera()
536 {
537         glLoadIdentity();
538         if(mode==CATALOGUE)
539                 glTranslatef(0, 0, -1);
540         else
541         {
542                 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
543                 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
544                 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
545         }
546 }
547
548 void Designer::render()
549 {
550         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
551         glEnable(GL_DEPTH_TEST);
552
553         project_3d();
554         apply_camera();
555         if(mode==CATALOGUE)
556                 cat_layout_3d->render();
557         else
558         {
559                 if(base_mesh)
560                 {
561                         GL::Texture::unbind();
562                         base_mesh->draw();
563                 }
564                 layout_3d->render(true);
565                 manipulator->render();
566                 if(mode==MEASURE)
567                         measure->render();
568         }
569
570         glMatrixMode(GL_PROJECTION);
571         glLoadIdentity();
572         glOrtho(0, screen_w, 0, screen_h, 0, 1);
573         glMatrixMode(GL_MODELVIEW);
574         glLoadIdentity();
575
576         glDisable(GL_DEPTH_TEST);
577
578         if(!tooltip.empty())
579         {
580                 glTranslatef(tooltip_x, tooltip_y, 0);
581                 glScalef(20, 20, 20);
582                 float width=font->get_string_width(tooltip);
583                 glColor4f(0, 0, 0, 0.5);
584                 glBegin(GL_QUADS);
585                 glVertex2f(0, 0);
586                 glVertex2f(width, 0);
587                 glVertex2f(width, 1);
588                 glVertex2f(0, 1);
589                 glEnd();
590                 glColor4f(1, 1, 1, 1);
591                 font->draw_string(tooltip);
592         }
593
594         if(mode==INPUT)
595                 input->render();
596 }
597
598 Track3D *Designer::pick_track(int x, int y)
599 {
600         Layout3D *l=layout_3d;
601         if(mode==CATALOGUE)
602                 l=cat_layout_3d;
603
604         float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
605         float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
606         float size=4.0/screen_h*0.82843;
607
608         project_3d();
609         apply_camera();
610
611         return l->pick_track(xx, yy, size);
612 }
613
614 void Designer::manipulation_status(const string &status)
615 {
616         tooltip=status;
617 }
618
619 void Designer::manipulation_done(bool)
620 {
621         mode=SELECT;
622 }
623
624 void Designer::measure_changed()
625 {
626         float pard=measure->get_parallel_distance()*1000;
627         float perpd=measure->get_perpendicular_distance()*1000;
628         float d=sqrt(pard*pard+perpd*perpd);
629         float adiff=measure->get_angle_difference()*180/M_PI;
630         ostringstream ss;
631         ss.precision(3);
632         ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
633         tooltip=ss.str();
634 }
635
636 void Designer::measure_done()
637 {
638         mode=SELECT;
639 }
640
641 void Designer::move_tooltip(int x, int y)
642 {
643         int w=static_cast<int>(font->get_string_width(tooltip)*20);
644         tooltip_x=max(min(static_cast<int>(screen_w)-w, x), 0);
645         tooltip_y=max(min(static_cast<int>(screen_h)-20, y), 0);
646 }
647
648 void Designer::save_accept()
649 {
650         layout->save(input->get_text());
651
652         input_dismiss();
653 }
654
655 void Designer::turnout_id_accept()
656 {
657         Track *track=selection->get_track();
658         unsigned id=lexical_cast<unsigned>(input->get_text());
659         track->set_turnout_id(id);
660
661         Track3D &t3d=layout_3d->get_track(*track);
662         if(id)
663                 t3d.set_color(GL::Color(0.5, 1, 1));
664         else
665                 t3d.set_color(GL::Color(1, 1, 1));
666
667         input_dismiss();
668 }
669
670 void Designer::sensor_id_accept()
671 {
672         const set<Track *> &tracks=selection->get_tracks();
673         unsigned id=lexical_cast<unsigned>(input->get_text());
674         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
675         {
676                 (*i)->set_sensor_id(id);
677
678                 Track3D &t3d=layout_3d->get_track(**i);
679                 if(id)
680                         t3d.set_color(GL::Color(1, 1, 0.5));
681                 else
682                         t3d.set_color(GL::Color(1, 1, 1));
683         }
684
685         input_dismiss();
686 }
687
688 void Designer::input_dismiss()
689 {
690         delete input;
691         input=0;
692         mode=SELECT;
693 }
694
695 Application::RegApp<Designer> Designer::reg;