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