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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 //
19 // $Id$
20
21 //JVK
22 // On 2D panels all instruments include light sources were in night displayed
23 // with a red mask (instrument light). It is not correct for light sources
24 // (bulbs). There is added new layer property "emissive" (boolean) (only for
25 // textured layers).
26 // If a layer has to shine set it in the "instrument_def_file.xml" inside the
27 // <layer> tag by adding <emissive>true</emissive> tag. When omitted the default
28 // value is for backward compatibility set to false.
29
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33
34 #include "panel.hxx"
35
36 #include <stdio.h> // sprintf
37 #include <string.h>
38 #include <iostream>
39 #include <cassert>
40
41 #include <osg/CullFace>
42 #include <osg/Depth>
43 #include <osg/Material>
44 #include <osg/Matrixf>
45 #include <osg/TexEnv>
46 #include <osg/PolygonOffset>
47
48 #include <simgear/compiler.h>
49
50 #include "fnt.h"
51
52 #include <simgear/debug/logstream.hxx>
53 #include <simgear/misc/sg_path.hxx>
54 #include <simgear/misc/strutils.hxx>
55 #include <simgear/scene/model/model.hxx>
56 #include <osg/GLU>
57
58 #include <Main/globals.hxx>
59 #include <Main/fg_props.hxx>
60 #include <Viewer/viewmgr.hxx>
61 #include <Viewer/view.hxx>
62 #include <Time/light.hxx>
63 #include <GUI/FGFontCache.hxx>
64 #include <Instrumentation/dclgps.hxx>
65
66 #define WIN_X 0
67 #define WIN_Y 0
68 #define WIN_W 1024
69 #define WIN_H 768
70
71 // The number of polygon-offset "units" to place between layers. In
72 // principle, one is supposed to be enough. In practice, I find that
73 // my hardware/driver requires many more.
74 #define POFF_UNITS 8
75
76 const double MOUSE_ACTION_REPEAT_DELAY = 0.5; // 500msec initial delay
77 const double MOUSE_ACTION_REPEAT_INTERVAL = 0.1; // 10Hz repeat rate
78
79 using std::map;
80
81 ////////////////////////////////////////////////////////////////////////
82 // Local functions.
83 ////////////////////////////////////////////////////////////////////////
84
85
86 /**
87 * Calculate the aspect adjustment for the panel.
88 */
89 static float
get_aspect_adjust(int xsize,int ysize)90 get_aspect_adjust (int xsize, int ysize)
91 {
92 float ideal_aspect = float(WIN_W) / float(WIN_H);
93 float real_aspect = float(xsize) / float(ysize);
94 return (real_aspect / ideal_aspect);
95 }
96
97 ////////////////////////////////////////////////////////////////////////
98 // Implementation of FGTextureManager.
99 ////////////////////////////////////////////////////////////////////////
100
101 map<std::string,osg::ref_ptr<osg::Texture2D> > FGTextureManager::_textureMap;
102
103 osg::Texture2D*
createTexture(const std::string & relativePath,bool staticTexture)104 FGTextureManager::createTexture (const std::string &relativePath, bool staticTexture)
105 {
106 osg::Texture2D* texture = _textureMap[relativePath].get();
107 if (texture == 0) {
108 SGPath tpath = globals->resolve_aircraft_path(relativePath);
109 texture = SGLoadTexture2D(staticTexture, tpath);
110
111 _textureMap[relativePath] = texture;
112 if (!_textureMap[relativePath].valid())
113 SG_LOG( SG_COCKPIT, SG_ALERT, "Texture *still* doesn't exist" );
114 SG_LOG( SG_COCKPIT, SG_DEBUG, "Created texture " << relativePath );
115 }
116
117 return texture;
118 }
119
120
addTexture(const std::string & relativePath,osg::Texture2D * texture)121 void FGTextureManager::addTexture(const std::string &relativePath,
122 osg::Texture2D* texture)
123 {
124 _textureMap[relativePath] = texture;
125 }
126
127 ////////////////////////////////////////////////////////////////////////
128 // Implementation of FGCropped Texture.
129 ////////////////////////////////////////////////////////////////////////
130
131
FGCroppedTexture()132 FGCroppedTexture::FGCroppedTexture ()
133 : _path(""), _texture(0),
134 _minX(0.0), _minY(0.0), _maxX(1.0), _maxY(1.0)
135 {
136 }
137
138
FGCroppedTexture(const std::string & path,float minX,float minY,float maxX,float maxY)139 FGCroppedTexture::FGCroppedTexture (const std::string &path,
140 float minX, float minY,
141 float maxX, float maxY)
142 : _path(path), _texture(0),
143 _minX(minX), _minY(minY), _maxX(maxX), _maxY(maxY)
144 {
145 }
146
147
~FGCroppedTexture()148 FGCroppedTexture::~FGCroppedTexture ()
149 {
150 }
151
152
153 osg::StateSet*
getTexture()154 FGCroppedTexture::getTexture ()
155 {
156 if (_texture == 0) {
157 _texture = new osg::StateSet;
158 _texture->setTextureAttribute(0, FGTextureManager::createTexture(_path));
159 _texture->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
160 _texture->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
161 }
162 return _texture.get();
163 }
164
165
166
167 ////////////////////////////////////////////////////////////////////////
168 // Implementation of FGPanel.
169 ////////////////////////////////////////////////////////////////////////
170
171 static fntRenderer text_renderer;
172 static sgVec4 panel_color;
173 static sgVec4 emissive_panel_color = {1,1,1,1};
174
175 /**
176 * Constructor.
177 */
FGPanel()178 FGPanel::FGPanel ()
179 : _mouseDown(false),
180 _mouseInstrument(0),
181 _width(WIN_W), _height(int(WIN_H * 0.5768 + 1)),
182 _x_offset(fgGetNode("/sim/panel/x-offset", true)),
183 _y_offset(fgGetNode("/sim/panel/y-offset", true)),
184 _jitter(fgGetNode("/sim/panel/jitter", true)),
185 _flipx(fgGetNode("/sim/panel/flip-x", true)),
186 _xsize_node(fgGetNode("/sim/startup/xsize", true)),
187 _ysize_node(fgGetNode("/sim/startup/ysize", true)),
188 _enable_depth_test(false),
189 _autohide(true),
190 _drawPanelHotspots("/sim/panel-hotspots")
191 {
192 }
193
194
195 /**
196 * Destructor.
197 */
~FGPanel()198 FGPanel::~FGPanel ()
199 {
200 for (instrument_list_type::iterator it = _instruments.begin();
201 it != _instruments.end();
202 it++) {
203 delete *it;
204 *it = 0;
205 }
206 }
207
208
209 /**
210 * Add an instrument to the panel.
211 */
212 void
addInstrument(FGPanelInstrument * instrument)213 FGPanel::addInstrument (FGPanelInstrument * instrument)
214 {
215 _instruments.push_back(instrument);
216 }
217
218 double
getAspectScale() const219 FGPanel::getAspectScale() const
220 {
221 // set corner-coordinates correctly
222
223 int xsize = _xsize_node->getIntValue();
224 int ysize = _ysize_node->getIntValue();
225 float aspect_adjust = get_aspect_adjust(xsize, ysize);
226
227 if (aspect_adjust < 1.0)
228 return ysize / (double) WIN_H;
229 else
230 return xsize /(double) WIN_W;
231 }
232
233 /**
234 * Handle repeatable mouse events. Called from update() and from
235 * fgUpdate3DPanels(). This functionality needs to move into the
236 * input subsystem. Counting a tick every two frames is clumsy...
237 */
updateMouseDelay(double dt)238 void FGPanel::updateMouseDelay(double dt)
239 {
240 if (!_mouseDown) {
241 return;
242 }
243
244 _mouseActionRepeat -= dt;
245 while (_mouseActionRepeat < 0.0) {
246 _mouseActionRepeat += MOUSE_ACTION_REPEAT_INTERVAL;
247 _mouseInstrument->doMouseAction(_mouseButton, 0, _mouseX, _mouseY);
248
249 }
250 }
251
252 void
draw(osg::State & state)253 FGPanel::draw(osg::State& state)
254 {
255
256 // In 3D mode, it's possible that we are being drawn exactly on top
257 // of an existing polygon. Use an offset to prevent z-fighting. In
258 // 2D mode, this is a no-op.
259 static osg::ref_ptr<osg::StateSet> panelStateSet;
260 if (!panelStateSet.valid()) {
261 panelStateSet = new osg::StateSet;
262 panelStateSet->setAttributeAndModes(new osg::PolygonOffset(-1, -POFF_UNITS));
263 panelStateSet->setTextureAttribute(0, new osg::TexEnv);
264
265 // Draw the background
266 panelStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
267 panelStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
268 panelStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
269 panelStateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
270
271 osg::Material* material = new osg::Material;
272 material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
273 material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
274 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
275 material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
276 material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1));
277 panelStateSet->setAttribute(material);
278
279 panelStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
280 panelStateSet->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK));
281 panelStateSet->setAttributeAndModes(new osg::Depth(osg::Depth::LEQUAL));
282 }
283 if ( _enable_depth_test )
284 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
285 else
286 panelStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
287 state.pushStateSet(panelStateSet.get());
288 state.apply();
289 state.setActiveTextureUnit(0);
290 state.setClientActiveTextureUnit(0);
291
292 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
293 sgCopyVec4( panel_color, l->scene_diffuse().data());
294 if ( fgGetDouble("/systems/electrical/outputs/instrument-lights") > 1.0 ) {
295 if ( panel_color[0] < 0.7 ) panel_color[0] = 0.7;
296 if ( panel_color[1] < 0.2 ) panel_color[1] = 0.2;
297 if ( panel_color[2] < 0.2 ) panel_color[2] = 0.2;
298 }
299 glColor4fv( panel_color );
300 if (_bg != 0) {
301 state.pushStateSet(_bg.get());
302 state.apply();
303 state.setActiveTextureUnit(0);
304 state.setClientActiveTextureUnit(0);
305
306 glBegin(GL_POLYGON);
307 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X, WIN_Y);
308 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + _width, WIN_Y);
309 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + _width, WIN_Y + _height);
310 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X, WIN_Y + _height);
311 glEnd();
312 state.popStateSet();
313 state.apply();
314 state.setActiveTextureUnit(0);
315 state.setClientActiveTextureUnit(0);
316
317 } else {
318 for (int i = 0; i < 4; i ++) {
319 // top row of textures...(1,3,5,7)
320 state.pushStateSet(_mbg[i*2].get());
321 state.apply();
322 state.setActiveTextureUnit(0);
323 state.setClientActiveTextureUnit(0);
324
325 glBegin(GL_POLYGON);
326 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
327 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
328 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + _height);
329 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + _height);
330 glEnd();
331 state.popStateSet();
332 state.apply();
333 state.setActiveTextureUnit(0);
334 state.setClientActiveTextureUnit(0);
335
336 // bottom row of textures...(2,4,6,8)
337 state.pushStateSet(_mbg[i*2+1].get());
338 state.apply();
339 state.setActiveTextureUnit(0);
340 state.setClientActiveTextureUnit(0);
341
342 glBegin(GL_POLYGON);
343 glTexCoord2f(0.0, 0.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y);
344 glTexCoord2f(1.0, 0.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y);
345 glTexCoord2f(1.0, 1.0); glVertex2f(WIN_X + (_width/4) * (i+1), WIN_Y + (_height/2));
346 glTexCoord2f(0.0, 1.0); glVertex2f(WIN_X + (_width/4) * i, WIN_Y + (_height/2));
347 glEnd();
348 state.popStateSet();
349 state.apply();
350 state.setActiveTextureUnit(0);
351 state.setClientActiveTextureUnit(0);
352
353 }
354 }
355
356 // Draw the instruments.
357 // Syd Adams: added instrument clipping
358 instrument_list_type::const_iterator current = _instruments.begin();
359 instrument_list_type::const_iterator end = _instruments.end();
360
361 GLdouble blx[4]={1.0,0.0,0.0,0.0};
362 GLdouble bly[4]={0.0,1.0,0.0,0.0};
363 GLdouble urx[4]={-1.0,0.0,0.0,0.0};
364 GLdouble ury[4]={0.0,-1.0,0.0,0.0};
365
366 for ( ; current != end; current++) {
367 FGPanelInstrument * instr = *current;
368 glPushMatrix();
369 glTranslated(instr->getXPos(), instr->getYPos(), 0);
370
371 int ix= instr->getWidth();
372 int iy= instr->getHeight();
373 glPushMatrix();
374 glTranslated(-ix/2,-iy/2,0);
375 glClipPlane(GL_CLIP_PLANE0,blx);
376 glClipPlane(GL_CLIP_PLANE1,bly);
377 glEnable(GL_CLIP_PLANE0);
378 glEnable(GL_CLIP_PLANE1);
379
380 glTranslated(ix,iy,0);
381 glClipPlane(GL_CLIP_PLANE2,urx);
382 glClipPlane(GL_CLIP_PLANE3,ury);
383 glEnable(GL_CLIP_PLANE2);
384 glEnable(GL_CLIP_PLANE3);
385 glPopMatrix();
386 instr->draw(state);
387
388 glPopMatrix();
389 }
390
391 glDisable(GL_CLIP_PLANE0);
392 glDisable(GL_CLIP_PLANE1);
393 glDisable(GL_CLIP_PLANE2);
394 glDisable(GL_CLIP_PLANE3);
395
396 state.popStateSet();
397 state.apply();
398 state.setActiveTextureUnit(0);
399 state.setClientActiveTextureUnit(0);
400
401
402 // Draw yellow "hotspots" if directed to. This is a panel authoring
403 // feature; not intended to be high performance or to look good.
404 if ( _drawPanelHotspots ) {
405 static osg::ref_ptr<osg::StateSet> hotspotStateSet;
406 if (!hotspotStateSet.valid()) {
407 hotspotStateSet = new osg::StateSet;
408 hotspotStateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::OFF);
409 hotspotStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
410 }
411
412 state.pushStateSet(hotspotStateSet.get());
413 state.apply();
414 state.setActiveTextureUnit(0);
415 state.setClientActiveTextureUnit(0);
416
417
418 glPushAttrib(GL_ENABLE_BIT);
419 glDisable(GL_COLOR_MATERIAL);
420 glColor3f(1, 1, 0);
421
422 for ( unsigned int i = 0; i < _instruments.size(); i++ )
423 _instruments[i]->drawHotspots(state);
424
425 // disable drawing of panel extents for the 2.8 release, since it
426 // confused use of hot-spot drawing in tutorials.
427 #ifdef DRAW_PANEL_EXTENTS
428 glColor3f(0, 1, 1);
429
430 int x0, y0, x1, y1;
431 getLogicalExtent(x0, y0, x1, y1);
432
433 glBegin(GL_LINE_LOOP);
434 glVertex2f(x0, y0);
435 glVertex2f(x1, y0);
436 glVertex2f(x1, y1);
437 glVertex2f(x0, y1);
438 glEnd();
439 #endif
440
441 glPopAttrib();
442
443 state.popStateSet();
444 state.apply();
445 state.setActiveTextureUnit(0);
446 state.setClientActiveTextureUnit(0);
447
448 }
449 }
450
451 /**
452 * Set the panel's background texture.
453 */
454 void
setBackground(osg::Texture2D * texture)455 FGPanel::setBackground (osg::Texture2D* texture)
456 {
457 osg::StateSet* stateSet = new osg::StateSet;
458 stateSet->setTextureAttribute(0, texture);
459 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
460 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
461 _bg = stateSet;
462 }
463
464 /**
465 * Set the panel's multiple background textures.
466 */
467 void
setMultiBackground(osg::Texture2D * texture,int idx)468 FGPanel::setMultiBackground (osg::Texture2D* texture, int idx)
469 {
470 _bg = 0;
471
472 osg::StateSet* stateSet = new osg::StateSet;
473 stateSet->setTextureAttribute(0, texture);
474 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
475 stateSet->setTextureAttribute(0, new osg::TexEnv(osg::TexEnv::MODULATE));
476 _mbg[idx] = stateSet;
477 }
478
479 /**
480 * Set the panel's x-offset.
481 */
482 void
setXOffset(int offset)483 FGPanel::setXOffset (int offset)
484 {
485 if (offset <= 0 && offset >= -_width + WIN_W)
486 _x_offset->setIntValue( offset );
487 }
488
489
490 /**
491 * Set the panel's y-offset.
492 */
493 void
setYOffset(int offset)494 FGPanel::setYOffset (int offset)
495 {
496 if (offset <= 0 && offset >= -_height)
497 _y_offset->setIntValue( offset );
498 }
499
500 /**
501 * Handle a mouse action in panel-local (not screen) coordinates.
502 * Used by the 3D panel code in Model/panelnode.cxx, in situations
503 * where the panel doesn't control its own screen location.
504 */
505 bool
doLocalMouseAction(int button,int updown,int x,int y)506 FGPanel::doLocalMouseAction(int button, int updown, int x, int y)
507 {
508 // Note a released button and return
509 if (updown == 1) {
510 if (_mouseInstrument != 0)
511 _mouseInstrument->doMouseAction(_mouseButton, 1, _mouseX, _mouseY);
512 _mouseDown = false;
513 _mouseInstrument = 0;
514 return false;
515 }
516
517 // Search for a matching instrument.
518 for (int i = 0; i < (int)_instruments.size(); i++) {
519 FGPanelInstrument *inst = _instruments[i];
520 int ix = inst->getXPos();
521 int iy = inst->getYPos();
522 int iw = inst->getWidth() / 2;
523 int ih = inst->getHeight() / 2;
524 if (x >= ix - iw && x < ix + iw && y >= iy - ih && y < iy + ih) {
525 _mouseDown = true;
526 _mouseActionRepeat = MOUSE_ACTION_REPEAT_DELAY;
527 _mouseInstrument = inst;
528 _mouseButton = button;
529 _mouseX = x - ix;
530 _mouseY = y - iy;
531 // Always do the action once.
532 return _mouseInstrument->doMouseAction(_mouseButton, 0,
533 _mouseX, _mouseY);
534 }
535 }
536 return false;
537 }
538
539 /**
540 * Perform a mouse action.
541 */
542 bool
doMouseAction(int button,int updown,int x,int y)543 FGPanel::doMouseAction (int button, int updown, int x, int y)
544 {
545 // FIXME: this same code appears in update()
546 int xsize = _xsize_node->getIntValue();
547 int ysize = _ysize_node->getIntValue();
548 float aspect_adjust = get_aspect_adjust(xsize, ysize);
549
550 // Scale for the real window size.
551 if (aspect_adjust < 1.0) {
552 x = int(((float)x / xsize) * WIN_W * aspect_adjust);
553 y = int(WIN_H - ((float(y) / ysize) * WIN_H));
554 } else {
555 x = int(((float)x / xsize) * WIN_W);
556 y = int((WIN_H - ((float(y) / ysize) * WIN_H)) / aspect_adjust);
557 }
558
559 // Adjust for offsets.
560 x -= _x_offset->getIntValue();
561 y -= _y_offset->getIntValue();
562
563 // Having fixed up the coordinates, fall through to the local
564 // coordinate handler.
565 return doLocalMouseAction(button, updown, x, y);
566 }
567
setDepthTest(bool enable)568 void FGPanel::setDepthTest (bool enable) {
569 _enable_depth_test = enable;
570 }
571
572 class IntRect
573 {
574
575 public:
IntRect()576 IntRect() :
577 x0(std::numeric_limits<int>::max()),
578 y0(std::numeric_limits<int>::max()),
579 x1(std::numeric_limits<int>::min()),
580 y1(std::numeric_limits<int>::min())
581 { }
582
IntRect(int x,int y,int w,int h)583 IntRect(int x, int y, int w, int h) :
584 x0(x), y0(y), x1(x + w), y1( y + h)
585 {
586 if (x1 < x0) {
587 std::swap(x0, x1);
588 }
589
590 if (y1 < y0) {
591 std::swap(y0, y1);
592 }
593
594 assert(x0 <= x1);
595 assert(y0 <= y1);
596 }
597
extend(const IntRect & r)598 void extend(const IntRect& r)
599 {
600 x0 = std::min(x0, r.x0);
601 y0 = std::min(y0, r.y0);
602 x1 = std::max(x1, r.x1);
603 y1 = std::max(y1, r.y1);
604 }
605
606 int x0, y0, x1, y1;
607 };
608
getLogicalExtent(int & x0,int & y0,int & x1,int & y1)609 void FGPanel::getLogicalExtent(int &x0, int& y0, int& x1, int &y1)
610 {
611 IntRect result;
612 for (auto inst : _instruments) {
613 inst->extendRect(result);
614 }
615
616 x0 = result.x0;
617 y0 = result.y0;
618 x1 = result.x1;
619 y1 = result.y1;
620 }
621
622 ////////////////////////////////////////////////////////////////////////.
623 // Implementation of FGPanelAction.
624 ////////////////////////////////////////////////////////////////////////
625
FGPanelAction()626 FGPanelAction::FGPanelAction ()
627 {
628 }
629
FGPanelAction(int button,int x,int y,int w,int h,bool repeatable)630 FGPanelAction::FGPanelAction (int button, int x, int y, int w, int h,
631 bool repeatable)
632 : _button(button), _x(x), _y(y), _w(w), _h(h), _repeatable(repeatable)
633 {
634 }
635
~FGPanelAction()636 FGPanelAction::~FGPanelAction ()
637 {
638 for (unsigned int i = 0; i < 2; i++) {
639 for (unsigned int j = 0; j < _bindings[i].size(); j++)
640 delete _bindings[i][j];
641 }
642 }
643
644 void
addBinding(SGBinding * binding,int updown)645 FGPanelAction::addBinding (SGBinding * binding, int updown)
646 {
647 _bindings[updown].push_back(binding);
648 }
649
650 bool
doAction(int updown)651 FGPanelAction::doAction (int updown)
652 {
653 if (test()) {
654 if ((updown != _last_state) || (updown == 0 && _repeatable)) {
655 int nBindings = _bindings[updown].size();
656 for (int i = 0; i < nBindings; i++)
657 _bindings[updown][i]->fire();
658 }
659 _last_state = updown;
660 return true;
661 } else {
662 return false;
663 }
664 }
665
666
667
668 ////////////////////////////////////////////////////////////////////////
669 // Implementation of FGPanelTransformation.
670 ////////////////////////////////////////////////////////////////////////
671
FGPanelTransformation()672 FGPanelTransformation::FGPanelTransformation ()
673 : table(0)
674 {
675 }
676
~FGPanelTransformation()677 FGPanelTransformation::~FGPanelTransformation ()
678 {
679 delete table;
680 }
681
682
683
684 ////////////////////////////////////////////////////////////////////////
685 // Implementation of FGPanelInstrument.
686 ////////////////////////////////////////////////////////////////////////
687
688
FGPanelInstrument()689 FGPanelInstrument::FGPanelInstrument ()
690 {
691 setPosition(0, 0);
692 setSize(0, 0);
693 }
694
FGPanelInstrument(int x,int y,int w,int h)695 FGPanelInstrument::FGPanelInstrument (int x, int y, int w, int h)
696 {
697 setPosition(x, y);
698 setSize(w, h);
699 }
700
~FGPanelInstrument()701 FGPanelInstrument::~FGPanelInstrument ()
702 {
703 for (action_list_type::iterator it = _actions.begin();
704 it != _actions.end();
705 it++) {
706 delete *it;
707 *it = 0;
708 }
709 }
710
711 void
drawHotspots(osg::State & state)712 FGPanelInstrument::drawHotspots(osg::State& state)
713 {
714 for ( unsigned int i = 0; i < _actions.size(); i++ ) {
715 FGPanelAction* a = _actions[i];
716 float x1 = getXPos() + a->getX();
717 float x2 = x1 + a->getWidth();
718 float y1 = getYPos() + a->getY();
719 float y2 = y1 + a->getHeight();
720
721 glBegin(GL_LINE_LOOP);
722 glVertex2f(x1, y1);
723 glVertex2f(x1, y2);
724 glVertex2f(x2, y2);
725 glVertex2f(x2, y1);
726 glEnd();
727 }
728 }
729
730 void
setPosition(int x,int y)731 FGPanelInstrument::setPosition (int x, int y)
732 {
733 _x = x;
734 _y = y;
735 }
736
737 void
setSize(int w,int h)738 FGPanelInstrument::setSize (int w, int h)
739 {
740 _w = w;
741 _h = h;
742 }
743
744 int
getXPos() const745 FGPanelInstrument::getXPos () const
746 {
747 return _x;
748 }
749
750 int
getYPos() const751 FGPanelInstrument::getYPos () const
752 {
753 return _y;
754 }
755
756 int
getWidth() const757 FGPanelInstrument::getWidth () const
758 {
759 return _w;
760 }
761
762 int
getHeight() const763 FGPanelInstrument::getHeight () const
764 {
765 return _h;
766 }
767
768 void
extendRect(IntRect & r) const769 FGPanelInstrument::extendRect(IntRect& r) const
770 {
771 IntRect instRect(_x, _y, _w, _h);
772 r.extend(instRect);
773
774 for (auto act : _actions) {
775 r.extend(IntRect(getXPos() + act->getX(),
776 getYPos() + act->getY(),
777 act->getWidth(),
778 act->getHeight()
779 ));
780 }
781 }
782
783 void
addAction(FGPanelAction * action)784 FGPanelInstrument::addAction (FGPanelAction * action)
785 {
786 _actions.push_back(action);
787 }
788
789 // Coordinates relative to centre.
790 bool
doMouseAction(int button,int updown,int x,int y)791 FGPanelInstrument::doMouseAction (int button, int updown, int x, int y)
792 {
793 if (test()) {
794 action_list_type::iterator it = _actions.begin();
795 action_list_type::iterator last = _actions.end();
796 for ( ; it != last; it++) {
797 if ((*it)->inArea(button, x, y) &&
798 (*it)->doAction(updown))
799 return true;
800 }
801 }
802 return false;
803 }
804
805
806
807 ////////////////////////////////////////////////////////////////////////
808 // Implementation of FGLayeredInstrument.
809 ////////////////////////////////////////////////////////////////////////
810
FGLayeredInstrument(int x,int y,int w,int h)811 FGLayeredInstrument::FGLayeredInstrument (int x, int y, int w, int h)
812 : FGPanelInstrument(x, y, w, h)
813 {
814 }
815
~FGLayeredInstrument()816 FGLayeredInstrument::~FGLayeredInstrument ()
817 {
818 for (layer_list::iterator it = _layers.begin(); it != _layers.end(); it++) {
819 delete *it;
820 *it = 0;
821 }
822 }
823
824 void
draw(osg::State & state)825 FGLayeredInstrument::draw (osg::State& state)
826 {
827 if (!test())
828 return;
829
830 for (int i = 0; i < (int)_layers.size(); i++) {
831 glPushMatrix();
832 _layers[i]->draw(state);
833 glPopMatrix();
834 }
835 }
836
837 int
addLayer(FGInstrumentLayer * layer)838 FGLayeredInstrument::addLayer (FGInstrumentLayer *layer)
839 {
840 int n = _layers.size();
841 if (layer->getWidth() == -1) {
842 layer->setWidth(getWidth());
843 }
844 if (layer->getHeight() == -1) {
845 layer->setHeight(getHeight());
846 }
847 _layers.push_back(layer);
848 return n;
849 }
850
851 int
addLayer(const FGCroppedTexture & texture,int w,int h)852 FGLayeredInstrument::addLayer (const FGCroppedTexture &texture,
853 int w, int h)
854 {
855 return addLayer(new FGTexturedLayer(texture, w, h));
856 }
857
858 void
addTransformation(FGPanelTransformation * transformation)859 FGLayeredInstrument::addTransformation (FGPanelTransformation * transformation)
860 {
861 int layer = _layers.size() - 1;
862 _layers[layer]->addTransformation(transformation);
863 }
864
865
866
867 ////////////////////////////////////////////////////////////////////////
868 // Implementation of FGSpecialInstrument.
869 ////////////////////////////////////////////////////////////////////////
870
FGSpecialInstrument(DCLGPS * sb)871 FGSpecialInstrument::FGSpecialInstrument (DCLGPS* sb)
872 : FGPanelInstrument()
873 {
874 complex = sb;
875 }
876
~FGSpecialInstrument()877 FGSpecialInstrument::~FGSpecialInstrument ()
878 {
879 }
880
881 void
draw(osg::State & state)882 FGSpecialInstrument::draw (osg::State& state)
883 {
884 complex->draw(state);
885 }
886
887
888
889 ////////////////////////////////////////////////////////////////////////
890 // Implementation of FGInstrumentLayer.
891 ////////////////////////////////////////////////////////////////////////
892
FGInstrumentLayer(int w,int h)893 FGInstrumentLayer::FGInstrumentLayer (int w, int h)
894 : _w(w),
895 _h(h)
896 {
897 }
898
~FGInstrumentLayer()899 FGInstrumentLayer::~FGInstrumentLayer ()
900 {
901 for (transformation_list::iterator it = _transformations.begin();
902 it != _transformations.end();
903 it++) {
904 delete *it;
905 *it = 0;
906 }
907 }
908
909 void
transform() const910 FGInstrumentLayer::transform () const
911 {
912 transformation_list::const_iterator it = _transformations.begin();
913 transformation_list::const_iterator last = _transformations.end();
914 while (it != last) {
915 FGPanelTransformation *t = *it;
916 if (t->test()) {
917 float val = (t->node == 0 ? 0.0 : t->node->getFloatValue());
918
919 if (t->has_mod)
920 val = fmod(val, t->mod);
921 if (val < t->min) {
922 val = t->min;
923 } else if (val > t->max) {
924 val = t->max;
925 }
926
927 if (t->table==0) {
928 val = val * t->factor + t->offset;
929 } else {
930 val = t->table->interpolate(val) * t->factor + t->offset;
931 }
932
933 switch (t->type) {
934 case FGPanelTransformation::XSHIFT:
935 glTranslatef(val, 0.0, 0.0);
936 break;
937 case FGPanelTransformation::YSHIFT:
938 glTranslatef(0.0, val, 0.0);
939 break;
940 case FGPanelTransformation::ROTATION:
941 glRotatef(-val, 0.0, 0.0, 1.0);
942 break;
943 }
944 }
945 it++;
946 }
947 }
948
949 void
addTransformation(FGPanelTransformation * transformation)950 FGInstrumentLayer::addTransformation (FGPanelTransformation * transformation)
951 {
952 _transformations.push_back(transformation);
953 }
954
955
956
957 ////////////////////////////////////////////////////////////////////////
958 // Implementation of FGGroupLayer.
959 ////////////////////////////////////////////////////////////////////////
960
FGGroupLayer()961 FGGroupLayer::FGGroupLayer ()
962 {
963 }
964
~FGGroupLayer()965 FGGroupLayer::~FGGroupLayer ()
966 {
967 for (unsigned int i = 0; i < _layers.size(); i++)
968 delete _layers[i];
969 }
970
971 void
draw(osg::State & state)972 FGGroupLayer::draw (osg::State& state)
973 {
974 if (test()) {
975 transform();
976 int nLayers = _layers.size();
977 for (int i = 0; i < nLayers; i++)
978 _layers[i]->draw(state);
979 }
980 }
981
982 void
addLayer(FGInstrumentLayer * layer)983 FGGroupLayer::addLayer (FGInstrumentLayer * layer)
984 {
985 _layers.push_back(layer);
986 }
987
988
989
990 ////////////////////////////////////////////////////////////////////////
991 // Implementation of FGTexturedLayer.
992 ////////////////////////////////////////////////////////////////////////
993
994
FGTexturedLayer(const FGCroppedTexture & texture,int w,int h)995 FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture &texture, int w, int h)
996 : FGInstrumentLayer(w, h),
997 _emissive(false)
998 {
999 setTexture(texture);
1000 }
1001
1002
~FGTexturedLayer()1003 FGTexturedLayer::~FGTexturedLayer ()
1004 {
1005 }
1006
1007
1008 void
draw(osg::State & state)1009 FGTexturedLayer::draw (osg::State& state)
1010 {
1011 if (test()) {
1012 int w2 = _w / 2;
1013 int h2 = _h / 2;
1014
1015 transform();
1016 state.pushStateSet(_texture.getTexture());
1017 state.apply();
1018 state.setActiveTextureUnit(0);
1019 state.setClientActiveTextureUnit(0);
1020
1021 glBegin(GL_POLYGON);
1022
1023 if (_emissive) {
1024 glColor4fv( emissive_panel_color );
1025 } else {
1026 // From Curt: turn on the panel
1027 // lights after sundown.
1028 glColor4fv( panel_color );
1029 }
1030
1031 glTexCoord2f(_texture.getMinX(), _texture.getMinY()); glVertex2f(-w2, -h2);
1032 glTexCoord2f(_texture.getMaxX(), _texture.getMinY()); glVertex2f(w2, -h2);
1033 glTexCoord2f(_texture.getMaxX(), _texture.getMaxY()); glVertex2f(w2, h2);
1034 glTexCoord2f(_texture.getMinX(), _texture.getMaxY()); glVertex2f(-w2, h2);
1035 glEnd();
1036 state.popStateSet();
1037 state.apply();
1038 state.setActiveTextureUnit(0);
1039 state.setClientActiveTextureUnit(0);
1040
1041 }
1042 }
1043
1044
1045
1046 ////////////////////////////////////////////////////////////////////////
1047 // Implementation of FGTextLayer.
1048 ////////////////////////////////////////////////////////////////////////
1049
FGTextLayer(int w,int h)1050 FGTextLayer::FGTextLayer (int w, int h)
1051 : FGInstrumentLayer(w, h), _pointSize(14.0), _font_name("Helvetica.txf")
1052 {
1053 _then.stamp();
1054 _color[0] = _color[1] = _color[2] = 0.0;
1055 _color[3] = 1.0;
1056 }
1057
~FGTextLayer()1058 FGTextLayer::~FGTextLayer ()
1059 {
1060 chunk_list::iterator it = _chunks.begin();
1061 chunk_list::iterator last = _chunks.end();
1062 for ( ; it != last; it++) {
1063 delete *it;
1064 }
1065 }
1066
1067 void
draw(osg::State & state)1068 FGTextLayer::draw (osg::State& state)
1069 {
1070 if (test()) {
1071 glColor4fv(_color);
1072 transform();
1073
1074 fntFont* font = FGFontCache::instance()->getTexFont(_font_name);
1075 if (!font) {
1076 return; // don't crash on missing fonts
1077 }
1078
1079 text_renderer.setFont(font);
1080
1081 text_renderer.setPointSize(_pointSize);
1082 text_renderer.begin();
1083 text_renderer.start3f(0, 0, 0);
1084
1085 _now.stamp();
1086 double diff = (_now - _then).toUSecs();
1087
1088 if (diff > 100000 || diff < 0 ) {
1089 // ( diff < 0 ) is a sanity check and indicates our time stamp
1090 // difference math probably overflowed. We can handle a max
1091 // difference of 35.8 minutes since the returned value is in
1092 // usec. So if the panel is left off longer than that we can
1093 // over flow the math with it is turned back on. This (diff <
1094 // 0) catches that situation, get's us out of trouble, and
1095 // back on track.
1096 recalc_value();
1097 _then = _now;
1098 }
1099
1100 // Something is goofy. The code in this file renders only CCW
1101 // polygons, and I have verified that the font code in plib
1102 // renders only CCW trianbles. Yet they come out backwards.
1103 // Something around here or in plib is either changing the winding
1104 // order or (more likely) pushing a left-handed matrix onto the
1105 // stack. But I can't find it; get out the chainsaw...
1106 glFrontFace(GL_CW);
1107 text_renderer.puts((char *)(_value.c_str()));
1108 glFrontFace(GL_CCW);
1109
1110 text_renderer.end();
1111 glColor4f(1.0, 1.0, 1.0, 1.0); // FIXME
1112 }
1113 }
1114
1115 void
addChunk(FGTextLayer::Chunk * chunk)1116 FGTextLayer::addChunk (FGTextLayer::Chunk * chunk)
1117 {
1118 _chunks.push_back(chunk);
1119 }
1120
1121 void
setColor(float r,float g,float b)1122 FGTextLayer::setColor (float r, float g, float b)
1123 {
1124 _color[0] = r;
1125 _color[1] = g;
1126 _color[2] = b;
1127 _color[3] = 1.0;
1128 }
1129
1130 void
setPointSize(float size)1131 FGTextLayer::setPointSize (float size)
1132 {
1133 _pointSize = size;
1134 }
1135
1136 void
setFontName(const std::string & name)1137 FGTextLayer::setFontName(const std::string &name)
1138 {
1139 _font_name = name + ".txf";
1140 fntFont* font = FGFontCache::instance()->getTexFont(_font_name);
1141 if (!font) {
1142 SG_LOG(SG_COCKPIT, SG_WARN, "unable to find font:" << name);
1143 }
1144 }
1145
1146
1147 void
setFont(fntFont * font)1148 FGTextLayer::setFont(fntFont * font)
1149 {
1150 text_renderer.setFont(font);
1151 }
1152
1153
1154 void
recalc_value() const1155 FGTextLayer::recalc_value () const
1156 {
1157 _value = "";
1158 chunk_list::const_iterator it = _chunks.begin();
1159 chunk_list::const_iterator last = _chunks.end();
1160 for ( ; it != last; it++) {
1161 _value += (*it)->getValue();
1162 }
1163 }
1164
1165
1166
1167 ////////////////////////////////////////////////////////////////////////
1168 // Implementation of FGTextLayer::Chunk.
1169 ////////////////////////////////////////////////////////////////////////
1170
Chunk(const std::string & text,const std::string & fmt)1171 FGTextLayer::Chunk::Chunk (const std::string &text, const std::string &fmt)
1172 : _type(FGTextLayer::TEXT),
1173 _fmt(simgear::strutils::sanitizePrintfFormat(fmt))
1174 {
1175 _text = text;
1176 if (_fmt.empty())
1177 _fmt = "%s";
1178 }
1179
Chunk(ChunkType type,const SGPropertyNode * node,const std::string & fmt,float mult,float offs,bool truncation)1180 FGTextLayer::Chunk::Chunk (ChunkType type, const SGPropertyNode * node,
1181 const std::string &fmt, float mult, float offs,
1182 bool truncation)
1183 : _type(type),
1184 _fmt(simgear::strutils::sanitizePrintfFormat(fmt)),
1185 _mult(mult),
1186 _offs(offs),
1187 _trunc(truncation)
1188 {
1189 if (_fmt.empty()) {
1190 if (type == TEXT_VALUE)
1191 _fmt = "%s";
1192 else
1193 _fmt = "%.2f";
1194 }
1195 _node = node;
1196 }
1197
1198 const char *
getValue() const1199 FGTextLayer::Chunk::getValue () const
1200 {
1201 if (test()) {
1202 _buf[0] = '\0';
1203 switch (_type) {
1204 case TEXT:
1205 sprintf(_buf, _fmt.c_str(), _text.c_str());
1206 return _buf;
1207 case TEXT_VALUE:
1208 sprintf(_buf, _fmt.c_str(), _node->getStringValue());
1209 break;
1210 case DOUBLE_VALUE:
1211 double d = _offs + _node->getFloatValue() * _mult;
1212 if (_trunc) d = (d < 0) ? -floor(-d) : floor(d);
1213 sprintf(_buf, _fmt.c_str(), d);
1214 break;
1215 }
1216 return _buf;
1217 } else {
1218 return "";
1219 }
1220 }
1221
1222
1223
1224 ////////////////////////////////////////////////////////////////////////
1225 // Implementation of FGSwitchLayer.
1226 ////////////////////////////////////////////////////////////////////////
1227
FGSwitchLayer()1228 FGSwitchLayer::FGSwitchLayer ()
1229 : FGGroupLayer()
1230 {
1231 }
1232
1233 void
draw(osg::State & state)1234 FGSwitchLayer::draw (osg::State& state)
1235 {
1236 if (test()) {
1237 transform();
1238 int nLayers = _layers.size();
1239 for (int i = 0; i < nLayers; i++) {
1240 if (_layers[i]->test()) {
1241 _layers[i]->draw(state);
1242 return;
1243 }
1244 }
1245 }
1246 }
1247
1248
1249 // end of panel.cxx
1250