]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/panel.cxx
Sep 8, 2000 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<string,ssgTexture *> FGTextureManager::_textureMap;
50
51 ssgTexture *
52 FGTextureManager::createTexture (const string &relativePath)
53 {
54   ssgTexture * texture = _textureMap[relativePath];
55   if (texture == 0) {
56     cerr << "Texture " << relativePath << " does not yet exist" << endl;
57     FGPath tpath(current_options.get_fg_root());
58     tpath.append(relativePath);
59     texture = new ssgTexture((char *)tpath.c_str(), false, false);
60     _textureMap[relativePath] = texture;
61     if (_textureMap[relativePath] == 0) 
62       cerr << "Texture *still* doesn't exist" << endl;
63     cerr << "Created texture " << relativePath
64          << " handle=" << texture->getHandle() << endl;
65   }
66
67   return texture;
68 }
69
70
71 \f
72 ////////////////////////////////////////////////////////////////////////
73 // Implementation of FGPanel.
74 ////////////////////////////////////////////////////////////////////////
75
76 FGPanel * current_panel = NULL;
77
78 FGPanel::FGPanel (int x, int y, int w, int h)
79   : _mouseDown(false),
80     _mouseInstrument(0),
81     _x(x), _y(y), _w(w), _h(h)
82 {
83   setVisibility(current_options.get_panel_status());
84   _panel_h = (int)(h * 0.5768 + 1);
85 }
86
87 FGPanel::~FGPanel ()
88 {
89   for (instrument_list_type::iterator it = _instruments.begin();
90        it != _instruments.end();
91        it++) {
92     delete *it;
93     *it = 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       _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   return false;
222 }
223
224
225 \f
226 ////////////////////////////////////////////////////////////////////////.
227 // Implementation of FGPanelAction.
228 ////////////////////////////////////////////////////////////////////////
229
230 FGPanelAction::FGPanelAction ()
231 {
232 }
233
234 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h)
235   : _button(button), _x(x), _y(y), _w(w), _h(h)
236 {
237 }
238
239 FGPanelAction::~FGPanelAction ()
240 {
241 }
242
243
244 \f
245 ////////////////////////////////////////////////////////////////////////
246 // Implementation of FGAdjustAction.
247 ////////////////////////////////////////////////////////////////////////
248
249 FGAdjustAction::FGAdjustAction (int button, int x, int y, int w, int h,
250                                 SGValue * value, float increment, 
251                                 float min, float max, bool wrap=false)
252   : FGPanelAction(button, x, y, w, h),
253     _value(value), _increment(increment), _min(min), _max(max), _wrap(wrap)
254 {
255 }
256
257 FGAdjustAction::~FGAdjustAction ()
258 {
259 }
260
261 void
262 FGAdjustAction::doAction ()
263 {
264   float val = _value->getFloatValue();
265   val += _increment;
266   if (val < _min) {
267     val = (_wrap ? _max : _min);
268   } else if (val > _max) {
269     val = (_wrap ? _min : _max);
270   }
271   _value->setDoubleValue(val);
272 }
273
274
275 \f
276 ////////////////////////////////////////////////////////////////////////
277 // Implementation of FGSwapAction.
278 ////////////////////////////////////////////////////////////////////////
279
280 FGSwapAction::FGSwapAction (int button, int x, int y, int w, int h,
281                             SGValue * value1, SGValue * value2)
282   : FGPanelAction(button, x, y, w, h), _value1(value1), _value2(value2)
283 {
284 }
285
286 FGSwapAction::~FGSwapAction ()
287 {
288 }
289
290 void
291 FGSwapAction::doAction ()
292 {
293   float val = _value1->getFloatValue();
294   _value1->setDoubleValue(_value2->getFloatValue());
295   _value2->setDoubleValue(val);
296 }
297
298
299 \f
300 ////////////////////////////////////////////////////////////////////////
301 // Implementation of FGToggleAction.
302 ////////////////////////////////////////////////////////////////////////
303
304 FGToggleAction::FGToggleAction (int button, int x, int y, int w, int h,
305                                 SGValue * value)
306   : FGPanelAction(button, x, y, w, h), _value(value)
307 {
308 }
309
310 FGToggleAction::~FGToggleAction ()
311 {
312 }
313
314 void
315 FGToggleAction::doAction ()
316 {
317   _value->setBoolValue(!(_value->getBoolValue()));
318 }
319
320
321 \f
322 ////////////////////////////////////////////////////////////////////////
323 // Implementation of FGPanelTransformation.
324 ////////////////////////////////////////////////////////////////////////
325
326 FGPanelTransformation::FGPanelTransformation ()
327 {
328 }
329
330 FGPanelTransformation::FGPanelTransformation (Type _type,
331                                               const SGValue * _value,
332                                               float _min, float _max,
333                                               float _factor, float _offset)
334   : type(_type), value(_value), min(_min), max(_max),
335     factor(_factor), offset(_offset)
336 {
337 }
338
339 FGPanelTransformation::~FGPanelTransformation ()
340 {
341 }
342
343
344 \f
345 ////////////////////////////////////////////////////////////////////////
346 // Implementation of FGPanelInstrument.
347 ////////////////////////////////////////////////////////////////////////
348
349
350 FGPanelInstrument::FGPanelInstrument ()
351 {
352   setPosition(0, 0);
353   setSize(0, 0);
354 }
355
356 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
357 {
358   setPosition(x, y);
359   setSize(w, h);
360 }
361
362 FGPanelInstrument::~FGPanelInstrument ()
363 {
364   for (action_list_type::iterator it = _actions.begin();
365        it != _actions.end();
366        it++) {
367     delete *it;
368     *it = 0;
369   }
370 }
371
372 void
373 FGPanelInstrument::setPosition (int x, int y)
374 {
375   _x = x;
376   _y = y;
377 }
378
379 void
380 FGPanelInstrument::setSize (int w, int h)
381 {
382   _w = w;
383   _h = h;
384 }
385
386 int
387 FGPanelInstrument::getXPos () const
388 {
389   return _x;
390 }
391
392 int
393 FGPanelInstrument::getYPos () const
394 {
395   return _y;
396 }
397
398 int
399 FGPanelInstrument::getWidth () const
400 {
401   return _w;
402 }
403
404 int
405 FGPanelInstrument::getHeight () const
406 {
407   return _h;
408 }
409
410 void
411 FGPanelInstrument::addAction (FGPanelAction * action)
412 {
413   _actions.push_back(action);
414 }
415
416                                 // Coordinates relative to centre.
417 bool
418 FGPanelInstrument::doMouseAction (int button, int x, int y)
419 {
420   action_list_type::iterator it = _actions.begin();
421   action_list_type::iterator last = _actions.end();
422   for ( ; it != last; it++) {
423     if ((*it)->inArea(button, x, y)) {
424       (*it)->doAction();
425       return true;
426     }
427   }
428   return false;
429 }
430
431
432 \f
433 ////////////////////////////////////////////////////////////////////////
434 // Implementation of FGLayeredInstrument.
435 ////////////////////////////////////////////////////////////////////////
436
437 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
438   : FGPanelInstrument(x, y, w, h)
439 {
440 }
441
442 FGLayeredInstrument::~FGLayeredInstrument ()
443 {
444   for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
445     delete *it;
446     *it = 0;
447   }
448 }
449
450 void
451 FGLayeredInstrument::draw ()
452 {
453   for (int i = 0; i < _layers.size(); i++) {
454     glPushMatrix();
455     glTranslatef(0.0, 0.0, (i / 100.0) + 0.1);
456     _layers[i]->draw();
457     glPopMatrix();
458   }
459 }
460
461 int
462 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
463 {
464   int n = _layers.size();
465   if (layer->getWidth() == -1) {
466     layer->setWidth(getWidth());
467   }
468   if (layer->getHeight() == -1) {
469     layer->setHeight(getHeight());
470   }
471   _layers.push_back(layer);
472   return n;
473 }
474
475 int
476 FGLayeredInstrument::addLayer (CroppedTexture &texture,
477                                int w = -1, int h = -1)
478 {
479   return addLayer(new FGTexturedLayer(texture, w, h));
480 }
481
482 void
483 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
484 {
485   int layer = _layers.size() - 1;
486   _layers[layer]->addTransformation(transformation);
487 }
488
489
490 \f
491 ////////////////////////////////////////////////////////////////////////
492 // Implementation of FGInstrumentLayer.
493 ////////////////////////////////////////////////////////////////////////
494
495 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
496   : _w(w),
497     _h(h)
498 {
499 }
500
501 FGInstrumentLayer::~FGInstrumentLayer ()
502 {
503   for (transformation_list::iterator it = _transformations.begin();
504        it != _transformations.end();
505        it++) {
506     delete *it;
507     *it = 0;
508   }
509 }
510
511 void
512 FGInstrumentLayer::transform () const
513 {
514   transformation_list::const_iterator it = _transformations.begin();
515   transformation_list::const_iterator last = _transformations.end();
516   while (it != last) {
517     FGPanelTransformation *t = *it;
518     float val = (t->value == 0 ? 0.0 : t->value->getFloatValue());
519     if (val < t->min) {
520       val = t->min;
521     } else if (val > t->max) {
522       val = t->max;
523     }
524     val = val * t->factor + t->offset;
525
526     switch (t->type) {
527     case FGPanelTransformation::XSHIFT:
528       glTranslatef(val, 0.0, 0.0);
529       break;
530     case FGPanelTransformation::YSHIFT:
531       glTranslatef(0.0, val, 0.0);
532       break;
533     case FGPanelTransformation::ROTATION:
534       glRotatef(-val, 0.0, 0.0, 1.0);
535       break;
536     }
537     it++;
538   }
539 }
540
541 void
542 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
543 {
544   _transformations.push_back(transformation);
545 }
546
547
548 \f
549 ////////////////////////////////////////////////////////////////////////
550 // Implementation of FGTexturedLayer.
551 ////////////////////////////////////////////////////////////////////////
552
553
554 FGTexturedLayer::FGTexturedLayer (CroppedTexture &texture, int w, int h)
555   : FGInstrumentLayer(w, h)
556 {
557   setTexture(texture);
558 }
559
560
561 FGTexturedLayer::~FGTexturedLayer ()
562 {
563 }
564
565
566 void
567 FGTexturedLayer::draw ()
568 {
569   int w2 = _w / 2;
570   int h2 = _h / 2;
571
572   transform();
573   glBindTexture(GL_TEXTURE_2D, _texture.texture->getHandle());
574   glBegin(GL_POLYGON);
575   if ( cur_light_params.sun_angle * RAD_TO_DEG < 95.0 ) {
576       glColor4fv( cur_light_params.scene_diffuse );
577   } else {
578       glColor4f(0.7, 0.2, 0.2, 1.0);
579   }
580   glTexCoord2f(_texture.minX, _texture.minY); glVertex2f(-w2, -h2);
581   glTexCoord2f(_texture.maxX, _texture.minY); glVertex2f(w2, -h2);
582   glTexCoord2f(_texture.maxX, _texture.maxY); glVertex2f(w2, h2);
583   glTexCoord2f(_texture.minX, _texture.maxY); glVertex2f(-w2, h2);
584   glEnd();
585 }
586
587
588 \f
589 ////////////////////////////////////////////////////////////////////////
590 // Implementation of FGTextLayer.
591 ////////////////////////////////////////////////////////////////////////
592
593 FGTextLayer::FGTextLayer (int w, int h, Chunk * chunk1, Chunk * chunk2,
594                           Chunk * chunk3)
595   : FGInstrumentLayer(w, h)
596 {
597   _color[0] = _color[1] = _color[2] = 0.0;
598   _color[3] = 1.0;
599   if (chunk1)
600     addChunk(chunk1);
601   if (chunk2)
602     addChunk(chunk2);
603   if (chunk3)
604     addChunk(chunk3);
605 }
606
607 FGTextLayer::~FGTextLayer ()
608 {
609   chunk_list::iterator it = _chunks.begin();
610   chunk_list::iterator last = _chunks.end();
611   for ( ; it != last; it++) {
612     delete *it;
613   }
614 }
615
616 void
617 FGTextLayer::draw ()
618 {
619   glPushMatrix();
620   glColor4fv(_color);
621   transform();
622   _renderer.setFont(guiFntHandle);
623   _renderer.setPointSize(14);
624   _renderer.begin();
625   _renderer.start3f(0, 0, 0);
626
627                                 // Render each of the chunks.
628   chunk_list::const_iterator it = _chunks.begin();
629   chunk_list::const_iterator last = _chunks.end();
630   for ( ; it != last; it++) {
631     _renderer.puts((*it)->getValue());
632   }
633
634   _renderer.end();
635   glColor4f(1.0, 1.0, 1.0, 1.0);        // FIXME
636   glPopMatrix();
637 }
638
639 void
640 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
641 {
642   _chunks.push_back(chunk);
643 }
644
645 void
646 FGTextLayer::setColor (float r, float g, float b)
647 {
648   _color[0] = r;
649   _color[1] = g;
650   _color[2] = b;
651   _color[3] = 1.0;
652 }
653
654 void
655 FGTextLayer::setPointSize (const float size)
656 {
657   _renderer.setPointSize(size);
658 }
659
660 void
661 FGTextLayer::setFont(fntFont * font)
662 {
663   _renderer.setFont(font);
664 }
665
666
667 \f
668 ////////////////////////////////////////////////////////////////////////
669 // Implementation of FGTextLayer::Chunk.
670 ////////////////////////////////////////////////////////////////////////
671
672 FGTextLayer::Chunk::Chunk (char * text, char * fmt = "%s")
673   : _type(FGTextLayer::TEXT), _fmt(fmt)
674 {
675   _value._text = text;
676 }
677
678 FGTextLayer::Chunk::Chunk (ChunkType type, const SGValue * value,
679                            char * fmt = 0, float mult = 1.0)
680   : _type(type), _fmt(fmt), _mult(mult)
681 {
682   if (_fmt == 0) {
683     if (type == TEXT_VALUE)
684       _fmt = "%s";
685     else
686       _fmt = "%.2f";
687   }
688   _value._value = value;
689 }
690
691 char *
692 FGTextLayer::Chunk::getValue () const
693 {
694   switch (_type) {
695   case TEXT:
696     sprintf(_buf, _fmt, _value._text);
697     return _buf;
698   case TEXT_VALUE:
699     sprintf(_buf, _fmt, _value._value->getStringValue().c_str());
700     break;
701   case DOUBLE_VALUE:
702     sprintf(_buf, _fmt, _value._value->getFloatValue() * _mult);
703     break;
704   }
705   return _buf;
706 }
707
708
709 \f
710 ////////////////////////////////////////////////////////////////////////
711 // Implementation of FGSwitchLayer.
712 ////////////////////////////////////////////////////////////////////////
713
714 FGSwitchLayer::FGSwitchLayer (int w, int h, const SGValue * value,
715                               FGInstrumentLayer * layer1,
716                               FGInstrumentLayer * layer2)
717   : FGInstrumentLayer(w, h), _value(value), _layer1(layer1), _layer2(layer2)
718 {
719 }
720
721 FGSwitchLayer::~FGSwitchLayer ()
722 {
723   delete _layer1;
724   delete _layer2;
725 }
726
727 void
728 FGSwitchLayer::draw ()
729 {
730   transform();
731   if (_value->getBoolValue()) {
732     _layer1->draw();
733   } else {
734     _layer2->draw();
735   }
736 }
737
738 \f
739 // end of panel.cxx