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