]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Updates from David Megginson:
[flightgear.git] / src / Cockpit / panel.cxx
1 //  panel.cxx - default, 2D single-engine prop instrument panel
2 //
3 //  Written by David Megginson, started January 2000.
4 //
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.
9 // 
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.
14 // 
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.
18 //
19 //  $Id$
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #ifdef HAVE_WINDOWS_H          
26 #  include <windows.h>
27 #endif
28
29 #include <string.h>
30
31 #include <plib/ssg.h>
32 #include <plib/fnt.h>
33
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>
39
40 #include "hud.hxx"
41 #include "panel.hxx"
42
43
44 \f
45 ////////////////////////////////////////////////////////////////////////
46 // Implementation of FGTextureManager.
47 ////////////////////////////////////////////////////////////////////////
48
49 map<const char *,ssgTexture *> FGTextureManager::_textureMap;
50
51 ssgTexture *
52 FGTextureManager::createTexture (const char * relativePath)
53 {
54   ssgTexture *texture;
55
56   texture = _textureMap[relativePath];
57   if (texture == 0) {
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;
64   }
65
66   return texture;
67 }
68
69
70 \f
71 ////////////////////////////////////////////////////////////////////////
72 // Implementation of FGPanel.
73 ////////////////////////////////////////////////////////////////////////
74
75 FGPanel * current_panel = NULL;
76
77 FGPanel::FGPanel (int x, int y, int w, int h)
78   : _mouseDown(false),
79     _mouseInstrument(0),
80     _x(x), _y(y), _w(w), _h(h)
81 {
82   setVisibility(current_options.get_panel_status());
83   _panel_h = (int)(h * 0.5768 + 1);
84 }
85
86 FGPanel::~FGPanel ()
87 {
88   instrument_list_type::iterator current = _instruments.begin();
89   instrument_list_type::iterator last = _instruments.end();
90   
91   for ( ; current != last; ++current) {
92     delete *current;
93     *current = 0;
94   }
95 }
96
97 void
98 FGPanel::addInstrument (FGPanelInstrument * instrument)
99 {
100   _instruments.push_back(instrument);
101 }
102
103 void
104 FGPanel::update () const
105 {
106                                 // Do nothing if the panel isn't visible.
107   if (!_visibility)
108     return;
109
110                                 // If the mouse is down, do something
111   if (_mouseDown) {
112     _mouseDelay--;
113     if (_mouseDelay < 0) {
114       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
115       _mouseDelay = 2;
116     }
117   }
118
119                                 // Now, draw the panel
120   glMatrixMode(GL_PROJECTION);
121   glPushMatrix();
122   glLoadIdentity();
123   gluOrtho2D(_x, _x + _w, _y, _y + _h);
124
125   glMatrixMode(GL_MODELVIEW);
126   glPushMatrix();
127   glLoadIdentity();
128
129                                 // Draw the background
130   glEnable(GL_TEXTURE_2D);
131   glDisable(GL_LIGHTING);
132   glEnable(GL_BLEND);
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 );
138   } else {
139       glColor4f(0.7, 0.2, 0.2, 1.0);
140   }
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);
145   glBegin(GL_POLYGON);
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);
150   glEnd();
151
152                                 // Draw the instruments.
153   instrument_list_type::const_iterator current = _instruments.begin();
154   instrument_list_type::const_iterator end = _instruments.end();
155
156   for ( ; current != end; current++) {
157     FGPanelInstrument * instr = *current;
158     glLoadIdentity();
159     glTranslated(instr->getXPos(), instr->getYPos(), 0);
160     instr->draw();
161   }
162
163   glMatrixMode(GL_PROJECTION);
164   glPopMatrix();
165   glMatrixMode(GL_MODELVIEW);
166   glPopMatrix();
167   ssgForceBasicState();
168   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
169 }
170
171 void
172 FGPanel::setVisibility (bool visibility)
173 {
174   _visibility = visibility;
175 }
176
177 bool
178 FGPanel::getVisibility () const
179 {
180   return _visibility;
181 }
182
183 void
184 FGPanel::setBackground (ssgTexture * texture)
185 {
186   _bg = texture;
187 }
188
189 bool
190 FGPanel::doMouseAction (int button, int updown, int x, int y)
191 {
192                                 // Note a released button and return
193   // cerr << "Doing mouse action\n";
194   if (updown == 1) {
195     _mouseDown = false;
196     _mouseInstrument = 0;
197     return true;
198   }
199
200   x = (int)(((float)x / current_view.get_winWidth()) * _w);
201   y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
202
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';
211       _mouseDown = true;
212       _mouseDelay = 20;
213       _mouseInstrument = inst;
214       _mouseButton = button;
215       _mouseX = x - ix;
216       _mouseY = y - iy;
217                                 // Always do the action once.
218       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
219       return true;
220     }
221   }
222 //   cout << "Did not click on an instrument\n";
223   return false;
224 }
225
226
227 \f
228 ////////////////////////////////////////////////////////////////////////
229 // Implementation of FGAdjustAction.
230 ////////////////////////////////////////////////////////////////////////
231
232 FGAdjustAction::FGAdjustAction (getter_type getter, setter_type setter,
233                                 float increment, float min, float max,
234                                 bool wrap=false)
235   : _getter(getter), _setter(setter), _increment(increment),
236     _min(min), _max(max), _wrap(wrap)
237 {
238 }
239
240 FGAdjustAction::~FGAdjustAction ()
241 {
242 }
243
244 void
245 FGAdjustAction::doAction ()
246 {
247   float value = (*_getter)();
248 //   cout << "Do action; value=" << value << '\n';
249   value += _increment;
250   if (value < _min) {
251     value = (_wrap ? _max : _min);
252   } else if (value > _max) {
253     value = (_wrap ? _min : _max);
254   }
255 //   cout << "New value is " << value << '\n';
256   (*_setter)(value);
257 }
258
259
260 \f
261 ////////////////////////////////////////////////////////////////////////
262 // Implementation of FGSwapAction.
263 ////////////////////////////////////////////////////////////////////////
264
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)
269 {
270 }
271
272 FGSwapAction::~FGSwapAction ()
273 {
274 }
275
276 void
277 FGSwapAction::doAction ()
278 {
279   float value = (*_getter1)();
280   (*_setter1)((*_getter2)());
281   (*_setter2)(value);
282 }
283
284
285 \f
286 ////////////////////////////////////////////////////////////////////////
287 // Implementation of FGToggleAction.
288 ////////////////////////////////////////////////////////////////////////
289
290 FGToggleAction::FGToggleAction (getter_type getter, setter_type setter)
291   : _getter(getter), _setter(setter)
292 {
293 }
294
295 FGToggleAction::~FGToggleAction ()
296 {
297 }
298
299 void
300 FGToggleAction::doAction ()
301 {
302   (*_setter)(!((*_getter)()));
303 }
304
305
306 \f
307 ////////////////////////////////////////////////////////////////////////
308 // Implementation of FGPanelInstrument.
309 ////////////////////////////////////////////////////////////////////////
310
311
312 FGPanelInstrument::FGPanelInstrument ()
313 {
314   setPosition(0, 0);
315   setSize(0, 0);
316 }
317
318 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
319 {
320   setPosition(x, y);
321   setSize(w, h);
322 }
323
324 FGPanelInstrument::~FGPanelInstrument ()
325 {
326   action_list_type::iterator it = _actions.begin();
327   action_list_type::iterator last = _actions.end();
328   for ( ; it != last; it++) {
329     delete it->action;
330   }
331 }
332
333 void
334 FGPanelInstrument::setPosition (int x, int y)
335 {
336   _x = x;
337   _y = y;
338 }
339
340 void
341 FGPanelInstrument::setSize (int w, int h)
342 {
343   _w = w;
344   _h = h;
345 }
346
347 int
348 FGPanelInstrument::getXPos () const
349 {
350   return _x;
351 }
352
353 int
354 FGPanelInstrument::getYPos () const
355 {
356   return _y;
357 }
358
359 int
360 FGPanelInstrument::getWidth () const
361 {
362   return _w;
363 }
364
365 int
366 FGPanelInstrument::getHeight () const
367 {
368   return _h;
369 }
370
371 void
372 FGPanelInstrument::addAction (int button, int x, int y, int w, int h,
373                               FGPanelAction * action)
374 {
375   FGPanelInstrument::inst_action act;
376   act.button = button;
377   act.x = x;
378   act.y = y;
379   act.w = w;
380   act.h = h;
381   act.action = action;
382   _actions.push_back(act);
383 }
384
385                                 // Coordinates relative to centre.
386 bool
387 FGPanelInstrument::doMouseAction (int button, int x, int y)
388 {
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();
398       return true;
399     }
400   }
401   return false;
402 }
403
404
405 \f
406 ////////////////////////////////////////////////////////////////////////
407 // Implementation of FGLayeredInstrument.
408 ////////////////////////////////////////////////////////////////////////
409
410 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
411   : FGPanelInstrument(x, y, w, h)
412 {
413 }
414
415 FGLayeredInstrument::~FGLayeredInstrument ()
416 {
417   // FIXME: free layers
418 }
419
420 void
421 FGLayeredInstrument::draw () const
422 {
423   for (int i = 0; i < _layers.size(); i++) {
424     glPushMatrix();
425     glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
426     _layers[i]->draw();
427     glPopMatrix();
428   }
429 }
430
431 int
432 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
433 {
434   int n = _layers.size();
435   if (layer->getWidth() == -1) {
436     layer->setWidth(getWidth());
437   }
438   if (layer->getHeight() == -1) {
439     layer->setHeight(getHeight());
440   }
441   _layers.push_back(layer);
442   return n;
443 }
444
445 int
446 FGLayeredInstrument::addLayer (CroppedTexture &texture,
447                                int w = -1, int h = -1)
448 {
449   return addLayer(new FGTexturedLayer(texture, w, h));
450 }
451
452 void
453 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
454                                         FGInstrumentLayer::transform_func func,
455                                         float min, float max,
456                                         float factor, float offset)
457 {
458   int layer = _layers.size() - 1;
459   _layers[layer]->addTransformation(type, func, min, max, factor, offset);
460 }
461
462 void
463 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
464                                         float offset)
465 {
466   addTransformation(type, 0, 0.0, 0.0, 1.0, offset);
467 }
468
469
470 \f
471 ////////////////////////////////////////////////////////////////////////
472 // Implementation of FGInstrumentLayer.
473 ////////////////////////////////////////////////////////////////////////
474
475 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
476   : _w(w),
477     _h(h)
478 {
479 }
480
481 FGInstrumentLayer::~FGInstrumentLayer ()
482 {
483   transformation_list::iterator it = _transformations.begin();
484   transformation_list::iterator end = _transformations.end();
485   while (it != end) {
486     delete *it;
487     it++;
488   }
489 }
490
491 void
492 FGInstrumentLayer::transform () const
493 {
494   transformation_list::const_iterator it = _transformations.begin();
495   transformation_list::const_iterator last = _transformations.end();
496   while (it != last) {
497     transformation *t = *it;
498     float value = (t->func == 0 ? 0.0 : (*(t->func))());
499     if (value < t->min) {
500       value = t->min;
501     } else if (value > t->max) {
502       value = t->max;
503     }
504     value = value * t->factor + t->offset;
505
506     switch (t->type) {
507     case XSHIFT:
508       glTranslatef(value, 0.0, 0.0);
509       break;
510     case YSHIFT:
511       glTranslatef(0.0, value, 0.0);
512       break;
513     case ROTATION:
514       glRotatef(-value, 0.0, 0.0, 1.0);
515       break;
516     }
517     it++;
518   }
519 }
520
521 void
522 FGInstrumentLayer::addTransformation (transform_type type,
523                                       transform_func func,
524                                       float min, float max,
525                                       float factor, float offset)
526 {
527   transformation *t = new transformation;
528   t->type = type;
529   t->func = func;
530   t->min = min;
531   t->max = max;
532   t->factor = factor;
533   t->offset = offset;
534   _transformations.push_back(t);
535 }
536
537
538 \f
539 ////////////////////////////////////////////////////////////////////////
540 // Implementation of FGTexturedLayer.
541 ////////////////////////////////////////////////////////////////////////
542
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)
548 // {
549 //   setTexture(texture);
550 // }
551
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)
556 {
557   setTexture(texture.texture);
558 }
559
560 FGTexturedLayer::~FGTexturedLayer ()
561 {
562 }
563
564 void
565 FGTexturedLayer::draw () const
566 {
567   int w2 = _w / 2;
568   int h2 = _h / 2;
569
570   transform();
571   glBindTexture(GL_TEXTURE_2D, _texture->getHandle());
572   glBegin(GL_POLYGON);
573   if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
574       glColor4fv( cur_light_params.scene_diffuse );
575   } else {
576       glColor4f(0.7, 0.2, 0.2, 1.0);
577   }
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);
582   glEnd();
583 }
584
585
586 \f
587 ////////////////////////////////////////////////////////////////////////
588 // Implementation of FGTextLayer.
589 ////////////////////////////////////////////////////////////////////////
590
591 FGTextLayer::FGTextLayer (int w, int h, Chunk * chunk1, Chunk * chunk2,
592                           Chunk * chunk3)
593   : FGInstrumentLayer(w, h)
594 {
595   _color[0] = _color[1] = _color[2] = 0.0;
596   _color[3] = 1.0;
597   if (chunk1)
598     addChunk(chunk1);
599   if (chunk2)
600     addChunk(chunk2);
601   if (chunk3)
602     addChunk(chunk3);
603 }
604
605 FGTextLayer::~FGTextLayer ()
606 {
607   chunk_list::iterator it = _chunks.begin();
608   chunk_list::iterator last = _chunks.end();
609   for ( ; it != last; it++) {
610     delete *it;
611   }
612 }
613
614 void
615 FGTextLayer::draw () const
616 {
617   glPushMatrix();
618   glColor4fv(_color);
619   transform();
620   _renderer.setFont(guiFntHandle);
621   _renderer.setPointSize(14);
622   _renderer.begin();
623   _renderer.start3f(0, 0, 0);
624
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());
630   }
631
632   _renderer.end();
633   glColor4f(1.0, 1.0, 1.0, 1.0);        // FIXME
634   glPopMatrix();
635 }
636
637 void
638 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
639 {
640   _chunks.push_back(chunk);
641 }
642
643 void
644 FGTextLayer::setColor (float r, float g, float b)
645 {
646   _color[0] = r;
647   _color[1] = g;
648   _color[2] = b;
649   _color[3] = 1.0;
650 }
651
652 void
653 FGTextLayer::setPointSize (const float size)
654 {
655   _renderer.setPointSize(size);
656 }
657
658 void
659 FGTextLayer::setFont(fntFont * font)
660 {
661   _renderer.setFont(font);
662 }
663
664
665 \f
666 ////////////////////////////////////////////////////////////////////////
667 // Implementation of FGTextLayer::Chunk.
668 ////////////////////////////////////////////////////////////////////////
669
670 FGTextLayer::Chunk::Chunk (char * text, char * fmt = "%s")
671   : _type(FGTextLayer::TEXT), _fmt(fmt)
672 {
673   _value._text = text;
674 }
675
676 FGTextLayer::Chunk::Chunk (text_func func, char * fmt = "%s")
677   : _type(FGTextLayer::TEXT_FUNC), _fmt(fmt)
678 {
679   _value._tfunc = func;
680 }
681
682 FGTextLayer::Chunk::Chunk (double_func func, char * fmt = "%.2f",
683                            float mult = 1.0)
684   : _type(FGTextLayer::DOUBLE_FUNC), _fmt(fmt), _mult(mult)
685 {
686   _value._dfunc = func;
687 }
688
689 char *
690 FGTextLayer::Chunk::getValue () const
691 {
692   switch (_type) {
693   case TEXT:
694     sprintf(_buf, _fmt, _value._text);
695     return _buf;
696   case TEXT_FUNC:
697     sprintf(_buf, _fmt, (*(_value._tfunc))());
698     break;
699   case DOUBLE_FUNC:
700     sprintf(_buf, _fmt, (*(_value._dfunc))() * _mult);
701     break;
702   }
703   return _buf;
704 }
705
706
707 \f
708 ////////////////////////////////////////////////////////////////////////
709 // Implementation of FGSwitchLayer.
710 ////////////////////////////////////////////////////////////////////////
711
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)
716 {
717 }
718
719 FGSwitchLayer::~FGSwitchLayer ()
720 {
721   delete _layer1;
722   delete _layer2;
723 }
724
725 void
726 FGSwitchLayer::draw () const
727 {
728   transform();
729   if ((*_func)()) {
730     _layer1->draw();
731   } else {
732     _layer2->draw();
733   }
734 }
735
736 \f
737 // end of panel.cxx