]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Clean up old clutter from Designer
[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_with_selected_objects<ExtendTool>();
231 }
232
233 void Designer::connect_tracks()
234 {
235         use_tool_with_selected_objects<ExtendTool>()->connect();
236 }
237
238 void Designer::flatten_tracks()
239 {
240         use_tool_with_selected_objects<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_with_selected_objects<ExtendTool>();
365         else if(key==Msp::Input::KEY_N)
366                 new_track();
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_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_with_selected_objects<ElevateTool>();
399         else if(key==Msp::Input::KEY_ESC)
400         {
401                 if(mode==CATALOGUE)
402                         mode = SELECT;
403                 else if(mode==SELECT)
404                         selection.clear();
405         }
406         else if(key==Msp::Input::KEY_X)
407                 erase_tracks();
408         else if(key==Msp::Input::KEY_F && shift)
409         {
410                 const set<Track *> &tracks = selection.get_objects<Track>();
411                 const set<Track *> &ltracks = layout->get_all<Track>();
412                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
413                 {
414                         (*i)->set_flex(!(*i)->get_flex());
415                         (*i)->break_links();
416                         for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
417                                 if(*j!=*i)
418                                         (*i)->snap_to(**j, true);
419
420                         update_track_icon(layout_3d->get<Track3D>(**i));
421                 }
422         }
423         else if(key==Msp::Input::KEY_F)
424                 use_tool_with_selected_objects<SlopeTool>()->flatten();
425         else if(key==Msp::Input::KEY_E && shift)
426                 use_tool_with_selected_objects<SlopeTool>()->even_slope(true);
427         else if(key==Msp::Input::KEY_E)
428                 use_tool_with_selected_objects<SlopeTool>()->even_slope();
429         else if(key==Msp::Input::KEY_A)
430         {
431                 if(cur_route)
432                         add_selection_to_route();
433                 else if(cur_zone)
434                         add_selection_to_zone();
435         }
436         else if(key==Msp::Input::KEY_C)
437                 use_tool_with_selected_objects<ExtendTool>()->connect();
438         else if(key==Msp::Input::KEY_V)
439                 svg_export();
440         else if(key==Msp::Input::KEY_P)
441                 track_properties();
442 }
443
444 template<typename T>
445 T *Designer::use_tool_with_selected_objects()
446 {
447         T *tool = new T(*this, keyboard, mouse, selection.get_objects());
448         use_tool(tool);
449         return tool;
450 }
451
452 template<typename T>
453 T *Designer::use_tool()
454 {
455         T *tool = new T(*this, keyboard, mouse);
456         use_tool(tool);
457         return tool;
458 }
459
460 void Designer::use_tool(Tool *tool)
461 {
462         if(cur_tool)
463         {
464                 delete cur_tool;
465                 cur_tool = 0;
466         }
467
468         cur_tool = tool;
469         cur_tool->signal_status.connect(sigc::mem_fun(this, &Designer::tool_status));
470         tool_status(cur_tool->get_status());
471         mode = TOOL;
472 }
473
474 void Designer::button_press(unsigned btn)
475 {
476         bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
477
478         Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
479         Vector ground = map_pointer_to_ground(pointer);
480
481         if(mode==CATALOGUE)
482         {
483                 if(btn==1)
484                 {
485                         Object *cobj = pick_object(pointer);
486                         if(cobj)
487                         {
488                                 Object *obj = cobj->clone(layout);
489                                 obj->set_position(ground);
490
491                                 selection.clear();
492                                 selection.add_object(obj);
493
494                                 mode = SELECT;
495                         }
496                 }
497                 else
498                         mode = SELECT;
499         }
500         else if(mode==SELECT)
501         {
502                 if(btn==1)
503                 {
504                         Object *obj = pick_object(pointer);
505                         if(obj)
506                         {
507                                 if(!shift)
508                                         selection.clear();
509                                 selection.toggle_object(obj);
510                         }
511                 }
512         }
513 }
514
515 void Designer::render()
516 {
517         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
518
519         View3D *view = (mode==CATALOGUE ? cat_view : main_view);
520         view->render();
521
522         root.render();
523 }
524
525 Object *Designer::pick_object(const Vector &pointer)
526 {
527         View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
528         const GL::Vector3 &cpos = view.get_camera().get_position();
529         GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(pointer.x, pointer.y, 0, 0));
530
531         return view.get_layout().get_layout().pick<Object>(Ray(cpos, Vector(cray)));
532 }
533
534 void Designer::update_track_icon(Track3D &track)
535 {
536         overlay->clear(track);
537
538         if(track.get_track().get_flex())
539                 overlay->add_graphic(track, "flex");
540
541         if(unsigned sid = track.get_track().get_sensor_id())
542         {
543                 overlay->add_graphic(track, "sensor");
544                 overlay->set_label(track, lexical_cast<string>(sid));
545         }
546         else if(unsigned tid = track.get_track().get_turnout_id())
547         {
548                 if(tid<0x800)
549                 {
550                         overlay->add_graphic(track, "turnout");
551                         overlay->set_label(track, lexical_cast<string>(tid));
552                 }
553         }
554 }
555
556 void Designer::selection_changed()
557 {
558         const set<Track *> &tracks = selection.get_objects<Track>();
559         if(tracks.empty())
560                 lbl_status->set_text(string());
561         else
562         {
563                 float len = 0;
564                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
565                         len += (*i)->get_type().get_total_length();
566                 lbl_status->set_text(format("%.2fm of track selected\n", len));
567         }
568 }
569
570 void Designer::tool_status(const string &status)
571 {
572         lbl_status->set_text(status);
573 }
574
575 void Designer::track_properties_response(int)
576 {
577         const set<Track *> &tracks = selection.get_objects<Track>();
578         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
579                 update_track_icon(layout_3d->get<Track3D>(**i));
580 }
581
582 void Designer::route_name_accept(const string &text)
583 {
584         if(cur_route)
585                 cur_route->set_name(text);
586 }
587
588 void Designer::svg_export_accept(const string &text)
589 {
590         SvgExporter svg_exp(*layout);
591         svg_exp.save(text);
592 }
593
594 string Designer::tooltip(int x, int y)
595 {
596         const GLtk::Geometry &rgeom = root.get_geometry();
597         if(Object *obj = pick_object(Vector(x*2.0f/rgeom.w-1.0f, y*2.0f/rgeom.h-1.0f, 0)))
598         {
599                 const ObjectType &otype = obj->get_type();
600                 string info = format("%d %s", otype.get_article_number(), otype.get_description());
601                 if(Track *track = dynamic_cast<Track *>(obj))
602                 {
603                         if(mode!=CATALOGUE && abs(track->get_tilt()).radians()>1e-4)
604                                 info += format(" (slope %.1f%%)", abs(tan(track->get_tilt())*100));
605                         if(track->get_turnout_id())
606                                 info += format(" (turnout %d)", track->get_turnout_id());
607                         else if(track->get_sensor_id())
608                                 info += format(" (sensor %d)", track->get_sensor_id());
609                 }
610                 return info;
611         }
612
613         return string();
614 }
615
616 void Designer::clear_paths()
617 {
618         for(list<Path3D *>::iterator i=highlight_paths.begin(); i!=highlight_paths.end(); ++i)
619                 delete *i;
620         highlight_paths.clear();
621 }
622
623 void Designer::show_route(const Route &route)
624 {
625         clear_paths();
626
627         const set<Track *> &rtracks = route.get_tracks();
628         for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
629         {
630                 Track3D &t3d = layout_3d->get<Track3D>(**i);
631                 Path3D *path = new Path3D(t3d);
632                 path->set_color(GL::Color(0.5, 0.8, 1.0));
633                 if(unsigned tid = (*i)->get_turnout_id())
634                         path->set_path(route.get_turnout(tid));
635         }
636 }
637
638 void Designer::show_zone(const Zone &zone)
639 {
640         clear_paths();
641
642         const Zone::TrackSet &ztracks = zone.get_tracks();
643         for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
644         {
645                 Track3D &t3d = layout_3d->get<Track3D>(**i);
646                 Path3D *path = new Path3D(t3d);
647                 path->set_color(GL::Color(0.8, 1.0, 0.5));
648         }
649 }