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