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