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