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;
116 FGPanel::FGPanel (int x, int y, int w, int h)
119 _x(x), _y(y), _w(w), _h(h)
121 setVisibility(current_options.get_panel_status());
122 _panel_h = (int)(h * 0.5768 + 1);
127 for (instrument_list_type::iterator it = _instruments.begin();
128 it != _instruments.end();
136 FGPanel::addInstrument (FGPanelInstrument * instrument)
138 _instruments.push_back(instrument);
142 FGPanel::update () const
144 // Do nothing if the panel isn't visible.
148 // If the mouse is down, do something
151 if (_mouseDelay < 0) {
152 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
157 // Now, draw the panel
158 glMatrixMode(GL_PROJECTION);
161 gluOrtho2D(_x, _x + _w, _y, _y + _h);
163 glMatrixMode(GL_MODELVIEW);
167 // Draw the background
168 glEnable(GL_TEXTURE_2D);
169 glDisable(GL_LIGHTING);
171 glEnable(GL_ALPHA_TEST);
172 glEnable(GL_COLOR_MATERIAL);
173 // glColor4f(1.0, 1.0, 1.0, 1.0);
174 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
175 glColor4fv( cur_light_params.scene_diffuse );
177 glColor4f(0.7, 0.2, 0.2, 1.0);
179 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
180 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
182 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
184 glTexCoord2f(0.0, 0.0); glVertex3f(_x, _y, 0);
185 glTexCoord2f(10.0, 0.0); glVertex3f(_x + _w, _y, 0);
186 glTexCoord2f(10.0, 5.0); glVertex3f(_x + _w, _y + _panel_h, 0);
187 glTexCoord2f(0.0, 5.0); glVertex3f(_x, _y + _panel_h, 0);
190 // Draw the instruments.
191 instrument_list_type::const_iterator current = _instruments.begin();
192 instrument_list_type::const_iterator end = _instruments.end();
194 for ( ; current != end; current++) {
195 FGPanelInstrument * instr = *current;
197 glTranslated(instr->getXPos(), instr->getYPos(), 0);
201 glMatrixMode(GL_PROJECTION);
203 glMatrixMode(GL_MODELVIEW);
205 ssgForceBasicState();
206 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
210 FGPanel::setVisibility (bool visibility)
212 _visibility = visibility;
216 FGPanel::getVisibility () const
222 FGPanel::setBackground (ssgTexture * texture)
228 FGPanel::doMouseAction (int button, int updown, int x, int y)
230 // Note a released button and return
231 // cerr << "Doing mouse action\n";
234 _mouseInstrument = 0;
238 x = (int)(((float)x / current_view.get_winWidth()) * _w);
239 y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
241 for (int i = 0; i < (int)_instruments.size(); i++) {
242 FGPanelInstrument *inst = _instruments[i];
243 int ix = inst->getXPos();
244 int iy = inst->getYPos();
245 int iw = inst->getWidth() / 2;
246 int ih = inst->getHeight() / 2;
247 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
250 _mouseInstrument = inst;
251 _mouseButton = button;
254 // Always do the action once.
255 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
264 ////////////////////////////////////////////////////////////////////////.
265 // Implementation of FGPanelAction.
266 ////////////////////////////////////////////////////////////////////////
268 FGPanelAction::FGPanelAction ()
272 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
273 : _button(button), _x(x), _y(y), _w(w), _h(h)
277 FGPanelAction::~FGPanelAction ()
283 ////////////////////////////////////////////////////////////////////////
284 // Implementation of FGAdjustAction.
285 ////////////////////////////////////////////////////////////////////////
287 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
288 SGValue * value, float increment,
289 float min, float max, bool wrap)
290 : FGPanelAction(button, x, y, w, h),
291 _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
295 FGAdjustAction::~FGAdjustAction ()
300 FGAdjustAction::doAction ()
302 float val = _value->getFloatValue();
305 val = (_wrap ? _max : _min);
306 } else if (val > _max) {
307 val = (_wrap ? _min : _max);
309 _value->setDoubleValue(val);
314 ////////////////////////////////////////////////////////////////////////
315 // Implementation of FGSwapAction.
316 ////////////////////////////////////////////////////////////////////////
318 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
319 SGValue * value1, SGValue * value2)
320 : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
324 FGSwapAction::~FGSwapAction ()
329 FGSwapAction::doAction ()
331 float val = _value1->getFloatValue();
332 _value1->setDoubleValue(_value2->getFloatValue());
333 _value2->setDoubleValue(val);
338 ////////////////////////////////////////////////////////////////////////
339 // Implementation of FGToggleAction.
340 ////////////////////////////////////////////////////////////////////////
342 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
344 : FGPanelAction(button, x, y, w, h), _value(value)
348 FGToggleAction::~FGToggleAction ()
353 FGToggleAction::doAction ()
355 _value->setBoolValue(!(_value->getBoolValue()));
360 ////////////////////////////////////////////////////////////////////////
361 // Implementation of FGPanelTransformation.
362 ////////////////////////////////////////////////////////////////////////
364 FGPanelTransformation::FGPanelTransformation ()
368 FGPanelTransformation::FGPanelTransformation (Type _type,
369 const SGValue * _value,
370 float _min, float _max,
371 float _factor, float _offset)
372 : type(_type), value(_value), min(_min), max(_max),
373 factor(_factor), offset(_offset)
377 FGPanelTransformation::~FGPanelTransformation ()
383 ////////////////////////////////////////////////////////////////////////
384 // Implementation of FGPanelInstrument.
385 ////////////////////////////////////////////////////////////////////////
388 FGPanelInstrument::FGPanelInstrument ()
394 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
400 FGPanelInstrument::~FGPanelInstrument ()
402 for (action_list_type::iterator it = _actions.begin();
403 it != _actions.end();
411 FGPanelInstrument::setPosition (int x, int y)
418 FGPanelInstrument::setSize (int w, int h)
425 FGPanelInstrument::getXPos () const
431 FGPanelInstrument::getYPos () const
437 FGPanelInstrument::getWidth () const
443 FGPanelInstrument::getHeight () const
449 FGPanelInstrument::addAction (FGPanelAction * action)
451 _actions.push_back(action);
454 // Coordinates relative to centre.
456 FGPanelInstrument::doMouseAction (int button, int x, int y)
458 action_list_type::iterator it = _actions.begin();
459 action_list_type::iterator last = _actions.end();
460 for ( ; it != last; it++) {
461 if ((*it)->inArea(button, x, y)) {
471 ////////////////////////////////////////////////////////////////////////
472 // Implementation of FGLayeredInstrument.
473 ////////////////////////////////////////////////////////////////////////
475 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
476 : FGPanelInstrument(x, y, w, h)
480 FGLayeredInstrument::~FGLayeredInstrument ()
482 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
489 FGLayeredInstrument::draw ()
491 for (int i = 0; i < (int)_layers.size(); i++) {
493 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
500 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
502 int n = _layers.size();
503 if (layer->getWidth() == -1) {
504 layer->setWidth(getWidth());
506 if (layer->getHeight() == -1) {
507 layer->setHeight(getHeight());
509 _layers.push_back(layer);
514 FGLayeredInstrument::addLayer (FGCroppedTexture &texture,
517 return addLayer(new FGTexturedLayer(texture, w, h));
521 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
523 int layer = _layers.size() - 1;
524 _layers[layer]->addTransformation(transformation);
529 ////////////////////////////////////////////////////////////////////////
530 // Implementation of FGInstrumentLayer.
531 ////////////////////////////////////////////////////////////////////////
533 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
539 FGInstrumentLayer::~FGInstrumentLayer ()
541 for (transformation_list::iterator it = _transformations.begin();
542 it != _transformations.end();
550 FGInstrumentLayer::transform () const
552 transformation_list::const_iterator it = _transformations.begin();
553 transformation_list::const_iterator last = _transformations.end();
555 FGPanelTransformation *t = *it;
556 float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
559 } else if (val > t->max) {
562 val = val * t->factor + t->offset;
565 case FGPanelTransformation::XSHIFT:
566 glTranslatef(val, 0.0, 0.0);
568 case FGPanelTransformation::YSHIFT:
569 glTranslatef(0.0, val, 0.0);
571 case FGPanelTransformation::ROTATION:
572 glRotatef(-val, 0.0, 0.0, 1.0);
580 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
582 _transformations.push_back(transformation);
587 ////////////////////////////////////////////////////////////////////////
588 // Implementation of FGTexturedLayer.
589 ////////////////////////////////////////////////////////////////////////
592 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
593 : FGInstrumentLayer(w, h)
599 FGTexturedLayer::~FGTexturedLayer ()
605 FGTexturedLayer::draw ()
611 glBindTexture(GL_TEXTURE_2D, _texture.getTexture()->getHandle());
614 // From Curt: turn on the panel
615 // lights after sundown.
616 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
617 glColor4fv( cur_light_params.scene_diffuse );
619 glColor4f(0.7, 0.2, 0.2, 1.0);
623 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
624 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
625 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
626 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
632 ////////////////////////////////////////////////////////////////////////
633 // Implementation of FGWindowLayer.
634 ////////////////////////////////////////////////////////////////////////
636 FGWindowLayer::FGWindowLayer (int w, int h)
637 : FGTexturedLayer (w, h)
641 FGWindowLayer::FGWindowLayer (const FGCroppedTexture &texture, int w, int h)
642 : FGTexturedLayer(texture, w, h)
646 FGWindowLayer::~FGWindowLayer ()
651 FGWindowLayer::draw ()
653 // doesn't do anything yet
654 FGTexturedLayer::draw();
659 ////////////////////////////////////////////////////////////////////////
660 // Implementation of FGTextLayer.
661 ////////////////////////////////////////////////////////////////////////
663 FGTextLayer::FGTextLayer (int w, int h, Chunk * chunk1, Chunk * chunk2,
665 : FGInstrumentLayer(w, h), _pointSize(14.0)
667 _color[0] = _color[1] = _color[2] = 0.0;
677 FGTextLayer::~FGTextLayer ()
679 chunk_list::iterator it = _chunks.begin();
680 chunk_list::iterator last = _chunks.end();
681 for ( ; it != last; it++) {
692 _renderer.setFont(guiFntHandle);
693 _renderer.setPointSize(_pointSize);
695 _renderer.start3f(0, 0, 0);
697 // Render each of the chunks.
698 chunk_list::const_iterator it = _chunks.begin();
699 chunk_list::const_iterator last = _chunks.end();
700 for ( ; it != last; it++) {
701 _renderer.puts((char *)((*it)->getValue()));
705 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
710 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
712 _chunks.push_back(chunk);
716 FGTextLayer::setColor (float r, float g, float b)
725 FGTextLayer::setPointSize (float size)
731 FGTextLayer::setFont(fntFont * font)
733 _renderer.setFont(font);
738 ////////////////////////////////////////////////////////////////////////
739 // Implementation of FGTextLayer::Chunk.
740 ////////////////////////////////////////////////////////////////////////
742 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
743 : _type(FGTextLayer::TEXT), _fmt(fmt)
750 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
751 const string &fmt, float mult)
752 : _type(type), _fmt(fmt), _mult(mult)
755 if (type == TEXT_VALUE)
764 FGTextLayer::Chunk::getValue () const
768 sprintf(_buf, _fmt.c_str(), _text.c_str());
771 sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
774 sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
782 ////////////////////////////////////////////////////////////////////////
783 // Implementation of FGSwitchLayer.
784 ////////////////////////////////////////////////////////////////////////
786 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
787 FGInstrumentLayer * layer1,
788 FGInstrumentLayer * layer2)
789 : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
793 FGSwitchLayer::~FGSwitchLayer ()
800 FGSwitchLayer::draw ()
803 if (_value->getBoolValue()) {