]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Sep. 22, 2000 panel updates from David Megginson to make the new config file
[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<string,ssgTexture *> FGTextureManager::_textureMap;
50
51 ssgTexture *
52 FGTextureManager::createTexture (const string &relativePath)
53 {
54   ssgTexture * texture = _textureMap[relativePath];
55   if (texture == 0) {
56     cerr << "Texture " << relativePath << " does not yet exist" << endl;
57     FGPath tpath(current_options.get_fg_root());
58     tpath.append(relativePath);
59     texture = new ssgTexture((char *)tpath.c_str(), false, false);
60     _textureMap[relativePath] = texture;
61     if (_textureMap[relativePath] == 0) 
62       cerr << "Texture *still* doesn't exist" << endl;
63     cerr << "Created texture " << relativePath
64          << " handle=" << texture->getHandle() << endl;
65   }
66
67   return texture;
68 }
69
70
71
72 \f
73 ////////////////////////////////////////////////////////////////////////
74 // Implementation of FGCropped Texture.
75 ////////////////////////////////////////////////////////////////////////
76
77
78 FGCroppedTexture::FGCroppedTexture ()
79   : _path(""), _texture(0),
80     _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
81 {
82 }
83
84
85 FGCroppedTexture::FGCroppedTexture (const string &path,
86                                     float minX, float minY,
87                                     float maxX, float maxY)
88   : _path(path), _texture(0),
89     _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
90 {
91 }
92
93
94 FGCroppedTexture::~FGCroppedTexture ()
95 {
96 }
97
98
99 ssgTexture *
100 FGCroppedTexture::getTexture ()
101 {
102   if (_texture == 0) {
103     _texture = FGTextureManager::createTexture(_path);
104   }
105   return _texture;
106 }
107
108
109 \f
110 ////////////////////////////////////////////////////////////////////////
111 // Implementation of FGPanel.
112 ////////////////////////////////////////////////////////////////////////
113
114 FGPanel * current_panel = NULL;
115
116 FGPanel::FGPanel (int x, int y, int w, int h)
117   : _mouseDown(false),
118     _mouseInstrument(0),
119     _x(x), _y(y), _w(w), _h(h)
120 {
121   setVisibility(current_options.get_panel_status());
122   _panel_h = (int)(h * 0.5768 + 1);
123 }
124
125 FGPanel::~FGPanel ()
126 {
127   for (instrument_list_type::iterator it = _instruments.begin();
128        it != _instruments.end();
129        it++) {
130     delete *it;
131     *it = 0;
132   }
133 }
134
135 void
136 FGPanel::addInstrument (FGPanelInstrument * instrument)
137 {
138   _instruments.push_back(instrument);
139 }
140
141 void
142 FGPanel::update () const
143 {
144                                 // Do nothing if the panel isn't visible.
145   if (!_visibility)
146     return;
147
148                                 // If the mouse is down, do something
149   if (_mouseDown) {
150     _mouseDelay--;
151     if (_mouseDelay < 0) {
152       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
153       _mouseDelay = 2;
154     }
155   }
156
157                                 // Now, draw the panel
158   glMatrixMode(GL_PROJECTION);
159   glPushMatrix();
160   glLoadIdentity();
161   gluOrtho2D(_x, _x + _w, _y, _y + _h);
162
163   glMatrixMode(GL_MODELVIEW);
164   glPushMatrix();
165   glLoadIdentity();
166
167                                 // Draw the background
168   glEnable(GL_TEXTURE_2D);
169   glDisable(GL_LIGHTING);
170   glEnable(GL_BLEND);
171   glEnable(GL_ALPHA_TEST);
172   glEnable(GL_COLOR_MATERIAL);
173   // glColor4f(1.0, 1.0, 1.0, 1.0);
174   if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
175       glColor4fv( cur_light_params.scene_diffuse );
176   } else {
177       glColor4f(0.7, 0.2, 0.2, 1.0);
178   }
179   glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
180   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
181   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
182   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
183   glBegin(GL_POLYGON);
184   glTexCoord2f(0.0, 0.0); glVertex3f(_x, _y, 0);
185   glTexCoord2f(10.0, 0.0); glVertex3f(_x + _w, _y, 0);
186   glTexCoord2f(10.0, 5.0); glVertex3f(_x + _w, _y + _panel_h, 0);
187   glTexCoord2f(0.0, 5.0); glVertex3f(_x, _y + _panel_h, 0);
188   glEnd();
189
190                                 // Draw the instruments.
191   instrument_list_type::const_iterator current = _instruments.begin();
192   instrument_list_type::const_iterator end = _instruments.end();
193
194   for ( ; current != end; current++) {
195     FGPanelInstrument * instr = *current;
196     glLoadIdentity();
197     glTranslated(instr->getXPos(), instr->getYPos(), 0);
198     instr->draw();
199   }
200
201   glMatrixMode(GL_PROJECTION);
202   glPopMatrix();
203   glMatrixMode(GL_MODELVIEW);
204   glPopMatrix();
205   ssgForceBasicState();
206   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
207 }
208
209 void
210 FGPanel::setVisibility (bool visibility)
211 {
212   _visibility = visibility;
213 }
214
215 bool
216 FGPanel::getVisibility () const
217 {
218   return _visibility;
219 }
220
221 void
222 FGPanel::setBackground (ssgTexture * texture)
223 {
224   _bg = texture;
225 }
226
227 bool
228 FGPanel::doMouseAction (int button, int updown, int x, int y)
229 {
230                                 // Note a released button and return
231   // cerr << "Doing mouse action\n";
232   if (updown == 1) {
233     _mouseDown = false;
234     _mouseInstrument = 0;
235     return true;
236   }
237
238   x = (int)(((float)x / current_view.get_winWidth()) * _w);
239   y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
240
241   for (int i = 0; i < (int)_instruments.size(); i++) {
242     FGPanelInstrument *inst = _instruments[i];
243     int ix = inst->getXPos();
244     int iy = inst->getYPos();
245     int iw = inst->getWidth() / 2;
246     int ih = inst->getHeight() / 2;
247     if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
248       _mouseDown = true;
249       _mouseDelay = 20;
250       _mouseInstrument = inst;
251       _mouseButton = button;
252       _mouseX = x - ix;
253       _mouseY = y - iy;
254                                 // Always do the action once.
255       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
256       return true;
257     }
258   }
259   return false;
260 }
261
262
263 \f
264 ////////////////////////////////////////////////////////////////////////.
265 // Implementation of FGPanelAction.
266 ////////////////////////////////////////////////////////////////////////
267
268 FGPanelAction::FGPanelAction ()
269 {
270 }
271
272 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
273   : _button(button), _x(x), _y(y), _w(w), _h(h)
274 {
275 }
276
277 FGPanelAction::~FGPanelAction ()
278 {
279 }
280
281
282 \f
283 ////////////////////////////////////////////////////////////////////////
284 // Implementation of FGAdjustAction.
285 ////////////////////////////////////////////////////////////////////////
286
287 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
288                                 SGValue * value, float increment, 
289                                 float min, float max, bool wrap)
290   : FGPanelAction(button, x, y, w, h),
291     _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
292 {
293 }
294
295 FGAdjustAction::~FGAdjustAction ()
296 {
297 }
298
299 void
300 FGAdjustAction::doAction ()
301 {
302   float val = _value->getFloatValue();
303   val += _increment;
304   if (val < _min) {
305     val = (_wrap ? _max : _min);
306   } else if (val > _max) {
307     val = (_wrap ? _min : _max);
308   }
309   _value->setDoubleValue(val);
310 }
311
312
313 \f
314 ////////////////////////////////////////////////////////////////////////
315 // Implementation of FGSwapAction.
316 ////////////////////////////////////////////////////////////////////////
317
318 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
319                             SGValue * value1, SGValue * value2)
320   : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
321 {
322 }
323
324 FGSwapAction::~FGSwapAction ()
325 {
326 }
327
328 void
329 FGSwapAction::doAction ()
330 {
331   float val = _value1->getFloatValue();
332   _value1->setDoubleValue(_value2->getFloatValue());
333   _value2->setDoubleValue(val);
334 }
335
336
337 \f
338 ////////////////////////////////////////////////////////////////////////
339 // Implementation of FGToggleAction.
340 ////////////////////////////////////////////////////////////////////////
341
342 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
343                                 SGValue * value)
344   : FGPanelAction(button, x, y, w, h), _value(value)
345 {
346 }
347
348 FGToggleAction::~FGToggleAction ()
349 {
350 }
351
352 void
353 FGToggleAction::doAction ()
354 {
355   _value->setBoolValue(!(_value->getBoolValue()));
356 }
357
358
359 \f
360 ////////////////////////////////////////////////////////////////////////
361 // Implementation of FGPanelTransformation.
362 ////////////////////////////////////////////////////////////////////////
363
364 FGPanelTransformation::FGPanelTransformation ()
365 {
366 }
367
368 FGPanelTransformation::FGPanelTransformation (Type _type,
369                                               const SGValue * _value,
370                                               float _min, float _max,
371                                               float _factor, float _offset)
372   : type(_type), value(_value), min(_min), max(_max),
373     factor(_factor), offset(_offset)
374 {
375 }
376
377 FGPanelTransformation::~FGPanelTransformation ()
378 {
379 }
380
381
382 \f
383 ////////////////////////////////////////////////////////////////////////
384 // Implementation of FGPanelInstrument.
385 ////////////////////////////////////////////////////////////////////////
386
387
388 FGPanelInstrument::FGPanelInstrument ()
389 {
390   setPosition(0, 0);
391   setSize(0, 0);
392 }
393
394 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
395 {
396   setPosition(x, y);
397   setSize(w, h);
398 }
399
400 FGPanelInstrument::~FGPanelInstrument ()
401 {
402   for (action_list_type::iterator it = _actions.begin();
403        it != _actions.end();
404        it++) {
405     delete *it;
406     *it = 0;
407   }
408 }
409
410 void
411 FGPanelInstrument::setPosition (int x, int y)
412 {
413   _x = x;
414   _y = y;
415 }
416
417 void
418 FGPanelInstrument::setSize (int w, int h)
419 {
420   _w = w;
421   _h = h;
422 }
423
424 int
425 FGPanelInstrument::getXPos () const
426 {
427   return _x;
428 }
429
430 int
431 FGPanelInstrument::getYPos () const
432 {
433   return _y;
434 }
435
436 int
437 FGPanelInstrument::getWidth () const
438 {
439   return _w;
440 }
441
442 int
443 FGPanelInstrument::getHeight () const
444 {
445   return _h;
446 }
447
448 void
449 FGPanelInstrument::addAction (FGPanelAction * action)
450 {
451   _actions.push_back(action);
452 }
453
454                                 // Coordinates relative to centre.
455 bool
456 FGPanelInstrument::doMouseAction (int button, int x, int y)
457 {
458   action_list_type::iterator it = _actions.begin();
459   action_list_type::iterator last = _actions.end();
460   for ( ; it != last; it++) {
461     if ((*it)->inArea(button, x, y)) {
462       (*it)->doAction();
463       return true;
464     }
465   }
466   return false;
467 }
468
469
470 \f
471 ////////////////////////////////////////////////////////////////////////
472 // Implementation of FGLayeredInstrument.
473 ////////////////////////////////////////////////////////////////////////
474
475 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
476   : FGPanelInstrument(x, y, w, h)
477 {
478 }
479
480 FGLayeredInstrument::~FGLayeredInstrument ()
481 {
482   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
483     delete *it;
484     *it = 0;
485   }
486 }
487
488 void
489 FGLayeredInstrument::draw ()
490 {
491   for (int i = 0; i < (int)_layers.size(); i++) {
492     glPushMatrix();
493     glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
494     _layers[i]->draw();
495     glPopMatrix();
496   }
497 }
498
499 int
500 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
501 {
502   int n = _layers.size();
503   if (layer->getWidth() == -1) {
504     layer->setWidth(getWidth());
505   }
506   if (layer->getHeight() == -1) {
507     layer->setHeight(getHeight());
508   }
509   _layers.push_back(layer);
510   return n;
511 }
512
513 int
514 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
515                                int w, int h)
516 {
517   return addLayer(new FGTexturedLayer(texture, w, h));
518 }
519
520 void
521 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
522 {
523   int layer = _layers.size() - 1;
524   _layers[layer]->addTransformation(transformation);
525 }
526
527
528 \f
529 ////////////////////////////////////////////////////////////////////////
530 // Implementation of FGInstrumentLayer.
531 ////////////////////////////////////////////////////////////////////////
532
533 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
534   : _w(w),
535     _h(h)
536 {
537 }
538
539 FGInstrumentLayer::~FGInstrumentLayer ()
540 {
541   for (transformation_list::iterator it = _transformations.begin();
542        it != _transformations.end();
543        it++) {
544     delete *it;
545     *it = 0;
546   }
547 }
548
549 void
550 FGInstrumentLayer::transform () const
551 {
552   transformation_list::const_iterator it = _transformations.begin();
553   transformation_list::const_iterator last = _transformations.end();
554   while (it != last) {
555     FGPanelTransformation *t = *it;
556     float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
557     if (val < t->min) {
558       val = t->min;
559     } else if (val > t->max) {
560       val = t->max;
561     }
562     val = val * t->factor + t->offset;
563
564     switch (t->type) {
565     case FGPanelTransformation::XSHIFT:
566       glTranslatef(val, 0.0, 0.0);
567       break;
568     case FGPanelTransformation::YSHIFT:
569       glTranslatef(0.0, val, 0.0);
570       break;
571     case FGPanelTransformation::ROTATION:
572       glRotatef(-val, 0.0, 0.0, 1.0);
573       break;
574     }
575     it++;
576   }
577 }
578
579 void
580 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
581 {
582   _transformations.push_back(transformation);
583 }
584
585
586 \f
587 ////////////////////////////////////////////////////////////////////////
588 // Implementation of FGTexturedLayer.
589 ////////////////////////////////////////////////////////////////////////
590
591
592 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
593   : FGInstrumentLayer(w, h)
594 {
595   setTexture(texture);
596 }
597
598
599 FGTexturedLayer::~FGTexturedLayer ()
600 {
601 }
602
603
604 void
605 FGTexturedLayer::draw ()
606 {
607   int w2 = _w / 2;
608   int h2 = _h / 2;
609
610   transform();
611   glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
612   glBegin(GL_POLYGON);
613
614                                 // From Curt: turn on the panel
615                                 // lights after sundown.
616   if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
617       glColor4fv( cur_light_params.scene_diffuse );
618   } else {
619       glColor4f(0.7, 0.2, 0.2, 1.0);
620   }
621
622
623   glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
624   glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
625   glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
626   glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
627   glEnd();
628 }
629
630
631 \f
632 ////////////////////////////////////////////////////////////////////////
633 // Implementation of FGWindowLayer.
634 ////////////////////////////////////////////////////////////////////////
635
636 FGWindowLayer::FGWindowLayer (int w, int h)
637   : FGTexturedLayer (w, h)
638 {
639 }
640
641 FGWindowLayer::FGWindowLayer (const FGCroppedTexture &texture, int w, int h)
642   : FGTexturedLayer(texture, w, h)
643 {
644 }
645
646 FGWindowLayer::~FGWindowLayer ()
647 {
648 }
649
650 void
651 FGWindowLayer::draw ()
652 {
653   // doesn't do anything yet
654   FGTexturedLayer::draw();
655 }
656
657
658 \f
659 ////////////////////////////////////////////////////////////////////////
660 // Implementation of FGTextLayer.
661 ////////////////////////////////////////////////////////////////////////
662
663 FGTextLayer::FGTextLayer (int w, int h, Chunk * chunk1, Chunk * chunk2,
664                           Chunk * chunk3)
665   : FGInstrumentLayer(w, h), _pointSize(14.0)
666 {
667   _color[0] = _color[1] = _color[2] = 0.0;
668   _color[3] = 1.0;
669   if (chunk1)
670     addChunk(chunk1);
671   if (chunk2)
672     addChunk(chunk2);
673   if (chunk3)
674     addChunk(chunk3);
675 }
676
677 FGTextLayer::~FGTextLayer ()
678 {
679   chunk_list::iterator it = _chunks.begin();
680   chunk_list::iterator last = _chunks.end();
681   for ( ; it != last; it++) {
682     delete *it;
683   }
684 }
685
686 void
687 FGTextLayer::draw ()
688 {
689   glPushMatrix();
690   glColor4fv(_color);
691   transform();
692   _renderer.setFont(guiFntHandle);
693   _renderer.setPointSize(_pointSize);
694   _renderer.begin();
695   _renderer.start3f(0, 0, 0);
696
697                                 // Render each of the chunks.
698   chunk_list::const_iterator it = _chunks.begin();
699   chunk_list::const_iterator last = _chunks.end();
700   for ( ; it != last; it++) {
701     _renderer.puts((char *)((*it)->getValue()));
702   }
703
704   _renderer.end();
705   glColor4f(1.0, 1.0, 1.0, 1.0);        // FIXME
706   glPopMatrix();
707 }
708
709 void
710 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
711 {
712   _chunks.push_back(chunk);
713 }
714
715 void
716 FGTextLayer::setColor (float r, float g, float b)
717 {
718   _color[0] = r;
719   _color[1] = g;
720   _color[2] = b;
721   _color[3] = 1.0;
722 }
723
724 void
725 FGTextLayer::setPointSize (float size)
726 {
727   _pointSize = size;
728 }
729
730 void
731 FGTextLayer::setFont(fntFont * font)
732 {
733   _renderer.setFont(font);
734 }
735
736
737 \f
738 ////////////////////////////////////////////////////////////////////////
739 // Implementation of FGTextLayer::Chunk.
740 ////////////////////////////////////////////////////////////////////////
741
742 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
743   : _type(FGTextLayer::TEXT), _fmt(fmt)
744 {
745   _text = text;
746   if (_fmt == "") 
747     _fmt = "%s";
748 }
749
750 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
751                            const string &fmt, float mult)
752   : _type(type), _fmt(fmt), _mult(mult)
753 {
754   if (_fmt == "") {
755     if (type == TEXT_VALUE)
756       _fmt = "%s";
757     else
758       _fmt = "%.2f";
759   }
760   _value = value;
761 }
762
763 const char *
764 FGTextLayer::Chunk::getValue () const
765 {
766   switch (_type) {
767   case TEXT:
768     sprintf(_buf, _fmt.c_str(), _text.c_str());
769     return _buf;
770   case TEXT_VALUE:
771     sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
772     break;
773   case DOUBLE_VALUE:
774     sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
775     break;
776   }
777   return _buf;
778 }
779
780
781 \f
782 ////////////////////////////////////////////////////////////////////////
783 // Implementation of FGSwitchLayer.
784 ////////////////////////////////////////////////////////////////////////
785
786 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
787                               FGInstrumentLayer * layer1,
788                               FGInstrumentLayer * layer2)
789   : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
790 {
791 }
792
793 FGSwitchLayer::~FGSwitchLayer ()
794 {
795   delete _layer1;
796   delete _layer2;
797 }
798
799 void
800 FGSwitchLayer::draw ()
801 {
802   transform();
803   if (_value->getBoolValue()) {
804     _layer1->draw();
805   } else {
806     _layer2->draw();
807   }
808 }
809
810 \f
811 // end of panel.cxx