]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Fix two problems:
[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 (double 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     float z = 0.1f;
790     float z_inc = 0.01;
791     bool vc = fgGetBool("/sim/virtual-cockpit");
792     for (int i = 0; i < (int)_layers.size(); i++) {
793       glPushMatrix();
794       if(!vc) {
795         glTranslatef(0.0, 0.0, z);
796         z += z_inc;
797       }
798       _layers[i]->draw();
799       glPopMatrix();
800     }
801   }
802 }
803
804 int
805 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
806 {
807   int n = _layers.size();
808   if (layer->getWidth() == -1) {
809     layer->setWidth(getWidth());
810   }
811   if (layer->getHeight() == -1) {
812     layer->setHeight(getHeight());
813   }
814   _layers.push_back(layer);
815   return n;
816 }
817
818 int
819 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
820                                int w, int h)
821 {
822   return addLayer(new FGTexturedLayer(texture, w, h));
823 }
824
825 void
826 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
827 {
828   int layer = _layers.size() - 1;
829   _layers[layer]->addTransformation(transformation);
830 }
831
832
833 \f
834 ////////////////////////////////////////////////////////////////////////
835 // Implementation of FGInstrumentLayer.
836 ////////////////////////////////////////////////////////////////////////
837
838 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
839   : _w(w),
840     _h(h)
841 {
842 }
843
844 FGInstrumentLayer::~FGInstrumentLayer ()
845 {
846   for (transformation_list::iterator it = _transformations.begin();
847        it != _transformations.end();
848        it++) {
849     delete *it;
850     *it = 0;
851   }
852 }
853
854 void
855 FGInstrumentLayer::transform () const
856 {
857   transformation_list::const_iterator it = _transformations.begin();
858   transformation_list::const_iterator last = _transformations.end();
859   while (it != last) {
860     FGPanelTransformation *t = *it;
861     if (t->test()) {
862       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
863       if (val < t->min) {
864         val = t->min;
865       } else if (val > t->max) {
866         val = t->max;
867       }
868       if(t->table==0) {
869         val = val * t->factor + t->offset;
870       } else {
871         val = t->table->interpolate(val) * t->factor + t->offset;
872       }
873       
874       switch (t->type) {
875       case FGPanelTransformation::XSHIFT:
876         glTranslatef(val, 0.0, 0.0);
877         break;
878       case FGPanelTransformation::YSHIFT:
879         glTranslatef(0.0, val, 0.0);
880         break;
881       case FGPanelTransformation::ROTATION:
882         glRotatef(-val, 0.0, 0.0, 1.0);
883         break;
884       }
885     }
886     it++;
887   }
888 }
889
890 void
891 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
892 {
893   _transformations.push_back(transformation);
894 }
895
896
897 \f
898 ////////////////////////////////////////////////////////////////////////
899 // Implementation of FGGroupLayer.
900 ////////////////////////////////////////////////////////////////////////
901
902 FGGroupLayer::FGGroupLayer ()
903 {
904 }
905
906 FGGroupLayer::~FGGroupLayer ()
907 {
908   for (unsigned int i = 0; i < _layers.size(); i++)
909     delete _layers[i];
910 }
911
912 void
913 FGGroupLayer::draw ()
914 {
915   if (test()) {
916     int nLayers = _layers.size();
917     for (int i = 0; i < nLayers; i++)
918       _layers[i]->draw();
919   }
920 }
921
922 void
923 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
924 {
925   _layers.push_back(layer);
926 }
927
928
929 \f
930 ////////////////////////////////////////////////////////////////////////
931 // Implementation of FGTexturedLayer.
932 ////////////////////////////////////////////////////////////////////////
933
934
935 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
936   : FGInstrumentLayer(w, h)
937 {
938   setTexture(texture);
939 }
940
941
942 FGTexturedLayer::~FGTexturedLayer ()
943 {
944 }
945
946
947 void
948 FGTexturedLayer::draw ()
949 {
950   if (test()) {
951     int w2 = _w / 2;
952     int h2 = _h / 2;
953     
954     transform();
955     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
956     glBegin(GL_POLYGON);
957     
958                                 // From Curt: turn on the panel
959                                 // lights after sundown.
960     if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
961       glColor4fv( cur_light_params.scene_diffuse );
962     } else {
963       glColor4f(0.7, 0.2, 0.2, 1.0);
964     }
965
966
967     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
968     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
969     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
970     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
971     glEnd();
972   }
973 }
974
975
976 \f
977 ////////////////////////////////////////////////////////////////////////
978 // Implementation of FGTextLayer.
979 ////////////////////////////////////////////////////////////////////////
980
981 FGTextLayer::FGTextLayer (int w, int h)
982   : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("default")
983 {
984   _then.stamp();
985   _color[0] = _color[1] = _color[2] = 0.0;
986   _color[3] = 1.0;
987 }
988
989 FGTextLayer::~FGTextLayer ()
990 {
991   chunk_list::iterator it = _chunks.begin();
992   chunk_list::iterator last = _chunks.end();
993   for ( ; it != last; it++) {
994     delete *it;
995   }
996 }
997
998 void
999 FGTextLayer::draw ()
1000 {
1001   if (test()) {
1002     glPushMatrix();
1003     glColor4fv(_color);
1004     transform();
1005     if ( _font_name == "led" ) {
1006         text_renderer.setFont(led_font);
1007     } else {
1008         text_renderer.setFont(guiFntHandle);
1009     }
1010     text_renderer.setPointSize(_pointSize);
1011     text_renderer.begin();
1012     text_renderer.start3f(0, 0, 0);
1013
1014     _now.stamp();
1015     long diff = _now - _then;
1016 #if 0
1017     // It would be nice to keep this #ifdef'd stuff for (04/18/2002 +
1018     // a couple days) as I verify my solution to the panel text
1019     // drawing problem is actually correct. -CLO
1020     cout << "time diff = " << diff << endl;
1021     if ( _now - _then < 0 ) {
1022         cout << "Eeek, the past is now in the future!" << endl;
1023         cout << "Now = " << _now.get_seconds() << " seconds "
1024              << _now.get_usec() << "usecs" << endl;
1025         cout << "Past = " << _then.get_seconds() << " seconds "
1026              << _then.get_usec() << "usecs" << endl;
1027         exit(-1);
1028     }
1029 #endif
1030     if (diff > 100000 || diff < 0 ) {
1031       // ( diff < 0 ) is a sanity check and indicates our time stamp
1032       // difference math probably overflowed.  We can handle a max
1033       // difference of 35.8 minutes since the returned value is in
1034       // usec.  So if the panel is left off longer than that we can
1035       // over flow the math with it is turned back on.  This (diff <
1036       // 0) catches that situation, get's us out of trouble, and
1037       // back on track.
1038       recalc_value();
1039       _then = _now;
1040     }
1041     text_renderer.puts((char *)(_value.c_str()));
1042
1043     text_renderer.end();
1044     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
1045     glPopMatrix();
1046   }
1047 }
1048
1049 void
1050 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1051 {
1052   _chunks.push_back(chunk);
1053 }
1054
1055 void
1056 FGTextLayer::setColor (float r, float g, float b)
1057 {
1058   _color[0] = r;
1059   _color[1] = g;
1060   _color[2] = b;
1061   _color[3] = 1.0;
1062 }
1063
1064 void
1065 FGTextLayer::setPointSize (float size)
1066 {
1067   _pointSize = size;
1068 }
1069
1070 void
1071 FGTextLayer::setFontName(const string &name)
1072 {
1073   _font_name = name;
1074 }
1075
1076
1077 void
1078 FGTextLayer::setFont(fntFont * font)
1079 {
1080   text_renderer.setFont(font);
1081 }
1082
1083
1084 void
1085 FGTextLayer::recalc_value () const
1086 {
1087   _value = "";
1088   chunk_list::const_iterator it = _chunks.begin();
1089   chunk_list::const_iterator last = _chunks.end();
1090   for ( ; it != last; it++) {
1091     _value += (*it)->getValue();
1092   }
1093 }
1094
1095
1096 \f
1097 ////////////////////////////////////////////////////////////////////////
1098 // Implementation of FGTextLayer::Chunk.
1099 ////////////////////////////////////////////////////////////////////////
1100
1101 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1102   : _type(FGTextLayer::TEXT), _fmt(fmt)
1103 {
1104   _text = text;
1105   if (_fmt.empty()) 
1106     _fmt = "%s";
1107 }
1108
1109 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1110                            const string &fmt, float mult)
1111   : _type(type), _fmt(fmt), _mult(mult)
1112 {
1113   if (_fmt.empty()) {
1114     if (type == TEXT_VALUE)
1115       _fmt = "%s";
1116     else
1117       _fmt = "%.2f";
1118   }
1119   _node = node;
1120 }
1121
1122 const char *
1123 FGTextLayer::Chunk::getValue () const
1124 {
1125   if (test()) {
1126     _buf[0] = '\0';
1127     switch (_type) {
1128     case TEXT:
1129       sprintf(_buf, _fmt.c_str(), _text.c_str());
1130       return _buf;
1131     case TEXT_VALUE:
1132       sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1133       break;
1134     case DOUBLE_VALUE:
1135       sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
1136       break;
1137     }
1138     return _buf;
1139   } else {
1140     return "";
1141   }
1142 }
1143
1144
1145 \f
1146 ////////////////////////////////////////////////////////////////////////
1147 // Implementation of FGSwitchLayer.
1148 ////////////////////////////////////////////////////////////////////////
1149
1150 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
1151                               FGInstrumentLayer * layer1,
1152                               FGInstrumentLayer * layer2)
1153   : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
1154 {
1155 }
1156
1157 FGSwitchLayer::~FGSwitchLayer ()
1158 {
1159   delete _layer1;
1160   delete _layer2;
1161 }
1162
1163 void
1164 FGSwitchLayer::draw ()
1165 {
1166   if (test()) {
1167     transform();
1168     if (_node->getBoolValue()) {
1169       _layer1->draw();
1170     } else {
1171       _layer2->draw();
1172     }
1173   }
1174 }
1175
1176 \f
1177 // end of panel.cxx
1178
1179
1180