Debian Codegraph @

src/Cockpit/panel.cxx

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