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)
453 for (unsigned int i = 0; i < _bindings.size(); i++)
457 FGPanelAction::~FGPanelAction ()
462 FGPanelAction::addBinding (FGBinding * binding)
464 _bindings.push_back(binding);
468 FGPanelAction::doAction ()
471 int nBindings = _bindings.size();
472 for (int i = 0; i < nBindings; i++) {
473 _bindings[i]->fire();
480 ////////////////////////////////////////////////////////////////////////
481 // Implementation of FGPanelTransformation.
482 ////////////////////////////////////////////////////////////////////////
484 FGPanelTransformation::FGPanelTransformation ()
489 FGPanelTransformation::~FGPanelTransformation ()
495 ////////////////////////////////////////////////////////////////////////
496 // Implementation of FGPanelInstrument.
497 ////////////////////////////////////////////////////////////////////////
500 FGPanelInstrument::FGPanelInstrument ()
506 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
512 FGPanelInstrument::~FGPanelInstrument ()
514 for (action_list_type::iterator it = _actions.begin();
515 it != _actions.end();
523 FGPanelInstrument::setPosition (int x, int y)
530 FGPanelInstrument::setSize (int w, int h)
537 FGPanelInstrument::getXPos () const
543 FGPanelInstrument::getYPos () const
549 FGPanelInstrument::getWidth () const
555 FGPanelInstrument::getHeight () const
561 FGPanelInstrument::addAction (FGPanelAction * action)
563 _actions.push_back(action);
566 // Coordinates relative to centre.
568 FGPanelInstrument::doMouseAction (int button, int x, int y)
571 action_list_type::iterator it = _actions.begin();
572 action_list_type::iterator last = _actions.end();
573 for ( ; it != last; it++) {
574 if ((*it)->inArea(button, x, y)) {
585 ////////////////////////////////////////////////////////////////////////
586 // Implementation of FGLayeredInstrument.
587 ////////////////////////////////////////////////////////////////////////
589 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
590 : FGPanelInstrument(x, y, w, h)
594 FGLayeredInstrument::~FGLayeredInstrument ()
596 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
603 FGLayeredInstrument::draw ()
606 for (int i = 0; i < (int)_layers.size(); i++) {
608 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
616 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
618 int n = _layers.size();
619 if (layer->getWidth() == -1) {
620 layer->setWidth(getWidth());
622 if (layer->getHeight() == -1) {
623 layer->setHeight(getHeight());
625 _layers.push_back(layer);
630 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
633 return addLayer(new FGTexturedLayer(texture, w, h));
637 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
639 int layer = _layers.size() - 1;
640 _layers[layer]->addTransformation(transformation);
645 ////////////////////////////////////////////////////////////////////////
646 // Implementation of FGInstrumentLayer.
647 ////////////////////////////////////////////////////////////////////////
649 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
655 FGInstrumentLayer::~FGInstrumentLayer ()
657 for (transformation_list::iterator it = _transformations.begin();
658 it != _transformations.end();
666 FGInstrumentLayer::transform () const
668 transformation_list::const_iterator it = _transformations.begin();
669 transformation_list::const_iterator last = _transformations.end();
671 FGPanelTransformation *t = *it;
673 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
676 } else if (val > t->max) {
680 val = val * t->factor + t->offset;
682 val = t->table->interpolate(val) * t->factor + t->offset;
686 case FGPanelTransformation::XSHIFT:
687 glTranslatef(val, 0.0, 0.0);
689 case FGPanelTransformation::YSHIFT:
690 glTranslatef(0.0, val, 0.0);
692 case FGPanelTransformation::ROTATION:
693 glRotatef(-val, 0.0, 0.0, 1.0);
702 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
704 _transformations.push_back(transformation);
709 ////////////////////////////////////////////////////////////////////////
710 // Implementation of FGGroupLayer.
711 ////////////////////////////////////////////////////////////////////////
713 FGGroupLayer::FGGroupLayer ()
717 FGGroupLayer::~FGGroupLayer ()
719 for (unsigned int i = 0; i < _layers.size(); i++)
724 FGGroupLayer::draw ()
727 int nLayers = _layers.size();
728 for (int i = 0; i < nLayers; i++)
734 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
736 _layers.push_back(layer);
741 ////////////////////////////////////////////////////////////////////////
742 // Implementation of FGTexturedLayer.
743 ////////////////////////////////////////////////////////////////////////
746 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
747 : FGInstrumentLayer(w, h)
753 FGTexturedLayer::~FGTexturedLayer ()
759 FGTexturedLayer::draw ()
766 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
769 // From Curt: turn on the panel
770 // lights after sundown.
771 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
772 glColor4fv( cur_light_params.scene_diffuse );
774 glColor4f(0.7, 0.2, 0.2, 1.0);
778 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
779 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
780 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
781 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
788 ////////////////////////////////////////////////////////////////////////
789 // Implementation of FGTextLayer.
790 ////////////////////////////////////////////////////////////////////////
792 FGTextLayer::FGTextLayer (int w, int h)
793 : FGInstrumentLayer(w, h), _pointSize(14.0)
796 _color[0] = _color[1] = _color[2] = 0.0;
800 FGTextLayer::~FGTextLayer ()
802 chunk_list::iterator it = _chunks.begin();
803 chunk_list::iterator last = _chunks.end();
804 for ( ; it != last; it++) {
816 text_renderer.setFont(guiFntHandle);
817 text_renderer.setPointSize(_pointSize);
818 text_renderer.begin();
819 text_renderer.start3f(0, 0, 0);
822 if (_now - _then > 100000) {
826 text_renderer.puts((char *)(_value.c_str()));
829 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
835 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
837 _chunks.push_back(chunk);
841 FGTextLayer::setColor (float r, float g, float b)
850 FGTextLayer::setPointSize (float size)
856 FGTextLayer::setFont(fntFont * font)
858 text_renderer.setFont(font);
863 FGTextLayer::recalc_value () const
866 chunk_list::const_iterator it = _chunks.begin();
867 chunk_list::const_iterator last = _chunks.end();
868 for ( ; it != last; it++) {
869 _value += (*it)->getValue();
875 ////////////////////////////////////////////////////////////////////////
876 // Implementation of FGTextLayer::Chunk.
877 ////////////////////////////////////////////////////////////////////////
879 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
880 : _type(FGTextLayer::TEXT), _fmt(fmt)
887 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
888 const string &fmt, float mult)
889 : _type(type), _fmt(fmt), _mult(mult)
892 if (type == TEXT_VALUE)
901 FGTextLayer::Chunk::getValue () const
906 sprintf(_buf, _fmt.c_str(), _text.c_str());
909 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
912 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
923 ////////////////////////////////////////////////////////////////////////
924 // Implementation of FGSwitchLayer.
925 ////////////////////////////////////////////////////////////////////////
927 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
928 FGInstrumentLayer * layer1,
929 FGInstrumentLayer * layer2)
930 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
934 FGSwitchLayer::~FGSwitchLayer ()
941 FGSwitchLayer::draw ()
945 if (_node->getBoolValue()) {