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>
36 #include <Main/globals.hxx>
37 #include <Objects/texload.h>
38 #include <Time/light.hxx>
47 return ((globals->get_options()->get_panel_status()) &&
48 (globals->get_options()->get_view_mode() == FGOptions::FG_VIEW_PILOT) &&
49 (globals->get_current_view()->get_view_offset() == 0.0));
54 ////////////////////////////////////////////////////////////////////////
55 // Implementation of FGTextureManager.
56 ////////////////////////////////////////////////////////////////////////
58 map<string,ssgTexture *> FGTextureManager::_textureMap;
61 FGTextureManager::createTexture (const string &relativePath)
63 ssgTexture * texture = _textureMap[relativePath];
65 cerr << "Texture " << relativePath << " does not yet exist" << endl;
66 FGPath tpath(globals->get_options()->get_fg_root());
67 tpath.append(relativePath);
68 texture = new ssgTexture((char *)tpath.c_str(), false, false);
69 _textureMap[relativePath] = texture;
70 if (_textureMap[relativePath] == 0)
71 cerr << "Texture *still* doesn't exist" << endl;
72 cerr << "Created texture " << relativePath
73 << " handle=" << texture->getHandle() << endl;
82 ////////////////////////////////////////////////////////////////////////
83 // Implementation of FGCropped Texture.
84 ////////////////////////////////////////////////////////////////////////
87 FGCroppedTexture::FGCroppedTexture ()
88 : _path(""), _texture(0),
89 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
94 FGCroppedTexture::FGCroppedTexture (const string &path,
95 float minX, float minY,
96 float maxX, float maxY)
97 : _path(path), _texture(0),
98 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
103 FGCroppedTexture::~FGCroppedTexture ()
109 FGCroppedTexture::getTexture ()
112 _texture = FGTextureManager::createTexture(_path);
119 ////////////////////////////////////////////////////////////////////////
120 // Implementation of FGPanel.
121 ////////////////////////////////////////////////////////////////////////
123 FGPanel * current_panel = NULL;
124 static fntRenderer text_renderer;
130 FGPanel::FGPanel (int window_x, int window_y, int window_w, int window_h)
133 _winx(window_x), _winy(window_y), _winw(window_w), _winh(window_h),
134 _width(_winw), _height(int(_winh * 0.5768 + 1)),
135 _x_offset(0), _y_offset(0), _view_height(int(_winh * 0.4232))
137 setVisibility(fgPanelVisible());
146 for (instrument_list_type::iterator it = _instruments.begin();
147 it != _instruments.end();
156 * Add an instrument to the panel.
159 FGPanel::addInstrument (FGPanelInstrument * instrument)
161 _instruments.push_back(instrument);
169 FGPanel::update () const
171 // Do nothing if the panel isn't visible.
172 if (!fgPanelVisible())
175 // If the mouse is down, do something
178 if (_mouseDelay < 0) {
179 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
184 // Now, draw the panel
185 glMatrixMode(GL_PROJECTION);
188 gluOrtho2D(_winx, _winx + _winw, _winy, _winy + _winh);
190 glMatrixMode(GL_MODELVIEW);
194 glTranslated(_x_offset, _y_offset, 0);
196 // Draw the background
197 glEnable(GL_TEXTURE_2D);
198 glDisable(GL_LIGHTING);
200 glEnable(GL_ALPHA_TEST);
201 glEnable(GL_COLOR_MATERIAL);
202 // glColor4f(1.0, 1.0, 1.0, 1.0);
203 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
204 glColor4fv( cur_light_params.scene_diffuse );
206 glColor4f(0.7, 0.2, 0.2, 1.0);
208 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
209 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
210 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
211 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
213 glTexCoord2f(0.0, 0.0); glVertex3f(_winx, _winy, 0);
214 glTexCoord2f(1.0, 0.0); glVertex3f(_winx + _width, _winy, 0);
215 glTexCoord2f(1.0, 1.0); glVertex3f(_winx + _width, _winy + _height, 0);
216 glTexCoord2f(0.0, 1.0); glVertex3f(_winx, _winy + _height, 0);
219 // Draw the instruments.
220 instrument_list_type::const_iterator current = _instruments.begin();
221 instrument_list_type::const_iterator end = _instruments.end();
223 for ( ; current != end; current++) {
224 FGPanelInstrument * instr = *current;
226 glTranslated(_x_offset, _y_offset, 0);
227 glTranslated(instr->getXPos(), instr->getYPos(), 0);
231 glMatrixMode(GL_PROJECTION);
233 glMatrixMode(GL_MODELVIEW);
235 ssgForceBasicState();
236 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
241 * Set the panel's visibility.
244 FGPanel::setVisibility (bool visibility)
246 _visibility = visibility;
251 * Return true if the panel is visible.
254 FGPanel::getVisibility () const
261 * Set the panel's background texture.
264 FGPanel::setBackground (ssgTexture * texture)
271 * Set the panel's x-offset.
274 FGPanel::setXOffset (int offset)
276 if (offset <= 0 && offset >= -_width + _winw)
282 * Set the panel's y-offset.
285 FGPanel::setYOffset (int offset)
287 if (offset <= 0 && offset >= -_height)
293 * Perform a mouse action.
296 FGPanel::doMouseAction (int button, int updown, int x, int y)
299 // Note a released button and return
300 // cerr << "Doing mouse action\n";
303 _mouseInstrument = 0;
307 // Scale for the real window size.
308 x = int(((float)x / globals->get_options()->get_xsize()) * _winw);
309 y = int(_winh - (((float)y / globals->get_options()->get_ysize())
312 // Adjust for offsets.
316 // Search for a matching instrument.
317 for (int i = 0; i < (int)_instruments.size(); i++) {
318 FGPanelInstrument *inst = _instruments[i];
319 int ix = inst->getXPos();
320 int iy = inst->getYPos();
321 int iw = inst->getWidth() / 2;
322 int ih = inst->getHeight() / 2;
323 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
326 _mouseInstrument = inst;
327 _mouseButton = button;
330 // Always do the action once.
331 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
340 ////////////////////////////////////////////////////////////////////////.
341 // Implementation of FGPanelAction.
342 ////////////////////////////////////////////////////////////////////////
344 FGPanelAction::FGPanelAction ()
348 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
349 : _button(button), _x(x), _y(y), _w(w), _h(h)
353 FGPanelAction::~FGPanelAction ()
359 ////////////////////////////////////////////////////////////////////////
360 // Implementation of FGAdjustAction.
361 ////////////////////////////////////////////////////////////////////////
363 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
364 SGValue * value, float increment,
365 float min, float max, bool wrap)
366 : FGPanelAction(button, x, y, w, h),
367 _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
371 FGAdjustAction::~FGAdjustAction ()
376 FGAdjustAction::doAction ()
378 float val = _value->getFloatValue();
381 val = (_wrap ? _max : _min);
382 } else if (val > _max) {
383 val = (_wrap ? _min : _max);
385 _value->setDoubleValue(val);
390 ////////////////////////////////////////////////////////////////////////
391 // Implementation of FGSwapAction.
392 ////////////////////////////////////////////////////////////////////////
394 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
395 SGValue * value1, SGValue * value2)
396 : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
400 FGSwapAction::~FGSwapAction ()
405 FGSwapAction::doAction ()
407 float val = _value1->getFloatValue();
408 _value1->setDoubleValue(_value2->getFloatValue());
409 _value2->setDoubleValue(val);
414 ////////////////////////////////////////////////////////////////////////
415 // Implementation of FGToggleAction.
416 ////////////////////////////////////////////////////////////////////////
418 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
420 : FGPanelAction(button, x, y, w, h), _value(value)
424 FGToggleAction::~FGToggleAction ()
429 FGToggleAction::doAction ()
431 _value->setBoolValue(!(_value->getBoolValue()));
436 ////////////////////////////////////////////////////////////////////////
437 // Implementation of FGPanelTransformation.
438 ////////////////////////////////////////////////////////////////////////
440 FGPanelTransformation::FGPanelTransformation ()
444 FGPanelTransformation::~FGPanelTransformation ()
450 ////////////////////////////////////////////////////////////////////////
451 // Implementation of FGPanelInstrument.
452 ////////////////////////////////////////////////////////////////////////
455 FGPanelInstrument::FGPanelInstrument ()
461 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
467 FGPanelInstrument::~FGPanelInstrument ()
469 for (action_list_type::iterator it = _actions.begin();
470 it != _actions.end();
478 FGPanelInstrument::setPosition (int x, int y)
485 FGPanelInstrument::setSize (int w, int h)
492 FGPanelInstrument::getXPos () const
498 FGPanelInstrument::getYPos () const
504 FGPanelInstrument::getWidth () const
510 FGPanelInstrument::getHeight () const
516 FGPanelInstrument::addAction (FGPanelAction * action)
518 _actions.push_back(action);
521 // Coordinates relative to centre.
523 FGPanelInstrument::doMouseAction (int button, int x, int y)
525 action_list_type::iterator it = _actions.begin();
526 action_list_type::iterator last = _actions.end();
527 for ( ; it != last; it++) {
528 if ((*it)->inArea(button, x, y)) {
538 ////////////////////////////////////////////////////////////////////////
539 // Implementation of FGLayeredInstrument.
540 ////////////////////////////////////////////////////////////////////////
542 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
543 : FGPanelInstrument(x, y, w, h)
547 FGLayeredInstrument::~FGLayeredInstrument ()
549 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
556 FGLayeredInstrument::draw ()
558 for (int i = 0; i < (int)_layers.size(); i++) {
560 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
567 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
569 int n = _layers.size();
570 if (layer->getWidth() == -1) {
571 layer->setWidth(getWidth());
573 if (layer->getHeight() == -1) {
574 layer->setHeight(getHeight());
576 _layers.push_back(layer);
581 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
584 return addLayer(new FGTexturedLayer(texture, w, h));
588 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
590 int layer = _layers.size() - 1;
591 _layers[layer]->addTransformation(transformation);
596 ////////////////////////////////////////////////////////////////////////
597 // Implementation of FGInstrumentLayer.
598 ////////////////////////////////////////////////////////////////////////
600 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
606 FGInstrumentLayer::~FGInstrumentLayer ()
608 for (transformation_list::iterator it = _transformations.begin();
609 it != _transformations.end();
617 FGInstrumentLayer::transform () const
619 transformation_list::const_iterator it = _transformations.begin();
620 transformation_list::const_iterator last = _transformations.end();
622 FGPanelTransformation *t = *it;
623 float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
626 } else if (val > t->max) {
629 val = val * t->factor + t->offset;
632 case FGPanelTransformation::XSHIFT:
633 glTranslatef(val, 0.0, 0.0);
635 case FGPanelTransformation::YSHIFT:
636 glTranslatef(0.0, val, 0.0);
638 case FGPanelTransformation::ROTATION:
639 glRotatef(-val, 0.0, 0.0, 1.0);
647 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
649 _transformations.push_back(transformation);
654 ////////////////////////////////////////////////////////////////////////
655 // Implementation of FGTexturedLayer.
656 ////////////////////////////////////////////////////////////////////////
659 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
660 : FGInstrumentLayer(w, h)
666 FGTexturedLayer::~FGTexturedLayer ()
672 FGTexturedLayer::draw ()
678 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
681 // From Curt: turn on the panel
682 // lights after sundown.
683 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
684 glColor4fv( cur_light_params.scene_diffuse );
686 glColor4f(0.7, 0.2, 0.2, 1.0);
690 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
691 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
692 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
693 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
699 ////////////////////////////////////////////////////////////////////////
700 // Implementation of FGTextLayer.
701 ////////////////////////////////////////////////////////////////////////
703 FGTextLayer::FGTextLayer (int w, int h)
704 : FGInstrumentLayer(w, h), _pointSize(14.0)
707 _color[0] = _color[1] = _color[2] = 0.0;
711 FGTextLayer::~FGTextLayer ()
713 chunk_list::iterator it = _chunks.begin();
714 chunk_list::iterator last = _chunks.end();
715 for ( ; it != last; it++) {
726 text_renderer.setFont(guiFntHandle);
727 text_renderer.setPointSize(_pointSize);
728 text_renderer.begin();
729 text_renderer.start3f(0, 0, 0);
732 if (_now - _then > 100000) {
736 text_renderer.puts((char *)(_value.c_str()));
739 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
744 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
746 _chunks.push_back(chunk);
750 FGTextLayer::setColor (float r, float g, float b)
759 FGTextLayer::setPointSize (float size)
765 FGTextLayer::setFont(fntFont * font)
767 text_renderer.setFont(font);
772 FGTextLayer::recalc_value () const
775 chunk_list::const_iterator it = _chunks.begin();
776 chunk_list::const_iterator last = _chunks.end();
777 for ( ; it != last; it++) {
778 _value += (*it)->getValue();
784 ////////////////////////////////////////////////////////////////////////
785 // Implementation of FGTextLayer::Chunk.
786 ////////////////////////////////////////////////////////////////////////
788 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
789 : _type(FGTextLayer::TEXT), _fmt(fmt)
796 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
797 const string &fmt, float mult)
798 : _type(type), _fmt(fmt), _mult(mult)
801 if (type == TEXT_VALUE)
810 FGTextLayer::Chunk::getValue () const
814 sprintf(_buf, _fmt.c_str(), _text.c_str());
817 sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
820 sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
828 ////////////////////////////////////////////////////////////////////////
829 // Implementation of FGSwitchLayer.
830 ////////////////////////////////////////////////////////////////////////
832 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
833 FGInstrumentLayer * layer1,
834 FGInstrumentLayer * layer2)
835 : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
839 FGSwitchLayer::~FGSwitchLayer ()
846 FGSwitchLayer::draw ()
849 if (_value->getBoolValue()) {