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/options.hxx>
37 #include <Main/views.hxx>
38 #include <Objects/texload.h>
45 ////////////////////////////////////////////////////////////////////////
46 // Implementation of FGTextureManager.
47 ////////////////////////////////////////////////////////////////////////
49 map<string,ssgTexture *> FGTextureManager::_textureMap;
52 FGTextureManager::createTexture (const string &relativePath)
54 ssgTexture * texture = _textureMap[relativePath];
56 cerr << "Texture " << relativePath << " does not yet exist" << endl;
57 FGPath tpath(current_options.get_fg_root());
58 tpath.append(relativePath);
59 texture = new ssgTexture((char *)tpath.c_str(), false, false);
60 _textureMap[relativePath] = texture;
61 if (_textureMap[relativePath] == 0)
62 cerr << "Texture *still* doesn't exist" << endl;
63 cerr << "Created texture " << relativePath
64 << " handle=" << texture->getHandle() << endl;
73 ////////////////////////////////////////////////////////////////////////
74 // Implementation of FGCropped Texture.
75 ////////////////////////////////////////////////////////////////////////
78 FGCroppedTexture::FGCroppedTexture ()
79 : _path(""), _texture(0),
80 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
85 FGCroppedTexture::FGCroppedTexture (const string &path,
86 float minX, float minY,
87 float maxX, float maxY)
88 : _path(path), _texture(0),
89 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
94 FGCroppedTexture::~FGCroppedTexture ()
100 FGCroppedTexture::getTexture ()
103 _texture = FGTextureManager::createTexture(_path);
110 ////////////////////////////////////////////////////////////////////////
111 // Implementation of FGPanel.
112 ////////////////////////////////////////////////////////////////////////
114 FGPanel * current_panel = NULL;
115 static fntRenderer text_renderer;
121 FGPanel::FGPanel (int window_x, int window_y, int window_w, int window_h)
124 _winx(window_x), _winy(window_y), _winw(window_w), _winh(window_h),
125 _width(_winw), _height(int(_winh * 0.5768 + 1)),
126 _x_offset(0), _y_offset(0), _view_height(int(_winh * 0.4232))
128 setVisibility(current_options.get_panel_status());
137 for (instrument_list_type::iterator it = _instruments.begin();
138 it != _instruments.end();
147 * Add an instrument to the panel.
150 FGPanel::addInstrument (FGPanelInstrument * instrument)
152 _instruments.push_back(instrument);
160 FGPanel::update () const
162 // Do nothing if the panel isn't visible.
166 // If the mouse is down, do something
169 if (_mouseDelay < 0) {
170 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
175 // Now, draw the panel
176 glMatrixMode(GL_PROJECTION);
179 gluOrtho2D(_winx, _winx + _winw, _winy, _winy + _winh);
181 glMatrixMode(GL_MODELVIEW);
185 glTranslated(_x_offset, _y_offset, 0);
187 // Draw the background
188 glEnable(GL_TEXTURE_2D);
189 glDisable(GL_LIGHTING);
191 glEnable(GL_ALPHA_TEST);
192 glEnable(GL_COLOR_MATERIAL);
193 // glColor4f(1.0, 1.0, 1.0, 1.0);
194 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
195 glColor4fv( cur_light_params.scene_diffuse );
197 glColor4f(0.7, 0.2, 0.2, 1.0);
199 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
202 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
204 glTexCoord2f(0.0, 0.0); glVertex3f(_winx, _winy, 0);
205 glTexCoord2f(10.0, 0.0); glVertex3f(_winx + _width, _winy, 0);
206 glTexCoord2f(10.0, 5.0); glVertex3f(_winx + _width, _winy + _height, 0);
207 glTexCoord2f(0.0, 5.0); glVertex3f(_winx, _winy + _height, 0);
210 // Draw the instruments.
211 instrument_list_type::const_iterator current = _instruments.begin();
212 instrument_list_type::const_iterator end = _instruments.end();
214 for ( ; current != end; current++) {
215 FGPanelInstrument * instr = *current;
217 glTranslated(_x_offset, _y_offset, 0);
218 glTranslated(instr->getXPos(), instr->getYPos(), 0);
222 glMatrixMode(GL_PROJECTION);
224 glMatrixMode(GL_MODELVIEW);
226 ssgForceBasicState();
227 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
232 * Set the panel's visibility.
235 FGPanel::setVisibility (bool visibility)
237 _visibility = visibility;
242 * Return true if the panel is visible.
245 FGPanel::getVisibility () const
252 * Set the panel's background texture.
255 FGPanel::setBackground (ssgTexture * texture)
262 * Set the panel's x-offset.
265 FGPanel::setXOffset (int offset)
267 if (offset <= 0 && offset >= -_width + _winw)
273 * Set the panel's y-offset.
276 FGPanel::setYOffset (int offset)
278 if (offset <= 0 && offset >= -_height)
284 * Perform a mouse action.
287 FGPanel::doMouseAction (int button, int updown, int x, int y)
290 // Note a released button and return
291 // cerr << "Doing mouse action\n";
294 _mouseInstrument = 0;
298 // Scale for the real window size.
299 x = int(((float)x / current_view.get_winWidth()) * _winw);
300 y = int(_winh - (((float)y / current_view.get_winHeight()) * _winh));
302 // Adjust for offsets.
306 // Search for a matching instrument.
307 for (int i = 0; i < (int)_instruments.size(); i++) {
308 FGPanelInstrument *inst = _instruments[i];
309 int ix = inst->getXPos();
310 int iy = inst->getYPos();
311 int iw = inst->getWidth() / 2;
312 int ih = inst->getHeight() / 2;
313 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
316 _mouseInstrument = inst;
317 _mouseButton = button;
320 // Always do the action once.
321 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
330 ////////////////////////////////////////////////////////////////////////.
331 // Implementation of FGPanelAction.
332 ////////////////////////////////////////////////////////////////////////
334 FGPanelAction::FGPanelAction ()
338 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
339 : _button(button), _x(x), _y(y), _w(w), _h(h)
343 FGPanelAction::~FGPanelAction ()
349 ////////////////////////////////////////////////////////////////////////
350 // Implementation of FGAdjustAction.
351 ////////////////////////////////////////////////////////////////////////
353 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
354 SGValue * value, float increment,
355 float min, float max, bool wrap)
356 : FGPanelAction(button, x, y, w, h),
357 _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
361 FGAdjustAction::~FGAdjustAction ()
366 FGAdjustAction::doAction ()
368 float val = _value->getFloatValue();
371 val = (_wrap ? _max : _min);
372 } else if (val > _max) {
373 val = (_wrap ? _min : _max);
375 _value->setDoubleValue(val);
380 ////////////////////////////////////////////////////////////////////////
381 // Implementation of FGSwapAction.
382 ////////////////////////////////////////////////////////////////////////
384 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
385 SGValue * value1, SGValue * value2)
386 : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
390 FGSwapAction::~FGSwapAction ()
395 FGSwapAction::doAction ()
397 float val = _value1->getFloatValue();
398 _value1->setDoubleValue(_value2->getFloatValue());
399 _value2->setDoubleValue(val);
404 ////////////////////////////////////////////////////////////////////////
405 // Implementation of FGToggleAction.
406 ////////////////////////////////////////////////////////////////////////
408 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
410 : FGPanelAction(button, x, y, w, h), _value(value)
414 FGToggleAction::~FGToggleAction ()
419 FGToggleAction::doAction ()
421 _value->setBoolValue(!(_value->getBoolValue()));
426 ////////////////////////////////////////////////////////////////////////
427 // Implementation of FGPanelTransformation.
428 ////////////////////////////////////////////////////////////////////////
430 FGPanelTransformation::FGPanelTransformation ()
434 FGPanelTransformation::~FGPanelTransformation ()
440 ////////////////////////////////////////////////////////////////////////
441 // Implementation of FGPanelInstrument.
442 ////////////////////////////////////////////////////////////////////////
445 FGPanelInstrument::FGPanelInstrument ()
451 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
457 FGPanelInstrument::~FGPanelInstrument ()
459 for (action_list_type::iterator it = _actions.begin();
460 it != _actions.end();
468 FGPanelInstrument::setPosition (int x, int y)
475 FGPanelInstrument::setSize (int w, int h)
482 FGPanelInstrument::getXPos () const
488 FGPanelInstrument::getYPos () const
494 FGPanelInstrument::getWidth () const
500 FGPanelInstrument::getHeight () const
506 FGPanelInstrument::addAction (FGPanelAction * action)
508 _actions.push_back(action);
511 // Coordinates relative to centre.
513 FGPanelInstrument::doMouseAction (int button, int x, int y)
515 action_list_type::iterator it = _actions.begin();
516 action_list_type::iterator last = _actions.end();
517 for ( ; it != last; it++) {
518 if ((*it)->inArea(button, x, y)) {
528 ////////////////////////////////////////////////////////////////////////
529 // Implementation of FGLayeredInstrument.
530 ////////////////////////////////////////////////////////////////////////
532 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
533 : FGPanelInstrument(x, y, w, h)
537 FGLayeredInstrument::~FGLayeredInstrument ()
539 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
546 FGLayeredInstrument::draw ()
548 for (int i = 0; i < (int)_layers.size(); i++) {
550 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
557 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
559 int n = _layers.size();
560 if (layer->getWidth() == -1) {
561 layer->setWidth(getWidth());
563 if (layer->getHeight() == -1) {
564 layer->setHeight(getHeight());
566 _layers.push_back(layer);
571 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
574 return addLayer(new FGTexturedLayer(texture, w, h));
578 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
580 int layer = _layers.size() - 1;
581 _layers[layer]->addTransformation(transformation);
586 ////////////////////////////////////////////////////////////////////////
587 // Implementation of FGInstrumentLayer.
588 ////////////////////////////////////////////////////////////////////////
590 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
596 FGInstrumentLayer::~FGInstrumentLayer ()
598 for (transformation_list::iterator it = _transformations.begin();
599 it != _transformations.end();
607 FGInstrumentLayer::transform () const
609 transformation_list::const_iterator it = _transformations.begin();
610 transformation_list::const_iterator last = _transformations.end();
612 FGPanelTransformation *t = *it;
613 float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
616 } else if (val > t->max) {
619 val = val * t->factor + t->offset;
622 case FGPanelTransformation::XSHIFT:
623 glTranslatef(val, 0.0, 0.0);
625 case FGPanelTransformation::YSHIFT:
626 glTranslatef(0.0, val, 0.0);
628 case FGPanelTransformation::ROTATION:
629 glRotatef(-val, 0.0, 0.0, 1.0);
637 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
639 _transformations.push_back(transformation);
644 ////////////////////////////////////////////////////////////////////////
645 // Implementation of FGTexturedLayer.
646 ////////////////////////////////////////////////////////////////////////
649 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
650 : FGInstrumentLayer(w, h)
656 FGTexturedLayer::~FGTexturedLayer ()
662 FGTexturedLayer::draw ()
668 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
671 // From Curt: turn on the panel
672 // lights after sundown.
673 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
674 glColor4fv( cur_light_params.scene_diffuse );
676 glColor4f(0.7, 0.2, 0.2, 1.0);
680 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
681 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
682 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
683 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
689 ////////////////////////////////////////////////////////////////////////
690 // Implementation of FGTextLayer.
691 ////////////////////////////////////////////////////////////////////////
693 FGTextLayer::FGTextLayer (int w, int h)
694 : FGInstrumentLayer(w, h), _pointSize(14.0)
697 _color[0] = _color[1] = _color[2] = 0.0;
701 FGTextLayer::~FGTextLayer ()
703 chunk_list::iterator it = _chunks.begin();
704 chunk_list::iterator last = _chunks.end();
705 for ( ; it != last; it++) {
716 text_renderer.setFont(guiFntHandle);
717 text_renderer.setPointSize(_pointSize);
718 text_renderer.begin();
719 text_renderer.start3f(0, 0, 0);
722 if (_now - _then > 100000) {
726 text_renderer.puts((char *)(_value.c_str()));
729 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
734 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
736 _chunks.push_back(chunk);
740 FGTextLayer::setColor (float r, float g, float b)
749 FGTextLayer::setPointSize (float size)
755 FGTextLayer::setFont(fntFont * font)
757 text_renderer.setFont(font);
762 FGTextLayer::recalc_value () const
765 chunk_list::const_iterator it = _chunks.begin();
766 chunk_list::const_iterator last = _chunks.end();
767 for ( ; it != last; it++) {
768 _value += (*it)->getValue();
774 ////////////////////////////////////////////////////////////////////////
775 // Implementation of FGTextLayer::Chunk.
776 ////////////////////////////////////////////////////////////////////////
778 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
779 : _type(FGTextLayer::TEXT), _fmt(fmt)
786 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
787 const string &fmt, float mult)
788 : _type(type), _fmt(fmt), _mult(mult)
791 if (type == TEXT_VALUE)
800 FGTextLayer::Chunk::getValue () const
804 sprintf(_buf, _fmt.c_str(), _text.c_str());
807 sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
810 sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
818 ////////////////////////////////////////////////////////////////////////
819 // Implementation of FGSwitchLayer.
820 ////////////////////////////////////////////////////////////////////////
822 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
823 FGInstrumentLayer * layer1,
824 FGInstrumentLayer * layer2)
825 : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
829 FGSwitchLayer::~FGSwitchLayer ()
836 FGSwitchLayer::draw ()
839 if (_value->getBoolValue()) {