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))
140 setVisibility(fgPanelVisible());
150 for (instrument_list_type::iterator it = _instruments.begin();
151 it != _instruments.end();
160 * Add an instrument to the panel.
163 FGPanel::addInstrument (FGPanelInstrument * instrument)
165 _instruments.push_back(instrument);
170 * Initialize the panel.
180 * Bind panel properties.
185 fgTie("/sim/panel/visibility", &_visibility);
186 fgTie("/sim/panel/x-offset", &_x_offset);
187 fgTie("/sim/panel/y-offset", &_y_offset);
192 * Unbind panel properties.
197 fgUntie("/sim/panel/visibility");
198 fgUntie("/sim/panel/x-offset");
199 fgUntie("/sim/panel/y-offset");
209 // Do nothing if the panel isn't visible.
210 if (!fgPanelVisible())
213 // If the mouse is down, do something
216 if (_mouseDelay < 0) {
217 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
222 // Now, draw the panel
223 glMatrixMode(GL_PROJECTION);
226 gluOrtho2D(_winx, _winx + _winw, _winy, _winy + _winh);
228 glMatrixMode(GL_MODELVIEW);
232 glTranslated(_x_offset, _y_offset, 0);
234 // Draw the background
235 glEnable(GL_TEXTURE_2D);
236 glDisable(GL_LIGHTING);
238 glEnable(GL_ALPHA_TEST);
239 glEnable(GL_COLOR_MATERIAL);
240 // glColor4f(1.0, 1.0, 1.0, 1.0);
241 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
242 glColor4fv( cur_light_params.scene_diffuse );
244 glColor4f(0.7, 0.2, 0.2, 1.0);
246 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
248 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
249 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
251 glTexCoord2f(0.0, 0.0); glVertex3f(_winx, _winy, 0);
252 glTexCoord2f(1.0, 0.0); glVertex3f(_winx + _width, _winy, 0);
253 glTexCoord2f(1.0, 1.0); glVertex3f(_winx + _width, _winy + _height, 0);
254 glTexCoord2f(0.0, 1.0); glVertex3f(_winx, _winy + _height, 0);
257 // Draw the instruments.
258 instrument_list_type::const_iterator current = _instruments.begin();
259 instrument_list_type::const_iterator end = _instruments.end();
261 for ( ; current != end; current++) {
262 FGPanelInstrument * instr = *current;
264 glTranslated(_x_offset, _y_offset, 0);
265 glTranslated(instr->getXPos(), instr->getYPos(), 0);
269 glMatrixMode(GL_PROJECTION);
271 glMatrixMode(GL_MODELVIEW);
273 ssgForceBasicState();
274 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
279 * Set the panel's visibility.
282 FGPanel::setVisibility (bool visibility)
284 _visibility = visibility;
289 * Return true if the panel is visible.
292 FGPanel::getVisibility () const
299 * Set the panel's background texture.
302 FGPanel::setBackground (ssgTexture * texture)
309 * Set the panel's x-offset.
312 FGPanel::setXOffset (int offset)
314 if (offset <= 0 && offset >= -_width + _winw)
320 * Set the panel's y-offset.
323 FGPanel::setYOffset (int offset)
325 if (offset <= 0 && offset >= -_height)
331 * Perform a mouse action.
334 FGPanel::doMouseAction (int button, int updown, int x, int y)
337 // Note a released button and return
338 // cerr << "Doing mouse action\n";
341 _mouseInstrument = 0;
345 // Scale for the real window size.
346 x = int(((float)x / fgGetInt("/sim/startup/xsize")) * _winw);
347 y = int(_winh - (((float)y / fgGetInt("/sim/startup/ysize"))
350 // Adjust for offsets.
354 // Search for a matching instrument.
355 for (int i = 0; i < (int)_instruments.size(); i++) {
356 FGPanelInstrument *inst = _instruments[i];
357 int ix = inst->getXPos();
358 int iy = inst->getYPos();
359 int iw = inst->getWidth() / 2;
360 int ih = inst->getHeight() / 2;
361 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
364 _mouseInstrument = inst;
365 _mouseButton = button;
368 // Always do the action once.
369 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
378 ////////////////////////////////////////////////////////////////////////.
379 // Implementation of FGPanelAction.
380 ////////////////////////////////////////////////////////////////////////
382 FGPanelAction::FGPanelAction ()
386 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
387 : _button(button), _x(x), _y(y), _w(w), _h(h)
391 FGPanelAction::~FGPanelAction ()
397 ////////////////////////////////////////////////////////////////////////
398 // Implementation of FGAdjustAction.
399 ////////////////////////////////////////////////////////////////////////
401 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
402 SGValue * value, float increment,
403 float min, float max, bool wrap)
404 : FGPanelAction(button, x, y, w, h),
405 _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
409 FGAdjustAction::~FGAdjustAction ()
414 FGAdjustAction::doAction ()
416 float val = _value->getFloatValue();
419 val = (_wrap ? _max : _min);
420 } else if (val > _max) {
421 val = (_wrap ? _min : _max);
423 _value->setDoubleValue(val);
428 ////////////////////////////////////////////////////////////////////////
429 // Implementation of FGSwapAction.
430 ////////////////////////////////////////////////////////////////////////
432 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
433 SGValue * value1, SGValue * value2)
434 : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
438 FGSwapAction::~FGSwapAction ()
443 FGSwapAction::doAction ()
445 float val = _value1->getFloatValue();
446 _value1->setDoubleValue(_value2->getFloatValue());
447 _value2->setDoubleValue(val);
452 ////////////////////////////////////////////////////////////////////////
453 // Implementation of FGToggleAction.
454 ////////////////////////////////////////////////////////////////////////
456 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
458 : FGPanelAction(button, x, y, w, h), _value(value)
462 FGToggleAction::~FGToggleAction ()
467 FGToggleAction::doAction ()
469 _value->setBoolValue(!(_value->getBoolValue()));
474 ////////////////////////////////////////////////////////////////////////
475 // Implementation of FGPanelTransformation.
476 ////////////////////////////////////////////////////////////////////////
478 FGPanelTransformation::FGPanelTransformation ()
482 FGPanelTransformation::~FGPanelTransformation ()
488 ////////////////////////////////////////////////////////////////////////
489 // Implementation of FGPanelInstrument.
490 ////////////////////////////////////////////////////////////////////////
493 FGPanelInstrument::FGPanelInstrument ()
499 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
505 FGPanelInstrument::~FGPanelInstrument ()
507 for (action_list_type::iterator it = _actions.begin();
508 it != _actions.end();
516 FGPanelInstrument::setPosition (int x, int y)
523 FGPanelInstrument::setSize (int w, int h)
530 FGPanelInstrument::getXPos () const
536 FGPanelInstrument::getYPos () const
542 FGPanelInstrument::getWidth () const
548 FGPanelInstrument::getHeight () const
554 FGPanelInstrument::addAction (FGPanelAction * action)
556 _actions.push_back(action);
559 // Coordinates relative to centre.
561 FGPanelInstrument::doMouseAction (int button, int x, int y)
563 action_list_type::iterator it = _actions.begin();
564 action_list_type::iterator last = _actions.end();
565 for ( ; it != last; it++) {
566 if ((*it)->inArea(button, x, y)) {
576 ////////////////////////////////////////////////////////////////////////
577 // Implementation of FGLayeredInstrument.
578 ////////////////////////////////////////////////////////////////////////
580 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
581 : FGPanelInstrument(x, y, w, h)
585 FGLayeredInstrument::~FGLayeredInstrument ()
587 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
594 FGLayeredInstrument::draw ()
596 for (int i = 0; i < (int)_layers.size(); i++) {
598 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
605 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
607 int n = _layers.size();
608 if (layer->getWidth() == -1) {
609 layer->setWidth(getWidth());
611 if (layer->getHeight() == -1) {
612 layer->setHeight(getHeight());
614 _layers.push_back(layer);
619 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
622 return addLayer(new FGTexturedLayer(texture, w, h));
626 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
628 int layer = _layers.size() - 1;
629 _layers[layer]->addTransformation(transformation);
634 ////////////////////////////////////////////////////////////////////////
635 // Implementation of FGInstrumentLayer.
636 ////////////////////////////////////////////////////////////////////////
638 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
644 FGInstrumentLayer::~FGInstrumentLayer ()
646 for (transformation_list::iterator it = _transformations.begin();
647 it != _transformations.end();
655 FGInstrumentLayer::transform () const
657 transformation_list::const_iterator it = _transformations.begin();
658 transformation_list::const_iterator last = _transformations.end();
660 FGPanelTransformation *t = *it;
661 float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
664 } else if (val > t->max) {
667 val = val * t->factor + t->offset;
670 case FGPanelTransformation::XSHIFT:
671 glTranslatef(val, 0.0, 0.0);
673 case FGPanelTransformation::YSHIFT:
674 glTranslatef(0.0, val, 0.0);
676 case FGPanelTransformation::ROTATION:
677 glRotatef(-val, 0.0, 0.0, 1.0);
685 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
687 _transformations.push_back(transformation);
692 ////////////////////////////////////////////////////////////////////////
693 // Implementation of FGTexturedLayer.
694 ////////////////////////////////////////////////////////////////////////
697 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
698 : FGInstrumentLayer(w, h)
704 FGTexturedLayer::~FGTexturedLayer ()
710 FGTexturedLayer::draw ()
716 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
719 // From Curt: turn on the panel
720 // lights after sundown.
721 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
722 glColor4fv( cur_light_params.scene_diffuse );
724 glColor4f(0.7, 0.2, 0.2, 1.0);
728 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
729 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
730 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
731 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
737 ////////////////////////////////////////////////////////////////////////
738 // Implementation of FGTextLayer.
739 ////////////////////////////////////////////////////////////////////////
741 FGTextLayer::FGTextLayer (int w, int h)
742 : FGInstrumentLayer(w, h), _pointSize(14.0)
745 _color[0] = _color[1] = _color[2] = 0.0;
749 FGTextLayer::~FGTextLayer ()
751 chunk_list::iterator it = _chunks.begin();
752 chunk_list::iterator last = _chunks.end();
753 for ( ; it != last; it++) {
764 text_renderer.setFont(guiFntHandle);
765 text_renderer.setPointSize(_pointSize);
766 text_renderer.begin();
767 text_renderer.start3f(0, 0, 0);
770 if (_now - _then > 100000) {
774 text_renderer.puts((char *)(_value.c_str()));
777 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
782 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
784 _chunks.push_back(chunk);
788 FGTextLayer::setColor (float r, float g, float b)
797 FGTextLayer::setPointSize (float size)
803 FGTextLayer::setFont(fntFont * font)
805 text_renderer.setFont(font);
810 FGTextLayer::recalc_value () const
813 chunk_list::const_iterator it = _chunks.begin();
814 chunk_list::const_iterator last = _chunks.end();
815 for ( ; it != last; it++) {
816 _value += (*it)->getValue();
822 ////////////////////////////////////////////////////////////////////////
823 // Implementation of FGTextLayer::Chunk.
824 ////////////////////////////////////////////////////////////////////////
826 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
827 : _type(FGTextLayer::TEXT), _fmt(fmt)
834 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
835 const string &fmt, float mult)
836 : _type(type), _fmt(fmt), _mult(mult)
839 if (type == TEXT_VALUE)
848 FGTextLayer::Chunk::getValue () const
852 sprintf(_buf, _fmt.c_str(), _text.c_str());
855 sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
858 sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
866 ////////////////////////////////////////////////////////////////////////
867 // Implementation of FGSwitchLayer.
868 ////////////////////////////////////////////////////////////////////////
870 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
871 FGInstrumentLayer * layer1,
872 FGInstrumentLayer * layer2)
873 : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
877 FGSwitchLayer::~FGSwitchLayer ()
884 FGSwitchLayer::draw ()
887 if (_value->getBoolValue()) {