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