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