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