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>
49 return ((current_panel != 0) &&
50 (current_panel->getVisibility()) &&
51 (globals->get_viewmgr()->get_current() == 0) &&
52 (globals->get_current_view()->get_view_offset() == 0.0));
57 ////////////////////////////////////////////////////////////////////////
58 // Implementation of FGTextureManager.
59 ////////////////////////////////////////////////////////////////////////
61 map<string,ssgTexture *> FGTextureManager::_textureMap;
64 FGTextureManager::createTexture (const string &relativePath)
66 ssgTexture * texture = _textureMap[relativePath];
68 SG_LOG( SG_COCKPIT, SG_DEBUG,
69 "Texture " << relativePath << " does not yet exist" );
70 SGPath tpath(globals->get_fg_root());
71 tpath.append(relativePath);
72 texture = new ssgTexture((char *)tpath.c_str(), false, false);
73 _textureMap[relativePath] = texture;
74 if (_textureMap[relativePath] == 0)
75 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
76 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
77 << " handle=" << texture->getHandle() );
86 ////////////////////////////////////////////////////////////////////////
87 // Implementation of FGCropped Texture.
88 ////////////////////////////////////////////////////////////////////////
91 FGCroppedTexture::FGCroppedTexture ()
92 : _path(""), _texture(0),
93 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
98 FGCroppedTexture::FGCroppedTexture (const string &path,
99 float minX, float minY,
100 float maxX, float maxY)
101 : _path(path), _texture(0),
102 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
107 FGCroppedTexture::~FGCroppedTexture ()
113 FGCroppedTexture::getTexture ()
116 _texture = FGTextureManager::createTexture(_path);
123 ////////////////////////////////////////////////////////////////////////
124 // Implementation of FGPanel.
125 ////////////////////////////////////////////////////////////////////////
127 FGPanel * current_panel = NULL;
128 static fntRenderer text_renderer;
134 FGPanel::FGPanel (int window_x, int window_y, int window_w, int window_h)
137 _winx(window_x), _winy(window_y), _winw(window_w), _winh(window_h),
138 _width(_winw), _height(int(_winh * 0.5768 + 1)),
139 _x_offset(0), _y_offset(0), _view_height(int(_winh * 0.4232)),
142 setVisibility(fgPanelVisible());
153 for (instrument_list_type::iterator it = _instruments.begin();
154 it != _instruments.end();
163 * Add an instrument to the panel.
166 FGPanel::addInstrument (FGPanelInstrument * instrument)
168 _instruments.push_back(instrument);
173 * Initialize the panel.
183 * Bind panel properties.
188 fgTie("/sim/panel/visibility", &_visibility);
189 fgTie("/sim/panel/x-offset", &_x_offset);
190 fgTie("/sim/panel/y-offset", &_y_offset);
196 * Unbind panel properties.
201 fgUntie("/sim/panel/visibility");
202 fgUntie("/sim/panel/x-offset");
203 fgUntie("/sim/panel/y-offset");
211 void FGPanel::update ()
213 update(_winx, _winw, _winy, _winh);
218 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
220 // Do nothing if the panel isn't visible.
221 if (!fgPanelVisible())
224 // If the mouse is down, do something
227 if (_mouseDelay < 0) {
228 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
233 // Now, draw the panel
234 glMatrixMode(GL_PROJECTION);
237 gluOrtho2D(winx, winx + winw, winy, winy + winh);
239 glMatrixMode(GL_MODELVIEW);
243 glTranslated(_x_offset, _y_offset, 0);
245 // Draw the background
246 glEnable(GL_TEXTURE_2D);
247 glDisable(GL_LIGHTING);
249 glEnable(GL_ALPHA_TEST);
250 glEnable(GL_COLOR_MATERIAL);
251 // glColor4f(1.0, 1.0, 1.0, 1.0);
252 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
253 glColor4fv( cur_light_params.scene_diffuse );
255 glColor4f(0.7, 0.2, 0.2, 1.0);
257 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
258 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
259 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
260 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
262 glTexCoord2f(0.0, 0.0); glVertex3f(_winx, _winy, 0);
263 glTexCoord2f(1.0, 0.0); glVertex3f(_winx + _width, _winy, 0);
264 glTexCoord2f(1.0, 1.0); glVertex3f(_winx + _width, _winy + _height, 0);
265 glTexCoord2f(0.0, 1.0); glVertex3f(_winx, _winy + _height, 0);
268 // Draw the instruments.
269 instrument_list_type::const_iterator current = _instruments.begin();
270 instrument_list_type::const_iterator end = _instruments.end();
272 for ( ; current != end; current++) {
273 FGPanelInstrument * instr = *current;
275 glTranslated(_x_offset, _y_offset, 0);
276 glTranslated(instr->getXPos(), instr->getYPos(), 0);
280 glMatrixMode(GL_PROJECTION);
282 glMatrixMode(GL_MODELVIEW);
284 ssgForceBasicState();
285 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
290 * Set the panel's visibility.
293 FGPanel::setVisibility (bool visibility)
295 _visibility = visibility;
300 * Return true if the panel is visible.
303 FGPanel::getVisibility () const
310 * Set the panel's background texture.
313 FGPanel::setBackground (ssgTexture * texture)
320 * Set the panel's x-offset.
323 FGPanel::setXOffset (int offset)
325 if (offset <= 0 && offset >= -_width + _winw)
331 * Set the panel's y-offset.
334 FGPanel::setYOffset (int offset)
336 if (offset <= 0 && offset >= -_height)
342 * Perform a mouse action.
345 FGPanel::doMouseAction (int button, int updown, int x, int y)
348 // Note a released button and return
349 // cerr << "Doing mouse action\n";
352 _mouseInstrument = 0;
356 // Scale for the real window size.
357 x = int(((float)x / fgGetInt("/sim/startup/xsize")) * _winw);
358 y = int(_winh - (((float)y / fgGetInt("/sim/startup/ysize"))
361 // Adjust for offsets.
365 // Search for a matching instrument.
366 for (int i = 0; i < (int)_instruments.size(); i++) {
367 FGPanelInstrument *inst = _instruments[i];
368 int ix = inst->getXPos();
369 int iy = inst->getYPos();
370 int iw = inst->getWidth() / 2;
371 int ih = inst->getHeight() / 2;
372 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
375 _mouseInstrument = inst;
376 _mouseButton = button;
379 // Always do the action once.
380 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
389 ////////////////////////////////////////////////////////////////////////.
390 // Implementation of FGPanelAction.
391 ////////////////////////////////////////////////////////////////////////
393 FGPanelAction::FGPanelAction ()
397 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
398 : _button(button), _x(x), _y(y), _w(w), _h(h)
402 FGPanelAction::~FGPanelAction ()
408 ////////////////////////////////////////////////////////////////////////
409 // Implementation of FGAdjustAction.
410 ////////////////////////////////////////////////////////////////////////
412 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
413 SGPropertyNode * node, float increment,
414 float min, float max, bool wrap)
415 : FGPanelAction(button, x, y, w, h),
416 _node(node), _increment(increment), _min(min), _max(max), _wrap(wrap)
420 FGAdjustAction::~FGAdjustAction ()
425 FGAdjustAction::doAction ()
427 float val = _node->getFloatValue();
430 val = (_wrap ? _max : _min);
431 } else if (val > _max) {
432 val = (_wrap ? _min : _max);
434 _node->setDoubleValue(val);
439 ////////////////////////////////////////////////////////////////////////
440 // Implementation of FGSwapAction.
441 ////////////////////////////////////////////////////////////////////////
443 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
444 SGPropertyNode * node1, SGPropertyNode * node2)
445 : FGPanelAction(button, x, y, w, h), _node1(node1), _node2(node2)
449 FGSwapAction::~FGSwapAction ()
454 FGSwapAction::doAction ()
456 float val = _node1->getFloatValue();
457 _node1->setDoubleValue(_node2->getFloatValue());
458 _node2->setDoubleValue(val);
463 ////////////////////////////////////////////////////////////////////////
464 // Implementation of FGToggleAction.
465 ////////////////////////////////////////////////////////////////////////
467 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
468 SGPropertyNode * node)
469 : FGPanelAction(button, x, y, w, h), _node(node)
473 FGToggleAction::~FGToggleAction ()
478 FGToggleAction::doAction ()
480 _node->setBoolValue(!(_node->getBoolValue()));
485 ////////////////////////////////////////////////////////////////////////
486 // Implementation of FGPanelTransformation.
487 ////////////////////////////////////////////////////////////////////////
489 FGPanelTransformation::FGPanelTransformation ()
494 FGPanelTransformation::~FGPanelTransformation ()
500 ////////////////////////////////////////////////////////////////////////
501 // Implementation of FGPanelInstrument.
502 ////////////////////////////////////////////////////////////////////////
505 FGPanelInstrument::FGPanelInstrument ()
511 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
517 FGPanelInstrument::~FGPanelInstrument ()
519 for (action_list_type::iterator it = _actions.begin();
520 it != _actions.end();
528 FGPanelInstrument::setPosition (int x, int y)
535 FGPanelInstrument::setSize (int w, int h)
542 FGPanelInstrument::getXPos () const
548 FGPanelInstrument::getYPos () const
554 FGPanelInstrument::getWidth () const
560 FGPanelInstrument::getHeight () const
566 FGPanelInstrument::addAction (FGPanelAction * action)
568 _actions.push_back(action);
571 // Coordinates relative to centre.
573 FGPanelInstrument::doMouseAction (int button, int x, int y)
575 action_list_type::iterator it = _actions.begin();
576 action_list_type::iterator last = _actions.end();
577 for ( ; it != last; it++) {
578 if ((*it)->inArea(button, x, y)) {
588 ////////////////////////////////////////////////////////////////////////
589 // Implementation of FGLayeredInstrument.
590 ////////////////////////////////////////////////////////////////////////
592 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
593 : FGPanelInstrument(x, y, w, h)
597 FGLayeredInstrument::~FGLayeredInstrument ()
599 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
606 FGLayeredInstrument::draw ()
608 for (int i = 0; i < (int)_layers.size(); i++) {
610 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
617 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
619 int n = _layers.size();
620 if (layer->getWidth() == -1) {
621 layer->setWidth(getWidth());
623 if (layer->getHeight() == -1) {
624 layer->setHeight(getHeight());
626 _layers.push_back(layer);
631 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
634 return addLayer(new FGTexturedLayer(texture, w, h));
638 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
640 int layer = _layers.size() - 1;
641 _layers[layer]->addTransformation(transformation);
646 ////////////////////////////////////////////////////////////////////////
647 // Implementation of FGInstrumentLayer.
648 ////////////////////////////////////////////////////////////////////////
650 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
656 FGInstrumentLayer::~FGInstrumentLayer ()
658 for (transformation_list::iterator it = _transformations.begin();
659 it != _transformations.end();
667 FGInstrumentLayer::transform () const
669 transformation_list::const_iterator it = _transformations.begin();
670 transformation_list::const_iterator last = _transformations.end();
672 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);
701 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
703 _transformations.push_back(transformation);
708 ////////////////////////////////////////////////////////////////////////
709 // Implementation of FGTexturedLayer.
710 ////////////////////////////////////////////////////////////////////////
713 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
714 : FGInstrumentLayer(w, h)
720 FGTexturedLayer::~FGTexturedLayer ()
726 FGTexturedLayer::draw ()
732 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
735 // From Curt: turn on the panel
736 // lights after sundown.
737 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
738 glColor4fv( cur_light_params.scene_diffuse );
740 glColor4f(0.7, 0.2, 0.2, 1.0);
744 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
745 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
746 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
747 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
753 ////////////////////////////////////////////////////////////////////////
754 // Implementation of FGTextLayer.
755 ////////////////////////////////////////////////////////////////////////
757 FGTextLayer::FGTextLayer (int w, int h)
758 : FGInstrumentLayer(w, h), _pointSize(14.0)
761 _color[0] = _color[1] = _color[2] = 0.0;
765 FGTextLayer::~FGTextLayer ()
767 chunk_list::iterator it = _chunks.begin();
768 chunk_list::iterator last = _chunks.end();
769 for ( ; it != last; it++) {
780 text_renderer.setFont(guiFntHandle);
781 text_renderer.setPointSize(_pointSize);
782 text_renderer.begin();
783 text_renderer.start3f(0, 0, 0);
786 if (_now - _then > 100000) {
790 text_renderer.puts((char *)(_value.c_str()));
793 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
798 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
800 _chunks.push_back(chunk);
804 FGTextLayer::setColor (float r, float g, float b)
813 FGTextLayer::setPointSize (float size)
819 FGTextLayer::setFont(fntFont * font)
821 text_renderer.setFont(font);
826 FGTextLayer::recalc_value () const
829 chunk_list::const_iterator it = _chunks.begin();
830 chunk_list::const_iterator last = _chunks.end();
831 for ( ; it != last; it++) {
832 _value += (*it)->getValue();
838 ////////////////////////////////////////////////////////////////////////
839 // Implementation of FGTextLayer::Chunk.
840 ////////////////////////////////////////////////////////////////////////
842 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
843 : _type(FGTextLayer::TEXT), _fmt(fmt)
850 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
851 const string &fmt, float mult)
852 : _type(type), _fmt(fmt), _mult(mult)
855 if (type == TEXT_VALUE)
864 FGTextLayer::Chunk::getValue () const
868 sprintf(_buf, _fmt.c_str(), _text.c_str());
871 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
874 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
882 ////////////////////////////////////////////////////////////////////////
883 // Implementation of FGSwitchLayer.
884 ////////////////////////////////////////////////////////////////////////
886 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
887 FGInstrumentLayer * layer1,
888 FGInstrumentLayer * layer2)
889 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
893 FGSwitchLayer::~FGSwitchLayer ()
900 FGSwitchLayer::draw ()
903 if (_node->getBoolValue()) {