]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Split the Manipulator class into several Tools
[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 "elevatetool.h"
25 #include "extendtool.h"
26 #include "input.h"
27 #include "layoutbar.h"
28 #include "manipulator.h"
29 #include "measure.h"
30 #include "movetool.h"
31 #include "rotatetool.h"
32 #include "routebar.h"
33 #include "selection.h"
34 #include "slopetool.h"
35 #include "svgexporter.h"
36 #include "trackbar.h"
37 #include "trackproperties.h"
38 #include "zonebar.h"
39 #include "zoneproperties.h"
40
41 using namespace std;
42 using namespace R2C2;
43 using namespace Msp;
44
45 Designer::Designer(int argc, char **argv):
46         window(1280, 960),
47         keyboard(window),
48         mouse(window),
49         ui_res("r2c2.res"),
50         root(ui_res, &window, &keyboard, &mouse),
51         base_object(0),
52         cur_route(0),
53         cur_zone(0),
54         mode(SELECT),
55         sel_wrap(selection),
56         cur_tool(0)
57 {
58         window.set_title("Railway Designer");
59         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
60
61         selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
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         if(argc>1)
72         {
73                 filename = argv[1];
74                 DataFile::load(*layout, argv[1]);
75
76                 if(!layout->get_base().empty())
77                 {
78                         base_object = new GL::Object;
79                         DataFile::load(*base_object, layout->get_base());
80                 }
81         }
82
83         // Setup OpenGL
84         cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
85
86         main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
87         GL::Pipeline *pipeline = &main_view->get_pipeline();
88
89         GL::Pipeline::Pass *pass = &pipeline->add_pass("unlit");
90         pass->set_depth_test(&GL::DepthTest::lequal());
91         pass->set_blend(&GL::Blend::alpha());
92
93         pass = &pipeline->add_pass("blended");
94         pass->set_lighting(&layout_3d->get_lighting());
95         pass->set_depth_test(&GL::DepthTest::lequal());
96         pass->set_blend(&GL::Blend::alpha());
97
98         pass = &pipeline->add_pass("overlay");
99         pass->set_blend(&GL::Blend::alpha());
100
101         if(base_object)
102                 pipeline->add_renderable(*base_object);
103         pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
104         pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
105
106         // Setup UI
107         keyboard.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Designer::key_press), false));
108         mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Designer::button_press), false));
109         root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
110         root_layout = new GLtk::Layout;
111         root_layout->set_margin(0);
112         root.set_layout(root_layout);
113
114         toolbars.push_back(new Layoutbar(*this));
115         toolbars.push_back(new Trackbar(*this));
116         toolbars.push_back(new Routebar(*this));
117         toolbars.push_back(new Zonebar(*this));
118         Toolbar *prev = 0;
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(root_layout, &GLtk::Layout::update)));
125                 if(prev)
126                 {
127                         root_layout->add_constraint(**i, GLtk::Layout::RIGHT_OF, *prev, 0);
128                         root_layout->add_constraint(**i, GLtk::Layout::COPY_HEIGHT, *prev);
129                 }
130                 prev = *i;
131         }
132
133         GLtk::Panel *statusbar = new GLtk::Panel;
134         root.add(*statusbar);
135         statusbar->set_focusable(false);
136         root_layout->set_gravity(*statusbar, -1, -1);
137         root_layout->set_expand(*statusbar, true, false);
138
139         lbl_status = new GLtk::Label;
140         statusbar->add(*lbl_status);
141         lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
142
143         pipeline->add_renderable_for_pass(sel_wrap, "unlit");
144         overlay = new Overlay3D(ui_res.get_default_font());
145         pipeline->add_renderable_for_pass(*overlay, "overlay");
146
147         camera_ctl = new CameraController(*main_view, keyboard, mouse);
148         cat_view->get_camera().set_look_direction(GL::Vector3(0, 0.13053, -0.99144));
149         cat_view->view_all(true);
150         main_view->view_all();
151
152         const set<Track *> &tracks = layout->get_all<Track>();
153         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
154                 update_track_icon(layout_3d->get<Track3D>(**i));
155 }
156
157 Designer::~Designer()
158 {
159         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
160                 delete *i;
161         delete camera_ctl;
162         delete overlay;
163         delete main_view;
164         delete base_object;
165         delete layout_3d;
166         delete layout;
167         delete cat_view;
168         delete cat_layout_3d;
169 }
170
171 int Designer::main()
172 {
173         window.show();
174
175         mode = SELECT;
176
177         return Application::main();
178 }
179
180 void Designer::save()
181 {
182         InputDialog *input = new InputDialog(*this, "Save layout", filename);
183         input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
184         root.add(*input);
185         root_layout->set_gravity(*input, 0, 0);
186 }
187
188 void Designer::quit()
189 {
190         exit(0);
191 }
192
193 void Designer::new_track()
194 {
195         if(mode!=SELECT)
196                 return;
197
198         mode = CATALOGUE;
199         lbl_status->set_text("Select new track or press Esc to cancel");
200 }
201
202 void Designer::erase_tracks()
203 {
204         if(mode!=SELECT)
205                 return;
206
207         set<Track *> tracks = selection.get_objects<Track>();
208         selection.clear();
209         for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
210         {
211                 overlay->clear(layout_3d->get<Track3D>(**i));
212                 delete *i;
213         }
214 }
215
216 void Designer::track_properties()
217 {
218         if(mode!=SELECT || selection.empty())
219                 return;
220
221         TrackProperties *track_prop = new TrackProperties(selection);
222         root.add(*track_prop);
223         root_layout->set_gravity(*track_prop, 0, 0);
224
225         track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
226 }
227
228 void Designer::extend_track()
229 {
230         use_tool<ExtendTool>();
231 }
232
233 void Designer::connect_tracks()
234 {
235         use_tool<ExtendTool>()->connect();
236 }
237
238 void Designer::flatten_tracks()
239 {
240         use_tool<SlopeTool>()->flatten();
241 }
242
243 void Designer::svg_export()
244 {
245         InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
246         input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
247         root.add(*input);
248         root_layout->set_gravity(*input, 0, 0);
249 }
250
251 void Designer::edit_route(Route *r)
252 {
253         cur_route = r;
254         cur_zone = 0;
255         if(cur_route)
256                 show_route(*cur_route);
257         else
258                 clear_paths();
259 }
260
261 void Designer::rename_route()
262 {
263         if(mode!=SELECT || !cur_route)
264                 return;
265
266         InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
267         input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
268         root.add(*input);
269         root_layout->set_gravity(*input, 0, 0);
270 }
271
272 void Designer::add_selection_to_route()
273 {
274         if(!cur_route)
275                 return;
276
277         try
278         {
279                 cur_route->add_tracks(selection.get_objects<Track>());
280         }
281         catch(const exception &e)
282         {
283                 lbl_status->set_text(e.what());
284         }
285
286         show_route(*cur_route);
287 }
288
289 void Designer::edit_zone(Zone *z)
290 {
291         cur_zone = z;
292         cur_route = 0;
293         if(cur_zone)
294                 show_zone(*cur_zone);
295         else
296                 clear_paths();
297 }
298
299 void Designer::zone_properties()
300 {
301         if(!cur_zone)
302                 return;
303
304         ZoneProperties *zone_prop = new ZoneProperties(*cur_zone);
305         root.add(*zone_prop);
306         const GLtk::Geometry &root_geom = root.get_geometry();
307         const GLtk::Geometry &dlg_geom = zone_prop->get_geometry();
308         zone_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
309 }
310
311 void Designer::add_selection_to_zone()
312 {
313         if(!cur_zone)
314                 return;
315
316         try
317         {
318                 cur_zone->add_tracks(selection.get_objects<Track>());
319         }
320         catch(const exception &e)
321         {
322                 lbl_status->set_text(e.what());
323         }
324
325         show_zone(*cur_zone);
326 }
327
328 Vector Designer::map_pointer_to_ground(const Vector &p)
329 {
330         GL::Vector4 vec = main_view->get_camera().unproject(GL::Vector4(p.x, p.y, 0, 0));
331         const GL::Vector3 &pos = main_view->get_camera().get_position();
332
333         return pos-GL::Vector3(vec.x, vec.y, vec.z)*(pos.z/vec.z);
334 }
335
336 void Designer::tick()
337 {
338         const Msp::Time::TimeStamp t = Msp::Time::now();
339         float dt = (t-last_tick)/Msp::Time::sec;
340         last_tick = t;
341
342         if(cur_tool && cur_tool->is_done())
343         {
344                 cur_tool->update_selection(selection);
345                 delete cur_tool;
346                 cur_tool = 0;
347                 mode = SELECT;
348         }
349
350         window.tick();
351         root.tick();
352         camera_ctl->tick(dt);
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                 use_tool<ExtendTool>();
365         else if(key==Msp::Input::KEY_N)
366                 new_track();
367         else if(key==Msp::Input::KEY_G)
368                 use_tool<MoveTool>();
369         else if(key==Msp::Input::KEY_R)
370                 use_tool<RotateTool>();
371         else if(key==Msp::Input::KEY_D)
372         {
373                 const set<Object *> &sel_objs = selection.get_objects();
374                 list<Object *> new_objs;
375                 for(set<Object *>::iterator i=sel_objs.begin(); i!=sel_objs.end(); ++i)
376                 {
377                         Object *obj = (*i)->clone(layout);
378                         for(list<Object *>::iterator j=new_objs.begin(); j!=new_objs.end(); ++j)
379                                 obj->link_to(**j);
380                         new_objs.push_back(obj);
381                 }
382
383                 selection.replace(new_objs.begin(), new_objs.end());
384
385                 cur_tool = new MoveTool(*this, mouse, selection.get_objects());
386         }
387         else if(key==Msp::Input::KEY_W)
388                 save();
389         else if(key==Msp::Input::KEY_PLUS)
390                 selection.select_more();
391         else if(key==Msp::Input::KEY_L && shift)
392                 selection.select_blocks();
393         else if(key==Msp::Input::KEY_L)
394                 selection.select_linked();
395         else if(key==Msp::Input::KEY_M)
396                 use_tool<Measure>();
397         else if(key==Msp::Input::KEY_Z)
398                 use_tool<ElevateTool>();
399         else if(key==Msp::Input::KEY_ESC)
400         {
401                 if(cur_tool)
402                 {
403                         delete cur_tool;
404                         cur_tool = 0;
405                 }
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_objects<Track>();
416                 const set<Track *> &ltracks = layout->get_all<Track>();
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<Track3D>(**i));
426                 }
427         }
428         else if(key==Msp::Input::KEY_F)
429                 use_tool<SlopeTool>()->flatten();
430         else if(key==Msp::Input::KEY_E && shift)
431                 use_tool<SlopeTool>()->even_slope(true);
432         else if(key==Msp::Input::KEY_E)
433                 use_tool<SlopeTool>()->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                 use_tool<ExtendTool>()->connect();
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 template<typename T>
450 T *Designer::use_tool()
451 {
452         if(cur_tool)
453         {
454                 delete cur_tool;
455                 cur_tool = 0;
456         }
457
458         T *tool = new T(*this, mouse, selection.get_objects());
459         cur_tool = tool;
460         cur_tool->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
461         manipulation_status(cur_tool->get_status());
462         mode = TOOL;
463
464         return tool;
465 }
466
467 void Designer::button_press(unsigned btn)
468 {
469         bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
470
471         Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
472         Vector ground = map_pointer_to_ground(pointer);
473
474         if(mode==CATALOGUE)
475         {
476                 if(btn==1)
477                 {
478                         Object *cobj = pick_object(pointer);
479                         if(cobj)
480                         {
481                                 Object *obj = cobj->clone(layout);
482                                 obj->set_position(ground);
483
484                                 selection.clear();
485                                 selection.add_object(obj);
486
487                                 mode = SELECT;
488                         }
489                 }
490                 else
491                         mode = SELECT;
492         }
493         else if(mode==SELECT)
494         {
495                 if(btn==1)
496                 {
497                         Object *obj = pick_object(pointer);
498                         if(obj)
499                         {
500                                 if(!shift)
501                                         selection.clear();
502                                 selection.toggle_object(obj);
503                         }
504                 }
505         }
506 }
507
508 void Designer::render()
509 {
510         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
511
512         View3D *view = (mode==CATALOGUE ? cat_view : main_view);
513         view->render();
514
515         root.render();
516 }
517
518 Object *Designer::pick_object(const Vector &pointer)
519 {
520         View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
521         const GL::Vector3 &cpos = view.get_camera().get_position();
522         GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(pointer.x, pointer.y, 0, 0));
523
524         return view.get_layout().get_layout().pick<Object>(Ray(cpos, Vector(cray)));
525 }
526
527 void Designer::update_track_icon(Track3D &track)
528 {
529         overlay->clear(track);
530
531         if(track.get_track().get_flex())
532                 overlay->add_graphic(track, "flex");
533
534         if(unsigned sid = track.get_track().get_sensor_id())
535         {
536                 overlay->add_graphic(track, "sensor");
537                 overlay->set_label(track, lexical_cast<string>(sid));
538         }
539         else if(unsigned tid = track.get_track().get_turnout_id())
540         {
541                 if(tid<0x800)
542                 {
543                         overlay->add_graphic(track, "turnout");
544                         overlay->set_label(track, lexical_cast<string>(tid));
545                 }
546         }
547 }
548
549 void Designer::selection_changed()
550 {
551         const set<Track *> &tracks = selection.get_objects<Track>();
552         if(tracks.empty())
553                 lbl_status->set_text(string());
554         else
555         {
556                 float len = 0;
557                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
558                         len += (*i)->get_type().get_total_length();
559                 lbl_status->set_text(format("%.2fm of track selected\n", len));
560         }
561 }
562
563 void Designer::manipulation_status(const string &status)
564 {
565         lbl_status->set_text(status);
566 }
567
568 void Designer::measure_done()
569 {
570         mode = SELECT;
571         selection_changed();
572 }
573
574 void Designer::track_properties_response(int)
575 {
576         const set<Track *> &tracks = selection.get_objects<Track>();
577         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
578                 update_track_icon(layout_3d->get<Track3D>(**i));
579 }
580
581 void Designer::route_name_accept(const string &text)
582 {
583         if(cur_route)
584                 cur_route->set_name(text);
585 }
586
587 void Designer::svg_export_accept(const string &text)
588 {
589         SvgExporter svg_exp(*layout);
590         svg_exp.save(text);
591 }
592
593 string Designer::tooltip(int x, int y)
594 {
595         const GLtk::Geometry &rgeom = root.get_geometry();
596         if(Object *obj = pick_object(Vector(x*2.0f/rgeom.w-1.0f, y*2.0f/rgeom.h-1.0f, 0)))
597         {
598                 const ObjectType &otype = obj->get_type();
599                 string info = format("%d %s", otype.get_article_number(), otype.get_description());
600                 if(Track *track = dynamic_cast<Track *>(obj))
601                 {
602                         if(mode!=CATALOGUE && abs(track->get_tilt()).radians()>1e-4)
603                                 info += format(" (slope %.1f%%)", abs(tan(track->get_tilt())*100));
604                         if(track->get_turnout_id())
605                                 info += format(" (turnout %d)", track->get_turnout_id());
606                         else if(track->get_sensor_id())
607                                 info += format(" (sensor %d)", track->get_sensor_id());
608                 }
609                 return info;
610         }
611
612         return string();
613 }
614
615 void Designer::clear_paths()
616 {
617         for(list<Path3D *>::iterator i=highlight_paths.begin(); i!=highlight_paths.end(); ++i)
618                 delete *i;
619         highlight_paths.clear();
620 }
621
622 void Designer::show_route(const Route &route)
623 {
624         clear_paths();
625
626         const set<Track *> &rtracks = route.get_tracks();
627         for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
628         {
629                 Track3D &t3d = layout_3d->get<Track3D>(**i);
630                 Path3D *path = new Path3D(t3d);
631                 path->set_color(GL::Color(0.5, 0.8, 1.0));
632                 if(unsigned tid = (*i)->get_turnout_id())
633                         path->set_path(route.get_turnout(tid));
634         }
635 }
636
637 void Designer::show_zone(const Zone &zone)
638 {
639         clear_paths();
640
641         const Zone::TrackSet &ztracks = zone.get_tracks();
642         for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
643         {
644                 Track3D &t3d = layout_3d->get<Track3D>(**i);
645                 Path3D *path = new Path3D(t3d);
646                 path->set_color(GL::Color(0.8, 1.0, 0.5));
647         }
648 }