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