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 <Main/viewmgr.hxx>
40 #include <Objects/texload.h>
41 #include <Time/light.hxx>
53 ////////////////////////////////////////////////////////////////////////
55 ////////////////////////////////////////////////////////////////////////
59 * Calculate the aspect adjustment for the panel.
62 get_aspect_adjust (int xsize, int ysize)
64 float ideal_aspect = float(WIN_W) / float(WIN_H);
65 float real_aspect = float(xsize) / float(ysize);
66 return (real_aspect / ideal_aspect);
71 ////////////////////////////////////////////////////////////////////////
73 ////////////////////////////////////////////////////////////////////////
78 return ((current_panel != 0) &&
79 (current_panel->getVisibility()) &&
80 (globals->get_viewmgr()->get_current() == 0) &&
81 (globals->get_current_view()->get_view_offset() == 0.0));
86 ////////////////////////////////////////////////////////////////////////
87 // Implementation of FGTextureManager.
88 ////////////////////////////////////////////////////////////////////////
90 map<string,ssgTexture *> FGTextureManager::_textureMap;
93 FGTextureManager::createTexture (const string &relativePath)
95 ssgTexture * texture = _textureMap[relativePath];
97 SG_LOG( SG_COCKPIT, SG_DEBUG,
98 "Texture " << relativePath << " does not yet exist" );
99 SGPath tpath(globals->get_fg_root());
100 tpath.append(relativePath);
101 texture = new ssgTexture((char *)tpath.c_str(), false, false);
102 _textureMap[relativePath] = texture;
103 if (_textureMap[relativePath] == 0)
104 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
105 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
106 << " handle=" << texture->getHandle() );
115 ////////////////////////////////////////////////////////////////////////
116 // Implementation of FGCropped Texture.
117 ////////////////////////////////////////////////////////////////////////
120 FGCroppedTexture::FGCroppedTexture ()
121 : _path(""), _texture(0),
122 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
127 FGCroppedTexture::FGCroppedTexture (const string &path,
128 float minX, float minY,
129 float maxX, float maxY)
130 : _path(path), _texture(0),
131 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
136 FGCroppedTexture::~FGCroppedTexture ()
142 FGCroppedTexture::getTexture ()
145 _texture = FGTextureManager::createTexture(_path);
152 ////////////////////////////////////////////////////////////////////////
153 // Implementation of FGPanel.
154 ////////////////////////////////////////////////////////////////////////
156 FGPanel * current_panel = NULL;
157 static fntRenderer text_renderer;
166 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
167 _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
169 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
170 _ysize_node(fgGetNode("/sim/startup/ysize", true))
172 setVisibility(fgPanelVisible());
183 for (instrument_list_type::iterator it = _instruments.begin();
184 it != _instruments.end();
193 * Add an instrument to the panel.
196 FGPanel::addInstrument (FGPanelInstrument * instrument)
198 _instruments.push_back(instrument);
203 * Initialize the panel.
213 * Bind panel properties.
218 fgTie("/sim/panel/visibility", &_visibility);
219 fgSetArchivable("/sim/panel/visibility");
220 fgTie("/sim/panel/x-offset", &_x_offset);
221 fgSetArchivable("/sim/panel/x-offset");
222 fgTie("/sim/panel/y-offset", &_y_offset);
223 fgSetArchivable("/sim/panel/y-offset");
229 * Unbind panel properties.
234 fgUntie("/sim/panel/visibility");
235 fgUntie("/sim/panel/x-offset");
236 fgUntie("/sim/panel/y-offset");
247 // Do nothing if the panel isn't visible.
248 if (!fgPanelVisible())
251 // If the mouse is down, do something
254 if (_mouseDelay < 0) {
255 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
260 // Now, draw the panel
261 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
262 _ysize_node->getIntValue());
263 if (aspect_adjust <1.0)
264 update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
266 update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
271 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
273 glMatrixMode(GL_PROJECTION);
276 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
277 // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
279 glMatrixMode(GL_MODELVIEW);
283 glTranslated(_x_offset, _y_offset, 0);
285 // Draw the background
286 glEnable(GL_TEXTURE_2D);
287 glDisable(GL_LIGHTING);
289 glEnable(GL_ALPHA_TEST);
290 glEnable(GL_COLOR_MATERIAL);
291 // glColor4f(1.0, 1.0, 1.0, 1.0);
292 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
293 glColor4fv( cur_light_params.scene_diffuse );
295 glColor4f(0.7, 0.2, 0.2, 1.0);
297 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
298 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
299 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
300 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
302 glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
303 glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
304 glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
305 glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
308 // Draw the instruments.
309 instrument_list_type::const_iterator current = _instruments.begin();
310 instrument_list_type::const_iterator end = _instruments.end();
312 for ( ; current != end; current++) {
313 FGPanelInstrument * instr = *current;
315 glTranslated(_x_offset, _y_offset, 0);
316 glTranslated(instr->getXPos(), instr->getYPos(), 0);
320 glMatrixMode(GL_PROJECTION);
322 glMatrixMode(GL_MODELVIEW);
324 ssgForceBasicState();
325 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
330 * Set the panel's visibility.
333 FGPanel::setVisibility (bool visibility)
335 _visibility = visibility;
340 * Return true if the panel is visible.
343 FGPanel::getVisibility () const
350 * Set the panel's background texture.
353 FGPanel::setBackground (ssgTexture * texture)
360 * Set the panel's x-offset.
363 FGPanel::setXOffset (int offset)
365 if (offset <= 0 && offset >= -_width + WIN_W)
371 * Set the panel's y-offset.
374 FGPanel::setYOffset (int offset)
376 if (offset <= 0 && offset >= -_height)
382 * Perform a mouse action.
385 FGPanel::doMouseAction (int button, int updown, int x, int y)
387 // FIXME: this same code appears in update()
388 int xsize = _xsize_node->getIntValue();
389 int ysize = _ysize_node->getIntValue();
390 float aspect_adjust = get_aspect_adjust(xsize, ysize);
392 // Note a released button and return
393 // cerr << "Doing mouse action\n";
396 _mouseInstrument = 0;
400 // Scale for the real window size.
401 if (aspect_adjust < 1.0) {
402 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
403 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
405 x = int(((float)x / xsize) * WIN_W);
406 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
409 // Adjust for offsets.
413 // Search for a matching instrument.
414 for (int i = 0; i < (int)_instruments.size(); i++) {
415 FGPanelInstrument *inst = _instruments[i];
416 int ix = inst->getXPos();
417 int iy = inst->getYPos();
418 int iw = inst->getWidth() / 2;
419 int ih = inst->getHeight() / 2;
420 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
423 _mouseInstrument = inst;
424 _mouseButton = button;
427 // Always do the action once.
428 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
437 ////////////////////////////////////////////////////////////////////////.
438 // Implementation of FGPanelAction.
439 ////////////////////////////////////////////////////////////////////////
441 FGPanelAction::FGPanelAction ()
445 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
446 : _button(button), _x(x), _y(y), _w(w), _h(h)
450 FGPanelAction::~FGPanelAction ()
455 FGPanelAction::addBinding (const FGBinding &binding)
457 _bindings.push_back(binding);
461 FGPanelAction::doAction ()
463 int nBindings = _bindings.size();
464 for (int i = 0; i < nBindings; i++) {
471 ////////////////////////////////////////////////////////////////////////
472 // Implementation of FGPanelTransformation.
473 ////////////////////////////////////////////////////////////////////////
475 FGPanelTransformation::FGPanelTransformation ()
480 FGPanelTransformation::~FGPanelTransformation ()
486 ////////////////////////////////////////////////////////////////////////
487 // Implementation of FGPanelInstrument.
488 ////////////////////////////////////////////////////////////////////////
491 FGPanelInstrument::FGPanelInstrument ()
497 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
503 FGPanelInstrument::~FGPanelInstrument ()
505 for (action_list_type::iterator it = _actions.begin();
506 it != _actions.end();
514 FGPanelInstrument::setPosition (int x, int y)
521 FGPanelInstrument::setSize (int w, int h)
528 FGPanelInstrument::getXPos () const
534 FGPanelInstrument::getYPos () const
540 FGPanelInstrument::getWidth () const
546 FGPanelInstrument::getHeight () const
552 FGPanelInstrument::addAction (FGPanelAction * action)
554 _actions.push_back(action);
557 // Coordinates relative to centre.
559 FGPanelInstrument::doMouseAction (int button, int x, int y)
561 action_list_type::iterator it = _actions.begin();
562 action_list_type::iterator last = _actions.end();
563 for ( ; it != last; it++) {
564 if ((*it)->inArea(button, x, y)) {
574 ////////////////////////////////////////////////////////////////////////
575 // Implementation of FGLayeredInstrument.
576 ////////////////////////////////////////////////////////////////////////
578 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
579 : FGPanelInstrument(x, y, w, h)
583 FGLayeredInstrument::~FGLayeredInstrument ()
585 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
592 FGLayeredInstrument::draw ()
594 for (int i = 0; i < (int)_layers.size(); i++) {
596 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
603 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
605 int n = _layers.size();
606 if (layer->getWidth() == -1) {
607 layer->setWidth(getWidth());
609 if (layer->getHeight() == -1) {
610 layer->setHeight(getHeight());
612 _layers.push_back(layer);
617 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
620 return addLayer(new FGTexturedLayer(texture, w, h));
624 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
626 int layer = _layers.size() - 1;
627 _layers[layer]->addTransformation(transformation);
632 ////////////////////////////////////////////////////////////////////////
633 // Implementation of FGInstrumentLayer.
634 ////////////////////////////////////////////////////////////////////////
636 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
642 FGInstrumentLayer::~FGInstrumentLayer ()
644 for (transformation_list::iterator it = _transformations.begin();
645 it != _transformations.end();
653 FGInstrumentLayer::transform () const
655 transformation_list::const_iterator it = _transformations.begin();
656 transformation_list::const_iterator last = _transformations.end();
658 FGPanelTransformation *t = *it;
659 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
662 } else if (val > t->max) {
666 val = val * t->factor + t->offset;
668 val = t->table->interpolate(val) * t->factor + t->offset;
672 case FGPanelTransformation::XSHIFT:
673 glTranslatef(val, 0.0, 0.0);
675 case FGPanelTransformation::YSHIFT:
676 glTranslatef(0.0, val, 0.0);
678 case FGPanelTransformation::ROTATION:
679 glRotatef(-val, 0.0, 0.0, 1.0);
687 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
689 _transformations.push_back(transformation);
694 ////////////////////////////////////////////////////////////////////////
695 // Implementation of FGTexturedLayer.
696 ////////////////////////////////////////////////////////////////////////
699 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
700 : FGInstrumentLayer(w, h)
706 FGTexturedLayer::~FGTexturedLayer ()
712 FGTexturedLayer::draw ()
718 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
721 // From Curt: turn on the panel
722 // lights after sundown.
723 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
724 glColor4fv( cur_light_params.scene_diffuse );
726 glColor4f(0.7, 0.2, 0.2, 1.0);
730 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
731 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
732 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
733 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
739 ////////////////////////////////////////////////////////////////////////
740 // Implementation of FGTextLayer.
741 ////////////////////////////////////////////////////////////////////////
743 FGTextLayer::FGTextLayer (int w, int h)
744 : FGInstrumentLayer(w, h), _pointSize(14.0)
747 _color[0] = _color[1] = _color[2] = 0.0;
751 FGTextLayer::~FGTextLayer ()
753 chunk_list::iterator it = _chunks.begin();
754 chunk_list::iterator last = _chunks.end();
755 for ( ; it != last; it++) {
766 text_renderer.setFont(guiFntHandle);
767 text_renderer.setPointSize(_pointSize);
768 text_renderer.begin();
769 text_renderer.start3f(0, 0, 0);
772 if (_now - _then > 100000) {
776 text_renderer.puts((char *)(_value.c_str()));
779 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
784 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
786 _chunks.push_back(chunk);
790 FGTextLayer::setColor (float r, float g, float b)
799 FGTextLayer::setPointSize (float size)
805 FGTextLayer::setFont(fntFont * font)
807 text_renderer.setFont(font);
812 FGTextLayer::recalc_value () const
815 chunk_list::const_iterator it = _chunks.begin();
816 chunk_list::const_iterator last = _chunks.end();
817 for ( ; it != last; it++) {
818 _value += (*it)->getValue();
824 ////////////////////////////////////////////////////////////////////////
825 // Implementation of FGTextLayer::Chunk.
826 ////////////////////////////////////////////////////////////////////////
828 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
829 : _type(FGTextLayer::TEXT), _fmt(fmt)
836 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
837 const string &fmt, float mult)
838 : _type(type), _fmt(fmt), _mult(mult)
841 if (type == TEXT_VALUE)
850 FGTextLayer::Chunk::getValue () const
854 sprintf(_buf, _fmt.c_str(), _text.c_str());
857 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
860 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
868 ////////////////////////////////////////////////////////////////////////
869 // Implementation of FGSwitchLayer.
870 ////////////////////////////////////////////////////////////////////////
872 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
873 FGInstrumentLayer * layer1,
874 FGInstrumentLayer * layer2)
875 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
879 FGSwitchLayer::~FGSwitchLayer ()
886 FGSwitchLayer::draw ()
889 if (_node->getBoolValue()) {