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 fgTie("/sim/panel/x-offset", &_x_offset);
219 fgTie("/sim/panel/y-offset", &_y_offset);
225 * Unbind panel properties.
230 fgUntie("/sim/panel/visibility");
231 fgUntie("/sim/panel/x-offset");
232 fgUntie("/sim/panel/y-offset");
243 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
244 _ysize_node->getIntValue());
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 glMatrixMode(GL_PROJECTION);
263 if (aspect_adjust <1.0)
264 gluOrtho2D(WIN_X, WIN_X + int(WIN_W * aspect_adjust),
265 WIN_Y, WIN_Y + WIN_H);
267 gluOrtho2D(WIN_X, WIN_X + WIN_W,
268 WIN_Y, WIN_Y + int(WIN_H / aspect_adjust));
270 glMatrixMode(GL_MODELVIEW);
274 glTranslated(_x_offset, _y_offset, 0);
276 // Draw the background
277 glEnable(GL_TEXTURE_2D);
278 glDisable(GL_LIGHTING);
280 glEnable(GL_ALPHA_TEST);
281 glEnable(GL_COLOR_MATERIAL);
282 // glColor4f(1.0, 1.0, 1.0, 1.0);
283 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
284 glColor4fv( cur_light_params.scene_diffuse );
286 glColor4f(0.7, 0.2, 0.2, 1.0);
288 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
289 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
290 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
291 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
293 glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
294 glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
295 glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
296 glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
299 // Draw the instruments.
300 instrument_list_type::const_iterator current = _instruments.begin();
301 instrument_list_type::const_iterator end = _instruments.end();
303 for ( ; current != end; current++) {
304 FGPanelInstrument * instr = *current;
306 glTranslated(_x_offset, _y_offset, 0);
307 glTranslated(instr->getXPos(), instr->getYPos(), 0);
311 glMatrixMode(GL_PROJECTION);
313 glMatrixMode(GL_MODELVIEW);
315 ssgForceBasicState();
316 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
321 * Set the panel's visibility.
324 FGPanel::setVisibility (bool visibility)
326 _visibility = visibility;
331 * Return true if the panel is visible.
334 FGPanel::getVisibility () const
341 * Set the panel's background texture.
344 FGPanel::setBackground (ssgTexture * texture)
351 * Set the panel's x-offset.
354 FGPanel::setXOffset (int offset)
356 if (offset <= 0 && offset >= -_width + WIN_W)
362 * Set the panel's y-offset.
365 FGPanel::setYOffset (int offset)
367 if (offset <= 0 && offset >= -_height)
373 * Perform a mouse action.
376 FGPanel::doMouseAction (int button, int updown, int x, int y)
378 // FIXME: this same code appears in update()
379 int xsize = _xsize_node->getIntValue();
380 int ysize = _ysize_node->getIntValue();
381 float aspect_adjust = get_aspect_adjust(xsize, ysize);
383 // Note a released button and return
384 // cerr << "Doing mouse action\n";
387 _mouseInstrument = 0;
391 // Scale for the real window size.
392 if (aspect_adjust < 1.0) {
393 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
394 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
396 x = int(((float)x / xsize) * WIN_W);
397 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
400 // Adjust for offsets.
404 // Search for a matching instrument.
405 for (int i = 0; i < (int)_instruments.size(); i++) {
406 FGPanelInstrument *inst = _instruments[i];
407 int ix = inst->getXPos();
408 int iy = inst->getYPos();
409 int iw = inst->getWidth() / 2;
410 int ih = inst->getHeight() / 2;
411 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
414 _mouseInstrument = inst;
415 _mouseButton = button;
418 // Always do the action once.
419 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
428 ////////////////////////////////////////////////////////////////////////.
429 // Implementation of FGPanelAction.
430 ////////////////////////////////////////////////////////////////////////
432 FGPanelAction::FGPanelAction ()
436 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
437 : _button(button), _x(x), _y(y), _w(w), _h(h)
441 FGPanelAction::~FGPanelAction ()
447 ////////////////////////////////////////////////////////////////////////
448 // Implementation of FGAdjustAction.
449 ////////////////////////////////////////////////////////////////////////
451 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
452 SGPropertyNode * node, float increment,
453 float min, float max, bool wrap)
454 : FGPanelAction(button, x, y, w, h),
455 _node(node), _increment(increment), _min(min), _max(max), _wrap(wrap)
459 FGAdjustAction::~FGAdjustAction ()
464 FGAdjustAction::doAction ()
466 float val = _node->getFloatValue();
469 val = (_wrap ? _max : _min);
470 } else if (val > _max) {
471 val = (_wrap ? _min : _max);
473 _node->setDoubleValue(val);
478 ////////////////////////////////////////////////////////////////////////
479 // Implementation of FGSwapAction.
480 ////////////////////////////////////////////////////////////////////////
482 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
483 SGPropertyNode * node1, SGPropertyNode * node2)
484 : FGPanelAction(button, x, y, w, h), _node1(node1), _node2(node2)
488 FGSwapAction::~FGSwapAction ()
493 FGSwapAction::doAction ()
495 float val = _node1->getFloatValue();
496 _node1->setDoubleValue(_node2->getFloatValue());
497 _node2->setDoubleValue(val);
502 ////////////////////////////////////////////////////////////////////////
503 // Implementation of FGToggleAction.
504 ////////////////////////////////////////////////////////////////////////
506 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
507 SGPropertyNode * node)
508 : FGPanelAction(button, x, y, w, h), _node(node)
512 FGToggleAction::~FGToggleAction ()
517 FGToggleAction::doAction ()
519 _node->setBoolValue(!(_node->getBoolValue()));
524 ////////////////////////////////////////////////////////////////////////
525 // Implementation of FGPanelTransformation.
526 ////////////////////////////////////////////////////////////////////////
528 FGPanelTransformation::FGPanelTransformation ()
533 FGPanelTransformation::~FGPanelTransformation ()
539 ////////////////////////////////////////////////////////////////////////
540 // Implementation of FGPanelInstrument.
541 ////////////////////////////////////////////////////////////////////////
544 FGPanelInstrument::FGPanelInstrument ()
550 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
556 FGPanelInstrument::~FGPanelInstrument ()
558 for (action_list_type::iterator it = _actions.begin();
559 it != _actions.end();
567 FGPanelInstrument::setPosition (int x, int y)
574 FGPanelInstrument::setSize (int w, int h)
581 FGPanelInstrument::getXPos () const
587 FGPanelInstrument::getYPos () const
593 FGPanelInstrument::getWidth () const
599 FGPanelInstrument::getHeight () const
605 FGPanelInstrument::addAction (FGPanelAction * action)
607 _actions.push_back(action);
610 // Coordinates relative to centre.
612 FGPanelInstrument::doMouseAction (int button, int x, int y)
614 action_list_type::iterator it = _actions.begin();
615 action_list_type::iterator last = _actions.end();
616 for ( ; it != last; it++) {
617 if ((*it)->inArea(button, x, y)) {
627 ////////////////////////////////////////////////////////////////////////
628 // Implementation of FGLayeredInstrument.
629 ////////////////////////////////////////////////////////////////////////
631 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
632 : FGPanelInstrument(x, y, w, h)
636 FGLayeredInstrument::~FGLayeredInstrument ()
638 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
645 FGLayeredInstrument::draw ()
647 for (int i = 0; i < (int)_layers.size(); i++) {
649 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
656 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
658 int n = _layers.size();
659 if (layer->getWidth() == -1) {
660 layer->setWidth(getWidth());
662 if (layer->getHeight() == -1) {
663 layer->setHeight(getHeight());
665 _layers.push_back(layer);
670 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
673 return addLayer(new FGTexturedLayer(texture, w, h));
677 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
679 int layer = _layers.size() - 1;
680 _layers[layer]->addTransformation(transformation);
685 ////////////////////////////////////////////////////////////////////////
686 // Implementation of FGInstrumentLayer.
687 ////////////////////////////////////////////////////////////////////////
689 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
695 FGInstrumentLayer::~FGInstrumentLayer ()
697 for (transformation_list::iterator it = _transformations.begin();
698 it != _transformations.end();
706 FGInstrumentLayer::transform () const
708 transformation_list::const_iterator it = _transformations.begin();
709 transformation_list::const_iterator last = _transformations.end();
711 FGPanelTransformation *t = *it;
712 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
715 } else if (val > t->max) {
719 val = val * t->factor + t->offset;
721 val = t->table->interpolate(val) * t->factor + t->offset;
725 case FGPanelTransformation::XSHIFT:
726 glTranslatef(val, 0.0, 0.0);
728 case FGPanelTransformation::YSHIFT:
729 glTranslatef(0.0, val, 0.0);
731 case FGPanelTransformation::ROTATION:
732 glRotatef(-val, 0.0, 0.0, 1.0);
740 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
742 _transformations.push_back(transformation);
747 ////////////////////////////////////////////////////////////////////////
748 // Implementation of FGTexturedLayer.
749 ////////////////////////////////////////////////////////////////////////
752 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
753 : FGInstrumentLayer(w, h)
759 FGTexturedLayer::~FGTexturedLayer ()
765 FGTexturedLayer::draw ()
771 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
774 // From Curt: turn on the panel
775 // lights after sundown.
776 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
777 glColor4fv( cur_light_params.scene_diffuse );
779 glColor4f(0.7, 0.2, 0.2, 1.0);
783 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
784 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
785 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
786 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
792 ////////////////////////////////////////////////////////////////////////
793 // Implementation of FGTextLayer.
794 ////////////////////////////////////////////////////////////////////////
796 FGTextLayer::FGTextLayer (int w, int h)
797 : FGInstrumentLayer(w, h), _pointSize(14.0)
800 _color[0] = _color[1] = _color[2] = 0.0;
804 FGTextLayer::~FGTextLayer ()
806 chunk_list::iterator it = _chunks.begin();
807 chunk_list::iterator last = _chunks.end();
808 for ( ; it != last; it++) {
819 text_renderer.setFont(guiFntHandle);
820 text_renderer.setPointSize(_pointSize);
821 text_renderer.begin();
822 text_renderer.start3f(0, 0, 0);
825 if (_now - _then > 100000) {
829 text_renderer.puts((char *)(_value.c_str()));
832 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
837 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
839 _chunks.push_back(chunk);
843 FGTextLayer::setColor (float r, float g, float b)
852 FGTextLayer::setPointSize (float size)
858 FGTextLayer::setFont(fntFont * font)
860 text_renderer.setFont(font);
865 FGTextLayer::recalc_value () const
868 chunk_list::const_iterator it = _chunks.begin();
869 chunk_list::const_iterator last = _chunks.end();
870 for ( ; it != last; it++) {
871 _value += (*it)->getValue();
877 ////////////////////////////////////////////////////////////////////////
878 // Implementation of FGTextLayer::Chunk.
879 ////////////////////////////////////////////////////////////////////////
881 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
882 : _type(FGTextLayer::TEXT), _fmt(fmt)
889 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
890 const string &fmt, float mult)
891 : _type(type), _fmt(fmt), _mult(mult)
894 if (type == TEXT_VALUE)
903 FGTextLayer::Chunk::getValue () const
907 sprintf(_buf, _fmt.c_str(), _text.c_str());
910 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
913 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 ()
942 if (_node->getBoolValue()) {