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