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