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"
24 #include "elevatetool.h"
25 #include "extendtool.h"
27 #include "layoutbar.h"
28 #include "manipulator.h"
31 #include "objectselecttool.h"
32 #include "rotatetool.h"
34 #include "selection.h"
35 #include "slopetool.h"
36 #include "svgexporter.h"
38 #include "trackproperties.h"
40 #include "zoneproperties.h"
46 Designer::Designer(int argc, char **argv):
51 root(ui_res, &window, &keyboard, &mouse),
59 window.set_title("Railway Designer");
60 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
62 // Setup catalogue and layout
63 DataFile::load(catalogue, "tracks.dat");
65 cat_layout_3d = new Layout3D(catalogue.get_layout());
67 layout = new Layout(catalogue);
68 layout_3d = new Layout3D(*layout);
73 DataFile::load(*layout, argv[1]);
75 if(!layout->get_base().empty())
77 base_object = new GL::Object;
78 DataFile::load(*base_object, layout->get_base());
83 cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
85 main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
86 GL::Pipeline *pipeline = &main_view->get_pipeline();
88 GL::Pipeline::Pass *pass = &pipeline->add_pass("unlit");
89 pass->set_depth_test(&GL::DepthTest::lequal());
90 pass->set_blend(&GL::Blend::alpha());
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());
97 pass = &pipeline->add_pass("overlay");
98 pass->set_blend(&GL::Blend::alpha());
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");
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);
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));
118 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
121 if(i!=toolbars.begin())
123 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(root_layout, &GLtk::Layout::update)));
126 root_layout->add_constraint(**i, GLtk::Layout::RIGHT_OF, *prev, 0);
127 root_layout->add_constraint(**i, GLtk::Layout::COPY_HEIGHT, *prev);
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);
138 lbl_status = new GLtk::Label;
139 statusbar->add(*lbl_status);
140 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
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");
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();
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));
156 Designer::~Designer()
158 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
167 delete cat_layout_3d;
176 return Application::main();
179 void Designer::save()
181 InputDialog *input = new InputDialog(*this, "Save layout", filename);
182 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
184 root_layout->set_gravity(*input, 0, 0);
187 void Designer::quit()
192 void Designer::new_object()
197 lbl_status->set_text("Select new object or press Esc to cancel");
200 void Designer::erase_objects()
204 set<Object *> objects = selection.get_objects();
206 for(set<Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
208 overlay->clear(layout_3d->get_3d(**i));
213 void Designer::track_properties()
216 if(selection.empty())
219 TrackProperties *track_prop = new TrackProperties(selection);
220 root.add(*track_prop);
221 root_layout->set_gravity(*track_prop, 0, 0);
223 track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
226 void Designer::extend_track()
228 use_tool_with_selected_objects<ExtendTool>();
231 void Designer::connect_tracks()
233 use_tool_with_selected_objects<ExtendTool>()->connect();
236 void Designer::flatten_tracks()
238 use_tool_with_selected_objects<SlopeTool>()->flatten();
241 void Designer::svg_export()
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));
246 root_layout->set_gravity(*input, 0, 0);
249 void Designer::edit_route(Route *r)
254 show_route(*cur_route);
259 void Designer::rename_route()
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));
268 root_layout->set_gravity(*input, 0, 0);
271 void Designer::add_selection_to_route()
278 cur_route->add_tracks(selection.get_objects<Track>());
280 catch(const exception &e)
282 lbl_status->set_text(e.what());
285 show_route(*cur_route);
288 void Designer::edit_zone(Zone *z)
293 show_zone(*cur_zone);
298 void Designer::zone_properties()
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);
310 void Designer::add_selection_to_zone()
317 cur_zone->add_tracks(selection.get_objects<Track>());
319 catch(const exception &e)
321 lbl_status->set_text(e.what());
324 show_zone(*cur_zone);
327 Vector Designer::map_pointer_to_ground(const Vector &p)
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();
332 return pos-GL::Vector3(vec.x, vec.y, vec.z)*(pos.z/vec.z);
335 void Designer::tick()
337 const Msp::Time::TimeStamp t = Msp::Time::now();
338 float dt = (t-last_tick)/Msp::Time::sec;
341 if(cur_tool && cur_tool->is_done())
343 cur_tool->update_selection(selection);
349 camera_ctl->tick(dt);
353 window.swap_buffers();
356 void Designer::key_press(unsigned key)
358 bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
360 if(key==Msp::Input::KEY_N && shift)
361 use_tool_with_selected_objects<ExtendTool>();
362 else if(key==Msp::Input::KEY_N)
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)
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)
374 Object *obj = (*i)->clone(layout);
375 for(list<Object *>::iterator j=new_objs.begin(); j!=new_objs.end(); ++j)
377 new_objs.push_back(obj);
380 selection.replace(new_objs.begin(), new_objs.end());
382 use_tool_with_selected_objects<MoveTool>();
384 else if(key==Msp::Input::KEY_W)
386 else if(key==Msp::Input::KEY_M)
388 else if(key==Msp::Input::KEY_Z)
389 use_tool_with_selected_objects<ElevateTool>();
390 else if(key==Msp::Input::KEY_ESC)
395 else if(key==Msp::Input::KEY_X)
397 else if(key==Msp::Input::KEY_F && shift)
399 const set<Track *> &tracks = selection.get_objects<Track>();
400 const set<Track *> <racks = layout->get_all<Track>();
401 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
403 (*i)->set_flex(!(*i)->get_flex());
405 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
407 (*i)->snap_to(**j, true);
409 update_track_icon(layout_3d->get_3d(**i));
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)
421 add_selection_to_route();
423 add_selection_to_zone();
425 else if(key==Msp::Input::KEY_C)
426 use_tool_with_selected_objects<ExtendTool>()->connect();
427 else if(key==Msp::Input::KEY_V)
429 else if(key==Msp::Input::KEY_P)
434 T *Designer::use_tool_with_selected_objects()
436 T *tool = new T(*this, keyboard, mouse, selection.get_objects());
442 T *Designer::use_tool()
444 T *tool = new T(*this, keyboard, mouse);
449 void Designer::use_tool(Tool *tool)
458 cur_tool->signal_status.connect(sigc::mem_fun(this, &Designer::tool_status));
459 tool_status(cur_tool->get_status());
463 void Designer::use_select_tool()
465 use_tool(new ObjectSelectTool(*this, keyboard, mouse, selection));
468 void Designer::button_press(unsigned btn)
470 Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
471 Vector ground = map_pointer_to_ground(pointer);
477 obj = pick_object(pointer);
481 obj = obj->clone(layout);
482 obj->set_position(ground);
485 selection.add_object(obj);
493 void Designer::render()
495 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
497 View3D *view = (mode==CATALOGUE ? cat_view : main_view);
503 Object *Designer::pick_object(const Vector &pointer)
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));
509 return view.get_layout().get_layout().pick<Object>(Ray(cpos, Vector(cray)));
512 void Designer::update_track_icon(Track3D &track)
514 overlay->clear(track);
516 if(track.get_track().get_flex())
517 overlay->add_graphic(track, "flex");
519 if(unsigned saddr = track.get_track().get_sensor_address())
521 overlay->add_graphic(track, "sensor");
522 overlay->set_label(track, lexical_cast<string>(saddr));
524 else if(unsigned taddr = track.get_track().get_turnout_address())
528 overlay->add_graphic(track, "turnout");
529 overlay->set_label(track, lexical_cast<string>(taddr));
534 void Designer::tool_status(const string &status)
536 lbl_status->set_text(status);
539 void Designer::track_properties_response(int)
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));
546 void Designer::route_name_accept(const string &text)
549 cur_route->set_name(text);
552 void Designer::svg_export_accept(const string &text)
554 SvgExporter svg_exp(*layout);
558 string Designer::tooltip(int x, int y)
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)))
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))
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());
580 void Designer::clear_paths()
582 for(list<Path3D *>::iterator i=highlight_paths.begin(); i!=highlight_paths.end(); ++i)
584 highlight_paths.clear();
587 void Designer::show_route(const Route &route)
591 const set<Track *> &rtracks = route.get_tracks();
592 for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
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));
602 void Designer::show_zone(const Zone &zone)
606 const Zone::TrackSet &ztracks = zone.get_tracks();
607 for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
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));