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