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