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<const char *,ssgTexture *> FGTextureManager::_textureMap;
52 FGTextureManager::createTexture (const char * relativePath)
56 texture = _textureMap[relativePath];
58 FGPath tpath(current_options.get_fg_root());
59 tpath.append(relativePath);
60 texture = new ssgTexture((char *)tpath.c_str(), false, false);
61 _textureMap[relativePath] = texture;
62 cerr << "Created texture " << relativePath
63 << " handle=" << texture->getHandle() << endl;
71 ////////////////////////////////////////////////////////////////////////
72 // Implementation of FGPanel.
73 ////////////////////////////////////////////////////////////////////////
75 FGPanel * current_panel = NULL;
77 FGPanel::FGPanel (int x, int y, int w, int h)
80 _x(x), _y(y), _w(w), _h(h)
82 setVisibility(current_options.get_panel_status());
83 _panel_h = (int)(h * 0.5768 + 1);
88 instrument_list_type::iterator current = _instruments.begin();
89 instrument_list_type::iterator last = _instruments.end();
91 for ( ; current != last; ++current) {
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 < _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) {
210 // cout << "Do mouse action for component " << i << '\n';
213 _mouseInstrument = inst;
214 _mouseButton = button;
217 // Always do the action once.
218 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
222 // cout << "Did not click on an instrument\n";
228 ////////////////////////////////////////////////////////////////////////
229 // Implementation of FGAdjustAction.
230 ////////////////////////////////////////////////////////////////////////
232 FGAdjustAction::FGAdjustAction (getter_type getter, setter_type setter,
233 float increment, float min, float max,
235 : _getter(getter), _setter(setter), _increment(increment),
236 _min(min), _max(max), _wrap(wrap)
240 FGAdjustAction::~FGAdjustAction ()
245 FGAdjustAction::doAction ()
247 float value = (*_getter)();
248 // cout << "Do action; value=" << value << '\n';
251 value = (_wrap ? _max : _min);
252 } else if (value > _max) {
253 value = (_wrap ? _min : _max);
255 // cout << "New value is " << value << '\n';
261 ////////////////////////////////////////////////////////////////////////
262 // Implementation of FGSwapAction.
263 ////////////////////////////////////////////////////////////////////////
265 FGSwapAction::FGSwapAction (getter_type getter1, setter_type setter1,
266 getter_type getter2, setter_type setter2)
267 : _getter1(getter1), _setter1(setter1),
268 _getter2(getter2), _setter2(setter2)
272 FGSwapAction::~FGSwapAction ()
277 FGSwapAction::doAction ()
279 float value = (*_getter1)();
280 (*_setter1)((*_getter2)());
286 ////////////////////////////////////////////////////////////////////////
287 // Implementation of FGToggleAction.
288 ////////////////////////////////////////////////////////////////////////
290 FGToggleAction::FGToggleAction (getter_type getter, setter_type setter)
291 : _getter(getter), _setter(setter)
295 FGToggleAction::~FGToggleAction ()
300 FGToggleAction::doAction ()
302 (*_setter)(!((*_getter)()));
307 ////////////////////////////////////////////////////////////////////////
308 // Implementation of FGPanelInstrument.
309 ////////////////////////////////////////////////////////////////////////
312 FGPanelInstrument::FGPanelInstrument ()
318 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
324 FGPanelInstrument::~FGPanelInstrument ()
326 action_list_type::iterator it = _actions.begin();
327 action_list_type::iterator last = _actions.end();
328 for ( ; it != last; it++) {
334 FGPanelInstrument::setPosition (int x, int y)
341 FGPanelInstrument::setSize (int w, int h)
348 FGPanelInstrument::getXPos () const
354 FGPanelInstrument::getYPos () const
360 FGPanelInstrument::getWidth () const
366 FGPanelInstrument::getHeight () const
372 FGPanelInstrument::addAction (int button, int x, int y, int w, int h,
373 FGPanelAction * action)
375 FGPanelInstrument::inst_action act;
382 _actions.push_back(act);
385 // Coordinates relative to centre.
387 FGPanelInstrument::doMouseAction (int button, int x, int y)
389 action_list_type::iterator it = _actions.begin();
390 action_list_type::iterator last = _actions.end();
391 // cout << "Mouse action at " << x << ',' << y << '\n';
392 for ( ; it != last; it++) {
393 // cout << "Trying action at " << it->x << ',' << it->y << ','
394 // << it->w <<',' << it->h << '\n';
395 if (button == it->button &&
396 x >= it->x && x < it->x + it->w && y >= it->y && y < it->y + it->h) {
397 it->action->doAction();
406 ////////////////////////////////////////////////////////////////////////
407 // Implementation of FGLayeredInstrument.
408 ////////////////////////////////////////////////////////////////////////
410 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
411 : FGPanelInstrument(x, y, w, h)
415 FGLayeredInstrument::~FGLayeredInstrument ()
417 // FIXME: free layers
421 FGLayeredInstrument::draw () const
423 for (int i = 0; i < _layers.size(); i++) {
425 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
432 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
434 int n = _layers.size();
435 if (layer->getWidth() == -1) {
436 layer->setWidth(getWidth());
438 if (layer->getHeight() == -1) {
439 layer->setHeight(getHeight());
441 _layers.push_back(layer);
446 FGLayeredInstrument::addLayer (CroppedTexture &texture,
447 int w = -1, int h = -1)
449 return addLayer(new FGTexturedLayer(texture, w, h));
453 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
454 FGInstrumentLayer::transform_func func,
455 float min, float max,
456 float factor, float offset)
458 int layer = _layers.size() - 1;
459 _layers[layer]->addTransformation(type, func, min, max, factor, offset);
463 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
466 addTransformation(type, 0, 0.0, 0.0, 1.0, offset);
471 ////////////////////////////////////////////////////////////////////////
472 // Implementation of FGInstrumentLayer.
473 ////////////////////////////////////////////////////////////////////////
475 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
481 FGInstrumentLayer::~FGInstrumentLayer ()
483 transformation_list::iterator it = _transformations.begin();
484 transformation_list::iterator end = _transformations.end();
492 FGInstrumentLayer::transform () const
494 transformation_list::const_iterator it = _transformations.begin();
495 transformation_list::const_iterator last = _transformations.end();
497 transformation *t = *it;
498 float value = (t->func == 0 ? 0.0 : (*(t->func))());
499 if (value < t->min) {
501 } else if (value > t->max) {
504 value = value * t->factor + t->offset;
508 glTranslatef(value, 0.0, 0.0);
511 glTranslatef(0.0, value, 0.0);
514 glRotatef(-value, 0.0, 0.0, 1.0);
522 FGInstrumentLayer::addTransformation (transform_type type,
524 float min, float max,
525 float factor, float offset)
527 transformation *t = new transformation;
534 _transformations.push_back(t);
539 ////////////////////////////////////////////////////////////////////////
540 // Implementation of FGTexturedLayer.
541 ////////////////////////////////////////////////////////////////////////
543 // FGTexturedLayer::FGTexturedLayer (ssgTexture * texture, int w, int h,
544 // float texX1 = 0.0, float texY1 = 0.0,
545 // float texX2 = 1.0, float texY2 = 1.0)
546 // : FGInstrumentLayer(w, h),
547 // _texX1(texX1), _texY1(texY1), _texX2(texX2), _texY2(texY2)
549 // setTexture(texture);
552 FGTexturedLayer::FGTexturedLayer (CroppedTexture &texture, int w, int h)
553 : FGInstrumentLayer(w, h),
554 _texX1(texture.minX), _texY1(texture.minY),
555 _texX2(texture.maxX), _texY2(texture.maxY)
557 setTexture(texture.texture);
560 FGTexturedLayer::~FGTexturedLayer ()
565 FGTexturedLayer::draw () const
571 glBindTexture(GL_TEXTURE_2D, _texture->getHandle());
573 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
574 glColor4fv( cur_light_params.scene_diffuse );
576 glColor4f(0.7, 0.2, 0.2, 1.0);
578 glTexCoord2f(_texX1, _texY1); glVertex2f(-w2, -h2);
579 glTexCoord2f(_texX2, _texY1); glVertex2f(w2, -h2);
580 glTexCoord2f(_texX2, _texY2); glVertex2f(w2, h2);
581 glTexCoord2f(_texX1, _texY2); glVertex2f(-w2, h2);
587 ////////////////////////////////////////////////////////////////////////
588 // Implementation of FGTextLayer.
589 ////////////////////////////////////////////////////////////////////////
591 FGTextLayer::FGTextLayer (int w, int h, Chunk * chunk1, Chunk * chunk2,
593 : FGInstrumentLayer(w, h)
595 _color[0] = _color[1] = _color[2] = 0.0;
605 FGTextLayer::~FGTextLayer ()
607 chunk_list::iterator it = _chunks.begin();
608 chunk_list::iterator last = _chunks.end();
609 for ( ; it != last; it++) {
615 FGTextLayer::draw () const
620 _renderer.setFont(guiFntHandle);
621 _renderer.setPointSize(14);
623 _renderer.start3f(0, 0, 0);
625 // Render each of the chunks.
626 chunk_list::const_iterator it = _chunks.begin();
627 chunk_list::const_iterator last = _chunks.end();
628 for ( ; it != last; it++) {
629 _renderer.puts((*it)->getValue());
633 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
638 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
640 _chunks.push_back(chunk);
644 FGTextLayer::setColor (float r, float g, float b)
653 FGTextLayer::setPointSize (const float size)
655 _renderer.setPointSize(size);
659 FGTextLayer::setFont(fntFont * font)
661 _renderer.setFont(font);
666 ////////////////////////////////////////////////////////////////////////
667 // Implementation of FGTextLayer::Chunk.
668 ////////////////////////////////////////////////////////////////////////
670 FGTextLayer::Chunk::Chunk (char * text, char * fmt = "%s")
671 : _type(FGTextLayer::TEXT), _fmt(fmt)
676 FGTextLayer::Chunk::Chunk (text_func func, char * fmt = "%s")
677 : _type(FGTextLayer::TEXT_FUNC), _fmt(fmt)
679 _value._tfunc = func;
682 FGTextLayer::Chunk::Chunk (double_func func, char * fmt = "%.2f",
684 : _type(FGTextLayer::DOUBLE_FUNC), _fmt(fmt), _mult(mult)
686 _value._dfunc = func;
690 FGTextLayer::Chunk::getValue () const
694 sprintf(_buf, _fmt, _value._text);
697 sprintf(_buf, _fmt, (*(_value._tfunc))());
700 sprintf(_buf, _fmt, (*(_value._dfunc))() * _mult);
708 ////////////////////////////////////////////////////////////////////////
709 // Implementation of FGSwitchLayer.
710 ////////////////////////////////////////////////////////////////////////
712 FGSwitchLayer::FGSwitchLayer (int w, int h, switch_func func,
713 FGInstrumentLayer * layer1,
714 FGInstrumentLayer * layer2)
715 : FGInstrumentLayer(w, h), _func(func), _layer1(layer1), _layer2(layer2)
719 FGSwitchLayer::~FGSwitchLayer ()
726 FGSwitchLayer::draw () const