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>
46 return ((globals->get_options()->get_panel_status()) &&
47 (globals->get_options()->get_view_mode() == FGOptions::FG_VIEW_PILOT) &&
48 (globals->get_current_view()->get_view_offset() == 0.0));
53 ////////////////////////////////////////////////////////////////////////
54 // Implementation of FGTextureManager.
55 ////////////////////////////////////////////////////////////////////////
57 map<string,ssgTexture *> FGTextureManager::_textureMap;
60 FGTextureManager::createTexture (const string &relativePath)
62 ssgTexture * texture = _textureMap[relativePath];
64 cerr << "Texture " << relativePath << " does not yet exist" << endl;
65 FGPath tpath(globals->get_options()->get_fg_root());
66 tpath.append(relativePath);
67 texture = new ssgTexture((char *)tpath.c_str(), false, false);
68 _textureMap[relativePath] = texture;
69 if (_textureMap[relativePath] == 0)
70 cerr << "Texture *still* doesn't exist" << endl;
71 cerr << "Created texture " << relativePath
72 << " handle=" << texture->getHandle() << endl;
81 ////////////////////////////////////////////////////////////////////////
82 // Implementation of FGCropped Texture.
83 ////////////////////////////////////////////////////////////////////////
86 FGCroppedTexture::FGCroppedTexture ()
87 : _path(""), _texture(0),
88 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
93 FGCroppedTexture::FGCroppedTexture (const string &path,
94 float minX, float minY,
95 float maxX, float maxY)
96 : _path(path), _texture(0),
97 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
102 FGCroppedTexture::~FGCroppedTexture ()
108 FGCroppedTexture::getTexture ()
111 _texture = FGTextureManager::createTexture(_path);
118 ////////////////////////////////////////////////////////////////////////
119 // Implementation of FGPanel.
120 ////////////////////////////////////////////////////////////////////////
122 FGPanel * current_panel = NULL;
123 static fntRenderer text_renderer;
129 FGPanel::FGPanel (int window_x, int window_y, int window_w, int window_h)
132 _winx(window_x), _winy(window_y), _winw(window_w), _winh(window_h),
133 _width(_winw), _height(int(_winh * 0.5768 + 1)),
134 _x_offset(0), _y_offset(0), _view_height(int(_winh * 0.4232))
136 setVisibility(fgPanelVisible());
145 for (instrument_list_type::iterator it = _instruments.begin();
146 it != _instruments.end();
155 * Add an instrument to the panel.
158 FGPanel::addInstrument (FGPanelInstrument * instrument)
160 _instruments.push_back(instrument);
168 FGPanel::update () const
170 // Do nothing if the panel isn't visible.
171 if (!fgPanelVisible())
174 // If the mouse is down, do something
177 if (_mouseDelay < 0) {
178 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
183 // Now, draw the panel
184 glMatrixMode(GL_PROJECTION);
187 gluOrtho2D(_winx, _winx + _winw, _winy, _winy + _winh);
189 glMatrixMode(GL_MODELVIEW);
193 glTranslated(_x_offset, _y_offset, 0);
195 // Draw the background
196 glEnable(GL_TEXTURE_2D);
197 glDisable(GL_LIGHTING);
199 glEnable(GL_ALPHA_TEST);
200 glEnable(GL_COLOR_MATERIAL);
201 // glColor4f(1.0, 1.0, 1.0, 1.0);
202 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
203 glColor4fv( cur_light_params.scene_diffuse );
205 glColor4f(0.7, 0.2, 0.2, 1.0);
207 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
209 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
210 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
212 glTexCoord2f(0.0, 0.0); glVertex3f(_winx, _winy, 0);
213 glTexCoord2f(1.0, 0.0); glVertex3f(_winx + _width, _winy, 0);
214 glTexCoord2f(1.0, 1.0); glVertex3f(_winx + _width, _winy + _height, 0);
215 glTexCoord2f(0.0, 1.0); glVertex3f(_winx, _winy + _height, 0);
218 // Draw the instruments.
219 instrument_list_type::const_iterator current = _instruments.begin();
220 instrument_list_type::const_iterator end = _instruments.end();
222 for ( ; current != end; current++) {
223 FGPanelInstrument * instr = *current;
225 glTranslated(_x_offset, _y_offset, 0);
226 glTranslated(instr->getXPos(), instr->getYPos(), 0);
230 glMatrixMode(GL_PROJECTION);
232 glMatrixMode(GL_MODELVIEW);
234 ssgForceBasicState();
235 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
240 * Set the panel's visibility.
243 FGPanel::setVisibility (bool visibility)
245 _visibility = visibility;
250 * Return true if the panel is visible.
253 FGPanel::getVisibility () const
260 * Set the panel's background texture.
263 FGPanel::setBackground (ssgTexture * texture)
270 * Set the panel's x-offset.
273 FGPanel::setXOffset (int offset)
275 if (offset <= 0 && offset >= -_width + _winw)
281 * Set the panel's y-offset.
284 FGPanel::setYOffset (int offset)
286 if (offset <= 0 && offset >= -_height)
292 * Perform a mouse action.
295 FGPanel::doMouseAction (int button, int updown, int x, int y)
298 // Note a released button and return
299 // cerr << "Doing mouse action\n";
302 _mouseInstrument = 0;
306 // Scale for the real window size.
307 x = int(((float)x / globals->get_options()->get_xsize()) * _winw);
308 y = int(_winh - (((float)y / globals->get_options()->get_ysize())
311 // Adjust for offsets.
315 // Search for a matching instrument.
316 for (int i = 0; i < (int)_instruments.size(); i++) {
317 FGPanelInstrument *inst = _instruments[i];
318 int ix = inst->getXPos();
319 int iy = inst->getYPos();
320 int iw = inst->getWidth() / 2;
321 int ih = inst->getHeight() / 2;
322 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
325 _mouseInstrument = inst;
326 _mouseButton = button;
329 // Always do the action once.
330 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
339 ////////////////////////////////////////////////////////////////////////.
340 // Implementation of FGPanelAction.
341 ////////////////////////////////////////////////////////////////////////
343 FGPanelAction::FGPanelAction ()
347 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
348 : _button(button), _x(x), _y(y), _w(w), _h(h)
352 FGPanelAction::~FGPanelAction ()
358 ////////////////////////////////////////////////////////////////////////
359 // Implementation of FGAdjustAction.
360 ////////////////////////////////////////////////////////////////////////
362 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
363 SGValue * value, float increment,
364 float min, float max, bool wrap)
365 : FGPanelAction(button, x, y, w, h),
366 _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
370 FGAdjustAction::~FGAdjustAction ()
375 FGAdjustAction::doAction ()
377 float val = _value->getFloatValue();
380 val = (_wrap ? _max : _min);
381 } else if (val > _max) {
382 val = (_wrap ? _min : _max);
384 _value->setDoubleValue(val);
389 ////////////////////////////////////////////////////////////////////////
390 // Implementation of FGSwapAction.
391 ////////////////////////////////////////////////////////////////////////
393 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
394 SGValue * value1, SGValue * value2)
395 : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
399 FGSwapAction::~FGSwapAction ()
404 FGSwapAction::doAction ()
406 float val = _value1->getFloatValue();
407 _value1->setDoubleValue(_value2->getFloatValue());
408 _value2->setDoubleValue(val);
413 ////////////////////////////////////////////////////////////////////////
414 // Implementation of FGToggleAction.
415 ////////////////////////////////////////////////////////////////////////
417 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
419 : FGPanelAction(button, x, y, w, h), _value(value)
423 FGToggleAction::~FGToggleAction ()
428 FGToggleAction::doAction ()
430 _value->setBoolValue(!(_value->getBoolValue()));
435 ////////////////////////////////////////////////////////////////////////
436 // Implementation of FGPanelTransformation.
437 ////////////////////////////////////////////////////////////////////////
439 FGPanelTransformation::FGPanelTransformation ()
443 FGPanelTransformation::~FGPanelTransformation ()
449 ////////////////////////////////////////////////////////////////////////
450 // Implementation of FGPanelInstrument.
451 ////////////////////////////////////////////////////////////////////////
454 FGPanelInstrument::FGPanelInstrument ()
460 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
466 FGPanelInstrument::~FGPanelInstrument ()
468 for (action_list_type::iterator it = _actions.begin();
469 it != _actions.end();
477 FGPanelInstrument::setPosition (int x, int y)
484 FGPanelInstrument::setSize (int w, int h)
491 FGPanelInstrument::getXPos () const
497 FGPanelInstrument::getYPos () const
503 FGPanelInstrument::getWidth () const
509 FGPanelInstrument::getHeight () const
515 FGPanelInstrument::addAction (FGPanelAction * action)
517 _actions.push_back(action);
520 // Coordinates relative to centre.
522 FGPanelInstrument::doMouseAction (int button, int x, int y)
524 action_list_type::iterator it = _actions.begin();
525 action_list_type::iterator last = _actions.end();
526 for ( ; it != last; it++) {
527 if ((*it)->inArea(button, x, y)) {
537 ////////////////////////////////////////////////////////////////////////
538 // Implementation of FGLayeredInstrument.
539 ////////////////////////////////////////////////////////////////////////
541 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
542 : FGPanelInstrument(x, y, w, h)
546 FGLayeredInstrument::~FGLayeredInstrument ()
548 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
555 FGLayeredInstrument::draw ()
557 for (int i = 0; i < (int)_layers.size(); i++) {
559 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
566 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
568 int n = _layers.size();
569 if (layer->getWidth() == -1) {
570 layer->setWidth(getWidth());
572 if (layer->getHeight() == -1) {
573 layer->setHeight(getHeight());
575 _layers.push_back(layer);
580 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
583 return addLayer(new FGTexturedLayer(texture, w, h));
587 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
589 int layer = _layers.size() - 1;
590 _layers[layer]->addTransformation(transformation);
595 ////////////////////////////////////////////////////////////////////////
596 // Implementation of FGInstrumentLayer.
597 ////////////////////////////////////////////////////////////////////////
599 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
605 FGInstrumentLayer::~FGInstrumentLayer ()
607 for (transformation_list::iterator it = _transformations.begin();
608 it != _transformations.end();
616 FGInstrumentLayer::transform () const
618 transformation_list::const_iterator it = _transformations.begin();
619 transformation_list::const_iterator last = _transformations.end();
621 FGPanelTransformation *t = *it;
622 float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
625 } else if (val > t->max) {
628 val = val * t->factor + t->offset;
631 case FGPanelTransformation::XSHIFT:
632 glTranslatef(val, 0.0, 0.0);
634 case FGPanelTransformation::YSHIFT:
635 glTranslatef(0.0, val, 0.0);
637 case FGPanelTransformation::ROTATION:
638 glRotatef(-val, 0.0, 0.0, 1.0);
646 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
648 _transformations.push_back(transformation);
653 ////////////////////////////////////////////////////////////////////////
654 // Implementation of FGTexturedLayer.
655 ////////////////////////////////////////////////////////////////////////
658 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
659 : FGInstrumentLayer(w, h)
665 FGTexturedLayer::~FGTexturedLayer ()
671 FGTexturedLayer::draw ()
677 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
680 // From Curt: turn on the panel
681 // lights after sundown.
682 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
683 glColor4fv( cur_light_params.scene_diffuse );
685 glColor4f(0.7, 0.2, 0.2, 1.0);
689 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
690 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
691 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
692 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
698 ////////////////////////////////////////////////////////////////////////
699 // Implementation of FGTextLayer.
700 ////////////////////////////////////////////////////////////////////////
702 FGTextLayer::FGTextLayer (int w, int h)
703 : FGInstrumentLayer(w, h), _pointSize(14.0)
706 _color[0] = _color[1] = _color[2] = 0.0;
710 FGTextLayer::~FGTextLayer ()
712 chunk_list::iterator it = _chunks.begin();
713 chunk_list::iterator last = _chunks.end();
714 for ( ; it != last; it++) {
725 text_renderer.setFont(guiFntHandle);
726 text_renderer.setPointSize(_pointSize);
727 text_renderer.begin();
728 text_renderer.start3f(0, 0, 0);
731 if (_now - _then > 100000) {
735 text_renderer.puts((char *)(_value.c_str()));
738 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
743 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
745 _chunks.push_back(chunk);
749 FGTextLayer::setColor (float r, float g, float b)
758 FGTextLayer::setPointSize (float size)
764 FGTextLayer::setFont(fntFont * font)
766 text_renderer.setFont(font);
771 FGTextLayer::recalc_value () const
774 chunk_list::const_iterator it = _chunks.begin();
775 chunk_list::const_iterator last = _chunks.end();
776 for ( ; it != last; it++) {
777 _value += (*it)->getValue();
783 ////////////////////////////////////////////////////////////////////////
784 // Implementation of FGTextLayer::Chunk.
785 ////////////////////////////////////////////////////////////////////////
787 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
788 : _type(FGTextLayer::TEXT), _fmt(fmt)
795 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
796 const string &fmt, float mult)
797 : _type(type), _fmt(fmt), _mult(mult)
800 if (type == TEXT_VALUE)
809 FGTextLayer::Chunk::getValue () const
813 sprintf(_buf, _fmt.c_str(), _text.c_str());
816 sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
819 sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
827 ////////////////////////////////////////////////////////////////////////
828 // Implementation of FGSwitchLayer.
829 ////////////////////////////////////////////////////////////////////////
831 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
832 FGInstrumentLayer * layer1,
833 FGInstrumentLayer * layer2)
834 : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
838 FGSwitchLayer::~FGSwitchLayer ()
845 FGSwitchLayer::draw ()
848 if (_value->getBoolValue()) {