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