]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Derive application classes from RegisteredApplication
[r2c2.git] / source / designer / designer.cpp
1 #include <signal.h>
2 #include <algorithm>
3 #include <cmath>
4 #include <msp/fs/utils.h>
5 #include <msp/gl/blend.h>
6 #include <msp/gl/framebuffer.h>
7 #include <msp/gl/matrix.h>
8 #include <msp/gl/misc.h>
9 #include <msp/gl/projection.h>
10 #include <msp/gl/tests.h>
11 #include <msp/gl/texture2d.h>
12 #include <msp/input/keys.h>
13 #include <msp/io/print.h>
14 #include <msp/stringcodec/codec.h>
15 #include <msp/stringcodec/utf8.h>
16 #include <msp/strings/lexicalcast.h>
17 #include <msp/strings/utils.h>
18 #include <msp/time/units.h>
19 #include <msp/time/utils.h>
20 #include "libr2c2/route.h"
21 #include "libr2c2/tracktype.h"
22 #include "libr2c2/zone.h"
23 #include "3d/path.h"
24 #include "designer.h"
25 #include "input.h"
26 #include "layoutbar.h"
27 #include "manipulator.h"
28 #include "measure.h"
29 #include "routebar.h"
30 #include "selection.h"
31 #include "svgexporter.h"
32 #include "trackbar.h"
33 #include "trackproperties.h"
34 #include "zonebar.h"
35 #include "zoneproperties.h"
36
37 using namespace std;
38 using namespace R2C2;
39 using namespace Msp;
40
41 Designer::Designer(int argc, char **argv):
42         window(1280, 960),
43         ui_res("r2c2.res"),
44         root(ui_res, window),
45         base_object(0),
46         cur_route(0),
47         cur_zone(0),
48         mode(SELECT),
49         manipulator(*this, root, selection),
50         measure(*this)
51 {
52         window.set_title("Railway Designer");
53         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
54
55         selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
56         manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
57         manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
58         measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
59         measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
60
61         // Setup catalogue and layout
62         DataFile::load(catalogue, "tracks.dat");
63
64         cat_layout_3d = new Layout3D(catalogue.get_layout());
65
66         layout = new Layout(catalogue);
67         layout_3d = new Layout3D(*layout);
68
69         layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
70         layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
71
72         if(argc>1)
73         {
74                 filename = argv[1];
75                 DataFile::load(*layout, argv[1]);
76
77                 if(!layout->get_base().empty())
78                 {
79                         base_object = new GL::Object;
80                         DataFile::load(*base_object, layout->get_base());
81                 }
82         }
83
84         // Setup OpenGL
85         cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
86
87         main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
88         GL::Pipeline *pipeline = &main_view->get_pipeline();
89
90         GL::PipelinePass *pass = &pipeline->add_pass("unlit");
91         pass->depth_test = &GL::DepthTest::lequal();
92         pass->blend = &GL::Blend::alpha();
93
94         pass = &pipeline->add_pass("blended");
95         pass->lighting = &layout_3d->get_lighting();
96         pass->depth_test = &GL::DepthTest::lequal();
97         pass->blend = &GL::Blend::alpha();
98
99         pass = &pipeline->add_pass("overlay");
100         pass->blend = &GL::Blend::alpha();
101
102         if(base_object)
103                 pipeline->add_renderable(*base_object);
104         pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
105         pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
106
107         // Setup UI
108         root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
109         root.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
110         root.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
111         root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
112
113         toolbars.push_back(new Layoutbar(*this));
114         toolbars.push_back(new Trackbar(*this));
115         toolbars.push_back(new Routebar(*this));
116         toolbars.push_back(new Zonebar(*this));
117         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
118         {
119                 root.add(**i);
120                 if(i!=toolbars.begin())
121                         (*i)->expand(false);
122                 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(this, &Designer::arrange_toolbars)));
123         }
124         arrange_toolbars();
125
126         GLtk::Panel *statusbar = new GLtk::Panel;
127         root.add(*statusbar);
128         statusbar->set_size(window.get_width(), 20);
129         statusbar->set_focusable(false);
130
131         lbl_status = new GLtk::Label;
132         statusbar->add(*lbl_status);
133         lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
134
135         track_wrap = new TrackWrap(*layout_3d, selection);
136         pipeline->add_renderable_for_pass(*track_wrap, "unlit");
137         overlay = new Overlay3D(ui_res.get_default_font());
138         pipeline->add_renderable_for_pass(*overlay, "overlay");
139
140         camera_ctl = new CameraController(*main_view, root);
141         cat_view->get_camera().set_look_direction(GL::Vector3(0, 0.13053, -0.99144));
142         cat_view->view_all(true);
143         main_view->view_all();
144
145         const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
146         for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
147                 update_track_icon(*i->second);
148 }
149
150 Designer::~Designer()
151 {
152         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
153                 delete *i;
154         delete camera_ctl;
155         delete track_wrap;
156         delete overlay;
157         delete main_view;
158         delete base_object;
159         delete layout_3d;
160         delete layout;
161         delete cat_view;
162         delete cat_layout_3d;
163 }
164
165 int Designer::main()
166 {
167         window.show();
168
169         mode = SELECT;
170
171         return Application::main();
172 }
173
174 void Designer::save()
175 {
176         InputDialog *input = new InputDialog(*this, "Save layout", filename);
177         input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
178 }
179
180 void Designer::quit()
181 {
182         exit(0);
183 }
184
185 void Designer::new_track()
186 {
187         if(mode!=SELECT)
188                 return;
189
190         mode = CATALOGUE;
191         lbl_status->set_text("Select new track or press Esc to cancel");
192 }
193
194 void Designer::erase_tracks()
195 {
196         if(mode!=SELECT)
197                 return;
198
199         set<Track *> tracks = selection.get_tracks();
200         selection.clear();
201         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
202         {
203                 overlay->clear(layout_3d->get_track(**i));
204                 delete *i;
205         }
206 }
207
208 void Designer::track_properties()
209 {
210         if(mode!=SELECT || selection.empty())
211                 return;
212
213         TrackProperties *track_prop = new TrackProperties(selection);
214         root.add(*track_prop);
215         const GLtk::Geometry &root_geom = root.get_geometry();
216         const GLtk::Geometry &dlg_geom = track_prop->get_geometry();
217         track_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
218
219         track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
220 }
221
222 void Designer::extend_track()
223 {
224         if(mode!=SELECT)
225                 return;
226
227         if(manipulator.start_extend())
228                 mode = MANIPULATE;
229 }
230
231 void Designer::connect_tracks()
232 {
233         if(mode!=SELECT)
234                 return;
235
236         manipulator.connect();
237 }
238
239 void Designer::flatten_tracks()
240 {
241         if(mode!=SELECT)
242                 return;
243
244         manipulator.flatten();
245 }
246
247 void Designer::svg_export()
248 {
249         InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
250         input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
251 }
252
253 void Designer::edit_route(Route *r)
254 {
255         cur_route = r;
256         cur_zone = 0;
257         if(cur_route)
258                 show_route(*cur_route);
259         else
260                 clear_paths();
261 }
262
263 void Designer::rename_route()
264 {
265         if(mode!=SELECT || !cur_route)
266                 return;
267
268         InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
269         input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
270 }
271
272 void Designer::add_selection_to_route()
273 {
274         if(!cur_route)
275                 return;
276
277         try
278         {
279                 cur_route->add_tracks(selection.get_tracks());
280         }
281         catch(const exception &e)
282         {
283                 lbl_status->set_text(e.what());
284         }
285
286         show_route(*cur_route);
287 }
288
289 void Designer::edit_zone(Zone *z)
290 {
291         cur_zone = z;
292         cur_route = 0;
293         if(cur_zone)
294                 show_zone(*cur_zone);
295         else
296                 clear_paths();
297 }
298
299 void Designer::zone_properties()
300 {
301         if(!cur_zone)
302                 return;
303
304         ZoneProperties *zone_prop = new ZoneProperties(*cur_zone);
305         root.add(*zone_prop);
306         const GLtk::Geometry &root_geom = root.get_geometry();
307         const GLtk::Geometry &dlg_geom = zone_prop->get_geometry();
308         zone_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
309 }
310
311 void Designer::add_selection_to_zone()
312 {
313         if(!cur_zone)
314                 return;
315
316         try
317         {
318                 cur_zone->add_tracks(selection.get_tracks());
319         }
320         catch(const exception &e)
321         {
322                 lbl_status->set_text(e.what());
323         }
324
325         show_zone(*cur_zone);
326 }
327
328 Vector Designer::map_pointer_to_ground(int x, int y)
329 {
330         float xf = x*2.0/window.get_width()-1.0;
331         float yf = y*2.0/window.get_height()-1.0;
332         GL::Vector4 vec = main_view->get_camera().unproject(GL::Vector4(xf, yf, 0, 0));
333         const GL::Vector3 &pos = main_view->get_camera().get_position();
334
335         return Vector(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
336 }
337
338 void Designer::tick()
339 {
340         const Msp::Time::TimeStamp t = Msp::Time::now();
341         float dt = (t-last_tick)/Msp::Time::sec;
342         last_tick = t;
343
344         if(mode==MANIPULATE_DONE)
345                 mode = SELECT;
346
347         window.tick();
348         root.tick();
349         camera_ctl->tick(dt);
350
351         for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
352                 layout_3d->get_track(**i).get_path().set_mask(0);
353         new_tracks.clear();
354
355         render();
356
357         window.swap_buffers();
358 }
359
360 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
361 {
362         key = Input::key_from_sys(key);
363         mod = Input::mod_from_sys(mod);
364
365         if(key==Msp::Input::KEY_N && (mod&Input::MOD_SHIFT))
366                 extend_track();
367         else if(key==Msp::Input::KEY_N)
368                 new_track();
369         else if(key==Msp::Input::KEY_G)
370         {
371                 manipulator.start_move();
372                 mode = MANIPULATE;
373         }
374         else if(key==Msp::Input::KEY_R)
375         {
376                 manipulator.start_rotate();
377                 mode = MANIPULATE;
378         }
379         else if(key==Msp::Input::KEY_D)
380         {
381                 manipulator.duplicate();
382                 manipulator.start_move();
383                 mode = MANIPULATE;
384         }
385         else if(key==Msp::Input::KEY_W)
386                 save();
387         else if(key==Msp::Input::KEY_PLUS)
388                 selection.select_more();
389         else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
390                 selection.select_blocks();
391         else if(key==Msp::Input::KEY_L)
392                 selection.select_linked();
393         else if(key==Msp::Input::KEY_M)
394         {
395                 measure.start();
396                 mode = MEASURE;
397         }
398         else if(key==Msp::Input::KEY_Z)
399         {
400                 manipulator.start_elevate();
401                 mode = MANIPULATE;
402         }
403         else if(key==Msp::Input::KEY_ESC)
404         {
405                 if(mode==MANIPULATE)
406                         manipulator.cancel();
407                 else if(mode==CATALOGUE)
408                         mode = SELECT;
409                 else
410                         selection.clear();
411         }
412         else if(key==Msp::Input::KEY_X)
413                 erase_tracks();
414         else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
415         {
416                 const set<Track *> &tracks = selection.get_tracks();
417                 const set<Track *> &ltracks = layout->get_tracks();
418                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
419                 {
420                         (*i)->set_flex(!(*i)->get_flex());
421                         (*i)->break_links();
422                         for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
423                                 if(*j!=*i)
424                                         (*i)->snap_to(**j, true);
425
426                         update_track_icon(layout_3d->get_track(**i));
427                 }
428         }
429         else if(key==Msp::Input::KEY_F)
430                 flatten_tracks();
431         else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
432                 manipulator.even_slope(true);
433         else if(key==Msp::Input::KEY_E)
434                 manipulator.even_slope();
435         else if(key==Msp::Input::KEY_A)
436         {
437                 if(cur_route)
438                         add_selection_to_route();
439                 else if(cur_zone)
440                         add_selection_to_zone();
441         }
442         else if(key==Msp::Input::KEY_C)
443                 connect_tracks();
444         else if(key==Msp::Input::KEY_V)
445                 svg_export();
446         else if(key==Msp::Input::KEY_P)
447                 track_properties();
448 }
449
450 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
451 {
452         y = window.get_height()-y-1;
453         mod = Input::mod_from_sys(mod);
454
455         Vector ground = map_pointer_to_ground(x, y);
456
457         if(mode==CATALOGUE)
458         {
459                 if(btn==1)
460                 {
461                         Track *ctrack = pick_track(x, y);
462                         if(ctrack)
463                         {
464                                 Track *track = new Track(*layout, ctrack->get_type());
465                                 track->set_position(ground);
466
467                                 selection.clear();
468                                 selection.add_track(track);
469
470                                 mode = SELECT;
471                         }
472                 }
473                 else
474                         mode = SELECT;
475         }
476         else if(mode==SELECT)
477         {
478                 if(btn==1)
479                 {
480                         Track *track = pick_track(x, y);
481                         if(track)
482                         {
483                                 if(!(mod&Input::MOD_SHIFT))
484                                         selection.clear();
485                                 selection.toggle_track(track);
486                         }
487                 }
488         }
489         else if(mode==MEASURE)
490                 measure.button_press(x, y, ground.x, ground.y, btn);
491 }
492
493 void Designer::pointer_motion(int x, int y)
494 {
495         y = window.get_height()-y-1;
496
497         if(!root.get_child_at(x, y))
498         {
499                 Vector ground = map_pointer_to_ground(x, y);
500                 measure.pointer_motion(x, y, ground.x, ground.y);
501         }
502 }
503
504 void Designer::render()
505 {
506         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
507
508         View3D *view = (mode==CATALOGUE ? cat_view : main_view);
509         view->render();
510         if(mode==MEASURE)
511         {
512                 GL::Bind bind_depth(GL::DepthTest::lequal());
513                 measure.render();
514         }
515
516         GL::matrix_mode(GL::PROJECTION);
517         GL::load_identity();
518         GL::ortho_bottomleft(window.get_width(), window.get_height());
519         GL::matrix_mode(GL::MODELVIEW);
520         GL::load_identity();
521
522         GL::Bind bind_blend(GL::Blend::alpha());
523         root.render();
524         // XXX Should fix GLtk so these would not be needed
525         GL::Texture::unbind();
526         glColor3f(1.0, 1.0, 1.0);
527 }
528
529 void Designer::track_added(Track &trk)
530 {
531         new_tracks.push_back(&trk);
532 }
533
534 void Designer::track_removed(Track &trk)
535 {
536         list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
537         if(i!=new_tracks.end())
538                 new_tracks.erase(i);
539 }
540
541 Track *Designer::pick_track(int x, int y)
542 {
543         float xx = x*2.0/window.get_width()-1.0;
544         float yy = y*2.0/window.get_height()-1.0;
545
546         View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
547         const GL::Vector3 &cpos = view.get_camera().get_position();
548         GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(xx, yy, 0, 0));
549
550         return view.get_layout().get_layout().pick_track(Vector(cpos.x, cpos.y, cpos.z), Vector(cray.x, cray.y, cray.z));
551 }
552
553 void Designer::update_track_icon(Track3D &track)
554 {
555         overlay->clear(track);
556
557         if(track.get_track().get_flex())
558                 overlay->add_graphic(track, "flex");
559
560         if(unsigned sid = track.get_track().get_sensor_id())
561         {
562                 overlay->add_graphic(track, "sensor");
563                 overlay->set_label(track, lexical_cast<string>(sid));
564         }
565         else if(unsigned tid = track.get_track().get_turnout_id())
566         {
567                 if(tid<0x800)
568                 {
569                         overlay->add_graphic(track, "turnout");
570                         overlay->set_label(track, lexical_cast<string>(tid));
571                 }
572         }
573 }
574
575 void Designer::selection_changed()
576 {
577         const set<Track *> &tracks = selection.get_tracks();
578         if(tracks.empty())
579                 lbl_status->set_text(string());
580         else
581         {
582                 float len = 0;
583                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
584                         len += (*i)->get_type().get_total_length();
585                 lbl_status->set_text(format("%.2fm of track selected\n", len));
586         }
587 }
588
589 void Designer::manipulation_status(const string &status)
590 {
591         lbl_status->set_text(status);
592 }
593
594 void Designer::manipulation_done(bool)
595 {
596         mode = MANIPULATE_DONE;
597         selection_changed();
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         string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
607         lbl_status->set_text(info);
608 }
609
610 void Designer::measure_done()
611 {
612         mode = SELECT;
613         selection_changed();
614 }
615
616 void Designer::arrange_toolbars()
617 {
618         unsigned x = 0;
619         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
620         {
621                 const GLtk::Geometry &geom = (*i)->get_geometry();
622                 (*i)->set_position(x, window.get_height()-geom.h);
623                 x += geom.w;
624         }
625 }
626
627 void Designer::track_properties_response(int)
628 {
629         const set<Track *> &tracks = selection.get_tracks();
630         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
631                 update_track_icon(layout_3d->get_track(**i));
632 }
633
634 void Designer::route_name_accept(const string &text)
635 {
636         if(cur_route)
637                 cur_route->set_name(text);
638 }
639
640 void Designer::svg_export_accept(const string &text)
641 {
642         SvgExporter svg_exp(*layout);
643         svg_exp.save(text);
644 }
645
646 string Designer::tooltip(int x, int y)
647 {
648         if(Track *track = pick_track(x, y))
649         {
650                 const TrackType &ttype = track->get_type();
651                 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
652                 if(mode!=CATALOGUE && abs(track->get_slope())>1e-4)
653                         info += format(" (slope %.1f%%)", abs(track->get_slope()/ttype.get_total_length()*100));
654                 if(track->get_turnout_id())
655                         info += format(" (turnout %d)", track->get_turnout_id());
656                 else if(track->get_sensor_id())
657                         info += format(" (sensor %d)", track->get_sensor_id());
658
659                 return info;
660         }
661
662         return string();
663 }
664
665 void Designer::clear_paths()
666 {
667         const set<Track *> &ltracks = layout->get_tracks();
668         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
669         {
670                 Track3D &t3d = layout_3d->get_track(**i);
671                 t3d.get_path().set_mask(0);
672         }
673 }
674
675 void Designer::show_route(const Route &route)
676 {
677         clear_paths();
678
679         const set<Track *> &rtracks = route.get_tracks();
680         for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
681         {
682                 Track3D &t3d = layout_3d->get_track(**i);
683                 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
684                 int path = -1;
685                 if(unsigned tid = (*i)->get_turnout_id())
686                         path = route.get_turnout(tid);
687                 if(path>=0)
688                         t3d.get_path().set_path(path);
689                 else
690                         t3d.get_path().set_mask((*i)->get_type().get_paths());
691         }
692 }
693
694 void Designer::show_zone(const Zone &zone)
695 {
696         clear_paths();
697
698         const Zone::TrackSet &ztracks = zone.get_tracks();
699         for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
700         {
701                 Track3D &t3d = layout_3d->get_track(**i);
702                 t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
703                 t3d.get_path().set_mask((*i)->get_type().get_paths());
704         }
705 }