]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
First quick hack at panel shading.
[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   if (updown == 1) {
194     _mouseDown = false;
195     _mouseInstrument = 0;
196     return true;
197   }
198
199   x = (int)(((float)x / current_view.get_winWidth()) * _w);
200   y = (int)(_h - (((float)y / current_view.get_winHeight()) * _h));
201
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';
210       _mouseDown = true;
211       _mouseDelay = 20;
212       _mouseInstrument = inst;
213       _mouseButton = button;
214       _mouseX = x - ix;
215       _mouseY = y - iy;
216                                 // Always do the action once.
217       _mouseInstrument->doMouseAction(_mouseButton, _mouseX, _mouseY);
218       return true;
219     }
220   }
221 //   cout << "Did not click on an instrument\n";
222   return false;
223 }
224
225
226 \f
227 ////////////////////////////////////////////////////////////////////////
228 // Implementation of FGAdjustAction.
229 ////////////////////////////////////////////////////////////////////////
230
231 FGAdjustAction::FGAdjustAction (getter_type getter, setter_type setter,
232                                 float increment, float min, float max,
233                                 bool wrap=false)
234   : _getter(getter), _setter(setter), _increment(increment),
235     _min(min), _max(max), _wrap(wrap)
236 {
237 }
238
239 FGAdjustAction::~FGAdjustAction ()
240 {
241 }
242
243 void
244 FGAdjustAction::doAction ()
245 {
246   float value = (*_getter)();
247 //   cout << "Do action; value=" << value << '\n';
248   value += _increment;
249   if (value < _min) {
250     value = (_wrap ? _max : _min);
251   } else if (value > _max) {
252     value = (_wrap ? _min : _max);
253   }
254 //   cout << "New value is " << value << '\n';
255   (*_setter)(value);
256 }
257
258
259 \f
260 ////////////////////////////////////////////////////////////////////////
261 // Implementation of FGSwapAction.
262 ////////////////////////////////////////////////////////////////////////
263
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)
268 {
269 }
270
271 FGSwapAction::~FGSwapAction ()
272 {
273 }
274
275 void
276 FGSwapAction::doAction ()
277 {
278   float value = (*_getter1)();
279   (*_setter1)((*_getter2)());
280   (*_setter2)(value);
281 }
282
283
284 \f
285 ////////////////////////////////////////////////////////////////////////
286 // Implementation of FGToggleAction.
287 ////////////////////////////////////////////////////////////////////////
288
289 FGToggleAction::FGToggleAction (getter_type getter, setter_type setter)
290   : _getter(getter), _setter(setter)
291 {
292 }
293
294 FGToggleAction::~FGToggleAction ()
295 {
296 }
297
298 void
299 FGToggleAction::doAction ()
300 {
301   (*_setter)(!((*_getter)()));
302 }
303
304
305 \f
306 ////////////////////////////////////////////////////////////////////////
307 // Implementation of FGPanelInstrument.
308 ////////////////////////////////////////////////////////////////////////
309
310
311 FGPanelInstrument::FGPanelInstrument ()
312 {
313   setPosition(0, 0);
314   setSize(0, 0);
315 }
316
317 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
318 {
319   setPosition(x, y);
320   setSize(w, h);
321 }
322
323 FGPanelInstrument::~FGPanelInstrument ()
324 {
325   action_list_type::iterator it = _actions.begin();
326   action_list_type::iterator last = _actions.end();
327   for ( ; it != last; it++) {
328     delete it->action;
329   }
330 }
331
332 void
333 FGPanelInstrument::setPosition (int x, int y)
334 {
335   _x = x;
336   _y = y;
337 }
338
339 void
340 FGPanelInstrument::setSize (int w, int h)
341 {
342   _w = w;
343   _h = h;
344 }
345
346 int
347 FGPanelInstrument::getXPos () const
348 {
349   return _x;
350 }
351
352 int
353 FGPanelInstrument::getYPos () const
354 {
355   return _y;
356 }
357
358 int
359 FGPanelInstrument::getWidth () const
360 {
361   return _w;
362 }
363
364 int
365 FGPanelInstrument::getHeight () const
366 {
367   return _h;
368 }
369
370 void
371 FGPanelInstrument::addAction (int button, int x, int y, int w, int h,
372                               FGPanelAction * action)
373 {
374   FGPanelInstrument::inst_action act;
375   act.button = button;
376   act.x = x;
377   act.y = y;
378   act.w = w;
379   act.h = h;
380   act.action = action;
381   _actions.push_back(act);
382 }
383
384                                 // Coordinates relative to centre.
385 bool
386 FGPanelInstrument::doMouseAction (int button, int x, int y)
387 {
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();
397       return true;
398     }
399   }
400   return false;
401 }
402
403
404 \f
405 ////////////////////////////////////////////////////////////////////////
406 // Implementation of FGLayeredInstrument.
407 ////////////////////////////////////////////////////////////////////////
408
409 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
410   : FGPanelInstrument(x, y, w, h)
411 {
412 }
413
414 FGLayeredInstrument::~FGLayeredInstrument ()
415 {
416   // FIXME: free layers
417 }
418
419 void
420 FGLayeredInstrument::draw () const
421 {
422   for (int i = 0; i < _layers.size(); i++) {
423     glPushMatrix();
424     glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
425     _layers[i]->draw();
426     glPopMatrix();
427   }
428 }
429
430 int
431 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
432 {
433   int n = _layers.size();
434   _layers.push_back(layer);
435   return n;
436 }
437
438 int
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)
443 {
444   if (w == -1)
445     w = _w;
446   if (h == -1)
447     h = _h;
448   FGTexturedLayer * layer = new FGTexturedLayer(texture, w, h);
449   layer->setTextureCoords(texX1, texY1, texX2, texY2);
450   return addLayer(layer);
451 }
452
453 void
454 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
455                                         FGInstrumentLayer::transform_func func,
456                                         float min, float max,
457                                         float factor, float offset)
458 {
459   int layer = _layers.size() - 1;
460   _layers[layer]->addTransformation(type, func, min, max, factor, offset);
461 }
462
463 void
464 FGLayeredInstrument::addTransformation (FGInstrumentLayer::transform_type type,
465                                         float offset)
466 {
467   addTransformation(type, 0, 0.0, 0.0, 1.0, offset);
468 }
469
470
471 \f
472 ////////////////////////////////////////////////////////////////////////
473 // Implementation of FGInstrumentLayer.
474 ////////////////////////////////////////////////////////////////////////
475
476 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
477   : _w(w),
478     _h(h)
479 {
480 }
481
482 FGInstrumentLayer::~FGInstrumentLayer ()
483 {
484   transformation_list::iterator it = _transformations.begin();
485   transformation_list::iterator end = _transformations.end();
486   while (it != end) {
487     delete *it;
488     it++;
489   }
490 }
491
492 void
493 FGInstrumentLayer::transform () const
494 {
495   transformation_list::const_iterator it = _transformations.begin();
496   transformation_list::const_iterator last = _transformations.end();
497   while (it != last) {
498     transformation *t = *it;
499     float value = (t->func == 0 ? 0.0 : (*(t->func))());
500     if (value < t->min) {
501       value = t->min;
502     } else if (value > t->max) {
503       value = t->max;
504     }
505     value = value * t->factor + t->offset;
506
507     switch (t->type) {
508     case XSHIFT:
509       glTranslatef(value, 0.0, 0.0);
510       break;
511     case YSHIFT:
512       glTranslatef(0.0, value, 0.0);
513       break;
514     case ROTATION:
515       glRotatef(-value, 0.0, 0.0, 1.0);
516       break;
517     }
518     it++;
519   }
520 }
521
522 void
523 FGInstrumentLayer::addTransformation (transform_type type,
524                                       transform_func func,
525                                       float min, float max,
526                                       float factor, float offset)
527 {
528   transformation *t = new transformation;
529   t->type = type;
530   t->func = func;
531   t->min = min;
532   t->max = max;
533   t->factor = factor;
534   t->offset = offset;
535   _transformations.push_back(t);
536 }
537
538
539 \f
540 ////////////////////////////////////////////////////////////////////////
541 // Implementation of FGTexturedLayer.
542 ////////////////////////////////////////////////////////////////////////
543
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)
549 {
550   setTexture(texture);
551 }
552
553 FGTexturedLayer::~FGTexturedLayer ()
554 {
555 }
556
557 void
558 FGTexturedLayer::draw () const
559 {
560   int w2 = _w / 2;
561   int h2 = _h / 2;
562
563   transform();
564   glBindTexture(GL_TEXTURE_2D, _texture->getHandle());
565   glBegin(GL_POLYGON);
566   if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
567       glColor4fv( cur_light_params.scene_diffuse );
568   } else {
569       glColor4f(0.7, 0.2, 0.2, 1.0);
570   }
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);
575   glEnd();
576 }
577
578
579 \f
580 ////////////////////////////////////////////////////////////////////////
581 // Implementation of FGTextLayer.
582 ////////////////////////////////////////////////////////////////////////
583
584 FGTextLayer::FGTextLayer (int w, int h)
585   : FGInstrumentLayer(w, h)
586 {
587   _color[0] = _color[1] = _color[2] = 0.0;
588   _color[3] = 1.0;
589 }
590
591 FGTextLayer::~FGTextLayer ()
592 {
593   chunk_list::iterator it = _chunks.begin();
594   chunk_list::iterator last = _chunks.end();
595   for ( ; it != last; it++) {
596     delete *it;
597   }
598 }
599
600 void
601 FGTextLayer::draw () const
602 {
603   glPushMatrix();
604   glColor4fv(_color);
605   transform();
606   _renderer.setFont(guiFntHandle);
607   _renderer.setPointSize(14);
608   _renderer.begin();
609   _renderer.start3f(0, 0, 0);
610
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());
616   }
617
618   _renderer.end();
619   glColor4f(1.0, 1.0, 1.0, 1.0);        // FIXME
620   glPopMatrix();
621 }
622
623 void
624 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
625 {
626   _chunks.push_back(chunk);
627 }
628
629 void
630 FGTextLayer::setColor (float r, float g, float b)
631 {
632   _color[0] = r;
633   _color[1] = g;
634   _color[2] = b;
635   _color[3] = 1.0;
636 }
637
638 void
639 FGTextLayer::setPointSize (const float size)
640 {
641   _renderer.setPointSize(size);
642 }
643
644 void
645 FGTextLayer::setFont(fntFont * font)
646 {
647   _renderer.setFont(font);
648 }
649
650
651 \f
652 ////////////////////////////////////////////////////////////////////////
653 // Implementation of FGTextLayer::Chunk.
654 ////////////////////////////////////////////////////////////////////////
655
656 FGTextLayer::Chunk::Chunk (char * text, char * fmt = "%s")
657   : _type(FGTextLayer::TEXT), _fmt(fmt)
658 {
659   _value._text = text;
660 }
661
662 FGTextLayer::Chunk::Chunk (text_func func, char * fmt = "%s")
663   : _type(FGTextLayer::TEXT_FUNC), _fmt(fmt)
664 {
665   _value._tfunc = func;
666 }
667
668 FGTextLayer::Chunk::Chunk (double_func func, char * fmt = "%.2f",
669                            float mult = 1.0)
670   : _type(FGTextLayer::DOUBLE_FUNC), _fmt(fmt), _mult(mult)
671 {
672   _value._dfunc = func;
673 }
674
675 char *
676 FGTextLayer::Chunk::getValue () const
677 {
678   switch (_type) {
679   case TEXT:
680     sprintf(_buf, _fmt, _value._text);
681     return _buf;
682   case TEXT_FUNC:
683     sprintf(_buf, _fmt, (*(_value._tfunc))());
684     break;
685   case DOUBLE_FUNC:
686     sprintf(_buf, _fmt, (*(_value._dfunc))() * _mult);
687     break;
688   }
689   return _buf;
690 }
691
692
693 \f
694 ////////////////////////////////////////////////////////////////////////
695 // Implementation of FGSwitchLayer.
696 ////////////////////////////////////////////////////////////////////////
697
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)
702 {
703 }
704
705 FGSwitchLayer::~FGSwitchLayer ()
706 {
707   delete _layer1;
708   delete _layer2;
709 }
710
711 void
712 FGSwitchLayer::draw () const
713 {
714   transform();
715   if ((*_func)()) {
716     _layer1->draw();
717   } else {
718     _layer2->draw();
719   }
720 }
721
722 \f
723 // end of panel.cxx