]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Do not require unique names for routes
[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                 const set<Track *> &stracks = selection.get_tracks();
231                 set<const Track *> tracks(stracks.begin(), stracks.end());
232                 cur_route->add_tracks(tracks);
233         }
234         catch(const Exception &e)
235         {
236                 IO::print("%s\n", e.what());
237         }
238
239         show_route(cur_route);
240 }
241
242 Point Designer::map_pointer_coords(int x, int y)
243 {
244         float xf = x*2.0/window.get_width()-1.0;
245         float yf = y*2.0/window.get_height()-1.0;
246         GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
247         const GL::Vector3 &pos = camera.get_position();
248
249         return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
250 }
251
252 void Designer::tick()
253 {
254         const Msp::Time::TimeStamp t = Msp::Time::now();
255         float dt = (t-last_tick)/Msp::Time::sec;
256         last_tick = t;
257
258         window.get_display().tick();
259         root->tick();
260         camera_ctl.tick(dt);
261
262         render();
263
264         window.swap_buffers();
265 }
266
267 void Designer::key_press(unsigned key, unsigned mod, wchar_t)
268 {
269         key = Input::key_from_sys(key);
270         mod = Input::mod_from_sys(mod);
271
272         if(key==Msp::Input::KEY_N)
273                 new_track();
274         else if(key==Msp::Input::KEY_G)
275         {
276                 manipulator.start_move();
277                 mode = MANIPULATE;
278         }
279         else if(key==Msp::Input::KEY_R)
280         {
281                 manipulator.start_rotate();
282                 mode = MANIPULATE;
283         }
284         else if(key==Msp::Input::KEY_D)
285         {
286                 manipulator.duplicate();
287                 manipulator.start_move();
288                 mode = MANIPULATE;
289         }
290         else if(key==Msp::Input::KEY_W)
291                 save();
292         else if(key==Msp::Input::KEY_PLUS)
293                 selection.select_more();
294         else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
295         {
296                 const set<Track *> &tracks = layout->get_tracks();
297                 float len = 0;
298                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
299                         len += (*i)->get_type().get_total_length();
300                 IO::print("Total length: %.1fm\n", len);
301         }
302         else if(key==Msp::Input::KEY_L)
303                 selection.select_linked();
304         else if(key==Msp::Input::KEY_M)
305         {
306                 measure.start();
307                 mode = MEASURE;
308         }
309         else if(key==Msp::Input::KEY_Z)
310         {
311                 manipulator.start_elevate();
312                 mode = MANIPULATE;
313         }
314         else if(key==Msp::Input::KEY_ESC)
315         {
316                 if(mode==MANIPULATE)
317                         manipulator.cancel();
318                 else if(mode==CATALOGUE)
319                         mode = SELECT;
320                 else
321                         selection.clear();
322         }
323         else if(key==Msp::Input::KEY_X)
324         {
325                 set<Track *> tracks = selection.get_tracks();
326                 selection.clear();
327                 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
328                 {
329                         overlay->clear(layout_3d->get_track(**i));
330                         layout->remove_track(**i);
331                         delete *i;
332                 }
333         }
334         else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
335         {
336                 const set<Track *> &tracks = selection.get_tracks();
337                 const set<Track *> &ltracks = layout->get_tracks();
338                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
339                 {
340                         (*i)->set_flex(!(*i)->get_flex());
341                         (*i)->break_links();
342                         for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
343                                 if(*j!=*i)
344                                         (*i)->snap_to(**j, true);
345
346                         update_track_icon(layout_3d->get_track(**i));
347                 }
348         }
349         else if(key==Msp::Input::KEY_F)
350                 manipulator.flatten();
351         else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
352                 manipulator.even_slope(true);
353         else if(key==Msp::Input::KEY_E)
354                 manipulator.even_slope();
355         else if(key==Msp::Input::KEY_T)
356                 set_turnout_id();
357         else if(key==Msp::Input::KEY_S)
358                 set_sensor_id();
359         else if(key==Msp::Input::KEY_A)
360                 add_selection_to_route();
361 }
362
363 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
364 {
365         y = window.get_height()-y-1;
366         mod = Input::mod_from_sys(mod);
367
368         Point ground = map_pointer_coords(x, y);
369
370         if(mode==CATALOGUE)
371         {
372                 if(btn==1)
373                 {
374                         Track3D *ctrack = pick_track(x, y);
375                         if(ctrack)
376                         {
377                                 Track *track = new Track(*layout, ctrack->get_track().get_type());
378                                 track->set_position(ground);
379
380                                 selection.clear();
381                                 selection.add_track(track);
382
383                                 mode = SELECT;
384                         }
385                 }
386                 else
387                         mode = SELECT;
388         }
389         else if(mode==SELECT)
390         {
391                 if(btn==1)
392                 {
393                         Track3D *track = pick_track(x, y);
394                         if(track)
395                         {
396                                 if(!(mod&Input::MOD_SHIFT))
397                                         selection.clear();
398                                 selection.toggle_track(&track->get_track());
399                         }
400                 }
401         }
402         else if(mode==MANIPULATE)
403                 manipulator.button_press(x, y, ground.x, ground.y, btn);
404         else if(mode==MEASURE)
405                 measure.button_press(x, y, ground.x, ground.y, btn);
406 }
407
408 void Designer::pointer_motion(int x, int y)
409 {
410         y = window.get_height()-y-1;
411
412         if(!root->get_child_at(x, y))
413         {
414                 Point ground = map_pointer_coords(x, y);
415                 manipulator.pointer_motion(x, y, ground.x, ground.y);
416                 measure.pointer_motion(x, y, ground.x, ground.y);
417         }
418 }
419
420 void Designer::apply_camera()
421 {
422         if(mode==CATALOGUE)
423         {
424                 GL::matrix_mode(GL::PROJECTION);
425                 GL::load_identity();
426                 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
427                 GL::matrix_mode(GL::MODELVIEW);
428                 GL::load_identity();
429                 GL::translate(0, 0, -1);
430         }
431         else
432                 camera.apply();
433 }
434
435 void Designer::render()
436 {
437         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
438
439         if(mode==CATALOGUE)
440         {
441                 apply_camera();
442                 cat_layout_3d->get_scene().render();
443         }
444         else
445         {
446                 pipeline->render_all();
447                 GL::enable(GL_CULL_FACE);
448                 {
449                         GL::Bind bind_blend(GL::Blend::alpha());
450                         overlay->render(0);
451                 }
452
453                 GL::Bind bind_depth(GL::DepthTest::lequal());
454                 manipulator.render();
455                 if(mode==MEASURE)
456                         measure.render();
457         }
458
459         GL::matrix_mode(GL::PROJECTION);
460         GL::load_identity();
461         GL::ortho_bottomleft(window.get_width(), window.get_height());
462         GL::matrix_mode(GL::MODELVIEW);
463         GL::load_identity();
464
465         GL::disable(GL::DEPTH_TEST);
466
467         GL::Bind bind_blend(GL::Blend::alpha());
468         root->render();
469         GL::Texture::unbind();
470 }
471
472 Track3D *Designer::pick_track(int x, int y)
473 {
474         Layout3D *l = layout_3d;
475         if(mode==CATALOGUE)
476                 l = cat_layout_3d;
477
478         float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
479         float yy = (float(y)/window.get_height()-0.5)*0.82843;
480         float size = 4.0/window.get_height()*0.82843;
481
482         apply_camera();
483
484         return l->pick_track(xx, yy, size);
485 }
486
487 void Designer::update_track_icon(Track3D &track)
488 {
489         overlay->clear(track);
490
491         if(track.get_track().get_flex())
492                 overlay->add_graphic(track, "flex");
493
494         if(unsigned sid = track.get_track().get_sensor_id())
495         {
496                 overlay->add_graphic(track, "sensor");
497                 overlay->set_label(track, lexical_cast(sid));
498         }
499         else if(unsigned tid = track.get_track().get_turnout_id())
500         {
501                 if(tid<0x800)
502                 {
503                         overlay->add_graphic(track, "turnout");
504                         overlay->set_label(track, lexical_cast(tid));
505                 }
506         }
507 }
508
509 void Designer::manipulation_status(const string &status)
510 {
511         lbl_status->set_text(status);
512 }
513
514 void Designer::manipulation_done(bool)
515 {
516         lbl_status->set_text(string());
517         mode = SELECT;
518 }
519
520 void Designer::measure_changed()
521 {
522         float pard = measure.get_parallel_distance()*1000;
523         float perpd = measure.get_perpendicular_distance()*1000;
524         float d = sqrt(pard*pard+perpd*perpd);
525         float adiff = measure.get_angle_difference()*180/M_PI;
526         string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
527         lbl_status->set_text(info);
528 }
529
530 void Designer::measure_done()
531 {
532         lbl_status->set_text(string());
533         mode = SELECT;
534 }
535
536 void Designer::turnout_id_accept(const string &text)
537 {
538         Track *track = selection.get_track();
539         unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
540         track->set_turnout_id(id);
541
542         update_track_icon(layout_3d->get_track(*track));
543 }
544
545 void Designer::sensor_id_accept(const string &text)
546 {
547         const set<Track *> &tracks = selection.get_tracks();
548         unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
549         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
550         {
551                 (*i)->set_sensor_id(id);
552
553                 update_track_icon(layout_3d->get_track(**i));
554         }
555 }
556
557 void Designer::route_name_accept(const string &text)
558 {
559         if(cur_route)
560                 cur_route->set_name(text);
561 }
562
563 void Designer::view_all()
564 {
565         Point minp;
566         Point maxp;
567
568         const list<Track3D *> &tracks = layout_3d->get_tracks();
569         for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
570         {
571                 Point tmin;
572                 Point tmax;
573                 (*i)->get_bounds(0, tmin, tmax);
574                 minp.x = min(minp.x, tmin.x);
575                 minp.y = min(minp.y, tmin.y);
576                 maxp.x = max(maxp.x, tmax.x);
577                 maxp.y = max(maxp.y, tmax.y);
578         }
579
580         float t = tan(camera.get_field_of_view()/2)*2;
581         float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
582         float cam_dist = size/t+size*0.25;
583         Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2);
584         camera.set_position(GL::Vector3(center.x, center.y-cam_dist*0.5, cam_dist*0.866));
585         camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
586 }
587
588 string Designer::tooltip(int x, int y)
589 {
590         if(Track3D *t3d = pick_track(x, y))
591         {
592                 const Track &track = t3d->get_track();
593                 const TrackType &ttype = track.get_type();
594                 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
595                 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
596                         info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
597                 if(track.get_turnout_id())
598                         info += format(" (turnout %d)", track.get_turnout_id());
599                 else if(track.get_sensor_id())
600                         info += format(" (sensor %d)", track.get_sensor_id());
601
602                 return info;
603         }
604
605         return string();
606 }
607
608 void Designer::show_route(const Route *route)
609 {
610         const set<Track *> &ltracks = layout->get_tracks();
611         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
612         {
613                 Track3D &t3d = layout_3d->get_track(**i);
614                 if(route && route->get_tracks().count(*i))
615                 {
616                         t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
617                         if((*i)->get_type().is_turnout())
618                         {
619                                 unsigned tid = (*i)->get_turnout_id();
620                                 int path = (tid ? route->get_turnout(tid) : -1);
621                                 if(path>=0)
622                                         t3d.get_path().set_path(path);
623                                 else
624                                         t3d.get_path().set_mask((*i)->get_type().get_paths());
625                         }
626                         else
627                                 t3d.get_path().set_path(0);
628                 }
629                 else
630                         t3d.get_path().set_mask(0);
631         }
632 }