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