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.
58 ////////////////////////////////////////////////////////////////////////
60 ////////////////////////////////////////////////////////////////////////
64 * Calculate the aspect adjustment for the panel.
67 get_aspect_adjust (int xsize, int ysize)
69 float ideal_aspect = float(WIN_W) / float(WIN_H);
70 float real_aspect = float(xsize) / float(ysize);
71 return (real_aspect / ideal_aspect);
76 ////////////////////////////////////////////////////////////////////////
78 ////////////////////////////////////////////////////////////////////////
83 if(current_panel == 0)
85 if(current_panel->getVisibility() == 0)
87 if(globals->get_viewmgr()->get_current() != 0)
89 if(globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS != 0)
96 ////////////////////////////////////////////////////////////////////////
97 // Implementation of FGTextureManager.
98 ////////////////////////////////////////////////////////////////////////
100 map<string,ssgTexture *> FGTextureManager::_textureMap;
103 FGTextureManager::createTexture (const string &relativePath)
105 ssgTexture * texture = _textureMap[relativePath];
107 SG_LOG( SG_COCKPIT, SG_DEBUG,
108 "Texture " << relativePath << " does not yet exist" );
109 SGPath tpath(globals->get_fg_root());
110 tpath.append(relativePath);
111 texture = new ssgTexture((char *)tpath.c_str(), false, false);
112 _textureMap[relativePath] = texture;
113 if (_textureMap[relativePath] == 0)
114 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
115 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
116 << " handle=" << texture->getHandle() );
125 ////////////////////////////////////////////////////////////////////////
126 // Implementation of FGCropped Texture.
127 ////////////////////////////////////////////////////////////////////////
130 FGCroppedTexture::FGCroppedTexture ()
131 : _path(""), _texture(0),
132 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
137 FGCroppedTexture::FGCroppedTexture (const string &path,
138 float minX, float minY,
139 float maxX, float maxY)
140 : _path(path), _texture(0),
141 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
146 FGCroppedTexture::~FGCroppedTexture ()
152 FGCroppedTexture::getTexture ()
155 _texture = FGTextureManager::createTexture(_path);
162 ////////////////////////////////////////////////////////////////////////
163 // Implementation of FGPanel.
164 ////////////////////////////////////////////////////////////////////////
166 FGPanel * current_panel = NULL;
167 static fntRenderer text_renderer;
168 static fntTexFont *default_font;
169 static fntTexFont *led_font;
177 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
178 _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
180 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
181 _ysize_node(fgGetNode("/sim/startup/ysize", true))
183 setVisibility(fgPanelVisible());
192 for (instrument_list_type::iterator it = _instruments.begin();
193 it != _instruments.end();
202 * Add an instrument to the panel.
205 FGPanel::addInstrument (FGPanelInstrument * instrument)
207 _instruments.push_back(instrument);
212 * Initialize the panel.
218 char* envp = ::getenv( "FG_FONTS" );
219 if ( envp != NULL ) {
220 base_path.set( envp );
222 base_path.set( globals->get_fg_root() );
223 base_path.append( "Fonts" );
228 // Install the default font
230 fntpath.append( "typewriter.txf" );
231 default_font = new fntTexFont ;
232 default_font -> load ( (char *)fntpath.c_str() ) ;
234 // Install the LED font
236 fntpath.append( "led.txf" );
237 led_font = new fntTexFont ;
238 led_font -> load ( (char *)fntpath.c_str() ) ;
243 * Bind panel properties.
248 fgSetArchivable("/sim/panel/visibility");
249 fgSetArchivable("/sim/panel/x-offset");
250 fgSetArchivable("/sim/panel/y-offset");
251 fgSetArchivable("/sim/panel/jitter");
256 * Unbind panel properties.
268 FGPanel::update (double dt)
270 // TODO: cache the nodes
271 _visibility = fgGetBool("/sim/panel/visibility");
272 _x_offset = fgGetInt("/sim/panel/x-offset");
273 _y_offset = fgGetInt("/sim/panel/y-offset");
274 _jitter = fgGetFloat("/sim/panel/jitter");
275 _flipx = fgGetBool("/sim/panel/flip-x");
277 // Do nothing if the panel isn't visible.
278 if ( !fgPanelVisible() ) {
282 // If the mouse is down, do something
285 if (_mouseDelay < 0) {
286 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
291 // Now, draw the panel
292 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
293 _ysize_node->getIntValue());
294 if (aspect_adjust <1.0)
295 update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
297 update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
302 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
304 // Calculate accelerations
305 // and jiggle the panel accordingly
306 // The factors and bounds are just
307 // initial guesses; using sqrt smooths
309 double x_offset = _x_offset;
310 double y_offset = _y_offset;
313 if (_jitter != 0.0) {
314 double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
315 double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
316 double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
318 double a_zx_pilot = a_z_pilot - a_x_pilot;
320 int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
321 (a_y_pilot < 0 ? -1 : 1);
322 int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
323 (a_zx_pilot < 0 ? -1 : 1);
325 // adjustments in screen coordinates
326 x_offset += x_adjust;
327 y_offset += y_adjust;
331 glMatrixMode(GL_PROJECTION);
335 gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
337 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
340 glMatrixMode(GL_MODELVIEW);
344 glTranslated(x_offset, y_offset, 0);
348 glMatrixMode(GL_PROJECTION);
350 glMatrixMode(GL_MODELVIEW);
353 ssgForceBasicState();
354 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
360 // In 3D mode, it's possible that we are being drawn exactly on top
361 // of an existing polygon. Use an offset to prevent z-fighting. In
362 // 2D mode, this is a no-op.
363 glEnable(GL_POLYGON_OFFSET_FILL);
364 glPolygonOffset(0, -POFF_UNITS);
367 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT
368 | GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT );
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 // restore some original state
430 glPolygonOffset(0, 0);
431 glDisable(GL_POLYGON_OFFSET_FILL);
435 * Set the panel's visibility.
438 FGPanel::setVisibility (bool visibility)
440 _visibility = visibility;
445 * Return true if the panel is visible.
448 FGPanel::getVisibility () const
455 * Set the panel's background texture.
458 FGPanel::setBackground (ssgTexture * texture)
464 * Set the panel's multiple background textures.
467 FGPanel::setMultiBackground (ssgTexture * texture, int idx)
474 * Set the panel's x-offset.
477 FGPanel::setXOffset (int offset)
479 if (offset <= 0 && offset >= -_width + WIN_W)
485 * Set the panel's y-offset.
488 FGPanel::setYOffset (int offset)
490 if (offset <= 0 && offset >= -_height)
495 * Perform a mouse action.
498 FGPanel::doMouseAction (int button, int updown, int x, int y)
500 // FIXME: this same code appears in update()
501 int xsize = _xsize_node->getIntValue();
502 int ysize = _ysize_node->getIntValue();
503 float aspect_adjust = get_aspect_adjust(xsize, ysize);
505 // Note a released button and return
506 // cerr << "Doing mouse action\n";
509 _mouseInstrument = 0;
513 // Scale for the real window size.
514 if (aspect_adjust < 1.0) {
515 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
516 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
518 x = int(((float)x / xsize) * WIN_W);
519 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
522 // Adjust for offsets.
526 // Search for a matching instrument.
527 for (int i = 0; i < (int)_instruments.size(); i++) {
528 FGPanelInstrument *inst = _instruments[i];
529 int ix = inst->getXPos();
530 int iy = inst->getYPos();
531 int iw = inst->getWidth() / 2;
532 int ih = inst->getHeight() / 2;
533 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
536 _mouseInstrument = inst;
537 _mouseButton = button;
540 // Always do the action once.
541 return _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
549 ////////////////////////////////////////////////////////////////////////.
550 // Implementation of FGPanelAction.
551 ////////////////////////////////////////////////////////////////////////
553 FGPanelAction::FGPanelAction ()
557 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
558 : _button(button), _x(x), _y(y), _w(w), _h(h)
560 for (unsigned int i = 0; i < _bindings.size(); i++)
564 FGPanelAction::~FGPanelAction ()
569 FGPanelAction::addBinding (FGBinding * binding)
571 _bindings.push_back(binding);
575 FGPanelAction::doAction ()
578 int nBindings = _bindings.size();
579 for (int i = 0; i < nBindings; i++) {
580 _bindings[i]->fire();
587 ////////////////////////////////////////////////////////////////////////
588 // Implementation of FGPanelTransformation.
589 ////////////////////////////////////////////////////////////////////////
591 FGPanelTransformation::FGPanelTransformation ()
596 FGPanelTransformation::~FGPanelTransformation ()
603 ////////////////////////////////////////////////////////////////////////
604 // Implementation of FGPanelInstrument.
605 ////////////////////////////////////////////////////////////////////////
608 FGPanelInstrument::FGPanelInstrument ()
614 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
620 FGPanelInstrument::~FGPanelInstrument ()
622 for (action_list_type::iterator it = _actions.begin();
623 it != _actions.end();
631 FGPanelInstrument::setPosition (int x, int y)
638 FGPanelInstrument::setSize (int w, int h)
645 FGPanelInstrument::getXPos () const
651 FGPanelInstrument::getYPos () const
657 FGPanelInstrument::getWidth () const
663 FGPanelInstrument::getHeight () const
669 FGPanelInstrument::addAction (FGPanelAction * action)
671 _actions.push_back(action);
674 // Coordinates relative to centre.
676 FGPanelInstrument::doMouseAction (int button, int x, int y)
679 action_list_type::iterator it = _actions.begin();
680 action_list_type::iterator last = _actions.end();
681 for ( ; it != last; it++) {
682 if ((*it)->inArea(button, x, y)) {
693 ////////////////////////////////////////////////////////////////////////
694 // Implementation of FGLayeredInstrument.
695 ////////////////////////////////////////////////////////////////////////
697 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
698 : FGPanelInstrument(x, y, w, h)
702 FGLayeredInstrument::~FGLayeredInstrument ()
704 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
711 FGLayeredInstrument::draw ()
716 for (int i = 0; i < (int)_layers.size(); i++) {
718 glPolygonOffset(-1, -POFF_UNITS*(i+2));
725 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
727 int n = _layers.size();
728 if (layer->getWidth() == -1) {
729 layer->setWidth(getWidth());
731 if (layer->getHeight() == -1) {
732 layer->setHeight(getHeight());
734 _layers.push_back(layer);
739 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
742 return addLayer(new FGTexturedLayer(texture, w, h));
746 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
748 int layer = _layers.size() - 1;
749 _layers[layer]->addTransformation(transformation);
754 ////////////////////////////////////////////////////////////////////////
755 // Implementation of FGInstrumentLayer.
756 ////////////////////////////////////////////////////////////////////////
758 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
764 FGInstrumentLayer::~FGInstrumentLayer ()
766 for (transformation_list::iterator it = _transformations.begin();
767 it != _transformations.end();
775 FGInstrumentLayer::transform () const
777 transformation_list::const_iterator it = _transformations.begin();
778 transformation_list::const_iterator last = _transformations.end();
780 FGPanelTransformation *t = *it;
782 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
785 } else if (val > t->max) {
789 val = val * t->factor + t->offset;
791 val = t->table->interpolate(val) * t->factor + t->offset;
795 case FGPanelTransformation::XSHIFT:
796 glTranslatef(val, 0.0, 0.0);
798 case FGPanelTransformation::YSHIFT:
799 glTranslatef(0.0, val, 0.0);
801 case FGPanelTransformation::ROTATION:
802 glRotatef(-val, 0.0, 0.0, 1.0);
811 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
813 _transformations.push_back(transformation);
818 ////////////////////////////////////////////////////////////////////////
819 // Implementation of FGGroupLayer.
820 ////////////////////////////////////////////////////////////////////////
822 FGGroupLayer::FGGroupLayer ()
826 FGGroupLayer::~FGGroupLayer ()
828 for (unsigned int i = 0; i < _layers.size(); i++)
833 FGGroupLayer::draw ()
836 int nLayers = _layers.size();
837 for (int i = 0; i < nLayers; i++)
843 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
845 _layers.push_back(layer);
850 ////////////////////////////////////////////////////////////////////////
851 // Implementation of FGTexturedLayer.
852 ////////////////////////////////////////////////////////////////////////
855 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
856 : FGInstrumentLayer(w, h)
862 FGTexturedLayer::~FGTexturedLayer ()
868 FGTexturedLayer::draw ()
875 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
878 // From Curt: turn on the panel
879 // lights after sundown.
880 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
881 glColor4fv( cur_light_params.scene_diffuse );
883 glColor4f(0.7, 0.2, 0.2, 1.0);
887 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
888 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
889 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
890 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
897 ////////////////////////////////////////////////////////////////////////
898 // Implementation of FGTextLayer.
899 ////////////////////////////////////////////////////////////////////////
901 FGTextLayer::FGTextLayer (int w, int h)
902 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("default")
905 _color[0] = _color[1] = _color[2] = 0.0;
909 FGTextLayer::~FGTextLayer ()
911 chunk_list::iterator it = _chunks.begin();
912 chunk_list::iterator last = _chunks.end();
913 for ( ; it != last; it++) {
924 if ( _font_name == "led" ) {
925 text_renderer.setFont(led_font);
927 text_renderer.setFont(guiFntHandle);
929 text_renderer.setPointSize(_pointSize);
930 text_renderer.begin();
931 text_renderer.start3f(0, 0, 0);
934 long diff = _now - _then;
936 if (diff > 100000 || diff < 0 ) {
937 // ( diff < 0 ) is a sanity check and indicates our time stamp
938 // difference math probably overflowed. We can handle a max
939 // difference of 35.8 minutes since the returned value is in
940 // usec. So if the panel is left off longer than that we can
941 // over flow the math with it is turned back on. This (diff <
942 // 0) catches that situation, get's us out of trouble, and
948 // Something is goofy. The code in this file renders only CCW
949 // polygons, and I have verified that the font code in plib
950 // renders only CCW trianbles. Yet they come out backwards.
951 // Something around here or in plib is either changing the winding
952 // order or (more likely) pushing a left-handed matrix onto the
953 // stack. But I can't find it; get out the chainsaw...
955 text_renderer.puts((char *)(_value.c_str()));
959 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
964 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
966 _chunks.push_back(chunk);
970 FGTextLayer::setColor (float r, float g, float b)
979 FGTextLayer::setPointSize (float size)
985 FGTextLayer::setFontName(const string &name)
992 FGTextLayer::setFont(fntFont * font)
994 text_renderer.setFont(font);
999 FGTextLayer::recalc_value () const
1002 chunk_list::const_iterator it = _chunks.begin();
1003 chunk_list::const_iterator last = _chunks.end();
1004 for ( ; it != last; it++) {
1005 _value += (*it)->getValue();
1011 ////////////////////////////////////////////////////////////////////////
1012 // Implementation of FGTextLayer::Chunk.
1013 ////////////////////////////////////////////////////////////////////////
1015 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
1016 : _type(FGTextLayer::TEXT), _fmt(fmt)
1023 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1024 const string &fmt, float mult)
1025 : _type(type), _fmt(fmt), _mult(mult)
1028 if (type == TEXT_VALUE)
1037 FGTextLayer::Chunk::getValue () const
1043 sprintf(_buf, _fmt.c_str(), _text.c_str());
1046 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1049 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
1060 ////////////////////////////////////////////////////////////////////////
1061 // Implementation of FGSwitchLayer.
1062 ////////////////////////////////////////////////////////////////////////
1064 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
1065 FGInstrumentLayer * layer1,
1066 FGInstrumentLayer * layer2)
1067 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
1071 FGSwitchLayer::~FGSwitchLayer ()
1078 FGSwitchLayer::draw ()
1082 if (_node->getBoolValue()) {