3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
12 #include <msp/gl/rendermode.h>
13 #include <msp/gl/select.h>
14 #include <msp/gl/texture2d.h>
15 #include <msp/input/keys.h>
16 #include <msp/strings/codec.h>
17 #include <msp/strings/lexicalcast.h>
18 #include <msp/strings/utf8.h>
19 #include <msp/strings/utils.h>
20 #include <msp/time/units.h>
21 #include <msp/time/utils.h>
22 #include "libmarklin/tracktype.h"
25 #include "manipulator.h"
27 #include "selection.h"
30 using namespace Marklin;
33 Designer::Designer(int argc, char **argv):
40 cam_pos(0, -0.5, 0.5),
48 catalogue.load("tracks.dat");
50 cat_layout=new Layout(catalogue);
51 cat_layout_3d=new Layout3D(*cat_layout);
53 const map<unsigned, TrackType *> &ctracks=catalogue.get_tracks();
55 for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
57 Track *track=new Track(*i->second);
58 track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
59 track->set_rotation(M_PI/2);
60 cat_layout->add_track(*track);
63 manipulator=new Manipulator(*this);
64 manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
65 manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
67 layout=new Layout(catalogue);
68 layout_3d=new Layout3D(*layout);
72 layout->load(argv[1]);
73 const list<Track3D *> <racks=layout_3d->get_tracks();
74 for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
76 if((*i)->get_track().get_sensor_id())
77 (*i)->set_color(GL::Color(1, 1, 0.5));
78 else if((*i)->get_track().get_turnout_id())
79 (*i)->set_color(GL::Color(0.5, 1, 1));
80 else if((*i)->get_track().get_flex())
81 (*i)->set_color(GL::Color(1, 0.5, 1));
85 selection=new Selection;
86 manipulator->set_selection(selection);
88 measure=new Measure(*this);
89 measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
90 measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
100 delete cat_layout_3d;
106 dpy=new Graphics::Display;
107 wnd=new Graphics::Window(*dpy, screen_w, screen_h);
108 glc=new Graphics::GLContext(*wnd);
110 wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
111 wnd->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
112 wnd->signal_key_release.connect(sigc::mem_fun(this, &Designer::key_release));
113 wnd->signal_button_press.connect(sigc::mem_fun(this, &Designer::button_press));
114 wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Designer::pointer_motion));
118 glEnableClientState(GL_VERTEX_ARRAY);
119 glEnable(GL_DEPTH_TEST);
121 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
122 glEnable(GL_CULL_FACE);
124 GL::Texture2D *font_tex=new GL::Texture2D;
125 font_tex->set_min_filter(GL::LINEAR);
126 font_tex->load_image("dejavu-20.png");
128 font->set_texture(*font_tex);
129 DataFile::load(*font, "dejavu-20.font");
146 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
148 float cos_pitch=cos(cam_pitch);
149 float sin_pitch=sin(cam_pitch);
150 float cos_yaw=cos(cam_yaw);
151 float sin_yaw=sin(cam_yaw);
153 float rx=sin_yaw*0.55228;
154 float ry=-cos_yaw*0.55228;
156 float ux=cos_yaw*-sin_pitch*0.41421;
157 float uy=sin_yaw*-sin_pitch*0.41421;
158 float uz=cos_pitch*0.41421;
160 float xf=static_cast<float>(x)*2/screen_w-1;
161 float yf=1-static_cast<float>(y)*2/screen_h;
163 float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
164 float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
165 float vz=sin_pitch + yf*uz;
167 gx=cam_pos.x-vx*cam_pos.z/vz;
168 gy=cam_pos.y-vy*cam_pos.z/vz;
171 void Designer::tick()
173 const Msp::Time::TimeStamp t=Msp::Time::now();
174 float dt=(t-last_tick)/Msp::Time::sec;
181 cam_pos.x+=cos(cam_yaw)*dt*move_y;
182 cam_pos.y+=sin(cam_yaw)*dt*move_y;
186 cam_pos.x+=sin(cam_yaw)*dt*move_x;
187 cam_pos.y+=-cos(cam_yaw)*dt*move_x;
191 cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
192 cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
193 cam_pos.z+=sin(cam_pitch)*dt*zoom;
197 float vx=cos(cam_yaw)*cos(cam_pitch);
198 float vy=sin(cam_yaw)*cos(cam_pitch);
199 float vz=sin(cam_pitch);
201 float gx=cam_pos.x-vx*cam_pos.z/vz;
202 float gy=cam_pos.y-vy*cam_pos.z/vz;
203 float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
205 cam_yaw+=M_PI*dt*rotate;
211 cam_pos.x=gx+cos(cam_yaw)*d;
212 cam_pos.y=gy+sin(cam_yaw)*d;
216 cam_pitch+=M_PI/2*dt*pitch;
217 if(cam_pitch>M_PI/12)
219 else if(cam_pitch<-M_PI/2)
223 if(tooltip_timeout && t>tooltip_timeout)
228 t3d=pick_track(pointer_x, pointer_y);
230 t3d=pick_track(pointer_x, pointer_y);
234 const Track &track=t3d->get_track();
235 const TrackType &ttype=track.get_type();
238 ss<<ttype.get_article_number()<<' '<<ttype.get_description();
240 ss<<" (slope "<<track.get_slope()/ttype.get_total_length()*100<<"%)";
241 if(track.get_turnout_id())
242 ss<<" (turnout "<<track.get_turnout_id()<<')';
243 else if(track.get_sensor_id())
244 ss<<" (sensor "<<track.get_sensor_id()<<')';
247 move_tooltip(pointer_x, pointer_y);
252 tooltip_timeout=Msp::Time::TimeStamp();
262 void Designer::key_press(unsigned code, unsigned mod, wchar_t ch)
264 unsigned key=Msp::Input::key_from_sys(code);
268 input->key_press(key, mod, ch);
272 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
275 if(key==Msp::Input::KEY_N)
277 else if(key==Msp::Input::KEY_G)
279 manipulator->start_move();
282 else if(key==Msp::Input::KEY_R)
284 manipulator->start_rotate();
287 else if(key==Msp::Input::KEY_D)
289 manipulator->duplicate();
290 manipulator->start_move();
293 else if(key==Msp::Input::KEY_W)
295 input=new ::Input(*this, "Filename");
296 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
297 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
300 else if(key==Msp::Input::KEY_PLUS)
301 selection->select_more();
302 else if(key==Msp::Input::KEY_L && (mod&1))
304 const set<Track *> &tracks=layout->get_tracks();
306 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
307 len+=(*i)->get_type().get_total_length();
308 cout<<"Total length: "<<len<<"m\n";
310 else if(key==Msp::Input::KEY_L)
311 selection->select_linked();
312 else if(key==Msp::Input::KEY_M)
317 else if(key==Msp::Input::KEY_Z)
319 manipulator->start_elevate();
322 else if(key==Msp::Input::KEY_ESC)
325 manipulator->cancel();
326 else if(mode==CATALOGUE)
331 else if(key==Msp::Input::KEY_X)
333 set<Track *> tracks=selection->get_tracks();
335 for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
337 layout->remove_track(**i);
341 else if(key==Msp::Input::KEY_F && (mod&1))
343 const set<Track *> &tracks=selection->get_tracks();
344 const set<Track *> <racks=layout->get_tracks();
345 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
347 (*i)->set_flex(!(*i)->get_flex());
349 for(set<Track *>::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
351 (*i)->snap_to(**j, true);
353 Track3D &t3d=layout_3d->get_track(**i);
355 t3d.set_color(GL::Color(1, 0.5, 1));
357 t3d.set_color(GL::Color(1, 1, 1));
360 else if(key==Msp::Input::KEY_F)
361 manipulator->flatten();
362 else if(key==Msp::Input::KEY_E && (mod&1))
363 manipulator->even_slope(true);
364 else if(key==Msp::Input::KEY_E)
365 manipulator->even_slope();
366 else if(key==Msp::Input::KEY_T)
368 Track *track=selection->get_track();
369 if(selection->size()==1 && track->get_type().get_n_routes()>1)
372 ss<<track->get_turnout_id();
373 input=new ::Input(*this, "Turnout ID", ss.str());
374 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
375 input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
379 else if(key==Msp::Input::KEY_S)
381 Track *track=selection->get_track();
382 if(selection->size()==1 && track->get_type().get_n_routes()==1)
385 ss<<track->get_sensor_id();
386 input=new ::Input(*this, "Sensor ID", ss.str());
387 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
388 input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
392 else if(key==Msp::Input::KEY_RIGHT)
394 else if(key==Msp::Input::KEY_LEFT)
396 else if(key==Msp::Input::KEY_UP)
398 else if(key==Msp::Input::KEY_DOWN)
400 else if(key==Msp::Input::KEY_INSERT)
402 else if(key==Msp::Input::KEY_PGUP)
404 else if(key==Msp::Input::KEY_HOME)
406 else if(key==Msp::Input::KEY_END)
408 else if(key==Msp::Input::KEY_DELETE)
410 else if(key==Msp::Input::KEY_PGDN)
414 void Designer::key_release(unsigned code, unsigned)
416 unsigned key=Msp::Input::key_from_sys(code);
421 if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
423 else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
425 else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
427 else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
429 else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
431 else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)
435 void Designer::button_press(int x, int y, unsigned btn, unsigned)
440 map_pointer_coords(x, y, gx, gy);
446 Track3D *ctrack=pick_track(x, y);
449 Track *track=ctrack->get_track().copy();
450 track->set_position(Point(gx, gy, 0));
451 layout->add_track(*track);
454 selection->add_track(track);
462 else if(mode==SELECT)
466 Track3D *track=pick_track(x, y);
471 selection->toggle_track(&track->get_track());
475 else if(mode==MANIPULATE)
476 manipulator->button_press(x, y, gx, gy, btn);
477 else if(mode==MEASURE)
478 measure->button_press(x, y, gx, gy, btn);
481 void Designer::pointer_motion(int x, int y)
486 map_pointer_coords(x, y, gx, gy);
488 if(mode==SELECT || mode==CATALOGUE)
492 tooltip_timeout=Msp::Time::now()+100*Msp::Time::msec;
497 manipulator->pointer_motion(x, y, gx, gy);
498 measure->pointer_motion(x, y, gx, gy);
501 if(mode==MEASURE || mode==MANIPULATE)
505 void Designer::project_3d()
507 glMatrixMode(GL_PROJECTION);
509 glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
510 glMatrixMode(GL_MODELVIEW);
513 void Designer::apply_camera()
517 glTranslatef(0, 0, -1);
520 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
521 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
522 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
526 void Designer::render()
528 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
529 glEnable(GL_DEPTH_TEST);
534 cat_layout_3d->render();
537 layout_3d->render(true);
538 manipulator->render();
543 glMatrixMode(GL_PROJECTION);
545 glOrtho(0, screen_w, 0, screen_h, 0, 1);
546 glMatrixMode(GL_MODELVIEW);
549 glDisable(GL_DEPTH_TEST);
553 glTranslatef(tooltip_x, tooltip_y, 0);
554 glScalef(20, 20, 20);
555 float width=font->get_string_width(tooltip);
556 glColor4f(0, 0, 0, 0.5);
559 glVertex2f(width, 0);
560 glVertex2f(width, 1);
563 glColor4f(1, 1, 1, 1);
564 font->draw_string(tooltip);
571 Track3D *Designer::pick_track(int x, int y)
573 Layout3D *l=layout_3d;
577 float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
578 float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
579 float size=4.0/screen_h*0.82843;
584 return l->pick_track(xx, yy, size);
587 void Designer::manipulation_status(const string &status)
592 void Designer::manipulation_done(bool)
597 void Designer::measure_changed()
599 float pard=measure->get_parallel_distance()*1000;
600 float perpd=measure->get_perpendicular_distance()*1000;
601 float d=sqrt(pard*pard+perpd*perpd);
602 float adiff=measure->get_angle_difference()*180/M_PI;
605 ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
609 void Designer::measure_done()
614 void Designer::move_tooltip(int x, int y)
616 int w=static_cast<int>(font->get_string_width(tooltip)*20);
617 tooltip_x=max(min(static_cast<int>(screen_w)-w, x), 0);
618 tooltip_y=max(min(static_cast<int>(screen_h)-20, y), 0);
621 void Designer::save_accept()
623 layout->save(input->get_text());
628 void Designer::turnout_id_accept()
630 Track *track=selection->get_track();
631 unsigned id=lexical_cast<unsigned>(input->get_text());
632 track->set_turnout_id(id);
634 Track3D &t3d=layout_3d->get_track(*track);
636 t3d.set_color(GL::Color(0.5, 1, 1));
638 t3d.set_color(GL::Color(1, 1, 1));
643 void Designer::sensor_id_accept()
645 Track *track=selection->get_track();
646 unsigned id=lexical_cast<unsigned>(input->get_text());
647 track->set_sensor_id(id);
649 Track3D &t3d=layout_3d->get_track(*track);
651 t3d.set_color(GL::Color(1, 1, 0.5));
653 t3d.set_color(GL::Color(1, 1, 1));
658 void Designer::input_dismiss()
665 Application::RegApp<Designer> Designer::reg;