]> git.tdb.fi Git - r2c2.git/blob - source/engineer/engineer.cpp
Convert engineer to use mspgbase instead of SDL
[r2c2.git] / source / engineer / engineer.cpp
1 #include <cmath>
2 #include <limits>
3 #include <GL/gl.h>
4 #include <msp/core/except.h>
5 #include <msp/core/getopt.h>
6 #include <msp/gbase/display.h>
7 #include <msp/gbase/window.h>
8 #include <msp/gl/immediate.h>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/projection.h>
11 #include <msp/gl/transform.h>
12 #include <msp/strings/formatter.h>
13 #include <msp/strings/lexicalcast.h>
14 #include <msp/strings/regex.h>
15 #include "engineer.h"
16 #include "mainpanel.h"
17 #include "trainpanel.h"
18 #include "trainproperties.h"
19
20 using namespace std;
21 using namespace Marklin;
22 using namespace Msp;
23
24 #include <iostream>
25
26 Engineer::Engineer(int argc, char **argv):
27         screen_w(1280),
28         screen_h(960),
29         fullscreen(false),
30         layout(catalogue),
31         layout_3d(layout),
32         no_lighting(false),
33         placing_train(0),
34         placing_block(0),
35         placing_entry(0),
36         simulate(false),
37         train_prop(0),
38         train_prop_stale(false)
39 {
40         string res;
41         bool   debug=false;
42         string device="/dev/ttyS0";
43         unsigned quality=4;
44
45         GetOpt getopt;
46         getopt.add_option('r', "resolution",  res,         GetOpt::REQUIRED_ARG);
47         getopt.add_option('f', "fullscreen",  fullscreen,  GetOpt::NO_ARG);
48         getopt.add_option('g', "debug",       debug,       GetOpt::NO_ARG);
49         getopt.add_option('d', "device",      device,      GetOpt::REQUIRED_ARG);
50         getopt.add_option('q', "quality",     quality,     GetOpt::REQUIRED_ARG);
51         getopt.add_option('s', "simulate",    simulate,    GetOpt::NO_ARG);
52         getopt.add_option(     "no-lighting", no_lighting, GetOpt::NO_ARG);
53         getopt(argc, argv);
54
55         if(!res.empty())
56         {
57                 if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res))
58                 {
59                         screen_w=lexical_cast<unsigned>(m[1].str);
60                         screen_h=lexical_cast<unsigned>(m[2].str);
61                 }
62                 else
63                         throw UsageError("Invalid resolution");
64         }
65
66         if(device!="none")
67                 control.open(device);
68
69         control.set_debug(debug);
70
71         layout_3d.set_quality(quality);
72
73         catalogue.load("tracks.dat");
74
75         const vector<string> &args=getopt.get_args();
76         if(args.empty())
77                 throw UsageError("No layout given");
78         layout.load(args.front());
79
80         trfc_mgr=new TrafficManager(control, layout);
81         trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
82
83         const map<unsigned, Sensor *> &sensors=control.get_sensors();
84         for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
85                 i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_event), i->second));
86
87         view_all();
88 }
89
90 Engineer::~Engineer()
91 {
92         delete trfc_mgr;
93 }
94
95 Train *Engineer::add_train(unsigned addr)
96 {
97         if(addr==0)
98         {
99                 train_prop=new TrainProperties(*this, ui_res, 0);
100                 root->add(*train_prop);
101                 train_prop->signal_ok.connect(sigc::mem_fun(this, &Engineer::dismiss_train_prop));
102                 train_prop_stale=false;
103                 train_prop->set_visible(true);
104
105                 return 0;
106         }
107         else
108         {
109                 Locomotive *loco=new Locomotive(control, addr);
110                 Train *train=new Train(*trfc_mgr, *loco);
111                 train->set_name(format("Train %d", trfc_mgr->get_trains().size()));
112
113                 TrainPanel *tpanel=new TrainPanel(*this, ui_res, *train);
114                 root->add(*tpanel);
115                 int y=main_panel->get_geometry().y;
116                 for(TrainPanelSeq::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
117                         y-=(*i)->get_geometry().h;
118                 tpanel->set_position(0, y-tpanel->get_geometry().h);
119                 train_panels.push_back(tpanel);
120                 tpanel->set_visible(true);
121
122                 placing_train=train;
123                 placing_block=0;
124                 main_panel->set_status_text("Select location");
125
126                 return train;
127         }
128 }
129
130 int Engineer::main()
131 {
132         dpy=new Graphics::Display;
133
134         Graphics::WindowOptions wopt;
135         wopt.width=screen_w;
136         wopt.height=screen_h;
137         wopt.fullscreen=fullscreen;
138         wnd=new Graphics::Window(*dpy, wopt);
139
140         Graphics::GLOptions glopt;
141         //glopt.multisample=4;
142         glc=new Graphics::GLContext(*wnd, glopt);
143
144         wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
145         wnd->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press));
146         wnd->signal_button_release.connect(sigc::mem_fun(this, &Engineer::button_release));
147         wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion));
148
149         glEnableClientState(GL_VERTEX_ARRAY);
150         glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
151         glEnable(GL_COLOR_MATERIAL);
152         glDepthFunc(GL_LEQUAL);
153         glEnable(GL_BLEND);
154         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
155
156         DataFile::load(ui_res, "engineer.res");
157         root=new GLtk::Root(ui_res, *wnd);
158         root->set_visible(true);
159
160         list<GL::Texture *> texs=ui_res.get_list<GL::Texture>();
161         for(list<GL::Texture *>::iterator i=texs.begin(); i!=texs.end(); ++i)
162         {
163                 (*i)->set_min_filter(GL::NEAREST);
164                 (*i)->set_mag_filter(GL::NEAREST);
165         }
166
167         main_panel=new MainPanel(*this, ui_res);
168         root->add(*main_panel);
169         main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
170         main_panel->set_visible(true);
171
172         wnd->show();
173
174         Application::main();
175
176         delete glc;
177         delete wnd;
178         delete dpy;
179
180         return exit_code;
181 }
182
183 void Engineer::tick()
184 {
185         dpy->tick();
186
187         control.tick();
188         trfc_mgr->tick();
189
190         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
191
192         project_3d();
193         glLoadIdentity();
194         glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
195         glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
196
197         if(!no_lighting)
198         {
199                 glEnable(GL_LIGHTING);
200                 glEnable(GL_LIGHT0);
201                 float params[4];
202                 params[0]=0;
203                 params[1]=-0.2;
204                 params[2]=1;
205                 params[3]=0;
206                 glLightfv(GL_LIGHT0, GL_POSITION, params);
207         }
208
209         //glEnable(GL_DEPTH_TEST);
210         glEnable(GL_MULTISAMPLE);
211
212         layout_3d.render();
213
214         glDisable(GL_LIGHTING);
215         glColor4f(1, 1, 1, 1);
216         const Track3DSeq &ltracks=layout_3d.get_tracks();
217         for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
218         {
219                 Track &track=(*i)->get_track();
220                 if(track.get_turnout_id())
221                 {
222                         Turnout &trnt=control.get_turnout(track.get_turnout_id());
223                         (*i)->render_route(trnt.get_route());
224                 }
225                 else
226                         (*i)->render_route(-1);
227         }
228
229         if(placing_train && placing_block)
230         {
231                 GL::push_matrix();
232
233                 const Marklin::Block::Endpoint &bep=placing_block->get_endpoints()[placing_entry];
234                 float rot=bep.track->get_endpoint_direction(bep.track_ep);
235                 Point pos=bep.track->get_endpoint_position(bep.track_ep);
236
237                 GL::translate(pos.x, pos.y, pos.z+0.03);
238                 GL::rotate(rot*180/M_PI+180, 0, 0, 1);
239                 GL::Texture::unbind();
240
241                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
242                 imm.color(1.0f, 1.0f, 1.0f);
243                 imm.begin(GL::TRIANGLE_FAN);
244                 imm.vertex(0.08,   0);
245                 imm.vertex(0.05,   0.03);
246                 imm.vertex(0.05,   0.01);
247                 imm.vertex(0,      0.01);
248                 imm.vertex(0,     -0.01);
249                 imm.vertex(0.05,  -0.01);
250                 imm.vertex(0.05,  -0.03);
251                 imm.end();
252
253                 GL::pop_matrix();
254         }
255
256         GL::matrix_mode(GL::PROJECTION);
257         GL::load_identity();
258         GL::ortho_bottomleft(screen_w, screen_h);
259         GL::matrix_mode(GL::MODELVIEW);
260         GL::load_identity();
261
262         glDisable(GL_DEPTH_TEST);
263         glDisable(GL_LIGHTING);
264         glDisable(GL_MULTISAMPLE);
265
266         root->render();
267
268         if(train_prop_stale)
269         {
270                 delete train_prop;
271                 train_prop=0;
272         }
273
274         glc->swap_buffers();
275 }
276
277 void Engineer::key_press(unsigned key, unsigned mod, wchar_t ch)
278 {
279         if(train_prop)
280                 train_prop->key_press(key, mod, ch);
281 }
282
283 void Engineer::button_press(int x, int y, unsigned btn, unsigned)
284 {
285         if(placing_train)
286         {
287                 if(btn==1 && placing_block)
288                 {
289                         set_block_color(*placing_block, Color(1, 1, 1));
290
291                         placing_train->place(placing_block, placing_entry);
292                         placing_train=0;
293                         main_panel->set_status_text(string());
294                 }
295                 else if(btn==3)
296                 {
297                         const vector<Block::Endpoint> &endpoints=placing_block->get_endpoints();
298                         placing_entry=(placing_entry+1)%endpoints.size();
299                 }
300         }
301         else
302         {
303                 Track3D *track=pick_track(x, screen_h-y-1);
304                 if(track)
305                 {
306                         if(unsigned tid=track->get_track().get_turnout_id())
307                         {
308                                 Turnout &turnout=control.get_turnout(tid);
309                                 turnout.set_route(1-turnout.get_route());
310                         }
311                         else if(simulate)
312                         {
313                                 if(unsigned sid=track->get_track().get_sensor_id())
314                                 {
315                                         Sensor &sensor=control.get_sensor(sid);
316                                         control.signal_sensor_event.emit(sid, !sensor.get_state());
317                                 }
318                         }
319                 }
320         }
321 }
322
323 void Engineer::button_release(int, int, unsigned, unsigned)
324 {
325 }
326
327 void Engineer::pointer_motion(int x, int y)
328 {
329         if(placing_train)
330         {
331                 Track3D *track=pick_track(x, screen_h-y-1);
332                 if(track && placing_train)
333                 {
334                         Block &block=trfc_mgr->get_block_by_track(track->get_track());
335                         if(&block!=placing_block)
336                         {
337                                 if(placing_block)
338                                         set_block_color(*placing_block, Color(1, 1, 1));
339                                 placing_block=&block;
340                                 placing_entry=0;
341                                 set_block_color(*placing_block, Color(0.5, 1, 0.7));
342                         }
343                 }
344                 else if(track && track->get_track().get_turnout_id())
345                         main_panel->set_status_text(format("Turnout %d", track->get_track().get_turnout_id()));
346                 else if(!placing_train)
347                         main_panel->set_status_text(string());
348         }
349 }
350
351 void Engineer::view_all()
352 {
353         const Track3DSeq &tracks=layout_3d.get_tracks();
354
355         cam_rot=0;
356         float best_height=-1;
357         float mid_x=0;
358         float mid_y=0;
359         for(float angle=0; angle<M_PI; angle+=0.01)
360         {
361                 float min_x=0;
362                 float max_x=0;
363                 float min_y=0;
364                 float max_y=0;
365                 for(Track3DSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
366                 {
367                         Point minp,maxp;
368                         (*i)->get_bounds(angle, minp, maxp);
369                         min_x=min(min_x, minp.x);
370                         max_x=max(max_x, maxp.x);
371                         min_y=min(min_y, minp.y);
372                         max_y=max(max_y, maxp.y);
373                 }
374
375                 float width=max_x-min_x;
376                 float height=max_y-min_y;
377                 height=max(height, width);
378
379                 if(height<best_height || best_height<0)
380                 {
381                         cam_rot=angle;
382                         best_height=height;
383                         mid_x=(min_x+max_x)/2;
384                         mid_y=(min_y+max_y)/2;
385                 }
386         }
387
388         float c=cos(cam_rot);
389         float s=sin(cam_rot);
390         cam_pos.x=c*mid_x-s*mid_y;
391         cam_pos.y=s*mid_x+c*mid_y;
392         cam_pos.z=max(best_height*1.05/0.82843, 0.15);
393 }
394
395 void Engineer::set_block_color(const Block &block, const Color &color)
396 {
397         const set<Track *> &tracks=block.get_tracks();
398         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
399                 layout_3d.get_track(*i)->set_color(color);
400 }
401
402 void Engineer::sensor_event(bool state, Sensor *sensor)
403 {
404         const list<Track3D *> &ltracks=layout_3d.get_tracks();
405         for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
406                 if((*i)->get_track().get_sensor_id()==sensor->get_address())
407                 {
408                         Block &block=trfc_mgr->get_block_by_track((*i)->get_track());
409                         if(state)
410                                 (*i)->set_color(Color(1, 0.5, 0.3));
411                         else if(block.get_train())
412                                 set_block_color(block, Color(1, 1, 0.3));
413                         else
414                                 (*i)->set_color(Color(1, 1, 1));
415                 }
416 }
417
418 void Engineer::block_reserved(const Block &block, const Train *train)
419 {
420         if(unsigned sid=block.get_sensor_id())
421         {
422                 Sensor &sensor=control.get_sensor(sid);
423                 if(sensor.get_state())
424                         return;
425         }
426
427         if(train)
428                 set_block_color(block, Color(1, 1, 0.3));
429         else
430                 set_block_color(block, Color(1, 1, 1));
431 }
432
433 void Engineer::project_3d()
434 {
435         glMatrixMode(GL_PROJECTION);
436         glLoadIdentity();
437         //glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
438         glFrustum(-0.069036, 0.041421, -0.041421, 0.041421, 0.1, 10);
439         glMatrixMode(GL_MODELVIEW);
440 }
441
442 Track3D *Engineer::pick_track(int x, int y)
443 {
444         float xx=((float)(x-(int)screen_w*5/8)/screen_h)*0.82843;
445         //float xx=((float)(x-(int)screen_w/2)/screen_h)*0.82843;
446         float yy=((float)y/screen_h-0.5)*0.82843;
447         float size=(float)4/screen_h*0.82843;
448
449         project_3d();
450         glLoadIdentity();
451         glRotatef(-cam_rot*180/M_PI, 0, 0, 1);
452         glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
453
454         return layout_3d.pick_track(xx, yy, size);
455 }
456
457 void Engineer::dismiss_train_prop()
458 {
459         train_prop_stale=true;
460 }
461
462 Application::RegApp<Engineer> Engineer::reg;