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