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())
262 // If the mouse is down, do something
265 if (_mouseDelay < 0) {
266 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
271 // Now, draw the panel
272 float aspect_adjust = get_aspect_adjust(_xsize_node->getIntValue(),
273 _ysize_node->getIntValue());
274 if (aspect_adjust <1.0)
275 update(WIN_X, int(WIN_W * aspect_adjust), WIN_Y, WIN_H);
277 update(WIN_X, WIN_W, WIN_Y, int(WIN_H / aspect_adjust));
282 FGPanel::update (GLfloat winx, GLfloat winw, GLfloat winy, GLfloat winh)
284 // Calculate accelerations
285 // and jiggle the panel accordingly
286 // The factors and bounds are just
287 // initial guesses; using sqrt smooths
289 double x_offset = _x_offset;
290 double y_offset = _y_offset;
292 if (_jitter != 0.0) {
293 double a_x_pilot = current_aircraft.fdm_state->get_A_X_pilot();
294 double a_y_pilot = current_aircraft.fdm_state->get_A_Y_pilot();
295 double a_z_pilot = current_aircraft.fdm_state->get_A_Z_pilot();
297 double a_zx_pilot = a_z_pilot - a_x_pilot;
299 int x_adjust = int(sqrt(fabs(a_y_pilot) * _jitter)) *
300 (a_y_pilot < 0 ? -1 : 1);
301 int y_adjust = int(sqrt(fabs(a_zx_pilot) * _jitter)) *
302 (a_zx_pilot < 0 ? -1 : 1);
304 // adjustments in screen coordinates
305 x_offset += x_adjust;
306 y_offset += y_adjust;
309 glMatrixMode(GL_PROJECTION);
312 gluOrtho2D(winx, winx + winw, winy, winy + winh); /* right side up */
313 // gluOrtho2D(winx + winw, winx, winy + winh, winy); /* up side down */
315 glMatrixMode(GL_MODELVIEW);
319 glTranslated(x_offset, y_offset, 0);
321 // Draw the background
322 glEnable(GL_TEXTURE_2D);
323 glDisable(GL_LIGHTING);
325 glEnable(GL_ALPHA_TEST);
326 glEnable(GL_COLOR_MATERIAL);
327 // glColor4f(1.0, 1.0, 1.0, 1.0);
328 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
329 glColor4fv( cur_light_params.scene_diffuse );
331 glColor4f(0.7, 0.2, 0.2, 1.0);
333 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
334 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
335 // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
336 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
338 glTexCoord2f(0.0, 0.0); glVertex3f(WIN_X, WIN_Y, 0);
339 glTexCoord2f(1.0, 0.0); glVertex3f(WIN_X + _width, WIN_Y, 0);
340 glTexCoord2f(1.0, 1.0); glVertex3f(WIN_X + _width, WIN_Y + _height, 0);
341 glTexCoord2f(0.0, 1.0); glVertex3f(WIN_X, WIN_Y + _height, 0);
344 // Draw the instruments.
345 instrument_list_type::const_iterator current = _instruments.begin();
346 instrument_list_type::const_iterator end = _instruments.end();
348 for ( ; current != end; current++) {
349 FGPanelInstrument * instr = *current;
351 glTranslated(x_offset, y_offset, 0);
352 glTranslated(instr->getXPos(), instr->getYPos(), 0);
356 glMatrixMode(GL_PROJECTION);
358 glMatrixMode(GL_MODELVIEW);
360 ssgForceBasicState();
361 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
366 * Set the panel's visibility.
369 FGPanel::setVisibility (bool visibility)
371 _visibility = visibility;
376 * Return true if the panel is visible.
379 FGPanel::getVisibility () const
386 * Set the panel's background texture.
389 FGPanel::setBackground (ssgTexture * texture)
396 * Set the panel's x-offset.
399 FGPanel::setXOffset (int offset)
401 if (offset <= 0 && offset >= -_width + WIN_W)
407 * Set the panel's y-offset.
410 FGPanel::setYOffset (int offset)
412 if (offset <= 0 && offset >= -_height)
418 * Perform a mouse action.
421 FGPanel::doMouseAction (int button, int updown, int x, int y)
423 // FIXME: this same code appears in update()
424 int xsize = _xsize_node->getIntValue();
425 int ysize = _ysize_node->getIntValue();
426 float aspect_adjust = get_aspect_adjust(xsize, ysize);
428 // Note a released button and return
429 // cerr << "Doing mouse action\n";
432 _mouseInstrument = 0;
436 // Scale for the real window size.
437 if (aspect_adjust < 1.0) {
438 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
439 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
441 x = int(((float)x / xsize) * WIN_W);
442 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
445 // Adjust for offsets.
449 // Search for a matching instrument.
450 for (int i = 0; i < (int)_instruments.size(); i++) {
451 FGPanelInstrument *inst = _instruments[i];
452 int ix = inst->getXPos();
453 int iy = inst->getYPos();
454 int iw = inst->getWidth() / 2;
455 int ih = inst->getHeight() / 2;
456 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
459 _mouseInstrument = inst;
460 _mouseButton = button;
463 // Always do the action once.
464 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
473 ////////////////////////////////////////////////////////////////////////.
474 // Implementation of FGPanelAction.
475 ////////////////////////////////////////////////////////////////////////
477 FGPanelAction::FGPanelAction ()
481 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
482 : _button(button), _x(x), _y(y), _w(w), _h(h)
484 for (unsigned int i = 0; i < _bindings.size(); i++)
488 FGPanelAction::~FGPanelAction ()
493 FGPanelAction::addBinding (FGBinding * binding)
495 _bindings.push_back(binding);
499 FGPanelAction::doAction ()
502 int nBindings = _bindings.size();
503 for (int i = 0; i < nBindings; i++) {
504 _bindings[i]->fire();
511 ////////////////////////////////////////////////////////////////////////
512 // Implementation of FGPanelTransformation.
513 ////////////////////////////////////////////////////////////////////////
515 FGPanelTransformation::FGPanelTransformation ()
520 FGPanelTransformation::~FGPanelTransformation ()
526 ////////////////////////////////////////////////////////////////////////
527 // Implementation of FGPanelInstrument.
528 ////////////////////////////////////////////////////////////////////////
531 FGPanelInstrument::FGPanelInstrument ()
537 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
543 FGPanelInstrument::~FGPanelInstrument ()
545 for (action_list_type::iterator it = _actions.begin();
546 it != _actions.end();
554 FGPanelInstrument::setPosition (int x, int y)
561 FGPanelInstrument::setSize (int w, int h)
568 FGPanelInstrument::getXPos () const
574 FGPanelInstrument::getYPos () const
580 FGPanelInstrument::getWidth () const
586 FGPanelInstrument::getHeight () const
592 FGPanelInstrument::addAction (FGPanelAction * action)
594 _actions.push_back(action);
597 // Coordinates relative to centre.
599 FGPanelInstrument::doMouseAction (int button, int x, int y)
602 action_list_type::iterator it = _actions.begin();
603 action_list_type::iterator last = _actions.end();
604 for ( ; it != last; it++) {
605 if ((*it)->inArea(button, x, y)) {
616 ////////////////////////////////////////////////////////////////////////
617 // Implementation of FGLayeredInstrument.
618 ////////////////////////////////////////////////////////////////////////
620 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
621 : FGPanelInstrument(x, y, w, h)
625 FGLayeredInstrument::~FGLayeredInstrument ()
627 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
634 FGLayeredInstrument::draw ()
637 for (int i = 0; i < (int)_layers.size(); i++) {
639 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
647 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
649 int n = _layers.size();
650 if (layer->getWidth() == -1) {
651 layer->setWidth(getWidth());
653 if (layer->getHeight() == -1) {
654 layer->setHeight(getHeight());
656 _layers.push_back(layer);
661 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
664 return addLayer(new FGTexturedLayer(texture, w, h));
668 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
670 int layer = _layers.size() - 1;
671 _layers[layer]->addTransformation(transformation);
676 ////////////////////////////////////////////////////////////////////////
677 // Implementation of FGInstrumentLayer.
678 ////////////////////////////////////////////////////////////////////////
680 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
686 FGInstrumentLayer::~FGInstrumentLayer ()
688 for (transformation_list::iterator it = _transformations.begin();
689 it != _transformations.end();
697 FGInstrumentLayer::transform () const
699 transformation_list::const_iterator it = _transformations.begin();
700 transformation_list::const_iterator last = _transformations.end();
702 FGPanelTransformation *t = *it;
704 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
707 } else if (val > t->max) {
711 val = val * t->factor + t->offset;
713 val = t->table->interpolate(val) * t->factor + t->offset;
717 case FGPanelTransformation::XSHIFT:
718 glTranslatef(val, 0.0, 0.0);
720 case FGPanelTransformation::YSHIFT:
721 glTranslatef(0.0, val, 0.0);
723 case FGPanelTransformation::ROTATION:
724 glRotatef(-val, 0.0, 0.0, 1.0);
733 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
735 _transformations.push_back(transformation);
740 ////////////////////////////////////////////////////////////////////////
741 // Implementation of FGGroupLayer.
742 ////////////////////////////////////////////////////////////////////////
744 FGGroupLayer::FGGroupLayer ()
748 FGGroupLayer::~FGGroupLayer ()
750 for (unsigned int i = 0; i < _layers.size(); i++)
755 FGGroupLayer::draw ()
758 int nLayers = _layers.size();
759 for (int i = 0; i < nLayers; i++)
765 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
767 _layers.push_back(layer);
772 ////////////////////////////////////////////////////////////////////////
773 // Implementation of FGTexturedLayer.
774 ////////////////////////////////////////////////////////////////////////
777 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
778 : FGInstrumentLayer(w, h)
784 FGTexturedLayer::~FGTexturedLayer ()
790 FGTexturedLayer::draw ()
797 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
800 // From Curt: turn on the panel
801 // lights after sundown.
802 if ( cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES < 95.0 ) {
803 glColor4fv( cur_light_params.scene_diffuse );
805 glColor4f(0.7, 0.2, 0.2, 1.0);
809 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
810 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
811 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
812 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
819 ////////////////////////////////////////////////////////////////////////
820 // Implementation of FGTextLayer.
821 ////////////////////////////////////////////////////////////////////////
823 FGTextLayer::FGTextLayer (int w, int h)
824 : FGInstrumentLayer(w, h), _pointSize(14.0)
827 _color[0] = _color[1] = _color[2] = 0.0;
831 FGTextLayer::~FGTextLayer ()
833 chunk_list::iterator it = _chunks.begin();
834 chunk_list::iterator last = _chunks.end();
835 for ( ; it != last; it++) {
847 text_renderer.setFont(guiFntHandle);
848 text_renderer.setPointSize(_pointSize);
849 text_renderer.begin();
850 text_renderer.start3f(0, 0, 0);
853 if (_now - _then > 100000) {
857 text_renderer.puts((char *)(_value.c_str()));
860 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
866 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
868 _chunks.push_back(chunk);
872 FGTextLayer::setColor (float r, float g, float b)
881 FGTextLayer::setPointSize (float size)
887 FGTextLayer::setFont(fntFont * font)
889 text_renderer.setFont(font);
894 FGTextLayer::recalc_value () const
897 chunk_list::const_iterator it = _chunks.begin();
898 chunk_list::const_iterator last = _chunks.end();
899 for ( ; it != last; it++) {
900 _value += (*it)->getValue();
906 ////////////////////////////////////////////////////////////////////////
907 // Implementation of FGTextLayer::Chunk.
908 ////////////////////////////////////////////////////////////////////////
910 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
911 : _type(FGTextLayer::TEXT), _fmt(fmt)
918 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
919 const string &fmt, float mult)
920 : _type(type), _fmt(fmt), _mult(mult)
923 if (type == TEXT_VALUE)
932 FGTextLayer::Chunk::getValue () const
937 sprintf(_buf, _fmt.c_str(), _text.c_str());
940 sprintf(_buf, _fmt.c_str(), _node->getStringValue().c_str());
943 sprintf(_buf, _fmt.c_str(), _node->getFloatValue() * _mult);
954 ////////////////////////////////////////////////////////////////////////
955 // Implementation of FGSwitchLayer.
956 ////////////////////////////////////////////////////////////////////////
958 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGPropertyNode * node,
959 FGInstrumentLayer * layer1,
960 FGInstrumentLayer * layer2)
961 : FGInstrumentLayer(w, h), _node(node), _layer1(layer1), _layer2(layer2)
965 FGSwitchLayer::~FGSwitchLayer ()
972 FGSwitchLayer::draw ()
976 if (_node->getBoolValue()) {