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 "rotatetool.h"
33 #include "selection.h"
34 #include "slopetool.h"
35 #include "svgexporter.h"
37 #include "trackproperties.h"
39 #include "zoneproperties.h"
45 Designer::Designer(int argc, char **argv):
50 root(ui_res, &window, &keyboard, &mouse),
58 window.set_title("Railway Designer");
59 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
61 selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
63 // Setup catalogue and layout
64 DataFile::load(catalogue, "tracks.dat");
66 cat_layout_3d = new Layout3D(catalogue.get_layout());
68 layout = new Layout(catalogue);
69 layout_3d = new Layout3D(*layout);
74 DataFile::load(*layout, argv[1]);
76 if(!layout->get_base().empty())
78 base_object = new GL::Object;
79 DataFile::load(*base_object, layout->get_base());
84 cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
86 main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
87 GL::Pipeline *pipeline = &main_view->get_pipeline();
89 GL::Pipeline::Pass *pass = &pipeline->add_pass("unlit");
90 pass->set_depth_test(&GL::DepthTest::lequal());
91 pass->set_blend(&GL::Blend::alpha());
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());
98 pass = &pipeline->add_pass("overlay");
99 pass->set_blend(&GL::Blend::alpha());
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");
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);
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));
119 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
122 if(i!=toolbars.begin())
124 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(root_layout, &GLtk::Layout::update)));
127 root_layout->add_constraint(**i, GLtk::Layout::RIGHT_OF, *prev, 0);
128 root_layout->add_constraint(**i, GLtk::Layout::COPY_HEIGHT, *prev);
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);
139 lbl_status = new GLtk::Label;
140 statusbar->add(*lbl_status);
141 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
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");
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();
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));
157 Designer::~Designer()
159 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
168 delete cat_layout_3d;
177 return Application::main();
180 void Designer::save()
182 InputDialog *input = new InputDialog(*this, "Save layout", filename);
183 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
185 root_layout->set_gravity(*input, 0, 0);
188 void Designer::quit()
193 void Designer::new_track()
199 lbl_status->set_text("Select new track or press Esc to cancel");
202 void Designer::erase_tracks()
207 set<Track *> tracks = selection.get_objects<Track>();
209 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
211 overlay->clear(layout_3d->get<Track3D>(**i));
216 void Designer::track_properties()
218 if(mode!=SELECT || selection.empty())
221 TrackProperties *track_prop = new TrackProperties(selection);
222 root.add(*track_prop);
223 root_layout->set_gravity(*track_prop, 0, 0);
225 track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
228 void Designer::extend_track()
230 use_tool_with_selected_objects<ExtendTool>();
233 void Designer::connect_tracks()
235 use_tool_with_selected_objects<ExtendTool>()->connect();
238 void Designer::flatten_tracks()
240 use_tool_with_selected_objects<SlopeTool>()->flatten();
243 void Designer::svg_export()
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));
248 root_layout->set_gravity(*input, 0, 0);
251 void Designer::edit_route(Route *r)
256 show_route(*cur_route);
261 void Designer::rename_route()
263 if(mode!=SELECT || !cur_route)
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));
269 root_layout->set_gravity(*input, 0, 0);
272 void Designer::add_selection_to_route()
279 cur_route->add_tracks(selection.get_objects<Track>());
281 catch(const exception &e)
283 lbl_status->set_text(e.what());
286 show_route(*cur_route);
289 void Designer::edit_zone(Zone *z)
294 show_zone(*cur_zone);
299 void Designer::zone_properties()
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);
311 void Designer::add_selection_to_zone()
318 cur_zone->add_tracks(selection.get_objects<Track>());
320 catch(const exception &e)
322 lbl_status->set_text(e.what());
325 show_zone(*cur_zone);
328 Vector Designer::map_pointer_to_ground(const Vector &p)
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();
333 return pos-GL::Vector3(vec.x, vec.y, vec.z)*(pos.z/vec.z);
336 void Designer::tick()
338 const Msp::Time::TimeStamp t = Msp::Time::now();
339 float dt = (t-last_tick)/Msp::Time::sec;
342 if(cur_tool && cur_tool->is_done())
344 cur_tool->update_selection(selection);
352 camera_ctl->tick(dt);
356 window.swap_buffers();
359 void Designer::key_press(unsigned key)
361 bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
363 if(key==Msp::Input::KEY_N && shift)
364 use_tool_with_selected_objects<ExtendTool>();
365 else if(key==Msp::Input::KEY_N)
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)
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)
377 Object *obj = (*i)->clone(layout);
378 for(list<Object *>::iterator j=new_objs.begin(); j!=new_objs.end(); ++j)
380 new_objs.push_back(obj);
383 selection.replace(new_objs.begin(), new_objs.end());
385 use_tool_with_selected_objects<MoveTool>();
387 else if(key==Msp::Input::KEY_W)
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)
397 else if(key==Msp::Input::KEY_Z)
398 use_tool_with_selected_objects<ElevateTool>();
399 else if(key==Msp::Input::KEY_ESC)
403 else if(mode==SELECT)
406 else if(key==Msp::Input::KEY_X)
408 else if(key==Msp::Input::KEY_F && shift)
410 const set<Track *> &tracks = selection.get_objects<Track>();
411 const set<Track *> <racks = layout->get_all<Track>();
412 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
414 (*i)->set_flex(!(*i)->get_flex());
416 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
418 (*i)->snap_to(**j, true);
420 update_track_icon(layout_3d->get<Track3D>(**i));
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)
432 add_selection_to_route();
434 add_selection_to_zone();
436 else if(key==Msp::Input::KEY_C)
437 use_tool_with_selected_objects<ExtendTool>()->connect();
438 else if(key==Msp::Input::KEY_V)
440 else if(key==Msp::Input::KEY_P)
445 T *Designer::use_tool_with_selected_objects()
447 T *tool = new T(*this, keyboard, mouse, selection.get_objects());
453 T *Designer::use_tool()
455 T *tool = new T(*this, keyboard, mouse);
460 void Designer::use_tool(Tool *tool)
469 cur_tool->signal_status.connect(sigc::mem_fun(this, &Designer::tool_status));
470 tool_status(cur_tool->get_status());
474 void Designer::button_press(unsigned btn)
476 bool shift = keyboard.get_button_state(Input::KEY_SHIFT_L) || keyboard.get_button_state(Input::KEY_SHIFT_R);
478 Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
479 Vector ground = map_pointer_to_ground(pointer);
485 Object *cobj = pick_object(pointer);
488 Object *obj = cobj->clone(layout);
489 obj->set_position(ground);
492 selection.add_object(obj);
500 else if(mode==SELECT)
504 Object *obj = pick_object(pointer);
509 selection.toggle_object(obj);
515 void Designer::render()
517 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
519 View3D *view = (mode==CATALOGUE ? cat_view : main_view);
525 Object *Designer::pick_object(const Vector &pointer)
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));
531 return view.get_layout().get_layout().pick<Object>(Ray(cpos, Vector(cray)));
534 void Designer::update_track_icon(Track3D &track)
536 overlay->clear(track);
538 if(track.get_track().get_flex())
539 overlay->add_graphic(track, "flex");
541 if(unsigned sid = track.get_track().get_sensor_id())
543 overlay->add_graphic(track, "sensor");
544 overlay->set_label(track, lexical_cast<string>(sid));
546 else if(unsigned tid = track.get_track().get_turnout_id())
550 overlay->add_graphic(track, "turnout");
551 overlay->set_label(track, lexical_cast<string>(tid));
556 void Designer::selection_changed()
558 const set<Track *> &tracks = selection.get_objects<Track>();
560 lbl_status->set_text(string());
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));
570 void Designer::tool_status(const string &status)
572 lbl_status->set_text(status);
575 void Designer::track_properties_response(int)
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));
582 void Designer::route_name_accept(const string &text)
585 cur_route->set_name(text);
588 void Designer::svg_export_accept(const string &text)
590 SvgExporter svg_exp(*layout);
594 string Designer::tooltip(int x, int y)
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)))
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))
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());
616 void Designer::clear_paths()
618 for(list<Path3D *>::iterator i=highlight_paths.begin(); i!=highlight_paths.end(); ++i)
620 highlight_paths.clear();
623 void Designer::show_route(const Route &route)
627 const set<Track *> &rtracks = route.get_tracks();
628 for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
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));
638 void Designer::show_zone(const Zone &zone)
642 const Zone::TrackSet &ztracks = zone.get_tracks();
643 for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
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));