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"
25 #include "elevatetool.h"
26 #include "extendtool.h"
28 #include "layoutbar.h"
29 #include "manipulator.h"
32 #include "objectproperties.h"
33 #include "objectselecttool.h"
34 #include "rotatetool.h"
36 #include "selection.h"
37 #include "slopetool.h"
38 #include "svgexporter.h"
39 #include "terraintool.h"
42 #include "zoneproperties.h"
48 Designer::Designer(int argc, char **argv):
52 ui_res("data/r2c2.res"),
53 root(ui_res, &window, &keyboard, &mouse),
62 window.set_title("Railway Designer");
63 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
65 // Setup catalogue and layout
66 catalogue.add_source("data/Märklin/H0");
68 cat_layout_3d = new Layout3D(catalogue.get_layout());
70 layout = new Layout(catalogue);
71 layout->get_clock().set_current_time(12*Time::hour);
72 layout_3d = new Layout3D(*layout);
78 DataFile::load(*layout, argv[1]);
80 if(!layout->get_base().empty())
82 base_object = new GL::Object;
83 DataFile::load(*base_object, layout->get_base());
88 cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
90 main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
91 GL::Pipeline *pipeline = &main_view->get_pipeline();
93 GL::Pipeline::Pass *pass = &pipeline->add_pass("unlit");
94 pass->set_depth_test(&GL::DepthTest::lequal());
95 pass->set_blend(&GL::Blend::alpha());
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());
102 pass = &pipeline->add_pass("overlay");
103 pass->set_blend(&GL::Blend::alpha());
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");
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);
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));
123 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
126 if(i!=toolbars.begin())
128 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(root_layout, &GLtk::Layout::update)));
131 root_layout->add_constraint(**i, GLtk::Layout::RIGHT_OF, *prev, 0);
132 root_layout->add_constraint(**i, GLtk::Layout::COPY_HEIGHT, *prev);
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);
143 lbl_status = new GLtk::Label;
144 statusbar->add(*lbl_status);
145 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
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");
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();
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);
161 Designer::~Designer()
163 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
172 delete cat_layout_3d;
181 return Application::main();
184 void Designer::save()
186 InputDialog *input = new InputDialog(*this, "Save layout", filename);
187 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
189 root_layout->set_gravity(*input, 0, 0);
192 void Designer::quit()
197 void Designer::new_object()
202 lbl_status->set_text("Select new object or press Esc to cancel");
205 void Designer::erase_objects()
209 set<Object *> objects = selection.get_objects();
211 for(set<Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
213 overlay->clear(layout_3d->get_3d(**i));
218 void Designer::object_properties()
221 if(selection.empty())
224 ObjectProperties *dlg = new ObjectProperties(selection);
226 root_layout->set_gravity(*dlg, 0, 0);
228 dlg->signal_response.connect(sigc::mem_fun(this, &Designer::object_properties_response));
231 void Designer::extend_track()
233 use_tool_with_selected_objects<ExtendTool>();
236 void Designer::connect_tracks()
238 use_tool_with_selected_objects<ExtendTool>()->connect();
241 void Designer::flatten_tracks()
243 use_tool_with_selected_objects<SlopeTool>()->flatten();
246 void Designer::svg_export()
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));
251 root_layout->set_gravity(*input, 0, 0);
254 void Designer::edit_route(Route *r)
259 show_route(*cur_route);
264 void Designer::rename_route()
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));
273 root_layout->set_gravity(*input, 0, 0);
276 void Designer::add_selection_to_route()
283 cur_route->add_tracks(selection.get_objects<Track>());
285 catch(const exception &e)
287 lbl_status->set_text(e.what());
290 show_route(*cur_route);
293 void Designer::edit_zone(Zone *z)
298 show_zone(*cur_zone);
303 void Designer::zone_properties()
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);
315 void Designer::add_selection_to_zone()
322 cur_zone->add_tracks(selection.get_objects<Track>());
324 catch(const exception &e)
326 lbl_status->set_text(e.what());
329 show_zone(*cur_zone);
332 Vector Designer::map_pointer_to_ground(const Vector &p)
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();
337 return pos-GL::Vector3(vec.x, vec.y, vec.z)*(pos.z/vec.z);
340 void Designer::tick()
342 const Msp::Time::TimeStamp t = Msp::Time::now();
343 float dt = (t-last_tick)/Msp::Time::sec;
346 if(cur_tool && cur_tool->is_done())
348 cur_tool->update_selection(selection);
355 camera_ctl->tick(dt);
359 window.swap_buffers();
362 void Designer::key_press(unsigned key)
364 bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
366 if(key==Msp::Input::KEY_N && shift)
367 use_tool_with_selected_objects<ExtendTool>();
368 else if(key==Msp::Input::KEY_N)
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)
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)
380 Object *obj = (*i)->clone(layout);
381 for(list<Object *>::iterator j=new_objs.begin(); j!=new_objs.end(); ++j)
383 new_objs.push_back(obj);
386 selection.replace(new_objs.begin(), new_objs.end());
388 use_tool_with_selected_objects<MoveTool>();
390 else if(key==Msp::Input::KEY_W)
392 else if(key==Msp::Input::KEY_M)
394 else if(key==Msp::Input::KEY_Z)
395 use_tool_with_selected_objects<ElevateTool>();
396 else if(key==Msp::Input::KEY_ESC)
401 else if(key==Msp::Input::KEY_X)
403 else if(key==Msp::Input::KEY_F && shift)
405 const set<Track *> &tracks = selection.get_objects<Track>();
406 const set<Track *> <racks = layout->get_all<Track>();
407 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
409 (*i)->set_flex(!(*i)->get_flex());
411 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
413 (*i)->snap_to(**j, true);
415 update_object_icon(**i);
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)
427 add_selection_to_route();
429 add_selection_to_zone();
431 else if(key==Msp::Input::KEY_C)
432 use_tool_with_selected_objects<ExtendTool>()->connect();
433 else if(key==Msp::Input::KEY_V)
435 else if(key==Msp::Input::KEY_P)
437 else if(key==Msp::Input::KEY_TAB)
439 Object *obj = selection.get_object();
440 if(Terrain *terrain = dynamic_cast<Terrain *>(obj))
441 use_tool(new TerrainTool(*this, keyboard, mouse, *terrain));
446 T *Designer::use_tool_with_selected_objects()
448 T *tool = new T(*this, keyboard, mouse, selection.get_objects());
454 T *Designer::use_tool()
456 T *tool = new T(*this, keyboard, mouse);
461 void Designer::use_tool(Tool *tool)
470 cur_tool->signal_status.connect(sigc::mem_fun(this, &Designer::tool_status));
472 tool_status(tool->get_status());
477 void Designer::use_select_tool()
479 use_tool(new ObjectSelectTool(*this, keyboard, mouse, selection));
482 void Designer::button_press(unsigned btn)
484 Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
485 Vector ground = map_pointer_to_ground(pointer);
491 obj = pick_object(pointer);
495 obj = obj->clone(layout);
496 obj->set_position(ground);
499 selection.add_object(obj);
507 void Designer::render()
509 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
511 View3D *view = (mode==CATALOGUE ? cat_view : main_view);
517 Object *Designer::pick_object(const Vector &pointer)
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));
523 return view.get_layout().get_layout().pick<Object>(Ray(cpos, cray.slice<3>(0)));
526 void Designer::update_object_icon(Object &obj)
528 Object3D &obj3d = layout_3d->get_3d(obj);
529 overlay->clear(obj3d);
531 if(Track *track = dynamic_cast<Track *>(&obj))
533 if(track->get_flex())
534 overlay->add_graphic(obj3d, "flex");
536 if(unsigned saddr = track->get_sensor_address())
538 overlay->add_graphic(obj3d, "trackcircuit");
539 overlay->set_label(obj3d, lexical_cast<string>(saddr));
541 else if(track->get_type().is_turnout())
543 unsigned taddr = track->get_turnout_address();
546 overlay->add_graphic(obj3d, "turnout");
547 overlay->set_label(obj3d, lexical_cast<string>(taddr));
551 else if(Signal *signal = dynamic_cast<Signal *>(&obj))
553 overlay->add_graphic(obj3d, "signal");
554 overlay->set_label(obj3d, lexical_cast<string>(signal->get_address()));
556 else if(BeamGate *gate = dynamic_cast<BeamGate *>(&obj))
558 overlay->add_graphic(obj3d, "beamgate");
559 overlay->set_label(obj3d, lexical_cast<string>(gate->get_address()));
563 void Designer::tool_status(const string &status)
565 lbl_status->set_text(status);
570 void Designer::object_properties_response(int)
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);
577 void Designer::route_name_accept(const string &text)
580 cur_route->set_name(text);
583 void Designer::svg_export_accept(const string &text)
585 SvgExporter svg_exp(*layout);
589 string Designer::tooltip(int x, int y)
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)))
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))
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());
607 const string &descr = otype.get_description();
611 info += otype.get_description();
620 void Designer::clear_paths()
622 for(list<Path3D *>::iterator i=highlight_paths.begin(); i!=highlight_paths.end(); ++i)
624 highlight_paths.clear();
627 void Designer::show_route(const Route &route)
631 const set<Track *> &rtracks = route.get_tracks();
632 for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
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);
643 void Designer::show_zone(const Zone &zone)
647 const Zone::TrackSet &ztracks = zone.get_tracks();
648 for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
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);