]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Tiled panel background support from Jim Wilson.
[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 <stdio.h>      // sprintf
30 #include <string.h>
31
32 #include <plib/ssg.h>
33 #include <plib/fnt.h>
34
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/misc/sg_path.hxx>
37
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
40 #include <Main/viewmgr.hxx>
41 #include <Objects/texload.h>
42 #include <Time/light.hxx>
43
44 #include "hud.hxx"
45 #include "panel.hxx"
46
47 #define WIN_X 0
48 #define WIN_Y 0
49 #define WIN_W 1024
50 #define WIN_H 768
51
52 #if defined( NONE ) && defined( _MSC_VER )
53 #  pragma message( "A sloppy coder has defined NONE as a macro!!!" )
54 #  undef NONE
55 #elif defined( NONE )
56 #  pragma warn A sloppy coder has defined NONE as a macro!!!
57 #  undef NONE
58 #endif
59
60
61 \f
62 ////////////////////////////////////////////////////////////////////////
63 // Local functions.
64 ////////////////////////////////////////////////////////////////////////
65
66
67 /**
68  * Calculate the aspect adjustment for the panel.
69  */
70 static float
71 get_aspect_adjust (int xsize, int ysize)
72 {
73   float ideal_aspect = float(WIN_W) / float(WIN_H);
74   float real_aspect = float(xsize) / float(ysize);
75   return (real_aspect / ideal_aspect);
76 }
77
78
79 \f
80 ////////////////////////////////////////////////////////////////////////
81 // Global functions.
82 ////////////////////////////////////////////////////////////////////////
83
84 bool
85 fgPanelVisible ()
86 {
87   return ((current_panel != 0) &&
88           (current_panel->getVisibility()) &&
89           (globals->get_viewmgr()->get_current() == 0) &&
90           (globals->get_current_view()->get_view_offset() == 0.0));
91 }
92
93
94 \f
95 ////////////////////////////////////////////////////////////////////////
96 // Implementation of FGTextureManager.
97 ////////////////////////////////////////////////////////////////////////
98
99 map<string,ssgTexture *> FGTextureManager::_textureMap;
100
101 ssgTexture *
102 FGTextureManager::createTexture (const string &relativePath)
103 {
104   ssgTexture * texture = _textureMap[relativePath];
105   if (texture == 0) {
106     SG_LOG( SG_COCKPIT, SG_DEBUG,
107             "Texture " << relativePath << " does not yet exist" );
108     SGPath tpath(globals->get_fg_root());
109     tpath.append(relativePath);
110     texture = new ssgTexture((char *)tpath.c_str(), false, false);
111     _textureMap[relativePath] = texture;
112     if (_textureMap[relativePath] == 0) 
113       SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
114     SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
115             << " handle=" << texture->getHandle() );
116   }
117
118   return texture;
119 }
120
121
122
123 \f
124 ////////////////////////////////////////////////////////////////////////
125 // Implementation of FGCropped Texture.
126 ////////////////////////////////////////////////////////////////////////
127
128
129 FGCroppedTexture::FGCroppedTexture ()
130   : _path(""), _texture(0),
131     _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
132 {
133 }
134
135
136 FGCroppedTexture::FGCroppedTexture (const string &path,
137                                     float minX, float minY,
138                                     float maxX, float maxY)
139   : _path(path), _texture(0),
140     _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
141 {
142 }
143
144
145 FGCroppedTexture::~FGCroppedTexture ()
146 {
147 }
148
149
150 ssgTexture *
151 FGCroppedTexture::getTexture ()
152 {
153   if (_texture == 0) {
154     _texture = FGTextureManager::createTexture(_path);
155   }
156   return _texture;
157 }
158
159
160 \f
161 ////////////////////////////////////////////////////////////////////////
162 // Implementation of FGPanel.
163 ////////////////////////////////////////////////////////////////////////
164
165 FGPanel * current_panel = NULL;
166 static fntRenderer text_renderer;
167
168
169 /**
170  * Constructor.
171  */
172 FGPanel::FGPanel ()
173   : _mouseDown(false),
174     _mouseInstrument(0),
175     _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
176     _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
177     _bound(false),
178     _jitter(0.0),
179     _xsize_node(fgGetNode("/sim/startup/xsize", true)),
180     _ysize_node(fgGetNode("/sim/startup/ysize", true))
181 {
182   setVisibility(fgPanelVisible());
183 }
184
185
186 /**
187  * Destructor.
188  */
189 FGPanel::~FGPanel ()
190 {
191   if (_bound)
192     unbind();
193   for (instrument_list_type::iterator it = _instruments.begin();
194        it != _instruments.end();
195        it++) {
196     delete *it;
197     *it = 0;
198   }
199 }
200
201
202 /**
203  * Add an instrument to the panel.
204  */
205 void
206 FGPanel::addInstrument (FGPanelInstrument * instrument)
207 {
208   _instruments.push_back(instrument);
209 }
210
211
212 /**
213  * Initialize the panel.
214  */
215 void
216 FGPanel::init ()
217 {
218   // NO-OP
219 }
220
221
222 /**
223  * Bind panel properties.
224  */
225 void
226 FGPanel::bind ()
227 {
228   fgTie("/sim/panel/visibility", &_visibility);
229   fgSetArchivable("/sim/panel/visibility");
230   fgTie("/sim/panel/x-offset", &_x_offset);
231   fgSetArchivable("/sim/panel/x-offset");
232   fgTie("/sim/panel/y-offset", &_y_offset);
233   fgSetArchivable("/sim/panel/y-offset");
234   fgTie("/sim/panel/jitter", &_jitter);
235   fgSetArchivable("/sim/panel/jitter");
236   _bound = true;
237 }
238
239
240 /**
241  * Unbind panel properties.
242  */
243 void
244 FGPanel::unbind ()
245 {
246   fgUntie("/sim/panel/visibility");
247   fgUntie("/sim/panel/x-offset");
248   fgUntie("/sim/panel/y-offset");
249   _bound = false;
250 }
251
252
253 /**
254  * Update the panel.
255  */
256 void
257 FGPanel::update (int dt)
258 {
259                                 // Do nothing if the panel isn't visible.
260     if ( !fgPanelVisible() ) {
261         return;
262     }
263
264                                 // If the mouse is down, do something
265     if (_mouseDown) {
266         _mouseDelay--;
267         if (_mouseDelay < 0) {
268             _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
269             _mouseDelay = 2;
270         }
271     }
272
273                                 // Now, draw the panel
274     float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
275                                             _ysize_node->getIntValue());
276     if (aspect_adjust <1.0)
277         update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
278     else
279         update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
280 }
281
282
283 void
284 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
285 {
286                                 // Calculate accelerations
287                                 // and jiggle the panel accordingly
288                                 // The factors and bounds are just
289                                 // initial guesses; using sqrt smooths
290                                 // out the spikes.
291   double x_offset = _x_offset;
292   double y_offset = _y_offset;
293
294   if (_jitter != 0.0) {
295     double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
296     double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
297     double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
298
299     double a_zx_pilot = a_z_pilot - a_x_pilot;
300     
301     int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
302                    (a_y_pilot < 0 ? -1 : 1);
303     int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
304                    (a_zx_pilot < 0 ? -1 : 1);
305
306                                 // adjustments in screen coordinates
307     x_offset += x_adjust;
308     y_offset += y_adjust;
309   }
310
311   glMatrixMode(GL_PROJECTION);
312   glPushMatrix();
313   glLoadIdentity();
314   gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
315   // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
316
317   glMatrixMode(GL_MODELVIEW);
318   glPushMatrix();
319   glLoadIdentity();
320
321   glTranslated(x_offset, y_offset, 0);
322
323                                 // Draw the background
324   glEnable(GL_TEXTURE_2D);
325   glDisable(GL_LIGHTING);
326   glEnable(GL_BLEND);
327   glEnable(GL_ALPHA_TEST);
328   glEnable(GL_COLOR_MATERIAL);
329   // glColor4f(1.0, 1.0, 1.0, 1.0);
330   if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
331       glColor4fv( cur_light_params.scene_diffuse );
332   } else {
333       glColor4f(0.7, 0.2, 0.2, 1.0);
334   }
335   if (_bg != 0) {
336     glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
337     // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
338     // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
339     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
340     glBegin(GL_POLYGON);
341     glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
342     glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
343     glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
344     glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
345     glEnd();
346   } else {
347     for (int i = 0; i < 4; i ++) {
348       // top row of textures...(1,3,5,7)
349       glBindTexture(GL_TEXTURE_2D, _mbg[i*2]->getHandle());
350       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
351       glBegin(GL_POLYGON);
352       glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X + (_width/4) * i, WIN_Y + (_height/2), 0);
353       glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2), 0);
354       glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + (_width/4) * (i+1), WIN_Y + _height, 0);
355       glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X + (_width/4) * i, WIN_Y + _height, 0);
356       glEnd();
357       // bottom row of textures...(2,4,6,8)
358       glBindTexture(GL_TEXTURE_2D, _mbg[(i*2)+1]->getHandle());
359       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
360       glBegin(GL_POLYGON);
361       glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X + (_width/4) * i, WIN_Y, 0);
362       glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + (_width/4) * (i+1), WIN_Y, 0);
363       glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2), 0);
364       glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X + (_width/4) * i, WIN_Y + (_height/2), 0);
365       glEnd();
366     }
367
368   }
369
370                                 // Draw the instruments.
371   instrument_list_type::const_iterator current = _instruments.begin();
372   instrument_list_type::const_iterator end = _instruments.end();
373
374   for ( ; current != end; current++) {
375     FGPanelInstrument * instr = *current;
376     glLoadIdentity();
377     glTranslated(x_offset, y_offset, 0);
378     glTranslated(instr->getXPos(), instr->getYPos(), 0);
379     instr->draw();
380   }
381
382   glMatrixMode(GL_PROJECTION);
383   glPopMatrix();
384   glMatrixMode(GL_MODELVIEW);
385   glPopMatrix();
386   ssgForceBasicState();
387   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
388 }
389
390
391 /**
392  * Set the panel's visibility.
393  */
394 void
395 FGPanel::setVisibility (bool visibility)
396 {
397   _visibility = visibility;
398 }
399
400
401 /**
402  * Return true if the panel is visible.
403  */
404 bool
405 FGPanel::getVisibility () const
406 {
407   return _visibility;
408 }
409
410
411 /**
412  * Set the panel's background texture.
413  */
414 void
415 FGPanel::setBackground (ssgTexture * texture)
416 {
417   _bg = texture;
418 }
419
420 /**
421  * Set the panel's multiple background textures.
422  */
423 void
424 FGPanel::setMultiBackground (ssgTexture * texture, int idx)
425 {
426   _bg = 0;
427   _mbg[idx] = texture;
428 }
429
430 /**
431  * Set the panel's x-offset.
432  */
433 void
434 FGPanel::setXOffset (int offset)
435 {
436   if (offset <= 0 && offset >= -_width + WIN_W)
437     _x_offset = offset;
438 }
439
440
441 /**
442  * Set the panel's y-offset.
443  */
444 void
445 FGPanel::setYOffset (int offset)
446 {
447   if (offset <= 0 && offset >= -_height)
448     _y_offset = offset;
449 }
450
451
452 /**
453  * Perform a mouse action.
454  */
455 bool
456 FGPanel::doMouseAction (int button, int updown, int x, int y)
457 {
458                                 // FIXME: this same code appears in update()
459   int xsize = _xsize_node->getIntValue();
460   int ysize = _ysize_node->getIntValue();
461   float aspect_adjust = get_aspect_adjust(xsize, ysize);
462
463                                 // Note a released button and return
464   // cerr << "Doing mouse action\n";
465   if (updown == 1) {
466     _mouseDown = false;
467     _mouseInstrument = 0;
468     return true;
469   }
470
471                                 // Scale for the real window size.
472   if (aspect_adjust < 1.0) {
473     x = int(((float)x / xsize) * WIN_W * aspect_adjust);
474     y = int(WIN_H - ((float(y) / ysize) * WIN_H));
475   } else {
476     x = int(((float)x / xsize) * WIN_W);
477     y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
478   }
479
480                                 // Adjust for offsets.
481   x -= _x_offset;
482   y -= _y_offset;
483
484                                 // Search for a matching instrument.
485   for (int i = 0; i < (int)_instruments.size(); i++) {
486     FGPanelInstrument *inst = _instruments[i];
487     int ix = inst->getXPos();
488     int iy = inst->getYPos();
489     int iw = inst->getWidth() / 2;
490     int ih = inst->getHeight() / 2;
491     if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
492       _mouseDown = true;
493       _mouseDelay = 20;
494       _mouseInstrument = inst;
495       _mouseButton = button;
496       _mouseX = x - ix;
497       _mouseY = y - iy;
498                                 // Always do the action once.
499       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
500       return true;
501     }
502   }
503   return false;
504 }
505
506
507 \f
508 ////////////////////////////////////////////////////////////////////////.
509 // Implementation of FGPanelAction.
510 ////////////////////////////////////////////////////////////////////////
511
512 FGPanelAction::FGPanelAction ()
513 {
514 }
515
516 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
517   : _button(button), _x(x), _y(y), _w(w), _h(h)
518 {
519   for (unsigned int i = 0; i < _bindings.size(); i++)
520     delete _bindings[i];
521 }
522
523 FGPanelAction::~FGPanelAction ()
524 {
525 }
526
527 void
528 FGPanelAction::addBinding (FGBinding * binding)
529 {
530   _bindings.push_back(binding);
531 }
532
533 void
534 FGPanelAction::doAction ()
535 {
536   if (test()) {
537     int nBindings = _bindings.size();
538     for (int i = 0; i < nBindings; i++) {
539       _bindings[i]->fire();
540     }
541   }
542 }
543
544
545 \f
546 ////////////////////////////////////////////////////////////////////////
547 // Implementation of FGPanelTransformation.
548 ////////////////////////////////////////////////////////////////////////
549
550 FGPanelTransformation::FGPanelTransformation ()
551   : table(0)
552 {
553 }
554
555 FGPanelTransformation::~FGPanelTransformation ()
556 {
557   delete table;
558 }
559
560
561 \f
562 ////////////////////////////////////////////////////////////////////////
563 // Implementation of FGPanelInstrument.
564 ////////////////////////////////////////////////////////////////////////
565
566
567 FGPanelInstrument::FGPanelInstrument ()
568 {
569   setPosition(0, 0);
570   setSize(0, 0);
571 }
572
573 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
574 {
575   setPosition(x, y);
576   setSize(w, h);
577 }
578
579 FGPanelInstrument::~FGPanelInstrument ()
580 {
581   for (action_list_type::iterator it = _actions.begin();
582        it != _actions.end();
583        it++) {
584     delete *it;
585     *it = 0;
586   }
587 }
588
589 void
590 FGPanelInstrument::setPosition (int x, int y)
591 {
592   _x = x;
593   _y = y;
594 }
595
596 void
597 FGPanelInstrument::setSize (int w, int h)
598 {
599   _w = w;
600   _h = h;
601 }
602
603 int
604 FGPanelInstrument::getXPos () const
605 {
606   return _x;
607 }
608
609 int
610 FGPanelInstrument::getYPos () const
611 {
612   return _y;
613 }
614
615 int
616 FGPanelInstrument::getWidth () const
617 {
618   return _w;
619 }
620
621 int
622 FGPanelInstrument::getHeight () const
623 {
624   return _h;
625 }
626
627 void
628 FGPanelInstrument::addAction (FGPanelAction * action)
629 {
630   _actions.push_back(action);
631 }
632
633                                 // Coordinates relative to centre.
634 bool
635 FGPanelInstrument::doMouseAction (int button, int x, int y)
636 {
637   if (test()) {
638     action_list_type::iterator it = _actions.begin();
639     action_list_type::iterator last = _actions.end();
640     for ( ; it != last; it++) {
641       if ((*it)->inArea(button, x, y)) {
642         (*it)->doAction();
643         return true;
644       }
645     }
646   }
647   return false;
648 }
649
650
651 \f
652 ////////////////////////////////////////////////////////////////////////
653 // Implementation of FGLayeredInstrument.
654 ////////////////////////////////////////////////////////////////////////
655
656 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
657   : FGPanelInstrument(x, y, w, h)
658 {
659 }
660
661 FGLayeredInstrument::~FGLayeredInstrument ()
662 {
663   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
664     delete *it;
665     *it = 0;
666   }
667 }
668
669 void
670 FGLayeredInstrument::draw ()
671 {
672   if (test()) {
673     for (int i = 0; i < (int)_layers.size(); i++) {
674       glPushMatrix();
675       glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
676       _layers[i]->draw();
677       glPopMatrix();
678     }
679   }
680 }
681
682 int
683 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
684 {
685   int n = _layers.size();
686   if (layer->getWidth() == -1) {
687     layer->setWidth(getWidth());
688   }
689   if (layer->getHeight() == -1) {
690     layer->setHeight(getHeight());
691   }
692   _layers.push_back(layer);
693   return n;
694 }
695
696 int
697 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
698                                int w, int h)
699 {
700   return addLayer(new FGTexturedLayer(texture, w, h));
701 }
702
703 void
704 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
705 {
706   int layer = _layers.size() - 1;
707   _layers[layer]->addTransformation(transformation);
708 }
709
710
711 \f
712 ////////////////////////////////////////////////////////////////////////
713 // Implementation of FGInstrumentLayer.
714 ////////////////////////////////////////////////////////////////////////
715
716 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
717   : _w(w),
718     _h(h)
719 {
720 }
721
722 FGInstrumentLayer::~FGInstrumentLayer ()
723 {
724   for (transformation_list::iterator it = _transformations.begin();
725        it != _transformations.end();
726        it++) {
727     delete *it;
728     *it = 0;
729   }
730 }
731
732 void
733 FGInstrumentLayer::transform () const
734 {
735   transformation_list::const_iterator it = _transformations.begin();
736   transformation_list::const_iterator last = _transformations.end();
737   while (it != last) {
738     FGPanelTransformation *t = *it;
739     if (t->test()) {
740       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
741       if (val < t->min) {
742         val = t->min;
743       } else if (val > t->max) {
744         val = t->max;
745       }
746       if(t->table==0) {
747         val = val * t->factor + t->offset;
748       } else {
749         val = t->table->interpolate(val) * t->factor + t->offset;
750       }
751       
752       switch (t->type) {
753       case FGPanelTransformation::XSHIFT:
754         glTranslatef(val, 0.0, 0.0);
755         break;
756       case FGPanelTransformation::YSHIFT:
757         glTranslatef(0.0, val, 0.0);
758         break;
759       case FGPanelTransformation::ROTATION:
760         glRotatef(-val, 0.0, 0.0, 1.0);
761         break;
762       }
763     }
764     it++;
765   }
766 }
767
768 void
769 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
770 {
771   _transformations.push_back(transformation);
772 }
773
774
775 \f
776 ////////////////////////////////////////////////////////////////////////
777 // Implementation of FGGroupLayer.
778 ////////////////////////////////////////////////////////////////////////
779
780 FGGroupLayer::FGGroupLayer ()
781 {
782 }
783
784 FGGroupLayer::~FGGroupLayer ()
785 {
786   for (unsigned int i = 0; i < _layers.size(); i++)
787     delete _layers[i];
788 }
789
790 void
791 FGGroupLayer::draw ()
792 {
793   if (test()) {
794     int nLayers = _layers.size();
795     for (int i = 0; i < nLayers; i++)
796       _layers[i]->draw();
797   }
798 }
799
800 void
801 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
802 {
803   _layers.push_back(layer);
804 }
805
806
807 \f
808 ////////////////////////////////////////////////////////////////////////
809 // Implementation of FGTexturedLayer.
810 ////////////////////////////////////////////////////////////////////////
811
812
813 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
814   : FGInstrumentLayer(w, h)
815 {
816   setTexture(texture);
817 }
818
819
820 FGTexturedLayer::~FGTexturedLayer ()
821 {
822 }
823
824
825 void
826 FGTexturedLayer::draw ()
827 {
828   if (test()) {
829     int w2 = _w / 2;
830     int h2 = _h / 2;
831     
832     transform();
833     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
834     glBegin(GL_POLYGON);
835     
836                                 // From Curt: turn on the panel
837                                 // lights after sundown.
838     if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
839       glColor4fv( cur_light_params.scene_diffuse );
840     } else {
841       glColor4f(0.7, 0.2, 0.2, 1.0);
842     }
843
844
845     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
846     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
847     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
848     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
849     glEnd();
850   }
851 }
852
853
854 \f
855 ////////////////////////////////////////////////////////////////////////
856 // Implementation of FGTextLayer.
857 ////////////////////////////////////////////////////////////////////////
858
859 FGTextLayer::FGTextLayer (int w, int h)
860   : FGInstrumentLayer(w, h), _pointSize(14.0)
861 {
862   _then.stamp();
863   _color[0] = _color[1] = _color[2] = 0.0;
864   _color[3] = 1.0;
865 }
866
867 FGTextLayer::~FGTextLayer ()
868 {
869   chunk_list::iterator it = _chunks.begin();
870   chunk_list::iterator last = _chunks.end();
871   for ( ; it != last; it++) {
872     delete *it;
873   }
874 }
875
876 void
877 FGTextLayer::draw ()
878 {
879   if (test()) {
880     glPushMatrix();
881     glColor4fv(_color);
882     transform();
883     text_renderer.setFont(guiFntHandle);
884     text_renderer.setPointSize(_pointSize);
885     text_renderer.begin();
886     text_renderer.start3f(0, 0, 0);
887
888     _now.stamp();
889     if (_now - _then > 100000) {
890       recalc_value();
891       _then = _now;
892     }
893     text_renderer.puts((char *)(_value.c_str()));
894
895     text_renderer.end();
896     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
897     glPopMatrix();
898   }
899 }
900
901 void
902 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
903 {
904   _chunks.push_back(chunk);
905 }
906
907 void
908 FGTextLayer::setColor (float r, float g, float b)
909 {
910   _color[0] = r;
911   _color[1] = g;
912   _color[2] = b;
913   _color[3] = 1.0;
914 }
915
916 void
917 FGTextLayer::setPointSize (float size)
918 {
919   _pointSize = size;
920 }
921
922 void
923 FGTextLayer::setFont(fntFont * font)
924 {
925   text_renderer.setFont(font);
926 }
927
928
929 void
930 FGTextLayer::recalc_value () const
931 {
932   _value = "";
933   chunk_list::const_iterator it = _chunks.begin();
934   chunk_list::const_iterator last = _chunks.end();
935   for ( ; it != last; it++) {
936     _value += (*it)->getValue();
937   }
938 }
939
940
941 \f
942 ////////////////////////////////////////////////////////////////////////
943 // Implementation of FGTextLayer::Chunk.
944 ////////////////////////////////////////////////////////////////////////
945
946 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
947   : _type(FGTextLayer::TEXT), _fmt(fmt)
948 {
949   _text = text;
950   if (_fmt == "") 
951     _fmt = "%s";
952 }
953
954 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
955                            const string &fmt, float mult)
956   : _type(type), _fmt(fmt), _mult(mult)
957 {
958   if (_fmt == "") {
959     if (type == TEXT_VALUE)
960       _fmt = "%s";
961     else
962       _fmt = "%.2f";
963   }
964   _node = node;
965 }
966
967 const char *
968 FGTextLayer::Chunk::getValue () const
969 {
970   if (test()) {
971     switch (_type) {
972     case TEXT:
973       sprintf(_buf, _fmt.c_str(), _text.c_str());
974       return _buf;
975     case TEXT_VALUE:
976       sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
977       break;
978     case DOUBLE_VALUE:
979       sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
980       break;
981     }
982     return _buf;
983   } else {
984     return "";
985   }
986 }
987
988
989 \f
990 ////////////////////////////////////////////////////////////////////////
991 // Implementation of FGSwitchLayer.
992 ////////////////////////////////////////////////////////////////////////
993
994 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
995                               FGInstrumentLayer * layer1,
996                               FGInstrumentLayer * layer2)
997   : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
998 {
999 }
1000
1001 FGSwitchLayer::~FGSwitchLayer ()
1002 {
1003   delete _layer1;
1004   delete _layer2;
1005 }
1006
1007 void
1008 FGSwitchLayer::draw ()
1009 {
1010   if (test()) {
1011     transform();
1012     if (_node->getBoolValue()) {
1013       _layer1->draw();
1014     } else {
1015       _layer2->draw();
1016     }
1017   }
1018 }
1019
1020 \f
1021 // end of panel.cxx
1022
1023