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