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 <Objects/texload.h>
40 #include <Time/light.hxx>
52 ////////////////////////////////////////////////////////////////////////
54 ////////////////////////////////////////////////////////////////////////
58 * Calculate the aspect adjustment for the panel.
61 get_aspect_adjust (int xsize, int ysize)
63 float ideal_aspect = float(WIN_W) / float(WIN_H);
64 float real_aspect = float(xsize) / float(ysize);
65 return (real_aspect / ideal_aspect);
70 ////////////////////////////////////////////////////////////////////////
72 ////////////////////////////////////////////////////////////////////////
77 return ((current_panel != 0) &&
78 (current_panel->getVisibility()) &&
79 (globals->get_viewmgr()->get_current() == 0) &&
80 (globals->get_current_view()->get_view_offset() == 0.0));
85 ////////////////////////////////////////////////////////////////////////
86 // Implementation of FGTextureManager.
87 ////////////////////////////////////////////////////////////////////////
89 map<string,ssgTexture *> FGTextureManager::_textureMap;
92 FGTextureManager::createTexture (const string &relativePath)
94 ssgTexture * texture = _textureMap[relativePath];
96 SG_LOG( SG_COCKPIT, SG_DEBUG,
97 "Texture " << relativePath << " does not yet exist" );
98 SGPath tpath(globals->get_fg_root());
99 tpath.append(relativePath);
100 texture = new ssgTexture((char *)tpath.c_str(), false, false);
101 _textureMap[relativePath] = texture;
102 if (_textureMap[relativePath] == 0)
103 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
104 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
105 << " handle=" << texture->getHandle() );
114 ////////////////////////////////////////////////////////////////////////
115 // Implementation of FGCropped Texture.
116 ////////////////////////////////////////////////////////////////////////
119 FGCroppedTexture::FGCroppedTexture ()
120 : _path(""), _texture(0),
121 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
126 FGCroppedTexture::FGCroppedTexture (const string &path,
127 float minX, float minY,
128 float maxX, float maxY)
129 : _path(path), _texture(0),
130 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
135 FGCroppedTexture::~FGCroppedTexture ()
141 FGCroppedTexture::getTexture ()
144 _texture = FGTextureManager::createTexture(_path);
151 ////////////////////////////////////////////////////////////////////////
152 // Implementation of FGPanel.
153 ////////////////////////////////////////////////////////////////////////
155 FGPanel * current_panel = NULL;
156 static fntRenderer text_renderer;
165 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
166 _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
168 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
169 _ysize_node(fgGetNode("/sim/startup/ysize", true))
171 setVisibility(fgPanelVisible());
182 for (instrument_list_type::iterator it = _instruments.begin();
183 it != _instruments.end();
192 * Add an instrument to the panel.
195 FGPanel::addInstrument (FGPanelInstrument * instrument)
197 _instruments.push_back(instrument);
202 * Initialize the panel.
212 * Bind panel properties.
217 fgTie("/sim/panel/visibility", &_visibility);
218 fgSetArchivable("/sim/panel/visibility");
219 fgTie("/sim/panel/x-offset", &_x_offset);
220 fgSetArchivable("/sim/panel/x-offset");
221 fgTie("/sim/panel/y-offset", &_y_offset);
222 fgSetArchivable("/sim/panel/y-offset");
228 * Unbind panel properties.
233 fgUntie("/sim/panel/visibility");
234 fgUntie("/sim/panel/x-offset");
235 fgUntie("/sim/panel/y-offset");
246 // Do nothing if the panel isn't visible.
247 if (!fgPanelVisible())
250 // If the mouse is down, do something
253 if (_mouseDelay < 0) {
254 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
259 // Now, draw the panel
260 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
261 _ysize_node->getIntValue());
262 if (aspect_adjust <1.0)
263 update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
265 update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
270 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
272 glMatrixMode(GL_PROJECTION);
275 gluOrtho2D(winx, winx + winw, winy, winy + winh);
277 glMatrixMode(GL_MODELVIEW);
281 glTranslated(_x_offset, _y_offset, 0);
283 // Draw the background
284 glEnable(GL_TEXTURE_2D);
285 glDisable(GL_LIGHTING);
287 glEnable(GL_ALPHA_TEST);
288 glEnable(GL_COLOR_MATERIAL);
289 // glColor4f(1.0, 1.0, 1.0, 1.0);
290 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
291 glColor4fv( cur_light_params.scene_diffuse );
293 glColor4f(0.7, 0.2, 0.2, 1.0);
295 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
296 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
297 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
298 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
300 glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
301 glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
302 glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
303 glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
306 // Draw the instruments.
307 instrument_list_type::const_iterator current = _instruments.begin();
308 instrument_list_type::const_iterator end = _instruments.end();
310 for ( ; current != end; current++) {
311 FGPanelInstrument * instr = *current;
313 glTranslated(_x_offset, _y_offset, 0);
314 glTranslated(instr->getXPos(), instr->getYPos(), 0);
318 glMatrixMode(GL_PROJECTION);
320 glMatrixMode(GL_MODELVIEW);
322 ssgForceBasicState();
323 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
328 * Set the panel's visibility.
331 FGPanel::setVisibility (bool visibility)
333 _visibility = visibility;
338 * Return true if the panel is visible.
341 FGPanel::getVisibility () const
348 * Set the panel's background texture.
351 FGPanel::setBackground (ssgTexture * texture)
358 * Set the panel's x-offset.
361 FGPanel::setXOffset (int offset)
363 if (offset <= 0 && offset >= -_width + WIN_W)
369 * Set the panel's y-offset.
372 FGPanel::setYOffset (int offset)
374 if (offset <= 0 && offset >= -_height)
380 * Perform a mouse action.
383 FGPanel::doMouseAction (int button, int updown, int x, int y)
385 // FIXME: this same code appears in update()
386 int xsize = _xsize_node->getIntValue();
387 int ysize = _ysize_node->getIntValue();
388 float aspect_adjust = get_aspect_adjust(xsize, ysize);
390 // Note a released button and return
391 // cerr << "Doing mouse action\n";
394 _mouseInstrument = 0;
398 // Scale for the real window size.
399 if (aspect_adjust < 1.0) {
400 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
401 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
403 x = int(((float)x / xsize) * WIN_W);
404 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
407 // Adjust for offsets.
411 // Search for a matching instrument.
412 for (int i = 0; i < (int)_instruments.size(); i++) {
413 FGPanelInstrument *inst = _instruments[i];
414 int ix = inst->getXPos();
415 int iy = inst->getYPos();
416 int iw = inst->getWidth() / 2;
417 int ih = inst->getHeight() / 2;
418 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
421 _mouseInstrument = inst;
422 _mouseButton = button;
425 // Always do the action once.
426 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
435 ////////////////////////////////////////////////////////////////////////.
436 // Implementation of FGPanelAction.
437 ////////////////////////////////////////////////////////////////////////
439 FGPanelAction::FGPanelAction ()
443 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
444 : _button(button), _x(x), _y(y), _w(w), _h(h)
448 FGPanelAction::~FGPanelAction ()
454 ////////////////////////////////////////////////////////////////////////
455 // Implementation of FGAdjustAction.
456 ////////////////////////////////////////////////////////////////////////
458 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
459 SGPropertyNode * node, float increment,
460 float min, float max, bool wrap)
461 : FGPanelAction(button, x, y, w, h),
462 _node(node), _increment(increment), _min(min), _max(max), _wrap(wrap)
466 FGAdjustAction::~FGAdjustAction ()
471 FGAdjustAction::doAction ()
473 float val = _node->getFloatValue();
476 val = (_wrap ? _max : _min);
477 } else if (val > _max) {
478 val = (_wrap ? _min : _max);
480 _node->setDoubleValue(val);
485 ////////////////////////////////////////////////////////////////////////
486 // Implementation of FGSwapAction.
487 ////////////////////////////////////////////////////////////////////////
489 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
490 SGPropertyNode * node1, SGPropertyNode * node2)
491 : FGPanelAction(button, x, y, w, h), _node1(node1), _node2(node2)
495 FGSwapAction::~FGSwapAction ()
500 FGSwapAction::doAction ()
502 float val = _node1->getFloatValue();
503 _node1->setDoubleValue(_node2->getFloatValue());
504 _node2->setDoubleValue(val);
509 ////////////////////////////////////////////////////////////////////////
510 // Implementation of FGToggleAction.
511 ////////////////////////////////////////////////////////////////////////
513 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
514 SGPropertyNode * node)
515 : FGPanelAction(button, x, y, w, h), _node(node)
519 FGToggleAction::~FGToggleAction ()
524 FGToggleAction::doAction ()
526 _node->setBoolValue(!(_node->getBoolValue()));
531 ////////////////////////////////////////////////////////////////////////
532 // Implementation of FGPanelTransformation.
533 ////////////////////////////////////////////////////////////////////////
535 FGPanelTransformation::FGPanelTransformation ()
540 FGPanelTransformation::~FGPanelTransformation ()
546 ////////////////////////////////////////////////////////////////////////
547 // Implementation of FGPanelInstrument.
548 ////////////////////////////////////////////////////////////////////////
551 FGPanelInstrument::FGPanelInstrument ()
557 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
563 FGPanelInstrument::~FGPanelInstrument ()
565 for (action_list_type::iterator it = _actions.begin();
566 it != _actions.end();
574 FGPanelInstrument::setPosition (int x, int y)
581 FGPanelInstrument::setSize (int w, int h)
588 FGPanelInstrument::getXPos () const
594 FGPanelInstrument::getYPos () const
600 FGPanelInstrument::getWidth () const
606 FGPanelInstrument::getHeight () const
612 FGPanelInstrument::addAction (FGPanelAction * action)
614 _actions.push_back(action);
617 // Coordinates relative to centre.
619 FGPanelInstrument::doMouseAction (int button, int x, int y)
621 action_list_type::iterator it = _actions.begin();
622 action_list_type::iterator last = _actions.end();
623 for ( ; it != last; it++) {
624 if ((*it)->inArea(button, x, y)) {
634 ////////////////////////////////////////////////////////////////////////
635 // Implementation of FGLayeredInstrument.
636 ////////////////////////////////////////////////////////////////////////
638 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
639 : FGPanelInstrument(x, y, w, h)
643 FGLayeredInstrument::~FGLayeredInstrument ()
645 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
652 FGLayeredInstrument::draw ()
654 for (int i = 0; i < (int)_layers.size(); i++) {
656 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
663 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
665 int n = _layers.size();
666 if (layer->getWidth() == -1) {
667 layer->setWidth(getWidth());
669 if (layer->getHeight() == -1) {
670 layer->setHeight(getHeight());
672 _layers.push_back(layer);
677 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
680 return addLayer(new FGTexturedLayer(texture, w, h));
684 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
686 int layer = _layers.size() - 1;
687 _layers[layer]->addTransformation(transformation);
692 ////////////////////////////////////////////////////////////////////////
693 // Implementation of FGInstrumentLayer.
694 ////////////////////////////////////////////////////////////////////////
696 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
702 FGInstrumentLayer::~FGInstrumentLayer ()
704 for (transformation_list::iterator it = _transformations.begin();
705 it != _transformations.end();
713 FGInstrumentLayer::transform () const
715 transformation_list::const_iterator it = _transformations.begin();
716 transformation_list::const_iterator last = _transformations.end();
718 FGPanelTransformation *t = *it;
719 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
722 } else if (val > t->max) {
726 val = val * t->factor + t->offset;
728 val = t->table->interpolate(val) * t->factor + t->offset;
732 case FGPanelTransformation::XSHIFT:
733 glTranslatef(val, 0.0, 0.0);
735 case FGPanelTransformation::YSHIFT:
736 glTranslatef(0.0, val, 0.0);
738 case FGPanelTransformation::ROTATION:
739 glRotatef(-val, 0.0, 0.0, 1.0);
747 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
749 _transformations.push_back(transformation);
754 ////////////////////////////////////////////////////////////////////////
755 // Implementation of FGTexturedLayer.
756 ////////////////////////////////////////////////////////////////////////
759 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
760 : FGInstrumentLayer(w, h)
766 FGTexturedLayer::~FGTexturedLayer ()
772 FGTexturedLayer::draw ()
778 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
781 // From Curt: turn on the panel
782 // lights after sundown.
783 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
784 glColor4fv( cur_light_params.scene_diffuse );
786 glColor4f(0.7, 0.2, 0.2, 1.0);
790 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
791 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
792 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
793 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
799 ////////////////////////////////////////////////////////////////////////
800 // Implementation of FGTextLayer.
801 ////////////////////////////////////////////////////////////////////////
803 FGTextLayer::FGTextLayer (int w, int h)
804 : FGInstrumentLayer(w, h), _pointSize(14.0)
807 _color[0] = _color[1] = _color[2] = 0.0;
811 FGTextLayer::~FGTextLayer ()
813 chunk_list::iterator it = _chunks.begin();
814 chunk_list::iterator last = _chunks.end();
815 for ( ; it != last; it++) {
826 text_renderer.setFont(guiFntHandle);
827 text_renderer.setPointSize(_pointSize);
828 text_renderer.begin();
829 text_renderer.start3f(0, 0, 0);
832 if (_now - _then > 100000) {
836 text_renderer.puts((char *)(_value.c_str()));
839 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
844 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
846 _chunks.push_back(chunk);
850 FGTextLayer::setColor (float r, float g, float b)
859 FGTextLayer::setPointSize (float size)
865 FGTextLayer::setFont(fntFont * font)
867 text_renderer.setFont(font);
872 FGTextLayer::recalc_value () const
875 chunk_list::const_iterator it = _chunks.begin();
876 chunk_list::const_iterator last = _chunks.end();
877 for ( ; it != last; it++) {
878 _value += (*it)->getValue();
884 ////////////////////////////////////////////////////////////////////////
885 // Implementation of FGTextLayer::Chunk.
886 ////////////////////////////////////////////////////////////////////////
888 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
889 : _type(FGTextLayer::TEXT), _fmt(fmt)
896 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
897 const string &fmt, float mult)
898 : _type(type), _fmt(fmt), _mult(mult)
901 if (type == TEXT_VALUE)
910 FGTextLayer::Chunk::getValue () const
914 sprintf(_buf, _fmt.c_str(), _text.c_str());
917 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
920 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
928 ////////////////////////////////////////////////////////////////////////
929 // Implementation of FGSwitchLayer.
930 ////////////////////////////////////////////////////////////////////////
932 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
933 FGInstrumentLayer * layer1,
934 FGInstrumentLayer * layer2)
935 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
939 FGSwitchLayer::~FGSwitchLayer ()
946 FGSwitchLayer::draw ()
949 if (_node->getBoolValue()) {