]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
User-visible
[flightgear.git] / src / Cockpit / panel.cxx
1 //  panel.cxx - default, 2D single-engine prop instrument panel
2 //
3 //  Written by David Megginson, started January 2000.
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License as
7 //  published by the Free Software Foundation; either version 2 of the
8 //  License, or (at your option) any later version.
9 // 
10 //  This program is distributed in the hope that it will be useful, but
11 //  WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //  General Public License for more details.
14 // 
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //
19 //  $Id$
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #ifdef HAVE_WINDOWS_H          
26 #  include <windows.h>
27 #endif
28
29 #include <string.h>
30
31 #include <plib/ssg.h>
32 #include <plib/fnt.h>
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/fgpath.hxx>
36 #include <Main/options.hxx>
37 #include <Main/views.hxx>
38 #include <Objects/texload.h>
39
40 #include "hud.hxx"
41 #include "panel.hxx"
42
43
44 \f
45 ////////////////////////////////////////////////////////////////////////
46 // Implementation of FGTextureManager.
47 ////////////////////////////////////////////////////////////////////////
48
49 map<const char *,ssgTexture *> FGTextureManager::_textureMap;
50
51 ssgTexture *
52 FGTextureManager::createTexture (const char * relativePath)
53 {
54   ssgTexture *texture;
55
56   texture = _textureMap[relativePath];
57   if (texture == 0) {
58     FGPath tpath(current_options.get_fg_root());
59     tpath.append(relativePath);
60     texture = new ssgTexture((char *)tpath.c_str(), false, false);
61     _textureMap[relativePath] = texture;
62     cerr << "Created texture " << relativePath
63          << " handle=" << texture->getHandle() << endl;
64   }
65
66   return texture;
67 }
68
69
70 \f
71 ////////////////////////////////////////////////////////////////////////
72 // Implementation of FGPanel.
73 ////////////////////////////////////////////////////////////////////////
74
75 FGPanel * current_panel = NULL;
76
77 FGPanel::FGPanel (int x, int y, int w, int h)
78   : _mouseDown(false),
79     _mouseInstrument(0),
80     _x(x), _y(y), _w(w), _h(h)
81 {
82   setVisibility(current_options.get_panel_status());
83   _panel_h = (int)(h * 0.5768 + 1);
84 }
85
86 FGPanel::~FGPanel ()
87 {
88   instrument_list_type::iterator current = _instruments.begin();
89   instrument_list_type::iterator last = _instruments.end();
90   
91   for ( ; current != last; ++current) {
92     delete *current;
93     *current = 0;
94   }
95 }
96
97 void
98 FGPanel::addInstrument (FGPanelInstrument * instrument)
99 {
100   _instruments.push_back(instrument);
101 }
102
103 void
104 FGPanel::update () const
105 {
106                                 // Do nothing if the panel isn't visible.
107   if (!_visibility)
108     return;
109
110                                 // If the mouse is down, do something
111   if (_mouseDown) {
112     _mouseDelay--;
113     if (_mouseDelay < 0) {
114       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
115       _mouseDelay = 2;
116     }
117   }
118
119                                 // Now, draw the panel
120   glMatrixMode(GL_PROJECTION);
121   glPushMatrix();
122   glLoadIdentity();
123   gluOrtho2D(_x, _x + _w, _y, _y + _h);
124
125   glMatrixMode(GL_MODELVIEW);
126   glPushMatrix();
127   glLoadIdentity();
128
129                                 // Draw the background
130   glEnable(GL_TEXTURE_2D);
131   glDisable(GL_LIGHTING);
132   glEnable(GL_BLEND);
133   glEnable(GL_ALPHA_TEST);
134   glEnable(GL_COLOR_MATERIAL);
135   glColor4f(1.0, 1.0, 1.0, 1.0);
136   glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
137   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
138   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
139   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
140   glBegin(GL_POLYGON);
141   glTexCoord2f(0.0, 0.0); glVertex3f(_x, _y, 0);
142   glTexCoord2f(10.0, 0.0); glVertex3f(_x + _w, _y, 0);
143   glTexCoord2f(10.0, 5.0); glVertex3f(_x + _w, _y + _panel_h, 0);
144   glTexCoord2f(0.0, 5.0); glVertex3f(_x, _y + _panel_h, 0);
145   glEnd();
146
147                                 // Draw the instruments.
148   instrument_list_type::const_iterator current = _instruments.begin();
149   instrument_list_type::const_iterator end = _instruments.end();
150
151   for ( ; current != end; current++) {
152     FGPanelInstrument * instr = *current;
153     glLoadIdentity();
154     glTranslated(instr->getXPos(), instr->getYPos(), 0);
155     instr->draw();
156   }
157
158   glMatrixMode(GL_PROJECTION);
159   glPopMatrix();
160   glMatrixMode(GL_MODELVIEW);
161   glPopMatrix();
162   ssgForceBasicState();
163   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
164 }
165
166 void
167 FGPanel::setVisibility (bool visibility)
168 {
169   _visibility = visibility;
170 }
171
172 bool
173 FGPanel::getVisibility () const
174 {
175   return _visibility;
176 }
177
178 void
179 FGPanel::setBackground (ssgTexture * texture)
180 {
181   _bg = texture;
182 }
183
184 bool
185 FGPanel::doMouseAction (int button, int updown, int x, int y)
186 {
187                                 // Note a released button and return
188   if (updown == 1) {
189     _mouseDown = false;
190     _mouseInstrument = 0;
191     return true;
192   }
193
194   x = (int)(((float)x / current_view.get_winWidth()) * _w);
195   y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
196
197   for (int i = 0; i < _instruments.size(); i++) {
198     FGPanelInstrument *inst = _instruments[i];
199     int ix = inst->getXPos();
200     int iy = inst->getYPos();
201     int iw = inst->getWidth() / 2;
202     int ih = inst->getHeight() / 2;
203     if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
204       cout << "Do mouse action for component " << i << '\n';
205       _mouseDown = true;
206       _mouseDelay = 20;
207       _mouseInstrument = inst;
208       _mouseButton = button;
209       _mouseX = x - ix;
210       _mouseY = y - iy;
211                                 // Always do the action once.
212       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
213       return true;
214     }
215   }
216   cout << "Did not click on an instrument\n";
217   return false;
218 }
219
220
221 \f
222 ////////////////////////////////////////////////////////////////////////
223 // Implementation of FGAdjustAction.
224 ////////////////////////////////////////////////////////////////////////
225
226 FGAdjustAction::FGAdjustAction (getter_type getter, setter_type setter,
227                                 double increment, double min, double max,
228                                 bool wrap=false)
229   : _getter(getter), _setter(setter), _increment(increment),
230     _min(min), _max(max), _wrap(wrap)
231 {
232 }
233
234 FGAdjustAction::~FGAdjustAction ()
235 {
236 }
237
238 void
239 FGAdjustAction::doAction ()
240 {
241   double value = (*_getter)();
242   cout << "Do action; value=" << value << '\n';
243   value += _increment;
244   if (value < _min) {
245     value = (_wrap ? _max : _min);
246   } else if (value > _max) {
247     value = (_wrap ? _min : _max);
248   }
249   cout << "New value is " << value << '\n';
250   (*_setter)(value);
251 }
252
253
254 \f
255 ////////////////////////////////////////////////////////////////////////
256 // Implementation of FGSwapAction.
257 ////////////////////////////////////////////////////////////////////////
258
259 FGSwapAction::FGSwapAction (getter_type getter1, setter_type setter1,
260                             getter_type getter2, setter_type setter2)
261   : _getter1(getter1), _setter1(setter1),
262     _getter2(getter2), _setter2(setter2)
263 {
264 }
265
266 FGSwapAction::~FGSwapAction ()
267 {
268 }
269
270 void
271 FGSwapAction::doAction ()
272 {
273   double value = (*_getter1)();
274   (*_setter1)((*_getter2)());
275   (*_setter2)(value);
276 }
277
278
279 \f
280 ////////////////////////////////////////////////////////////////////////
281 // Implementation of FGToggleAction.
282 ////////////////////////////////////////////////////////////////////////
283
284 FGToggleAction::FGToggleAction (getter_type getter, setter_type setter)
285   : _getter(getter), _setter(setter)
286 {
287 }
288
289 FGToggleAction::~FGToggleAction ()
290 {
291 }
292
293 void
294 FGToggleAction::doAction ()
295 {
296   (*_setter)(!((*_getter)()));
297 }
298
299
300 \f
301 ////////////////////////////////////////////////////////////////////////
302 // Implementation of FGPanelInstrument.
303 ////////////////////////////////////////////////////////////////////////
304
305
306 FGPanelInstrument::FGPanelInstrument ()
307 {
308   setPosition(0, 0);
309   setSize(0, 0);
310 }
311
312 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
313 {
314   setPosition(x, y);
315   setSize(w, h);
316 }
317
318 FGPanelInstrument::~FGPanelInstrument ()
319 {
320   action_list_type::iterator it = _actions.begin();
321   action_list_type::iterator last = _actions.end();
322   for ( ; it != last; it++) {
323     delete it->action;
324   }
325 }
326
327 void
328 FGPanelInstrument::setPosition (int x, int y)
329 {
330   _x = x;
331   _y = y;
332 }
333
334 void
335 FGPanelInstrument::setSize (int w, int h)
336 {
337   _w = w;
338   _h = h;
339 }
340
341 int
342 FGPanelInstrument::getXPos () const
343 {
344   return _x;
345 }
346
347 int
348 FGPanelInstrument::getYPos () const
349 {
350   return _y;
351 }
352
353 int
354 FGPanelInstrument::getWidth () const
355 {
356   return _w;
357 }
358
359 int
360 FGPanelInstrument::getHeight () const
361 {
362   return _h;
363 }
364
365 void
366 FGPanelInstrument::addAction (int button, int x, int y, int w, int h,
367                               FGPanelAction * action)
368 {
369   FGPanelInstrument::inst_action act;
370   act.button = button;
371   act.x = x;
372   act.y = y;
373   act.w = w;
374   act.h = h;
375   act.action = action;
376   _actions.push_back(act);
377 }
378
379                                 // Coordinates relative to centre.
380 bool
381 FGPanelInstrument::doMouseAction (int button, int x, int y)
382 {
383   action_list_type::iterator it = _actions.begin();
384   action_list_type::iterator last = _actions.end();
385   cout << "Mouse action at " << x << ',' << y << '\n';
386   for ( ; it != last; it++) {
387     cout << "Trying action at " << it->x << ',' << it->y << ','
388          << it->w <<',' << it->h << '\n';
389     if (button == it->button &&
390         x >= it->x && x < it->x + it->w && y >= it->y && y < it->y + it->h) {
391       it->action->doAction();
392       return true;
393     }
394   }
395   return false;
396 }
397
398
399 \f
400 ////////////////////////////////////////////////////////////////////////
401 // Implementation of FGLayeredInstrument.
402 ////////////////////////////////////////////////////////////////////////
403
404 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
405   : FGPanelInstrument(x, y, w, h)
406 {
407 }
408
409 FGLayeredInstrument::~FGLayeredInstrument ()
410 {
411   // FIXME: free layers
412 }
413
414 void
415 FGLayeredInstrument::draw () const
416 {
417   for (int i = 0; i < _layers.size(); i++) {
418     glPushMatrix();
419     glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
420     _layers[i]->draw();
421     glPopMatrix();
422   }
423 }
424
425 int
426 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
427 {
428   int n = _layers.size();
429   _layers.push_back(layer);
430   return n;
431 }
432
433 int
434 FGLayeredInstrument::addLayer (ssgTexture * texture)
435 {
436   return addLayer(new FGTexturedLayer(texture, _w, _h));
437 }
438
439 void
440 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
441                                         FGInstrumentLayer::transform_func func,
442                                         double min, double max,
443                                         double factor, double offset)
444 {
445   int layer = _layers.size() - 1;
446   _layers[layer]->addTransformation(type, func, min, max, factor, offset);
447 }
448
449 void
450 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
451                                         double offset)
452 {
453   addTransformation(type, 0, 0.0, 0.0, 1.0, offset);
454 }
455
456
457 \f
458 ////////////////////////////////////////////////////////////////////////
459 // Implementation of FGInstrumentLayer.
460 ////////////////////////////////////////////////////////////////////////
461
462 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
463   : _w(w),
464     _h(h)
465 {
466 }
467
468 FGInstrumentLayer::~FGInstrumentLayer ()
469 {
470   transformation_list::iterator it = _transformations.begin();
471   transformation_list::iterator end = _transformations.end();
472   while (it != end) {
473     delete *it;
474     it++;
475   }
476 }
477
478 void
479 FGInstrumentLayer::transform () const
480 {
481   transformation_list::const_iterator it = _transformations.begin();
482   transformation_list::const_iterator last = _transformations.end();
483   while (it != last) {
484     transformation *t = *it;
485     double value = (t->func == 0 ? 0.0 : (*(t->func))());
486     if (value < t->min) {
487       value = t->min;
488     } else if (value > t->max) {
489       value = t->max;
490     }
491     value = value * t->factor + t->offset;
492
493     switch (t->type) {
494     case XSHIFT:
495       glTranslatef(value, 0.0, 0.0);
496       break;
497     case YSHIFT:
498       glTranslatef(0.0, value, 0.0);
499       break;
500     case ROTATION:
501       glRotatef(-value, 0.0, 0.0, 1.0);
502       break;
503     }
504     it++;
505   }
506 }
507
508 void
509 FGInstrumentLayer::addTransformation (transform_type type,
510                                       transform_func func,
511                                       double min, double max,
512                                       double factor, double offset)
513 {
514   transformation *t = new transformation;
515   t->type = type;
516   t->func = func;
517   t->min = min;
518   t->max = max;
519   t->factor = factor;
520   t->offset = offset;
521   _transformations.push_back(t);
522 }
523
524
525 \f
526 ////////////////////////////////////////////////////////////////////////
527 // Implementation of FGTexturedLayer.
528 ////////////////////////////////////////////////////////////////////////
529
530 FGTexturedLayer::FGTexturedLayer (ssgTexture * texture, int w, int h,
531                                   double texX1 = 0.0, double texY1 = 0.0,
532                                   double texX2 = 1.0, double texY2 = 1.0)
533   : FGInstrumentLayer(w, h),
534     _texX1(texX1), _texY1(texY1), _texX2(texX2), _texY2(texY2)
535 {
536   setTexture(texture);
537 }
538
539 FGTexturedLayer::~FGTexturedLayer ()
540 {
541 }
542
543 void
544 FGTexturedLayer::draw () const
545 {
546   int w2 = _w / 2;
547   int h2 = _h / 2;
548
549   transform();
550   glBindTexture(GL_TEXTURE_2D, _texture->getHandle());
551   glBegin(GL_POLYGON);
552
553   glTexCoord2f(_texX1, _texY1); glVertex2f(-w2, -h2);
554   glTexCoord2f(_texX2, _texY1); glVertex2f(w2, -h2);
555   glTexCoord2f(_texX2, _texY2); glVertex2f(w2, h2);
556   glTexCoord2f(_texX1, _texY2); glVertex2f(-w2, h2);
557   glEnd();
558 }
559
560
561 \f
562 ////////////////////////////////////////////////////////////////////////
563 // Implementation of FGTextLayer.
564 ////////////////////////////////////////////////////////////////////////
565
566 FGTextLayer::FGTextLayer (int w, int h)
567   : FGInstrumentLayer(w, h)
568 {
569   _color[0] = _color[1] = _color[2] = 0.0;
570   _color[3] = 1.0;
571 }
572
573 FGTextLayer::~FGTextLayer ()
574 {
575   chunk_list::iterator it = _chunks.begin();
576   chunk_list::iterator last = _chunks.end();
577   for ( ; it != last; it++) {
578     delete *it;
579   }
580 }
581
582 void
583 FGTextLayer::draw () const
584 {
585   glPushMatrix();
586   glColor4fv(_color);
587   transform();
588   _renderer.setFont(guiFntHandle);
589   _renderer.setPointSize(14);
590   _renderer.begin();
591   _renderer.start3f(0, 0, 0);
592
593                                 // Render each of the chunks.
594   chunk_list::const_iterator it = _chunks.begin();
595   chunk_list::const_iterator last = _chunks.end();
596   for ( ; it != last; it++) {
597     _renderer.puts((*it)->getValue());
598   }
599
600   _renderer.end();
601   glColor4f(1.0, 1.0, 1.0, 1.0);        // FIXME
602   glPopMatrix();
603 }
604
605 void
606 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
607 {
608   _chunks.push_back(chunk);
609 }
610
611 void
612 FGTextLayer::setColor (float r, float g, float b)
613 {
614   _color[0] = r;
615   _color[1] = g;
616   _color[2] = b;
617   _color[3] = 1.0;
618 }
619
620 void
621 FGTextLayer::setPointSize (const float size)
622 {
623   _renderer.setPointSize(size);
624 }
625
626 void
627 FGTextLayer::setFont(fntFont * font)
628 {
629   _renderer.setFont(font);
630 }
631
632
633 \f
634 ////////////////////////////////////////////////////////////////////////
635 // Implementation of FGTextLayer::Chunk.
636 ////////////////////////////////////////////////////////////////////////
637
638 FGTextLayer::Chunk::Chunk (char * text, char * fmt = "%s")
639   : _type(FGTextLayer::TEXT), _fmt(fmt)
640 {
641   _value._text = text;
642 }
643
644 FGTextLayer::Chunk::Chunk (text_func func, char * fmt = "%s")
645   : _type(FGTextLayer::TEXT_FUNC), _fmt(fmt)
646 {
647   _value._tfunc = func;
648 }
649
650 FGTextLayer::Chunk::Chunk (double_func func, char * fmt = "%.2f",
651                            double mult = 1.0)
652   : _type(FGTextLayer::DOUBLE_FUNC), _fmt(fmt), _mult(mult)
653 {
654   _value._dfunc = func;
655 }
656
657 char *
658 FGTextLayer::Chunk::getValue () const
659 {
660   switch (_type) {
661   case TEXT:
662     sprintf(_buf, _fmt, _value._text);
663     return _buf;
664   case TEXT_FUNC:
665     sprintf(_buf, _fmt, (*(_value._tfunc))());
666     break;
667   case DOUBLE_FUNC:
668     sprintf(_buf, _fmt, (*(_value._dfunc))() * _mult);
669     break;
670   }
671   return _buf;
672 }
673
674
675 \f
676 ////////////////////////////////////////////////////////////////////////
677 // Implementation of FGSwitchLayer.
678 ////////////////////////////////////////////////////////////////////////
679
680 FGSwitchLayer::FGSwitchLayer (int w, int h, switch_func func,
681                               FGInstrumentLayer * layer1,
682                               FGInstrumentLayer * layer2)
683   : FGInstrumentLayer(w, h), _func(func), _layer1(layer1), _layer2(layer2)
684 {
685 }
686
687 FGSwitchLayer::~FGSwitchLayer ()
688 {
689   delete _layer1;
690   delete _layer2;
691 }
692
693 void
694 FGSwitchLayer::draw () const
695 {
696   transform();
697   if ((*_func)()) {
698     _layer1->draw();
699   } else {
700     _layer2->draw();
701   }
702 }
703
704 \f
705 // end of panel.cxx