]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Add an SVG exporter to Designer
[r2c2.git] / source / designer / designer.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <signal.h>
9 #include <algorithm>
10 #include <cmath>
11 #include <msp/fs/utils.h>
12 #include <msp/gl/blend.h>
13 #include <msp/gl/framebuffer.h>
14 #include <msp/gl/matrix.h>
15 #include <msp/gl/misc.h>
16 #include <msp/gl/projection.h>
17 #include <msp/gl/rendermode.h>
18 #include <msp/gl/select.h>
19 #include <msp/gl/tests.h>
20 #include <msp/gl/texture2d.h>
21 #include <msp/input/keys.h>
22 #include <msp/io/print.h>
23 #include <msp/strings/codec.h>
24 #include <msp/strings/lexicalcast.h>
25 #include <msp/strings/utf8.h>
26 #include <msp/strings/utils.h>
27 #include <msp/time/units.h>
28 #include <msp/time/utils.h>
29 #include "libmarklin/route.h"
30 #include "libmarklin/tracktype.h"
31 #include "3d/path.h"
32 #include "designer.h"
33 #include "input.h"
34 #include "manipulator.h"
35 #include "measure.h"
36 #include "selection.h"
37 #include "svgexporter.h"
38 #include "toolbar.h"
39
40 using namespace std;
41 using namespace Marklin;
42 using namespace Msp;
43
44 Application::RegApp<Designer> Designer::reg;
45
46 Designer::Designer(int argc, char **argv):
47         window(1280, 960),
48         ui_res("marklin.res"),
49         root(ui_res, window),
50         base_object(0),
51         cur_route(0),
52         mode(SELECT),
53         manipulator(*this, root, selection),
54         measure(*this),
55         camera_ctl(*this, root, camera),
56         track_wrap(*this, selection)
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         manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
63         manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
64         measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
65         measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
66
67         // Setup catalogue and layout
68         DataFile::load(catalogue, "tracks.dat");
69
70         cat_layout_3d = new Layout3D(catalogue.get_layout());
71
72         layout = new Layout(catalogue);
73         layout_3d = new Layout3D(*layout);
74
75         layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
76         layout->signal_track_removed.connect(sigc::mem_fun(this, &Designer::track_removed));
77
78         if(argc>1)
79         {
80                 filename = argv[1];
81                 DataFile::load(*layout, argv[1]);
82
83                 if(!layout->get_base().empty())
84                 {
85                         base_object = new GL::Object;
86                         DataFile::load(*base_object, layout->get_base());
87                 }
88         }
89
90         // Setup OpenGL
91         GL::enable(GL_CULL_FACE);
92
93         pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
94         pipeline->set_camera(&camera);
95         pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
96         if(base_object)
97                 pipeline->add_renderable(*base_object);
98         pipeline->add_renderable_for_pass(track_wrap, "unlit");
99         pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
100         pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
101
102         light.set_position(0, -0.259, 0.966, 0);
103         lighting.attach(0, light);
104
105         GL::PipelinePass *pass = &pipeline->add_pass(0);
106         pass->lighting = &lighting;
107         pass->depth_test = &GL::DepthTest::lequal();
108
109         pass = &pipeline->add_pass("unlit");
110         pass->depth_test = &GL::DepthTest::lequal();
111         pass->blend = &GL::Blend::alpha();
112
113         pass = &pipeline->add_pass("blended");
114         pass->lighting = &lighting;
115         pass->depth_test = &GL::DepthTest::lequal();
116         pass->blend = &GL::Blend::alpha();
117
118         camera.set_up_direction(GL::Vector3(0, 0, 1));
119         camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
120
121         // Setup UI
122         root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
123         root.signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
124         root.signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
125         root.signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
126
127         toolbar = new Toolbar(*this);
128         root.add(*toolbar);
129         toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
130         toolbar->set_focusable(false);
131
132         GLtk::Panel *statusbar = new GLtk::Panel(ui_res);
133         root.add(*statusbar);
134         statusbar->set_size(window.get_width(), 20);
135         statusbar->set_focusable(false);
136
137         lbl_status = new GLtk::Label(ui_res);
138         statusbar->add(*lbl_status);
139         lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
140
141         overlay = new Overlay3D(window, camera, ui_res.get_default_font());
142
143         camera_ctl.view_all();
144
145         const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
146         for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
147                 update_track_icon(*i->second);
148 }
149
150 Designer::~Designer()
151 {
152         delete overlay;
153         delete pipeline;
154         delete base_object;
155         delete layout_3d;
156         delete layout;
157         delete cat_layout_3d;
158 }
159
160 int Designer::main()
161 {
162         window.show();
163
164         mode = SELECT;
165
166         return Application::main();
167 }
168
169 void Designer::save()
170 {
171         InputDialog *input = new InputDialog(*this, "Save layout", filename);
172         input->signal_accept.connect(sigc::mem_fun(layout, &Layout::save));
173 }
174
175 void Designer::quit()
176 {
177         exit(0);
178 }
179
180 void Designer::new_track()
181 {
182         mode = CATALOGUE;
183 }
184
185 void Designer::set_turnout_id()
186 {
187         Track *track = selection.get_track();
188         if(selection.size()==1 && track->get_type().is_turnout())
189         {
190                 InputDialog *input = new InputDialog(*this, "Turnout ID", lexical_cast(track->get_turnout_id()));
191                 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
192         }
193 }
194
195 void Designer::set_sensor_id()
196 {
197         const set<Track *> &tracks = selection.get_tracks();
198         bool ok = false;
199         int id = -1;
200         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
201         {
202                 if(!(*i)->get_type().is_turnout())
203                         ok = true;
204                 if(static_cast<int>((*i)->get_sensor_id())!=id)
205                 {
206                         if(id==-1)
207                                 id = (*i)->get_sensor_id();
208                         else
209                                 id = -2;
210                 }
211         }
212
213         if(ok)
214         {
215                 InputDialog *input = new InputDialog(*this, "Sensor ID", (id>=0 ? lexical_cast(id) : string()));
216                 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
217         }
218 }
219
220 void Designer::rename_route()
221 {
222         if(!cur_route)
223                 return;
224
225         InputDialog *input = new InputDialog(*this, "Route name", cur_route->get_name());
226         input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
227 }
228
229 void Designer::svg_export()
230 {
231         InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
232         input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
233 }
234
235 void Designer::edit_route(Route *r)
236 {
237         cur_route = r;
238         show_route(r);
239 }
240
241 void Designer::add_selection_to_route()
242 {
243         if(!cur_route)
244                 return;
245
246         try
247         {
248                 cur_route->add_tracks(selection.get_tracks());
249         }
250         catch(const Exception &e)
251         {
252                 lbl_status->set_text(e.what());
253         }
254
255         show_route(cur_route);
256 }
257
258 Point Designer::map_pointer_to_ground(int x, int y)
259 {
260         float xf = x*2.0/window.get_width()-1.0;
261         float yf = y*2.0/window.get_height()-1.0;
262         GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
263         const GL::Vector3 &pos = camera.get_position();
264
265         return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
266 }
267
268 void Designer::tick()
269 {
270         const Msp::Time::TimeStamp t = Msp::Time::now();
271         float dt = (t-last_tick)/Msp::Time::sec;
272         last_tick = t;
273
274         window.tick();
275         root.tick();
276         camera_ctl.tick(dt);
277
278         for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
279                 layout_3d->get_track(**i).get_path().set_mask(0);
280         new_tracks.clear();
281
282         render();
283
284         window.swap_buffers();
285 }
286
287 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
288 {
289         key = Input::key_from_sys(key);
290         mod = Input::mod_from_sys(mod);
291
292         if(key==Msp::Input::KEY_N && (mod&Input::MOD_SHIFT))
293         {
294                 manipulator.start_extend();
295                 mode = MANIPULATE;
296         }
297         else if(key==Msp::Input::KEY_N)
298                 new_track();
299         else if(key==Msp::Input::KEY_G)
300         {
301                 manipulator.start_move();
302                 mode = MANIPULATE;
303         }
304         else if(key==Msp::Input::KEY_R)
305         {
306                 manipulator.start_rotate();
307                 mode = MANIPULATE;
308         }
309         else if(key==Msp::Input::KEY_D)
310         {
311                 manipulator.duplicate();
312                 manipulator.start_move();
313                 mode = MANIPULATE;
314         }
315         else if(key==Msp::Input::KEY_W)
316                 save();
317         else if(key==Msp::Input::KEY_PLUS)
318                 selection.select_more();
319         else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
320                 selection.select_blocks();
321         else if(key==Msp::Input::KEY_L)
322                 selection.select_linked();
323         else if(key==Msp::Input::KEY_M)
324         {
325                 measure.start();
326                 mode = MEASURE;
327         }
328         else if(key==Msp::Input::KEY_Z)
329         {
330                 manipulator.start_elevate();
331                 mode = MANIPULATE;
332         }
333         else if(key==Msp::Input::KEY_ESC)
334         {
335                 if(mode==MANIPULATE)
336                         manipulator.cancel();
337                 else if(mode==CATALOGUE)
338                         mode = SELECT;
339                 else
340                         selection.clear();
341         }
342         else if(key==Msp::Input::KEY_X)
343         {
344                 set<Track *> tracks = selection.get_tracks();
345                 selection.clear();
346                 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
347                 {
348                         overlay->clear(layout_3d->get_track(**i));
349                         layout->remove_track(**i);
350                         delete *i;
351                 }
352         }
353         else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
354         {
355                 const set<Track *> &tracks = selection.get_tracks();
356                 const set<Track *> &ltracks = layout->get_tracks();
357                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
358                 {
359                         (*i)->set_flex(!(*i)->get_flex());
360                         (*i)->break_links();
361                         for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
362                                 if(*j!=*i)
363                                         (*i)->snap_to(**j, true);
364
365                         update_track_icon(layout_3d->get_track(**i));
366                 }
367         }
368         else if(key==Msp::Input::KEY_F)
369                 manipulator.flatten();
370         else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
371                 manipulator.even_slope(true);
372         else if(key==Msp::Input::KEY_E)
373                 manipulator.even_slope();
374         else if(key==Msp::Input::KEY_T)
375                 set_turnout_id();
376         else if(key==Msp::Input::KEY_S)
377                 set_sensor_id();
378         else if(key==Msp::Input::KEY_A)
379                 add_selection_to_route();
380         else if(key==Msp::Input::KEY_C)
381                 manipulator.connect();
382         else if(key==Msp::Input::KEY_V)
383                 svg_export();
384 }
385
386 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
387 {
388         y = window.get_height()-y-1;
389         mod = Input::mod_from_sys(mod);
390
391         Point ground = map_pointer_to_ground(x, y);
392
393         if(mode==CATALOGUE)
394         {
395                 if(btn==1)
396                 {
397                         Track3D *ctrack = pick_track(x, y);
398                         if(ctrack)
399                         {
400                                 Track *track = new Track(*layout, ctrack->get_track().get_type());
401                                 track->set_position(ground);
402
403                                 selection.clear();
404                                 selection.add_track(track);
405
406                                 mode = SELECT;
407                         }
408                 }
409                 else
410                         mode = SELECT;
411         }
412         else if(mode==SELECT)
413         {
414                 if(btn==1)
415                 {
416                         Track3D *track = pick_track(x, y);
417                         if(track)
418                         {
419                                 if(!(mod&Input::MOD_SHIFT))
420                                         selection.clear();
421                                 selection.toggle_track(&track->get_track());
422                         }
423                 }
424         }
425         else if(mode==MEASURE)
426                 measure.button_press(x, y, ground.x, ground.y, btn);
427 }
428
429 void Designer::pointer_motion(int x, int y)
430 {
431         y = window.get_height()-y-1;
432
433         if(!root.get_child_at(x, y))
434         {
435                 Point ground = map_pointer_to_ground(x, y);
436                 measure.pointer_motion(x, y, ground.x, ground.y);
437         }
438 }
439
440 void Designer::apply_camera()
441 {
442         if(mode==CATALOGUE)
443         {
444                 GL::matrix_mode(GL::PROJECTION);
445                 GL::load_identity();
446                 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
447                 GL::matrix_mode(GL::MODELVIEW);
448                 GL::load_identity();
449                 GL::translate(0, 0, -1);
450         }
451         else
452                 camera.apply();
453 }
454
455 void Designer::render()
456 {
457         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
458
459         if(mode==CATALOGUE)
460         {
461                 apply_camera();
462                 cat_layout_3d->get_scene().render();
463         }
464         else
465         {
466                 pipeline->render_all();
467                 GL::enable(GL_CULL_FACE);
468                 {
469                         GL::Bind bind_blend(GL::Blend::alpha());
470                         overlay->render(0);
471                 }
472
473                 GL::Bind bind_depth(GL::DepthTest::lequal());
474                 if(mode==MEASURE)
475                         measure.render();
476         }
477
478         GL::matrix_mode(GL::PROJECTION);
479         GL::load_identity();
480         GL::ortho_bottomleft(window.get_width(), window.get_height());
481         GL::matrix_mode(GL::MODELVIEW);
482         GL::load_identity();
483
484         GL::disable(GL::DEPTH_TEST);
485
486         GL::Bind bind_blend(GL::Blend::alpha());
487         root.render();
488         GL::Texture::unbind();
489 }
490
491 void Designer::track_added(Track &trk)
492 {
493         new_tracks.push_back(&trk);
494 }
495
496 void Designer::track_removed(Track &trk)
497 {
498         list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
499         if(i!=new_tracks.end())
500                 new_tracks.erase(i);
501 }
502
503 Track3D *Designer::pick_track(int x, int y)
504 {
505         Layout3D *l = layout_3d;
506         if(mode==CATALOGUE)
507                 l = cat_layout_3d;
508
509         float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
510         float yy = (float(y)/window.get_height()-0.5)*0.82843;
511         float size = 4.0/window.get_height()*0.82843;
512
513         apply_camera();
514
515         return l->pick_track(xx, yy, size);
516 }
517
518 void Designer::update_track_icon(Track3D &track)
519 {
520         overlay->clear(track);
521
522         if(track.get_track().get_flex())
523                 overlay->add_graphic(track, "flex");
524
525         if(unsigned sid = track.get_track().get_sensor_id())
526         {
527                 overlay->add_graphic(track, "sensor");
528                 overlay->set_label(track, lexical_cast(sid));
529         }
530         else if(unsigned tid = track.get_track().get_turnout_id())
531         {
532                 if(tid<0x800)
533                 {
534                         overlay->add_graphic(track, "turnout");
535                         overlay->set_label(track, lexical_cast(tid));
536                 }
537         }
538 }
539
540 void Designer::selection_changed()
541 {
542         const set<Track *> &tracks = selection.get_tracks();
543         if(tracks.empty())
544                 lbl_status->set_text(string());
545         else
546         {
547                 float len = 0;
548                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
549                         len += (*i)->get_type().get_total_length();
550                 lbl_status->set_text(format("%.2fm of track selected\n", len));
551         }
552 }
553
554 void Designer::manipulation_status(const string &status)
555 {
556         lbl_status->set_text(status);
557 }
558
559 void Designer::manipulation_done(bool)
560 {
561         mode = SELECT;
562         selection_changed();
563 }
564
565 void Designer::measure_changed()
566 {
567         float pard = measure.get_parallel_distance()*1000;
568         float perpd = measure.get_perpendicular_distance()*1000;
569         float d = sqrt(pard*pard+perpd*perpd);
570         float adiff = measure.get_angle_difference()*180/M_PI;
571         string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
572         lbl_status->set_text(info);
573 }
574
575 void Designer::measure_done()
576 {
577         mode = SELECT;
578         selection_changed();
579 }
580
581 void Designer::turnout_id_accept(const string &text)
582 {
583         Track *track = selection.get_track();
584         unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
585         track->set_turnout_id(id);
586
587         update_track_icon(layout_3d->get_track(*track));
588 }
589
590 void Designer::sensor_id_accept(const string &text)
591 {
592         const set<Track *> &tracks = selection.get_tracks();
593         unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
594         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
595         {
596                 (*i)->set_sensor_id(id);
597
598                 update_track_icon(layout_3d->get_track(**i));
599         }
600 }
601
602 void Designer::route_name_accept(const string &text)
603 {
604         if(cur_route)
605                 cur_route->set_name(text);
606 }
607
608 void Designer::svg_export_accept(const string &text)
609 {
610         SvgExporter svg_exp(*layout);
611         svg_exp.save(text);
612 }
613
614 string Designer::tooltip(int x, int y)
615 {
616         if(Track3D *t3d = pick_track(x, y))
617         {
618                 const Track &track = t3d->get_track();
619                 const TrackType &ttype = track.get_type();
620                 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
621                 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
622                         info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
623                 if(track.get_turnout_id())
624                         info += format(" (turnout %d)", track.get_turnout_id());
625                 else if(track.get_sensor_id())
626                         info += format(" (sensor %d)", track.get_sensor_id());
627
628                 return info;
629         }
630
631         return string();
632 }
633
634 void Designer::show_route(const Route *route)
635 {
636         const set<Track *> &ltracks = layout->get_tracks();
637         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
638         {
639                 Track3D &t3d = layout_3d->get_track(**i);
640                 if(route && route->has_track(**i))
641                 {
642                         t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
643                         if((*i)->get_type().is_turnout())
644                         {
645                                 unsigned tid = (*i)->get_turnout_id();
646                                 int path = (tid ? route->get_turnout(tid) : -1);
647                                 if(path>=0)
648                                         t3d.get_path().set_path(path);
649                                 else
650                                         t3d.get_path().set_mask((*i)->get_type().get_paths());
651                         }
652                         else
653                                 t3d.get_path().set_path(0);
654                 }
655                 else
656                         t3d.get_path().set_mask(0);
657         }
658 }