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