]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Small tweaks to initialization sequence and logic so we can default to
[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 <string.h>
30
31 #include <plib/ssg.h>
32 #include <plib/fnt.h>
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sg_path.hxx>
36
37 #include <Main/globals.hxx>
38 #include <Main/fg_props.hxx>
39 #include <Main/viewmgr.hxx>
40 #include <Objects/texload.h>
41 #include <Time/light.hxx>
42
43 #include "hud.hxx"
44 #include "panel.hxx"
45
46 #define WIN_X 0
47 #define WIN_Y 0
48 #define WIN_W 1024
49 #define WIN_H 768
50
51 #if defined( NONE ) && defined( _MSC_VER )
52 #  pragma message( "A sloppy coder has defined NONE as a macro!!!" )
53 #  undef NONE
54 #elif defined( NONE )
55 #  pragma warn A sloppy coder has defined NONE as a macro!!!
56 #  undef NONE
57 #endif
58
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 ((current_panel != 0) &&
87           (current_panel->getVisibility()) &&
88           (globals->get_viewmgr()->get_current() == 0) &&
89           (globals->get_current_view()->get_view_offset() == 0.0));
90 }
91
92
93 \f
94 ////////////////////////////////////////////////////////////////////////
95 // Implementation of FGTextureManager.
96 ////////////////////////////////////////////////////////////////////////
97
98 map<string,ssgTexture *> FGTextureManager::_textureMap;
99
100 ssgTexture *
101 FGTextureManager::createTexture (const string &relativePath)
102 {
103   ssgTexture * texture = _textureMap[relativePath];
104   if (texture == 0) {
105     SG_LOG( SG_COCKPIT, SG_DEBUG,
106             "Texture " << relativePath << " does not yet exist" );
107     SGPath tpath(globals->get_fg_root());
108     tpath.append(relativePath);
109     texture = new ssgTexture((char *)tpath.c_str(), false, false);
110     _textureMap[relativePath] = texture;
111     if (_textureMap[relativePath] == 0) 
112       SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
113     SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
114             << " handle=" << texture->getHandle() );
115   }
116
117   return texture;
118 }
119
120
121
122 \f
123 ////////////////////////////////////////////////////////////////////////
124 // Implementation of FGCropped Texture.
125 ////////////////////////////////////////////////////////////////////////
126
127
128 FGCroppedTexture::FGCroppedTexture ()
129   : _path(""), _texture(0),
130     _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
131 {
132 }
133
134
135 FGCroppedTexture::FGCroppedTexture (const string &path,
136                                     float minX, float minY,
137                                     float maxX, float maxY)
138   : _path(path), _texture(0),
139     _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
140 {
141 }
142
143
144 FGCroppedTexture::~FGCroppedTexture ()
145 {
146 }
147
148
149 ssgTexture *
150 FGCroppedTexture::getTexture ()
151 {
152   if (_texture == 0) {
153     _texture = FGTextureManager::createTexture(_path);
154   }
155   return _texture;
156 }
157
158
159 \f
160 ////////////////////////////////////////////////////////////////////////
161 // Implementation of FGPanel.
162 ////////////////////////////////////////////////////////////////////////
163
164 FGPanel * current_panel = NULL;
165 static fntRenderer text_renderer;
166
167
168 /**
169  * Constructor.
170  */
171 FGPanel::FGPanel ()
172   : _mouseDown(false),
173     _mouseInstrument(0),
174     _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
175     _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
176     _bound(false),
177     _jitter(0.0),
178     _xsize_node(fgGetNode("/sim/startup/xsize", true)),
179     _ysize_node(fgGetNode("/sim/startup/ysize", true))
180 {
181   setVisibility(fgPanelVisible());
182 }
183
184
185 /**
186  * Destructor.
187  */
188 FGPanel::~FGPanel ()
189 {
190   if (_bound)
191     unbind();
192   for (instrument_list_type::iterator it = _instruments.begin();
193        it != _instruments.end();
194        it++) {
195     delete *it;
196     *it = 0;
197   }
198 }
199
200
201 /**
202  * Add an instrument to the panel.
203  */
204 void
205 FGPanel::addInstrument (FGPanelInstrument * instrument)
206 {
207   _instruments.push_back(instrument);
208 }
209
210
211 /**
212  * Initialize the panel.
213  */
214 void
215 FGPanel::init ()
216 {
217   // NO-OP
218 }
219
220
221 /**
222  * Bind panel properties.
223  */
224 void
225 FGPanel::bind ()
226 {
227   fgTie("/sim/panel/visibility", &_visibility);
228   fgSetArchivable("/sim/panel/visibility");
229   fgTie("/sim/panel/x-offset", &_x_offset);
230   fgSetArchivable("/sim/panel/x-offset");
231   fgTie("/sim/panel/y-offset", &_y_offset);
232   fgSetArchivable("/sim/panel/y-offset");
233   fgTie("/sim/panel/jitter", &_jitter);
234   fgSetArchivable("/sim/panel/jitter");
235   _bound = true;
236 }
237
238
239 /**
240  * Unbind panel properties.
241  */
242 void
243 FGPanel::unbind ()
244 {
245   fgUntie("/sim/panel/visibility");
246   fgUntie("/sim/panel/x-offset");
247   fgUntie("/sim/panel/y-offset");
248   _bound = false;
249 }
250
251
252 /**
253  * Update the panel.
254  */
255 void
256 FGPanel::update ()
257 {
258                                 // Do nothing if the panel isn't visible.
259     if ( !fgPanelVisible() ) {
260         return;
261     }
262
263                                 // If the mouse is down, do something
264     if (_mouseDown) {
265         _mouseDelay--;
266         if (_mouseDelay < 0) {
267             _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
268             _mouseDelay = 2;
269         }
270     }
271
272                                 // Now, draw the panel
273     float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
274                                             _ysize_node->getIntValue());
275     if (aspect_adjust <1.0)
276         update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
277     else
278         update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
279 }
280
281
282 void
283 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
284 {
285                                 // Calculate accelerations
286                                 // and jiggle the panel accordingly
287                                 // The factors and bounds are just
288                                 // initial guesses; using sqrt smooths
289                                 // out the spikes.
290   double x_offset = _x_offset;
291   double y_offset = _y_offset;
292
293   if (_jitter != 0.0) {
294     double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
295     double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
296     double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
297
298     double a_zx_pilot = a_z_pilot - a_x_pilot;
299     
300     int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
301                    (a_y_pilot < 0 ? -1 : 1);
302     int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
303                    (a_zx_pilot < 0 ? -1 : 1);
304
305                                 // adjustments in screen coordinates
306     x_offset += x_adjust;
307     y_offset += y_adjust;
308   }
309
310   glMatrixMode(GL_PROJECTION);
311   glPushMatrix();
312   glLoadIdentity();
313   gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
314   // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
315
316   glMatrixMode(GL_MODELVIEW);
317   glPushMatrix();
318   glLoadIdentity();
319
320   glTranslated(x_offset, y_offset, 0);
321
322                                 // Draw the background
323   glEnable(GL_TEXTURE_2D);
324   glDisable(GL_LIGHTING);
325   glEnable(GL_BLEND);
326   glEnable(GL_ALPHA_TEST);
327   glEnable(GL_COLOR_MATERIAL);
328   // glColor4f(1.0, 1.0, 1.0, 1.0);
329   if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
330       glColor4fv( cur_light_params.scene_diffuse );
331   } else {
332       glColor4f(0.7, 0.2, 0.2, 1.0);
333   }
334   glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
335   // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
336   // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
337   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
338   glBegin(GL_POLYGON);
339   glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
340   glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
341   glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
342   glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
343   glEnd();
344
345                                 // Draw the instruments.
346   instrument_list_type::const_iterator current = _instruments.begin();
347   instrument_list_type::const_iterator end = _instruments.end();
348
349   for ( ; current != end; current++) {
350     FGPanelInstrument * instr = *current;
351     glLoadIdentity();
352     glTranslated(x_offset, y_offset, 0);
353     glTranslated(instr->getXPos(), instr->getYPos(), 0);
354     instr->draw();
355   }
356
357   glMatrixMode(GL_PROJECTION);
358   glPopMatrix();
359   glMatrixMode(GL_MODELVIEW);
360   glPopMatrix();
361   ssgForceBasicState();
362   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
363 }
364
365
366 /**
367  * Set the panel's visibility.
368  */
369 void
370 FGPanel::setVisibility (bool visibility)
371 {
372   _visibility = visibility;
373 }
374
375
376 /**
377  * Return true if the panel is visible.
378  */
379 bool
380 FGPanel::getVisibility () const
381 {
382   return _visibility;
383 }
384
385
386 /**
387  * Set the panel's background texture.
388  */
389 void
390 FGPanel::setBackground (ssgTexture * texture)
391 {
392   _bg = texture;
393 }
394
395
396 /**
397  * Set the panel's x-offset.
398  */
399 void
400 FGPanel::setXOffset (int offset)
401 {
402   if (offset <= 0 && offset >= -_width + WIN_W)
403     _x_offset = offset;
404 }
405
406
407 /**
408  * Set the panel's y-offset.
409  */
410 void
411 FGPanel::setYOffset (int offset)
412 {
413   if (offset <= 0 && offset >= -_height)
414     _y_offset = offset;
415 }
416
417
418 /**
419  * Perform a mouse action.
420  */
421 bool
422 FGPanel::doMouseAction (int button, int updown, int x, int y)
423 {
424                                 // FIXME: this same code appears in update()
425   int xsize = _xsize_node->getIntValue();
426   int ysize = _ysize_node->getIntValue();
427   float aspect_adjust = get_aspect_adjust(xsize, ysize);
428
429                                 // Note a released button and return
430   // cerr << "Doing mouse action\n";
431   if (updown == 1) {
432     _mouseDown = false;
433     _mouseInstrument = 0;
434     return true;
435   }
436
437                                 // Scale for the real window size.
438   if (aspect_adjust < 1.0) {
439     x = int(((float)x / xsize) * WIN_W * aspect_adjust);
440     y = int(WIN_H - ((float(y) / ysize) * WIN_H));
441   } else {
442     x = int(((float)x / xsize) * WIN_W);
443     y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
444   }
445
446                                 // Adjust for offsets.
447   x -= _x_offset;
448   y -= _y_offset;
449
450                                 // Search for a matching instrument.
451   for (int i = 0; i < (int)_instruments.size(); i++) {
452     FGPanelInstrument *inst = _instruments[i];
453     int ix = inst->getXPos();
454     int iy = inst->getYPos();
455     int iw = inst->getWidth() / 2;
456     int ih = inst->getHeight() / 2;
457     if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
458       _mouseDown = true;
459       _mouseDelay = 20;
460       _mouseInstrument = inst;
461       _mouseButton = button;
462       _mouseX = x - ix;
463       _mouseY = y - iy;
464                                 // Always do the action once.
465       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
466       return true;
467     }
468   }
469   return false;
470 }
471
472
473 \f
474 ////////////////////////////////////////////////////////////////////////.
475 // Implementation of FGPanelAction.
476 ////////////////////////////////////////////////////////////////////////
477
478 FGPanelAction::FGPanelAction ()
479 {
480 }
481
482 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
483   : _button(button), _x(x), _y(y), _w(w), _h(h)
484 {
485   for (unsigned int i = 0; i < _bindings.size(); i++)
486     delete _bindings[i];
487 }
488
489 FGPanelAction::~FGPanelAction ()
490 {
491 }
492
493 void
494 FGPanelAction::addBinding (FGBinding * binding)
495 {
496   _bindings.push_back(binding);
497 }
498
499 void
500 FGPanelAction::doAction ()
501 {
502   if (test()) {
503     int nBindings = _bindings.size();
504     for (int i = 0; i < nBindings; i++) {
505       _bindings[i]->fire();
506     }
507   }
508 }
509
510
511 \f
512 ////////////////////////////////////////////////////////////////////////
513 // Implementation of FGPanelTransformation.
514 ////////////////////////////////////////////////////////////////////////
515
516 FGPanelTransformation::FGPanelTransformation ()
517   : table(0)
518 {
519 }
520
521 FGPanelTransformation::~FGPanelTransformation ()
522 {
523   delete table;
524 }
525
526
527 \f
528 ////////////////////////////////////////////////////////////////////////
529 // Implementation of FGPanelInstrument.
530 ////////////////////////////////////////////////////////////////////////
531
532
533 FGPanelInstrument::FGPanelInstrument ()
534 {
535   setPosition(0, 0);
536   setSize(0, 0);
537 }
538
539 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
540 {
541   setPosition(x, y);
542   setSize(w, h);
543 }
544
545 FGPanelInstrument::~FGPanelInstrument ()
546 {
547   for (action_list_type::iterator it = _actions.begin();
548        it != _actions.end();
549        it++) {
550     delete *it;
551     *it = 0;
552   }
553 }
554
555 void
556 FGPanelInstrument::setPosition (int x, int y)
557 {
558   _x = x;
559   _y = y;
560 }
561
562 void
563 FGPanelInstrument::setSize (int w, int h)
564 {
565   _w = w;
566   _h = h;
567 }
568
569 int
570 FGPanelInstrument::getXPos () const
571 {
572   return _x;
573 }
574
575 int
576 FGPanelInstrument::getYPos () const
577 {
578   return _y;
579 }
580
581 int
582 FGPanelInstrument::getWidth () const
583 {
584   return _w;
585 }
586
587 int
588 FGPanelInstrument::getHeight () const
589 {
590   return _h;
591 }
592
593 void
594 FGPanelInstrument::addAction (FGPanelAction * action)
595 {
596   _actions.push_back(action);
597 }
598
599                                 // Coordinates relative to centre.
600 bool
601 FGPanelInstrument::doMouseAction (int button, int x, int y)
602 {
603   if (test()) {
604     action_list_type::iterator it = _actions.begin();
605     action_list_type::iterator last = _actions.end();
606     for ( ; it != last; it++) {
607       if ((*it)->inArea(button, x, y)) {
608         (*it)->doAction();
609         return true;
610       }
611     }
612   }
613   return false;
614 }
615
616
617 \f
618 ////////////////////////////////////////////////////////////////////////
619 // Implementation of FGLayeredInstrument.
620 ////////////////////////////////////////////////////////////////////////
621
622 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
623   : FGPanelInstrument(x, y, w, h)
624 {
625 }
626
627 FGLayeredInstrument::~FGLayeredInstrument ()
628 {
629   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
630     delete *it;
631     *it = 0;
632   }
633 }
634
635 void
636 FGLayeredInstrument::draw ()
637 {
638   if (test()) {
639     for (int i = 0; i < (int)_layers.size(); i++) {
640       glPushMatrix();
641       glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
642       _layers[i]->draw();
643       glPopMatrix();
644     }
645   }
646 }
647
648 int
649 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
650 {
651   int n = _layers.size();
652   if (layer->getWidth() == -1) {
653     layer->setWidth(getWidth());
654   }
655   if (layer->getHeight() == -1) {
656     layer->setHeight(getHeight());
657   }
658   _layers.push_back(layer);
659   return n;
660 }
661
662 int
663 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
664                                int w, int h)
665 {
666   return addLayer(new FGTexturedLayer(texture, w, h));
667 }
668
669 void
670 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
671 {
672   int layer = _layers.size() - 1;
673   _layers[layer]->addTransformation(transformation);
674 }
675
676
677 \f
678 ////////////////////////////////////////////////////////////////////////
679 // Implementation of FGInstrumentLayer.
680 ////////////////////////////////////////////////////////////////////////
681
682 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
683   : _w(w),
684     _h(h)
685 {
686 }
687
688 FGInstrumentLayer::~FGInstrumentLayer ()
689 {
690   for (transformation_list::iterator it = _transformations.begin();
691        it != _transformations.end();
692        it++) {
693     delete *it;
694     *it = 0;
695   }
696 }
697
698 void
699 FGInstrumentLayer::transform () const
700 {
701   transformation_list::const_iterator it = _transformations.begin();
702   transformation_list::const_iterator last = _transformations.end();
703   while (it != last) {
704     FGPanelTransformation *t = *it;
705     if (t->test()) {
706       float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
707       if (val < t->min) {
708         val = t->min;
709       } else if (val > t->max) {
710         val = t->max;
711       }
712       if(t->table==0) {
713         val = val * t->factor + t->offset;
714       } else {
715         val = t->table->interpolate(val) * t->factor + t->offset;
716       }
717       
718       switch (t->type) {
719       case FGPanelTransformation::XSHIFT:
720         glTranslatef(val, 0.0, 0.0);
721         break;
722       case FGPanelTransformation::YSHIFT:
723         glTranslatef(0.0, val, 0.0);
724         break;
725       case FGPanelTransformation::ROTATION:
726         glRotatef(-val, 0.0, 0.0, 1.0);
727         break;
728       }
729     }
730     it++;
731   }
732 }
733
734 void
735 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
736 {
737   _transformations.push_back(transformation);
738 }
739
740
741 \f
742 ////////////////////////////////////////////////////////////////////////
743 // Implementation of FGGroupLayer.
744 ////////////////////////////////////////////////////////////////////////
745
746 FGGroupLayer::FGGroupLayer ()
747 {
748 }
749
750 FGGroupLayer::~FGGroupLayer ()
751 {
752   for (unsigned int i = 0; i < _layers.size(); i++)
753     delete _layers[i];
754 }
755
756 void
757 FGGroupLayer::draw ()
758 {
759   if (test()) {
760     int nLayers = _layers.size();
761     for (int i = 0; i < nLayers; i++)
762       _layers[i]->draw();
763   }
764 }
765
766 void
767 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
768 {
769   _layers.push_back(layer);
770 }
771
772
773 \f
774 ////////////////////////////////////////////////////////////////////////
775 // Implementation of FGTexturedLayer.
776 ////////////////////////////////////////////////////////////////////////
777
778
779 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
780   : FGInstrumentLayer(w, h)
781 {
782   setTexture(texture);
783 }
784
785
786 FGTexturedLayer::~FGTexturedLayer ()
787 {
788 }
789
790
791 void
792 FGTexturedLayer::draw ()
793 {
794   if (test()) {
795     int w2 = _w / 2;
796     int h2 = _h / 2;
797     
798     transform();
799     glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
800     glBegin(GL_POLYGON);
801     
802                                 // From Curt: turn on the panel
803                                 // lights after sundown.
804     if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
805       glColor4fv( cur_light_params.scene_diffuse );
806     } else {
807       glColor4f(0.7, 0.2, 0.2, 1.0);
808     }
809
810
811     glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
812     glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
813     glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
814     glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
815     glEnd();
816   }
817 }
818
819
820 \f
821 ////////////////////////////////////////////////////////////////////////
822 // Implementation of FGTextLayer.
823 ////////////////////////////////////////////////////////////////////////
824
825 FGTextLayer::FGTextLayer (int w, int h)
826   : FGInstrumentLayer(w, h), _pointSize(14.0)
827 {
828   _then.stamp();
829   _color[0] = _color[1] = _color[2] = 0.0;
830   _color[3] = 1.0;
831 }
832
833 FGTextLayer::~FGTextLayer ()
834 {
835   chunk_list::iterator it = _chunks.begin();
836   chunk_list::iterator last = _chunks.end();
837   for ( ; it != last; it++) {
838     delete *it;
839   }
840 }
841
842 void
843 FGTextLayer::draw ()
844 {
845   if (test()) {
846     glPushMatrix();
847     glColor4fv(_color);
848     transform();
849     text_renderer.setFont(guiFntHandle);
850     text_renderer.setPointSize(_pointSize);
851     text_renderer.begin();
852     text_renderer.start3f(0, 0, 0);
853
854     _now.stamp();
855     if (_now - _then > 100000) {
856       recalc_value();
857       _then = _now;
858     }
859     text_renderer.puts((char *)(_value.c_str()));
860
861     text_renderer.end();
862     glColor4f(1.0, 1.0, 1.0, 1.0);      // FIXME
863     glPopMatrix();
864   }
865 }
866
867 void
868 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
869 {
870   _chunks.push_back(chunk);
871 }
872
873 void
874 FGTextLayer::setColor (float r, float g, float b)
875 {
876   _color[0] = r;
877   _color[1] = g;
878   _color[2] = b;
879   _color[3] = 1.0;
880 }
881
882 void
883 FGTextLayer::setPointSize (float size)
884 {
885   _pointSize = size;
886 }
887
888 void
889 FGTextLayer::setFont(fntFont * font)
890 {
891   text_renderer.setFont(font);
892 }
893
894
895 void
896 FGTextLayer::recalc_value () const
897 {
898   _value = "";
899   chunk_list::const_iterator it = _chunks.begin();
900   chunk_list::const_iterator last = _chunks.end();
901   for ( ; it != last; it++) {
902     _value += (*it)->getValue();
903   }
904 }
905
906
907 \f
908 ////////////////////////////////////////////////////////////////////////
909 // Implementation of FGTextLayer::Chunk.
910 ////////////////////////////////////////////////////////////////////////
911
912 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
913   : _type(FGTextLayer::TEXT), _fmt(fmt)
914 {
915   _text = text;
916   if (_fmt == "") 
917     _fmt = "%s";
918 }
919
920 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
921                            const string &fmt, float mult)
922   : _type(type), _fmt(fmt), _mult(mult)
923 {
924   if (_fmt == "") {
925     if (type == TEXT_VALUE)
926       _fmt = "%s";
927     else
928       _fmt = "%.2f";
929   }
930   _node = node;
931 }
932
933 const char *
934 FGTextLayer::Chunk::getValue () const
935 {
936   if (test()) {
937     switch (_type) {
938     case TEXT:
939       sprintf(_buf, _fmt.c_str(), _text.c_str());
940       return _buf;
941     case TEXT_VALUE:
942       sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
943       break;
944     case DOUBLE_VALUE:
945       sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
946       break;
947     }
948     return _buf;
949   } else {
950     return "";
951   }
952 }
953
954
955 \f
956 ////////////////////////////////////////////////////////////////////////
957 // Implementation of FGSwitchLayer.
958 ////////////////////////////////////////////////////////////////////////
959
960 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
961                               FGInstrumentLayer * layer1,
962                               FGInstrumentLayer * layer2)
963   : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
964 {
965 }
966
967 FGSwitchLayer::~FGSwitchLayer ()
968 {
969   delete _layer1;
970   delete _layer2;
971 }
972
973 void
974 FGSwitchLayer::draw ()
975 {
976   if (test()) {
977     transform();
978     if (_node->getBoolValue()) {
979       _layer1->draw();
980     } else {
981       _layer2->draw();
982     }
983   }
984 }
985
986 \f
987 // end of panel.cxx