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
195 _mouseInstrument = 0;
199 x = (int)(((float)x / current_view.get_winWidth()) * _w);
200 y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
202 for (int i = 0; i < _instruments.size(); i++) {
203 FGPanelInstrument *inst = _instruments[i];
204 int ix = inst->getXPos();
205 int iy = inst->getYPos();
206 int iw = inst->getWidth() / 2;
207 int ih = inst->getHeight() / 2;
208 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
209 // cout << "Do mouse action for component " << i << '\n';
212 _mouseInstrument = inst;
213 _mouseButton = button;
216 // Always do the action once.
217 _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
221 // cout << "Did not click on an instrument\n";
227 ////////////////////////////////////////////////////////////////////////
228 // Implementation of FGAdjustAction.
229 ////////////////////////////////////////////////////////////////////////
231 FGAdjustAction::FGAdjustAction (getter_type getter, setter_type setter,
232 float increment, float min, float max,
234 : _getter(getter), _setter(setter), _increment(increment),
235 _min(min), _max(max), _wrap(wrap)
239 FGAdjustAction::~FGAdjustAction ()
244 FGAdjustAction::doAction ()
246 float value = (*_getter)();
247 // cout << "Do action; value=" << value << '\n';
250 value = (_wrap ? _max : _min);
251 } else if (value > _max) {
252 value = (_wrap ? _min : _max);
254 // cout << "New value is " << value << '\n';
260 ////////////////////////////////////////////////////////////////////////
261 // Implementation of FGSwapAction.
262 ////////////////////////////////////////////////////////////////////////
264 FGSwapAction::FGSwapAction (getter_type getter1, setter_type setter1,
265 getter_type getter2, setter_type setter2)
266 : _getter1(getter1), _setter1(setter1),
267 _getter2(getter2), _setter2(setter2)
271 FGSwapAction::~FGSwapAction ()
276 FGSwapAction::doAction ()
278 float value = (*_getter1)();
279 (*_setter1)((*_getter2)());
285 ////////////////////////////////////////////////////////////////////////
286 // Implementation of FGToggleAction.
287 ////////////////////////////////////////////////////////////////////////
289 FGToggleAction::FGToggleAction (getter_type getter, setter_type setter)
290 : _getter(getter), _setter(setter)
294 FGToggleAction::~FGToggleAction ()
299 FGToggleAction::doAction ()
301 (*_setter)(!((*_getter)()));
306 ////////////////////////////////////////////////////////////////////////
307 // Implementation of FGPanelInstrument.
308 ////////////////////////////////////////////////////////////////////////
311 FGPanelInstrument::FGPanelInstrument ()
317 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
323 FGPanelInstrument::~FGPanelInstrument ()
325 action_list_type::iterator it = _actions.begin();
326 action_list_type::iterator last = _actions.end();
327 for ( ; it != last; it++) {
333 FGPanelInstrument::setPosition (int x, int y)
340 FGPanelInstrument::setSize (int w, int h)
347 FGPanelInstrument::getXPos () const
353 FGPanelInstrument::getYPos () const
359 FGPanelInstrument::getWidth () const
365 FGPanelInstrument::getHeight () const
371 FGPanelInstrument::addAction (int button, int x, int y, int w, int h,
372 FGPanelAction * action)
374 FGPanelInstrument::inst_action act;
381 _actions.push_back(act);
384 // Coordinates relative to centre.
386 FGPanelInstrument::doMouseAction (int button, int x, int y)
388 action_list_type::iterator it = _actions.begin();
389 action_list_type::iterator last = _actions.end();
390 // cout << "Mouse action at " << x << ',' << y << '\n';
391 for ( ; it != last; it++) {
392 // cout << "Trying action at " << it->x << ',' << it->y << ','
393 // << it->w <<',' << it->h << '\n';
394 if (button == it->button &&
395 x >= it->x && x < it->x + it->w && y >= it->y && y < it->y + it->h) {
396 it->action->doAction();
405 ////////////////////////////////////////////////////////////////////////
406 // Implementation of FGLayeredInstrument.
407 ////////////////////////////////////////////////////////////////////////
409 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
410 : FGPanelInstrument(x, y, w, h)
414 FGLayeredInstrument::~FGLayeredInstrument ()
416 // FIXME: free layers
420 FGLayeredInstrument::draw () const
422 for (int i = 0; i < _layers.size(); i++) {
424 glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
431 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
433 int n = _layers.size();
434 _layers.push_back(layer);
439 FGLayeredInstrument::addLayer (ssgTexture * texture,
440 int w = -1, int h = -1,
441 float texX1 = 0.0, float texY1 = 0.0,
442 float texX2 = 1.0, float texY2 = 1.0)
448 FGTexturedLayer * layer = new FGTexturedLayer(texture, w, h);
449 layer->setTextureCoords(texX1, texY1, texX2, texY2);
450 return addLayer(layer);
454 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
455 FGInstrumentLayer::transform_func func,
456 float min, float max,
457 float factor, float offset)
459 int layer = _layers.size() - 1;
460 _layers[layer]->addTransformation(type, func, min, max, factor, offset);
464 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
467 addTransformation(type, 0, 0.0, 0.0, 1.0, offset);
472 ////////////////////////////////////////////////////////////////////////
473 // Implementation of FGInstrumentLayer.
474 ////////////////////////////////////////////////////////////////////////
476 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
482 FGInstrumentLayer::~FGInstrumentLayer ()
484 transformation_list::iterator it = _transformations.begin();
485 transformation_list::iterator end = _transformations.end();
493 FGInstrumentLayer::transform () const
495 transformation_list::const_iterator it = _transformations.begin();
496 transformation_list::const_iterator last = _transformations.end();
498 transformation *t = *it;
499 float value = (t->func == 0 ? 0.0 : (*(t->func))());
500 if (value < t->min) {
502 } else if (value > t->max) {
505 value = value * t->factor + t->offset;
509 glTranslatef(value, 0.0, 0.0);
512 glTranslatef(0.0, value, 0.0);
515 glRotatef(-value, 0.0, 0.0, 1.0);
523 FGInstrumentLayer::addTransformation (transform_type type,
525 float min, float max,
526 float factor, float offset)
528 transformation *t = new transformation;
535 _transformations.push_back(t);
540 ////////////////////////////////////////////////////////////////////////
541 // Implementation of FGTexturedLayer.
542 ////////////////////////////////////////////////////////////////////////
544 FGTexturedLayer::FGTexturedLayer (ssgTexture * texture, int w, int h,
545 float texX1 = 0.0, float texY1 = 0.0,
546 float texX2 = 1.0, float texY2 = 1.0)
547 : FGInstrumentLayer(w, h),
548 _texX1(texX1), _texY1(texY1), _texX2(texX2), _texY2(texY2)
553 FGTexturedLayer::~FGTexturedLayer ()
558 FGTexturedLayer::draw () const
564 glBindTexture(GL_TEXTURE_2D, _texture->getHandle());
566 if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
567 glColor4fv( cur_light_params.scene_diffuse );
569 glColor4f(0.7, 0.2, 0.2, 1.0);
571 glTexCoord2f(_texX1, _texY1); glVertex2f(-w2, -h2);
572 glTexCoord2f(_texX2, _texY1); glVertex2f(w2, -h2);
573 glTexCoord2f(_texX2, _texY2); glVertex2f(w2, h2);
574 glTexCoord2f(_texX1, _texY2); glVertex2f(-w2, h2);
580 ////////////////////////////////////////////////////////////////////////
581 // Implementation of FGTextLayer.
582 ////////////////////////////////////////////////////////////////////////
584 FGTextLayer::FGTextLayer (int w, int h)
585 : FGInstrumentLayer(w, h)
587 _color[0] = _color[1] = _color[2] = 0.0;
591 FGTextLayer::~FGTextLayer ()
593 chunk_list::iterator it = _chunks.begin();
594 chunk_list::iterator last = _chunks.end();
595 for ( ; it != last; it++) {
601 FGTextLayer::draw () const
606 _renderer.setFont(guiFntHandle);
607 _renderer.setPointSize(14);
609 _renderer.start3f(0, 0, 0);
611 // Render each of the chunks.
612 chunk_list::const_iterator it = _chunks.begin();
613 chunk_list::const_iterator last = _chunks.end();
614 for ( ; it != last; it++) {
615 _renderer.puts((*it)->getValue());
619 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
624 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
626 _chunks.push_back(chunk);
630 FGTextLayer::setColor (float r, float g, float b)
639 FGTextLayer::setPointSize (const float size)
641 _renderer.setPointSize(size);
645 FGTextLayer::setFont(fntFont * font)
647 _renderer.setFont(font);
652 ////////////////////////////////////////////////////////////////////////
653 // Implementation of FGTextLayer::Chunk.
654 ////////////////////////////////////////////////////////////////////////
656 FGTextLayer::Chunk::Chunk (char * text, char * fmt = "%s")
657 : _type(FGTextLayer::TEXT), _fmt(fmt)
662 FGTextLayer::Chunk::Chunk (text_func func, char * fmt = "%s")
663 : _type(FGTextLayer::TEXT_FUNC), _fmt(fmt)
665 _value._tfunc = func;
668 FGTextLayer::Chunk::Chunk (double_func func, char * fmt = "%.2f",
670 : _type(FGTextLayer::DOUBLE_FUNC), _fmt(fmt), _mult(mult)
672 _value._dfunc = func;
676 FGTextLayer::Chunk::getValue () const
680 sprintf(_buf, _fmt, _value._text);
683 sprintf(_buf, _fmt, (*(_value._tfunc))());
686 sprintf(_buf, _fmt, (*(_value._dfunc))() * _mult);
694 ////////////////////////////////////////////////////////////////////////
695 // Implementation of FGSwitchLayer.
696 ////////////////////////////////////////////////////////////////////////
698 FGSwitchLayer::FGSwitchLayer (int w, int h, switch_func func,
699 FGInstrumentLayer * layer1,
700 FGInstrumentLayer * layer2)
701 : FGInstrumentLayer(w, h), _func(func), _layer1(layer1), _layer2(layer2)
705 FGSwitchLayer::~FGSwitchLayer ()
712 FGSwitchLayer::draw () const