]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Update Track3D to use GL::Renderer
[r2c2.git] / source / designer / designer.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2006-2010 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/rendermode.h>
18 #include <msp/gl/select.h>
19 #include <msp/gl/tests.h>
20 #include <msp/gl/texture2d.h>
21 #include <msp/input/keys.h>
22 #include <msp/io/print.h>
23 #include <msp/strings/codec.h>
24 #include <msp/strings/lexicalcast.h>
25 #include <msp/strings/utf8.h>
26 #include <msp/strings/utils.h>
27 #include <msp/time/units.h>
28 #include <msp/time/utils.h>
29 #include "libr2c2/route.h"
30 #include "libr2c2/tracktype.h"
31 #include "libr2c2/zone.h"
32 #include "3d/path.h"
33 #include "designer.h"
34 #include "input.h"
35 #include "layoutbar.h"
36 #include "manipulator.h"
37 #include "measure.h"
38 #include "routebar.h"
39 #include "selection.h"
40 #include "svgexporter.h"
41 #include "trackbar.h"
42 #include "trackproperties.h"
43 #include "zonebar.h"
44 #include "zoneproperties.h"
45
46 using namespace std;
47 using namespace R2C2;
48 using namespace Msp;
49
50 Application::RegApp<Designer> Designer::reg;
51
52 Designer::Designer(int argc, char **argv):
53         window(1280, 960),
54         ui_res("r2c2.res"),
55         root(ui_res, window),
56         base_object(0),
57         cur_route(0),
58         cur_zone(0),
59         mode(SELECT),
60         manipulator(*this, root, selection),
61         measure(*this),
62         camera_ctl(*this, root, camera),
63         track_wrap(*this, selection)
64 {
65         window.set_title("Railway Designer");
66         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
67
68         selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
69         manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
70         manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
71         measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
72         measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
73
74         // Setup catalogue and layout
75         DataFile::load(catalogue, "tracks.dat");
76
77         cat_layout_3d = new Layout3D(catalogue.get_layout());
78
79         layout = new Layout(catalogue);
80         layout_3d = new Layout3D(*layout);
81
82         layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
83         layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
84
85         if(argc>1)
86         {
87                 filename = argv[1];
88                 DataFile::load(*layout, argv[1]);
89
90                 if(!layout->get_base().empty())
91                 {
92                         base_object = new GL::Object;
93                         DataFile::load(*base_object, layout->get_base());
94                 }
95         }
96
97         // Setup OpenGL
98         GL::enable(GL_CULL_FACE);
99
100         pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
101         pipeline->set_camera(&camera);
102         pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
103         if(base_object)
104                 pipeline->add_renderable(*base_object);
105         pipeline->add_renderable_for_pass(track_wrap, "unlit");
106         pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
107         pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
108
109         light.set_position(0, -0.259, 0.966, 0);
110         lighting.attach(0, light);
111
112         GL::PipelinePass *pass = &pipeline->add_pass(0);
113         pass->lighting = &lighting;
114         pass->depth_test = &GL::DepthTest::lequal();
115
116         pass = &pipeline->add_pass("unlit");
117         pass->depth_test = &GL::DepthTest::lequal();
118         pass->blend = &GL::Blend::alpha();
119
120         pass = &pipeline->add_pass("blended");
121         pass->lighting = &lighting;
122         pass->depth_test = &GL::DepthTest::lequal();
123         pass->blend = &GL::Blend::alpha();
124
125         camera.set_up_direction(GL::Vector3(0, 0, 1));
126         camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
127
128         // Setup UI
129         root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
130         root.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
131         root.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
132         root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
133
134         toolbars.push_back(new Layoutbar(*this));
135         toolbars.push_back(new Trackbar(*this));
136         toolbars.push_back(new Routebar(*this));
137         toolbars.push_back(new Zonebar(*this));
138         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
139         {
140                 root.add(**i);
141                 if(i!=toolbars.begin())
142                         (*i)->expand(false);
143                 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(this, &Designer::arrange_toolbars)));
144         }
145         arrange_toolbars();
146
147         GLtk::Panel *statusbar = new GLtk::Panel;
148         root.add(*statusbar);
149         statusbar->set_size(window.get_width(), 20);
150         statusbar->set_focusable(false);
151
152         lbl_status = new GLtk::Label;
153         statusbar->add(*lbl_status);
154         lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
155
156         overlay = new Overlay3D(window, camera, ui_res.get_default_font());
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 Point 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 Point(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         Point ground = map_pointer_to_ground(x, y);
468
469         if(mode==CATALOGUE)
470         {
471                 if(btn==1)
472                 {
473                         Track3D *ctrack = pick_track(x, y);
474                         if(ctrack)
475                         {
476                                 Track *track = new Track(*layout, ctrack->get_track().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                         Track3D *track = pick_track(x, y);
493                         if(track)
494                         {
495                                 if(!(mod&Input::MOD_SHIFT))
496                                         selection.clear();
497                                 selection.toggle_track(&track->get_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                 Point 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                 GL::enable(GL_CULL_FACE);
544                 {
545                         GL::Bind bind_blend(GL::Blend::alpha());
546                         overlay->render(0);
547                 }
548
549                 GL::Bind bind_depth(GL::DepthTest::lequal());
550                 if(mode==MEASURE)
551                         measure.render();
552         }
553
554         GL::matrix_mode(GL::PROJECTION);
555         GL::load_identity();
556         GL::ortho_bottomleft(window.get_width(), window.get_height());
557         GL::matrix_mode(GL::MODELVIEW);
558         GL::load_identity();
559
560         GL::disable(GL::DEPTH_TEST);
561
562         GL::Bind bind_blend(GL::Blend::alpha());
563         root.render();
564         GL::Texture::unbind();
565 }
566
567 void Designer::track_added(Track &trk)
568 {
569         new_tracks.push_back(&trk);
570 }
571
572 void Designer::track_removed(Track &trk)
573 {
574         list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
575         if(i!=new_tracks.end())
576                 new_tracks.erase(i);
577 }
578
579 Track3D *Designer::pick_track(int x, int y)
580 {
581         Layout3D *l = layout_3d;
582         if(mode==CATALOGUE)
583                 l = cat_layout_3d;
584
585         float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
586         float yy = (float(y)/window.get_height()-0.5)*0.82843;
587         float size = 4.0/window.get_height()*0.82843;
588
589         apply_camera();
590
591         return l->pick_track(xx, yy, size);
592 }
593
594 void Designer::update_track_icon(Track3D &track)
595 {
596         overlay->clear(track);
597
598         if(track.get_track().get_flex())
599                 overlay->add_graphic(track, "flex");
600
601         if(unsigned sid = track.get_track().get_sensor_id())
602         {
603                 overlay->add_graphic(track, "sensor");
604                 overlay->set_label(track, lexical_cast(sid));
605         }
606         else if(unsigned tid = track.get_track().get_turnout_id())
607         {
608                 if(tid<0x800)
609                 {
610                         overlay->add_graphic(track, "turnout");
611                         overlay->set_label(track, lexical_cast(tid));
612                 }
613         }
614 }
615
616 void Designer::selection_changed()
617 {
618         const set<Track *> &tracks = selection.get_tracks();
619         if(tracks.empty())
620                 lbl_status->set_text(string());
621         else
622         {
623                 float len = 0;
624                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
625                         len += (*i)->get_type().get_total_length();
626                 lbl_status->set_text(format("%.2fm of track selected\n", len));
627         }
628 }
629
630 void Designer::manipulation_status(const string &status)
631 {
632         lbl_status->set_text(status);
633 }
634
635 void Designer::manipulation_done(bool)
636 {
637         mode = MANIPULATE_DONE;
638         selection_changed();
639 }
640
641 void Designer::measure_changed()
642 {
643         float pard = measure.get_parallel_distance()*1000;
644         float perpd = measure.get_perpendicular_distance()*1000;
645         float d = sqrt(pard*pard+perpd*perpd);
646         float adiff = measure.get_angle_difference()*180/M_PI;
647         string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
648         lbl_status->set_text(info);
649 }
650
651 void Designer::measure_done()
652 {
653         mode = SELECT;
654         selection_changed();
655 }
656
657 void Designer::arrange_toolbars()
658 {
659         unsigned x = 0;
660         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
661         {
662                 const GLtk::Geometry &geom = (*i)->get_geometry();
663                 (*i)->set_position(x, window.get_height()-geom.h);
664                 x += geom.w;
665         }
666 }
667
668 void Designer::track_properties_response(int)
669 {
670         const set<Track *> &tracks = selection.get_tracks();
671         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
672                 update_track_icon(layout_3d->get_track(**i));
673 }
674
675 void Designer::route_name_accept(const string &text)
676 {
677         if(cur_route)
678                 cur_route->set_name(text);
679 }
680
681 void Designer::svg_export_accept(const string &text)
682 {
683         SvgExporter svg_exp(*layout);
684         svg_exp.save(text);
685 }
686
687 string Designer::tooltip(int x, int y)
688 {
689         if(Track3D *t3d = pick_track(x, y))
690         {
691                 const Track &track = t3d->get_track();
692                 const TrackType &ttype = track.get_type();
693                 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
694                 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
695                         info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
696                 if(track.get_turnout_id())
697                         info += format(" (turnout %d)", track.get_turnout_id());
698                 else if(track.get_sensor_id())
699                         info += format(" (sensor %d)", track.get_sensor_id());
700
701                 return info;
702         }
703
704         return string();
705 }
706
707 void Designer::clear_paths()
708 {
709         const set<Track *> &ltracks = layout->get_tracks();
710         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
711         {
712                 Track3D &t3d = layout_3d->get_track(**i);
713                 t3d.get_path().set_mask(0);
714         }
715 }
716
717 void Designer::show_route(const Route &route)
718 {
719         clear_paths();
720
721         const set<Track *> &rtracks = route.get_tracks();
722         for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
723         {
724                 Track3D &t3d = layout_3d->get_track(**i);
725                 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
726                 int path = -1;
727                 if(unsigned tid = (*i)->get_turnout_id())
728                         path = route.get_turnout(tid);
729                 if(path>=0)
730                         t3d.get_path().set_path(path);
731                 else
732                         t3d.get_path().set_mask((*i)->get_type().get_paths());
733         }
734 }
735
736 void Designer::show_zone(const Zone &zone)
737 {
738         clear_paths();
739
740         const Zone::TrackSet &ztracks = zone.get_tracks();
741         for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
742         {
743                 Track3D &t3d = layout_3d->get_track(**i);
744                 t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
745                 t3d.get_path().set_mask((*i)->get_type().get_paths());
746         }
747 }