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