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/fgpath.hxx>
37 #include <Main/globals.hxx>
38 #include <Main/fg_props.hxx>
39 #include <Objects/texload.h>
40 #include <Time/light.hxx>
49 return ((globals->get_options()->get_panel_status()) &&
50 (globals->get_viewmgr()->get_current() == 0) &&
51 (globals->get_current_view()->get_view_offset() == 0.0));
56 ////////////////////////////////////////////////////////////////////////
57 // Implementation of FGTextureManager.
58 ////////////////////////////////////////////////////////////////////////
60 map<string,ssgTexture *> FGTextureManager::_textureMap;
63 FGTextureManager::createTexture (const string &relativePath)
65 ssgTexture * texture = _textureMap[relativePath];
67 cerr << "Texture " << relativePath << " does not yet exist" << endl;
68 FGPath tpath(globals->get_options()->get_fg_root());
69 tpath.append(relativePath);
70 texture = new ssgTexture((char *)tpath.c_str(), false, false);
71 _textureMap[relativePath] = texture;
72 if (_textureMap[relativePath] == 0)
73 cerr << "Texture *still* doesn't exist" << endl;
74 cerr << "Created texture " << relativePath
75 << " handle=" << texture->getHandle() << endl;
84 ////////////////////////////////////////////////////////////////////////
85 // Implementation of FGCropped Texture.
86 ////////////////////////////////////////////////////////////////////////
89 FGCroppedTexture::FGCroppedTexture ()
90 : _path(""), _texture(0),
91 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
96 FGCroppedTexture::FGCroppedTexture (const string &path,
97 float minX, float minY,
98 float maxX, float maxY)
99 : _path(path), _texture(0),
100 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
105 FGCroppedTexture::~FGCroppedTexture ()
111 FGCroppedTexture::getTexture ()
114 _texture = FGTextureManager::createTexture(_path);
121 ////////////////////////////////////////////////////////////////////////
122 // Implementation of FGPanel.
123 ////////////////////////////////////////////////////////////////////////
125 FGPanel * current_panel = NULL;
126 static fntRenderer text_renderer;
132 FGPanel::FGPanel (int window_x, int window_y, int window_w, int window_h)
135 _winx(window_x), _winy(window_y), _winw(window_w), _winh(window_h),
136 _width(_winw), _height(int(_winh * 0.5768 + 1)),
137 _x_offset(0), _y_offset(0), _view_height(int(_winh * 0.4232))
139 setVisibility(fgPanelVisible());
149 for (instrument_list_type::iterator it = _instruments.begin();
150 it != _instruments.end();
159 * Add an instrument to the panel.
162 FGPanel::addInstrument (FGPanelInstrument * instrument)
164 _instruments.push_back(instrument);
169 * Initialize the panel.
179 * Bind panel properties.
184 fgTie("/sim/panel/visibility", &_visibility);
185 fgTie("/sim/panel/x-offset", &_x_offset);
186 fgTie("/sim/panel/y-offset", &_y_offset);
191 * Unbind panel properties.
196 fgUntie("/sim/panel/visibility");
197 fgUntie("/sim/panel/x-offset");
198 fgUntie("/sim/panel/y-offset");
208 // Do nothing if the panel isn't visible.
209 if (!fgPanelVisible())
212 // If the mouse is down, do something
215 if (_mouseDelay < 0) {
216 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
221 // Now, draw the panel
222 glMatrixMode(GL_PROJECTION);
225 gluOrtho2D(_winx, _winx + _winw, _winy, _winy + _winh);
227 glMatrixMode(GL_MODELVIEW);
231 glTranslated(_x_offset, _y_offset, 0);
233 // Draw the background
234 glEnable(GL_TEXTURE_2D);
235 glDisable(GL_LIGHTING);
237 glEnable(GL_ALPHA_TEST);
238 glEnable(GL_COLOR_MATERIAL);
239 // glColor4f(1.0, 1.0, 1.0, 1.0);
240 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
241 glColor4fv( cur_light_params.scene_diffuse );
243 glColor4f(0.7, 0.2, 0.2, 1.0);
245 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
246 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
248 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
250 glTexCoord2f(0.0, 0.0); glVertex3f(_winx, _winy, 0);
251 glTexCoord2f(1.0, 0.0); glVertex3f(_winx + _width, _winy, 0);
252 glTexCoord2f(1.0, 1.0); glVertex3f(_winx + _width, _winy + _height, 0);
253 glTexCoord2f(0.0, 1.0); glVertex3f(_winx, _winy + _height, 0);
256 // Draw the instruments.
257 instrument_list_type::const_iterator current = _instruments.begin();
258 instrument_list_type::const_iterator end = _instruments.end();
260 for ( ; current != end; current++) {
261 FGPanelInstrument * instr = *current;
263 glTranslated(_x_offset, _y_offset, 0);
264 glTranslated(instr->getXPos(), instr->getYPos(), 0);
268 glMatrixMode(GL_PROJECTION);
270 glMatrixMode(GL_MODELVIEW);
272 ssgForceBasicState();
273 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
278 * Set the panel's visibility.
281 FGPanel::setVisibility (bool visibility)
283 _visibility = visibility;
288 * Return true if the panel is visible.
291 FGPanel::getVisibility () const
298 * Set the panel's background texture.
301 FGPanel::setBackground (ssgTexture * texture)
308 * Set the panel's x-offset.
311 FGPanel::setXOffset (int offset)
313 if (offset <= 0 && offset >= -_width + _winw)
319 * Set the panel's y-offset.
322 FGPanel::setYOffset (int offset)
324 if (offset <= 0 && offset >= -_height)
330 * Perform a mouse action.
333 FGPanel::doMouseAction (int button, int updown, int x, int y)
336 // Note a released button and return
337 // cerr << "Doing mouse action\n";
340 _mouseInstrument = 0;
344 // Scale for the real window size.
345 x = int(((float)x / globals->get_options()->get_xsize()) * _winw);
346 y = int(_winh - (((float)y / globals->get_options()->get_ysize())
349 // Adjust for offsets.
353 // Search for a matching instrument.
354 for (int i = 0; i < (int)_instruments.size(); i++) {
355 FGPanelInstrument *inst = _instruments[i];
356 int ix = inst->getXPos();
357 int iy = inst->getYPos();
358 int iw = inst->getWidth() / 2;
359 int ih = inst->getHeight() / 2;
360 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
363 _mouseInstrument = inst;
364 _mouseButton = button;
367 // Always do the action once.
368 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
377 ////////////////////////////////////////////////////////////////////////.
378 // Implementation of FGPanelAction.
379 ////////////////////////////////////////////////////////////////////////
381 FGPanelAction::FGPanelAction ()
385 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
386 : _button(button), _x(x), _y(y), _w(w), _h(h)
390 FGPanelAction::~FGPanelAction ()
396 ////////////////////////////////////////////////////////////////////////
397 // Implementation of FGAdjustAction.
398 ////////////////////////////////////////////////////////////////////////
400 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
401 SGValue * value, float increment,
402 float min, float max, bool wrap)
403 : FGPanelAction(button, x, y, w, h),
404 _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
408 FGAdjustAction::~FGAdjustAction ()
413 FGAdjustAction::doAction ()
415 float val = _value->getFloatValue();
418 val = (_wrap ? _max : _min);
419 } else if (val > _max) {
420 val = (_wrap ? _min : _max);
422 _value->setDoubleValue(val);
427 ////////////////////////////////////////////////////////////////////////
428 // Implementation of FGSwapAction.
429 ////////////////////////////////////////////////////////////////////////
431 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
432 SGValue * value1, SGValue * value2)
433 : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
437 FGSwapAction::~FGSwapAction ()
442 FGSwapAction::doAction ()
444 float val = _value1->getFloatValue();
445 _value1->setDoubleValue(_value2->getFloatValue());
446 _value2->setDoubleValue(val);
451 ////////////////////////////////////////////////////////////////////////
452 // Implementation of FGToggleAction.
453 ////////////////////////////////////////////////////////////////////////
455 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
457 : FGPanelAction(button, x, y, w, h), _value(value)
461 FGToggleAction::~FGToggleAction ()
466 FGToggleAction::doAction ()
468 _value->setBoolValue(!(_value->getBoolValue()));
473 ////////////////////////////////////////////////////////////////////////
474 // Implementation of FGPanelTransformation.
475 ////////////////////////////////////////////////////////////////////////
477 FGPanelTransformation::FGPanelTransformation ()
481 FGPanelTransformation::~FGPanelTransformation ()
487 ////////////////////////////////////////////////////////////////////////
488 // Implementation of FGPanelInstrument.
489 ////////////////////////////////////////////////////////////////////////
492 FGPanelInstrument::FGPanelInstrument ()
498 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
504 FGPanelInstrument::~FGPanelInstrument ()
506 for (action_list_type::iterator it = _actions.begin();
507 it != _actions.end();
515 FGPanelInstrument::setPosition (int x, int y)
522 FGPanelInstrument::setSize (int w, int h)
529 FGPanelInstrument::getXPos () const
535 FGPanelInstrument::getYPos () const
541 FGPanelInstrument::getWidth () const
547 FGPanelInstrument::getHeight () const
553 FGPanelInstrument::addAction (FGPanelAction * action)
555 _actions.push_back(action);
558 // Coordinates relative to centre.
560 FGPanelInstrument::doMouseAction (int button, int x, int y)
562 action_list_type::iterator it = _actions.begin();
563 action_list_type::iterator last = _actions.end();
564 for ( ; it != last; it++) {
565 if ((*it)->inArea(button, x, y)) {
575 ////////////////////////////////////////////////////////////////////////
576 // Implementation of FGLayeredInstrument.
577 ////////////////////////////////////////////////////////////////////////
579 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
580 : FGPanelInstrument(x, y, w, h)
584 FGLayeredInstrument::~FGLayeredInstrument ()
586 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
593 FGLayeredInstrument::draw ()
595 for (int i = 0; i < (int)_layers.size(); i++) {
597 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
604 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
606 int n = _layers.size();
607 if (layer->getWidth() == -1) {
608 layer->setWidth(getWidth());
610 if (layer->getHeight() == -1) {
611 layer->setHeight(getHeight());
613 _layers.push_back(layer);
618 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
621 return addLayer(new FGTexturedLayer(texture, w, h));
625 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
627 int layer = _layers.size() - 1;
628 _layers[layer]->addTransformation(transformation);
633 ////////////////////////////////////////////////////////////////////////
634 // Implementation of FGInstrumentLayer.
635 ////////////////////////////////////////////////////////////////////////
637 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
643 FGInstrumentLayer::~FGInstrumentLayer ()
645 for (transformation_list::iterator it = _transformations.begin();
646 it != _transformations.end();
654 FGInstrumentLayer::transform () const
656 transformation_list::const_iterator it = _transformations.begin();
657 transformation_list::const_iterator last = _transformations.end();
659 FGPanelTransformation *t = *it;
660 float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
663 } else if (val > t->max) {
666 val = val * t->factor + t->offset;
669 case FGPanelTransformation::XSHIFT:
670 glTranslatef(val, 0.0, 0.0);
672 case FGPanelTransformation::YSHIFT:
673 glTranslatef(0.0, val, 0.0);
675 case FGPanelTransformation::ROTATION:
676 glRotatef(-val, 0.0, 0.0, 1.0);
684 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
686 _transformations.push_back(transformation);
691 ////////////////////////////////////////////////////////////////////////
692 // Implementation of FGTexturedLayer.
693 ////////////////////////////////////////////////////////////////////////
696 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
697 : FGInstrumentLayer(w, h)
703 FGTexturedLayer::~FGTexturedLayer ()
709 FGTexturedLayer::draw ()
715 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
718 // From Curt: turn on the panel
719 // lights after sundown.
720 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
721 glColor4fv( cur_light_params.scene_diffuse );
723 glColor4f(0.7, 0.2, 0.2, 1.0);
727 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
728 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
729 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
730 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
736 ////////////////////////////////////////////////////////////////////////
737 // Implementation of FGTextLayer.
738 ////////////////////////////////////////////////////////////////////////
740 FGTextLayer::FGTextLayer (int w, int h)
741 : FGInstrumentLayer(w, h), _pointSize(14.0)
744 _color[0] = _color[1] = _color[2] = 0.0;
748 FGTextLayer::~FGTextLayer ()
750 chunk_list::iterator it = _chunks.begin();
751 chunk_list::iterator last = _chunks.end();
752 for ( ; it != last; it++) {
763 text_renderer.setFont(guiFntHandle);
764 text_renderer.setPointSize(_pointSize);
765 text_renderer.begin();
766 text_renderer.start3f(0, 0, 0);
769 if (_now - _then > 100000) {
773 text_renderer.puts((char *)(_value.c_str()));
776 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
781 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
783 _chunks.push_back(chunk);
787 FGTextLayer::setColor (float r, float g, float b)
796 FGTextLayer::setPointSize (float size)
802 FGTextLayer::setFont(fntFont * font)
804 text_renderer.setFont(font);
809 FGTextLayer::recalc_value () const
812 chunk_list::const_iterator it = _chunks.begin();
813 chunk_list::const_iterator last = _chunks.end();
814 for ( ; it != last; it++) {
815 _value += (*it)->getValue();
821 ////////////////////////////////////////////////////////////////////////
822 // Implementation of FGTextLayer::Chunk.
823 ////////////////////////////////////////////////////////////////////////
825 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
826 : _type(FGTextLayer::TEXT), _fmt(fmt)
833 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
834 const string &fmt, float mult)
835 : _type(type), _fmt(fmt), _mult(mult)
838 if (type == TEXT_VALUE)
847 FGTextLayer::Chunk::getValue () const
851 sprintf(_buf, _fmt.c_str(), _text.c_str());
854 sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
857 sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
865 ////////////////////////////////////////////////////////////////////////
866 // Implementation of FGSwitchLayer.
867 ////////////////////////////////////////////////////////////////////////
869 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
870 FGInstrumentLayer * layer1,
871 FGInstrumentLayer * layer2)
872 : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
876 FGSwitchLayer::~FGSwitchLayer ()
883 FGSwitchLayer::draw ()
886 if (_value->getBoolValue()) {