]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Add editable terrain objects
[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("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 {
61         window.set_title("Railway Designer");
62         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
63
64         // Setup catalogue and layout
65         DataFile::load(catalogue, "tracks.dat");
66         DataFile::load(catalogue, "terrain.dat");
67
68         cat_layout_3d = new Layout3D(catalogue.get_layout());
69
70         layout = new Layout(catalogue);
71         layout_3d = new Layout3D(*layout);
72
73         if(argc>1)
74         {
75                 filename = argv[1];
76                 DataFile::load(*layout, argv[1]);
77
78                 if(!layout->get_base().empty())
79                 {
80                         base_object = new GL::Object;
81                         DataFile::load(*base_object, layout->get_base());
82                 }
83         }
84
85         // Setup OpenGL
86         cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
87
88         main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
89         GL::Pipeline *pipeline = &main_view->get_pipeline();
90
91         GL::Pipeline::Pass *pass = &pipeline->add_pass("unlit");
92         pass->set_depth_test(&GL::DepthTest::lequal());
93         pass->set_blend(&GL::Blend::alpha());
94
95         pass = &pipeline->add_pass("blended");
96         pass->set_lighting(&layout_3d->get_lighting());
97         pass->set_depth_test(&GL::DepthTest::lequal());
98         pass->set_blend(&GL::Blend::alpha());
99
100         pass = &pipeline->add_pass("overlay");
101         pass->set_blend(&GL::Blend::alpha());
102
103         if(base_object)
104                 pipeline->add_renderable(*base_object);
105         pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
106         pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
107
108         // Setup UI
109         keyboard.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Designer::key_press), false));
110         mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Designer::button_press), false));
111         root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
112         root_layout = new GLtk::Layout;
113         root_layout->set_margin(0);
114         root.set_layout(root_layout);
115
116         toolbars.push_back(new Layoutbar(*this));
117         toolbars.push_back(new Trackbar(*this));
118         toolbars.push_back(new Routebar(*this));
119         toolbars.push_back(new Zonebar(*this));
120         Toolbar *prev = 0;
121         for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
122         {
123                 root.add(**i);
124                 if(i!=toolbars.begin())
125                         (*i)->expand(false);
126                 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(root_layout, &GLtk::Layout::update)));
127                 if(prev)
128                 {
129                         root_layout->add_constraint(**i, GLtk::Layout::RIGHT_OF, *prev, 0);
130                         root_layout->add_constraint(**i, GLtk::Layout::COPY_HEIGHT, *prev);
131                 }
132                 prev = *i;
133         }
134
135         GLtk::Panel *statusbar = new GLtk::Panel;
136         root.add(*statusbar);
137         statusbar->set_focusable(false);
138         root_layout->set_gravity(*statusbar, -1, -1);
139         root_layout->set_expand(*statusbar, true, false);
140
141         lbl_status = new GLtk::Label;
142         statusbar->add(*lbl_status);
143         lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
144
145         pipeline->add_renderable_for_pass(sel_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, keyboard, mouse);
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 set<Object *> &objects = layout->get_all<Object>();
155         for(set<Object *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
156                 update_object_icon(**i);
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 overlay;
165         delete main_view;
166         delete base_object;
167         delete layout_3d;
168         delete layout;
169         delete cat_view;
170         delete cat_layout_3d;
171 }
172
173 int Designer::main()
174 {
175         window.show();
176
177         use_select_tool();
178
179         return Application::main();
180 }
181
182 void Designer::save()
183 {
184         InputDialog *input = new InputDialog(*this, "Save layout", filename);
185         input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
186         root.add(*input);
187         root_layout->set_gravity(*input, 0, 0);
188 }
189
190 void Designer::quit()
191 {
192         exit(0);
193 }
194
195 void Designer::new_object()
196 {
197         use_select_tool();
198
199         mode = CATALOGUE;
200         lbl_status->set_text("Select new object or press Esc to cancel");
201 }
202
203 void Designer::erase_objects()
204 {
205         use_select_tool();
206
207         set<Object *> objects = selection.get_objects();
208         selection.clear();
209         for(set<Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
210         {
211                 overlay->clear(layout_3d->get_3d(**i));
212                 delete *i;
213         }
214 }
215
216 void Designer::object_properties()
217 {
218         use_select_tool();
219         if(selection.empty())
220                 return;
221
222         ObjectProperties *dlg = new ObjectProperties(selection);
223         root.add(*dlg);
224         root_layout->set_gravity(*dlg, 0, 0);
225
226         dlg->signal_response.connect(sigc::mem_fun(this, &Designer::object_properties_response));
227 }
228
229 void Designer::extend_track()
230 {
231         use_tool_with_selected_objects<ExtendTool>();
232 }
233
234 void Designer::connect_tracks()
235 {
236         use_tool_with_selected_objects<ExtendTool>()->connect();
237 }
238
239 void Designer::flatten_tracks()
240 {
241         use_tool_with_selected_objects<SlopeTool>()->flatten();
242 }
243
244 void Designer::svg_export()
245 {
246         InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
247         input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
248         root.add(*input);
249         root_layout->set_gravity(*input, 0, 0);
250 }
251
252 void Designer::edit_route(Route *r)
253 {
254         cur_route = r;
255         cur_zone = 0;
256         if(cur_route)
257                 show_route(*cur_route);
258         else
259                 clear_paths();
260 }
261
262 void Designer::rename_route()
263 {
264         use_select_tool();
265         if(!cur_route)
266                 return;
267
268         InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
269         input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
270         root.add(*input);
271         root_layout->set_gravity(*input, 0, 0);
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_objects<Track>());
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_objects<Track>());
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 pos-GL::Vector3(vec.x, vec.y, vec.z)*(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(cur_tool && cur_tool->is_done())
345         {
346                 cur_tool->update_selection(selection);
347                 use_select_tool();
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_with_selected_objects<ExtendTool>();
365         else if(key==Msp::Input::KEY_N)
366                 new_object();
367         else if(key==Msp::Input::KEY_G)
368                 use_tool_with_selected_objects<MoveTool>();
369         else if(key==Msp::Input::KEY_R)
370                 use_tool_with_selected_objects<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                 use_tool_with_selected_objects<MoveTool>();
386         }
387         else if(key==Msp::Input::KEY_W)
388                 save();
389         else if(key==Msp::Input::KEY_M)
390                 use_tool<Measure>();
391         else if(key==Msp::Input::KEY_Z)
392                 use_tool_with_selected_objects<ElevateTool>();
393         else if(key==Msp::Input::KEY_ESC)
394         {
395                 if(mode==CATALOGUE)
396                         use_select_tool();
397         }
398         else if(key==Msp::Input::KEY_X)
399                 erase_objects();
400         else if(key==Msp::Input::KEY_F && shift)
401         {
402                 const set<Track *> &tracks = selection.get_objects<Track>();
403                 const set<Track *> &ltracks = layout->get_all<Track>();
404                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
405                 {
406                         (*i)->set_flex(!(*i)->get_flex());
407                         (*i)->break_links();
408                         for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
409                                 if(*j!=*i)
410                                         (*i)->snap_to(**j, true);
411
412                         update_object_icon(**i);
413                 }
414         }
415         else if(key==Msp::Input::KEY_F)
416                 use_tool_with_selected_objects<SlopeTool>()->flatten();
417         else if(key==Msp::Input::KEY_E && shift)
418                 use_tool_with_selected_objects<SlopeTool>()->even_slope(true);
419         else if(key==Msp::Input::KEY_E)
420                 use_tool_with_selected_objects<SlopeTool>()->even_slope();
421         else if(key==Msp::Input::KEY_A)
422         {
423                 if(cur_route)
424                         add_selection_to_route();
425                 else if(cur_zone)
426                         add_selection_to_zone();
427         }
428         else if(key==Msp::Input::KEY_C)
429                 use_tool_with_selected_objects<ExtendTool>()->connect();
430         else if(key==Msp::Input::KEY_V)
431                 svg_export();
432         else if(key==Msp::Input::KEY_P)
433                 object_properties();
434         else if(key==Msp::Input::KEY_TAB)
435         {
436                 Object *obj = selection.get_object();
437                 if(Terrain *terrain = dynamic_cast<Terrain *>(obj))
438                         use_tool(new TerrainTool(*this, keyboard, mouse, *terrain));
439         }
440 }
441
442 template<typename T>
443 T *Designer::use_tool_with_selected_objects()
444 {
445         T *tool = new T(*this, keyboard, mouse, selection.get_objects());
446         use_tool(tool);
447         return tool;
448 }
449
450 template<typename T>
451 T *Designer::use_tool()
452 {
453         T *tool = new T(*this, keyboard, mouse);
454         use_tool(tool);
455         return tool;
456 }
457
458 void Designer::use_tool(Tool *tool)
459 {
460         if(cur_tool)
461         {
462                 delete cur_tool;
463                 cur_tool = 0;
464         }
465
466         cur_tool = tool;
467         cur_tool->signal_status.connect(sigc::mem_fun(this, &Designer::tool_status));
468         tool_status(cur_tool->get_status());
469         mode = TOOL;
470 }
471
472 void Designer::use_select_tool()
473 {
474         use_tool(new ObjectSelectTool(*this, keyboard, mouse, selection));
475 }
476
477 void Designer::button_press(unsigned btn)
478 {
479         Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
480         Vector ground = map_pointer_to_ground(pointer);
481
482         if(mode==CATALOGUE)
483         {
484                 Object *obj;
485                 if(btn==1)
486                         obj = pick_object(pointer);
487
488                 if(obj)
489                 {
490                         obj = obj->clone(layout);
491                         obj->set_position(ground);
492
493                         selection.clear();
494                         selection.add_object(obj);
495                 }
496
497                 if(obj || btn==3)
498                         use_select_tool();
499         }
500 }
501
502 void Designer::render()
503 {
504         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
505
506         View3D *view = (mode==CATALOGUE ? cat_view : main_view);
507         view->render();
508
509         root.render();
510 }
511
512 Object *Designer::pick_object(const Vector &pointer)
513 {
514         View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
515         const GL::Vector3 &cpos = view.get_camera().get_position();
516         GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(pointer.x, pointer.y, 0, 0));
517
518         return view.get_layout().get_layout().pick<Object>(Ray(cpos, Vector(cray)));
519 }
520
521 void Designer::update_object_icon(Object &obj)
522 {
523         Object3D &obj3d = layout_3d->get_3d(obj);
524         overlay->clear(obj3d);
525
526         if(Track *track = dynamic_cast<Track *>(&obj))
527         {
528                 if(track->get_flex())
529                         overlay->add_graphic(obj3d, "flex");
530
531                 if(unsigned saddr = track->get_sensor_address())
532                 {
533                         overlay->add_graphic(obj3d, "trackcircuit");
534                         overlay->set_label(obj3d, lexical_cast<string>(saddr));
535                 }
536                 else if(unsigned taddr = track->get_turnout_address())
537                 {
538                         if(taddr<0x800)
539                         {
540                                 overlay->add_graphic(obj3d, "turnout");
541                                 overlay->set_label(obj3d, lexical_cast<string>(taddr));
542                         }
543                 }
544         }
545         else if(Signal *signal = dynamic_cast<Signal *>(&obj))
546         {
547                 overlay->add_graphic(obj3d, "signal");
548                 overlay->set_label(obj3d, lexical_cast<string>(signal->get_address()));
549         }
550         else if(BeamGate *gate = dynamic_cast<BeamGate *>(&obj))
551         {
552                 overlay->add_graphic(obj3d, "beamgate");
553                 overlay->set_label(obj3d, lexical_cast<string>(gate->get_address()));
554         }
555 }
556
557 void Designer::tool_status(const string &status)
558 {
559         lbl_status->set_text(status);
560 }
561
562 void Designer::object_properties_response(int)
563 {
564         const set<Object *> &objects = selection.get_objects();
565         for(set<Object *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
566                 update_object_icon(**i);
567 }
568
569 void Designer::route_name_accept(const string &text)
570 {
571         if(cur_route)
572                 cur_route->set_name(text);
573 }
574
575 void Designer::svg_export_accept(const string &text)
576 {
577         SvgExporter svg_exp(*layout);
578         svg_exp.save(text);
579 }
580
581 string Designer::tooltip(int x, int y)
582 {
583         const GLtk::Geometry &rgeom = root.get_geometry();
584         if(Object *obj = pick_object(Vector(x*2.0f/rgeom.w-1.0f, y*2.0f/rgeom.h-1.0f, 0)))
585         {
586                 const ObjectType &otype = obj->get_type();
587                 string info = format("%d %s", otype.get_article_number(), otype.get_description());
588                 if(Track *track = dynamic_cast<Track *>(obj))
589                 {
590                         if(mode!=CATALOGUE && abs(track->get_tilt()).radians()>1e-4)
591                                 info += format(" (slope %.1f%%)", abs(tan(track->get_tilt())*100));
592                         if(track->get_turnout_address())
593                                 info += format(" (turnout %d)", track->get_turnout_address());
594                         else if(track->get_sensor_address())
595                                 info += format(" (sensor %d)", track->get_sensor_address());
596                 }
597                 return info;
598         }
599
600         return string();
601 }
602
603 void Designer::clear_paths()
604 {
605         for(list<Path3D *>::iterator i=highlight_paths.begin(); i!=highlight_paths.end(); ++i)
606                 delete *i;
607         highlight_paths.clear();
608 }
609
610 void Designer::show_route(const Route &route)
611 {
612         clear_paths();
613
614         const set<Track *> &rtracks = route.get_tracks();
615         for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
616         {
617                 Track3D &t3d = layout_3d->get_3d(**i);
618                 Path3D *path = new Path3D(t3d);
619                 path->set_color(GL::Color(0.5, 0.8, 1.0));
620                 if(unsigned taddr = (*i)->get_turnout_address())
621                         path->set_path(route.get_turnout(taddr));
622         }
623 }
624
625 void Designer::show_zone(const Zone &zone)
626 {
627         clear_paths();
628
629         const Zone::TrackSet &ztracks = zone.get_tracks();
630         for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
631         {
632                 Track3D &t3d = layout_3d->get_3d(**i);
633                 Path3D *path = new Path3D(t3d);
634                 path->set_color(GL::Color(0.8, 1.0, 0.5));
635         }
636 }