]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Make use of the geometry part of libmspmath
[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_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
72         layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_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
115         toolbars.push_back(new Layoutbar(*this));
116         toolbars.push_back(new Trackbar(*this));
117         toolbars.push_back(new Routebar(*this));
118         toolbars.push_back(new Zonebar(*this));
119         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
120         {
121                 root.add(**i);
122                 if(i!=toolbars.begin())
123                         (*i)->expand(false);
124                 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(this, &Designer::arrange_toolbars)));
125         }
126         arrange_toolbars();
127
128         GLtk::Panel *statusbar = new GLtk::Panel;
129         root.add(*statusbar);
130         statusbar->set_size(window.get_width(), 20);
131         statusbar->set_focusable(false);
132
133         lbl_status = new GLtk::Label;
134         statusbar->add(*lbl_status);
135         lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
136
137         pipeline->add_renderable_for_pass(sel_wrap, "unlit");
138         overlay = new Overlay3D(ui_res.get_default_font());
139         pipeline->add_renderable_for_pass(*overlay, "overlay");
140         pipeline->add_renderable_for_pass(measure, "unlit");
141
142         camera_ctl = new CameraController(*main_view, keyboard, mouse);
143         cat_view->get_camera().set_look_direction(GL::Vector3(0, 0.13053, -0.99144));
144         cat_view->view_all(true);
145         main_view->view_all();
146
147         const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
148         for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
149                 update_track_icon(*i->second);
150 }
151
152 Designer::~Designer()
153 {
154         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
155                 delete *i;
156         delete camera_ctl;
157         delete overlay;
158         delete main_view;
159         delete base_object;
160         delete layout_3d;
161         delete layout;
162         delete cat_view;
163         delete cat_layout_3d;
164 }
165
166 int Designer::main()
167 {
168         window.show();
169
170         mode = SELECT;
171
172         return Application::main();
173 }
174
175 void Designer::save()
176 {
177         InputDialog *input = new InputDialog(*this, "Save layout", filename);
178         input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
179 }
180
181 void Designer::quit()
182 {
183         exit(0);
184 }
185
186 void Designer::new_track()
187 {
188         if(mode!=SELECT)
189                 return;
190
191         mode = CATALOGUE;
192         lbl_status->set_text("Select new track or press Esc to cancel");
193 }
194
195 void Designer::erase_tracks()
196 {
197         if(mode!=SELECT)
198                 return;
199
200         set<Track *> tracks = selection.get_objects<Track>();
201         selection.clear();
202         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
203         {
204                 overlay->clear(layout_3d->get_track(**i));
205                 delete *i;
206         }
207 }
208
209 void Designer::track_properties()
210 {
211         if(mode!=SELECT || selection.empty())
212                 return;
213
214         TrackProperties *track_prop = new TrackProperties(selection);
215         root.add(*track_prop);
216         const GLtk::Geometry &root_geom = root.get_geometry();
217         const GLtk::Geometry &dlg_geom = track_prop->get_geometry();
218         track_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
219
220         track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
221 }
222
223 void Designer::extend_track()
224 {
225         if(mode!=SELECT)
226                 return;
227
228         if(manipulator.start_extend())
229                 mode = MANIPULATE;
230 }
231
232 void Designer::connect_tracks()
233 {
234         if(mode!=SELECT)
235                 return;
236
237         manipulator.connect();
238 }
239
240 void Designer::flatten_tracks()
241 {
242         if(mode!=SELECT)
243                 return;
244
245         manipulator.flatten();
246 }
247
248 void Designer::svg_export()
249 {
250         InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
251         input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
252 }
253
254 void Designer::edit_route(Route *r)
255 {
256         cur_route = r;
257         cur_zone = 0;
258         if(cur_route)
259                 show_route(*cur_route);
260         else
261                 clear_paths();
262 }
263
264 void Designer::rename_route()
265 {
266         if(mode!=SELECT || !cur_route)
267                 return;
268
269         InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
270         input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
271 }
272
273 void Designer::add_selection_to_route()
274 {
275         if(!cur_route)
276                 return;
277
278         try
279         {
280                 cur_route->add_tracks(selection.get_objects<Track>());
281         }
282         catch(const exception &e)
283         {
284                 lbl_status->set_text(e.what());
285         }
286
287         show_route(*cur_route);
288 }
289
290 void Designer::edit_zone(Zone *z)
291 {
292         cur_zone = z;
293         cur_route = 0;
294         if(cur_zone)
295                 show_zone(*cur_zone);
296         else
297                 clear_paths();
298 }
299
300 void Designer::zone_properties()
301 {
302         if(!cur_zone)
303                 return;
304
305         ZoneProperties *zone_prop = new ZoneProperties(*cur_zone);
306         root.add(*zone_prop);
307         const GLtk::Geometry &root_geom = root.get_geometry();
308         const GLtk::Geometry &dlg_geom = zone_prop->get_geometry();
309         zone_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
310 }
311
312 void Designer::add_selection_to_zone()
313 {
314         if(!cur_zone)
315                 return;
316
317         try
318         {
319                 cur_zone->add_tracks(selection.get_objects<Track>());
320         }
321         catch(const exception &e)
322         {
323                 lbl_status->set_text(e.what());
324         }
325
326         show_zone(*cur_zone);
327 }
328
329 Vector Designer::map_pointer_to_ground(const Vector &p)
330 {
331         GL::Vector4 vec = main_view->get_camera().unproject(GL::Vector4(p.x, p.y, 0, 0));
332         const GL::Vector3 &pos = main_view->get_camera().get_position();
333
334         return pos-GL::Vector3(vec.x, vec.y, vec.z)*(pos.z/vec.z);
335 }
336
337 void Designer::tick()
338 {
339         const Msp::Time::TimeStamp t = Msp::Time::now();
340         float dt = (t-last_tick)/Msp::Time::sec;
341         last_tick = t;
342
343         if(mode==MANIPULATE_DONE)
344                 mode = SELECT;
345
346         window.tick();
347         root.tick();
348         camera_ctl->tick(dt);
349
350         for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
351                 layout_3d->get_track(**i).get_path().set_mask(0);
352         new_tracks.clear();
353
354         render();
355
356         window.swap_buffers();
357 }
358
359 void Designer::key_press(unsigned key)
360 {
361         bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
362
363         if(key==Msp::Input::KEY_N && shift)
364                 extend_track();
365         else if(key==Msp::Input::KEY_N)
366                 new_track();
367         else if(key==Msp::Input::KEY_G)
368         {
369                 manipulator.start_move();
370                 mode = MANIPULATE;
371         }
372         else if(key==Msp::Input::KEY_R)
373         {
374                 manipulator.start_rotate();
375                 mode = MANIPULATE;
376         }
377         else if(key==Msp::Input::KEY_D)
378         {
379                 manipulator.duplicate();
380                 manipulator.start_move();
381                 mode = MANIPULATE;
382         }
383         else if(key==Msp::Input::KEY_W)
384                 save();
385         else if(key==Msp::Input::KEY_PLUS)
386                 selection.select_more();
387         else if(key==Msp::Input::KEY_L && shift)
388                 selection.select_blocks();
389         else if(key==Msp::Input::KEY_L)
390                 selection.select_linked();
391         else if(key==Msp::Input::KEY_M)
392         {
393                 measure.start();
394                 mode = MEASURE;
395         }
396         else if(key==Msp::Input::KEY_Z)
397         {
398                 manipulator.start_elevate();
399                 mode = MANIPULATE;
400         }
401         else if(key==Msp::Input::KEY_ESC)
402         {
403                 if(mode==MANIPULATE)
404                         manipulator.cancel();
405                 else if(mode==CATALOGUE)
406                         mode = SELECT;
407                 else
408                         selection.clear();
409         }
410         else if(key==Msp::Input::KEY_X)
411                 erase_tracks();
412         else if(key==Msp::Input::KEY_F && shift)
413         {
414                 const set<Track *> &tracks = selection.get_objects<Track>();
415                 const set<Track *> &ltracks = layout->get_tracks();
416                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
417                 {
418                         (*i)->set_flex(!(*i)->get_flex());
419                         (*i)->break_links();
420                         for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
421                                 if(*j!=*i)
422                                         (*i)->snap_to(**j, true);
423
424                         update_track_icon(layout_3d->get_track(**i));
425                 }
426         }
427         else if(key==Msp::Input::KEY_F)
428                 flatten_tracks();
429         else if(key==Msp::Input::KEY_E && shift)
430                 manipulator.even_slope(true);
431         else if(key==Msp::Input::KEY_E)
432                 manipulator.even_slope();
433         else if(key==Msp::Input::KEY_A)
434         {
435                 if(cur_route)
436                         add_selection_to_route();
437                 else if(cur_zone)
438                         add_selection_to_zone();
439         }
440         else if(key==Msp::Input::KEY_C)
441                 connect_tracks();
442         else if(key==Msp::Input::KEY_V)
443                 svg_export();
444         else if(key==Msp::Input::KEY_P)
445                 track_properties();
446 }
447
448 void Designer::button_press(unsigned btn)
449 {
450         bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
451
452         Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
453         Vector ground = map_pointer_to_ground(pointer);
454
455         if(mode==CATALOGUE)
456         {
457                 if(btn==1)
458                 {
459                         Object *cobj = pick_object(pointer);
460                         if(cobj)
461                         {
462                                 Object *obj = cobj->clone(layout);
463                                 obj->set_position(ground);
464
465                                 selection.clear();
466                                 selection.add_object(obj);
467
468                                 mode = SELECT;
469                         }
470                 }
471                 else
472                         mode = SELECT;
473         }
474         else if(mode==SELECT)
475         {
476                 if(btn==1)
477                 {
478                         Object *obj = pick_object(pointer);
479                         if(obj)
480                         {
481                                 if(!shift)
482                                         selection.clear();
483                                 selection.toggle_object(obj);
484                         }
485                 }
486         }
487         else if(mode==MEASURE)
488                 measure.button_press(ground.x, ground.y, btn);
489 }
490
491 void Designer::axis_motion(unsigned, float, float)
492 {
493         Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
494         float wx = pointer.x*window.get_width();
495         float wy = pointer.y*window.get_height();
496
497         if(!root.get_child_at(wx, wy))
498         {
499                 Vector ground = map_pointer_to_ground(pointer);
500                 measure.pointer_motion(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
511         root.render();
512         // XXX Should fix GLtk so this would not be needed
513         glColor3f(1.0, 1.0, 1.0);
514 }
515
516 void Designer::track_added(Track &trk)
517 {
518         new_tracks.push_back(&trk);
519 }
520
521 void Designer::track_removed(Track &trk)
522 {
523         list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
524         if(i!=new_tracks.end())
525                 new_tracks.erase(i);
526 }
527
528 Object *Designer::pick_object(const Vector &pointer)
529 {
530         View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
531         const GL::Vector3 &cpos = view.get_camera().get_position();
532         GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(pointer.x, pointer.y, 0, 0));
533
534         return view.get_layout().get_layout().pick_object(Ray(cpos, Vector(cray)));
535 }
536
537 void Designer::update_track_icon(Track3D &track)
538 {
539         overlay->clear(track);
540
541         if(track.get_track().get_flex())
542                 overlay->add_graphic(track, "flex");
543
544         if(unsigned sid = track.get_track().get_sensor_id())
545         {
546                 overlay->add_graphic(track, "sensor");
547                 overlay->set_label(track, lexical_cast<string>(sid));
548         }
549         else if(unsigned tid = track.get_track().get_turnout_id())
550         {
551                 if(tid<0x800)
552                 {
553                         overlay->add_graphic(track, "turnout");
554                         overlay->set_label(track, lexical_cast<string>(tid));
555                 }
556         }
557 }
558
559 void Designer::selection_changed()
560 {
561         const set<Track *> &tracks = selection.get_objects<Track>();
562         if(tracks.empty())
563                 lbl_status->set_text(string());
564         else
565         {
566                 float len = 0;
567                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
568                         len += (*i)->get_type().get_total_length();
569                 lbl_status->set_text(format("%.2fm of track selected\n", len));
570         }
571 }
572
573 void Designer::manipulation_status(const string &status)
574 {
575         lbl_status->set_text(status);
576 }
577
578 void Designer::manipulation_done(bool)
579 {
580         mode = MANIPULATE_DONE;
581         selection_changed();
582 }
583
584 void Designer::measure_changed()
585 {
586         float pard = measure.get_parallel_distance()*1000;
587         float perpd = measure.get_perpendicular_distance()*1000;
588         float d = sqrt(pard*pard+perpd*perpd);
589         const Angle &adiff = measure.get_angle_difference();
590         string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff.degrees());
591         lbl_status->set_text(info);
592 }
593
594 void Designer::measure_done()
595 {
596         mode = SELECT;
597         selection_changed();
598 }
599
600 void Designer::arrange_toolbars()
601 {
602         unsigned x = 0;
603         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
604         {
605                 const GLtk::Geometry &geom = (*i)->get_geometry();
606                 (*i)->set_position(x, window.get_height()-geom.h);
607                 x += geom.w;
608         }
609 }
610
611 void Designer::track_properties_response(int)
612 {
613         const set<Track *> &tracks = selection.get_objects<Track>();
614         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
615                 update_track_icon(layout_3d->get_track(**i));
616 }
617
618 void Designer::route_name_accept(const string &text)
619 {
620         if(cur_route)
621                 cur_route->set_name(text);
622 }
623
624 void Designer::svg_export_accept(const string &text)
625 {
626         SvgExporter svg_exp(*layout);
627         svg_exp.save(text);
628 }
629
630 string Designer::tooltip(int x, int y)
631 {
632         if(Object *obj = pick_object(Vector(x*2.0f/window.get_width()-1.0f, y*2.0f/window.get_height()-1.0f, 0)))
633         {
634                 const ObjectType &otype = obj->get_type();
635                 string info = format("%d %s", otype.get_article_number(), otype.get_description());
636                 if(Track *track = dynamic_cast<Track *>(obj))
637                 {
638                         const TrackType &ttype = track->get_type();
639                         if(mode!=CATALOGUE && abs(track->get_slope())>1e-4)
640                                 info += format(" (slope %.1f%%)", abs(track->get_slope()/ttype.get_total_length()*100));
641                         if(track->get_turnout_id())
642                                 info += format(" (turnout %d)", track->get_turnout_id());
643                         else if(track->get_sensor_id())
644                                 info += format(" (sensor %d)", track->get_sensor_id());
645                 }
646                 return info;
647         }
648
649         return string();
650 }
651
652 void Designer::clear_paths()
653 {
654         const set<Track *> &ltracks = layout->get_tracks();
655         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
656         {
657                 Track3D &t3d = layout_3d->get_track(**i);
658                 t3d.get_path().set_mask(0);
659         }
660 }
661
662 void Designer::show_route(const Route &route)
663 {
664         clear_paths();
665
666         const set<Track *> &rtracks = route.get_tracks();
667         for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
668         {
669                 Track3D &t3d = layout_3d->get_track(**i);
670                 t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
671                 int path = -1;
672                 if(unsigned tid = (*i)->get_turnout_id())
673                         path = route.get_turnout(tid);
674                 if(path>=0)
675                         t3d.get_path().set_path(path);
676                 else
677                         t3d.get_path().set_mask((*i)->get_type().get_paths());
678         }
679 }
680
681 void Designer::show_zone(const Zone &zone)
682 {
683         clear_paths();
684
685         const Zone::TrackSet &ztracks = zone.get_tracks();
686         for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
687         {
688                 Track3D &t3d = layout_3d->get_track(**i);
689                 t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
690                 t3d.get_path().set_mask((*i)->get_type().get_paths());
691         }
692 }