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