]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Introduce a tilt (vertical angle) property to Object
[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/tests.h>
10 #include <msp/gl/texture2d.h>
11 #include <msp/input/keys.h>
12 #include <msp/io/print.h>
13 #include <msp/stringcodec/codec.h>
14 #include <msp/stringcodec/utf8.h>
15 #include <msp/strings/lexicalcast.h>
16 #include <msp/strings/utils.h>
17 #include <msp/time/units.h>
18 #include <msp/time/utils.h>
19 #include "libr2c2/route.h"
20 #include "libr2c2/tracktype.h"
21 #include "libr2c2/zone.h"
22 #include "3d/path.h"
23 #include "designer.h"
24 #include "input.h"
25 #include "layoutbar.h"
26 #include "manipulator.h"
27 #include "measure.h"
28 #include "routebar.h"
29 #include "selection.h"
30 #include "svgexporter.h"
31 #include "trackbar.h"
32 #include "trackproperties.h"
33 #include "zonebar.h"
34 #include "zoneproperties.h"
35
36 using namespace std;
37 using namespace R2C2;
38 using namespace Msp;
39
40 Designer::Designer(int argc, char **argv):
41         window(1280, 960),
42         keyboard(window),
43         mouse(window),
44         ui_res("r2c2.res"),
45         root(ui_res, &window, &keyboard, &mouse),
46         base_object(0),
47         cur_route(0),
48         cur_zone(0),
49         mode(SELECT),
50         sel_wrap(selection),
51         manipulator(*this, mouse, selection),
52         measure(*this)
53 {
54         window.set_title("Railway Designer");
55         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
56
57         selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
58         manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
59         manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
60         measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
61         measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
62
63         // Setup catalogue and layout
64         DataFile::load(catalogue, "tracks.dat");
65
66         cat_layout_3d = new Layout3D(catalogue.get_layout());
67
68         layout = new Layout(catalogue);
69         layout_3d = new Layout3D(*layout);
70
71         layout->signal_object_added.connect(sigc::mem_fun(this, &Designer::object_added));
72         layout->signal_object_removed.connect(sigc::mem_fun(this, &Designer::object_removed));
73
74         if(argc>1)
75         {
76                 filename = argv[1];
77                 DataFile::load(*layout, argv[1]);
78
79                 if(!layout->get_base().empty())
80                 {
81                         base_object = new GL::Object;
82                         DataFile::load(*base_object, layout->get_base());
83                 }
84         }
85
86         // Setup OpenGL
87         cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
88
89         main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
90         GL::Pipeline *pipeline = &main_view->get_pipeline();
91
92         GL::Pipeline::Pass *pass = &pipeline->add_pass("unlit");
93         pass->set_depth_test(&GL::DepthTest::lequal());
94         pass->set_blend(&GL::Blend::alpha());
95
96         pass = &pipeline->add_pass("blended");
97         pass->set_lighting(&layout_3d->get_lighting());
98         pass->set_depth_test(&GL::DepthTest::lequal());
99         pass->set_blend(&GL::Blend::alpha());
100
101         pass = &pipeline->add_pass("overlay");
102         pass->set_blend(&GL::Blend::alpha());
103
104         if(base_object)
105                 pipeline->add_renderable(*base_object);
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         // Setup UI
110         keyboard.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Designer::key_press), false));
111         mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Designer::button_press), false));
112         mouse.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Designer::axis_motion), false));
113         root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
114         root_layout = new GLtk::Layout;
115         root_layout->set_margin(0);
116         root.set_layout(root_layout);
117
118         toolbars.push_back(new Layoutbar(*this));
119         toolbars.push_back(new Trackbar(*this));
120         toolbars.push_back(new Routebar(*this));
121         toolbars.push_back(new Zonebar(*this));
122         Toolbar *prev = 0;
123         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
124         {
125                 root.add(**i);
126                 if(i!=toolbars.begin())
127                         (*i)->expand(false);
128                 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(root_layout, &GLtk::Layout::update)));
129                 if(prev)
130                 {
131                         root_layout->add_constraint(**i, GLtk::Layout::RIGHT_OF, *prev, 0);
132                         root_layout->add_constraint(**i, GLtk::Layout::COPY_HEIGHT, *prev);
133                 }
134                 prev = *i;
135         }
136
137         GLtk::Panel *statusbar = new GLtk::Panel;
138         root.add(*statusbar);
139         statusbar->set_focusable(false);
140         root_layout->set_gravity(*statusbar, -1, -1);
141         root_layout->set_expand(*statusbar, true, false);
142
143         lbl_status = new GLtk::Label;
144         statusbar->add(*lbl_status);
145         lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
146
147         pipeline->add_renderable_for_pass(sel_wrap, "unlit");
148         overlay = new Overlay3D(ui_res.get_default_font());
149         pipeline->add_renderable_for_pass(*overlay, "overlay");
150         pipeline->add_renderable_for_pass(measure, "unlit");
151
152         camera_ctl = new CameraController(*main_view, keyboard, mouse);
153         cat_view->get_camera().set_look_direction(GL::Vector3(0, 0.13053, -0.99144));
154         cat_view->view_all(true);
155         main_view->view_all();
156
157         const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
158         for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
159                 update_track_icon(*i->second);
160 }
161
162 Designer::~Designer()
163 {
164         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
165                 delete *i;
166         delete camera_ctl;
167         delete overlay;
168         delete main_view;
169         delete base_object;
170         delete layout_3d;
171         delete layout;
172         delete cat_view;
173         delete cat_layout_3d;
174 }
175
176 int Designer::main()
177 {
178         window.show();
179
180         mode = SELECT;
181
182         return Application::main();
183 }
184
185 void Designer::save()
186 {
187         InputDialog *input = new InputDialog(*this, "Save layout", filename);
188         input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
189         root.add(*input);
190         root_layout->set_gravity(*input, 0, 0);
191 }
192
193 void Designer::quit()
194 {
195         exit(0);
196 }
197
198 void Designer::new_track()
199 {
200         if(mode!=SELECT)
201                 return;
202
203         mode = CATALOGUE;
204         lbl_status->set_text("Select new track or press Esc to cancel");
205 }
206
207 void Designer::erase_tracks()
208 {
209         if(mode!=SELECT)
210                 return;
211
212         set<Track *> tracks = selection.get_objects<Track>();
213         selection.clear();
214         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
215         {
216                 overlay->clear(layout_3d->get_track(**i));
217                 delete *i;
218         }
219 }
220
221 void Designer::track_properties()
222 {
223         if(mode!=SELECT || selection.empty())
224                 return;
225
226         TrackProperties *track_prop = new TrackProperties(selection);
227         root.add(*track_prop);
228         root_layout->set_gravity(*track_prop, 0, 0);
229
230         track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
231 }
232
233 void Designer::extend_track()
234 {
235         if(mode!=SELECT)
236                 return;
237
238         if(manipulator.start_extend())
239                 mode = MANIPULATE;
240 }
241
242 void Designer::connect_tracks()
243 {
244         if(mode!=SELECT)
245                 return;
246
247         manipulator.connect();
248 }
249
250 void Designer::flatten_tracks()
251 {
252         if(mode!=SELECT)
253                 return;
254
255         manipulator.flatten();
256 }
257
258 void Designer::svg_export()
259 {
260         InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
261         input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
262         root.add(*input);
263         root_layout->set_gravity(*input, 0, 0);
264 }
265
266 void Designer::edit_route(Route *r)
267 {
268         cur_route = r;
269         cur_zone = 0;
270         if(cur_route)
271                 show_route(*cur_route);
272         else
273                 clear_paths();
274 }
275
276 void Designer::rename_route()
277 {
278         if(mode!=SELECT || !cur_route)
279                 return;
280
281         InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
282         input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
283         root.add(*input);
284         root_layout->set_gravity(*input, 0, 0);
285 }
286
287 void Designer::add_selection_to_route()
288 {
289         if(!cur_route)
290                 return;
291
292         try
293         {
294                 cur_route->add_tracks(selection.get_objects<Track>());
295         }
296         catch(const exception &e)
297         {
298                 lbl_status->set_text(e.what());
299         }
300
301         show_route(*cur_route);
302 }
303
304 void Designer::edit_zone(Zone *z)
305 {
306         cur_zone = z;
307         cur_route = 0;
308         if(cur_zone)
309                 show_zone(*cur_zone);
310         else
311                 clear_paths();
312 }
313
314 void Designer::zone_properties()
315 {
316         if(!cur_zone)
317                 return;
318
319         ZoneProperties *zone_prop = new ZoneProperties(*cur_zone);
320         root.add(*zone_prop);
321         const GLtk::Geometry &root_geom = root.get_geometry();
322         const GLtk::Geometry &dlg_geom = zone_prop->get_geometry();
323         zone_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
324 }
325
326 void Designer::add_selection_to_zone()
327 {
328         if(!cur_zone)
329                 return;
330
331         try
332         {
333                 cur_zone->add_tracks(selection.get_objects<Track>());
334         }
335         catch(const exception &e)
336         {
337                 lbl_status->set_text(e.what());
338         }
339
340         show_zone(*cur_zone);
341 }
342
343 Vector Designer::map_pointer_to_ground(const Vector &p)
344 {
345         GL::Vector4 vec = main_view->get_camera().unproject(GL::Vector4(p.x, p.y, 0, 0));
346         const GL::Vector3 &pos = main_view->get_camera().get_position();
347
348         return pos-GL::Vector3(vec.x, vec.y, vec.z)*(pos.z/vec.z);
349 }
350
351 void Designer::tick()
352 {
353         const Msp::Time::TimeStamp t = Msp::Time::now();
354         float dt = (t-last_tick)/Msp::Time::sec;
355         last_tick = t;
356
357         if(mode==MANIPULATE_DONE)
358                 mode = SELECT;
359
360         window.tick();
361         root.tick();
362         camera_ctl->tick(dt);
363
364         for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
365                 layout_3d->get_track(**i).get_path().set_mask(0);
366         new_tracks.clear();
367
368         render();
369
370         window.swap_buffers();
371 }
372
373 void Designer::key_press(unsigned key)
374 {
375         bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
376
377         if(key==Msp::Input::KEY_N && 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 && 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 && shift)
427         {
428                 const set<Track *> &tracks = selection.get_objects<Track>();
429                 const set<Track *> &ltracks = layout->get_all<Track>();
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 && 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(unsigned btn)
463 {
464         bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
465
466         Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
467         Vector ground = map_pointer_to_ground(pointer);
468
469         if(mode==CATALOGUE)
470         {
471                 if(btn==1)
472                 {
473                         Object *cobj = pick_object(pointer);
474                         if(cobj)
475                         {
476                                 Object *obj = cobj->clone(layout);
477                                 obj->set_position(ground);
478
479                                 selection.clear();
480                                 selection.add_object(obj);
481
482                                 mode = SELECT;
483                         }
484                 }
485                 else
486                         mode = SELECT;
487         }
488         else if(mode==SELECT)
489         {
490                 if(btn==1)
491                 {
492                         Object *obj = pick_object(pointer);
493                         if(obj)
494                         {
495                                 if(!shift)
496                                         selection.clear();
497                                 selection.toggle_object(obj);
498                         }
499                 }
500         }
501         else if(mode==MEASURE)
502                 measure.button_press(ground.x, ground.y, btn);
503 }
504
505 void Designer::axis_motion(unsigned, float, float)
506 {
507         Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
508         float wx = pointer.x*window.get_width();
509         float wy = pointer.y*window.get_height();
510
511         if(!root.get_child_at(wx, wy))
512         {
513                 Vector ground = map_pointer_to_ground(pointer);
514                 measure.pointer_motion(ground.x, ground.y);
515         }
516 }
517
518 void Designer::render()
519 {
520         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
521
522         View3D *view = (mode==CATALOGUE ? cat_view : main_view);
523         view->render();
524
525         root.render();
526         // XXX Should fix GLtk so this would not be needed
527         glColor3f(1.0, 1.0, 1.0);
528 }
529
530 void Designer::object_added(Object &obj)
531 {
532         if(Track *trk = dynamic_cast<Track *>(&obj))
533                 new_tracks.push_back(trk);
534 }
535
536 void Designer::object_removed(Object &obj)
537 {
538         if(Track *trk = dynamic_cast<Track *>(&obj))
539         {
540                 list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), trk);
541                 if(i!=new_tracks.end())
542                         new_tracks.erase(i);
543         }
544 }
545
546 Object *Designer::pick_object(const Vector &pointer)
547 {
548         View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
549         const GL::Vector3 &cpos = view.get_camera().get_position();
550         GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(pointer.x, pointer.y, 0, 0));
551
552         return view.get_layout().get_layout().pick<Object>(Ray(cpos, Vector(cray)));
553 }
554
555 void Designer::update_track_icon(Track3D &track)
556 {
557         overlay->clear(track);
558
559         if(track.get_track().get_flex())
560                 overlay->add_graphic(track, "flex");
561
562         if(unsigned sid = track.get_track().get_sensor_id())
563         {
564                 overlay->add_graphic(track, "sensor");
565                 overlay->set_label(track, lexical_cast<string>(sid));
566         }
567         else if(unsigned tid = track.get_track().get_turnout_id())
568         {
569                 if(tid<0x800)
570                 {
571                         overlay->add_graphic(track, "turnout");
572                         overlay->set_label(track, lexical_cast<string>(tid));
573                 }
574         }
575 }
576
577 void Designer::selection_changed()
578 {
579         const set<Track *> &tracks = selection.get_objects<Track>();
580         if(tracks.empty())
581                 lbl_status->set_text(string());
582         else
583         {
584                 float len = 0;
585                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
586                         len += (*i)->get_type().get_total_length();
587                 lbl_status->set_text(format("%.2fm of track selected\n", len));
588         }
589 }
590
591 void Designer::manipulation_status(const string &status)
592 {
593         lbl_status->set_text(status);
594 }
595
596 void Designer::manipulation_done(bool)
597 {
598         mode = MANIPULATE_DONE;
599         selection_changed();
600 }
601
602 void Designer::measure_changed()
603 {
604         float pard = measure.get_parallel_distance()*1000;
605         float perpd = measure.get_perpendicular_distance()*1000;
606         float d = sqrt(pard*pard+perpd*perpd);
607         const Angle &adiff = measure.get_angle_difference();
608         string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff.degrees());
609         lbl_status->set_text(info);
610 }
611
612 void Designer::measure_done()
613 {
614         mode = SELECT;
615         selection_changed();
616 }
617
618 void Designer::track_properties_response(int)
619 {
620         const set<Track *> &tracks = selection.get_objects<Track>();
621         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
622                 update_track_icon(layout_3d->get_track(**i));
623 }
624
625 void Designer::route_name_accept(const string &text)
626 {
627         if(cur_route)
628                 cur_route->set_name(text);
629 }
630
631 void Designer::svg_export_accept(const string &text)
632 {
633         SvgExporter svg_exp(*layout);
634         svg_exp.save(text);
635 }
636
637 string Designer::tooltip(int x, int y)
638 {
639         const GLtk::Geometry &rgeom = root.get_geometry();
640         if(Object *obj = pick_object(Vector(x*2.0f/rgeom.w-1.0f, y*2.0f/rgeom.h-1.0f, 0)))
641         {
642                 const ObjectType &otype = obj->get_type();
643                 string info = format("%d %s", otype.get_article_number(), otype.get_description());
644                 if(Track *track = dynamic_cast<Track *>(obj))
645                 {
646                         if(mode!=CATALOGUE && abs(track->get_tilt()).radians()>1e-4)
647                                 info += format(" (slope %.1f%%)", abs(tan(track->get_tilt())*100));
648                         if(track->get_turnout_id())
649                                 info += format(" (turnout %d)", track->get_turnout_id());
650                         else if(track->get_sensor_id())
651                                 info += format(" (sensor %d)", track->get_sensor_id());
652                 }
653                 return info;
654         }
655
656         return string();
657 }
658
659 void Designer::clear_paths()
660 {
661         const set<Track *> &ltracks = layout->get_all<Track>();
662         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
663         {
664                 Track3D &t3d = layout_3d->get_track(**i);
665                 t3d.get_path().set_mask(0);
666         }
667 }
668
669 void Designer::show_route(const Route &route)
670 {
671         clear_paths();
672
673         const set<Track *> &rtracks = route.get_tracks();
674         for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
675         {
676                 Track3D &t3d = layout_3d->get_track(**i);
677                 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
678                 int path = -1;
679                 if(unsigned tid = (*i)->get_turnout_id())
680                         path = route.get_turnout(tid);
681                 if(path>=0)
682                         t3d.get_path().set_path(path);
683                 else
684                         t3d.get_path().set_mask((*i)->get_type().get_paths());
685         }
686 }
687
688 void Designer::show_zone(const Zone &zone)
689 {
690         clear_paths();
691
692         const Zone::TrackSet &ztracks = zone.get_tracks();
693         for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
694         {
695                 Track3D &t3d = layout_3d->get_track(**i);
696                 t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
697                 t3d.get_path().set_mask((*i)->get_type().get_paths());
698         }
699 }