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 glBindTexture(GL_TEXTURE_2D, _bg->getHandle());
137 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
139 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
141 glTexCoord2f(0.0, 0.0); glVertex3f(_x, _y, 0);
142 glTexCoord2f(10.0, 0.0); glVertex3f(_x + _w, _y, 0);
143 glTexCoord2f(10.0, 5.0); glVertex3f(_x + _w, _y + _panel_h, 0);
144 glTexCoord2f(0.0, 5.0); glVertex3f(_x, _y + _panel_h, 0);
147 // Draw the instruments.
148 instrument_list_type::const_iterator current = _instruments.begin();
149 instrument_list_type::const_iterator end = _instruments.end();
151 for ( ; current != end; current++) {
152 FGPanelInstrument * instr = *current;
154 glTranslated(instr->getXPos(), instr->getYPos(), 0);
158 glMatrixMode(GL_PROJECTION);
160 glMatrixMode(GL_MODELVIEW);
162 ssgForceBasicState();
163 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
167 FGPanel::setVisibility (bool visibility)
169 _visibility = visibility;
173 FGPanel::getVisibility () const
179 FGPanel::setBackground (ssgTexture * texture)
185 FGPanel::doMouseAction (int button, int updown, int x, int y)
187 // Note a released button and return
190 _mouseInstrument = 0;
194 x = (int)(((float)x / current_view.get_winWidth()) * _w);
195 y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
197 for (int i = 0; i < _instruments.size(); i++) {
198 FGPanelInstrument *inst = _instruments[i];
199 int ix = inst->getXPos();
200 int iy = inst->getYPos();
201 int iw = inst->getWidth() / 2;
202 int ih = inst->getHeight() / 2;
203 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
204 // cout << "Do mouse action for component " << i << '\n';
207 _mouseInstrument = inst;
208 _mouseButton = button;
211 // Always do the action once.
212 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
216 // cout << "Did not click on an instrument\n";
222 ////////////////////////////////////////////////////////////////////////
223 // Implementation of FGAdjustAction.
224 ////////////////////////////////////////////////////////////////////////
226 FGAdjustAction::FGAdjustAction (getter_type getter, setter_type setter,
227 float increment, float min, float max,
229 : _getter(getter), _setter(setter), _increment(increment),
230 _min(min), _max(max), _wrap(wrap)
234 FGAdjustAction::~FGAdjustAction ()
239 FGAdjustAction::doAction ()
241 float value = (*_getter)();
242 // cout << "Do action; value=" << value << '\n';
245 value = (_wrap ? _max : _min);
246 } else if (value > _max) {
247 value = (_wrap ? _min : _max);
249 // cout << "New value is " << value << '\n';
255 ////////////////////////////////////////////////////////////////////////
256 // Implementation of FGSwapAction.
257 ////////////////////////////////////////////////////////////////////////
259 FGSwapAction::FGSwapAction (getter_type getter1, setter_type setter1,
260 getter_type getter2, setter_type setter2)
261 : _getter1(getter1), _setter1(setter1),
262 _getter2(getter2), _setter2(setter2)
266 FGSwapAction::~FGSwapAction ()
271 FGSwapAction::doAction ()
273 float value = (*_getter1)();
274 (*_setter1)((*_getter2)());
280 ////////////////////////////////////////////////////////////////////////
281 // Implementation of FGToggleAction.
282 ////////////////////////////////////////////////////////////////////////
284 FGToggleAction::FGToggleAction (getter_type getter, setter_type setter)
285 : _getter(getter), _setter(setter)
289 FGToggleAction::~FGToggleAction ()
294 FGToggleAction::doAction ()
296 (*_setter)(!((*_getter)()));
301 ////////////////////////////////////////////////////////////////////////
302 // Implementation of FGPanelInstrument.
303 ////////////////////////////////////////////////////////////////////////
306 FGPanelInstrument::FGPanelInstrument ()
312 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
318 FGPanelInstrument::~FGPanelInstrument ()
320 action_list_type::iterator it = _actions.begin();
321 action_list_type::iterator last = _actions.end();
322 for ( ; it != last; it++) {
328 FGPanelInstrument::setPosition (int x, int y)
335 FGPanelInstrument::setSize (int w, int h)
342 FGPanelInstrument::getXPos () const
348 FGPanelInstrument::getYPos () const
354 FGPanelInstrument::getWidth () const
360 FGPanelInstrument::getHeight () const
366 FGPanelInstrument::addAction (int button, int x, int y, int w, int h,
367 FGPanelAction * action)
369 FGPanelInstrument::inst_action act;
376 _actions.push_back(act);
379 // Coordinates relative to centre.
381 FGPanelInstrument::doMouseAction (int button, int x, int y)
383 action_list_type::iterator it = _actions.begin();
384 action_list_type::iterator last = _actions.end();
385 // cout << "Mouse action at " << x << ',' << y << '\n';
386 for ( ; it != last; it++) {
387 // cout << "Trying action at " << it->x << ',' << it->y << ','
388 // << it->w <<',' << it->h << '\n';
389 if (button == it->button &&
390 x >= it->x && x < it->x + it->w && y >= it->y && y < it->y + it->h) {
391 it->action->doAction();
400 ////////////////////////////////////////////////////////////////////////
401 // Implementation of FGLayeredInstrument.
402 ////////////////////////////////////////////////////////////////////////
404 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
405 : FGPanelInstrument(x, y, w, h)
409 FGLayeredInstrument::~FGLayeredInstrument ()
411 // FIXME: free layers
415 FGLayeredInstrument::draw () const
417 for (int i = 0; i < _layers.size(); i++) {
419 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
426 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
428 int n = _layers.size();
429 _layers.push_back(layer);
434 FGLayeredInstrument::addLayer (ssgTexture * texture,
435 int w = -1, int h = -1,
436 float texX1 = 0.0, float texY1 = 0.0,
437 float texX2 = 1.0, float texY2 = 1.0)
443 FGTexturedLayer * layer = new FGTexturedLayer(texture, w, h);
444 layer->setTextureCoords(texX1, texY1, texX2, texY2);
445 return addLayer(layer);
449 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
450 FGInstrumentLayer::transform_func func,
451 float min, float max,
452 float factor, float offset)
454 int layer = _layers.size() - 1;
455 _layers[layer]->addTransformation(type, func, min, max, factor, offset);
459 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
462 addTransformation(type, 0, 0.0, 0.0, 1.0, offset);
467 ////////////////////////////////////////////////////////////////////////
468 // Implementation of FGInstrumentLayer.
469 ////////////////////////////////////////////////////////////////////////
471 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
477 FGInstrumentLayer::~FGInstrumentLayer ()
479 transformation_list::iterator it = _transformations.begin();
480 transformation_list::iterator end = _transformations.end();
488 FGInstrumentLayer::transform () const
490 transformation_list::const_iterator it = _transformations.begin();
491 transformation_list::const_iterator last = _transformations.end();
493 transformation *t = *it;
494 float value = (t->func == 0 ? 0.0 : (*(t->func))());
495 if (value < t->min) {
497 } else if (value > t->max) {
500 value = value * t->factor + t->offset;
504 glTranslatef(value, 0.0, 0.0);
507 glTranslatef(0.0, value, 0.0);
510 glRotatef(-value, 0.0, 0.0, 1.0);
518 FGInstrumentLayer::addTransformation (transform_type type,
520 float min, float max,
521 float factor, float offset)
523 transformation *t = new transformation;
530 _transformations.push_back(t);
535 ////////////////////////////////////////////////////////////////////////
536 // Implementation of FGTexturedLayer.
537 ////////////////////////////////////////////////////////////////////////
539 FGTexturedLayer::FGTexturedLayer (ssgTexture * texture, int w, int h,
540 float texX1 = 0.0, float texY1 = 0.0,
541 float texX2 = 1.0, float texY2 = 1.0)
542 : FGInstrumentLayer(w, h),
543 _texX1(texX1), _texY1(texY1), _texX2(texX2), _texY2(texY2)
548 FGTexturedLayer::~FGTexturedLayer ()
553 FGTexturedLayer::draw () const
559 glBindTexture(GL_TEXTURE_2D, _texture->getHandle());
562 glTexCoord2f(_texX1, _texY1); glVertex2f(-w2, -h2);
563 glTexCoord2f(_texX2, _texY1); glVertex2f(w2, -h2);
564 glTexCoord2f(_texX2, _texY2); glVertex2f(w2, h2);
565 glTexCoord2f(_texX1, _texY2); glVertex2f(-w2, h2);
571 ////////////////////////////////////////////////////////////////////////
572 // Implementation of FGTextLayer.
573 ////////////////////////////////////////////////////////////////////////
575 FGTextLayer::FGTextLayer (int w, int h)
576 : FGInstrumentLayer(w, h)
578 _color[0] = _color[1] = _color[2] = 0.0;
582 FGTextLayer::~FGTextLayer ()
584 chunk_list::iterator it = _chunks.begin();
585 chunk_list::iterator last = _chunks.end();
586 for ( ; it != last; it++) {
592 FGTextLayer::draw () const
597 _renderer.setFont(guiFntHandle);
598 _renderer.setPointSize(14);
600 _renderer.start3f(0, 0, 0);
602 // Render each of the chunks.
603 chunk_list::const_iterator it = _chunks.begin();
604 chunk_list::const_iterator last = _chunks.end();
605 for ( ; it != last; it++) {
606 _renderer.puts((*it)->getValue());
610 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
615 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
617 _chunks.push_back(chunk);
621 FGTextLayer::setColor (float r, float g, float b)
630 FGTextLayer::setPointSize (const float size)
632 _renderer.setPointSize(size);
636 FGTextLayer::setFont(fntFont * font)
638 _renderer.setFont(font);
643 ////////////////////////////////////////////////////////////////////////
644 // Implementation of FGTextLayer::Chunk.
645 ////////////////////////////////////////////////////////////////////////
647 FGTextLayer::Chunk::Chunk (char * text, char * fmt = "%s")
648 : _type(FGTextLayer::TEXT), _fmt(fmt)
653 FGTextLayer::Chunk::Chunk (text_func func, char * fmt = "%s")
654 : _type(FGTextLayer::TEXT_FUNC), _fmt(fmt)
656 _value._tfunc = func;
659 FGTextLayer::Chunk::Chunk (double_func func, char * fmt = "%.2f",
661 : _type(FGTextLayer::DOUBLE_FUNC), _fmt(fmt), _mult(mult)
663 _value._dfunc = func;
667 FGTextLayer::Chunk::getValue () const
671 sprintf(_buf, _fmt, _value._text);
674 sprintf(_buf, _fmt, (*(_value._tfunc))());
677 sprintf(_buf, _fmt, (*(_value._dfunc))() * _mult);
685 ////////////////////////////////////////////////////////////////////////
686 // Implementation of FGSwitchLayer.
687 ////////////////////////////////////////////////////////////////////////
689 FGSwitchLayer::FGSwitchLayer (int w, int h, switch_func func,
690 FGInstrumentLayer * layer1,
691 FGInstrumentLayer * layer2)
692 : FGInstrumentLayer(w, h), _func(func), _layer1(layer1), _layer2(layer2)
696 FGSwitchLayer::~FGSwitchLayer ()
703 FGSwitchLayer::draw () const