]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
c071b5b4612d46d47d331d6545fd07da008e7483
[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, 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_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.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_coords(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==MANIPULATE)
417                 manipulator.button_press(x, y, ground.x, ground.y, btn);
418         else if(mode==MEASURE)
419                 measure.button_press(x, y, ground.x, ground.y, btn);
420 }
421
422 void Designer::pointer_motion(int x, int y)
423 {
424         y = window.get_height()-y-1;
425
426         if(!root.get_child_at(x, y))
427         {
428                 Point ground = map_pointer_coords(x, y);
429                 manipulator.pointer_motion(x, y, ground.x, ground.y);
430                 measure.pointer_motion(x, y, ground.x, ground.y);
431         }
432 }
433
434 void Designer::apply_camera()
435 {
436         if(mode==CATALOGUE)
437         {
438                 GL::matrix_mode(GL::PROJECTION);
439                 GL::load_identity();
440                 GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
441                 GL::matrix_mode(GL::MODELVIEW);
442                 GL::load_identity();
443                 GL::translate(0, 0, -1);
444         }
445         else
446                 camera.apply();
447 }
448
449 void Designer::render()
450 {
451         GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
452
453         if(mode==CATALOGUE)
454         {
455                 apply_camera();
456                 cat_layout_3d->get_scene().render();
457         }
458         else
459         {
460                 pipeline->render_all();
461                 GL::enable(GL_CULL_FACE);
462                 {
463                         GL::Bind bind_blend(GL::Blend::alpha());
464                         overlay->render(0);
465                 }
466
467                 GL::Bind bind_depth(GL::DepthTest::lequal());
468                 if(mode==MEASURE)
469                         measure.render();
470         }
471
472         GL::matrix_mode(GL::PROJECTION);
473         GL::load_identity();
474         GL::ortho_bottomleft(window.get_width(), window.get_height());
475         GL::matrix_mode(GL::MODELVIEW);
476         GL::load_identity();
477
478         GL::disable(GL::DEPTH_TEST);
479
480         GL::Bind bind_blend(GL::Blend::alpha());
481         root.render();
482         GL::Texture::unbind();
483 }
484
485 void Designer::track_added(Track &trk)
486 {
487         new_tracks.push_back(&trk);
488 }
489
490 void Designer::track_removed(Track &trk)
491 {
492         list<Track *>::iterator i = find(new_tracks.begin(), new_tracks.end(), &trk);
493         if(i!=new_tracks.end())
494                 new_tracks.erase(i);
495 }
496
497 Track3D *Designer::pick_track(int x, int y)
498 {
499         Layout3D *l = layout_3d;
500         if(mode==CATALOGUE)
501                 l = cat_layout_3d;
502
503         float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
504         float yy = (float(y)/window.get_height()-0.5)*0.82843;
505         float size = 4.0/window.get_height()*0.82843;
506
507         apply_camera();
508
509         return l->pick_track(xx, yy, size);
510 }
511
512 void Designer::update_track_icon(Track3D &track)
513 {
514         overlay->clear(track);
515
516         if(track.get_track().get_flex())
517                 overlay->add_graphic(track, "flex");
518
519         if(unsigned sid = track.get_track().get_sensor_id())
520         {
521                 overlay->add_graphic(track, "sensor");
522                 overlay->set_label(track, lexical_cast(sid));
523         }
524         else if(unsigned tid = track.get_track().get_turnout_id())
525         {
526                 if(tid<0x800)
527                 {
528                         overlay->add_graphic(track, "turnout");
529                         overlay->set_label(track, lexical_cast(tid));
530                 }
531         }
532 }
533
534 void Designer::selection_changed()
535 {
536         const set<Track *> &tracks = selection.get_tracks();
537         if(tracks.empty())
538                 lbl_status->set_text(string());
539         else
540         {
541                 float len = 0;
542                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
543                         len += (*i)->get_type().get_total_length();
544                 lbl_status->set_text(format("%.2fm of track selected\n", len));
545         }
546 }
547
548 void Designer::manipulation_status(const string &status)
549 {
550         lbl_status->set_text(status);
551 }
552
553 void Designer::manipulation_done(bool)
554 {
555         mode = SELECT;
556         selection_changed();
557 }
558
559 void Designer::measure_changed()
560 {
561         float pard = measure.get_parallel_distance()*1000;
562         float perpd = measure.get_perpendicular_distance()*1000;
563         float d = sqrt(pard*pard+perpd*perpd);
564         float adiff = measure.get_angle_difference()*180/M_PI;
565         string info = format("Par %.1fmm - Perp %.1fmm - Total %.1fmm - Angle %.1f°", pard, perpd, d, adiff);
566         lbl_status->set_text(info);
567 }
568
569 void Designer::measure_done()
570 {
571         mode = SELECT;
572         selection_changed();
573 }
574
575 void Designer::turnout_id_accept(const string &text)
576 {
577         Track *track = selection.get_track();
578         unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
579         track->set_turnout_id(id);
580
581         update_track_icon(layout_3d->get_track(*track));
582 }
583
584 void Designer::sensor_id_accept(const string &text)
585 {
586         const set<Track *> &tracks = selection.get_tracks();
587         unsigned id = (text.empty() ? 0 : lexical_cast<unsigned>(text));
588         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
589         {
590                 (*i)->set_sensor_id(id);
591
592                 update_track_icon(layout_3d->get_track(**i));
593         }
594 }
595
596 void Designer::route_name_accept(const string &text)
597 {
598         if(cur_route)
599                 cur_route->set_name(text);
600 }
601
602 string Designer::tooltip(int x, int y)
603 {
604         if(Track3D *t3d = pick_track(x, y))
605         {
606                 const Track &track = t3d->get_track();
607                 const TrackType &ttype = track.get_type();
608                 string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
609                 if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
610                         info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
611                 if(track.get_turnout_id())
612                         info += format(" (turnout %d)", track.get_turnout_id());
613                 else if(track.get_sensor_id())
614                         info += format(" (sensor %d)", track.get_sensor_id());
615
616                 return info;
617         }
618
619         return string();
620 }
621
622 void Designer::show_route(const Route *route)
623 {
624         const set<Track *> &ltracks = layout->get_tracks();
625         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
626         {
627                 Track3D &t3d = layout_3d->get_track(**i);
628                 if(route && route->has_track(**i))
629                 {
630                         t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
631                         if((*i)->get_type().is_turnout())
632                         {
633                                 unsigned tid = (*i)->get_turnout_id();
634                                 int path = (tid ? route->get_turnout(tid) : -1);
635                                 if(path>=0)
636                                         t3d.get_path().set_path(path);
637                                 else
638                                         t3d.get_path().set_mask((*i)->get_type().get_paths());
639                         }
640                         else
641                                 t3d.get_path().set_path(0);
642                 }
643                 else
644                         t3d.get_path().set_mask(0);
645         }
646 }