1 // panel.cxx - default, 2D single-engine prop instrument panel
3 // Written by David Megginson, started January 2000.
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.
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.
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.
29 #include <stdio.h> // sprintf
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/misc/sg_path.hxx>
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>
52 // The number of polygon-offset "units" to place between layers. In
53 // principle, one is supposed to be enough. In practice, I find that
54 // my hardware/driver requires many more.
57 #if defined( NONE ) && defined( _MSC_VER )
58 # pragma message( "A sloppy coder has defined NONE as a macro!!!" )
61 # pragma warn A sloppy coder has defined NONE as a macro!!!
66 ////////////////////////////////////////////////////////////////////////
68 ////////////////////////////////////////////////////////////////////////
72 * Calculate the aspect adjustment for the panel.
75 get_aspect_adjust (int xsize, int ysize)
77 float ideal_aspect = float(WIN_W) / float(WIN_H);
78 float real_aspect = float(xsize) / float(ysize);
79 return (real_aspect / ideal_aspect);
84 ////////////////////////////////////////////////////////////////////////
86 ////////////////////////////////////////////////////////////////////////
91 if(current_panel == 0)
93 if(current_panel->getVisibility() == 0)
95 if(globals->get_viewmgr()->get_current() != 0)
97 if(globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS != 0)
104 ////////////////////////////////////////////////////////////////////////
105 // Implementation of FGTextureManager.
106 ////////////////////////////////////////////////////////////////////////
108 map<string,ssgTexture *> FGTextureManager::_textureMap;
111 FGTextureManager::createTexture (const string &relativePath)
113 ssgTexture * texture = _textureMap[relativePath];
115 SG_LOG( SG_COCKPIT, SG_DEBUG,
116 "Texture " << relativePath << " does not yet exist" );
117 SGPath tpath(globals->get_fg_root());
118 tpath.append(relativePath);
119 texture = new ssgTexture((char *)tpath.c_str(), false, false);
120 _textureMap[relativePath] = texture;
121 if (_textureMap[relativePath] == 0)
122 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
123 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
124 << " handle=" << texture->getHandle() );
133 ////////////////////////////////////////////////////////////////////////
134 // Implementation of FGCropped Texture.
135 ////////////////////////////////////////////////////////////////////////
138 FGCroppedTexture::FGCroppedTexture ()
139 : _path(""), _texture(0),
140 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
145 FGCroppedTexture::FGCroppedTexture (const string &path,
146 float minX, float minY,
147 float maxX, float maxY)
148 : _path(path), _texture(0),
149 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
154 FGCroppedTexture::~FGCroppedTexture ()
160 FGCroppedTexture::getTexture ()
163 _texture = FGTextureManager::createTexture(_path);
170 ////////////////////////////////////////////////////////////////////////
171 // Implementation of FGPanel.
172 ////////////////////////////////////////////////////////////////////////
174 FGPanel * current_panel = NULL;
175 static fntRenderer text_renderer;
176 static fntTexFont *default_font;
177 static fntTexFont *led_font;
185 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
186 _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
188 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
189 _ysize_node(fgGetNode("/sim/startup/ysize", true))
191 setVisibility(fgPanelVisible());
200 for (instrument_list_type::iterator it = _instruments.begin();
201 it != _instruments.end();
210 * Add an instrument to the panel.
213 FGPanel::addInstrument (FGPanelInstrument * instrument)
215 _instruments.push_back(instrument);
220 * Initialize the panel.
226 char* envp = ::getenv( "FG_FONTS" );
227 if ( envp != NULL ) {
228 base_path.set( envp );
230 base_path.set( globals->get_fg_root() );
231 base_path.append( "Fonts" );
236 // Install the default font
238 fntpath.append( "typewriter.txf" );
239 default_font = new fntTexFont ;
240 default_font -> load ( (char *)fntpath.c_str() ) ;
242 // Install the LED font
244 fntpath.append( "led.txf" );
245 led_font = new fntTexFont ;
246 led_font -> load ( (char *)fntpath.c_str() ) ;
251 * Bind panel properties.
256 fgSetArchivable("/sim/panel/visibility");
257 fgSetArchivable("/sim/panel/x-offset");
258 fgSetArchivable("/sim/panel/y-offset");
259 fgSetArchivable("/sim/panel/jitter");
264 * Unbind panel properties.
276 FGPanel::update (double dt)
278 // TODO: cache the nodes
279 _visibility = fgGetBool("/sim/panel/visibility");
280 _x_offset = fgGetInt("/sim/panel/x-offset");
281 _y_offset = fgGetInt("/sim/panel/y-offset");
282 _jitter = fgGetFloat("/sim/panel/jitter");
284 // Do nothing if the panel isn't visible.
285 if ( !fgPanelVisible() ) {
289 // If the mouse is down, do something
292 if (_mouseDelay < 0) {
293 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
298 // Now, draw the panel
299 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
300 _ysize_node->getIntValue());
301 if (aspect_adjust <1.0)
302 update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
304 update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
309 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
311 // Calculate accelerations
312 // and jiggle the panel accordingly
313 // The factors and bounds are just
314 // initial guesses; using sqrt smooths
316 double x_offset = _x_offset;
317 double y_offset = _y_offset;
320 if (_jitter != 0.0) {
321 double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
322 double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
323 double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
325 double a_zx_pilot = a_z_pilot - a_x_pilot;
327 int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
328 (a_y_pilot < 0 ? -1 : 1);
329 int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
330 (a_zx_pilot < 0 ? -1 : 1);
332 // adjustments in screen coordinates
333 x_offset += x_adjust;
334 y_offset += y_adjust;
338 glMatrixMode(GL_PROJECTION);
341 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
342 // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
344 glMatrixMode(GL_MODELVIEW);
348 glTranslated(x_offset, y_offset, 0);
352 glMatrixMode(GL_PROJECTION);
354 glMatrixMode(GL_MODELVIEW);
357 ssgForceBasicState();
358 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
364 // In 3D mode, it's possible that we are being drawn exactly on top
365 // of an existing polygon. Use an offset to prevent z-fighting. In
366 // 2D mode, this is a no-op.
367 glEnable(GL_POLYGON_OFFSET_FILL);
368 glPolygonOffset(0, -POFF_UNITS);
370 // Draw the background
371 glEnable(GL_TEXTURE_2D);
372 glDisable(GL_LIGHTING);
374 glEnable(GL_ALPHA_TEST);
375 glEnable(GL_COLOR_MATERIAL);
376 // glColor4f(1.0, 1.0, 1.0, 1.0);
377 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
378 glColor4fv( cur_light_params.scene_diffuse );
380 glColor4f(0.7, 0.2, 0.2, 1.0);
383 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
384 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
385 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
386 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
388 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
389 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
390 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
391 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
394 for (int i = 0; i < 4; i ++) {
395 // top row of textures...(1,3,5,7)
396 glBindTexture(GL_TEXTURE_2D, _mbg[i*2]->getHandle());
397 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
399 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
400 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
401 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
402 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
404 // bottom row of textures...(2,4,6,8)
405 glBindTexture(GL_TEXTURE_2D, _mbg[(i*2)+1]->getHandle());
406 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
408 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
409 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
410 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
411 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
416 // Draw the instruments.
417 instrument_list_type::const_iterator current = _instruments.begin();
418 instrument_list_type::const_iterator end = _instruments.end();
420 for ( ; current != end; current++) {
421 FGPanelInstrument * instr = *current;
423 glTranslated(instr->getXPos(), instr->getYPos(), 0);
428 glDisable(GL_POLYGON_OFFSET_FILL);
432 * Set the panel's visibility.
435 FGPanel::setVisibility (bool visibility)
437 _visibility = visibility;
442 * Return true if the panel is visible.
445 FGPanel::getVisibility () const
452 * Set the panel's background texture.
455 FGPanel::setBackground (ssgTexture * texture)
461 * Set the panel's multiple background textures.
464 FGPanel::setMultiBackground (ssgTexture * texture, int idx)
471 * Set the panel's x-offset.
474 FGPanel::setXOffset (int offset)
476 if (offset <= 0 && offset >= -_width + WIN_W)
482 * Set the panel's y-offset.
485 FGPanel::setYOffset (int offset)
487 if (offset <= 0 && offset >= -_height)
492 * Perform a mouse action.
495 FGPanel::doMouseAction (int button, int updown, int x, int y)
497 // FIXME: this same code appears in update()
498 int xsize = _xsize_node->getIntValue();
499 int ysize = _ysize_node->getIntValue();
500 float aspect_adjust = get_aspect_adjust(xsize, ysize);
502 // Note a released button and return
503 // cerr << "Doing mouse action\n";
506 _mouseInstrument = 0;
510 // Scale for the real window size.
511 if (aspect_adjust < 1.0) {
512 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
513 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
515 x = int(((float)x / xsize) * WIN_W);
516 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
519 // Adjust for offsets.
523 // Search for a matching instrument.
524 for (int i = 0; i < (int)_instruments.size(); i++) {
525 FGPanelInstrument *inst = _instruments[i];
526 int ix = inst->getXPos();
527 int iy = inst->getYPos();
528 int iw = inst->getWidth() / 2;
529 int ih = inst->getHeight() / 2;
530 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
533 _mouseInstrument = inst;
534 _mouseButton = button;
537 // Always do the action once.
538 return _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
546 ////////////////////////////////////////////////////////////////////////.
547 // Implementation of FGPanelAction.
548 ////////////////////////////////////////////////////////////////////////
550 FGPanelAction::FGPanelAction ()
554 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
555 : _button(button), _x(x), _y(y), _w(w), _h(h)
557 for (unsigned int i = 0; i < _bindings.size(); i++)
561 FGPanelAction::~FGPanelAction ()
566 FGPanelAction::addBinding (FGBinding * binding)
568 _bindings.push_back(binding);
572 FGPanelAction::doAction ()
575 int nBindings = _bindings.size();
576 for (int i = 0; i < nBindings; i++) {
577 _bindings[i]->fire();
584 ////////////////////////////////////////////////////////////////////////
585 // Implementation of FGPanelTransformation.
586 ////////////////////////////////////////////////////////////////////////
588 FGPanelTransformation::FGPanelTransformation ()
593 FGPanelTransformation::~FGPanelTransformation ()
600 ////////////////////////////////////////////////////////////////////////
601 // Implementation of FGPanelInstrument.
602 ////////////////////////////////////////////////////////////////////////
605 FGPanelInstrument::FGPanelInstrument ()
611 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
617 FGPanelInstrument::~FGPanelInstrument ()
619 for (action_list_type::iterator it = _actions.begin();
620 it != _actions.end();
628 FGPanelInstrument::setPosition (int x, int y)
635 FGPanelInstrument::setSize (int w, int h)
642 FGPanelInstrument::getXPos () const
648 FGPanelInstrument::getYPos () const
654 FGPanelInstrument::getWidth () const
660 FGPanelInstrument::getHeight () const
666 FGPanelInstrument::addAction (FGPanelAction * action)
668 _actions.push_back(action);
671 // Coordinates relative to centre.
673 FGPanelInstrument::doMouseAction (int button, int x, int y)
676 action_list_type::iterator it = _actions.begin();
677 action_list_type::iterator last = _actions.end();
678 for ( ; it != last; it++) {
679 if ((*it)->inArea(button, x, y)) {
690 ////////////////////////////////////////////////////////////////////////
691 // Implementation of FGLayeredInstrument.
692 ////////////////////////////////////////////////////////////////////////
694 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
695 : FGPanelInstrument(x, y, w, h)
699 FGLayeredInstrument::~FGLayeredInstrument ()
701 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
708 FGLayeredInstrument::draw ()
713 for (int i = 0; i < (int)_layers.size(); i++) {
715 glPolygonOffset(-1, -POFF_UNITS*(i+2));
722 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
724 int n = _layers.size();
725 if (layer->getWidth() == -1) {
726 layer->setWidth(getWidth());
728 if (layer->getHeight() == -1) {
729 layer->setHeight(getHeight());
731 _layers.push_back(layer);
736 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
739 return addLayer(new FGTexturedLayer(texture, w, h));
743 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
745 int layer = _layers.size() - 1;
746 _layers[layer]->addTransformation(transformation);
751 ////////////////////////////////////////////////////////////////////////
752 // Implementation of FGInstrumentLayer.
753 ////////////////////////////////////////////////////////////////////////
755 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
761 FGInstrumentLayer::~FGInstrumentLayer ()
763 for (transformation_list::iterator it = _transformations.begin();
764 it != _transformations.end();
772 FGInstrumentLayer::transform () const
774 transformation_list::const_iterator it = _transformations.begin();
775 transformation_list::const_iterator last = _transformations.end();
777 FGPanelTransformation *t = *it;
779 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
782 } else if (val > t->max) {
786 val = val * t->factor + t->offset;
788 val = t->table->interpolate(val) * t->factor + t->offset;
792 case FGPanelTransformation::XSHIFT:
793 glTranslatef(val, 0.0, 0.0);
795 case FGPanelTransformation::YSHIFT:
796 glTranslatef(0.0, val, 0.0);
798 case FGPanelTransformation::ROTATION:
799 glRotatef(-val, 0.0, 0.0, 1.0);
808 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
810 _transformations.push_back(transformation);
815 ////////////////////////////////////////////////////////////////////////
816 // Implementation of FGGroupLayer.
817 ////////////////////////////////////////////////////////////////////////
819 FGGroupLayer::FGGroupLayer ()
823 FGGroupLayer::~FGGroupLayer ()
825 for (unsigned int i = 0; i < _layers.size(); i++)
830 FGGroupLayer::draw ()
833 int nLayers = _layers.size();
834 for (int i = 0; i < nLayers; i++)
840 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
842 _layers.push_back(layer);
847 ////////////////////////////////////////////////////////////////////////
848 // Implementation of FGTexturedLayer.
849 ////////////////////////////////////////////////////////////////////////
852 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
853 : FGInstrumentLayer(w, h)
859 FGTexturedLayer::~FGTexturedLayer ()
865 FGTexturedLayer::draw ()
872 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
875 // From Curt: turn on the panel
876 // lights after sundown.
877 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
878 glColor4fv( cur_light_params.scene_diffuse );
880 glColor4f(0.7, 0.2, 0.2, 1.0);
884 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
885 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
886 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
887 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
894 ////////////////////////////////////////////////////////////////////////
895 // Implementation of FGTextLayer.
896 ////////////////////////////////////////////////////////////////////////
898 FGTextLayer::FGTextLayer (int w, int h)
899 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("default")
902 _color[0] = _color[1] = _color[2] = 0.0;
906 FGTextLayer::~FGTextLayer ()
908 chunk_list::iterator it = _chunks.begin();
909 chunk_list::iterator last = _chunks.end();
910 for ( ; it != last; it++) {
921 if ( _font_name == "led" ) {
922 text_renderer.setFont(led_font);
924 text_renderer.setFont(guiFntHandle);
926 text_renderer.setPointSize(_pointSize);
927 text_renderer.begin();
928 text_renderer.start3f(0, 0, 0);
931 long diff = _now - _then;
933 if (diff > 100000 || diff < 0 ) {
934 // ( diff < 0 ) is a sanity check and indicates our time stamp
935 // difference math probably overflowed. We can handle a max
936 // difference of 35.8 minutes since the returned value is in
937 // usec. So if the panel is left off longer than that we can
938 // over flow the math with it is turned back on. This (diff <
939 // 0) catches that situation, get's us out of trouble, and
945 // Something is goofy. The code in this file renders only CCW
946 // polygons, and I have verified that the font code in plib
947 // renders only CCW trianbles. Yet they come out backwards.
948 // Something around here or in plib is either changing the winding
949 // order or (more likely) pushing a left-handed matrix onto the
950 // stack. But I can't find it; get out the chainsaw...
952 text_renderer.puts((char *)(_value.c_str()));
956 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
961 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
963 _chunks.push_back(chunk);
967 FGTextLayer::setColor (float r, float g, float b)
976 FGTextLayer::setPointSize (float size)
982 FGTextLayer::setFontName(const string &name)
989 FGTextLayer::setFont(fntFont * font)
991 text_renderer.setFont(font);
996 FGTextLayer::recalc_value () const
999 chunk_list::const_iterator it = _chunks.begin();
1000 chunk_list::const_iterator last = _chunks.end();
1001 for ( ; it != last; it++) {
1002 _value += (*it)->getValue();
1008 ////////////////////////////////////////////////////////////////////////
1009 // Implementation of FGTextLayer::Chunk.
1010 ////////////////////////////////////////////////////////////////////////
1012 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1013 : _type(FGTextLayer::TEXT), _fmt(fmt)
1020 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1021 const string &fmt, float mult)
1022 : _type(type), _fmt(fmt), _mult(mult)
1025 if (type == TEXT_VALUE)
1034 FGTextLayer::Chunk::getValue () const
1040 sprintf(_buf, _fmt.c_str(), _text.c_str());
1043 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1046 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
1057 ////////////////////////////////////////////////////////////////////////
1058 // Implementation of FGSwitchLayer.
1059 ////////////////////////////////////////////////////////////////////////
1061 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
1062 FGInstrumentLayer * layer1,
1063 FGInstrumentLayer * layer2)
1064 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
1068 FGSwitchLayer::~FGSwitchLayer ()
1075 FGSwitchLayer::draw ()
1079 if (_node->getBoolValue()) {