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.
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sg_path.hxx>
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>
52 #pragma warn A sloppy coder has defined NONE as a macro!!!
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 return ((current_panel != 0) &&
84 (current_panel->getVisibility()) &&
85 (globals->get_viewmgr()->get_current() == 0) &&
86 (globals->get_current_view()->get_view_offset() == 0.0));
91 ////////////////////////////////////////////////////////////////////////
92 // Implementation of FGTextureManager.
93 ////////////////////////////////////////////////////////////////////////
95 map<string,ssgTexture *> FGTextureManager::_textureMap;
98 FGTextureManager::createTexture (const string &relativePath)
100 ssgTexture * texture = _textureMap[relativePath];
102 SG_LOG( SG_COCKPIT, SG_DEBUG,
103 "Texture " << relativePath << " does not yet exist" );
104 SGPath tpath(globals->get_fg_root());
105 tpath.append(relativePath);
106 texture = new ssgTexture((char *)tpath.c_str(), false, false);
107 _textureMap[relativePath] = texture;
108 if (_textureMap[relativePath] == 0)
109 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
110 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
111 << " handle=" << texture->getHandle() );
120 ////////////////////////////////////////////////////////////////////////
121 // Implementation of FGCropped Texture.
122 ////////////////////////////////////////////////////////////////////////
125 FGCroppedTexture::FGCroppedTexture ()
126 : _path(""), _texture(0),
127 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
132 FGCroppedTexture::FGCroppedTexture (const string &path,
133 float minX, float minY,
134 float maxX, float maxY)
135 : _path(path), _texture(0),
136 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
141 FGCroppedTexture::~FGCroppedTexture ()
147 FGCroppedTexture::getTexture ()
150 _texture = FGTextureManager::createTexture(_path);
157 ////////////////////////////////////////////////////////////////////////
158 // Implementation of FGPanel.
159 ////////////////////////////////////////////////////////////////////////
161 FGPanel * current_panel = NULL;
162 static fntRenderer text_renderer;
171 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
172 _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
174 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
175 _ysize_node(fgGetNode("/sim/startup/ysize", true))
177 setVisibility(fgPanelVisible());
188 for (instrument_list_type::iterator it = _instruments.begin();
189 it != _instruments.end();
198 * Add an instrument to the panel.
201 FGPanel::addInstrument (FGPanelInstrument * instrument)
203 _instruments.push_back(instrument);
208 * Initialize the panel.
218 * Bind panel properties.
223 fgTie("/sim/panel/visibility", &_visibility);
224 fgSetArchivable("/sim/panel/visibility");
225 fgTie("/sim/panel/x-offset", &_x_offset);
226 fgSetArchivable("/sim/panel/x-offset");
227 fgTie("/sim/panel/y-offset", &_y_offset);
228 fgSetArchivable("/sim/panel/y-offset");
234 * Unbind panel properties.
239 fgUntie("/sim/panel/visibility");
240 fgUntie("/sim/panel/x-offset");
241 fgUntie("/sim/panel/y-offset");
252 // Do nothing if the panel isn't visible.
253 if (!fgPanelVisible())
256 // If the mouse is down, do something
259 if (_mouseDelay < 0) {
260 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
265 // Now, draw the panel
266 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
267 _ysize_node->getIntValue());
268 if (aspect_adjust <1.0)
269 update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
271 update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
276 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
278 glMatrixMode(GL_PROJECTION);
281 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
282 // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
284 glMatrixMode(GL_MODELVIEW);
288 glTranslated(_x_offset, _y_offset, 0);
290 // Draw the background
291 glEnable(GL_TEXTURE_2D);
292 glDisable(GL_LIGHTING);
294 glEnable(GL_ALPHA_TEST);
295 glEnable(GL_COLOR_MATERIAL);
296 // glColor4f(1.0, 1.0, 1.0, 1.0);
297 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
298 glColor4fv( cur_light_params.scene_diffuse );
300 glColor4f(0.7, 0.2, 0.2, 1.0);
302 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
303 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
304 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
305 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
307 glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
308 glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
309 glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
310 glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
313 // Draw the instruments.
314 instrument_list_type::const_iterator current = _instruments.begin();
315 instrument_list_type::const_iterator end = _instruments.end();
317 for ( ; current != end; current++) {
318 FGPanelInstrument * instr = *current;
320 glTranslated(_x_offset, _y_offset, 0);
321 glTranslated(instr->getXPos(), instr->getYPos(), 0);
325 glMatrixMode(GL_PROJECTION);
327 glMatrixMode(GL_MODELVIEW);
329 ssgForceBasicState();
330 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
335 * Set the panel's visibility.
338 FGPanel::setVisibility (bool visibility)
340 _visibility = visibility;
345 * Return true if the panel is visible.
348 FGPanel::getVisibility () const
355 * Set the panel's background texture.
358 FGPanel::setBackground (ssgTexture * texture)
365 * Set the panel's x-offset.
368 FGPanel::setXOffset (int offset)
370 if (offset <= 0 && offset >= -_width + WIN_W)
376 * Set the panel's y-offset.
379 FGPanel::setYOffset (int offset)
381 if (offset <= 0 && offset >= -_height)
387 * Perform a mouse action.
390 FGPanel::doMouseAction (int button, int updown, int x, int y)
392 // FIXME: this same code appears in update()
393 int xsize = _xsize_node->getIntValue();
394 int ysize = _ysize_node->getIntValue();
395 float aspect_adjust = get_aspect_adjust(xsize, ysize);
397 // Note a released button and return
398 // cerr << "Doing mouse action\n";
401 _mouseInstrument = 0;
405 // Scale for the real window size.
406 if (aspect_adjust < 1.0) {
407 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
408 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
410 x = int(((float)x / xsize) * WIN_W);
411 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
414 // Adjust for offsets.
418 // Search for a matching instrument.
419 for (int i = 0; i < (int)_instruments.size(); i++) {
420 FGPanelInstrument *inst = _instruments[i];
421 int ix = inst->getXPos();
422 int iy = inst->getYPos();
423 int iw = inst->getWidth() / 2;
424 int ih = inst->getHeight() / 2;
425 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
428 _mouseInstrument = inst;
429 _mouseButton = button;
432 // Always do the action once.
433 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
442 ////////////////////////////////////////////////////////////////////////.
443 // Implementation of FGPanelAction.
444 ////////////////////////////////////////////////////////////////////////
446 FGPanelAction::FGPanelAction ()
450 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
451 : _button(button), _x(x), _y(y), _w(w), _h(h)
455 FGPanelAction::~FGPanelAction ()
460 FGPanelAction::addBinding (const FGBinding &binding)
462 _bindings.push_back(binding);
466 FGPanelAction::doAction ()
469 int nBindings = _bindings.size();
470 for (int i = 0; i < nBindings; i++) {
478 ////////////////////////////////////////////////////////////////////////
479 // Implementation of FGPanelTransformation.
480 ////////////////////////////////////////////////////////////////////////
482 FGPanelTransformation::FGPanelTransformation ()
487 FGPanelTransformation::~FGPanelTransformation ()
493 ////////////////////////////////////////////////////////////////////////
494 // Implementation of FGPanelInstrument.
495 ////////////////////////////////////////////////////////////////////////
498 FGPanelInstrument::FGPanelInstrument ()
504 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
510 FGPanelInstrument::~FGPanelInstrument ()
512 for (action_list_type::iterator it = _actions.begin();
513 it != _actions.end();
521 FGPanelInstrument::setPosition (int x, int y)
528 FGPanelInstrument::setSize (int w, int h)
535 FGPanelInstrument::getXPos () const
541 FGPanelInstrument::getYPos () const
547 FGPanelInstrument::getWidth () const
553 FGPanelInstrument::getHeight () const
559 FGPanelInstrument::addAction (FGPanelAction * action)
561 _actions.push_back(action);
564 // Coordinates relative to centre.
566 FGPanelInstrument::doMouseAction (int button, int x, int y)
569 action_list_type::iterator it = _actions.begin();
570 action_list_type::iterator last = _actions.end();
571 for ( ; it != last; it++) {
572 if ((*it)->inArea(button, x, y)) {
583 ////////////////////////////////////////////////////////////////////////
584 // Implementation of FGLayeredInstrument.
585 ////////////////////////////////////////////////////////////////////////
587 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
588 : FGPanelInstrument(x, y, w, h)
592 FGLayeredInstrument::~FGLayeredInstrument ()
594 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
601 FGLayeredInstrument::draw ()
604 for (int i = 0; i < (int)_layers.size(); i++) {
606 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
614 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
616 int n = _layers.size();
617 if (layer->getWidth() == -1) {
618 layer->setWidth(getWidth());
620 if (layer->getHeight() == -1) {
621 layer->setHeight(getHeight());
623 _layers.push_back(layer);
628 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
631 return addLayer(new FGTexturedLayer(texture, w, h));
635 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
637 int layer = _layers.size() - 1;
638 _layers[layer]->addTransformation(transformation);
643 ////////////////////////////////////////////////////////////////////////
644 // Implementation of FGInstrumentLayer.
645 ////////////////////////////////////////////////////////////////////////
647 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
653 FGInstrumentLayer::~FGInstrumentLayer ()
655 for (transformation_list::iterator it = _transformations.begin();
656 it != _transformations.end();
664 FGInstrumentLayer::transform () const
666 transformation_list::const_iterator it = _transformations.begin();
667 transformation_list::const_iterator last = _transformations.end();
669 FGPanelTransformation *t = *it;
671 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
674 } else if (val > t->max) {
678 val = val * t->factor + t->offset;
680 val = t->table->interpolate(val) * t->factor + t->offset;
684 case FGPanelTransformation::XSHIFT:
685 glTranslatef(val, 0.0, 0.0);
687 case FGPanelTransformation::YSHIFT:
688 glTranslatef(0.0, val, 0.0);
690 case FGPanelTransformation::ROTATION:
691 glRotatef(-val, 0.0, 0.0, 1.0);
700 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
702 _transformations.push_back(transformation);
707 ////////////////////////////////////////////////////////////////////////
708 // Implementation of FGGroupLayer.
709 ////////////////////////////////////////////////////////////////////////
711 FGGroupLayer::FGGroupLayer ()
715 FGGroupLayer::~FGGroupLayer ()
717 for (int i = 0; i < _layers.size(); i++)
722 FGGroupLayer::draw ()
725 int nLayers = _layers.size();
726 for (int i = 0; i < nLayers; i++)
732 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
734 _layers.push_back(layer);
739 ////////////////////////////////////////////////////////////////////////
740 // Implementation of FGTexturedLayer.
741 ////////////////////////////////////////////////////////////////////////
744 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
745 : FGInstrumentLayer(w, h)
751 FGTexturedLayer::~FGTexturedLayer ()
757 FGTexturedLayer::draw ()
764 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
767 // From Curt: turn on the panel
768 // lights after sundown.
769 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
770 glColor4fv( cur_light_params.scene_diffuse );
772 glColor4f(0.7, 0.2, 0.2, 1.0);
776 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
777 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
778 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
779 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
786 ////////////////////////////////////////////////////////////////////////
787 // Implementation of FGTextLayer.
788 ////////////////////////////////////////////////////////////////////////
790 FGTextLayer::FGTextLayer (int w, int h)
791 : FGInstrumentLayer(w, h), _pointSize(14.0)
794 _color[0] = _color[1] = _color[2] = 0.0;
798 FGTextLayer::~FGTextLayer ()
800 chunk_list::iterator it = _chunks.begin();
801 chunk_list::iterator last = _chunks.end();
802 for ( ; it != last; it++) {
814 text_renderer.setFont(guiFntHandle);
815 text_renderer.setPointSize(_pointSize);
816 text_renderer.begin();
817 text_renderer.start3f(0, 0, 0);
820 if (_now - _then > 100000) {
824 text_renderer.puts((char *)(_value.c_str()));
827 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
833 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
835 _chunks.push_back(chunk);
839 FGTextLayer::setColor (float r, float g, float b)
848 FGTextLayer::setPointSize (float size)
854 FGTextLayer::setFont(fntFont * font)
856 text_renderer.setFont(font);
861 FGTextLayer::recalc_value () const
864 chunk_list::const_iterator it = _chunks.begin();
865 chunk_list::const_iterator last = _chunks.end();
866 for ( ; it != last; it++) {
867 _value += (*it)->getValue();
873 ////////////////////////////////////////////////////////////////////////
874 // Implementation of FGTextLayer::Chunk.
875 ////////////////////////////////////////////////////////////////////////
877 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
878 : _type(FGTextLayer::TEXT), _fmt(fmt)
885 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
886 const string &fmt, float mult)
887 : _type(type), _fmt(fmt), _mult(mult)
890 if (type == TEXT_VALUE)
899 FGTextLayer::Chunk::getValue () const
904 sprintf(_buf, _fmt.c_str(), _text.c_str());
907 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
910 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
921 ////////////////////////////////////////////////////////////////////////
922 // Implementation of FGSwitchLayer.
923 ////////////////////////////////////////////////////////////////////////
925 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
926 FGInstrumentLayer * layer1,
927 FGInstrumentLayer * layer2)
928 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
932 FGSwitchLayer::~FGSwitchLayer ()
939 FGSwitchLayer::draw ()
943 if (_node->getBoolValue()) {