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