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;
118 FGPanel::FGPanel (int x, int y, int w, int h)
121 _x(x), _y(y), _w(w), _h(h)
123 setVisibility(current_options.get_panel_status());
124 _panel_h = (int)(h * 0.5768 + 1);
129 for (instrument_list_type::iterator it = _instruments.begin();
130 it != _instruments.end();
138 FGPanel::addInstrument (FGPanelInstrument * instrument)
140 _instruments.push_back(instrument);
144 FGPanel::update () const
146 // Do nothing if the panel isn't visible.
150 // If the mouse is down, do something
153 if (_mouseDelay < 0) {
154 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
159 // Now, draw the panel
160 glMatrixMode(GL_PROJECTION);
163 gluOrtho2D(_x, _x + _w, _y, _y + _h);
165 glMatrixMode(GL_MODELVIEW);
169 // Draw the background
170 glEnable(GL_TEXTURE_2D);
171 glDisable(GL_LIGHTING);
173 glEnable(GL_ALPHA_TEST);
174 glEnable(GL_COLOR_MATERIAL);
175 // glColor4f(1.0, 1.0, 1.0, 1.0);
176 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
177 glColor4fv( cur_light_params.scene_diffuse );
179 glColor4f(0.7, 0.2, 0.2, 1.0);
181 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
184 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
186 glTexCoord2f(0.0, 0.0); glVertex3f(_x, _y, 0);
187 glTexCoord2f(10.0, 0.0); glVertex3f(_x + _w, _y, 0);
188 glTexCoord2f(10.0, 5.0); glVertex3f(_x + _w, _y + _panel_h, 0);
189 glTexCoord2f(0.0, 5.0); glVertex3f(_x, _y + _panel_h, 0);
192 // Draw the instruments.
193 instrument_list_type::const_iterator current = _instruments.begin();
194 instrument_list_type::const_iterator end = _instruments.end();
196 for ( ; current != end; current++) {
197 FGPanelInstrument * instr = *current;
199 glTranslated(instr->getXPos(), instr->getYPos(), 0);
203 glMatrixMode(GL_PROJECTION);
205 glMatrixMode(GL_MODELVIEW);
207 ssgForceBasicState();
208 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
212 FGPanel::setVisibility (bool visibility)
214 _visibility = visibility;
218 FGPanel::getVisibility () const
224 FGPanel::setBackground (ssgTexture * texture)
230 FGPanel::doMouseAction (int button, int updown, int x, int y)
232 // Note a released button and return
233 // cerr << "Doing mouse action\n";
236 _mouseInstrument = 0;
240 x = (int)(((float)x / current_view.get_winWidth()) * _w);
241 y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
243 for (int i = 0; i < (int)_instruments.size(); i++) {
244 FGPanelInstrument *inst = _instruments[i];
245 int ix = inst->getXPos();
246 int iy = inst->getYPos();
247 int iw = inst->getWidth() / 2;
248 int ih = inst->getHeight() / 2;
249 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
252 _mouseInstrument = inst;
253 _mouseButton = button;
256 // Always do the action once.
257 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
266 ////////////////////////////////////////////////////////////////////////.
267 // Implementation of FGPanelAction.
268 ////////////////////////////////////////////////////////////////////////
270 FGPanelAction::FGPanelAction ()
274 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
275 : _button(button), _x(x), _y(y), _w(w), _h(h)
279 FGPanelAction::~FGPanelAction ()
285 ////////////////////////////////////////////////////////////////////////
286 // Implementation of FGAdjustAction.
287 ////////////////////////////////////////////////////////////////////////
289 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
290 SGValue * value, float increment,
291 float min, float max, bool wrap)
292 : FGPanelAction(button, x, y, w, h),
293 _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
297 FGAdjustAction::~FGAdjustAction ()
302 FGAdjustAction::doAction ()
304 float val = _value->getFloatValue();
307 val = (_wrap ? _max : _min);
308 } else if (val > _max) {
309 val = (_wrap ? _min : _max);
311 _value->setDoubleValue(val);
316 ////////////////////////////////////////////////////////////////////////
317 // Implementation of FGSwapAction.
318 ////////////////////////////////////////////////////////////////////////
320 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
321 SGValue * value1, SGValue * value2)
322 : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
326 FGSwapAction::~FGSwapAction ()
331 FGSwapAction::doAction ()
333 float val = _value1->getFloatValue();
334 _value1->setDoubleValue(_value2->getFloatValue());
335 _value2->setDoubleValue(val);
340 ////////////////////////////////////////////////////////////////////////
341 // Implementation of FGToggleAction.
342 ////////////////////////////////////////////////////////////////////////
344 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
346 : FGPanelAction(button, x, y, w, h), _value(value)
350 FGToggleAction::~FGToggleAction ()
355 FGToggleAction::doAction ()
357 _value->setBoolValue(!(_value->getBoolValue()));
362 ////////////////////////////////////////////////////////////////////////
363 // Implementation of FGPanelTransformation.
364 ////////////////////////////////////////////////////////////////////////
366 FGPanelTransformation::FGPanelTransformation ()
370 FGPanelTransformation::~FGPanelTransformation ()
376 ////////////////////////////////////////////////////////////////////////
377 // Implementation of FGPanelInstrument.
378 ////////////////////////////////////////////////////////////////////////
381 FGPanelInstrument::FGPanelInstrument ()
387 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
393 FGPanelInstrument::~FGPanelInstrument ()
395 for (action_list_type::iterator it = _actions.begin();
396 it != _actions.end();
404 FGPanelInstrument::setPosition (int x, int y)
411 FGPanelInstrument::setSize (int w, int h)
418 FGPanelInstrument::getXPos () const
424 FGPanelInstrument::getYPos () const
430 FGPanelInstrument::getWidth () const
436 FGPanelInstrument::getHeight () const
442 FGPanelInstrument::addAction (FGPanelAction * action)
444 _actions.push_back(action);
447 // Coordinates relative to centre.
449 FGPanelInstrument::doMouseAction (int button, int x, int y)
451 action_list_type::iterator it = _actions.begin();
452 action_list_type::iterator last = _actions.end();
453 for ( ; it != last; it++) {
454 if ((*it)->inArea(button, x, y)) {
464 ////////////////////////////////////////////////////////////////////////
465 // Implementation of FGLayeredInstrument.
466 ////////////////////////////////////////////////////////////////////////
468 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
469 : FGPanelInstrument(x, y, w, h)
473 FGLayeredInstrument::~FGLayeredInstrument ()
475 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
482 FGLayeredInstrument::draw ()
484 for (int i = 0; i < (int)_layers.size(); i++) {
486 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
493 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
495 int n = _layers.size();
496 if (layer->getWidth() == -1) {
497 layer->setWidth(getWidth());
499 if (layer->getHeight() == -1) {
500 layer->setHeight(getHeight());
502 _layers.push_back(layer);
507 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
510 return addLayer(new FGTexturedLayer(texture, w, h));
514 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
516 int layer = _layers.size() - 1;
517 _layers[layer]->addTransformation(transformation);
522 ////////////////////////////////////////////////////////////////////////
523 // Implementation of FGInstrumentLayer.
524 ////////////////////////////////////////////////////////////////////////
526 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
532 FGInstrumentLayer::~FGInstrumentLayer ()
534 for (transformation_list::iterator it = _transformations.begin();
535 it != _transformations.end();
543 FGInstrumentLayer::transform () const
545 transformation_list::const_iterator it = _transformations.begin();
546 transformation_list::const_iterator last = _transformations.end();
548 FGPanelTransformation *t = *it;
549 float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
552 } else if (val > t->max) {
555 val = val * t->factor + t->offset;
558 case FGPanelTransformation::XSHIFT:
559 glTranslatef(val, 0.0, 0.0);
561 case FGPanelTransformation::YSHIFT:
562 glTranslatef(0.0, val, 0.0);
564 case FGPanelTransformation::ROTATION:
565 glRotatef(-val, 0.0, 0.0, 1.0);
573 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
575 _transformations.push_back(transformation);
580 ////////////////////////////////////////////////////////////////////////
581 // Implementation of FGTexturedLayer.
582 ////////////////////////////////////////////////////////////////////////
585 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
586 : FGInstrumentLayer(w, h)
592 FGTexturedLayer::~FGTexturedLayer ()
598 FGTexturedLayer::draw ()
604 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
607 // From Curt: turn on the panel
608 // lights after sundown.
609 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
610 glColor4fv( cur_light_params.scene_diffuse );
612 glColor4f(0.7, 0.2, 0.2, 1.0);
616 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
617 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
618 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
619 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
625 ////////////////////////////////////////////////////////////////////////
626 // Implementation of FGTextLayer.
627 ////////////////////////////////////////////////////////////////////////
629 FGTextLayer::FGTextLayer (int w, int h)
630 : FGInstrumentLayer(w, h), _pointSize(14.0)
633 _color[0] = _color[1] = _color[2] = 0.0;
637 FGTextLayer::~FGTextLayer ()
639 chunk_list::iterator it = _chunks.begin();
640 chunk_list::iterator last = _chunks.end();
641 for ( ; it != last; it++) {
652 text_renderer.setFont(guiFntHandle);
653 text_renderer.setPointSize(_pointSize);
654 text_renderer.begin();
655 text_renderer.start3f(0, 0, 0);
658 if (_now - _then > 100000) {
662 text_renderer.puts((char *)(_value.c_str()));
665 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
670 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
672 _chunks.push_back(chunk);
676 FGTextLayer::setColor (float r, float g, float b)
685 FGTextLayer::setPointSize (float size)
691 FGTextLayer::setFont(fntFont * font)
693 text_renderer.setFont(font);
698 FGTextLayer::recalc_value () const
701 chunk_list::const_iterator it = _chunks.begin();
702 chunk_list::const_iterator last = _chunks.end();
703 for ( ; it != last; it++) {
704 _value += (*it)->getValue();
710 ////////////////////////////////////////////////////////////////////////
711 // Implementation of FGTextLayer::Chunk.
712 ////////////////////////////////////////////////////////////////////////
714 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
715 : _type(FGTextLayer::TEXT), _fmt(fmt)
722 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
723 const string &fmt, float mult)
724 : _type(type), _fmt(fmt), _mult(mult)
727 if (type == TEXT_VALUE)
736 FGTextLayer::Chunk::getValue () const
740 sprintf(_buf, _fmt.c_str(), _text.c_str());
743 sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
746 sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
754 ////////////////////////////////////////////////////////////////////////
755 // Implementation of FGSwitchLayer.
756 ////////////////////////////////////////////////////////////////////////
758 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
759 FGInstrumentLayer * layer1,
760 FGInstrumentLayer * layer2)
761 : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
765 FGSwitchLayer::~FGSwitchLayer ()
772 FGSwitchLayer::draw ()
775 if (_value->getBoolValue()) {