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 cerr << "Texture " << relativePath << " does not yet exist" << endl;
69 SGPath tpath(globals->get_fg_root());
70 tpath.append(relativePath);
71 texture = new ssgTexture((char *)tpath.c_str(), false, false);
72 _textureMap[relativePath] = texture;
73 if (_textureMap[relativePath] == 0)
74 cerr << "Texture *still* doesn't exist" << endl;
75 cerr << "Created texture " << relativePath
76 << " handle=" << texture->getHandle() << endl;
85 ////////////////////////////////////////////////////////////////////////
86 // Implementation of FGCropped Texture.
87 ////////////////////////////////////////////////////////////////////////
90 FGCroppedTexture::FGCroppedTexture ()
91 : _path(""), _texture(0),
92 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
97 FGCroppedTexture::FGCroppedTexture (const string &path,
98 float minX, float minY,
99 float maxX, float maxY)
100 : _path(path), _texture(0),
101 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
106 FGCroppedTexture::~FGCroppedTexture ()
112 FGCroppedTexture::getTexture ()
115 _texture = FGTextureManager::createTexture(_path);
122 ////////////////////////////////////////////////////////////////////////
123 // Implementation of FGPanel.
124 ////////////////////////////////////////////////////////////////////////
126 FGPanel * current_panel = NULL;
127 static fntRenderer text_renderer;
133 FGPanel::FGPanel (int window_x, int window_y, int window_w, int window_h)
136 _winx(window_x), _winy(window_y), _winw(window_w), _winh(window_h),
137 _width(_winw), _height(int(_winh * 0.5768 + 1)),
138 _x_offset(0), _y_offset(0), _view_height(int(_winh * 0.4232)),
141 setVisibility(fgPanelVisible());
152 for (instrument_list_type::iterator it = _instruments.begin();
153 it != _instruments.end();
162 * Add an instrument to the panel.
165 FGPanel::addInstrument (FGPanelInstrument * instrument)
167 _instruments.push_back(instrument);
172 * Initialize the panel.
182 * Bind panel properties.
187 fgTie("/sim/panel/visibility", &_visibility);
188 fgTie("/sim/panel/x-offset", &_x_offset);
189 fgTie("/sim/panel/y-offset", &_y_offset);
195 * Unbind panel properties.
200 fgUntie("/sim/panel/visibility");
201 fgUntie("/sim/panel/x-offset");
202 fgUntie("/sim/panel/y-offset");
213 // Do nothing if the panel isn't visible.
214 if (!fgPanelVisible())
217 // If the mouse is down, do something
220 if (_mouseDelay < 0) {
221 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
226 // Now, draw the panel
227 glMatrixMode(GL_PROJECTION);
230 gluOrtho2D(_winx, _winx + _winw, _winy, _winy + _winh);
232 glMatrixMode(GL_MODELVIEW);
236 glTranslated(_x_offset, _y_offset, 0);
238 // Draw the background
239 glEnable(GL_TEXTURE_2D);
240 glDisable(GL_LIGHTING);
242 glEnable(GL_ALPHA_TEST);
243 glEnable(GL_COLOR_MATERIAL);
244 // glColor4f(1.0, 1.0, 1.0, 1.0);
245 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
246 glColor4fv( cur_light_params.scene_diffuse );
248 glColor4f(0.7, 0.2, 0.2, 1.0);
250 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
253 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
255 glTexCoord2f(0.0, 0.0); glVertex3f(_winx, _winy, 0);
256 glTexCoord2f(1.0, 0.0); glVertex3f(_winx + _width, _winy, 0);
257 glTexCoord2f(1.0, 1.0); glVertex3f(_winx + _width, _winy + _height, 0);
258 glTexCoord2f(0.0, 1.0); glVertex3f(_winx, _winy + _height, 0);
261 // Draw the instruments.
262 instrument_list_type::const_iterator current = _instruments.begin();
263 instrument_list_type::const_iterator end = _instruments.end();
265 for ( ; current != end; current++) {
266 FGPanelInstrument * instr = *current;
268 glTranslated(_x_offset, _y_offset, 0);
269 glTranslated(instr->getXPos(), instr->getYPos(), 0);
273 glMatrixMode(GL_PROJECTION);
275 glMatrixMode(GL_MODELVIEW);
277 ssgForceBasicState();
278 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
283 * Set the panel's visibility.
286 FGPanel::setVisibility (bool visibility)
288 _visibility = visibility;
293 * Return true if the panel is visible.
296 FGPanel::getVisibility () const
303 * Set the panel's background texture.
306 FGPanel::setBackground (ssgTexture * texture)
313 * Set the panel's x-offset.
316 FGPanel::setXOffset (int offset)
318 if (offset <= 0 && offset >= -_width + _winw)
324 * Set the panel's y-offset.
327 FGPanel::setYOffset (int offset)
329 if (offset <= 0 && offset >= -_height)
335 * Perform a mouse action.
338 FGPanel::doMouseAction (int button, int updown, int x, int y)
341 // Note a released button and return
342 // cerr << "Doing mouse action\n";
345 _mouseInstrument = 0;
349 // Scale for the real window size.
350 x = int(((float)x / fgGetInt("/sim/startup/xsize")) * _winw);
351 y = int(_winh - (((float)y / fgGetInt("/sim/startup/ysize"))
354 // Adjust for offsets.
358 // Search for a matching instrument.
359 for (int i = 0; i < (int)_instruments.size(); i++) {
360 FGPanelInstrument *inst = _instruments[i];
361 int ix = inst->getXPos();
362 int iy = inst->getYPos();
363 int iw = inst->getWidth() / 2;
364 int ih = inst->getHeight() / 2;
365 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
368 _mouseInstrument = inst;
369 _mouseButton = button;
372 // Always do the action once.
373 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
382 ////////////////////////////////////////////////////////////////////////.
383 // Implementation of FGPanelAction.
384 ////////////////////////////////////////////////////////////////////////
386 FGPanelAction::FGPanelAction ()
390 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
391 : _button(button), _x(x), _y(y), _w(w), _h(h)
395 FGPanelAction::~FGPanelAction ()
401 ////////////////////////////////////////////////////////////////////////
402 // Implementation of FGAdjustAction.
403 ////////////////////////////////////////////////////////////////////////
405 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
406 SGValue * value, float increment,
407 float min, float max, bool wrap)
408 : FGPanelAction(button, x, y, w, h),
409 _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
413 FGAdjustAction::~FGAdjustAction ()
418 FGAdjustAction::doAction ()
420 float val = _value->getFloatValue();
423 val = (_wrap ? _max : _min);
424 } else if (val > _max) {
425 val = (_wrap ? _min : _max);
427 _value->setDoubleValue(val);
432 ////////////////////////////////////////////////////////////////////////
433 // Implementation of FGSwapAction.
434 ////////////////////////////////////////////////////////////////////////
436 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
437 SGValue * value1, SGValue * value2)
438 : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
442 FGSwapAction::~FGSwapAction ()
447 FGSwapAction::doAction ()
449 float val = _value1->getFloatValue();
450 _value1->setDoubleValue(_value2->getFloatValue());
451 _value2->setDoubleValue(val);
456 ////////////////////////////////////////////////////////////////////////
457 // Implementation of FGToggleAction.
458 ////////////////////////////////////////////////////////////////////////
460 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
462 : FGPanelAction(button, x, y, w, h), _value(value)
466 FGToggleAction::~FGToggleAction ()
471 FGToggleAction::doAction ()
473 _value->setBoolValue(!(_value->getBoolValue()));
478 ////////////////////////////////////////////////////////////////////////
479 // Implementation of FGPanelTransformation.
480 ////////////////////////////////////////////////////////////////////////
482 FGPanelTransformation::FGPanelTransformation ()
487 FGPanelTransformation::~FGPanelTransformation ()
493 ////////////////////////////////////////////////////////////////////////
494 // Implementation of FGPanelInstrument.
495 ////////////////////////////////////////////////////////////////////////
498 FGPanelInstrument::FGPanelInstrument ()
504 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
510 FGPanelInstrument::~FGPanelInstrument ()
512 for (action_list_type::iterator it = _actions.begin();
513 it != _actions.end();
521 FGPanelInstrument::setPosition (int x, int y)
528 FGPanelInstrument::setSize (int w, int h)
535 FGPanelInstrument::getXPos () const
541 FGPanelInstrument::getYPos () const
547 FGPanelInstrument::getWidth () const
553 FGPanelInstrument::getHeight () const
559 FGPanelInstrument::addAction (FGPanelAction * action)
561 _actions.push_back(action);
564 // Coordinates relative to centre.
566 FGPanelInstrument::doMouseAction (int button, int x, int y)
568 action_list_type::iterator it = _actions.begin();
569 action_list_type::iterator last = _actions.end();
570 for ( ; it != last; it++) {
571 if ((*it)->inArea(button, x, y)) {
581 ////////////////////////////////////////////////////////////////////////
582 // Implementation of FGLayeredInstrument.
583 ////////////////////////////////////////////////////////////////////////
585 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
586 : FGPanelInstrument(x, y, w, h)
590 FGLayeredInstrument::~FGLayeredInstrument ()
592 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
599 FGLayeredInstrument::draw ()
601 for (int i = 0; i < (int)_layers.size(); i++) {
603 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
610 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
612 int n = _layers.size();
613 if (layer->getWidth() == -1) {
614 layer->setWidth(getWidth());
616 if (layer->getHeight() == -1) {
617 layer->setHeight(getHeight());
619 _layers.push_back(layer);
624 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
627 return addLayer(new FGTexturedLayer(texture, w, h));
631 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
633 int layer = _layers.size() - 1;
634 _layers[layer]->addTransformation(transformation);
639 ////////////////////////////////////////////////////////////////////////
640 // Implementation of FGInstrumentLayer.
641 ////////////////////////////////////////////////////////////////////////
643 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
649 FGInstrumentLayer::~FGInstrumentLayer ()
651 for (transformation_list::iterator it = _transformations.begin();
652 it != _transformations.end();
660 FGInstrumentLayer::transform () const
662 transformation_list::const_iterator it = _transformations.begin();
663 transformation_list::const_iterator last = _transformations.end();
665 FGPanelTransformation *t = *it;
666 float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
669 } else if (val > t->max) {
673 val = val * t->factor + t->offset;
675 val = t->table->interpolate(val) * t->factor + t->offset;
679 case FGPanelTransformation::XSHIFT:
680 glTranslatef(val, 0.0, 0.0);
682 case FGPanelTransformation::YSHIFT:
683 glTranslatef(0.0, val, 0.0);
685 case FGPanelTransformation::ROTATION:
686 glRotatef(-val, 0.0, 0.0, 1.0);
694 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
696 _transformations.push_back(transformation);
701 ////////////////////////////////////////////////////////////////////////
702 // Implementation of FGTexturedLayer.
703 ////////////////////////////////////////////////////////////////////////
706 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
707 : FGInstrumentLayer(w, h)
713 FGTexturedLayer::~FGTexturedLayer ()
719 FGTexturedLayer::draw ()
725 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
728 // From Curt: turn on the panel
729 // lights after sundown.
730 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
731 glColor4fv( cur_light_params.scene_diffuse );
733 glColor4f(0.7, 0.2, 0.2, 1.0);
737 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
738 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
739 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
740 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
746 ////////////////////////////////////////////////////////////////////////
747 // Implementation of FGTextLayer.
748 ////////////////////////////////////////////////////////////////////////
750 FGTextLayer::FGTextLayer (int w, int h)
751 : FGInstrumentLayer(w, h), _pointSize(14.0)
754 _color[0] = _color[1] = _color[2] = 0.0;
758 FGTextLayer::~FGTextLayer ()
760 chunk_list::iterator it = _chunks.begin();
761 chunk_list::iterator last = _chunks.end();
762 for ( ; it != last; it++) {
773 text_renderer.setFont(guiFntHandle);
774 text_renderer.setPointSize(_pointSize);
775 text_renderer.begin();
776 text_renderer.start3f(0, 0, 0);
779 if (_now - _then > 100000) {
783 text_renderer.puts((char *)(_value.c_str()));
786 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
791 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
793 _chunks.push_back(chunk);
797 FGTextLayer::setColor (float r, float g, float b)
806 FGTextLayer::setPointSize (float size)
812 FGTextLayer::setFont(fntFont * font)
814 text_renderer.setFont(font);
819 FGTextLayer::recalc_value () const
822 chunk_list::const_iterator it = _chunks.begin();
823 chunk_list::const_iterator last = _chunks.end();
824 for ( ; it != last; it++) {
825 _value += (*it)->getValue();
831 ////////////////////////////////////////////////////////////////////////
832 // Implementation of FGTextLayer::Chunk.
833 ////////////////////////////////////////////////////////////////////////
835 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
836 : _type(FGTextLayer::TEXT), _fmt(fmt)
843 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
844 const string &fmt, float mult)
845 : _type(type), _fmt(fmt), _mult(mult)
848 if (type == TEXT_VALUE)
857 FGTextLayer::Chunk::getValue () const
861 sprintf(_buf, _fmt.c_str(), _text.c_str());
864 sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
867 sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
875 ////////////////////////////////////////////////////////////////////////
876 // Implementation of FGSwitchLayer.
877 ////////////////////////////////////////////////////////////////////////
879 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
880 FGInstrumentLayer * layer1,
881 FGInstrumentLayer * layer2)
882 : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
886 FGSwitchLayer::~FGSwitchLayer ()
893 FGSwitchLayer::draw ()
896 if (_value->getBoolValue()) {