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;
72 ////////////////////////////////////////////////////////////////////////
73 // Implementation of FGPanel.
74 ////////////////////////////////////////////////////////////////////////
76 FGPanel * current_panel = NULL;
78 FGPanel::FGPanel (int x, int y, int w, int h)
81 _x(x), _y(y), _w(w), _h(h)
83 setVisibility(current_options.get_panel_status());
84 _panel_h = (int)(h * 0.5768 + 1);
89 for (instrument_list_type::iterator it = _instruments.begin();
90 it != _instruments.end();
98 FGPanel::addInstrument (FGPanelInstrument * instrument)
100 _instruments.push_back(instrument);
104 FGPanel::update () const
106 // Do nothing if the panel isn't visible.
110 // If the mouse is down, do something
113 if (_mouseDelay < 0) {
114 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
119 // Now, draw the panel
120 glMatrixMode(GL_PROJECTION);
123 gluOrtho2D(_x, _x + _w, _y, _y + _h);
125 glMatrixMode(GL_MODELVIEW);
129 // Draw the background
130 glEnable(GL_TEXTURE_2D);
131 glDisable(GL_LIGHTING);
133 glEnable(GL_ALPHA_TEST);
134 glEnable(GL_COLOR_MATERIAL);
135 // glColor4f(1.0, 1.0, 1.0, 1.0);
136 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
137 glColor4fv( cur_light_params.scene_diffuse );
139 glColor4f(0.7, 0.2, 0.2, 1.0);
141 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
142 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
144 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
146 glTexCoord2f(0.0, 0.0); glVertex3f(_x, _y, 0);
147 glTexCoord2f(10.0, 0.0); glVertex3f(_x + _w, _y, 0);
148 glTexCoord2f(10.0, 5.0); glVertex3f(_x + _w, _y + _panel_h, 0);
149 glTexCoord2f(0.0, 5.0); glVertex3f(_x, _y + _panel_h, 0);
152 // Draw the instruments.
153 instrument_list_type::const_iterator current = _instruments.begin();
154 instrument_list_type::const_iterator end = _instruments.end();
156 for ( ; current != end; current++) {
157 FGPanelInstrument * instr = *current;
159 glTranslated(instr->getXPos(), instr->getYPos(), 0);
163 glMatrixMode(GL_PROJECTION);
165 glMatrixMode(GL_MODELVIEW);
167 ssgForceBasicState();
168 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
172 FGPanel::setVisibility (bool visibility)
174 _visibility = visibility;
178 FGPanel::getVisibility () const
184 FGPanel::setBackground (ssgTexture * texture)
190 FGPanel::doMouseAction (int button, int updown, int x, int y)
192 // Note a released button and return
193 // cerr << "Doing mouse action\n";
196 _mouseInstrument = 0;
200 x = (int)(((float)x / current_view.get_winWidth()) * _w);
201 y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
203 for (int i = 0; i < (int)_instruments.size(); i++) {
204 FGPanelInstrument *inst = _instruments[i];
205 int ix = inst->getXPos();
206 int iy = inst->getYPos();
207 int iw = inst->getWidth() / 2;
208 int ih = inst->getHeight() / 2;
209 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
212 _mouseInstrument = inst;
213 _mouseButton = button;
216 // Always do the action once.
217 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
226 ////////////////////////////////////////////////////////////////////////.
227 // Implementation of FGPanelAction.
228 ////////////////////////////////////////////////////////////////////////
230 FGPanelAction::FGPanelAction ()
234 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
235 : _button(button), _x(x), _y(y), _w(w), _h(h)
239 FGPanelAction::~FGPanelAction ()
245 ////////////////////////////////////////////////////////////////////////
246 // Implementation of FGAdjustAction.
247 ////////////////////////////////////////////////////////////////////////
249 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
250 SGValue * value, float increment,
251 float min, float max, bool wrap)
252 : FGPanelAction(button, x, y, w, h),
253 _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
257 FGAdjustAction::~FGAdjustAction ()
262 FGAdjustAction::doAction ()
264 float val = _value->getFloatValue();
267 val = (_wrap ? _max : _min);
268 } else if (val > _max) {
269 val = (_wrap ? _min : _max);
271 _value->setDoubleValue(val);
276 ////////////////////////////////////////////////////////////////////////
277 // Implementation of FGSwapAction.
278 ////////////////////////////////////////////////////////////////////////
280 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
281 SGValue * value1, SGValue * value2)
282 : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
286 FGSwapAction::~FGSwapAction ()
291 FGSwapAction::doAction ()
293 float val = _value1->getFloatValue();
294 _value1->setDoubleValue(_value2->getFloatValue());
295 _value2->setDoubleValue(val);
300 ////////////////////////////////////////////////////////////////////////
301 // Implementation of FGToggleAction.
302 ////////////////////////////////////////////////////////////////////////
304 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
306 : FGPanelAction(button, x, y, w, h), _value(value)
310 FGToggleAction::~FGToggleAction ()
315 FGToggleAction::doAction ()
317 _value->setBoolValue(!(_value->getBoolValue()));
322 ////////////////////////////////////////////////////////////////////////
323 // Implementation of FGPanelTransformation.
324 ////////////////////////////////////////////////////////////////////////
326 FGPanelTransformation::FGPanelTransformation ()
330 FGPanelTransformation::FGPanelTransformation (Type _type,
331 const SGValue * _value,
332 float _min, float _max,
333 float _factor, float _offset)
334 : type(_type), value(_value), min(_min), max(_max),
335 factor(_factor), offset(_offset)
339 FGPanelTransformation::~FGPanelTransformation ()
345 ////////////////////////////////////////////////////////////////////////
346 // Implementation of FGPanelInstrument.
347 ////////////////////////////////////////////////////////////////////////
350 FGPanelInstrument::FGPanelInstrument ()
356 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
362 FGPanelInstrument::~FGPanelInstrument ()
364 for (action_list_type::iterator it = _actions.begin();
365 it != _actions.end();
373 FGPanelInstrument::setPosition (int x, int y)
380 FGPanelInstrument::setSize (int w, int h)
387 FGPanelInstrument::getXPos () const
393 FGPanelInstrument::getYPos () const
399 FGPanelInstrument::getWidth () const
405 FGPanelInstrument::getHeight () const
411 FGPanelInstrument::addAction (FGPanelAction * action)
413 _actions.push_back(action);
416 // Coordinates relative to centre.
418 FGPanelInstrument::doMouseAction (int button, int x, int y)
420 action_list_type::iterator it = _actions.begin();
421 action_list_type::iterator last = _actions.end();
422 for ( ; it != last; it++) {
423 if ((*it)->inArea(button, x, y)) {
433 ////////////////////////////////////////////////////////////////////////
434 // Implementation of FGLayeredInstrument.
435 ////////////////////////////////////////////////////////////////////////
437 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
438 : FGPanelInstrument(x, y, w, h)
442 FGLayeredInstrument::~FGLayeredInstrument ()
444 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
451 FGLayeredInstrument::draw ()
453 for (int i = 0; i < (int)_layers.size(); i++) {
455 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
462 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
464 int n = _layers.size();
465 if (layer->getWidth() == -1) {
466 layer->setWidth(getWidth());
468 if (layer->getHeight() == -1) {
469 layer->setHeight(getHeight());
471 _layers.push_back(layer);
476 FGLayeredInstrument::addLayer (CroppedTexture &texture,
479 return addLayer(new FGTexturedLayer(texture, w, h));
483 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
485 int layer = _layers.size() - 1;
486 _layers[layer]->addTransformation(transformation);
491 ////////////////////////////////////////////////////////////////////////
492 // Implementation of FGInstrumentLayer.
493 ////////////////////////////////////////////////////////////////////////
495 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
501 FGInstrumentLayer::~FGInstrumentLayer ()
503 for (transformation_list::iterator it = _transformations.begin();
504 it != _transformations.end();
512 FGInstrumentLayer::transform () const
514 transformation_list::const_iterator it = _transformations.begin();
515 transformation_list::const_iterator last = _transformations.end();
517 FGPanelTransformation *t = *it;
518 float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
521 } else if (val > t->max) {
524 val = val * t->factor + t->offset;
527 case FGPanelTransformation::XSHIFT:
528 glTranslatef(val, 0.0, 0.0);
530 case FGPanelTransformation::YSHIFT:
531 glTranslatef(0.0, val, 0.0);
533 case FGPanelTransformation::ROTATION:
534 glRotatef(-val, 0.0, 0.0, 1.0);
542 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
544 _transformations.push_back(transformation);
549 ////////////////////////////////////////////////////////////////////////
550 // Implementation of FGTexturedLayer.
551 ////////////////////////////////////////////////////////////////////////
554 FGTexturedLayer::FGTexturedLayer (const CroppedTexture &texture, int w, int h)
555 : FGInstrumentLayer(w, h)
561 FGTexturedLayer::~FGTexturedLayer ()
567 FGTexturedLayer::draw ()
573 glBindTexture(GL_TEXTURE_2D, _texture.texture->getHandle());
575 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
576 glColor4fv( cur_light_params.scene_diffuse );
578 glColor4f(0.7, 0.2, 0.2, 1.0);
580 glTexCoord2f(_texture.minX, _texture.minY); glVertex2f(-w2, -h2);
581 glTexCoord2f(_texture.maxX, _texture.minY); glVertex2f(w2, -h2);
582 glTexCoord2f(_texture.maxX, _texture.maxY); glVertex2f(w2, h2);
583 glTexCoord2f(_texture.minX, _texture.maxY); glVertex2f(-w2, h2);
589 ////////////////////////////////////////////////////////////////////////
590 // Implementation of FGWindowLayer.
591 ////////////////////////////////////////////////////////////////////////
593 FGWindowLayer::FGWindowLayer (int w, int h)
594 : FGTexturedLayer (w, h)
598 FGWindowLayer::FGWindowLayer (const CroppedTexture &texture, int w, int h)
599 : FGTexturedLayer(texture, w, h)
603 FGWindowLayer::~FGWindowLayer ()
608 FGWindowLayer::draw ()
610 // doesn't do anything yet
611 FGTexturedLayer::draw();
616 ////////////////////////////////////////////////////////////////////////
617 // Implementation of FGTextLayer.
618 ////////////////////////////////////////////////////////////////////////
620 FGTextLayer::FGTextLayer (int w, int h, Chunk * chunk1, Chunk * chunk2,
622 : FGInstrumentLayer(w, h), _pointSize(14.0)
624 _color[0] = _color[1] = _color[2] = 0.0;
634 FGTextLayer::~FGTextLayer ()
636 chunk_list::iterator it = _chunks.begin();
637 chunk_list::iterator last = _chunks.end();
638 for ( ; it != last; it++) {
649 _renderer.setFont(guiFntHandle);
650 _renderer.setPointSize(_pointSize);
652 _renderer.start3f(0, 0, 0);
654 // Render each of the chunks.
655 chunk_list::const_iterator it = _chunks.begin();
656 chunk_list::const_iterator last = _chunks.end();
657 for ( ; it != last; it++) {
658 _renderer.puts((char *)((*it)->getValue()));
662 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
667 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
669 _chunks.push_back(chunk);
673 FGTextLayer::setColor (float r, float g, float b)
682 FGTextLayer::setPointSize (float size)
688 FGTextLayer::setFont(fntFont * font)
690 _renderer.setFont(font);
695 ////////////////////////////////////////////////////////////////////////
696 // Implementation of FGTextLayer::Chunk.
697 ////////////////////////////////////////////////////////////////////////
699 FGTextLayer::Chunk::Chunk (const string &text, const string &fmt)
700 : _type(FGTextLayer::TEXT), _fmt(fmt)
707 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
708 const string &fmt, float mult)
709 : _type(type), _fmt(fmt), _mult(mult)
712 if (type == TEXT_VALUE)
721 FGTextLayer::Chunk::getValue () const
725 sprintf(_buf, _fmt.c_str(), _text.c_str());
728 sprintf(_buf, _fmt.c_str(), _value->getStringValue().c_str());
731 sprintf(_buf, _fmt.c_str(), _value->getFloatValue() * _mult);
739 ////////////////////////////////////////////////////////////////////////
740 // Implementation of FGSwitchLayer.
741 ////////////////////////////////////////////////////////////////////////
743 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
744 FGInstrumentLayer * layer1,
745 FGInstrumentLayer * layer2)
746 : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
750 FGSwitchLayer::~FGSwitchLayer ()
757 FGSwitchLayer::draw ()
760 if (_value->getBoolValue()) {