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 // Do nothing if the panel isn't visible.
244 if (!fgPanelVisible())
247 // If the mouse is down, do something
250 if (_mouseDelay < 0) {
251 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
256 // Now, draw the panel
257 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
258 _ysize_node->getIntValue());
259 if (aspect_adjust <1.0)
260 update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
262 update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
267 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
269 glMatrixMode(GL_PROJECTION);
272 gluOrtho2D(winx, winx + winw, winy, winy + winh);
274 glMatrixMode(GL_MODELVIEW);
278 glTranslated(_x_offset, _y_offset, 0);
280 // Draw the background
281 glEnable(GL_TEXTURE_2D);
282 glDisable(GL_LIGHTING);
284 glEnable(GL_ALPHA_TEST);
285 glEnable(GL_COLOR_MATERIAL);
286 // glColor4f(1.0, 1.0, 1.0, 1.0);
287 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
288 glColor4fv( cur_light_params.scene_diffuse );
290 glColor4f(0.7, 0.2, 0.2, 1.0);
292 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
293 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
294 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
295 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
297 glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
298 glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
299 glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
300 glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
303 // Draw the instruments.
304 instrument_list_type::const_iterator current = _instruments.begin();
305 instrument_list_type::const_iterator end = _instruments.end();
307 for ( ; current != end; current++) {
308 FGPanelInstrument * instr = *current;
310 glTranslated(_x_offset, _y_offset, 0);
311 glTranslated(instr->getXPos(), instr->getYPos(), 0);
315 glMatrixMode(GL_PROJECTION);
317 glMatrixMode(GL_MODELVIEW);
319 ssgForceBasicState();
320 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
325 * Set the panel's visibility.
328 FGPanel::setVisibility (bool visibility)
330 _visibility = visibility;
335 * Return true if the panel is visible.
338 FGPanel::getVisibility () const
345 * Set the panel's background texture.
348 FGPanel::setBackground (ssgTexture * texture)
355 * Set the panel's x-offset.
358 FGPanel::setXOffset (int offset)
360 if (offset <= 0 && offset >= -_width + WIN_W)
366 * Set the panel's y-offset.
369 FGPanel::setYOffset (int offset)
371 if (offset <= 0 && offset >= -_height)
377 * Perform a mouse action.
380 FGPanel::doMouseAction (int button, int updown, int x, int y)
382 // FIXME: this same code appears in update()
383 int xsize = _xsize_node->getIntValue();
384 int ysize = _ysize_node->getIntValue();
385 float aspect_adjust = get_aspect_adjust(xsize, ysize);
387 // Note a released button and return
388 // cerr << "Doing mouse action\n";
391 _mouseInstrument = 0;
395 // Scale for the real window size.
396 if (aspect_adjust < 1.0) {
397 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
398 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
400 x = int(((float)x / xsize) * WIN_W);
401 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
404 // Adjust for offsets.
408 // Search for a matching instrument.
409 for (int i = 0; i < (int)_instruments.size(); i++) {
410 FGPanelInstrument *inst = _instruments[i];
411 int ix = inst->getXPos();
412 int iy = inst->getYPos();
413 int iw = inst->getWidth() / 2;
414 int ih = inst->getHeight() / 2;
415 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
418 _mouseInstrument = inst;
419 _mouseButton = button;
422 // Always do the action once.
423 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
432 ////////////////////////////////////////////////////////////////////////.
433 // Implementation of FGPanelAction.
434 ////////////////////////////////////////////////////////////////////////
436 FGPanelAction::FGPanelAction ()
440 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
441 : _button(button), _x(x), _y(y), _w(w), _h(h)
445 FGPanelAction::~FGPanelAction ()
451 ////////////////////////////////////////////////////////////////////////
452 // Implementation of FGAdjustAction.
453 ////////////////////////////////////////////////////////////////////////
455 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
456 SGPropertyNode * node, float increment,
457 float min, float max, bool wrap)
458 : FGPanelAction(button, x, y, w, h),
459 _node(node), _increment(increment), _min(min), _max(max), _wrap(wrap)
463 FGAdjustAction::~FGAdjustAction ()
468 FGAdjustAction::doAction ()
470 float val = _node->getFloatValue();
473 val = (_wrap ? _max : _min);
474 } else if (val > _max) {
475 val = (_wrap ? _min : _max);
477 _node->setDoubleValue(val);
482 ////////////////////////////////////////////////////////////////////////
483 // Implementation of FGSwapAction.
484 ////////////////////////////////////////////////////////////////////////
486 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
487 SGPropertyNode * node1, SGPropertyNode * node2)
488 : FGPanelAction(button, x, y, w, h), _node1(node1), _node2(node2)
492 FGSwapAction::~FGSwapAction ()
497 FGSwapAction::doAction ()
499 float val = _node1->getFloatValue();
500 _node1->setDoubleValue(_node2->getFloatValue());
501 _node2->setDoubleValue(val);
506 ////////////////////////////////////////////////////////////////////////
507 // Implementation of FGToggleAction.
508 ////////////////////////////////////////////////////////////////////////
510 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
511 SGPropertyNode * node)
512 : FGPanelAction(button, x, y, w, h), _node(node)
516 FGToggleAction::~FGToggleAction ()
521 FGToggleAction::doAction ()
523 _node->setBoolValue(!(_node->getBoolValue()));
528 ////////////////////////////////////////////////////////////////////////
529 // Implementation of FGPanelTransformation.
530 ////////////////////////////////////////////////////////////////////////
532 FGPanelTransformation::FGPanelTransformation ()
537 FGPanelTransformation::~FGPanelTransformation ()
543 ////////////////////////////////////////////////////////////////////////
544 // Implementation of FGPanelInstrument.
545 ////////////////////////////////////////////////////////////////////////
548 FGPanelInstrument::FGPanelInstrument ()
554 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
560 FGPanelInstrument::~FGPanelInstrument ()
562 for (action_list_type::iterator it = _actions.begin();
563 it != _actions.end();
571 FGPanelInstrument::setPosition (int x, int y)
578 FGPanelInstrument::setSize (int w, int h)
585 FGPanelInstrument::getXPos () const
591 FGPanelInstrument::getYPos () const
597 FGPanelInstrument::getWidth () const
603 FGPanelInstrument::getHeight () const
609 FGPanelInstrument::addAction (FGPanelAction * action)
611 _actions.push_back(action);
614 // Coordinates relative to centre.
616 FGPanelInstrument::doMouseAction (int button, int x, int y)
618 action_list_type::iterator it = _actions.begin();
619 action_list_type::iterator last = _actions.end();
620 for ( ; it != last; it++) {
621 if ((*it)->inArea(button, x, y)) {
631 ////////////////////////////////////////////////////////////////////////
632 // Implementation of FGLayeredInstrument.
633 ////////////////////////////////////////////////////////////////////////
635 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
636 : FGPanelInstrument(x, y, w, h)
640 FGLayeredInstrument::~FGLayeredInstrument ()
642 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
649 FGLayeredInstrument::draw ()
651 for (int i = 0; i < (int)_layers.size(); i++) {
653 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
660 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
662 int n = _layers.size();
663 if (layer->getWidth() == -1) {
664 layer->setWidth(getWidth());
666 if (layer->getHeight() == -1) {
667 layer->setHeight(getHeight());
669 _layers.push_back(layer);
674 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
677 return addLayer(new FGTexturedLayer(texture, w, h));
681 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
683 int layer = _layers.size() - 1;
684 _layers[layer]->addTransformation(transformation);
689 ////////////////////////////////////////////////////////////////////////
690 // Implementation of FGInstrumentLayer.
691 ////////////////////////////////////////////////////////////////////////
693 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
699 FGInstrumentLayer::~FGInstrumentLayer ()
701 for (transformation_list::iterator it = _transformations.begin();
702 it != _transformations.end();
710 FGInstrumentLayer::transform () const
712 transformation_list::const_iterator it = _transformations.begin();
713 transformation_list::const_iterator last = _transformations.end();
715 FGPanelTransformation *t = *it;
716 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
719 } else if (val > t->max) {
723 val = val * t->factor + t->offset;
725 val = t->table->interpolate(val) * t->factor + t->offset;
729 case FGPanelTransformation::XSHIFT:
730 glTranslatef(val, 0.0, 0.0);
732 case FGPanelTransformation::YSHIFT:
733 glTranslatef(0.0, val, 0.0);
735 case FGPanelTransformation::ROTATION:
736 glRotatef(-val, 0.0, 0.0, 1.0);
744 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
746 _transformations.push_back(transformation);
751 ////////////////////////////////////////////////////////////////////////
752 // Implementation of FGTexturedLayer.
753 ////////////////////////////////////////////////////////////////////////
756 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
757 : FGInstrumentLayer(w, h)
763 FGTexturedLayer::~FGTexturedLayer ()
769 FGTexturedLayer::draw ()
775 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
778 // From Curt: turn on the panel
779 // lights after sundown.
780 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
781 glColor4fv( cur_light_params.scene_diffuse );
783 glColor4f(0.7, 0.2, 0.2, 1.0);
787 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
788 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
789 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
790 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
796 ////////////////////////////////////////////////////////////////////////
797 // Implementation of FGTextLayer.
798 ////////////////////////////////////////////////////////////////////////
800 FGTextLayer::FGTextLayer (int w, int h)
801 : FGInstrumentLayer(w, h), _pointSize(14.0)
804 _color[0] = _color[1] = _color[2] = 0.0;
808 FGTextLayer::~FGTextLayer ()
810 chunk_list::iterator it = _chunks.begin();
811 chunk_list::iterator last = _chunks.end();
812 for ( ; it != last; it++) {
823 text_renderer.setFont(guiFntHandle);
824 text_renderer.setPointSize(_pointSize);
825 text_renderer.begin();
826 text_renderer.start3f(0, 0, 0);
829 if (_now - _then > 100000) {
833 text_renderer.puts((char *)(_value.c_str()));
836 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
841 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
843 _chunks.push_back(chunk);
847 FGTextLayer::setColor (float r, float g, float b)
856 FGTextLayer::setPointSize (float size)
862 FGTextLayer::setFont(fntFont * font)
864 text_renderer.setFont(font);
869 FGTextLayer::recalc_value () const
872 chunk_list::const_iterator it = _chunks.begin();
873 chunk_list::const_iterator last = _chunks.end();
874 for ( ; it != last; it++) {
875 _value += (*it)->getValue();
881 ////////////////////////////////////////////////////////////////////////
882 // Implementation of FGTextLayer::Chunk.
883 ////////////////////////////////////////////////////////////////////////
885 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
886 : _type(FGTextLayer::TEXT), _fmt(fmt)
893 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
894 const string &fmt, float mult)
895 : _type(type), _fmt(fmt), _mult(mult)
898 if (type == TEXT_VALUE)
907 FGTextLayer::Chunk::getValue () const
911 sprintf(_buf, _fmt.c_str(), _text.c_str());
914 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
917 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
925 ////////////////////////////////////////////////////////////////////////
926 // Implementation of FGSwitchLayer.
927 ////////////////////////////////////////////////////////////////////////
929 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
930 FGInstrumentLayer * layer1,
931 FGInstrumentLayer * layer2)
932 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
936 FGSwitchLayer::~FGSwitchLayer ()
943 FGSwitchLayer::draw ()
946 if (_node->getBoolValue()) {