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