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 #if defined( NONE ) && defined( _MSC_VER )
53 # pragma message( "A sloppy coder has defined NONE as a macro!!!" )
56 # pragma warn A sloppy coder has defined NONE as a macro!!!
62 ////////////////////////////////////////////////////////////////////////
64 ////////////////////////////////////////////////////////////////////////
68 * Calculate the aspect adjustment for the panel.
71 get_aspect_adjust (int xsize, int ysize)
73 float ideal_aspect = float(WIN_W) / float(WIN_H);
74 float real_aspect = float(xsize) / float(ysize);
75 return (real_aspect / ideal_aspect);
80 ////////////////////////////////////////////////////////////////////////
82 ////////////////////////////////////////////////////////////////////////
87 return ((current_panel != 0) &&
88 (current_panel->getVisibility()) &&
89 (globals->get_viewmgr()->get_current() == 0) &&
90 (globals->get_current_view()->get_view_offset() == 0.0));
95 ////////////////////////////////////////////////////////////////////////
96 // Implementation of FGTextureManager.
97 ////////////////////////////////////////////////////////////////////////
99 map<string,ssgTexture *> FGTextureManager::_textureMap;
102 FGTextureManager::createTexture (const string &relativePath)
104 ssgTexture * texture = _textureMap[relativePath];
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() );
124 ////////////////////////////////////////////////////////////////////////
125 // Implementation of FGCropped Texture.
126 ////////////////////////////////////////////////////////////////////////
129 FGCroppedTexture::FGCroppedTexture ()
130 : _path(""), _texture(0),
131 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
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)
145 FGCroppedTexture::~FGCroppedTexture ()
151 FGCroppedTexture::getTexture ()
154 _texture = FGTextureManager::createTexture(_path);
161 ////////////////////////////////////////////////////////////////////////
162 // Implementation of FGPanel.
163 ////////////////////////////////////////////////////////////////////////
165 FGPanel * current_panel = NULL;
166 static fntRenderer text_renderer;
175 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
176 _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
179 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
180 _ysize_node(fgGetNode("/sim/startup/ysize", true))
182 setVisibility(fgPanelVisible());
193 for (instrument_list_type::iterator it = _instruments.begin();
194 it != _instruments.end();
203 * Add an instrument to the panel.
206 FGPanel::addInstrument (FGPanelInstrument * instrument)
208 _instruments.push_back(instrument);
213 * Initialize the panel.
223 * Bind panel properties.
228 fgTie("/sim/panel/visibility", &_visibility);
229 fgSetArchivable("/sim/panel/visibility");
230 fgTie("/sim/panel/x-offset", &_x_offset);
231 fgSetArchivable("/sim/panel/x-offset");
232 fgTie("/sim/panel/y-offset", &_y_offset);
233 fgSetArchivable("/sim/panel/y-offset");
234 fgTie("/sim/panel/jitter", &_jitter);
235 fgSetArchivable("/sim/panel/jitter");
241 * Unbind panel properties.
246 fgUntie("/sim/panel/visibility");
247 fgUntie("/sim/panel/x-offset");
248 fgUntie("/sim/panel/y-offset");
257 FGPanel::update (int dt)
259 // Do nothing if the panel isn't visible.
260 if ( !fgPanelVisible() ) {
264 // If the mouse is down, do something
267 if (_mouseDelay < 0) {
268 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
273 // Now, draw the panel
274 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
275 _ysize_node->getIntValue());
276 if (aspect_adjust <1.0)
277 update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
279 update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
284 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
286 // Calculate accelerations
287 // and jiggle the panel accordingly
288 // The factors and bounds are just
289 // initial guesses; using sqrt smooths
291 double x_offset = _x_offset;
292 double y_offset = _y_offset;
294 if (_jitter != 0.0) {
295 double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
296 double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
297 double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
299 double a_zx_pilot = a_z_pilot - a_x_pilot;
301 int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
302 (a_y_pilot < 0 ? -1 : 1);
303 int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
304 (a_zx_pilot < 0 ? -1 : 1);
306 // adjustments in screen coordinates
307 x_offset += x_adjust;
308 y_offset += y_adjust;
311 glMatrixMode(GL_PROJECTION);
314 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
315 // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
317 glMatrixMode(GL_MODELVIEW);
321 glTranslated(x_offset, y_offset, 0);
323 // Draw the background
324 glEnable(GL_TEXTURE_2D);
325 glDisable(GL_LIGHTING);
327 glEnable(GL_ALPHA_TEST);
328 glEnable(GL_COLOR_MATERIAL);
329 // glColor4f(1.0, 1.0, 1.0, 1.0);
330 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
331 glColor4fv( cur_light_params.scene_diffuse );
333 glColor4f(0.7, 0.2, 0.2, 1.0);
335 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
336 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
337 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
338 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
340 glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
341 glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
342 glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
343 glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
346 // Draw the instruments.
347 instrument_list_type::const_iterator current = _instruments.begin();
348 instrument_list_type::const_iterator end = _instruments.end();
350 for ( ; current != end; current++) {
351 FGPanelInstrument * instr = *current;
353 glTranslated(x_offset, y_offset, 0);
354 glTranslated(instr->getXPos(), instr->getYPos(), 0);
358 glMatrixMode(GL_PROJECTION);
360 glMatrixMode(GL_MODELVIEW);
362 ssgForceBasicState();
363 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
368 * Set the panel's visibility.
371 FGPanel::setVisibility (bool visibility)
373 _visibility = visibility;
378 * Return true if the panel is visible.
381 FGPanel::getVisibility () const
388 * Set the panel's background texture.
391 FGPanel::setBackground (ssgTexture * texture)
398 * Set the panel's x-offset.
401 FGPanel::setXOffset (int offset)
403 if (offset <= 0 && offset >= -_width + WIN_W)
409 * Set the panel's y-offset.
412 FGPanel::setYOffset (int offset)
414 if (offset <= 0 && offset >= -_height)
420 * Perform a mouse action.
423 FGPanel::doMouseAction (int button, int updown, int x, int y)
425 // FIXME: this same code appears in update()
426 int xsize = _xsize_node->getIntValue();
427 int ysize = _ysize_node->getIntValue();
428 float aspect_adjust = get_aspect_adjust(xsize, ysize);
430 // Note a released button and return
431 // cerr << "Doing mouse action\n";
434 _mouseInstrument = 0;
438 // Scale for the real window size.
439 if (aspect_adjust < 1.0) {
440 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
441 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
443 x = int(((float)x / xsize) * WIN_W);
444 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
447 // Adjust for offsets.
451 // Search for a matching instrument.
452 for (int i = 0; i < (int)_instruments.size(); i++) {
453 FGPanelInstrument *inst = _instruments[i];
454 int ix = inst->getXPos();
455 int iy = inst->getYPos();
456 int iw = inst->getWidth() / 2;
457 int ih = inst->getHeight() / 2;
458 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
461 _mouseInstrument = inst;
462 _mouseButton = button;
465 // Always do the action once.
466 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
475 ////////////////////////////////////////////////////////////////////////.
476 // Implementation of FGPanelAction.
477 ////////////////////////////////////////////////////////////////////////
479 FGPanelAction::FGPanelAction ()
483 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
484 : _button(button), _x(x), _y(y), _w(w), _h(h)
486 for (unsigned int i = 0; i < _bindings.size(); i++)
490 FGPanelAction::~FGPanelAction ()
495 FGPanelAction::addBinding (FGBinding * binding)
497 _bindings.push_back(binding);
501 FGPanelAction::doAction ()
504 int nBindings = _bindings.size();
505 for (int i = 0; i < nBindings; i++) {
506 _bindings[i]->fire();
513 ////////////////////////////////////////////////////////////////////////
514 // Implementation of FGPanelTransformation.
515 ////////////////////////////////////////////////////////////////////////
517 FGPanelTransformation::FGPanelTransformation ()
522 FGPanelTransformation::~FGPanelTransformation ()
529 ////////////////////////////////////////////////////////////////////////
530 // Implementation of FGPanelInstrument.
531 ////////////////////////////////////////////////////////////////////////
534 FGPanelInstrument::FGPanelInstrument ()
540 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
546 FGPanelInstrument::~FGPanelInstrument ()
548 for (action_list_type::iterator it = _actions.begin();
549 it != _actions.end();
557 FGPanelInstrument::setPosition (int x, int y)
564 FGPanelInstrument::setSize (int w, int h)
571 FGPanelInstrument::getXPos () const
577 FGPanelInstrument::getYPos () const
583 FGPanelInstrument::getWidth () const
589 FGPanelInstrument::getHeight () const
595 FGPanelInstrument::addAction (FGPanelAction * action)
597 _actions.push_back(action);
600 // Coordinates relative to centre.
602 FGPanelInstrument::doMouseAction (int button, int x, int y)
605 action_list_type::iterator it = _actions.begin();
606 action_list_type::iterator last = _actions.end();
607 for ( ; it != last; it++) {
608 if ((*it)->inArea(button, x, y)) {
619 ////////////////////////////////////////////////////////////////////////
620 // Implementation of FGLayeredInstrument.
621 ////////////////////////////////////////////////////////////////////////
623 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
624 : FGPanelInstrument(x, y, w, h)
628 FGLayeredInstrument::~FGLayeredInstrument ()
630 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
637 FGLayeredInstrument::draw ()
640 for (int i = 0; i < (int)_layers.size(); i++) {
642 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
650 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
652 int n = _layers.size();
653 if (layer->getWidth() == -1) {
654 layer->setWidth(getWidth());
656 if (layer->getHeight() == -1) {
657 layer->setHeight(getHeight());
659 _layers.push_back(layer);
664 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
667 return addLayer(new FGTexturedLayer(texture, w, h));
671 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
673 int layer = _layers.size() - 1;
674 _layers[layer]->addTransformation(transformation);
679 ////////////////////////////////////////////////////////////////////////
680 // Implementation of FGInstrumentLayer.
681 ////////////////////////////////////////////////////////////////////////
683 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
689 FGInstrumentLayer::~FGInstrumentLayer ()
691 for (transformation_list::iterator it = _transformations.begin();
692 it != _transformations.end();
700 FGInstrumentLayer::transform () const
702 transformation_list::const_iterator it = _transformations.begin();
703 transformation_list::const_iterator last = _transformations.end();
705 FGPanelTransformation *t = *it;
707 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
710 } else if (val > t->max) {
714 val = val * t->factor + t->offset;
716 val = t->table->interpolate(val) * t->factor + t->offset;
720 case FGPanelTransformation::XSHIFT:
721 glTranslatef(val, 0.0, 0.0);
723 case FGPanelTransformation::YSHIFT:
724 glTranslatef(0.0, val, 0.0);
726 case FGPanelTransformation::ROTATION:
727 glRotatef(-val, 0.0, 0.0, 1.0);
736 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
738 _transformations.push_back(transformation);
743 ////////////////////////////////////////////////////////////////////////
744 // Implementation of FGGroupLayer.
745 ////////////////////////////////////////////////////////////////////////
747 FGGroupLayer::FGGroupLayer ()
751 FGGroupLayer::~FGGroupLayer ()
753 for (unsigned int i = 0; i < _layers.size(); i++)
758 FGGroupLayer::draw ()
761 int nLayers = _layers.size();
762 for (int i = 0; i < nLayers; i++)
768 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
770 _layers.push_back(layer);
775 ////////////////////////////////////////////////////////////////////////
776 // Implementation of FGTexturedLayer.
777 ////////////////////////////////////////////////////////////////////////
780 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
781 : FGInstrumentLayer(w, h)
787 FGTexturedLayer::~FGTexturedLayer ()
793 FGTexturedLayer::draw ()
800 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
803 // From Curt: turn on the panel
804 // lights after sundown.
805 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
806 glColor4fv( cur_light_params.scene_diffuse );
808 glColor4f(0.7, 0.2, 0.2, 1.0);
812 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
813 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
814 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
815 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
822 ////////////////////////////////////////////////////////////////////////
823 // Implementation of FGTextLayer.
824 ////////////////////////////////////////////////////////////////////////
826 FGTextLayer::FGTextLayer (int w, int h)
827 : FGInstrumentLayer(w, h), _pointSize(14.0)
830 _color[0] = _color[1] = _color[2] = 0.0;
834 FGTextLayer::~FGTextLayer ()
836 chunk_list::iterator it = _chunks.begin();
837 chunk_list::iterator last = _chunks.end();
838 for ( ; it != last; it++) {
850 text_renderer.setFont(guiFntHandle);
851 text_renderer.setPointSize(_pointSize);
852 text_renderer.begin();
853 text_renderer.start3f(0, 0, 0);
856 if (_now - _then > 100000) {
860 text_renderer.puts((char *)(_value.c_str()));
863 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
869 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
871 _chunks.push_back(chunk);
875 FGTextLayer::setColor (float r, float g, float b)
884 FGTextLayer::setPointSize (float size)
890 FGTextLayer::setFont(fntFont * font)
892 text_renderer.setFont(font);
897 FGTextLayer::recalc_value () const
900 chunk_list::const_iterator it = _chunks.begin();
901 chunk_list::const_iterator last = _chunks.end();
902 for ( ; it != last; it++) {
903 _value += (*it)->getValue();
909 ////////////////////////////////////////////////////////////////////////
910 // Implementation of FGTextLayer::Chunk.
911 ////////////////////////////////////////////////////////////////////////
913 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
914 : _type(FGTextLayer::TEXT), _fmt(fmt)
921 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
922 const string &fmt, float mult)
923 : _type(type), _fmt(fmt), _mult(mult)
926 if (type == TEXT_VALUE)
935 FGTextLayer::Chunk::getValue () const
940 sprintf(_buf, _fmt.c_str(), _text.c_str());
943 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
946 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
957 ////////////////////////////////////////////////////////////////////////
958 // Implementation of FGSwitchLayer.
959 ////////////////////////////////////////////////////////////////////////
961 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
962 FGInstrumentLayer * layer1,
963 FGInstrumentLayer * layer2)
964 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
968 FGSwitchLayer::~FGSwitchLayer ()
975 FGSwitchLayer::draw ()
979 if (_node->getBoolValue()) {