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");
214 // Do nothing if the panel isn't visible.
215 if (!fgPanelVisible())
218 // If the mouse is down, do something
221 if (_mouseDelay < 0) {
222 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
227 // Now, draw the panel
228 glMatrixMode(GL_PROJECTION);
231 gluOrtho2D(_winx, _winx + _winw, _winy, _winy + _winh);
233 glMatrixMode(GL_MODELVIEW);
237 glTranslated(_x_offset, _y_offset, 0);
239 // Draw the background
240 glEnable(GL_TEXTURE_2D);
241 glDisable(GL_LIGHTING);
243 glEnable(GL_ALPHA_TEST);
244 glEnable(GL_COLOR_MATERIAL);
245 // glColor4f(1.0, 1.0, 1.0, 1.0);
246 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
247 glColor4fv( cur_light_params.scene_diffuse );
249 glColor4f(0.7, 0.2, 0.2, 1.0);
251 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
253 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
254 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
256 glTexCoord2f(0.0, 0.0); glVertex3f(_winx, _winy, 0);
257 glTexCoord2f(1.0, 0.0); glVertex3f(_winx + _width, _winy, 0);
258 glTexCoord2f(1.0, 1.0); glVertex3f(_winx + _width, _winy + _height, 0);
259 glTexCoord2f(0.0, 1.0); glVertex3f(_winx, _winy + _height, 0);
262 // Draw the instruments.
263 instrument_list_type::const_iterator current = _instruments.begin();
264 instrument_list_type::const_iterator end = _instruments.end();
266 for ( ; current != end; current++) {
267 FGPanelInstrument * instr = *current;
269 glTranslated(_x_offset, _y_offset, 0);
270 glTranslated(instr->getXPos(), instr->getYPos(), 0);
274 glMatrixMode(GL_PROJECTION);
276 glMatrixMode(GL_MODELVIEW);
278 ssgForceBasicState();
279 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
284 * Set the panel's visibility.
287 FGPanel::setVisibility (bool visibility)
289 _visibility = visibility;
294 * Return true if the panel is visible.
297 FGPanel::getVisibility () const
304 * Set the panel's background texture.
307 FGPanel::setBackground (ssgTexture * texture)
314 * Set the panel's x-offset.
317 FGPanel::setXOffset (int offset)
319 if (offset <= 0 && offset >= -_width + _winw)
325 * Set the panel's y-offset.
328 FGPanel::setYOffset (int offset)
330 if (offset <= 0 && offset >= -_height)
336 * Perform a mouse action.
339 FGPanel::doMouseAction (int button, int updown, int x, int y)
342 // Note a released button and return
343 // cerr << "Doing mouse action\n";
346 _mouseInstrument = 0;
350 // Scale for the real window size.
351 x = int(((float)x / fgGetInt("/sim/startup/xsize")) * _winw);
352 y = int(_winh - (((float)y / fgGetInt("/sim/startup/ysize"))
355 // Adjust for offsets.
359 // Search for a matching instrument.
360 for (int i = 0; i < (int)_instruments.size(); i++) {
361 FGPanelInstrument *inst = _instruments[i];
362 int ix = inst->getXPos();
363 int iy = inst->getYPos();
364 int iw = inst->getWidth() / 2;
365 int ih = inst->getHeight() / 2;
366 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
369 _mouseInstrument = inst;
370 _mouseButton = button;
373 // Always do the action once.
374 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
383 ////////////////////////////////////////////////////////////////////////.
384 // Implementation of FGPanelAction.
385 ////////////////////////////////////////////////////////////////////////
387 FGPanelAction::FGPanelAction ()
391 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
392 : _button(button), _x(x), _y(y), _w(w), _h(h)
396 FGPanelAction::~FGPanelAction ()
402 ////////////////////////////////////////////////////////////////////////
403 // Implementation of FGAdjustAction.
404 ////////////////////////////////////////////////////////////////////////
406 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
407 SGValue * value, float increment,
408 float min, float max, bool wrap)
409 : FGPanelAction(button, x, y, w, h),
410 _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
414 FGAdjustAction::~FGAdjustAction ()
419 FGAdjustAction::doAction ()
421 float val = _value->getFloatValue();
424 val = (_wrap ? _max : _min);
425 } else if (val > _max) {
426 val = (_wrap ? _min : _max);
428 _value->setDoubleValue(val);
433 ////////////////////////////////////////////////////////////////////////
434 // Implementation of FGSwapAction.
435 ////////////////////////////////////////////////////////////////////////
437 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
438 SGValue * value1, SGValue * value2)
439 : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
443 FGSwapAction::~FGSwapAction ()
448 FGSwapAction::doAction ()
450 float val = _value1->getFloatValue();
451 _value1->setDoubleValue(_value2->getFloatValue());
452 _value2->setDoubleValue(val);
457 ////////////////////////////////////////////////////////////////////////
458 // Implementation of FGToggleAction.
459 ////////////////////////////////////////////////////////////////////////
461 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
463 : FGPanelAction(button, x, y, w, h), _value(value)
467 FGToggleAction::~FGToggleAction ()
472 FGToggleAction::doAction ()
474 _value->setBoolValue(!(_value->getBoolValue()));
479 ////////////////////////////////////////////////////////////////////////
480 // Implementation of FGPanelTransformation.
481 ////////////////////////////////////////////////////////////////////////
483 FGPanelTransformation::FGPanelTransformation ()
488 FGPanelTransformation::~FGPanelTransformation ()
494 ////////////////////////////////////////////////////////////////////////
495 // Implementation of FGPanelInstrument.
496 ////////////////////////////////////////////////////////////////////////
499 FGPanelInstrument::FGPanelInstrument ()
505 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
511 FGPanelInstrument::~FGPanelInstrument ()
513 for (action_list_type::iterator it = _actions.begin();
514 it != _actions.end();
522 FGPanelInstrument::setPosition (int x, int y)
529 FGPanelInstrument::setSize (int w, int h)
536 FGPanelInstrument::getXPos () const
542 FGPanelInstrument::getYPos () const
548 FGPanelInstrument::getWidth () const
554 FGPanelInstrument::getHeight () const
560 FGPanelInstrument::addAction (FGPanelAction * action)
562 _actions.push_back(action);
565 // Coordinates relative to centre.
567 FGPanelInstrument::doMouseAction (int button, int x, int y)
569 action_list_type::iterator it = _actions.begin();
570 action_list_type::iterator last = _actions.end();
571 for ( ; it != last; it++) {
572 if ((*it)->inArea(button, x, y)) {
582 ////////////////////////////////////////////////////////////////////////
583 // Implementation of FGLayeredInstrument.
584 ////////////////////////////////////////////////////////////////////////
586 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
587 : FGPanelInstrument(x, y, w, h)
591 FGLayeredInstrument::~FGLayeredInstrument ()
593 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
600 FGLayeredInstrument::draw ()
602 for (int i = 0; i < (int)_layers.size(); i++) {
604 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
611 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
613 int n = _layers.size();
614 if (layer->getWidth() == -1) {
615 layer->setWidth(getWidth());
617 if (layer->getHeight() == -1) {
618 layer->setHeight(getHeight());
620 _layers.push_back(layer);
625 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
628 return addLayer(new FGTexturedLayer(texture, w, h));
632 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
634 int layer = _layers.size() - 1;
635 _layers[layer]->addTransformation(transformation);
640 ////////////////////////////////////////////////////////////////////////
641 // Implementation of FGInstrumentLayer.
642 ////////////////////////////////////////////////////////////////////////
644 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
650 FGInstrumentLayer::~FGInstrumentLayer ()
652 for (transformation_list::iterator it = _transformations.begin();
653 it != _transformations.end();
661 FGInstrumentLayer::transform () const
663 transformation_list::const_iterator it = _transformations.begin();
664 transformation_list::const_iterator last = _transformations.end();
666 FGPanelTransformation *t = *it;
667 float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
670 } else if (val > t->max) {
674 val = val * t->factor + t->offset;
676 val = t->table->interpolate(val) * t->factor + t->offset;
680 case FGPanelTransformation::XSHIFT:
681 glTranslatef(val, 0.0, 0.0);
683 case FGPanelTransformation::YSHIFT:
684 glTranslatef(0.0, val, 0.0);
686 case FGPanelTransformation::ROTATION:
687 glRotatef(-val, 0.0, 0.0, 1.0);
695 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
697 _transformations.push_back(transformation);
702 ////////////////////////////////////////////////////////////////////////
703 // Implementation of FGTexturedLayer.
704 ////////////////////////////////////////////////////////////////////////
707 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
708 : FGInstrumentLayer(w, h)
714 FGTexturedLayer::~FGTexturedLayer ()
720 FGTexturedLayer::draw ()
726 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
729 // From Curt: turn on the panel
730 // lights after sundown.
731 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
732 glColor4fv( cur_light_params.scene_diffuse );
734 glColor4f(0.7, 0.2, 0.2, 1.0);
738 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
739 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
740 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
741 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
747 ////////////////////////////////////////////////////////////////////////
748 // Implementation of FGTextLayer.
749 ////////////////////////////////////////////////////////////////////////
751 FGTextLayer::FGTextLayer (int w, int h)
752 : FGInstrumentLayer(w, h), _pointSize(14.0)
755 _color[0] = _color[1] = _color[2] = 0.0;
759 FGTextLayer::~FGTextLayer ()
761 chunk_list::iterator it = _chunks.begin();
762 chunk_list::iterator last = _chunks.end();
763 for ( ; it != last; it++) {
774 text_renderer.setFont(guiFntHandle);
775 text_renderer.setPointSize(_pointSize);
776 text_renderer.begin();
777 text_renderer.start3f(0, 0, 0);
780 if (_now - _then > 100000) {
784 text_renderer.puts((char *)(_value.c_str()));
787 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
792 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
794 _chunks.push_back(chunk);
798 FGTextLayer::setColor (float r, float g, float b)
807 FGTextLayer::setPointSize (float size)
813 FGTextLayer::setFont(fntFont * font)
815 text_renderer.setFont(font);
820 FGTextLayer::recalc_value () const
823 chunk_list::const_iterator it = _chunks.begin();
824 chunk_list::const_iterator last = _chunks.end();
825 for ( ; it != last; it++) {
826 _value += (*it)->getValue();
832 ////////////////////////////////////////////////////////////////////////
833 // Implementation of FGTextLayer::Chunk.
834 ////////////////////////////////////////////////////////////////////////
836 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
837 : _type(FGTextLayer::TEXT), _fmt(fmt)
844 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
845 const string &fmt, float mult)
846 : _type(type), _fmt(fmt), _mult(mult)
849 if (type == TEXT_VALUE)
858 FGTextLayer::Chunk::getValue () const
862 sprintf(_buf, _fmt.c_str(), _text.c_str());
865 sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
868 sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
876 ////////////////////////////////////////////////////////////////////////
877 // Implementation of FGSwitchLayer.
878 ////////////////////////////////////////////////////////////////////////
880 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
881 FGInstrumentLayer * layer1,
882 FGInstrumentLayer * layer2)
883 : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
887 FGSwitchLayer::~FGSwitchLayer ()
894 FGSwitchLayer::draw ()
897 if (_value->getBoolValue()) {