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>
51 #if defined( NONE ) && defined( _MSC_VER )
52 # pragma message( "A sloppy coder has defined NONE as a macro!!!" )
55 # pragma warn A sloppy coder has defined NONE as a macro!!!
61 ////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////
67 * Calculate the aspect adjustment for the panel.
70 get_aspect_adjust (int xsize, int ysize)
72 float ideal_aspect = float(WIN_W) / float(WIN_H);
73 float real_aspect = float(xsize) / float(ysize);
74 return (real_aspect / ideal_aspect);
79 ////////////////////////////////////////////////////////////////////////
81 ////////////////////////////////////////////////////////////////////////
86 return ((current_panel != 0) &&
87 (current_panel->getVisibility()) &&
88 (globals->get_viewmgr()->get_current() == 0) &&
89 (globals->get_current_view()->get_view_offset() == 0.0));
94 ////////////////////////////////////////////////////////////////////////
95 // Implementation of FGTextureManager.
96 ////////////////////////////////////////////////////////////////////////
98 map<string,ssgTexture *> FGTextureManager::_textureMap;
101 FGTextureManager::createTexture (const string &relativePath)
103 ssgTexture * texture = _textureMap[relativePath];
105 SG_LOG( SG_COCKPIT, SG_DEBUG,
106 "Texture " << relativePath << " does not yet exist" );
107 SGPath tpath(globals->get_fg_root());
108 tpath.append(relativePath);
109 texture = new ssgTexture((char *)tpath.c_str(), false, false);
110 _textureMap[relativePath] = texture;
111 if (_textureMap[relativePath] == 0)
112 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
113 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath
114 << " handle=" << texture->getHandle() );
123 ////////////////////////////////////////////////////////////////////////
124 // Implementation of FGCropped Texture.
125 ////////////////////////////////////////////////////////////////////////
128 FGCroppedTexture::FGCroppedTexture ()
129 : _path(""), _texture(0),
130 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
135 FGCroppedTexture::FGCroppedTexture (const string &path,
136 float minX, float minY,
137 float maxX, float maxY)
138 : _path(path), _texture(0),
139 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
144 FGCroppedTexture::~FGCroppedTexture ()
150 FGCroppedTexture::getTexture ()
153 _texture = FGTextureManager::createTexture(_path);
160 ////////////////////////////////////////////////////////////////////////
161 // Implementation of FGPanel.
162 ////////////////////////////////////////////////////////////////////////
164 FGPanel * current_panel = NULL;
165 static fntRenderer text_renderer;
174 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
175 _x_offset(0), _y_offset(0), _view_height(int(WIN_H * 0.4232)),
178 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
179 _ysize_node(fgGetNode("/sim/startup/ysize", true))
181 setVisibility(fgPanelVisible());
192 for (instrument_list_type::iterator it = _instruments.begin();
193 it != _instruments.end();
202 * Add an instrument to the panel.
205 FGPanel::addInstrument (FGPanelInstrument * instrument)
207 _instruments.push_back(instrument);
212 * Initialize the panel.
222 * Bind panel properties.
227 fgTie("/sim/panel/visibility", &_visibility);
228 fgSetArchivable("/sim/panel/visibility");
229 fgTie("/sim/panel/x-offset", &_x_offset);
230 fgSetArchivable("/sim/panel/x-offset");
231 fgTie("/sim/panel/y-offset", &_y_offset);
232 fgSetArchivable("/sim/panel/y-offset");
233 fgTie("/sim/panel/jitter", &_jitter);
234 fgSetArchivable("/sim/panel/jitter");
240 * Unbind panel properties.
245 fgUntie("/sim/panel/visibility");
246 fgUntie("/sim/panel/x-offset");
247 fgUntie("/sim/panel/y-offset");
258 // Do nothing if the panel isn't visible.
259 if ( !fgPanelVisible() ) {
263 // If the mouse is down, do something
266 if (_mouseDelay < 0) {
267 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
272 // Now, draw the panel
273 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
274 _ysize_node->getIntValue());
275 if (aspect_adjust <1.0)
276 update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
278 update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
283 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
285 // Calculate accelerations
286 // and jiggle the panel accordingly
287 // The factors and bounds are just
288 // initial guesses; using sqrt smooths
290 double x_offset = _x_offset;
291 double y_offset = _y_offset;
293 if (_jitter != 0.0) {
294 double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
295 double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
296 double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
298 double a_zx_pilot = a_z_pilot - a_x_pilot;
300 int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
301 (a_y_pilot < 0 ? -1 : 1);
302 int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
303 (a_zx_pilot < 0 ? -1 : 1);
305 // adjustments in screen coordinates
306 x_offset += x_adjust;
307 y_offset += y_adjust;
310 glMatrixMode(GL_PROJECTION);
313 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
314 // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
316 glMatrixMode(GL_MODELVIEW);
320 glTranslated(x_offset, y_offset, 0);
322 // Draw the background
323 glEnable(GL_TEXTURE_2D);
324 glDisable(GL_LIGHTING);
326 glEnable(GL_ALPHA_TEST);
327 glEnable(GL_COLOR_MATERIAL);
328 // glColor4f(1.0, 1.0, 1.0, 1.0);
329 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
330 glColor4fv( cur_light_params.scene_diffuse );
332 glColor4f(0.7, 0.2, 0.2, 1.0);
334 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
335 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
336 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
337 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
339 glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
340 glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
341 glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
342 glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
345 // Draw the instruments.
346 instrument_list_type::const_iterator current = _instruments.begin();
347 instrument_list_type::const_iterator end = _instruments.end();
349 for ( ; current != end; current++) {
350 FGPanelInstrument * instr = *current;
352 glTranslated(x_offset, y_offset, 0);
353 glTranslated(instr->getXPos(), instr->getYPos(), 0);
357 glMatrixMode(GL_PROJECTION);
359 glMatrixMode(GL_MODELVIEW);
361 ssgForceBasicState();
362 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
367 * Set the panel's visibility.
370 FGPanel::setVisibility (bool visibility)
372 _visibility = visibility;
377 * Return true if the panel is visible.
380 FGPanel::getVisibility () const
387 * Set the panel's background texture.
390 FGPanel::setBackground (ssgTexture * texture)
397 * Set the panel's x-offset.
400 FGPanel::setXOffset (int offset)
402 if (offset <= 0 && offset >= -_width + WIN_W)
408 * Set the panel's y-offset.
411 FGPanel::setYOffset (int offset)
413 if (offset <= 0 && offset >= -_height)
419 * Perform a mouse action.
422 FGPanel::doMouseAction (int button, int updown, int x, int y)
424 // FIXME: this same code appears in update()
425 int xsize = _xsize_node->getIntValue();
426 int ysize = _ysize_node->getIntValue();
427 float aspect_adjust = get_aspect_adjust(xsize, ysize);
429 // Note a released button and return
430 // cerr << "Doing mouse action\n";
433 _mouseInstrument = 0;
437 // Scale for the real window size.
438 if (aspect_adjust < 1.0) {
439 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
440 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
442 x = int(((float)x / xsize) * WIN_W);
443 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
446 // Adjust for offsets.
450 // Search for a matching instrument.
451 for (int i = 0; i < (int)_instruments.size(); i++) {
452 FGPanelInstrument *inst = _instruments[i];
453 int ix = inst->getXPos();
454 int iy = inst->getYPos();
455 int iw = inst->getWidth() / 2;
456 int ih = inst->getHeight() / 2;
457 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
460 _mouseInstrument = inst;
461 _mouseButton = button;
464 // Always do the action once.
465 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
474 ////////////////////////////////////////////////////////////////////////.
475 // Implementation of FGPanelAction.
476 ////////////////////////////////////////////////////////////////////////
478 FGPanelAction::FGPanelAction ()
482 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
483 : _button(button), _x(x), _y(y), _w(w), _h(h)
485 for (unsigned int i = 0; i < _bindings.size(); i++)
489 FGPanelAction::~FGPanelAction ()
494 FGPanelAction::addBinding (FGBinding * binding)
496 _bindings.push_back(binding);
500 FGPanelAction::doAction ()
503 int nBindings = _bindings.size();
504 for (int i = 0; i < nBindings; i++) {
505 _bindings[i]->fire();
512 ////////////////////////////////////////////////////////////////////////
513 // Implementation of FGPanelTransformation.
514 ////////////////////////////////////////////////////////////////////////
516 FGPanelTransformation::FGPanelTransformation ()
521 FGPanelTransformation::~FGPanelTransformation ()
528 ////////////////////////////////////////////////////////////////////////
529 // Implementation of FGPanelInstrument.
530 ////////////////////////////////////////////////////////////////////////
533 FGPanelInstrument::FGPanelInstrument ()
539 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
545 FGPanelInstrument::~FGPanelInstrument ()
547 for (action_list_type::iterator it = _actions.begin();
548 it != _actions.end();
556 FGPanelInstrument::setPosition (int x, int y)
563 FGPanelInstrument::setSize (int w, int h)
570 FGPanelInstrument::getXPos () const
576 FGPanelInstrument::getYPos () const
582 FGPanelInstrument::getWidth () const
588 FGPanelInstrument::getHeight () const
594 FGPanelInstrument::addAction (FGPanelAction * action)
596 _actions.push_back(action);
599 // Coordinates relative to centre.
601 FGPanelInstrument::doMouseAction (int button, int x, int y)
604 action_list_type::iterator it = _actions.begin();
605 action_list_type::iterator last = _actions.end();
606 for ( ; it != last; it++) {
607 if ((*it)->inArea(button, x, y)) {
618 ////////////////////////////////////////////////////////////////////////
619 // Implementation of FGLayeredInstrument.
620 ////////////////////////////////////////////////////////////////////////
622 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
623 : FGPanelInstrument(x, y, w, h)
627 FGLayeredInstrument::~FGLayeredInstrument ()
629 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
636 FGLayeredInstrument::draw ()
639 for (int i = 0; i < (int)_layers.size(); i++) {
641 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
649 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
651 int n = _layers.size();
652 if (layer->getWidth() == -1) {
653 layer->setWidth(getWidth());
655 if (layer->getHeight() == -1) {
656 layer->setHeight(getHeight());
658 _layers.push_back(layer);
663 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
666 return addLayer(new FGTexturedLayer(texture, w, h));
670 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
672 int layer = _layers.size() - 1;
673 _layers[layer]->addTransformation(transformation);
678 ////////////////////////////////////////////////////////////////////////
679 // Implementation of FGInstrumentLayer.
680 ////////////////////////////////////////////////////////////////////////
682 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
688 FGInstrumentLayer::~FGInstrumentLayer ()
690 for (transformation_list::iterator it = _transformations.begin();
691 it != _transformations.end();
699 FGInstrumentLayer::transform () const
701 transformation_list::const_iterator it = _transformations.begin();
702 transformation_list::const_iterator last = _transformations.end();
704 FGPanelTransformation *t = *it;
706 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
709 } else if (val > t->max) {
713 val = val * t->factor + t->offset;
715 val = t->table->interpolate(val) * t->factor + t->offset;
719 case FGPanelTransformation::XSHIFT:
720 glTranslatef(val, 0.0, 0.0);
722 case FGPanelTransformation::YSHIFT:
723 glTranslatef(0.0, val, 0.0);
725 case FGPanelTransformation::ROTATION:
726 glRotatef(-val, 0.0, 0.0, 1.0);
735 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
737 _transformations.push_back(transformation);
742 ////////////////////////////////////////////////////////////////////////
743 // Implementation of FGGroupLayer.
744 ////////////////////////////////////////////////////////////////////////
746 FGGroupLayer::FGGroupLayer ()
750 FGGroupLayer::~FGGroupLayer ()
752 for (unsigned int i = 0; i < _layers.size(); i++)
757 FGGroupLayer::draw ()
760 int nLayers = _layers.size();
761 for (int i = 0; i < nLayers; i++)
767 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
769 _layers.push_back(layer);
774 ////////////////////////////////////////////////////////////////////////
775 // Implementation of FGTexturedLayer.
776 ////////////////////////////////////////////////////////////////////////
779 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
780 : FGInstrumentLayer(w, h)
786 FGTexturedLayer::~FGTexturedLayer ()
792 FGTexturedLayer::draw ()
799 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
802 // From Curt: turn on the panel
803 // lights after sundown.
804 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
805 glColor4fv( cur_light_params.scene_diffuse );
807 glColor4f(0.7, 0.2, 0.2, 1.0);
811 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
812 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
813 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
814 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
821 ////////////////////////////////////////////////////////////////////////
822 // Implementation of FGTextLayer.
823 ////////////////////////////////////////////////////////////////////////
825 FGTextLayer::FGTextLayer (int w, int h)
826 : FGInstrumentLayer(w, h), _pointSize(14.0)
829 _color[0] = _color[1] = _color[2] = 0.0;
833 FGTextLayer::~FGTextLayer ()
835 chunk_list::iterator it = _chunks.begin();
836 chunk_list::iterator last = _chunks.end();
837 for ( ; it != last; it++) {
849 text_renderer.setFont(guiFntHandle);
850 text_renderer.setPointSize(_pointSize);
851 text_renderer.begin();
852 text_renderer.start3f(0, 0, 0);
855 if (_now - _then > 100000) {
859 text_renderer.puts((char *)(_value.c_str()));
862 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
868 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
870 _chunks.push_back(chunk);
874 FGTextLayer::setColor (float r, float g, float b)
883 FGTextLayer::setPointSize (float size)
889 FGTextLayer::setFont(fntFont * font)
891 text_renderer.setFont(font);
896 FGTextLayer::recalc_value () const
899 chunk_list::const_iterator it = _chunks.begin();
900 chunk_list::const_iterator last = _chunks.end();
901 for ( ; it != last; it++) {
902 _value += (*it)->getValue();
908 ////////////////////////////////////////////////////////////////////////
909 // Implementation of FGTextLayer::Chunk.
910 ////////////////////////////////////////////////////////////////////////
912 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
913 : _type(FGTextLayer::TEXT), _fmt(fmt)
920 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
921 const string &fmt, float mult)
922 : _type(type), _fmt(fmt), _mult(mult)
925 if (type == TEXT_VALUE)
934 FGTextLayer::Chunk::getValue () const
939 sprintf(_buf, _fmt.c_str(), _text.c_str());
942 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
945 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
956 ////////////////////////////////////////////////////////////////////////
957 // Implementation of FGSwitchLayer.
958 ////////////////////////////////////////////////////////////////////////
960 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
961 FGInstrumentLayer * layer1,
962 FGInstrumentLayer * layer2)
963 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
967 FGSwitchLayer::~FGSwitchLayer ()
974 FGSwitchLayer::draw ()
978 if (_node->getBoolValue()) {