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