]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Eliminate dependencie on tying variable pointers (in preparation for
[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     _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->get_view_tilt();
456     float rot = view->get_view_offset();
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 true;
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       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
617       return true;
618     }
619   }
620   return false;
621 }
622
623
624 \f
625 ////////////////////////////////////////////////////////////////////////.
626 // Implementation of FGPanelAction.
627 ////////////////////////////////////////////////////////////////////////
628
629 FGPanelAction::FGPanelAction ()
630 {
631 }
632
633 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
634   : _button(button), _x(x), _y(y), _w(w), _h(h)
635 {
636   for (unsigned int i = 0; i < _bindings.size(); i++)
637     delete _bindings[i];
638 }
639
640 FGPanelAction::~FGPanelAction ()
641 {
642 }
643
644 void
645 FGPanelAction::addBinding (FGBinding * binding)
646 {
647   _bindings.push_back(binding);
648 }
649
650 void
651 FGPanelAction::doAction ()
652 {
653   if (test()) {
654     int nBindings = _bindings.size();
655     for (int i = 0; i < nBindings; i++) {
656       _bindings[i]->fire();
657     }
658   }
659 }
660
661
662 \f
663 ////////////////////////////////////////////////////////////////////////
664 // Implementation of FGPanelTransformation.
665 ////////////////////////////////////////////////////////////////////////
666
667 FGPanelTransformation::FGPanelTransformation ()
668   : table(0)
669 {
670 }
671
672 FGPanelTransformation::~FGPanelTransformation ()
673 {
674   delete table;
675 }
676
677
678 \f
679 ////////////////////////////////////////////////////////////////////////
680 // Implementation of FGPanelInstrument.
681 ////////////////////////////////////////////////////////////////////////
682
683
684 FGPanelInstrument::FGPanelInstrument ()
685 {
686   setPosition(0, 0);
687   setSize(0, 0);
688 }
689
690 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
691 {
692   setPosition(x, y);
693   setSize(w, h);
694 }
695
696 FGPanelInstrument::~FGPanelInstrument ()
697 {
698   for (action_list_type::iterator it = _actions.begin();
699        it != _actions.end();
700        it++) {
701     delete *it;
702     *it = 0;
703   }
704 }
705
706 void
707 FGPanelInstrument::setPosition (int x, int y)
708 {
709   _x = x;
710   _y = y;
711 }
712
713 void
714 FGPanelInstrument::setSize (int w, int h)
715 {
716   _w = w;
717   _h = h;
718 }
719
720 int
721 FGPanelInstrument::getXPos () const
722 {
723   return _x;
724 }
725
726 int
727 FGPanelInstrument::getYPos () const
728 {
729   return _y;
730 }
731
732 int
733 FGPanelInstrument::getWidth () const
734 {
735   return _w;
736 }
737
738 int
739 FGPanelInstrument::getHeight () const
740 {
741   return _h;
742 }
743
744 void
745 FGPanelInstrument::addAction (FGPanelAction * action)
746 {
747   _actions.push_back(action);
748 }
749
750                                 // Coordinates relative to centre.
751 bool
752 FGPanelInstrument::doMouseAction (int button, int x, int y)
753 {
754   if (test()) {
755     action_list_type::iterator it = _actions.begin();
756     action_list_type::iterator last = _actions.end();
757     for ( ; it != last; it++) {
758       if ((*it)->inArea(button, x, y)) {
759         (*it)->doAction();
760         return true;
761       }
762     }
763   }
764   return false;
765 }
766
767
768 \f
769 ////////////////////////////////////////////////////////////////////////
770 // Implementation of FGLayeredInstrument.
771 ////////////////////////////////////////////////////////////////////////
772
773 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
774   : FGPanelInstrument(x, y, w, h)
775 {
776 }
777
778 FGLayeredInstrument::~FGLayeredInstrument ()
779 {
780   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
781     delete *it;
782     *it = 0;
783   }
784 }
785
786 void
787 FGLayeredInstrument::draw ()
788 {
789   if (test()) {
790     for (int i = 0; i < (int)_layers.size(); i++) {
791       glPushMatrix();
792       if(!fgGetBool("/sim/virtual-cockpit"))
793           glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
794       _layers[i]->draw();
795       glPopMatrix();
796     }
797   }
798 }
799
800 int
801 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
802 {
803   int n = _layers.size();
804   if (layer->getWidth() == -1) {
805     layer->setWidth(getWidth());
806   }
807   if (layer->getHeight() == -1) {
808     layer->setHeight(getHeight());
809   }
810   _layers.push_back(layer);
811   return n;
812 }
813
814 int
815 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
816                                int w, int h)
817 {
818   return addLayer(new FGTexturedLayer(texture, w, h));
819 }
820
821 void
822 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
823 {
824   int layer = _layers.size() - 1;
825   _layers[layer]->addTransformation(transformation);
826 }
827
828
829 \f
830 ////////////////////////////////////////////////////////////////////////
831 // Implementation of FGInstrumentLayer.
832 ////////////////////////////////////////////////////////////////////////
833
834 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
835   : _w(w),
836     _h(h)
837 {
838 }
839
840 FGInstrumentLayer::~FGInstrumentLayer ()
841 {
842   for (transformation_list::iterator it = _transformations.begin();
843        it != _transformations.end();
844        it++) {
845     delete *it;
846     *it = 0;
847   }
848 }
849
850 void
851 FGInstrumentLayer::transform () const
852 {
853   transformation_list::const_iterator it = _transformations.begin();
854   transformation_list::const_iterator last = _transformations.end();
855   while (it != last) {
856     FGPanelTransformation *t = *it;
857     if (t->test()) {
858       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
859       if (val < t->min) {
860         val = t->min;
861       } else if (val > t->max) {
862         val = t->max;
863       }
864       if(t->table==0) {
865         val = val * t->factor + t->offset;
866       } else {
867         val = t->table->interpolate(val) * t->factor + t->offset;
868       }
869       
870       switch (t->type) {
871       case FGPanelTransformation::XSHIFT:
872         glTranslatef(val, 0.0, 0.0);
873         break;
874       case FGPanelTransformation::YSHIFT:
875         glTranslatef(0.0, val, 0.0);
876         break;
877       case FGPanelTransformation::ROTATION:
878         glRotatef(-val, 0.0, 0.0, 1.0);
879         break;
880       }
881     }
882     it++;
883   }
884 }
885
886 void
887 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
888 {
889   _transformations.push_back(transformation);
890 }
891
892
893 \f
894 ////////////////////////////////////////////////////////////////////////
895 // Implementation of FGGroupLayer.
896 ////////////////////////////////////////////////////////////////////////
897
898 FGGroupLayer::FGGroupLayer ()
899 {
900 }
901
902 FGGroupLayer::~FGGroupLayer ()
903 {
904   for (unsigned int i = 0; i < _layers.size(); i++)
905     delete _layers[i];
906 }
907
908 void
909 FGGroupLayer::draw ()
910 {
911   if (test()) {
912     int nLayers = _layers.size();
913     for (int i = 0; i < nLayers; i++)
914       _layers[i]->draw();
915   }
916 }
917
918 void
919 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
920 {
921   _layers.push_back(layer);
922 }
923
924
925 \f
926 ////////////////////////////////////////////////////////////////////////
927 // Implementation of FGTexturedLayer.
928 ////////////////////////////////////////////////////////////////////////
929
930
931 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
932   : FGInstrumentLayer(w, h)
933 {
934   setTexture(texture);
935 }
936
937
938 FGTexturedLayer::~FGTexturedLayer ()
939 {
940 }
941
942
943 void
944 FGTexturedLayer::draw ()
945 {
946   if (test()) {
947     int w2 = _w / 2;
948     int h2 = _h / 2;
949     
950     transform();
951     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
952     glBegin(GL_POLYGON);
953     
954                                 // From Curt: turn on the panel
955                                 // lights after sundown.
956     if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
957       glColor4fv( cur_light_params.scene_diffuse );
958     } else {
959       glColor4f(0.7, 0.2, 0.2, 1.0);
960     }
961
962
963     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
964     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
965     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
966     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
967     glEnd();
968   }
969 }
970
971
972 \f
973 ////////////////////////////////////////////////////////////////////////
974 // Implementation of FGTextLayer.
975 ////////////////////////////////////////////////////////////////////////
976
977 FGTextLayer::FGTextLayer (int w, int h)
978   : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("default")
979 {
980   _then.stamp();
981   _color[0] = _color[1] = _color[2] = 0.0;
982   _color[3] = 1.0;
983 }
984
985 FGTextLayer::~FGTextLayer ()
986 {
987   chunk_list::iterator it = _chunks.begin();
988   chunk_list::iterator last = _chunks.end();
989   for ( ; it != last; it++) {
990     delete *it;
991   }
992 }
993
994 void
995 FGTextLayer::draw ()
996 {
997   if (test()) {
998     glPushMatrix();
999     glColor4fv(_color);
1000     transform();
1001     if ( _font_name == "led" ) {
1002         text_renderer.setFont(led_font);
1003     } else {
1004         text_renderer.setFont(guiFntHandle);
1005     }
1006     text_renderer.setPointSize(_pointSize);
1007     text_renderer.begin();
1008     text_renderer.start3f(0, 0, 0);
1009
1010     _now.stamp();
1011     if (_now - _then > 100000) {
1012       recalc_value();
1013       _then = _now;
1014     }
1015     text_renderer.puts((char *)(_value.c_str()));
1016
1017     text_renderer.end();
1018     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
1019     glPopMatrix();
1020   }
1021 }
1022
1023 void
1024 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1025 {
1026   _chunks.push_back(chunk);
1027 }
1028
1029 void
1030 FGTextLayer::setColor (float r, float g, float b)
1031 {
1032   _color[0] = r;
1033   _color[1] = g;
1034   _color[2] = b;
1035   _color[3] = 1.0;
1036 }
1037
1038 void
1039 FGTextLayer::setPointSize (float size)
1040 {
1041   _pointSize = size;
1042 }
1043
1044 void
1045 FGTextLayer::setFontName(const string &name)
1046 {
1047   _font_name = name;
1048 }
1049
1050
1051 void
1052 FGTextLayer::setFont(fntFont * font)
1053 {
1054   text_renderer.setFont(font);
1055 }
1056
1057
1058 void
1059 FGTextLayer::recalc_value () const
1060 {
1061   _value = "";
1062   chunk_list::const_iterator it = _chunks.begin();
1063   chunk_list::const_iterator last = _chunks.end();
1064   for ( ; it != last; it++) {
1065     _value += (*it)->getValue();
1066   }
1067 }
1068
1069
1070 \f
1071 ////////////////////////////////////////////////////////////////////////
1072 // Implementation of FGTextLayer::Chunk.
1073 ////////////////////////////////////////////////////////////////////////
1074
1075 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1076   : _type(FGTextLayer::TEXT), _fmt(fmt)
1077 {
1078   _text = text;
1079   if (_fmt == "") 
1080     _fmt = "%s";
1081 }
1082
1083 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1084                            const string &fmt, float mult)
1085   : _type(type), _fmt(fmt), _mult(mult)
1086 {
1087   if (_fmt == "") {
1088     if (type == TEXT_VALUE)
1089       _fmt = "%s";
1090     else
1091       _fmt = "%.2f";
1092   }
1093   _node = node;
1094 }
1095
1096 const char *
1097 FGTextLayer::Chunk::getValue () const
1098 {
1099   if (test()) {
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().c_str());
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