]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Rewrite CameraController to make use of mouse
[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         track_wrap(*this, selection)
52 {
53         window.set_title("Railway Designer");
54         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
55
56         selection.signal_changed.connect(sigc::mem_fun(this, &Designer::selection_changed));
57         manipulator.signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
58         manipulator.signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
59         measure.signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
60         measure.signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
61
62         // Setup catalogue and layout
63         DataFile::load(catalogue, "tracks.dat");
64
65         cat_layout_3d = new Layout3D(catalogue.get_layout());
66
67         layout = new Layout(catalogue);
68         layout_3d = new Layout3D(*layout);
69
70         layout->signal_track_added.connect(sigc::mem_fun(this, &Designer::track_added));
71
72         if(argc>1)
73         {
74                 filename = argv[1];
75                 DataFile::load(*layout, argv[1]);
76
77                 if(!layout->get_base().empty())
78                 {
79                         base_object = new GL::Object;
80                         DataFile::load(*base_object, layout->get_base());
81                 }
82         }
83
84         // Setup OpenGL
85         GL::enable(GL_CULL_FACE);
86
87         pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
88         pipeline->set_camera(&camera);
89         pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
90         if(base_object)
91                 pipeline->add_renderable(*base_object);
92         pipeline->add_renderable_for_pass(track_wrap, "unlit");
93         pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
94         pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
95
96         light.set_position(0, -0.259, 0.966, 0);
97         lighting.attach(0, light);
98
99         GL::PipelinePass *pass = &pipeline->add_pass(0);
100         pass->lighting = &lighting;
101         pass->depth_test = &GL::DepthTest::lequal();
102
103         pass = &pipeline->add_pass("unlit");
104         pass->depth_test = &GL::DepthTest::lequal();
105         pass->blend = &GL::Blend::alpha();
106
107         pass = &pipeline->add_pass("blended");
108         pass->lighting = &lighting;
109         pass->depth_test = &GL::DepthTest::lequal();
110         pass->blend = &GL::Blend::alpha();
111
112         camera.set_up_direction(GL::Vector3(0, 0, 1));
113         camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
114
115         // Setup UI
116         DataFile::load(ui_res, "marklin.res");
117         root = new GLtk::Root(ui_res, window);
118         root->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
119         root->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
120         root->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
121         root->signal_tooltip.connect(sigc::mem_fun(this, &Designer::tooltip));
122
123         toolbar = new Toolbar(*this);
124         root->add(*toolbar);
125         toolbar->set_position(0, window.get_height()-toolbar->get_geometry().h);
126         toolbar->set_focusable(false);
127
128         GLtk::Panel *statusbar = new GLtk::Panel(ui_res);
129         root->add(*statusbar);
130         statusbar->set_size(window.get_width(), 20);
131         statusbar->set_focusable(false);
132
133         lbl_status = new GLtk::Label(ui_res);
134         statusbar->add(*lbl_status);
135         lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
136
137         overlay = new Overlay3D(window, camera, ui_res.get_default_font());
138
139         camera_ctl = new CameraController(*this, *root, camera);
140         camera_ctl->view_all();
141
142         const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
143         for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
144                 update_track_icon(*i->second);
145 }
146
147 Designer::~Designer()
148 {
149         delete camera_ctl;
150         delete overlay;
151         delete root;
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_coords(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.get_display().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)
286                 new_track();
287         else if(key==Msp::Input::KEY_G)
288         {
289                 manipulator.start_move();
290                 mode = MANIPULATE;
291         }
292         else if(key==Msp::Input::KEY_R)
293         {
294                 manipulator.start_rotate();
295                 mode = MANIPULATE;
296         }
297         else if(key==Msp::Input::KEY_D)
298         {
299                 manipulator.duplicate();
300                 manipulator.start_move();
301                 mode = MANIPULATE;
302         }
303         else if(key==Msp::Input::KEY_W)
304                 save();
305         else if(key==Msp::Input::KEY_PLUS)
306                 selection.select_more();
307         else if(key==Msp::Input::KEY_L && (mod&Input::MOD_SHIFT))
308                 selection.select_blocks();
309         else if(key==Msp::Input::KEY_L)
310                 selection.select_linked();
311         else if(key==Msp::Input::KEY_M)
312         {
313                 measure.start();
314                 mode = MEASURE;
315         }
316         else if(key==Msp::Input::KEY_Z)
317         {
318                 manipulator.start_elevate();
319                 mode = MANIPULATE;
320         }
321         else if(key==Msp::Input::KEY_ESC)
322         {
323                 if(mode==MANIPULATE)
324                         manipulator.cancel();
325                 else if(mode==CATALOGUE)
326                         mode = SELECT;
327                 else
328                         selection.clear();
329         }
330         else if(key==Msp::Input::KEY_X)
331         {
332                 set<Track *> tracks = selection.get_tracks();
333                 selection.clear();
334                 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
335                 {
336                         overlay->clear(layout_3d->get_track(**i));
337                         layout->remove_track(**i);
338                         delete *i;
339                 }
340         }
341         else if(key==Msp::Input::KEY_F && (mod&Input::MOD_SHIFT))
342         {
343                 const set<Track *> &tracks = selection.get_tracks();
344                 const set<Track *> &ltracks = layout->get_tracks();
345                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
346                 {
347                         (*i)->set_flex(!(*i)->get_flex());
348                         (*i)->break_links();
349                         for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
350                                 if(*j!=*i)
351                                         (*i)->snap_to(**j, true);
352
353                         update_track_icon(layout_3d->get_track(**i));
354                 }
355         }
356         else if(key==Msp::Input::KEY_F)
357                 manipulator.flatten();
358         else if(key==Msp::Input::KEY_E && (mod&Input::MOD_SHIFT))
359                 manipulator.even_slope(true);
360         else if(key==Msp::Input::KEY_E)
361                 manipulator.even_slope();
362         else if(key==Msp::Input::KEY_T)
363                 set_turnout_id();
364         else if(key==Msp::Input::KEY_S)
365                 set_sensor_id();
366         else if(key==Msp::Input::KEY_A)
367                 add_selection_to_route();
368         else if(key==Msp::Input::KEY_C)
369                 manipulator.connect();
370 }
371
372 void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
373 {
374         y = window.get_height()-y-1;
375         mod = Input::mod_from_sys(mod);
376
377         Point ground = map_pointer_coords(x, y);
378
379         if(mode==CATALOGUE)
380         {
381                 if(btn==1)
382                 {
383                         Track3D *ctrack = pick_track(x, y);
384                         if(ctrack)
385                         {
386                                 Track *track = new Track(*layout, ctrack->get_track().get_type());
387                                 track->set_position(ground);
388
389                                 selection.clear();
390                                 selection.add_track(track);
391
392                                 mode = SELECT;
393                         }
394                 }
395                 else
396                         mode = SELECT;
397         }
398         else if(mode==SELECT)
399         {
400                 if(btn==1)
401                 {
402                         Track3D *track = pick_track(x, y);
403                         if(track)
404                         {
405                                 if(!(mod&Input::MOD_SHIFT))
406                                         selection.clear();
407                                 selection.toggle_track(&track->get_track());
408                         }
409                 }
410         }
411         else if(mode==MANIPULATE)
412                 manipulator.button_press(x, y, ground.x, ground.y, btn);
413         else if(mode==MEASURE)
414                 measure.button_press(x, y, ground.x, ground.y, btn);
415 }
416
417 void Designer::pointer_motion(int x, int y)
418 {
419         y = window.get_height()-y-1;
420
421         if(!root->get_child_at(x, y))
422         {
423                 Point ground = map_pointer_coords(x, y);
424                 manipulator.pointer_motion(x, y, ground.x, ground.y);
425                 measure.pointer_motion(x, y, ground.x, ground.y);
426         }
427 }
428
429 void Designer::apply_camera()
430 {
431         if(mode==CATALOGUE)
432         {
433                 GL::matrix_mode(GL::PROJECTION);
434                 GL::load_identity();
435                 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
436                 GL::matrix_mode(GL::MODELVIEW);
437                 GL::load_identity();
438                 GL::translate(0, 0, -1);
439         }
440         else
441                 camera.apply();
442 }
443
444 void Designer::render()
445 {
446         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
447
448         if(mode==CATALOGUE)
449         {
450                 apply_camera();
451                 cat_layout_3d->get_scene().render();
452         }
453         else
454         {
455                 pipeline->render_all();
456                 GL::enable(GL_CULL_FACE);
457                 {
458                         GL::Bind bind_blend(GL::Blend::alpha());
459                         overlay->render(0);
460                 }
461
462                 GL::Bind bind_depth(GL::DepthTest::lequal());
463                 if(mode==MEASURE)
464                         measure.render();
465         }
466
467         GL::matrix_mode(GL::PROJECTION);
468         GL::load_identity();
469         GL::ortho_bottomleft(window.get_width(), window.get_height());
470         GL::matrix_mode(GL::MODELVIEW);
471         GL::load_identity();
472
473         GL::disable(GL::DEPTH_TEST);
474
475         GL::Bind bind_blend(GL::Blend::alpha());
476         root->render();
477         GL::Texture::unbind();
478 }
479
480 void Designer::track_added(Track &trk)
481 {
482         new_tracks.push_back(&trk);
483 }
484
485 Track3D *Designer::pick_track(int x, int y)
486 {
487         Layout3D *l = layout_3d;
488         if(mode==CATALOGUE)
489                 l = cat_layout_3d;
490
491         float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
492         float yy = (float(y)/window.get_height()-0.5)*0.82843;
493         float size = 4.0/window.get_height()*0.82843;
494
495         apply_camera();
496
497         return l->pick_track(xx, yy, size);
498 }
499
500 void Designer::update_track_icon(Track3D &track)
501 {
502         overlay->clear(track);
503
504         if(track.get_track().get_flex())
505                 overlay->add_graphic(track, "flex");
506
507         if(unsigned sid = track.get_track().get_sensor_id())
508         {
509                 overlay->add_graphic(track, "sensor");
510                 overlay->set_label(track, lexical_cast(sid));
511         }
512         else if(unsigned tid = track.get_track().get_turnout_id())
513         {
514                 if(tid<0x800)
515                 {
516                         overlay->add_graphic(track, "turnout");
517                         overlay->set_label(track, lexical_cast(tid));
518                 }
519         }
520 }
521
522 void Designer::selection_changed()
523 {
524         const set<Track *> &tracks = selection.get_tracks();
525         if(tracks.empty())
526                 lbl_status->set_text(string());
527         else
528         {
529                 float len = 0;
530                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
531                         len += (*i)->get_type().get_total_length();
532                 lbl_status->set_text(format("%.2fm of track selected\n", len));
533         }
534 }
535
536 void Designer::manipulation_status(const string &status)
537 {
538         lbl_status->set_text(status);
539 }
540
541 void Designer::manipulation_done(bool)
542 {
543         mode = SELECT;
544         selection_changed();
545 }
546
547 void Designer::measure_changed()
548 {
549         float pard = measure.get_parallel_distance()*1000;
550         float perpd = measure.get_perpendicular_distance()*1000;
551         float d = sqrt(pard*pard+perpd*perpd);
552         float adiff = measure.get_angle_difference()*180/M_PI;
553         string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
554         lbl_status->set_text(info);
555 }
556
557 void Designer::measure_done()
558 {
559         mode = SELECT;
560         selection_changed();
561 }
562
563 void Designer::turnout_id_accept(const string &text)
564 {
565         Track *track = selection.get_track();
566         unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
567         track->set_turnout_id(id);
568
569         update_track_icon(layout_3d->get_track(*track));
570 }
571
572 void Designer::sensor_id_accept(const string &text)
573 {
574         const set<Track *> &tracks = selection.get_tracks();
575         unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
576         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
577         {
578                 (*i)->set_sensor_id(id);
579
580                 update_track_icon(layout_3d->get_track(**i));
581         }
582 }
583
584 void Designer::route_name_accept(const string &text)
585 {
586         if(cur_route)
587                 cur_route->set_name(text);
588 }
589
590 string Designer::tooltip(int x, int y)
591 {
592         if(Track3D *t3d = pick_track(x, y))
593         {
594                 const Track &track = t3d->get_track();
595                 const TrackType &ttype = track.get_type();
596                 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
597                 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
598                         info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
599                 if(track.get_turnout_id())
600                         info += format(" (turnout %d)", track.get_turnout_id());
601                 else if(track.get_sensor_id())
602                         info += format(" (sensor %d)", track.get_sensor_id());
603
604                 return info;
605         }
606
607         return string();
608 }
609
610 void Designer::show_route(const Route *route)
611 {
612         const set<Track *> &ltracks = layout->get_tracks();
613         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
614         {
615                 Track3D &t3d = layout_3d->get_track(**i);
616                 if(route && route->has_track(**i))
617                 {
618                         t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
619                         if((*i)->get_type().is_turnout())
620                         {
621                                 unsigned tid = (*i)->get_turnout_id();
622                                 int path = (tid ? route->get_turnout(tid) : -1);
623                                 if(path>=0)
624                                         t3d.get_path().set_path(path);
625                                 else
626                                         t3d.get_path().set_mask((*i)->get_type().get_paths());
627                         }
628                         else
629                                 t3d.get_path().set_path(0);
630                 }
631                 else
632                         t3d.get_path().set_mask(0);
633         }
634 }