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