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):
53 root(ui_res, &window, &keyboard, &mouse),
61 window.set_title("Railway Designer");
62 window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
64 // Setup catalogue and layout
65 DataFile::load(catalogue, "tracks.dat");
66 DataFile::load(catalogue, "terrain.dat");
68 cat_layout_3d = new Layout3D(catalogue.get_layout());
70 layout = new Layout(catalogue);
71 layout_3d = new Layout3D(*layout);
76 DataFile::load(*layout, argv[1]);
78 if(!layout->get_base().empty())
80 base_object = new GL::Object;
81 DataFile::load(*base_object, layout->get_base());
86 cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
88 main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
89 GL::Pipeline *pipeline = &main_view->get_pipeline();
91 GL::Pipeline::Pass *pass = &pipeline->add_pass("unlit");
92 pass->set_depth_test(&GL::DepthTest::lequal());
93 pass->set_blend(&GL::Blend::alpha());
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());
100 pass = &pipeline->add_pass("overlay");
101 pass->set_blend(&GL::Blend::alpha());
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");
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);
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));
121 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
124 if(i!=toolbars.begin())
126 (*i)->signal_expanded.connect(sigc::hide(sigc::mem_fun(root_layout, &GLtk::Layout::update)));
129 root_layout->add_constraint(**i, GLtk::Layout::RIGHT_OF, *prev, 0);
130 root_layout->add_constraint(**i, GLtk::Layout::COPY_HEIGHT, *prev);
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);
141 lbl_status = new GLtk::Label;
142 statusbar->add(*lbl_status);
143 lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
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");
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();
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);
159 Designer::~Designer()
161 for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
170 delete cat_layout_3d;
179 return Application::main();
182 void Designer::save()
184 InputDialog *input = new InputDialog(*this, "Save layout", filename);
185 input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
187 root_layout->set_gravity(*input, 0, 0);
190 void Designer::quit()
195 void Designer::new_object()
200 lbl_status->set_text("Select new object or press Esc to cancel");
203 void Designer::erase_objects()
207 set<Object *> objects = selection.get_objects();
209 for(set<Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
211 overlay->clear(layout_3d->get_3d(**i));
216 void Designer::object_properties()
219 if(selection.empty())
222 ObjectProperties *dlg = new ObjectProperties(selection);
224 root_layout->set_gravity(*dlg, 0, 0);
226 dlg->signal_response.connect(sigc::mem_fun(this, &Designer::object_properties_response));
229 void Designer::extend_track()
231 use_tool_with_selected_objects<ExtendTool>();
234 void Designer::connect_tracks()
236 use_tool_with_selected_objects<ExtendTool>()->connect();
239 void Designer::flatten_tracks()
241 use_tool_with_selected_objects<SlopeTool>()->flatten();
244 void Designer::svg_export()
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));
249 root_layout->set_gravity(*input, 0, 0);
252 void Designer::edit_route(Route *r)
257 show_route(*cur_route);
262 void Designer::rename_route()
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));
271 root_layout->set_gravity(*input, 0, 0);
274 void Designer::add_selection_to_route()
281 cur_route->add_tracks(selection.get_objects<Track>());
283 catch(const exception &e)
285 lbl_status->set_text(e.what());
288 show_route(*cur_route);
291 void Designer::edit_zone(Zone *z)
296 show_zone(*cur_zone);
301 void Designer::zone_properties()
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);
313 void Designer::add_selection_to_zone()
320 cur_zone->add_tracks(selection.get_objects<Track>());
322 catch(const exception &e)
324 lbl_status->set_text(e.what());
327 show_zone(*cur_zone);
330 Vector Designer::map_pointer_to_ground(const Vector &p)
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();
335 return pos-GL::Vector3(vec.x, vec.y, vec.z)*(pos.z/vec.z);
338 void Designer::tick()
340 const Msp::Time::TimeStamp t = Msp::Time::now();
341 float dt = (t-last_tick)/Msp::Time::sec;
344 if(cur_tool && cur_tool->is_done())
346 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_M)
391 else if(key==Msp::Input::KEY_Z)
392 use_tool_with_selected_objects<ElevateTool>();
393 else if(key==Msp::Input::KEY_ESC)
398 else if(key==Msp::Input::KEY_X)
400 else if(key==Msp::Input::KEY_F && shift)
402 const set<Track *> &tracks = selection.get_objects<Track>();
403 const set<Track *> <racks = layout->get_all<Track>();
404 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
406 (*i)->set_flex(!(*i)->get_flex());
408 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
410 (*i)->snap_to(**j, true);
412 update_object_icon(**i);
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)
424 add_selection_to_route();
426 add_selection_to_zone();
428 else if(key==Msp::Input::KEY_C)
429 use_tool_with_selected_objects<ExtendTool>()->connect();
430 else if(key==Msp::Input::KEY_V)
432 else if(key==Msp::Input::KEY_P)
434 else if(key==Msp::Input::KEY_TAB)
436 Object *obj = selection.get_object();
437 if(Terrain *terrain = dynamic_cast<Terrain *>(obj))
438 use_tool(new TerrainTool(*this, keyboard, mouse, *terrain));
443 T *Designer::use_tool_with_selected_objects()
445 T *tool = new T(*this, keyboard, mouse, selection.get_objects());
451 T *Designer::use_tool()
453 T *tool = new T(*this, keyboard, mouse);
458 void Designer::use_tool(Tool *tool)
467 cur_tool->signal_status.connect(sigc::mem_fun(this, &Designer::tool_status));
468 tool_status(cur_tool->get_status());
472 void Designer::use_select_tool()
474 use_tool(new ObjectSelectTool(*this, keyboard, mouse, selection));
477 void Designer::button_press(unsigned btn)
479 Vector pointer(mouse.get_axis_value(0), mouse.get_axis_value(1), 0);
480 Vector ground = map_pointer_to_ground(pointer);
486 obj = pick_object(pointer);
490 obj = obj->clone(layout);
491 obj->set_position(ground);
494 selection.add_object(obj);
502 void Designer::render()
504 GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
506 View3D *view = (mode==CATALOGUE ? cat_view : main_view);
512 Object *Designer::pick_object(const Vector &pointer)
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));
518 return view.get_layout().get_layout().pick<Object>(Ray(cpos, Vector(cray)));
521 void Designer::update_object_icon(Object &obj)
523 Object3D &obj3d = layout_3d->get_3d(obj);
524 overlay->clear(obj3d);
526 if(Track *track = dynamic_cast<Track *>(&obj))
528 if(track->get_flex())
529 overlay->add_graphic(obj3d, "flex");
531 if(unsigned saddr = track->get_sensor_address())
533 overlay->add_graphic(obj3d, "trackcircuit");
534 overlay->set_label(obj3d, lexical_cast<string>(saddr));
536 else if(unsigned taddr = track->get_turnout_address())
540 overlay->add_graphic(obj3d, "turnout");
541 overlay->set_label(obj3d, lexical_cast<string>(taddr));
545 else if(Signal *signal = dynamic_cast<Signal *>(&obj))
547 overlay->add_graphic(obj3d, "signal");
548 overlay->set_label(obj3d, lexical_cast<string>(signal->get_address()));
550 else if(BeamGate *gate = dynamic_cast<BeamGate *>(&obj))
552 overlay->add_graphic(obj3d, "beamgate");
553 overlay->set_label(obj3d, lexical_cast<string>(gate->get_address()));
557 void Designer::tool_status(const string &status)
559 lbl_status->set_text(status);
562 void Designer::object_properties_response(int)
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);
569 void Designer::route_name_accept(const string &text)
572 cur_route->set_name(text);
575 void Designer::svg_export_accept(const string &text)
577 SvgExporter svg_exp(*layout);
581 string Designer::tooltip(int x, int y)
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)))
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))
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());
603 void Designer::clear_paths()
605 for(list<Path3D *>::iterator i=highlight_paths.begin(); i!=highlight_paths.end(); ++i)
607 highlight_paths.clear();
610 void Designer::show_route(const Route &route)
614 const set<Track *> &rtracks = route.get_tracks();
615 for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
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));
625 void Designer::show_zone(const Zone &zone)
629 const Zone::TrackSet &ztracks = zone.get_tracks();
630 for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
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));