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>
52 #pragma warn A sloppy coder has defined NONE as a macro!!!
58 ////////////////////////////////////////////////////////////////////////
60 ////////////////////////////////////////////////////////////////////////
64 * Calculate the aspect adjustment for the panel.
67 get_aspect_adjust (int xsize, int ysize)
69 float ideal_aspect = float(WIN_W) / float(WIN_H);
70 float real_aspect = float(xsize) / float(ysize);
71 return (real_aspect / ideal_aspect);
76 ////////////////////////////////////////////////////////////////////////
78 ////////////////////////////////////////////////////////////////////////
83 return ((current_panel != 0) &&
84 (current_panel->getVisibility()) &&
85 (globals->get_viewmgr()->get_current() == 0) &&
86 (globals->get_current_view()->get_view_offset() == 0.0));
91 ////////////////////////////////////////////////////////////////////////
92 // Implementation of FGTextureManager.
93 ////////////////////////////////////////////////////////////////////////
95 map<string,ssgTexture *> FGTextureManager::_textureMap;
98 FGTextureManager::createTexture (const string &relativePath)
100 ssgTexture * texture = _textureMap[relativePath];
102 SG_LOG( SG_COCKPIT, SG_DEBUG,
103 "Texture " << relativePath << " does not yet exist" );
104 SGPath tpath(globals->get_fg_root());
105 tpath.append(relativePath);
106 texture = new ssgTexture((char *)tpath.c_str(), false, false);
107 _textureMap[relativePath] = texture;
108 if (_textureMap[relativePath] == 0)
109 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
110 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
111 << " handle=" << texture->getHandle() );
120 ////////////////////////////////////////////////////////////////////////
121 // Implementation of FGCropped Texture.
122 ////////////////////////////////////////////////////////////////////////
125 FGCroppedTexture::FGCroppedTexture ()
126 : _path(""), _texture(0),
127 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
132 FGCroppedTexture::FGCroppedTexture (const string &path,
133 float minX, float minY,
134 float maxX, float maxY)
135 : _path(path), _texture(0),
136 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
141 FGCroppedTexture::~FGCroppedTexture ()
147 FGCroppedTexture::getTexture ()
150 _texture = FGTextureManager::createTexture(_path);
157 ////////////////////////////////////////////////////////////////////////
158 // Implementation of FGPanel.
159 ////////////////////////////////////////////////////////////////////////
161 FGPanel * current_panel = NULL;
162 static fntRenderer text_renderer;
171 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
172 _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
175 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
176 _ysize_node(fgGetNode("/sim/startup/ysize", true))
178 setVisibility(fgPanelVisible());
189 for (instrument_list_type::iterator it = _instruments.begin();
190 it != _instruments.end();
199 * Add an instrument to the panel.
202 FGPanel::addInstrument (FGPanelInstrument * instrument)
204 _instruments.push_back(instrument);
209 * Initialize the panel.
219 * Bind panel properties.
224 fgTie("/sim/panel/visibility", &_visibility);
225 fgSetArchivable("/sim/panel/visibility");
226 fgTie("/sim/panel/x-offset", &_x_offset);
227 fgSetArchivable("/sim/panel/x-offset");
228 fgTie("/sim/panel/y-offset", &_y_offset);
229 fgSetArchivable("/sim/panel/y-offset");
230 fgTie("/sim/panel/jitter", &_jitter);
231 fgSetArchivable("/sim/panel/jitter");
237 * Unbind panel properties.
242 fgUntie("/sim/panel/visibility");
243 fgUntie("/sim/panel/x-offset");
244 fgUntie("/sim/panel/y-offset");
255 // Do nothing if the panel isn't visible.
256 if (!fgPanelVisible())
259 // If the mouse is down, do something
262 if (_mouseDelay < 0) {
263 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
268 // Now, draw the panel
269 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
270 _ysize_node->getIntValue());
271 if (aspect_adjust <1.0)
272 update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
274 update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
279 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
281 // Calculate accelerations
282 // and jiggle the panel accordingly
283 // The factors and bounds are just
284 // initial guesses; using sqrt smooths
286 double x_offset = _x_offset;
287 double y_offset = _y_offset;
289 if (_jitter != 0.0) {
290 double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
291 double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
292 double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
294 double a_zx_pilot = a_z_pilot - a_x_pilot;
296 int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
297 (a_y_pilot < 0 ? -1 : 1);
298 int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
299 (a_zx_pilot < 0 ? -1 : 1);
301 // adjustments in screen coordinates
302 x_offset += x_adjust;
303 y_offset += y_adjust;
306 glMatrixMode(GL_PROJECTION);
309 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
310 // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
312 glMatrixMode(GL_MODELVIEW);
316 glTranslated(x_offset, y_offset, 0);
318 // Draw the background
319 glEnable(GL_TEXTURE_2D);
320 glDisable(GL_LIGHTING);
322 glEnable(GL_ALPHA_TEST);
323 glEnable(GL_COLOR_MATERIAL);
324 // glColor4f(1.0, 1.0, 1.0, 1.0);
325 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
326 glColor4fv( cur_light_params.scene_diffuse );
328 glColor4f(0.7, 0.2, 0.2, 1.0);
330 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
331 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
332 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
333 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
335 glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
336 glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
337 glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
338 glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
341 // Draw the instruments.
342 instrument_list_type::const_iterator current = _instruments.begin();
343 instrument_list_type::const_iterator end = _instruments.end();
345 for ( ; current != end; current++) {
346 FGPanelInstrument * instr = *current;
348 glTranslated(x_offset, y_offset, 0);
349 glTranslated(instr->getXPos(), instr->getYPos(), 0);
353 glMatrixMode(GL_PROJECTION);
355 glMatrixMode(GL_MODELVIEW);
357 ssgForceBasicState();
358 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
363 * Set the panel's visibility.
366 FGPanel::setVisibility (bool visibility)
368 _visibility = visibility;
373 * Return true if the panel is visible.
376 FGPanel::getVisibility () const
383 * Set the panel's background texture.
386 FGPanel::setBackground (ssgTexture * texture)
393 * Set the panel's x-offset.
396 FGPanel::setXOffset (int offset)
398 if (offset <= 0 && offset >= -_width + WIN_W)
404 * Set the panel's y-offset.
407 FGPanel::setYOffset (int offset)
409 if (offset <= 0 && offset >= -_height)
415 * Perform a mouse action.
418 FGPanel::doMouseAction (int button, int updown, int x, int y)
420 // FIXME: this same code appears in update()
421 int xsize = _xsize_node->getIntValue();
422 int ysize = _ysize_node->getIntValue();
423 float aspect_adjust = get_aspect_adjust(xsize, ysize);
425 // Note a released button and return
426 // cerr << "Doing mouse action\n";
429 _mouseInstrument = 0;
433 // Scale for the real window size.
434 if (aspect_adjust < 1.0) {
435 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
436 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
438 x = int(((float)x / xsize) * WIN_W);
439 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
442 // Adjust for offsets.
446 // Search for a matching instrument.
447 for (int i = 0; i < (int)_instruments.size(); i++) {
448 FGPanelInstrument *inst = _instruments[i];
449 int ix = inst->getXPos();
450 int iy = inst->getYPos();
451 int iw = inst->getWidth() / 2;
452 int ih = inst->getHeight() / 2;
453 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
456 _mouseInstrument = inst;
457 _mouseButton = button;
460 // Always do the action once.
461 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
470 ////////////////////////////////////////////////////////////////////////.
471 // Implementation of FGPanelAction.
472 ////////////////////////////////////////////////////////////////////////
474 FGPanelAction::FGPanelAction ()
478 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
479 : _button(button), _x(x), _y(y), _w(w), _h(h)
481 for (unsigned int i = 0; i < _bindings.size(); i++)
485 FGPanelAction::~FGPanelAction ()
490 FGPanelAction::addBinding (FGBinding * binding)
492 _bindings.push_back(binding);
496 FGPanelAction::doAction ()
499 int nBindings = _bindings.size();
500 for (int i = 0; i < nBindings; i++) {
501 _bindings[i]->fire();
508 ////////////////////////////////////////////////////////////////////////
509 // Implementation of FGPanelTransformation.
510 ////////////////////////////////////////////////////////////////////////
512 FGPanelTransformation::FGPanelTransformation ()
517 FGPanelTransformation::~FGPanelTransformation ()
523 ////////////////////////////////////////////////////////////////////////
524 // Implementation of FGPanelInstrument.
525 ////////////////////////////////////////////////////////////////////////
528 FGPanelInstrument::FGPanelInstrument ()
534 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
540 FGPanelInstrument::~FGPanelInstrument ()
542 for (action_list_type::iterator it = _actions.begin();
543 it != _actions.end();
551 FGPanelInstrument::setPosition (int x, int y)
558 FGPanelInstrument::setSize (int w, int h)
565 FGPanelInstrument::getXPos () const
571 FGPanelInstrument::getYPos () const
577 FGPanelInstrument::getWidth () const
583 FGPanelInstrument::getHeight () const
589 FGPanelInstrument::addAction (FGPanelAction * action)
591 _actions.push_back(action);
594 // Coordinates relative to centre.
596 FGPanelInstrument::doMouseAction (int button, int x, int y)
599 action_list_type::iterator it = _actions.begin();
600 action_list_type::iterator last = _actions.end();
601 for ( ; it != last; it++) {
602 if ((*it)->inArea(button, x, y)) {
613 ////////////////////////////////////////////////////////////////////////
614 // Implementation of FGLayeredInstrument.
615 ////////////////////////////////////////////////////////////////////////
617 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
618 : FGPanelInstrument(x, y, w, h)
622 FGLayeredInstrument::~FGLayeredInstrument ()
624 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
631 FGLayeredInstrument::draw ()
634 for (int i = 0; i < (int)_layers.size(); i++) {
636 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
644 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
646 int n = _layers.size();
647 if (layer->getWidth() == -1) {
648 layer->setWidth(getWidth());
650 if (layer->getHeight() == -1) {
651 layer->setHeight(getHeight());
653 _layers.push_back(layer);
658 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
661 return addLayer(new FGTexturedLayer(texture, w, h));
665 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
667 int layer = _layers.size() - 1;
668 _layers[layer]->addTransformation(transformation);
673 ////////////////////////////////////////////////////////////////////////
674 // Implementation of FGInstrumentLayer.
675 ////////////////////////////////////////////////////////////////////////
677 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
683 FGInstrumentLayer::~FGInstrumentLayer ()
685 for (transformation_list::iterator it = _transformations.begin();
686 it != _transformations.end();
694 FGInstrumentLayer::transform () const
696 transformation_list::const_iterator it = _transformations.begin();
697 transformation_list::const_iterator last = _transformations.end();
699 FGPanelTransformation *t = *it;
701 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
704 } else if (val > t->max) {
708 val = val * t->factor + t->offset;
710 val = t->table->interpolate(val) * t->factor + t->offset;
714 case FGPanelTransformation::XSHIFT:
715 glTranslatef(val, 0.0, 0.0);
717 case FGPanelTransformation::YSHIFT:
718 glTranslatef(0.0, val, 0.0);
720 case FGPanelTransformation::ROTATION:
721 glRotatef(-val, 0.0, 0.0, 1.0);
730 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
732 _transformations.push_back(transformation);
737 ////////////////////////////////////////////////////////////////////////
738 // Implementation of FGGroupLayer.
739 ////////////////////////////////////////////////////////////////////////
741 FGGroupLayer::FGGroupLayer ()
745 FGGroupLayer::~FGGroupLayer ()
747 for (unsigned int i = 0; i < _layers.size(); i++)
752 FGGroupLayer::draw ()
755 int nLayers = _layers.size();
756 for (int i = 0; i < nLayers; i++)
762 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
764 _layers.push_back(layer);
769 ////////////////////////////////////////////////////////////////////////
770 // Implementation of FGTexturedLayer.
771 ////////////////////////////////////////////////////////////////////////
774 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
775 : FGInstrumentLayer(w, h)
781 FGTexturedLayer::~FGTexturedLayer ()
787 FGTexturedLayer::draw ()
794 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
797 // From Curt: turn on the panel
798 // lights after sundown.
799 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
800 glColor4fv( cur_light_params.scene_diffuse );
802 glColor4f(0.7, 0.2, 0.2, 1.0);
806 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
807 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
808 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
809 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
816 ////////////////////////////////////////////////////////////////////////
817 // Implementation of FGTextLayer.
818 ////////////////////////////////////////////////////////////////////////
820 FGTextLayer::FGTextLayer (int w, int h)
821 : FGInstrumentLayer(w, h), _pointSize(14.0)
824 _color[0] = _color[1] = _color[2] = 0.0;
828 FGTextLayer::~FGTextLayer ()
830 chunk_list::iterator it = _chunks.begin();
831 chunk_list::iterator last = _chunks.end();
832 for ( ; it != last; it++) {
844 text_renderer.setFont(guiFntHandle);
845 text_renderer.setPointSize(_pointSize);
846 text_renderer.begin();
847 text_renderer.start3f(0, 0, 0);
850 if (_now - _then > 100000) {
854 text_renderer.puts((char *)(_value.c_str()));
857 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
863 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
865 _chunks.push_back(chunk);
869 FGTextLayer::setColor (float r, float g, float b)
878 FGTextLayer::setPointSize (float size)
884 FGTextLayer::setFont(fntFont * font)
886 text_renderer.setFont(font);
891 FGTextLayer::recalc_value () const
894 chunk_list::const_iterator it = _chunks.begin();
895 chunk_list::const_iterator last = _chunks.end();
896 for ( ; it != last; it++) {
897 _value += (*it)->getValue();
903 ////////////////////////////////////////////////////////////////////////
904 // Implementation of FGTextLayer::Chunk.
905 ////////////////////////////////////////////////////////////////////////
907 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
908 : _type(FGTextLayer::TEXT), _fmt(fmt)
915 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
916 const string &fmt, float mult)
917 : _type(type), _fmt(fmt), _mult(mult)
920 if (type == TEXT_VALUE)
929 FGTextLayer::Chunk::getValue () const
934 sprintf(_buf, _fmt.c_str(), _text.c_str());
937 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
940 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
951 ////////////////////////////////////////////////////////////////////////
952 // Implementation of FGSwitchLayer.
953 ////////////////////////////////////////////////////////////////////////
955 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
956 FGInstrumentLayer * layer1,
957 FGInstrumentLayer * layer2)
958 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
962 FGSwitchLayer::~FGSwitchLayer ()
969 FGSwitchLayer::draw ()
973 if (_node->getBoolValue()) {