]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD.hxx
initialize variables before using them
[flightgear.git] / src / Instrumentation / HUD / HUD.hxx
1 // HUD.hxx -- Head Up Display
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  [micheleamerica#geocities:com]
6 // Copyright (C) 2006  Melchior FRANZ  [mfranz#aon:at]
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 #ifndef _HUD_HXX
23 #define _HUD_HXX
24
25 #include <simgear/compiler.h>
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <vector>
32 #include <deque>
33 #include STL_FSTREAM
34
35 SG_USING_STD(deque);
36 SG_USING_STD(vector);
37 SG_USING_NAMESPACE(std);
38
39 #include <osg/State>
40
41 #include <plib/sg.h>
42
43 #include <simgear/math/SGLimits.hxx>
44 #include <simgear/constants.h>
45 #include <simgear/structure/subsystem_mgr.hxx>
46
47 #include <Airports/runways.hxx>     // FGRunway
48 #include <GUI/gui.h>                // fntRenderer ?   guiErrorMessage()
49 #include <GUI/new_gui.hxx>          // FGFontCache, FGColor
50 #include <Include/fg_typedefs.h>
51 #include <Main/fg_props.hxx>
52
53
54 class FGViewer;
55 class SGCondition;
56
57
58 class LineSegment {
59 public:
60     LineSegment(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1)
61         : _x0(x0), _y0(y0), _x1(x1), _y1(y1) {}
62
63     void draw() const {
64         glVertex2f(_x0, _y0);
65         glVertex2f(_x1, _y1);
66     }
67
68 private:
69     GLfloat _x0, _y0, _x1, _y1;
70 };
71
72
73
74 class LineList {
75 public:
76     void add(const LineSegment& seg) { _list.push_back(seg); }
77     void erase() { _list.erase(_list.begin(), _list.end()); }
78     inline unsigned int size() const { return _list.size(); }
79     void draw() {
80         glBegin(GL_LINES);
81         vector<LineSegment>::const_iterator it, end = _list.end();
82         for (it = _list.begin(); it != end; ++it)
83             it->draw();
84         glEnd();
85     }
86
87 private:
88     vector<LineSegment> _list;
89 };
90
91
92
93 class HUDText {
94 public:
95     HUDText(fntRenderer *f, float x, float y, const char *s, int align = 0, int digits = 0);
96     void draw();
97
98 private:
99     fntRenderer *_fnt;
100     float _x, _y;
101     int _digits;
102     static const int BUFSIZE = 64;
103     char _msg[BUFSIZE];
104 };
105
106
107
108 class TextList {
109 public:
110     TextList() { _font = 0; }
111
112     void setFont(fntRenderer *Renderer) { _font = Renderer; }
113     void add(float x, float y, const char *s, int align = 0, int digit = 0) {
114         _list.push_back(HUDText(_font, x, y, s, align, digit));
115     }
116     void erase() { _list.erase(_list.begin(), _list.end()); }
117     void align(const char *s, int align, float *x, float *y,
118             float *l, float *r, float *b, float *t) const;
119     void draw();
120
121 private:
122     fntRenderer *_font;
123     vector<HUDText> _list;
124 };
125
126
127
128
129
130
131 class HUD : public SGSubsystem, public SGPropertyChangeListener {
132 public:
133     HUD();
134     ~HUD();
135     void init();
136     void update(double);
137
138     // called from Main/renderer.cxx to draw 2D and 3D HUD
139     void draw(osg::State&);
140
141     // listener callback to read various HUD related properties
142     void valueChanged(SGPropertyNode *);
143     // set current glColor
144     void setColor() const;
145     inline bool isVisible() const { return _visible; }
146     inline bool isAntialiased() const { return _antialiased; }
147     inline bool isTransparent() const { return _transparent; }
148     inline bool is3D() const { return _3Denabled; }
149     inline float alphaClamp() const { return _cl; }
150     inline double timer() const { return _timer; }
151     static void textAlign(fntRenderer *rend, const char *s, int align, float *x, float *y,
152             float *l, float *r, float *b, float *t);
153
154     enum Units { FEET, METER };
155     Units getUnits() const { return _units; }
156
157     enum {
158         HORIZONTAL = 0x0000,  // keep that at zero?
159         VERTICAL   = 0x0001,
160         TOP        = 0x0002,
161         BOTTOM     = 0x0004,
162         LEFT       = 0x0008,
163         RIGHT      = 0x0010,
164         NOTICKS    = 0x0020,
165         NOTEXT     = 0x0040,
166         BOTH       = (LEFT|RIGHT),
167
168         // for alignment (with LEFT, RIGHT, TOP, BOTTOM)
169         HCENTER    = 0x0080,
170         VCENTER    = 0x0100,
171         CENTER     = (HCENTER|VCENTER),
172     };
173
174 protected:
175     void common_draw();
176     int load(const char *, float x = 320.0f, float y = 240.0f,
177             int level = 0, const string& indent = "");
178
179 private:
180     void draw3D();
181     void draw2D(GLfloat, GLfloat, GLfloat, GLfloat);
182
183     class Input;
184     class Item;
185     class Label;
186     class Scale;
187     class Gauge;
188     class Tape;
189     class Dial;
190     class TurnBankIndicator;
191     class Ladder;
192     class Runway;
193     class AimingReticle;
194
195     deque<Item *> _items;
196
197     SGPropertyNode_ptr _current;
198     SGPropertyNode_ptr _visibility;
199     SGPropertyNode_ptr _3DenabledN;
200     SGPropertyNode_ptr _antialiasing;
201     SGPropertyNode_ptr _transparency;
202     SGPropertyNode_ptr _red, _green, _blue, _alpha;
203     SGPropertyNode_ptr _alpha_clamp;
204     SGPropertyNode_ptr _brightness;
205     bool _visible;
206     bool _3Denabled;
207     bool _antialiased;
208     bool _transparent;
209     float _r, _g, _b, _a, _cl;
210
211     SGPropertyNode_ptr _scr_widthN, _scr_heightN;
212     int _scr_width, _scr_height;
213     SGPropertyNode_ptr _unitsN;
214     Units _units;
215     double _timer;
216
217     fntRenderer *_font_renderer;
218     FGFontCache *_font_cache;
219     fntTexFont *_font;
220     float _font_size;
221     int _style;
222
223     TextList _text_list;
224     LineList _line_list;
225     LineList _stipple_line_list;
226 };
227
228
229
230 class HUD::Input {
231 public:
232     Input(const SGPropertyNode *n, float factor = 1.0, float offset = 0.0,
233             float min = -SGLimitsf::max(), float max = SGLimitsf::max()) :
234         _valid(false),
235         _property(0),
236         _damped(SGLimitsf::max())
237     {
238         if (!n)
239             return;
240         _factor = n->getFloatValue("factor", factor);
241         _offset = n->getFloatValue("offset", offset);
242         _min = n->getFloatValue("min", min);
243         _max = n->getFloatValue("max", max);
244         _coeff = 1.0 - 1.0 / powf(10, fabsf(n->getFloatValue("damp", 0.0)));
245         SGPropertyNode *p = ((SGPropertyNode *)n)->getNode("property", false);
246         if (p) {
247             const char *path = p->getStringValue();
248             if (path && path[0]) {
249                 _property = fgGetNode(path, true);
250                 _valid = true;
251             }
252         }
253     }
254
255     bool getBoolValue() const {
256         assert(_property);
257         return _property->getBoolValue();
258     }
259
260     const char *getStringValue() const {
261         assert(_property);
262         return _property->getStringValue();
263     }
264
265     float getFloatValue() {
266         assert(_property);
267         float f = _property->getFloatValue() * _factor + _offset;
268         if (_damped == SGLimitsf::max())
269             _damped = f;
270         if (_coeff > 0.0f)
271             f = _damped = f * (1.0f - _coeff) + _damped * _coeff;
272         return clamp(f);
273     }
274
275     inline float isValid() const { return _valid; }
276     inline float min() const { return _min; }
277     inline float max() const { return _max; }
278     inline float factor() const { return _factor; }
279     float clamp(float v) const { return v < _min ? _min : v > _max ? _max : v; }
280
281     void set_min(float m, bool force = true) {
282         if (force || _min == -SGLimitsf::max())
283             _min = m;
284     }
285     void set_max(float m, bool force = true) {
286         if (force || _max == SGLimitsf::max())
287             _max = m;
288     }
289
290 private:
291     bool _valid;
292     SGConstPropertyNode_ptr _property;
293     float _factor;
294     float _offset;
295     float _min;
296     float _max;
297     float _coeff;
298     float _damped;
299 };
300
301
302
303 class HUD::Item {
304 public:
305     Item(HUD *parent, const SGPropertyNode *, float x = 0.0f, float y = 0.0f);
306     virtual ~Item () {}
307     virtual void draw() = 0;
308     virtual bool isEnabled();
309
310 protected:
311     enum Format {
312         INVALID,
313         NONE,
314         INT,
315         LONG,
316         FLOAT,
317         DOUBLE,
318         STRING,
319     };
320
321     Format  check_format(const char *) const;
322     inline float get_span()       const { return _scr_span; }
323     inline int   get_digits()     const { return _digits; }
324
325     inline bool option_vert()    const { return (_options & VERTICAL) == VERTICAL; }
326     inline bool option_left()    const { return (_options & LEFT) == LEFT; }
327     inline bool option_right()   const { return (_options & RIGHT) == RIGHT; }
328     inline bool option_both()    const { return (_options & BOTH) == BOTH; }
329     inline bool option_noticks() const { return (_options & NOTICKS) == NOTICKS; }
330     inline bool option_notext()  const { return (_options & NOTEXT) == NOTEXT; }
331     inline bool option_top()     const { return (_options & TOP) == TOP; }
332     inline bool option_bottom()  const { return (_options & BOTTOM) == BOTTOM; }
333
334     void draw_line(float x1, float y1, float x2, float y2);
335     void draw_stipple_line(float x1, float y1, float x2, float y2);
336     void draw_text(float x, float y, char *msg, int align = 0, int digit = 0);
337     void draw_circle(float x1, float y1, float r) const;
338     void draw_bullet(float, float, float);
339
340     HUD         *_hud;
341     string      _name;
342     int         _options;
343     float       _x, _y, _w, _h;
344     float       _center_x, _center_y;
345
346 private:
347     SGCondition *_condition;
348     float       _disp_factor;   // Multiply by to get numbers shown on scale.
349     float       _scr_span;      // Working values for draw;
350     int         _digits;
351 };
352
353
354
355 class HUD::Label : public Item {
356 public:
357     Label(HUD *parent, const SGPropertyNode *, float x, float y);
358     virtual void draw();
359
360 private:
361     bool    blink();
362
363     Input   _input;
364     Format  _mode;
365     string  _format;
366     int     _halign;    // HUDText alignment
367     int     _blink;
368     bool    _box;
369     float   _text_y;
370     float   _pointer_width;
371     float   _pointer_length;
372
373     SGCondition *_blink_condition;
374     double  _blink_interval;
375     double  _blink_target;  // time for next blink state change
376     bool    _blink_state;
377 };
378
379
380
381 // abstract base class for both moving scale and moving needle (fixed scale)
382 // indicators.
383 //
384 class HUD::Scale : public Item {
385 public:
386     Scale(HUD *parent, const SGPropertyNode *, float x, float y);
387     virtual void draw    ( void ) {}  // No-op here. Defined in derived classes.
388
389 protected:
390     inline float factor() const { return _display_factor; }
391     inline float range_to_show() const { return _range_shown; }
392
393     Input _input;
394     float _major_divs;      // major division marker units
395     float _minor_divs;      // minor division marker units
396     unsigned int _modulo;   // Roll over point
397
398 private:
399     float _range_shown;     // Width Units.
400     float _display_factor;  // factor => screen units/range values.
401 };
402
403
404 class HUD::Gauge : public Scale {
405 public:
406     Gauge(HUD *parent, const SGPropertyNode *, float x, float y);
407     virtual void draw();
408 };
409
410
411
412 // displays the indicated quantity on a scale that moves past the
413 // pointer. It may be horizontal or vertical.
414 //
415 class HUD::Tape : public Scale {
416 public:
417     Tape(HUD *parent, const SGPropertyNode *, float x, float y);
418     virtual void draw();
419
420 protected:
421     void draw_vertical(float);
422     void draw_horizontal(float);
423     void draw_fixed_pointer(float, float, float, float, float, float);
424     char *format_value(float);
425
426 private:
427     float  _val_span;
428     float  _half_width_units;
429     bool   _draw_tick_bottom;
430     bool   _draw_tick_top;
431     bool   _draw_tick_right;
432     bool   _draw_tick_left;
433     bool   _draw_cap_bottom;
434     bool   _draw_cap_top;
435     bool   _draw_cap_right;
436     bool   _draw_cap_left;
437     float  _marker_offset;
438     float  _label_gap;
439     bool   _pointer;
440     Format _label_fmt;
441     string _format;
442     int    _div_ratio;          // _major_divs/_minor_divs
443     bool   _odd_type;           // whether to put numbers at 0/2/4 or 1/3/5
444
445     enum { BUFSIZE = 64 };
446     char   _buf[BUFSIZE];
447
448     enum PointerType { FIXED, MOVING } _pointer_type;
449     enum TickType { LINE, CIRCLE } _tick_type;
450     enum TickLength { VARIABLE, CONSTANT } _tick_length;
451 };
452
453
454
455 class HUD::Dial : public Scale {
456 public:
457     Dial(HUD *parent, const SGPropertyNode *, float x, float y);
458     virtual void draw();
459
460 private:
461     float  _radius;
462     int    _divisions;
463 };
464
465
466
467 class HUD::TurnBankIndicator : public Item {
468 public:
469     TurnBankIndicator(HUD *parent, const SGPropertyNode *, float x, float y);
470     virtual void draw();
471
472 private:
473     void draw_scale();
474     void draw_tee();
475     void draw_line(float, float, float, float);
476     void draw_tick(float angle, float r1, float r2, int side);
477
478     Input _bank;
479     Input _sideslip;
480
481     float _gap_width;
482     bool  _bank_scale;
483 };
484
485
486
487 class HUD::Ladder : public Item {
488 public:
489     Ladder(HUD *parent, const SGPropertyNode *, float x, float y);
490     virtual void draw();
491
492 private:
493     void draw_zenith(float, float);
494     void draw_nadir(float, float);
495
496     void draw_text(float x, float y, char *s, int align = 0) {
497         _locTextList.add(x, y, s, align, 0);
498     }
499
500     void draw_line(float x1, float y1, float x2, float y2) {
501         _locLineList.add(LineSegment(x1, y1, x2, y2));
502     }
503
504     void draw_stipple_line(float x1, float y1, float x2, float y2) {
505         _locStippleLineList.add(LineSegment(x1, y1, x2, y2));
506     }
507
508     enum   Type { PITCH, CLIMB_DIVE } _type;
509     Input  _pitch;
510     Input  _roll;
511     float  _width_units;
512     int    _div_units;
513     unsigned int _scr_hole;
514     float  _vmax;
515     float  _vmin;
516     float  _compression;
517     bool   _dynamic_origin;
518     bool   _clip_plane;
519     bool   _frl;               // fuselage reference line
520     bool   _target_spot;
521     bool   _target_markers;
522     bool   _velocity_vector;
523     bool   _drift_marker;
524     bool   _alpha_bracket;
525     bool   _energy_marker;
526     bool   _climb_dive_marker;
527     bool   _glide_slope_marker;
528     float  _glide_slope;
529     bool   _energy_worm;
530     bool   _waypoint_marker;
531     bool   _zenith;
532     bool   _nadir;
533     bool   _hat;
534
535     // The Ladder has its own temporary display lists
536     TextList _locTextList;
537     LineList _locLineList;
538     LineList _locStippleLineList;
539 };
540
541
542
543 // responsible for rendering the active runway in the hud (if visible).
544 //
545 class HUD::Runway : public Item {
546 public:
547     Runway(HUD *parent, const SGPropertyNode *, float x, float y);
548     virtual void draw();
549
550 private:
551     void boundPoint(const sgdVec3& v, sgdVec3& m);
552     bool boundOutsidePoints(sgdVec3& v, sgdVec3& m);
553     bool drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& p1, const sgdVec3& p2);
554     void drawArrow();
555     bool get_active_runway(FGRunway& rwy);
556     void get_rwy_points(sgdVec3 *points);
557     void setLineWidth();
558
559     SGPropertyNode_ptr _agl;
560     sgdVec3 _points3d[6], _points2d[6];
561     double _mm[16];
562     double _pm[16];
563     double _arrow_scale;  // scales of runway indication arrow
564     double _arrow_radius;
565     double _line_scale;   // maximum line scale
566     double _scale_dist;   // distance where to start scaling the lines
567     double _default_pitch;
568     double _default_heading;
569     GLint  _view[4];
570     FGRunway _runway;
571     FGViewer* _cockpit_view;
572     unsigned short _stipple_out;    // stipple pattern of the outline of the runway
573     unsigned short _stipple_center; // stipple pattern of the center line of the runway
574     bool   _draw_arrow;             // draw arrow when runway is not visible in HUD
575     bool   _draw_arrow_always;      // always draws arrow
576     float  _left, _right, _top, _bottom;
577 };
578
579
580 class HUD::AimingReticle : public Item {
581 public:
582     AimingReticle(HUD *parent, const SGPropertyNode *, float x, float y);
583     virtual void draw();
584
585 private:
586     SGCondition *_active_condition;  // stadiametric (true) or standby (false)
587     Input   _diameter;               // inner/outer radius relation
588     float   _bullet_size;
589     float   _inner_radius;
590 };
591
592
593
594 #endif // _HUD_HXX