]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Match the API change in GLtk
[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;
133         root.add(*statusbar);
134         statusbar->set_size(window.get_width(), 20);
135         statusbar->set_focusable(false);
136
137         lbl_status = new GLtk::Label;
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         if(mode==MANIPULATE_DONE)
275                 mode = SELECT;
276
277         window.tick();
278         root.tick();
279         camera_ctl.tick(dt);
280
281         for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
282                 layout_3d->get_track(**i).get_path().set_mask(0);
283         new_tracks.clear();
284
285         render();
286
287         window.swap_buffers();
288 }
289
290 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
291 {
292         key = Input::key_from_sys(key);
293         mod = Input::mod_from_sys(mod);
294
295         if(key==Msp::Input::KEY_N && (mod&Input::MOD_SHIFT))
296         {
297                 manipulator.start_extend();
298                 mode = MANIPULATE;
299         }
300         else if(key==Msp::Input::KEY_N)
301                 new_track();
302         else if(key==Msp::Input::KEY_G)
303         {
304                 manipulator.start_move();
305                 mode = MANIPULATE;
306         }
307         else if(key==Msp::Input::KEY_R)
308         {
309                 manipulator.start_rotate();
310                 mode = MANIPULATE;
311         }
312         else if(key==Msp::Input::KEY_D)
313         {
314                 manipulator.duplicate();
315                 manipulator.start_move();
316                 mode = MANIPULATE;
317         }
318         else if(key==Msp::Input::KEY_W)
319                 save();
320         else if(key==Msp::Input::KEY_PLUS)
321                 selection.select_more();
322         else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
323                 selection.select_blocks();
324         else if(key==Msp::Input::KEY_L)
325                 selection.select_linked();
326         else if(key==Msp::Input::KEY_M)
327         {
328                 measure.start();
329                 mode = MEASURE;
330         }
331         else if(key==Msp::Input::KEY_Z)
332         {
333                 manipulator.start_elevate();
334                 mode = MANIPULATE;
335         }
336         else if(key==Msp::Input::KEY_ESC)
337         {
338                 if(mode==MANIPULATE)
339                         manipulator.cancel();
340                 else if(mode==CATALOGUE)
341                         mode = SELECT;
342                 else
343                         selection.clear();
344         }
345         else if(key==Msp::Input::KEY_X)
346         {
347                 set<Track *> tracks = selection.get_tracks();
348                 selection.clear();
349                 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
350                 {
351                         overlay->clear(layout_3d->get_track(**i));
352                         layout->remove_track(**i);
353                         delete *i;
354                 }
355         }
356         else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
357         {
358                 const set<Track *> &tracks = selection.get_tracks();
359                 const set<Track *> &ltracks = layout->get_tracks();
360                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
361                 {
362                         (*i)->set_flex(!(*i)->get_flex());
363                         (*i)->break_links();
364                         for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
365                                 if(*j!=*i)
366                                         (*i)->snap_to(**j, true);
367
368                         update_track_icon(layout_3d->get_track(**i));
369                 }
370         }
371         else if(key==Msp::Input::KEY_F)
372                 manipulator.flatten();
373         else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
374                 manipulator.even_slope(true);
375         else if(key==Msp::Input::KEY_E)
376                 manipulator.even_slope();
377         else if(key==Msp::Input::KEY_T)
378                 set_turnout_id();
379         else if(key==Msp::Input::KEY_S)
380                 set_sensor_id();
381         else if(key==Msp::Input::KEY_A)
382                 add_selection_to_route();
383         else if(key==Msp::Input::KEY_C)
384                 manipulator.connect();
385         else if(key==Msp::Input::KEY_V)
386                 svg_export();
387 }
388
389 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
390 {
391         y = window.get_height()-y-1;
392         mod = Input::mod_from_sys(mod);
393
394         Point ground = map_pointer_to_ground(x, y);
395
396         if(mode==CATALOGUE)
397         {
398                 if(btn==1)
399                 {
400                         Track3D *ctrack = pick_track(x, y);
401                         if(ctrack)
402                         {
403                                 Track *track = new Track(*layout, ctrack->get_track().get_type());
404                                 track->set_position(ground);
405
406                                 selection.clear();
407                                 selection.add_track(track);
408
409                                 mode = SELECT;
410                         }
411                 }
412                 else
413                         mode = SELECT;
414         }
415         else if(mode==SELECT)
416         {
417                 if(btn==1)
418                 {
419                         Track3D *track = pick_track(x, y);
420                         if(track)
421                         {
422                                 if(!(mod&Input::MOD_SHIFT))
423                                         selection.clear();
424                                 selection.toggle_track(&track->get_track());
425                         }
426                 }
427         }
428         else if(mode==MEASURE)
429                 measure.button_press(x, y, ground.x, ground.y, btn);
430 }
431
432 void Designer::pointer_motion(int x, int y)
433 {
434         y = window.get_height()-y-1;
435
436         if(!root.get_child_at(x, y))
437         {
438                 Point ground = map_pointer_to_ground(x, y);
439                 measure.pointer_motion(x, y, ground.x, ground.y);
440         }
441 }
442
443 void Designer::apply_camera()
444 {
445         if(mode==CATALOGUE)
446         {
447                 GL::matrix_mode(GL::PROJECTION);
448                 GL::load_identity();
449                 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
450                 GL::matrix_mode(GL::MODELVIEW);
451                 GL::load_identity();
452                 GL::translate(0, 0, -1);
453         }
454         else
455                 camera.apply();
456 }
457
458 void Designer::render()
459 {
460         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
461
462         if(mode==CATALOGUE)
463         {
464                 apply_camera();
465                 cat_layout_3d->get_scene().render();
466         }
467         else
468         {
469                 pipeline->render_all();
470                 GL::enable(GL_CULL_FACE);
471                 {
472                         GL::Bind bind_blend(GL::Blend::alpha());
473                         overlay->render(0);
474                 }
475
476                 GL::Bind bind_depth(GL::DepthTest::lequal());
477                 if(mode==MEASURE)
478                         measure.render();
479         }
480
481         GL::matrix_mode(GL::PROJECTION);
482         GL::load_identity();
483         GL::ortho_bottomleft(window.get_width(), window.get_height());
484         GL::matrix_mode(GL::MODELVIEW);
485         GL::load_identity();
486
487         GL::disable(GL::DEPTH_TEST);
488
489         GL::Bind bind_blend(GL::Blend::alpha());
490         root.render();
491         GL::Texture::unbind();
492 }
493
494 void Designer::track_added(Track &trk)
495 {
496         new_tracks.push_back(&trk);
497 }
498
499 void Designer::track_removed(Track &trk)
500 {
501         list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
502         if(i!=new_tracks.end())
503                 new_tracks.erase(i);
504 }
505
506 Track3D *Designer::pick_track(int x, int y)
507 {
508         Layout3D *l = layout_3d;
509         if(mode==CATALOGUE)
510                 l = cat_layout_3d;
511
512         float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
513         float yy = (float(y)/window.get_height()-0.5)*0.82843;
514         float size = 4.0/window.get_height()*0.82843;
515
516         apply_camera();
517
518         return l->pick_track(xx, yy, size);
519 }
520
521 void Designer::update_track_icon(Track3D &track)
522 {
523         overlay->clear(track);
524
525         if(track.get_track().get_flex())
526                 overlay->add_graphic(track, "flex");
527
528         if(unsigned sid = track.get_track().get_sensor_id())
529         {
530                 overlay->add_graphic(track, "sensor");
531                 overlay->set_label(track, lexical_cast(sid));
532         }
533         else if(unsigned tid = track.get_track().get_turnout_id())
534         {
535                 if(tid<0x800)
536                 {
537                         overlay->add_graphic(track, "turnout");
538                         overlay->set_label(track, lexical_cast(tid));
539                 }
540         }
541 }
542
543 void Designer::selection_changed()
544 {
545         const set<Track *> &tracks = selection.get_tracks();
546         if(tracks.empty())
547                 lbl_status->set_text(string());
548         else
549         {
550                 float len = 0;
551                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
552                         len += (*i)->get_type().get_total_length();
553                 lbl_status->set_text(format("%.2fm of track selected\n", len));
554         }
555 }
556
557 void Designer::manipulation_status(const string &status)
558 {
559         lbl_status->set_text(status);
560 }
561
562 void Designer::manipulation_done(bool)
563 {
564         mode = MANIPULATE_DONE;
565         selection_changed();
566 }
567
568 void Designer::measure_changed()
569 {
570         float pard = measure.get_parallel_distance()*1000;
571         float perpd = measure.get_perpendicular_distance()*1000;
572         float d = sqrt(pard*pard+perpd*perpd);
573         float adiff = measure.get_angle_difference()*180/M_PI;
574         string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
575         lbl_status->set_text(info);
576 }
577
578 void Designer::measure_done()
579 {
580         mode = SELECT;
581         selection_changed();
582 }
583
584 void Designer::turnout_id_accept(const string &text)
585 {
586         Track *track = selection.get_track();
587         unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
588         track->set_turnout_id(id);
589
590         update_track_icon(layout_3d->get_track(*track));
591 }
592
593 void Designer::sensor_id_accept(const string &text)
594 {
595         const set<Track *> &tracks = selection.get_tracks();
596         unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
597         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
598         {
599                 (*i)->set_sensor_id(id);
600
601                 update_track_icon(layout_3d->get_track(**i));
602         }
603 }
604
605 void Designer::route_name_accept(const string &text)
606 {
607         if(cur_route)
608                 cur_route->set_name(text);
609 }
610
611 void Designer::svg_export_accept(const string &text)
612 {
613         SvgExporter svg_exp(*layout);
614         svg_exp.save(text);
615 }
616
617 string Designer::tooltip(int x, int y)
618 {
619         if(Track3D *t3d = pick_track(x, y))
620         {
621                 const Track &track = t3d->get_track();
622                 const TrackType &ttype = track.get_type();
623                 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
624                 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
625                         info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
626                 if(track.get_turnout_id())
627                         info += format(" (turnout %d)", track.get_turnout_id());
628                 else if(track.get_sensor_id())
629                         info += format(" (sensor %d)", track.get_sensor_id());
630
631                 return info;
632         }
633
634         return string();
635 }
636
637 void Designer::show_route(const Route *route)
638 {
639         const set<Track *> &ltracks = layout->get_tracks();
640         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
641         {
642                 Track3D &t3d = layout_3d->get_track(**i);
643                 if(route && route->has_track(**i))
644                 {
645                         t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
646                         if((*i)->get_type().is_turnout())
647                         {
648                                 unsigned tid = (*i)->get_turnout_id();
649                                 int path = (tid ? route->get_turnout(tid) : -1);
650                                 if(path>=0)
651                                         t3d.get_path().set_path(path);
652                                 else
653                                         t3d.get_path().set_mask((*i)->get_type().get_paths());
654                         }
655                         else
656                                 t3d.get_path().set_path(0);
657                 }
658                 else
659                         t3d.get_path().set_mask(0);
660         }
661 }